Skip to content

Commit 4ea96b1

Browse files
committed
Example code for reproducing thedmd/imgui-node-editor#205
0 parents  commit 4ea96b1

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build/
2+
cmake-build-*/
3+
external/*
4+
.idea/

CMakeLists.txt

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
project(node_clipping_issue)
3+
set(CMAKE_CXX_STANDARD 17)
4+
5+
if (NOT IS_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/external/imgui)
6+
message(FATAL_ERROR "Please run ./fetch_external.sh in order to populate external/")
7+
endif()
8+
9+
##########################################################
10+
# Add ImGui & HelloImGui
11+
##########################################################
12+
# i. HelloImGui will build ImGui as part of its build (and here, we tell it to use our own version in external/imgui)
13+
set(HELLOIMGUI_IMGUI_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/external/imgui")
14+
add_subdirectory(external/hello_imgui)
15+
# ii. HelloImGui provides a function `hello_imgui_add_app` available in order to easily add applications
16+
list(APPEND CMAKE_MODULE_PATH ${HELLOIMGUI_CMAKE_PATH})
17+
include(hello_imgui_add_app)
18+
19+
20+
##########################################################
21+
# Add imgui-node-editor
22+
##########################################################
23+
set(imgui_node_editor_dir ${CMAKE_CURRENT_LIST_DIR}/external/imgui-node-editor)
24+
file(GLOB imgui_node_editor_src ${imgui_node_editor_dir}/*.cpp ${imgui_node_editor_dir}/*.h)
25+
add_library(imgui_node_editor ${imgui_node_editor_src})
26+
target_include_directories(imgui_node_editor PUBLIC ${imgui_node_editor_dir})
27+
target_link_libraries(imgui_node_editor PUBLIC imgui)
28+
29+
##########################################################
30+
# Build your app
31+
##########################################################
32+
hello_imgui_add_app(node_clipping_issue node_clipping_issue.cpp)
33+
target_link_libraries(node_clipping_issue PRIVATE imgui_node_editor)

Readme.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Example code for reproducing https://github.com/thedmd/imgui-node-editor/issues/205
2+
3+
## Build instructions
4+
5+
```
6+
./fetch_external.sh
7+
mkdir build
8+
cd build
9+
cmake ..
10+
make -j
11+
```
12+
13+
## Code explanation
14+
15+
This code is self-contained in a short file: [node_clipping_issue.cpp](node_clipping_issue.cpp)
16+
17+
It uses HelloImGui in order to simplify the example creation. HelloImGui is based on a quite recent version of the ImGui docking branch.
18+
19+
It was not possible to use the ImGui version inside node editor, since it is outdated, and adds stack layout patches to ImGui which are not trivial to rebase.
20+
21+
## Fixes applied to imgui-node-editor (based on the develop branch)
22+
23+
#### [remove duplicate ImVec2 operator-](https://github.com/pthom/imgui-node-editor/commit/0da24eb1bbc4355113509ab519d37b1f856660c0)
24+
(operator- is now provided by imgui)
25+
26+
#### [ed::CreateItemAction::Begin: if (m_CurrentStage == None) => m_InActive = false](https://github.com/pthom/imgui-node-editor/commit/2ba8ca4f3a36ee086950d98b34422c81e550722c)
27+
(Undo this commit to see the related issue: execution will fail at startup)
28+
29+
Workaround another unrelated issue that is triggered by this example.
30+
31+
#### [Fix #205: workaround clipping issues when link with docked windows](https://github.com/pthom/imgui-node-editor/commit/2265a4bad141a07478292186eb282c85a4bb6991)
32+
(Undo this commit to see the issue: clipping issues will appear when creating links)
33+
34+
This is a workaround for
35+
https://github.com/thedmd/imgui-node-editor/issues/205
36+
https://github.com/pthom/imgui_bundle/issues/117
37+
Adding a simple pixel at m_WidgetRect.Max is enough to bypass the issue.
38+
39+
This is not a real fix, since it merely adds a fake pixel at (xmax, ymax), so that the app knows where the clipping should end. A better solution would be required.

fetch_external.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
THIS_DIR=$(dirname "$0")
2+
echo THIS_DIR=$THIS_DIR
3+
mkdir -p external
4+
cd $THIS_DIR/external
5+
6+
git clone -b docking https://github.com/ocornut/imgui.git
7+
git clone -b master https://github.com/pthom/hello_imgui.git
8+
9+
# Clone imgui-node-editor
10+
#git clone -b develop https://github.com/thedmd/imgui-node-editor.git # official version
11+
git clone -b fix_win_clipping https://github.com/pthom/imgui-node-editor.git # fork with fixes + workaround

node_clipping_issue.cpp

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include "imgui.h"
2+
#include "imgui_node_editor.h"
3+
#include "hello_imgui/hello_imgui.h"
4+
5+
namespace ed = ax::NodeEditor;
6+
7+
// A simple utility to get reproducible ids at each frame
8+
struct IdCounter
9+
{
10+
int _next_id = 0;
11+
int id()
12+
{
13+
_next_id++;
14+
return _next_id;
15+
}
16+
void reset(){
17+
_next_id = 0;
18+
}
19+
};
20+
21+
22+
// this is the main Gui function: it draws two nodes and enables to initiate a link
23+
void gui()
24+
{
25+
static IdCounter next_id;
26+
static IdCounter next_link_id;
27+
next_id.reset();
28+
next_link_id.reset();
29+
30+
ImGui::SetNextWindowSize(ImVec2(0.0f, 0.0f));
31+
ed::Begin("My Editor");
32+
33+
ed::BeginNode(next_id.id());
34+
ImGui::Text("Node 1");
35+
ed::BeginPin(next_id.id(), ed::PinKind::Output);
36+
ImGui::Text("Out ->");
37+
ed::EndPin();
38+
ed::EndNode();
39+
40+
ed::BeginNode(next_id.id());
41+
ImGui::Text("Node 2");
42+
ed::BeginPin(next_id.id(), ed::PinKind::Input);
43+
ImGui::Text("-> In");
44+
ed::EndPin();
45+
ed::EndNode();
46+
47+
if (ed::BeginCreate())
48+
{
49+
ed::PinId input_pin_id;
50+
ed::PinId output_pin_id;
51+
if (ed::QueryNewLink(&input_pin_id, &output_pin_id))
52+
{
53+
if (ed::AcceptNewItem())
54+
ed::Link(next_link_id.id(), input_pin_id, output_pin_id);
55+
}
56+
ed::EndCreate();
57+
}
58+
59+
ed::End();
60+
}
61+
62+
63+
int main(int, char**)
64+
{
65+
// Prepare the node editor context
66+
ed::Config gNodeEditorConfig;
67+
ed::EditorContext *gNodeEditorContext = ed::CreateEditor(&gNodeEditorConfig);
68+
ed::SetCurrentEditor(gNodeEditorContext);
69+
70+
// We use HelloImGui, with a DockSpace (since the issue will occur with dockable windows)
71+
HelloImGui::RunnerParams runner_params;
72+
runner_params.imGuiWindowParams.defaultImGuiWindowType = HelloImGui::DefaultImGuiWindowType::ProvideFullScreenDockSpace;
73+
// runner_params.imGuiWindowParams.enableViewports = true;
74+
75+
// We create a dockable window in HelloImGui
76+
HelloImGui::DockableWindow dockableWindow;
77+
{
78+
dockableWindow.GuiFunction = gui;
79+
dockableWindow.label = "graph";
80+
dockableWindow.dockSpaceName = "MainDockSpace";
81+
}
82+
runner_params.dockingParams.dockableWindows = { dockableWindow };
83+
84+
// Run the app
85+
HelloImGui::Run(runner_params);
86+
87+
// Destroy node editor context
88+
ed::DestroyEditor(gNodeEditorContext);
89+
90+
return 0;
91+
}

0 commit comments

Comments
 (0)