1
0
Fork 0
mirror of https://github.com/ocornut/imgui.git synced 2026-01-25 02:24:24 +00:00

Error handling: rework macros, add IM_ASSERT_USER_ERROR_RET(), IM_ASSERT_USER_ERROR_RETV() to simplify code.

This commit is contained in:
ocornut 2026-01-19 12:04:43 +01:00
parent ea122de913
commit 12223cc3e9
4 changed files with 36 additions and 157 deletions

View file

@ -3635,11 +3635,7 @@ void ImGui::PushStyleVar(ImGuiStyleVar idx, float val)
{
ImGuiContext& g = *GImGui;
const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx);
if (var_info->DataType != ImGuiDataType_Float || var_info->Count != 1)
{
IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!");
return;
}
IM_ASSERT_USER_ERROR_RET(var_info->DataType == ImGuiDataType_Float && var_info->Count == 1, "Calling PushStyleVar() variant with wrong type!");
float* pvar = (float*)var_info->GetVarPtr(&g.Style);
g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar));
*pvar = val;
@ -3649,11 +3645,7 @@ void ImGui::PushStyleVarX(ImGuiStyleVar idx, float val_x)
{
ImGuiContext& g = *GImGui;
const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx);
if (var_info->DataType != ImGuiDataType_Float || var_info->Count != 2)
{
IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!");
return;
}
IM_ASSERT_USER_ERROR_RET(var_info->DataType == ImGuiDataType_Float && var_info->Count == 2, "Calling PushStyleVar() variant with wrong type!");
ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style);
g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar));
pvar->x = val_x;
@ -3663,11 +3655,7 @@ void ImGui::PushStyleVarY(ImGuiStyleVar idx, float val_y)
{
ImGuiContext& g = *GImGui;
const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx);
if (var_info->DataType != ImGuiDataType_Float || var_info->Count != 2)
{
IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!");
return;
}
IM_ASSERT_USER_ERROR_RET(var_info->DataType == ImGuiDataType_Float && var_info->Count == 2, "Calling PushStyleVar() variant with wrong type!");
ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style);
g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar));
pvar->y = val_y;
@ -3677,11 +3665,7 @@ void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val)
{
ImGuiContext& g = *GImGui;
const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx);
if (var_info->DataType != ImGuiDataType_Float || var_info->Count != 2)
{
IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!");
return;
}
IM_ASSERT_USER_ERROR_RET(var_info->DataType == ImGuiDataType_Float && var_info->Count == 2, "Calling PushStyleVar() variant with wrong type!");
ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style);
g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar));
*pvar = val;
@ -5941,11 +5925,7 @@ void ImGui::EndFrame()
// Don't process EndFrame() multiple times.
if (g.FrameCountEnded == g.FrameCount)
return;
if (!g.WithinFrameScope)
{
IM_ASSERT_USER_ERROR(g.WithinFrameScope, "Forgot to call ImGui::NewFrame()?");
return;
}
IM_ASSERT_USER_ERROR_RET(g.WithinFrameScope, "Forgot to call ImGui::NewFrame()?");
CallContextHooks(&g, ImGuiContextHookType_EndFramePre);
@ -8221,11 +8201,7 @@ void ImGui::PushItemFlag(ImGuiItemFlags option, bool enabled)
void ImGui::PopItemFlag()
{
ImGuiContext& g = *GImGui;
if (g.ItemFlagsStack.Size <= 1)
{
IM_ASSERT_USER_ERROR(0, "Calling PopItemFlag() too many times!");
return;
}
IM_ASSERT_USER_ERROR_RET(g.ItemFlagsStack.Size > 1, "Calling PopItemFlag() too many times!");
g.ItemFlagsStack.pop_back();
g.CurrentItemFlags = g.ItemFlagsStack.back();
}
@ -8255,11 +8231,7 @@ void ImGui::BeginDisabled(bool disabled)
void ImGui::EndDisabled()
{
ImGuiContext& g = *GImGui;
if (g.DisabledStackSize <= 0)
{
IM_ASSERT_USER_ERROR(0, "Calling EndDisabled() too many times!");
return;
}
IM_ASSERT_USER_ERROR_RET(g.DisabledStackSize > 0, "Calling EndDisabled() too many times!");
g.DisabledStackSize--;
bool was_disabled = (g.CurrentItemFlags & ImGuiItemFlags_Disabled) != 0;
//PopItemFlag();
@ -8306,11 +8278,7 @@ void ImGui::PopTextWrapPos()
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
if (window->DC.TextWrapPosStack.Size <= 0)
{
IM_ASSERT_USER_ERROR(0, "Calling PopTextWrapPos() too many times!");
return;
}
IM_ASSERT_USER_ERROR_RET(window->DC.TextWrapPosStack.Size > 0, "Calling PopTextWrapPos() too many times!");
window->DC.TextWrapPos = window->DC.TextWrapPosStack.back();
window->DC.TextWrapPosStack.pop_back();
}
@ -8709,11 +8677,7 @@ void ImGui::PushFocusScope(ImGuiID id)
void ImGui::PopFocusScope()
{
ImGuiContext& g = *GImGui;
if (g.FocusScopeStack.Size <= g.StackSizesInBeginForCurrentWindow->SizeOfFocusScopeStack)
{
IM_ASSERT_USER_ERROR(0, "Calling PopFocusScope() too many times!");
return;
}
IM_ASSERT_USER_ERROR_RET(g.FocusScopeStack.Size > g.StackSizesInBeginForCurrentWindow->SizeOfFocusScopeStack, "Calling PopFocusScope() too many times!");
g.FocusScopeStack.pop_back();
g.CurrentFocusScopeId = g.FocusScopeStack.Size ? g.FocusScopeStack.back().ID : 0;
}
@ -9104,11 +9068,7 @@ void ImGui::PushFont(ImFont* font, float font_size_base)
void ImGui::PopFont()
{
ImGuiContext& g = *GImGui;
if (g.FontStack.Size <= 0)
{
IM_ASSERT_USER_ERROR(0, "Calling PopFont() too many times!");
return;
}
IM_ASSERT_USER_ERROR_RET(g.FontStack.Size > 0, "Calling PopFont() too many times!");
ImFontStackData* font_stack_data = &g.FontStack.back();
SetCurrentFont(font_stack_data->Font, font_stack_data->FontSizeBeforeScaling, font_stack_data->FontSizeAfterScaling);
g.FontStack.pop_back();
@ -9248,11 +9208,7 @@ ImGuiID ImGui::GetIDWithSeed(int n, ImGuiID seed)
void ImGui::PopID()
{
ImGuiWindow* window = GImGui->CurrentWindow;
if (window->IDStack.Size <= 1)
{
IM_ASSERT_USER_ERROR(0, "Calling PopID() too many times!");
return;
}
IM_ASSERT_USER_ERROR_RET(window->IDStack.Size > 1, "Calling PopID() too many times!");
window->IDStack.pop_back();
}
@ -12457,11 +12413,7 @@ void ImGui::EndPopup()
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
if ((window->Flags & ImGuiWindowFlags_Popup) == 0 || g.BeginPopupStack.Size == 0)
{
IM_ASSERT_USER_ERROR(0, "Calling EndPopup() in wrong window!");
return;
}
IM_ASSERT_USER_ERROR_RET((window->Flags & ImGuiWindowFlags_Popup) != 0 && g.BeginPopupStack.Size > 0, "Calling EndPopup() in wrong window!");
// Make all menus and popups wrap around for now, may need to expose that policy (e.g. focus scope could include wrap/loop policy flags used by new move requests)
if (g.NavWindow == window)