Skip to content

Commit ddb4ecc

Browse files
committed
UI: updated dearimgui/test engine
1 parent e3a43ab commit ddb4ecc

File tree

6 files changed

+110
-33
lines changed

6 files changed

+110
-33
lines changed

src/modules/ui/dearimgui/imgui.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15224,7 +15224,7 @@ static bool ImGui::UpdateTryMergeWindowIntoHostViewports(ImGuiWindow* window)
1522415224

1522515225
// Translate Dear ImGui windows when a Host Viewport has been moved
1522615226
// (This additionally keeps windows at the same place when ImGuiConfigFlags_ViewportsEnable is toggled!)
15227-
void ImGui::TranslateWindowsInViewport(ImGuiViewportP* viewport, const ImVec2& old_pos, const ImVec2& new_pos)
15227+
void ImGui::TranslateWindowsInViewport(ImGuiViewportP* viewport, const ImVec2& old_pos, const ImVec2& new_pos, const ImVec2& old_size, const ImVec2& new_size)
1522815228
{
1522915229
ImGuiContext& g = *GImGui;
1523015230
IM_ASSERT(viewport->Window == NULL && (viewport->Flags & ImGuiViewportFlags_CanHostOtherWindows));
@@ -15238,7 +15238,7 @@ void ImGui::TranslateWindowsInViewport(ImGuiViewportP* viewport, const ImVec2& o
1523815238
ImRect test_still_fit_rect(old_pos, old_pos + viewport->Size);
1523915239
ImVec2 delta_pos = new_pos - old_pos;
1524015240
for (ImGuiWindow* window : g.Windows) // FIXME-OPT
15241-
if (translate_all_windows || (window->Viewport == viewport && test_still_fit_rect.Contains(window->Rect())))
15241+
if (translate_all_windows || (window->Viewport == viewport && (old_size == new_size || test_still_fit_rect.Contains(window->Rect()))))
1524215242
TranslateWindow(window, delta_pos);
1524315243
}
1524415244

@@ -15415,7 +15415,7 @@ static void ImGui::UpdateViewportsNewFrame()
1541515415
// (This additionally keeps windows at the same place when ImGuiConfigFlags_ViewportsEnable is toggled!)
1541615416
const ImVec2 viewport_delta_pos = viewport->Pos - viewport->LastPos;
1541715417
if ((viewport->Flags & ImGuiViewportFlags_CanHostOtherWindows) && (viewport_delta_pos.x != 0.0f || viewport_delta_pos.y != 0.0f))
15418-
TranslateWindowsInViewport(viewport, viewport->LastPos, viewport->Pos);
15418+
TranslateWindowsInViewport(viewport, viewport->LastPos, viewport->Pos, viewport->LastSize, viewport->Size);
1541915419

1542015420
// Update DPI scale
1542115421
float new_dpi_scale;
@@ -15525,6 +15525,7 @@ static void ImGui::UpdateViewportsEndFrame()
1552515525
{
1552615526
ImGuiViewportP* viewport = g.Viewports[i];
1552715527
viewport->LastPos = viewport->Pos;
15528+
viewport->LastSize = viewport->Size;
1552815529
if (viewport->LastFrameActive < g.FrameCount || viewport->Size.x <= 0.0f || viewport->Size.y <= 0.0f)
1552915530
if (i > 0) // Always include main viewport in the list
1553015531
continue;
@@ -15571,7 +15572,7 @@ ImGuiViewportP* ImGui::AddUpdateViewport(ImGuiWindow* window, ImGuiID id, const
1557115572
viewport->ID = id;
1557215573
viewport->Idx = g.Viewports.Size;
1557315574
viewport->Pos = viewport->LastPos = pos;
15574-
viewport->Size = size;
15575+
viewport->Size = viewport->LastSize = size;
1557515576
viewport->Flags = flags;
1557615577
UpdateViewportPlatformMonitor(viewport);
1557715578
g.Viewports.push_back(viewport);

src/modules/ui/dearimgui/imgui_internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,7 @@ struct ImGuiViewportP : public ImGuiViewport
19681968
int LastFocusedStampCount; // Last stamp number from when a window hosted by this viewport was focused (by comparing this value between two viewport we have an implicit viewport z-order we use as fallback)
19691969
ImGuiID LastNameHash;
19701970
ImVec2 LastPos;
1971+
ImVec2 LastSize;
19711972
float Alpha; // Window opacity (when dragging dockable windows/viewports we make them transparent)
19721973
float LastAlpha;
19731974
bool LastFocusedHadNavWindow;// Instead of maintaining a LastFocusedWindow (which may harder to correctly maintain), we merely store weither NavWindow != NULL last time the viewport was focused.
@@ -3411,7 +3412,7 @@ namespace ImGui
34113412
IMGUI_API void CallContextHooks(ImGuiContext* context, ImGuiContextHookType type);
34123413

34133414
// Viewports
3414-
IMGUI_API void TranslateWindowsInViewport(ImGuiViewportP* viewport, const ImVec2& old_pos, const ImVec2& new_pos);
3415+
IMGUI_API void TranslateWindowsInViewport(ImGuiViewportP* viewport, const ImVec2& old_pos, const ImVec2& new_pos, const ImVec2& old_size, const ImVec2& new_size);
34153416
IMGUI_API void ScaleWindowsInViewport(ImGuiViewportP* viewport, float scale);
34163417
IMGUI_API void DestroyPlatformWindow(ImGuiViewportP* viewport);
34173418
IMGUI_API void SetWindowViewport(ImGuiWindow* window, ImGuiViewportP* viewport);

src/modules/ui/dearimgui/imgui_test_engine/imgui_te_context.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3792,7 +3792,31 @@ ImGuiID ImGuiTestContext::PopupGetWindowID(ImGuiTestRef ref)
37923792
}
37933793

37943794
#ifdef IMGUI_HAS_VIEWPORT
3795-
// Simulate a platform focus WITHOUT a click perceived by dear imgui. Similare to clicking on Platform title bar.
3795+
void ImGuiTestContext::ViewportPlatform_SetWindowPos(ImGuiViewport* viewport, const ImVec2& pos)
3796+
{
3797+
if (IsError())
3798+
return;
3799+
3800+
IMGUI_TEST_CONTEXT_REGISTER_DEPTH(this);
3801+
LogDebug("ViewportPlatform_SetWindowPos(0x%08X, {%.2f,%.2f)", viewport->ID, pos.x, pos.y);
3802+
Inputs->Queue.push_back(ImGuiTestInput::ForViewportSetPos(viewport->ID, pos)); // Queued since this will poke into backend, best to do in main thread.
3803+
Yield(); // Submit to Platform
3804+
Yield(); // Let Dear ImGui next frame see it
3805+
}
3806+
3807+
void ImGuiTestContext::ViewportPlatform_SetWindowSize(ImGuiViewport* viewport, const ImVec2& size)
3808+
{
3809+
if (IsError())
3810+
return;
3811+
3812+
IMGUI_TEST_CONTEXT_REGISTER_DEPTH(this);
3813+
LogDebug("ViewportPlatform_SetWindowSize(0x%08X, {%.2f,%.2f)", viewport->ID, size.x, size.y);
3814+
Inputs->Queue.push_back(ImGuiTestInput::ForViewportSetSize(viewport->ID, size)); // Queued since this will poke into backend, best to do in main thread.
3815+
Yield(); // Submit to Platform
3816+
Yield(); // Let Dear ImGui next frame see it
3817+
}
3818+
3819+
// Simulate a platform focus WITHOUT a click perceived by dear imgui. Similar to clicking on Platform title bar.
37963820
void ImGuiTestContext::ViewportPlatform_SetWindowFocus(ImGuiViewport* viewport)
37973821
{
37983822
if (IsError())

src/modules/ui/dearimgui/imgui_test_engine/imgui_te_context.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,8 @@ struct IMGUI_API ImGuiTestContext
483483
// IMPORTANT: Those function may alter Platform state (unless using the "Mock Viewport" backend). Use carefully.
484484
// Those are mostly useful to simulate OS actions and testing of viewport-specific features, may not be useful to most users.
485485
#ifdef IMGUI_HAS_VIEWPORT
486-
//void ViewportPlatform_SetWindowPos(ImGuiViewport* viewport, const ImVec2& pos);
487-
//void ViewportPlatform_SetWindowSize(ImGuiViewport* viewport, const ImVec2& size);
486+
void ViewportPlatform_SetWindowPos(ImGuiViewport* viewport, const ImVec2& pos);
487+
void ViewportPlatform_SetWindowSize(ImGuiViewport* viewport, const ImVec2& size);
488488
void ViewportPlatform_SetWindowFocus(ImGuiViewport* viewport);
489489
void ViewportPlatform_CloseWindow(ImGuiViewport* viewport);
490490
#endif

src/modules/ui/dearimgui/imgui_test_engine/imgui_te_engine.cpp

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -658,39 +658,69 @@ void ImGuiTestEngine_ApplyInputToImGuiContext(ImGuiTestEngine* engine)
658658
io.AddInputCharacter(input.Char);
659659
break;
660660
}
661+
#ifdef IMGUI_HAS_VIEWPORT
661662
case ImGuiTestInputType_ViewportFocus:
662663
{
663-
#ifdef IMGUI_HAS_VIEWPORT
664-
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
665-
{
666-
IM_ASSERT(engine->TestContext != NULL);
667-
ImGuiViewport* viewport = ImGui::FindViewportByID(input.ViewportId);
668-
if (viewport == NULL)
669-
engine->TestContext->LogError("ViewportPlatform_SetWindowFocus(%08X): cannot find viewport anymore!", input.ViewportId);
670-
else if (platform_io.Platform_SetWindowSize == NULL)
671-
engine->TestContext->LogError("ViewportPlatform_SetWindowFocus(%08X): backend's Platform_SetWindowSize() is not set", input.ViewportId);
672-
else
673-
platform_io.Platform_SetWindowFocus(viewport);
674-
}
675-
#endif
664+
if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) == 0)
665+
break;
666+
IM_ASSERT(engine->TestContext != NULL);
667+
ImGuiViewport* viewport = ImGui::FindViewportByID(input.ViewportId);
668+
if (viewport == NULL)
669+
engine->TestContext->LogError("ViewportPlatform_SetWindowFocus(%08X): cannot find viewport anymore!", input.ViewportId);
670+
else if (platform_io.Platform_SetWindowFocus == NULL)
671+
engine->TestContext->LogError("ViewportPlatform_SetWindowFocus(%08X): backend's Platform_SetWindowFocus() is not set", input.ViewportId);
672+
else
673+
platform_io.Platform_SetWindowFocus(viewport);
674+
break;
675+
}
676+
case ImGuiTestInputType_ViewportSetPos:
677+
{
678+
if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) == 0)
679+
break;
680+
IM_ASSERT(engine->TestContext != NULL);
681+
ImGuiViewport* viewport = ImGui::FindViewportByID(input.ViewportId);
682+
if (viewport == NULL)
683+
engine->TestContext->LogError("ViewportPlatform_SetWindowPos(%08X): cannot find viewport anymore!", input.ViewportId);
684+
else if (platform_io.Platform_SetWindowPos == NULL)
685+
engine->TestContext->LogError("ViewportPlatform_SetWindowPos(%08X): backend's Platform_SetWindowPos() is not set", input.ViewportId);
686+
else
687+
platform_io.Platform_SetWindowPos(viewport, input.ViewportPosSize);
688+
break;
689+
}
690+
case ImGuiTestInputType_ViewportSetSize:
691+
{
692+
if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) == 0)
693+
break;
694+
IM_ASSERT(engine->TestContext != NULL);
695+
ImGuiViewport* viewport = ImGui::FindViewportByID(input.ViewportId);
696+
if (viewport == NULL)
697+
engine->TestContext->LogError("ViewportPlatform_SetWindowSize(%08X): cannot find viewport anymore!", input.ViewportId);
698+
else if (platform_io.Platform_SetWindowPos == NULL)
699+
engine->TestContext->LogError("ViewportPlatform_SetWindowSize(%08X): backend's Platform_SetWindowSize() is not set", input.ViewportId);
700+
else
701+
platform_io.Platform_SetWindowSize(viewport, input.ViewportPosSize);
676702
break;
677703
}
678704
case ImGuiTestInputType_ViewportClose:
679705
{
680-
#ifdef IMGUI_HAS_VIEWPORT
681-
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
682-
{
683-
IM_ASSERT(engine->TestContext != NULL);
684-
ImGuiViewport* viewport = ImGui::FindViewportByID(input.ViewportId);
685-
if (viewport == NULL)
686-
engine->TestContext->LogError("ViewportPlatform_CloseWindow(%08X): cannot find viewport anymore!", input.ViewportId);
687-
else
688-
viewport->PlatformRequestClose = true;
689-
// FIXME: doesn't apply to actual backend
690-
}
691-
#endif
706+
if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) == 0)
707+
break;
708+
IM_ASSERT(engine->TestContext != NULL);
709+
ImGuiViewport* viewport = ImGui::FindViewportByID(input.ViewportId);
710+
if (viewport == NULL)
711+
engine->TestContext->LogError("ViewportPlatform_CloseWindow(%08X): cannot find viewport anymore!", input.ViewportId);
712+
else
713+
viewport->PlatformRequestClose = true;
714+
// FIXME: doesn't apply to actual backend
692715
break;
693716
}
717+
#else
718+
case ImGuiTestInputType_ViewportFocus:
719+
case ImGuiTestInputType_ViewportSetPos:
720+
case ImGuiTestInputType_ViewportSetSize:
721+
case ImGuiTestInputType_ViewportClose:
722+
break;
723+
#endif
694724
case ImGuiTestInputType_None:
695725
default:
696726
break;

src/modules/ui/dearimgui/imgui_test_engine/imgui_te_internal.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ enum ImGuiTestInputType
6767
ImGuiTestInputType_Key,
6868
ImGuiTestInputType_Char,
6969
ImGuiTestInputType_ViewportFocus,
70+
ImGuiTestInputType_ViewportSetPos,
71+
ImGuiTestInputType_ViewportSetSize,
7072
ImGuiTestInputType_ViewportClose
7173
};
7274

@@ -78,6 +80,7 @@ struct ImGuiTestInput
7880
ImWchar Char = 0;
7981
bool Down = false;
8082
ImGuiID ViewportId = 0;
83+
ImVec2 ViewportPosSize;
8184

8285
static ImGuiTestInput ForKeyChord(ImGuiKeyChord key_chord, bool down)
8386
{
@@ -104,6 +107,24 @@ struct ImGuiTestInput
104107
return inp;
105108
}
106109

110+
static ImGuiTestInput ForViewportSetPos(ImGuiID viewport_id, const ImVec2& pos)
111+
{
112+
ImGuiTestInput inp;
113+
inp.Type = ImGuiTestInputType_ViewportSetPos;
114+
inp.ViewportId = viewport_id;
115+
inp.ViewportPosSize = pos;
116+
return inp;
117+
}
118+
119+
static ImGuiTestInput ForViewportSetSize(ImGuiID viewport_id, const ImVec2& size)
120+
{
121+
ImGuiTestInput inp;
122+
inp.Type = ImGuiTestInputType_ViewportSetSize;
123+
inp.ViewportId = viewport_id;
124+
inp.ViewportPosSize = size;
125+
return inp;
126+
}
127+
107128
static ImGuiTestInput ForViewportClose(ImGuiID viewport_id)
108129
{
109130
ImGuiTestInput inp;

0 commit comments

Comments
 (0)