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

docking
ocornut 4 years ago
parent e5cb04b132
commit a38c6dfcc8

@ -3310,6 +3310,24 @@ void ImGui::DestroyContext(ImGuiContext* ctx)
IM_DELETE(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() ImGuiIO& ImGui::GetIO()
{ {
IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); 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() ?"); IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?");
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
#ifdef IMGUI_ENABLE_TEST_ENGINE CallContextHooks(&g, ImGuiContextHookType_NewFramePre);
ImGuiTestEngineHook_PreNewFrame(&g);
#endif
// Check and assert for various common IO and Configuration mistakes // Check and assert for various common IO and Configuration mistakes
ErrorCheckNewFrameSanityChecks(); ErrorCheckNewFrameSanityChecks();
@ -3907,9 +3923,7 @@ void ImGui::NewFrame()
Begin("Debug##Default"); Begin("Debug##Default");
IM_ASSERT(g.CurrentWindow->IsFallbackWindow == true); IM_ASSERT(g.CurrentWindow->IsFallbackWindow == true);
#ifdef IMGUI_ENABLE_TEST_ENGINE CallContextHooks(&g, ImGuiContextHookType_NewFramePost);
ImGuiTestEngineHook_PostNewFrame(&g);
#endif
} }
// [DEBUG] Item picker tool - start with DebugStartItemPicker() - useful to visually select an item and break into its call-stack. // [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) if (g.SettingsLoaded && g.IO.IniFilename != NULL)
{ {
ImGuiContext* backup_context = GImGui; ImGuiContext* backup_context = GImGui;
SetCurrentContext(context); SetCurrentContext(&g);
SaveIniSettingsToDisk(g.IO.IniFilename); SaveIniSettingsToDisk(g.IO.IniFilename);
SetCurrentContext(backup_context); SetCurrentContext(backup_context);
} }
// Notify hooked test engine, if any CallContextHooks(&g, ImGuiContextHookType_Shutdown);
#ifdef IMGUI_ENABLE_TEST_ENGINE
ImGuiTestEngineHook_Shutdown(context);
#endif
// Clear everything else // Clear everything else
for (int i = 0; i < g.Windows.Size; i++) for (int i = 0; i < g.Windows.Size; i++)
@ -4202,6 +4213,8 @@ void ImGui::EndFrame()
return; return;
IM_ASSERT(g.WithinFrameScope && "Forgot to call ImGui::NewFrame()?"); IM_ASSERT(g.WithinFrameScope && "Forgot to call ImGui::NewFrame()?");
CallContextHooks(&g, ImGuiContextHookType_EndFramePre);
ErrorCheckEndFrameSanityChecks(); ErrorCheckEndFrameSanityChecks();
// Notify OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME) // 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.MouseWheel = g.IO.MouseWheelH = 0.0f;
g.IO.InputQueueCharacters.resize(0); g.IO.InputQueueCharacters.resize(0);
memset(g.IO.NavInputs, 0, sizeof(g.IO.NavInputs)); memset(g.IO.NavInputs, 0, sizeof(g.IO.NavInputs));
CallContextHooks(&g, ImGuiContextHookType_EndFramePost);
} }
void ImGui::Render() void ImGui::Render()
@ -4281,6 +4296,8 @@ void ImGui::Render()
g.IO.MetricsRenderWindows = 0; g.IO.MetricsRenderWindows = 0;
g.DrawDataBuilder.Clear(); g.DrawDataBuilder.Clear();
CallContextHooks(&g, ImGuiContextHookType_RenderPre);
// Add background ImDrawList // Add background ImDrawList
if (!g.BackgroundDrawList.VtxBuffer.empty()) if (!g.BackgroundDrawList.VtxBuffer.empty())
AddDrawListToDrawData(&g.DrawDataBuilder.Layers[0], &g.BackgroundDrawList); AddDrawListToDrawData(&g.DrawDataBuilder.Layers[0], &g.BackgroundDrawList);
@ -4318,6 +4335,8 @@ void ImGui::Render()
if (g.DrawData.CmdListsCount > 0 && g.IO.RenderDrawListsFn != NULL) if (g.DrawData.CmdListsCount > 0 && g.IO.RenderDrawListsFn != NULL)
g.IO.RenderDrawListsFn(&g.DrawData); g.IO.RenderDrawListsFn(&g.DrawData);
#endif #endif
CallContextHooks(&g, ImGuiContextHookType_RenderPost);
} }
// Calculate text size. Text can be multi-line. Optionally ignore text after a ## marker. // Calculate text size. Text can be multi-line. Optionally ignore text after a ## marker.

@ -60,7 +60,7 @@ Index of this file:
// Version // Version
// (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) // (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens)
#define IMGUI_VERSION "1.79" #define IMGUI_VERSION "1.79"
#define IMGUI_VERSION_NUM 17900 #define IMGUI_VERSION_NUM 17901
#define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx))
// Define attributes of all API symbols declarations (e.g. for DLL under Windows) // Define attributes of all API symbols declarations (e.g. for DLL under Windows)

@ -19,16 +19,17 @@ Index of this file:
// [SECTION] ImDrawList support // [SECTION] ImDrawList support
// [SECTION] Widgets support: flags, enums, data structures // [SECTION] Widgets support: flags, enums, data structures
// [SECTION] Columns support // [SECTION] Columns support
// [SECTION] Settings support
// [SECTION] Multi-select support // [SECTION] Multi-select support
// [SECTION] Docking support // [SECTION] Docking support
// [SECTION] Viewport support // [SECTION] Viewport support
// [SECTION] Settings support
// [SECTION] Generic context hooks
// [SECTION] ImGuiContext (main imgui context) // [SECTION] ImGuiContext (main imgui context)
// [SECTION] ImGuiWindowTempData, ImGuiWindow // [SECTION] ImGuiWindowTempData, ImGuiWindow
// [SECTION] Tab bar, Tab item support // [SECTION] Tab bar, Tab item support
// [SECTION] Table support // [SECTION] Table support
// [SECTION] Internal API // [SECTION] Internal API
// [SECTION] Test Engine Hooks (imgui_test_engine) // [SECTION] Test Engine specific hooks (imgui_test_engine)
*/ */
@ -93,6 +94,7 @@ struct ImGuiColorMod; // Stacked color modifier, backup of modifie
struct ImGuiColumnData; // Storage data for a single column struct ImGuiColumnData; // Storage data for a single column
struct ImGuiColumns; // Storage data for a columns set struct ImGuiColumns; // Storage data for a columns set
struct ImGuiContext; // Main Dear ImGui context struct ImGuiContext; // Main Dear ImGui context
struct ImGuiContextHook; // Hook for extensions like ImGuiTestEngine
struct ImGuiDataTypeInfo; // Type information associated to a ImGuiDataType enum struct ImGuiDataTypeInfo; // Type information associated to a ImGuiDataType enum
struct ImGuiGroupData; // Stacked storage data for BeginGroup()/EndGroup() struct ImGuiGroupData; // Stacked storage data for BeginGroup()/EndGroup()
struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box
@ -1099,6 +1101,23 @@ struct ImGuiSettingsHandler
ImGuiSettingsHandler() { memset(this, 0, sizeof(*this)); } ImGuiSettingsHandler() { memset(this, 0, sizeof(*this)); }
}; };
//-----------------------------------------------------------------------------
// [SECTION] Generic context hooks
//-----------------------------------------------------------------------------
typedef void (*ImGuiContextHookCallback)(ImGuiContext* ctx, ImGuiContextHook* hook);
enum ImGuiContextHookType { ImGuiContextHookType_NewFramePre, ImGuiContextHookType_NewFramePost, ImGuiContextHookType_EndFramePre, ImGuiContextHookType_EndFramePost, ImGuiContextHookType_RenderPre, ImGuiContextHookType_RenderPost, ImGuiContextHookType_Shutdown };
struct ImGuiContextHook
{
ImGuiContextHookType Type;
ImGuiID Owner;
ImGuiContextHookCallback Callback;
void* UserData;
ImGuiContextHook() { memset(this, 0, sizeof(*this)); }
};
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// [SECTION] ImGuiContext (main imgui context) // [SECTION] ImGuiContext (main imgui context)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -1301,6 +1320,7 @@ struct ImGuiContext
ImGuiTextBuffer SettingsIniData; // In memory .ini settings ImGuiTextBuffer SettingsIniData; // In memory .ini settings
ImVector<ImGuiSettingsHandler> SettingsHandlers; // List of .ini settings handlers ImVector<ImGuiSettingsHandler> SettingsHandlers; // List of .ini settings handlers
ImChunkStream<ImGuiWindowSettings> SettingsWindows; // ImGuiWindow .ini settings entries ImChunkStream<ImGuiWindowSettings> SettingsWindows; // ImGuiWindow .ini settings entries
ImVector<ImGuiContextHook> Hooks; // Hooks for extensions (e.g. test engine)
// Capture/Logging // Capture/Logging
bool LogEnabled; // Currently capturing bool LogEnabled; // Currently capturing
@ -1819,6 +1839,10 @@ namespace ImGui
IMGUI_API void UpdateMouseMovingWindowNewFrame(); IMGUI_API void UpdateMouseMovingWindowNewFrame();
IMGUI_API void UpdateMouseMovingWindowEndFrame(); IMGUI_API void UpdateMouseMovingWindowEndFrame();
// Generic context hooks
IMGUI_API void AddContextHook(ImGuiContext* context, const ImGuiContextHook* hook);
IMGUI_API void CallContextHooks(ImGuiContext* context, ImGuiContextHookType type);
// Settings // Settings
IMGUI_API void MarkIniSettingsDirty(); IMGUI_API void MarkIniSettingsDirty();
IMGUI_API void MarkIniSettingsDirty(ImGuiWindow* window); IMGUI_API void MarkIniSettingsDirty(ImGuiWindow* window);
@ -2055,13 +2079,10 @@ IMGUI_API void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned cha
IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride); IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// [SECTION] Test Engine Hooks (imgui_test_engine) // [SECTION] Test Engine specific hooks (imgui_test_engine)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifdef IMGUI_ENABLE_TEST_ENGINE #ifdef IMGUI_ENABLE_TEST_ENGINE
extern void ImGuiTestEngineHook_Shutdown(ImGuiContext* ctx);
extern void ImGuiTestEngineHook_PreNewFrame(ImGuiContext* ctx);
extern void ImGuiTestEngineHook_PostNewFrame(ImGuiContext* ctx);
extern void ImGuiTestEngineHook_ItemAdd(ImGuiContext* ctx, const ImRect& bb, ImGuiID id); extern void ImGuiTestEngineHook_ItemAdd(ImGuiContext* ctx, const ImRect& bb, ImGuiID id);
extern void ImGuiTestEngineHook_ItemInfo(ImGuiContext* ctx, ImGuiID id, const char* label, ImGuiItemStatusFlags flags); extern void ImGuiTestEngineHook_ItemInfo(ImGuiContext* ctx, ImGuiID id, const char* label, ImGuiItemStatusFlags flags);
extern void ImGuiTestEngineHook_IdInfo(ImGuiContext* ctx, ImGuiDataType data_type, ImGuiID id, const void* data_id); extern void ImGuiTestEngineHook_IdInfo(ImGuiContext* ctx, ImGuiDataType data_type, ImGuiID id, const void* data_id);
@ -2080,6 +2101,8 @@ extern void ImGuiTestEngineHook_Log(ImGuiContext* ctx, const cha
#define IMGUI_TEST_ENGINE_ID_INFO2(_ID,_TYPE,_DATA,_DATA2) do { } while (0) #define IMGUI_TEST_ENGINE_ID_INFO2(_ID,_TYPE,_DATA,_DATA2) do { } while (0)
#endif #endif
//-----------------------------------------------------------------------------
#if defined(__clang__) #if defined(__clang__)
#pragma clang diagnostic pop #pragma clang diagnostic pop
#elif defined(__GNUC__) #elif defined(__GNUC__)

Loading…
Cancel
Save