Skip to content

Commit 0bbbbe0

Browse files
committed
Internal: Refactor: Moved RenderArrow, RenderBullet, RenderCheckMark to imgui_draw.cpp, changed RenderCheckMark to avoid using ImGui context
1 parent 2fd411a commit 0bbbbe0

File tree

4 files changed

+66
-63
lines changed

4 files changed

+66
-63
lines changed

imgui.cpp

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,8 +2542,9 @@ const char* ImGui::GetStyleColorName(ImGuiCol idx)
25422542

25432543
//-----------------------------------------------------------------------------
25442544
// [SECTION] RENDER HELPERS
2545-
// Some of those (internal) functions are currently quite a legacy mess - their signature and behavior will change.
2546-
// Also see imgui_draw.cpp for some more which have been reworked to not rely on ImGui:: state.
2545+
// Some of those (internal) functions are currently quite a legacy mess - their signature and behavior will change,
2546+
// we need a nicer separation between low-level functions and high-level functions relying on the ImGui context.
2547+
// Also see imgui_draw.cpp for some more which have been reworked to not rely on ImGui:: context.
25472548
//-----------------------------------------------------------------------------
25482549

25492550
const char* ImGui::FindRenderedTextEnd(const char* text, const char* text_end)
@@ -2753,61 +2754,6 @@ void ImGui::RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding)
27532754
}
27542755
}
27552756

2756-
// Render an arrow aimed to be aligned with text (p_min is a position in the same space text would be positioned). To e.g. denote expanded/collapsed state
2757-
void ImGui::RenderArrow(ImDrawList* draw_list, ImVec2 pos, ImU32 col, ImGuiDir dir, float scale)
2758-
{
2759-
const float h = draw_list->_Data->FontSize * 1.00f;
2760-
float r = h * 0.40f * scale;
2761-
ImVec2 center = pos + ImVec2(h * 0.50f, h * 0.50f * scale);
2762-
2763-
ImVec2 a, b, c;
2764-
switch (dir)
2765-
{
2766-
case ImGuiDir_Up:
2767-
case ImGuiDir_Down:
2768-
if (dir == ImGuiDir_Up) r = -r;
2769-
a = ImVec2(+0.000f,+0.750f) * r;
2770-
b = ImVec2(-0.866f,-0.750f) * r;
2771-
c = ImVec2(+0.866f,-0.750f) * r;
2772-
break;
2773-
case ImGuiDir_Left:
2774-
case ImGuiDir_Right:
2775-
if (dir == ImGuiDir_Left) r = -r;
2776-
a = ImVec2(+0.750f,+0.000f) * r;
2777-
b = ImVec2(-0.750f,+0.866f) * r;
2778-
c = ImVec2(-0.750f,-0.866f) * r;
2779-
break;
2780-
case ImGuiDir_None:
2781-
case ImGuiDir_COUNT:
2782-
IM_ASSERT(0);
2783-
break;
2784-
}
2785-
draw_list->AddTriangleFilled(center + a, center + b, center + c, col);
2786-
}
2787-
2788-
void ImGui::RenderBullet(ImDrawList* draw_list, ImVec2 pos, ImU32 col)
2789-
{
2790-
draw_list->AddCircleFilled(pos, draw_list->_Data->FontSize * 0.20f, col, 8);
2791-
}
2792-
2793-
void ImGui::RenderCheckMark(ImVec2 pos, ImU32 col, float sz)
2794-
{
2795-
ImGuiContext& g = *GImGui;
2796-
ImGuiWindow* window = g.CurrentWindow;
2797-
2798-
float thickness = ImMax(sz / 5.0f, 1.0f);
2799-
sz -= thickness*0.5f;
2800-
pos += ImVec2(thickness*0.25f, thickness*0.25f);
2801-
2802-
float third = sz / 3.0f;
2803-
float bx = pos.x + third;
2804-
float by = pos.y + sz - third*0.5f;
2805-
window->DrawList->PathLineTo(ImVec2(bx - third, by - third));
2806-
window->DrawList->PathLineTo(ImVec2(bx, by));
2807-
window->DrawList->PathLineTo(ImVec2(bx + third*2, by - third*2));
2808-
window->DrawList->PathStroke(col, false, thickness);
2809-
}
2810-
28112757
void ImGui::RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags)
28122758
{
28132759
ImGuiContext& g = *GImGui;

imgui_draw.cpp

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Index of this file:
1616
// [SECTION] ImFontAtlas glyph ranges helpers
1717
// [SECTION] ImFontGlyphRangesBuilder
1818
// [SECTION] ImFont
19-
// [SECTION] Internal Render Helpers
19+
// [SECTION] ImGui Internal Render Helpers
2020
// [SECTION] Decompression code
2121
// [SECTION] Default font data (ProggyClean.ttf)
2222
@@ -3161,13 +3161,70 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col
31613161
}
31623162

31633163
//-----------------------------------------------------------------------------
3164-
// [SECTION] Internal Render Helpers
3165-
// (progressively moved from imgui.cpp to here when they are redesigned to stop accessing ImGui global state)
3164+
// [SECTION] ImGui Internal Render Helpers
31663165
//-----------------------------------------------------------------------------
3166+
// Vaguely redesigned to stop accessing ImGui global state:
3167+
// - RenderArrow()
3168+
// - RenderBullet()
3169+
// - RenderCheckMark()
31673170
// - RenderMouseCursor()
31683171
// - RenderArrowPointingAt()
31693172
// - RenderRectFilledRangeH()
31703173
//-----------------------------------------------------------------------------
3174+
// Function in need of a redesign (legacy mess)
3175+
//-----------------------------------------------------------------------------
3176+
3177+
// Render an arrow aimed to be aligned with text (p_min is a position in the same space text would be positioned). To e.g. denote expanded/collapsed state
3178+
void ImGui::RenderArrow(ImDrawList* draw_list, ImVec2 pos, ImU32 col, ImGuiDir dir, float scale)
3179+
{
3180+
const float h = draw_list->_Data->FontSize * 1.00f;
3181+
float r = h * 0.40f * scale;
3182+
ImVec2 center = pos + ImVec2(h * 0.50f, h * 0.50f * scale);
3183+
3184+
ImVec2 a, b, c;
3185+
switch (dir)
3186+
{
3187+
case ImGuiDir_Up:
3188+
case ImGuiDir_Down:
3189+
if (dir == ImGuiDir_Up) r = -r;
3190+
a = ImVec2(+0.000f, +0.750f) * r;
3191+
b = ImVec2(-0.866f, -0.750f) * r;
3192+
c = ImVec2(+0.866f, -0.750f) * r;
3193+
break;
3194+
case ImGuiDir_Left:
3195+
case ImGuiDir_Right:
3196+
if (dir == ImGuiDir_Left) r = -r;
3197+
a = ImVec2(+0.750f, +0.000f) * r;
3198+
b = ImVec2(-0.750f, +0.866f) * r;
3199+
c = ImVec2(-0.750f, -0.866f) * r;
3200+
break;
3201+
case ImGuiDir_None:
3202+
case ImGuiDir_COUNT:
3203+
IM_ASSERT(0);
3204+
break;
3205+
}
3206+
draw_list->AddTriangleFilled(center + a, center + b, center + c, col);
3207+
}
3208+
3209+
void ImGui::RenderBullet(ImDrawList* draw_list, ImVec2 pos, ImU32 col)
3210+
{
3211+
draw_list->AddCircleFilled(pos, draw_list->_Data->FontSize * 0.20f, col, 8);
3212+
}
3213+
3214+
void ImGui::RenderCheckMark(ImDrawList* draw_list, ImVec2 pos, ImU32 col, float sz)
3215+
{
3216+
float thickness = ImMax(sz / 5.0f, 1.0f);
3217+
sz -= thickness * 0.5f;
3218+
pos += ImVec2(thickness * 0.25f, thickness * 0.25f);
3219+
3220+
float third = sz / 3.0f;
3221+
float bx = pos.x + third;
3222+
float by = pos.y + sz - third * 0.5f;
3223+
draw_list->PathLineTo(ImVec2(bx - third, by - third));
3224+
draw_list->PathLineTo(ImVec2(bx, by));
3225+
draw_list->PathLineTo(ImVec2(bx + third * 2.0f, by - third * 2.0f));
3226+
draw_list->PathStroke(col, false, thickness);
3227+
}
31713228

31723229
void ImGui::RenderMouseCursor(ImDrawList* draw_list, ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow)
31733230
{

imgui_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1813,14 +1813,14 @@ namespace ImGui
18131813
IMGUI_API void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border = true, float rounding = 0.0f);
18141814
IMGUI_API void RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding = 0.0f);
18151815
IMGUI_API void RenderColorRectWithAlphaCheckerboard(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, float grid_step, ImVec2 grid_off, float rounding = 0.0f, int rounding_corners_flags = ~0);
1816-
IMGUI_API void RenderCheckMark(ImVec2 pos, ImU32 col, float sz);
18171816
IMGUI_API void RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags = ImGuiNavHighlightFlags_TypeDefault); // Navigation highlight
18181817
IMGUI_API const char* FindRenderedTextEnd(const char* text, const char* text_end = NULL); // Find the optional ## from which we stop displaying text.
18191818
IMGUI_API void LogRenderedText(const ImVec2* ref_pos, const char* text, const char* text_end = NULL);
18201819

18211820
// Render helpers (those functions don't access any ImGui state!)
18221821
IMGUI_API void RenderArrow(ImDrawList* draw_list, ImVec2 pos, ImU32 col, ImGuiDir dir, float scale = 1.0f);
18231822
IMGUI_API void RenderBullet(ImDrawList* draw_list, ImVec2 pos, ImU32 col);
1823+
IMGUI_API void RenderCheckMark(ImDrawList* draw_list, ImVec2 pos, ImU32 col, float sz);
18241824
IMGUI_API void RenderMouseCursor(ImDrawList* draw_list, ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow);
18251825
IMGUI_API void RenderArrowPointingAt(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, ImGuiDir direction, ImU32 col);
18261826
IMGUI_API void RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, ImU32 col, float x_start_norm, float x_end_norm, float rounding);

imgui_widgets.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ bool ImGui::Checkbox(const char* label, bool* v)
10441044
else if (*v)
10451045
{
10461046
const float pad = ImMax(1.0f, IM_FLOOR(square_sz / 6.0f));
1047-
RenderCheckMark(check_bb.Min + ImVec2(pad, pad), check_col, square_sz - pad*2.0f);
1047+
RenderCheckMark(window->DrawList, check_bb.Min + ImVec2(pad, pad), check_col, square_sz - pad*2.0f);
10481048
}
10491049

10501050
if (g.LogEnabled)
@@ -6433,7 +6433,7 @@ bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, boo
64336433
PopStyleColor();
64346434
}
64356435
if (selected)
6436-
RenderCheckMark(pos + ImVec2(window->DC.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);
6436+
RenderCheckMark(window->DrawList, pos + ImVec2(window->DC.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);
64376437
}
64386438

64396439
IMGUI_TEST_ENGINE_ITEM_INFO(window->DC.LastItemId, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (selected ? ImGuiItemStatusFlags_Checked : 0));

0 commit comments

Comments
 (0)