Internals: Added support for context hooks (for test engine or other extensions)

This commit is contained in:
ocornut
2020-10-09 17:13:03 +02:00
parent e5cb04b132
commit a38c6dfcc8
3 changed files with 60 additions and 18 deletions

View File

@ -3310,6 +3310,24 @@ void ImGui::DestroyContext(ImGuiContext* ctx)
IM_DELETE(ctx);
}
// No specific ordering/dependency support, will see as needed
void ImGui::AddContextHook(ImGuiContext* ctx, const ImGuiContextHook* hook)
{
ImGuiContext& g = *ctx;
IM_ASSERT(hook->Callback != NULL);
g.Hooks.push_back(*hook);
}
// Call context hooks (used by e.g. test engine)
// We assume a small number of hooks so all stored in same array
void ImGui::CallContextHooks(ImGuiContext* ctx, ImGuiContextHookType hook_type)
{
ImGuiContext& g = *ctx;
for (int n = 0; n < g.Hooks.Size; n++)
if (g.Hooks[n].Type == hook_type)
g.Hooks[n].Callback(&g, &g.Hooks[n]);
}
ImGuiIO& ImGui::GetIO()
{
IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?");
@ -3735,9 +3753,7 @@ void ImGui::NewFrame()
IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?");
ImGuiContext& g = *GImGui;
#ifdef IMGUI_ENABLE_TEST_ENGINE
ImGuiTestEngineHook_PreNewFrame(&g);
#endif
CallContextHooks(&g, ImGuiContextHookType_NewFramePre);
// Check and assert for various common IO and Configuration mistakes
ErrorCheckNewFrameSanityChecks();
@ -3907,9 +3923,7 @@ void ImGui::NewFrame()
Begin("Debug##Default");
IM_ASSERT(g.CurrentWindow->IsFallbackWindow == true);
#ifdef IMGUI_ENABLE_TEST_ENGINE
ImGuiTestEngineHook_PostNewFrame(&g);
#endif
CallContextHooks(&g, ImGuiContextHookType_NewFramePost);
}
// [DEBUG] Item picker tool - start with DebugStartItemPicker() - useful to visually select an item and break into its call-stack.
@ -3994,15 +4008,12 @@ void ImGui::Shutdown(ImGuiContext* context)
if (g.SettingsLoaded && g.IO.IniFilename != NULL)
{
ImGuiContext* backup_context = GImGui;
SetCurrentContext(context);
SetCurrentContext(&g);
SaveIniSettingsToDisk(g.IO.IniFilename);
SetCurrentContext(backup_context);
}
// Notify hooked test engine, if any
#ifdef IMGUI_ENABLE_TEST_ENGINE
ImGuiTestEngineHook_Shutdown(context);
#endif
CallContextHooks(&g, ImGuiContextHookType_Shutdown);
// Clear everything else
for (int i = 0; i < g.Windows.Size; i++)
@ -4202,6 +4213,8 @@ void ImGui::EndFrame()
return;
IM_ASSERT(g.WithinFrameScope && "Forgot to call ImGui::NewFrame()?");
CallContextHooks(&g, ImGuiContextHookType_EndFramePre);
ErrorCheckEndFrameSanityChecks();
// Notify OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME)
@ -4268,6 +4281,8 @@ void ImGui::EndFrame()
g.IO.MouseWheel = g.IO.MouseWheelH = 0.0f;
g.IO.InputQueueCharacters.resize(0);
memset(g.IO.NavInputs, 0, sizeof(g.IO.NavInputs));
CallContextHooks(&g, ImGuiContextHookType_EndFramePost);
}
void ImGui::Render()
@ -4281,6 +4296,8 @@ void ImGui::Render()
g.IO.MetricsRenderWindows = 0;
g.DrawDataBuilder.Clear();
CallContextHooks(&g, ImGuiContextHookType_RenderPre);
// Add background ImDrawList
if (!g.BackgroundDrawList.VtxBuffer.empty())
AddDrawListToDrawData(&g.DrawDataBuilder.Layers[0], &g.BackgroundDrawList);
@ -4318,6 +4335,8 @@ void ImGui::Render()
if (g.DrawData.CmdListsCount > 0 && g.IO.RenderDrawListsFn != NULL)
g.IO.RenderDrawListsFn(&g.DrawData);
#endif
CallContextHooks(&g, ImGuiContextHookType_RenderPost);
}
// Calculate text size. Text can be multi-line. Optionally ignore text after a ## marker.