Skip to content

Commit 8b17842

Browse files
committed
[Bundle] : call ImGuiContextHookType_Begin/EndWindow
cf thedmd#242 (comment) This helps fixing issues in popup positionning inside the node editor
1 parent 8584d97 commit 8b17842

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

imgui_canvas.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,60 @@ bool ImGuiEx::Canvas::Begin(ImGuiID id, const ImVec2& size)
145145

146146
m_InBeginEnd = true;
147147

148+
// [ADAPT_IMGUI_BUNDLE]: added ImGuiContextHookType_BeginWindow, ImGuiContextHookType_EndWindow, cf https://github.com/thedmd/imgui-node-editor/issues/242#issuecomment-1681806764
149+
{
150+
auto beginWindowHook = ImGuiContextHook{};
151+
beginWindowHook.UserData = this;
152+
beginWindowHook.Type = ImGuiContextHookType_BeginWindow;
153+
beginWindowHook.Callback = []( ImGuiContext * context, ImGuiContextHook * hook )
154+
{
155+
//ImGui::SetNextWindowViewport( ImGui::GetCurrentWindow()->Viewport->ID );
156+
157+
auto canvas = reinterpret_cast< Canvas * >( hook->UserData );
158+
if ( canvas->m_SuspendCounter == 0 )
159+
{
160+
if ( ( context->NextWindowData.Flags & ImGuiNextWindowDataFlags_HasPos ) != 0 )
161+
{
162+
auto pos = canvas->FromLocal( context->NextWindowData.PosVal );
163+
ImGui::SetNextWindowPos( pos, context->NextWindowData.PosCond, context->NextWindowData.PosPivotVal );
164+
}
165+
166+
if ( context->BeginPopupStack.size() )
167+
{
168+
auto & popup = context->BeginPopupStack.back();
169+
popup.OpenPopupPos = canvas->FromLocal( popup.OpenPopupPos );
170+
popup.OpenMousePos = canvas->FromLocal( popup.OpenMousePos );
171+
}
172+
173+
if ( context->OpenPopupStack.size() )
174+
{
175+
auto & popup = context->OpenPopupStack.back();
176+
popup.OpenPopupPos = canvas->FromLocal( popup.OpenPopupPos );
177+
popup.OpenMousePos = canvas->FromLocal( popup.OpenMousePos );
178+
}
179+
180+
}
181+
canvas->m_BeginWindowCursorBackup = ImGui::GetCursorScreenPos();
182+
canvas->Suspend();
183+
};
184+
185+
m_beginWindowHook = ImGui::AddContextHook( ImGui::GetCurrentContext(), &beginWindowHook );
186+
187+
auto endWindowHook = ImGuiContextHook{};
188+
endWindowHook.UserData = this;
189+
endWindowHook.Type = ImGuiContextHookType_EndWindow;
190+
endWindowHook.Callback = []( ImGuiContext * ctx, ImGuiContextHook * hook )
191+
{
192+
auto canvas = reinterpret_cast< Canvas * >( hook->UserData );
193+
canvas->Resume();
194+
ImGui::SetCursorScreenPos( canvas->m_BeginWindowCursorBackup );
195+
ImGui::GetCurrentWindow()->DC.IsSetPos = false;
196+
};
197+
198+
m_endWindowHook = ImGui::AddContextHook( ImGui::GetCurrentContext(), &endWindowHook );
199+
}
200+
// [/ADAPT_IMGUI_BUNDLE]
201+
148202
return true;
149203
}
150204

@@ -180,6 +234,14 @@ void ImGuiEx::Canvas::End()
180234
//m_DrawList->AddRect(m_WidgetPosition - ImVec2(1.0f, 1.0f), m_WidgetPosition + m_WidgetSize + ImVec2(1.0f, 1.0f), IM_COL32(196, 0, 0, 255));
181235

182236
m_InBeginEnd = false;
237+
238+
// [ADAPT_IMGUI_BUNDLE]: added ImGuiContextHookType_BeginWindow, ImGuiContextHookType_EndWindow, cf https://github.com/thedmd/imgui-node-editor/issues/242#issuecomment-1681806764
239+
{
240+
ImGui::RemoveContextHook( ImGui::GetCurrentContext(), m_beginWindowHook );
241+
ImGui::RemoveContextHook( ImGui::GetCurrentContext(), m_endWindowHook );
242+
}
243+
// [/ADAPT_IMGUI_BUNDLE]
244+
183245
}
184246

185247
void ImGuiEx::Canvas::SetView(const ImVec2& origin, float scale)

imgui_canvas.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,12 @@ struct Canvas
261261
ImVec2 m_ViewportWorkPosBackup;
262262
ImVec2 m_ViewportWorkSizeBackup;
263263
# endif
264-
};
264+
265+
// [ADAPT_IMGUI_BUNDLE]: added ImGuiContextHookType_BeginWindow, ImGuiContextHookType_EndWindow, cf https://github.com/thedmd/imgui-node-editor/issues/242#issuecomment-1681806764
266+
ImGuiID m_beginWindowHook, m_endWindowHook;
267+
ImVec2 m_BeginWindowCursorBackup;
268+
// [/ADAPT_IMGUI_BUNDLE]
269+
};
265270

266271
} // namespace ImGuiEx
267272

0 commit comments

Comments
 (0)