diff --git a/imgui.cpp b/imgui.cpp index 4519c96fb..850e8b13e 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -4732,6 +4732,28 @@ ImGuiID ImGui::AddContextHook(ImGuiContext* ctx, const ImGuiContextHook* hook) return g.HookIdNext; } +// Metrics/Debugger: register a hook describing user draw callbacks (see ImGuiDebugDrawCmdHookFn). +void ImGui::DebugAddDrawCmdHook(ImGuiDebugDrawCmdHookFn hook, void* user_data) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(hook != NULL); + ImGuiDebugDrawCmdHook entry; + entry.Callback = hook; + entry.UserData = user_data; + g.DebugDrawCmdHooks.push_back(entry); +} + +void ImGui::DebugRemoveDrawCmdHook(ImGuiDebugDrawCmdHookFn hook, void* user_data) +{ + ImGuiContext& g = *GImGui; + for (int n = 0; n < g.DebugDrawCmdHooks.Size; n++) + if (g.DebugDrawCmdHooks[n].Callback == hook && g.DebugDrawCmdHooks[n].UserData == user_data) + { + g.DebugDrawCmdHooks.erase(g.DebugDrawCmdHooks.Data + n); + return; + } +} + // Deferred removal, avoiding issue with changing vector while iterating it void ImGui::RemoveContextHook(ImGuiContext* ctx, ImGuiID hook_id) { @@ -17691,7 +17713,15 @@ void ImGui::DebugNodeDrawList(ImGuiWindow* window, ImGuiViewportP* viewport, con { if (pcmd->UserCallback) { - BulletText("Callback %p, user_data %p", pcmd->UserCallback, pcmd->UserCallbackData); + bool handled = false; + for (ImGuiDebugDrawCmdHook& hook : g.DebugDrawCmdHooks) + if (hook.Callback(draw_list, pcmd, fg_draw_list, hook.UserData)) + { + handled = true; + break; + } + if (!handled) + BulletText("Callback %p, user_data %p", pcmd->UserCallback, pcmd->UserCallbackData); continue; } diff --git a/imgui_internal.h b/imgui_internal.h index 57851e7ee..f1da8afae 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -2298,6 +2298,19 @@ struct ImGuiIDStackTool typedef void (*ImGuiContextHookCallback)(ImGuiContext* ctx, ImGuiContextHook* hook); enum ImGuiContextHookType { ImGuiContextHookType_NewFramePre, ImGuiContextHookType_NewFramePost, ImGuiContextHookType_EndFramePre, ImGuiContextHookType_EndFramePost, ImGuiContextHookType_RenderPre, ImGuiContextHookType_RenderPost, ImGuiContextHookType_Shutdown, ImGuiContextHookType_PendingRemoval_ }; +// Metrics/Debugger: hooks describing user draw callbacks (ImDrawCmd::UserCallback) in DebugNodeDrawList(). +// Hooks are tried in registration order for every callback command; the first one returning true has fully +// displayed the command (typically a TreeNode() and its contents, and an overlay on hover drawn in +// 'fg_draw_list', which may be NULL). If none handles it, the default "Callback %p, user_data %p" is shown. +#define IMGUI_HAS_DEBUG_DRAWCMD_HOOKS // DebugAddDrawCmdHook() / DebugRemoveDrawCmdHook() are available +typedef bool (*ImGuiDebugDrawCmdHookFn)(const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, ImDrawList* fg_draw_list, void* user_data); + +struct ImGuiDebugDrawCmdHook +{ + ImGuiDebugDrawCmdHookFn Callback; + void* UserData; +}; + struct ImGuiContextHook { ImGuiID HookId; // A unique ID assigned by AddContextHook() @@ -2696,6 +2709,7 @@ struct ImGuiContext float DebugFlashStyleColorTime; ImVec4 DebugFlashStyleColorBackup; ImGuiMetricsConfig DebugMetricsConfig; + ImVector DebugDrawCmdHooks; // Metrics/Debugger: user draw callback hooks (DebugAddDrawCmdHook) ImGuiDebugItemPathQuery DebugItemPathQuery; ImGuiIDStackTool DebugIDStackTool; ImGuiDebugAllocInfo DebugAllocInfo; @@ -3936,6 +3950,8 @@ namespace ImGui IMGUI_API void DebugNodeColumns(ImGuiOldColumns* columns); IMGUI_API void DebugNodeDrawList(ImGuiWindow* window, ImGuiViewportP* viewport, const ImDrawList* draw_list, const char* label); IMGUI_API void DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, bool show_mesh, bool show_aabb); + IMGUI_API void DebugAddDrawCmdHook(ImGuiDebugDrawCmdHookFn hook, void* user_data = NULL); + IMGUI_API void DebugRemoveDrawCmdHook(ImGuiDebugDrawCmdHookFn hook, void* user_data = NULL); IMGUI_API void DebugNodeFont(ImFont* font); IMGUI_API void DebugNodeFontGlyphsForSrcMask(ImFont* font, ImFontBaked* baked, int src_mask); IMGUI_API void DebugNodeFontGlyph(ImFont* font, const ImFontGlyph* glyph);