Skip to content

Commit 3e0c0c5

Browse files
committed
Add IsEditorHovered
- This should help ensure that node editor interactions don't happen when the mouse is outside the canvas, or the canvas is blocked by another window.
1 parent c3c10fc commit 3e0c0c5

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

example/color_node_editor.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,10 +691,11 @@ class ColorNodeEditor
691691
imnodes::Link(iter->first, iter->second.from, iter->second.to);
692692
}
693693

694-
imnodes::EndNodeEditor();
695-
696694
const bool open_popup =
697-
ImGui::IsMouseClicked(1) || ImGui::IsKeyReleased(SDL_SCANCODE_A);
695+
ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows) &&
696+
imnodes::IsEditorHovered() && ImGui::IsKeyReleased(SDL_SCANCODE_A);
697+
698+
imnodes::EndNodeEditor();
698699

699700
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(8.f, 8.f));
700701
if (!ImGui::IsAnyItemHovered() && open_popup)

example/multi_editor.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ void show_editor(const char* editor_name, Editor& editor)
7171
imnodes::Link(link.id, link.start_attr, link.end_attr);
7272
}
7373

74+
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows) &&
75+
imnodes::IsEditorHovered() && ImGui::IsKeyReleased(SDL_SCANCODE_A))
76+
{
77+
const int node_id = ++editor.current_id;
78+
imnodes::SetNodeScreenSpacePos(node_id, ImGui::GetMousePos());
79+
editor.nodes.push_back(Node(node_id, 0.f));
80+
}
81+
7482
imnodes::EndNodeEditor();
7583

7684
{
@@ -97,14 +105,6 @@ void show_editor(const char* editor_name, Editor& editor)
97105
}
98106
}
99107

100-
if (ImGui::IsKeyReleased(SDL_SCANCODE_A) &&
101-
ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows))
102-
{
103-
const int node_id = ++editor.current_id;
104-
imnodes::SetNodeScreenSpacePos(node_id, ImGui::GetMousePos());
105-
editor.nodes.push_back(Node(node_id, 0.f));
106-
}
107-
108108
ImGui::End();
109109
}
110110

imnodes.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,12 @@ void SetNodeDraggable(int node_id, const bool draggable)
18581858
node.draggable = draggable;
18591859
}
18601860

1861+
bool IsEditorHovered()
1862+
{
1863+
return g.canvas_rect_screen_space.Contains(ImGui::GetMousePos()) &&
1864+
ImGui::IsWindowHovered();
1865+
}
1866+
18611867
bool IsNodeHovered(int* const node_id)
18621868
{
18631869
assert(g.current_scope == Scope_None);

imnodes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ void SetNodeGridSpacePos(int node_id, const ImVec2& grid_pos);
187187
// Enable or disable the ability to click and drag a specific node.
188188
void SetNodeDraggable(int node_id, const bool draggable);
189189

190+
// Returns true if the current node editor canvas is being hovered over by the
191+
// mouse, and is not blocked by any other windows.
192+
bool IsEditorHovered();
190193
// The following functions return true if a UI element is being hovered over by
191194
// the mouse cursor. Assigns the id of the UI element being hovered over to the
192195
// function argument. Use these functions after EndNodeEditor() has been called.

0 commit comments

Comments
 (0)