Skip to content

Commit d20e3ee

Browse files
committed
Tests: Adding imgui-test engine hooks (experimental) to provide missing widget state to the testing system.
1 parent 125e624 commit d20e3ee

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

imgui.cpp

+6-14
Original file line numberDiff line numberDiff line change
@@ -957,14 +957,6 @@ static void UpdateMouseWheel();
957957
static void UpdateManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4]);
958958
}
959959

960-
// Test engine hooks (imgui-test)
961-
//#define IMGUI_ENABLE_TEST_ENGINE_HOOKS
962-
#ifdef IMGUI_ENABLE_TEST_ENGINE_HOOKS
963-
extern void ImGuiTestEngineHook_PreNewFrame();
964-
extern void ImGuiTestEngineHook_PostNewFrame();
965-
extern void ImGuiTestEngineHook_ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg);
966-
#endif
967-
968960
//-----------------------------------------------------------------------------
969961
// [SECTION] CONTEXT AND MEMORY ALLOCATORS
970962
//-----------------------------------------------------------------------------
@@ -2607,10 +2599,6 @@ void ImGui::ItemSize(const ImRect& bb, float text_offset_y)
26072599
// declare their minimum size requirement to ItemSize() and then use a larger region for drawing/interaction, which is passed to ItemAdd().
26082600
bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg)
26092601
{
2610-
#ifdef IMGUI_ENABLE_TEST_ENGINE_HOOKS
2611-
ImGuiTestEngineHook_ItemAdd(bb, id, nav_bb_arg);
2612-
#endif
2613-
26142602
ImGuiContext& g = *GImGui;
26152603
ImGuiWindow* window = g.CurrentWindow;
26162604

@@ -2632,6 +2620,10 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg)
26322620
window->DC.LastItemRect = bb;
26332621
window->DC.LastItemStatusFlags = 0;
26342622

2623+
#ifdef IMGUI_ENABLE_TEST_ENGINE
2624+
ImGuiTestEngineHook_ItemAdd(id, bb);
2625+
#endif
2626+
26352627
// Clipping test
26362628
const bool is_clipped = IsClippedEx(bb, id, false);
26372629
if (is_clipped)
@@ -3130,7 +3122,7 @@ void ImGui::NewFrame()
31303122
IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() or ImGui::SetCurrentContext()?");
31313123
ImGuiContext& g = *GImGui;
31323124

3133-
#ifdef IMGUI_ENABLE_TEST_ENGINE_HOOKS
3125+
#ifdef IMGUI_ENABLE_TEST_ENGINE
31343126
ImGuiTestEngineHook_PreNewFrame();
31353127
#endif
31363128

@@ -3305,7 +3297,7 @@ void ImGui::NewFrame()
33053297
SetNextWindowSize(ImVec2(400,400), ImGuiCond_FirstUseEver);
33063298
Begin("Debug##Default");
33073299

3308-
#ifdef IMGUI_ENABLE_TEST_ENGINE_HOOKS
3300+
#ifdef IMGUI_ENABLE_TEST_ENGINE
33093301
ImGuiTestEngineHook_PostNewFrame();
33103302
#endif
33113303
}

imgui_internal.h

+20
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,14 @@ enum ImGuiItemStatusFlags_
314314
ImGuiItemStatusFlags_HoveredRect = 1 << 0,
315315
ImGuiItemStatusFlags_HasDisplayRect = 1 << 1,
316316
ImGuiItemStatusFlags_Edited = 1 << 2 // Value exposed by item was edited in the current frame (should match the bool return value of most widgets)
317+
318+
#ifdef IMGUI_ENABLE_TEST_ENGINE
319+
, // [imgui-test only]
320+
ImGuiItemStatusFlags_Openable = 1 << 10, //
321+
ImGuiItemStatusFlags_Opened = 1 << 11, //
322+
ImGuiItemStatusFlags_Checkable = 1 << 12, //
323+
ImGuiItemStatusFlags_Checked = 1 << 13 //
324+
#endif
317325
};
318326

319327
// FIXME: this is in development, not exposed/functional as a generic feature yet.
@@ -1329,6 +1337,18 @@ IMGUI_API void ImFontAtlasBuildFinish(ImFontAtlas* atlas);
13291337
IMGUI_API void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_multiply_factor);
13301338
IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride);
13311339

1340+
// Test engine hooks (imgui-test)
1341+
//#define IMGUI_ENABLE_TEST_ENGINE
1342+
#ifdef IMGUI_ENABLE_TEST_ENGINE
1343+
extern void ImGuiTestEngineHook_PreNewFrame();
1344+
extern void ImGuiTestEngineHook_PostNewFrame();
1345+
extern void ImGuiTestEngineHook_ItemAdd(ImGuiID id, const ImRect& bb);
1346+
extern void ImGuiTestEngineHook_ItemInfo(ImGuiID id, const char* label, int flags);
1347+
#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID, _LABEL, _FLAGS) ImGuiTestEngineHook_ItemInfo(_ID, _LABEL, _FLAGS) // Register status flags
1348+
#else
1349+
#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID, _LABEL, _FLAGS) do { } while (0)
1350+
#endif
1351+
13321352
#ifdef __clang__
13331353
#pragma clang diagnostic pop
13341354
#endif

imgui_widgets.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ bool ImGui::Checkbox(const char* label, bool* v)
913913
if (label_size.x > 0.0f)
914914
RenderText(text_bb.Min, label);
915915

916+
IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0));
916917
return pressed;
917918
}
918919

@@ -4760,6 +4761,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l
47604761
// (Ideally we'd want to add a flag for the user to specify if we want the hit test to be done up to the right side of the content or not)
47614762
const ImRect interact_bb = display_frame ? frame_bb : ImRect(frame_bb.Min.x, frame_bb.Min.y, frame_bb.Min.x + text_width + style.ItemSpacing.x*2, frame_bb.Max.y);
47624763
bool is_open = TreeNodeBehaviorIsOpen(id, flags);
4764+
bool is_leaf = (flags & ImGuiTreeNodeFlags_Leaf) != 0;
47634765

47644766
// Store a flag for the current depth to tell if we will allow closing this node when navigating one of its child.
47654767
// For this purpose we essentially compare if g.NavIdIsAlive went from 0 to 1 between TreeNode() and TreePop().
@@ -4775,6 +4777,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l
47754777
{
47764778
if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen))
47774779
TreePushRawID(id);
4780+
IMGUI_TEST_ENGINE_ITEM_INFO(window->DC.LastItemId, label, window->DC.ItemFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0));
47784781
return is_open;
47794782
}
47804783

@@ -4784,13 +4787,13 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l
47844787
// - OpenOnArrow .................... single-click on arrow to open
47854788
// - OpenOnDoubleClick|OpenOnArrow .. single-click on arrow or double-click anywhere to open
47864789
ImGuiButtonFlags button_flags = ImGuiButtonFlags_NoKeyModifiers | ((flags & ImGuiTreeNodeFlags_AllowItemOverlap) ? ImGuiButtonFlags_AllowItemOverlap : 0);
4787-
if (!(flags & ImGuiTreeNodeFlags_Leaf))
4790+
if (!is_leaf)
47884791
button_flags |= ImGuiButtonFlags_PressedOnDragDropHold;
47894792
if (flags & ImGuiTreeNodeFlags_OpenOnDoubleClick)
47904793
button_flags |= ImGuiButtonFlags_PressedOnDoubleClick | ((flags & ImGuiTreeNodeFlags_OpenOnArrow) ? ImGuiButtonFlags_PressedOnClickRelease : 0);
47914794

47924795
bool hovered, held, pressed = ButtonBehavior(interact_bb, id, &hovered, &held, button_flags);
4793-
if (!(flags & ImGuiTreeNodeFlags_Leaf))
4796+
if (!is_leaf)
47944797
{
47954798
bool toggled = false;
47964799
if (pressed)
@@ -4858,7 +4861,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l
48584861

48594862
if (flags & ImGuiTreeNodeFlags_Bullet)
48604863
RenderBullet(frame_bb.Min + ImVec2(text_offset_x * 0.5f, g.FontSize*0.50f + text_base_offset_y));
4861-
else if (!(flags & ImGuiTreeNodeFlags_Leaf))
4864+
else if (!is_leaf)
48624865
RenderArrow(frame_bb.Min + ImVec2(padding.x, g.FontSize*0.15f + text_base_offset_y), is_open ? ImGuiDir_Down : ImGuiDir_Right, 0.70f);
48634866
if (g.LogEnabled)
48644867
LogRenderedText(&text_pos, ">");
@@ -4867,6 +4870,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l
48674870

48684871
if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen))
48694872
TreePushRawID(id);
4873+
IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0));
48704874
return is_open;
48714875
}
48724876

@@ -5650,6 +5654,8 @@ bool ImGui::BeginMenu(const char* label, bool enabled)
56505654
if (want_close && IsPopupOpen(id))
56515655
ClosePopupToLevel(g.CurrentPopupStack.Size);
56525656

5657+
IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Openable | (menu_is_open ? ImGuiItemStatusFlags_Opened : 0));
5658+
56535659
if (!menu_is_open && want_open && g.OpenPopupStack.Size > g.CurrentPopupStack.Size)
56545660
{
56555661
// Don't recycle same menu level in the same frame, first close the other menu and yield for a frame.
@@ -5729,6 +5735,8 @@ bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, boo
57295735
if (selected)
57305736
RenderCheckMark(pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * 0.40f, g.FontSize * 0.134f * 0.5f), GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled), g.FontSize * 0.866f);
57315737
}
5738+
5739+
IMGUI_TEST_ENGINE_ITEM_INFO(window->DC.LastItemId, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (selected ? ImGuiItemStatusFlags_Checked : 0));
57325740
return pressed;
57335741
}
57345742

0 commit comments

Comments
 (0)