From df2319a854e543f2e83f51f483a4fb1fba37111a Mon Sep 17 00:00:00 2001 From: ocornut Date: Fri, 20 Aug 2021 16:56:13 +0200 Subject: [PATCH] Small optimizations to BeginDisabled() to allow frequent calls (#211) Not intended for frequent calls but I suspect some people will do it either way... Rough/indicative: measured 0.1 ms for 5000 calls in release, 0.5 ms in debug on my desktop windows. --- imgui.cpp | 19 ++++++++++++++----- imgui.h | 2 +- imgui_internal.h | 2 ++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 7af9380e..549e5dc8 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -989,7 +989,7 @@ static void* GImAllocatorUserData = NULL; ImGuiStyle::ImGuiStyle() { Alpha = 1.0f; // Global alpha applies to everything in Dear ImGui. - DisabledAlpha = 0.60f; // Additional alpha multiplier for disabled items (multiply over current value of Alpha). + DisabledAlpha = 0.60f; // Additional alpha multiplier applied by BeginDisabled(). Multiply over current value of Alpha. WindowPadding = ImVec2(8,8); // Padding within a window WindowRounding = 0.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows. Large values tend to lead to variety of artifacts and are not recommended. WindowBorderSize = 1.0f; // Thickness of border around windows. Generally set to 0.0f or 1.0f. Other values not well tested. @@ -6622,22 +6622,31 @@ void ImGui::PopItemFlag() // - Visually this is currently altering alpha, but it is expected that in a future styling system this would work differently. // - Feedback welcome at https://github.com/ocornut/imgui/issues/211 // - BeginDisabled(false) essentially does nothing but is provided to facilitate use of boolean expressions +// - Optimized shortcuts instead of PushStyleVar() + PushItemFlag() void ImGui::BeginDisabled(bool disabled) { ImGuiContext& g = *GImGui; bool was_disabled = (g.CurrentItemFlags & ImGuiItemFlags_Disabled) != 0; if (!was_disabled && disabled) - PushStyleVar(ImGuiStyleVar_Alpha, g.Style.Alpha * g.Style.DisabledAlpha); - PushItemFlag(ImGuiItemFlags_Disabled, was_disabled || disabled); + { + //PushStyleVar(ImGuiStyleVar_Alpha, g.Style.Alpha * g.Style.DisabledAlpha); + g.DisabledAlphaBackup = g.Style.Alpha; + g.Style.Alpha *= g.Style.DisabledAlpha; + } + //PushItemFlag(ImGuiItemFlags_Disabled, was_disabled || disabled); + g.CurrentItemFlags |= ImGuiItemFlags_Disabled; + g.ItemFlagsStack.push_back(g.CurrentItemFlags); } void ImGui::EndDisabled() { ImGuiContext& g = *GImGui; bool was_disabled = (g.CurrentItemFlags & ImGuiItemFlags_Disabled) != 0; - PopItemFlag(); + //PopItemFlag(); + g.ItemFlagsStack.pop_back(); + g.CurrentItemFlags &= ~ImGuiItemFlags_Disabled; if (was_disabled && (g.CurrentItemFlags & ImGuiItemFlags_Disabled) == 0) - PopStyleVar(); + g.Style.Alpha = g.DisabledAlphaBackup; //PopStyleVar(); } // FIXME: Look into renaming this once we have settled the new Focus/Activation/TabStop system. diff --git a/imgui.h b/imgui.h index 4428d96e..7b869919 100644 --- a/imgui.h +++ b/imgui.h @@ -1744,7 +1744,7 @@ IM_MSVC_RUNTIME_CHECKS_RESTORE struct ImGuiStyle { float Alpha; // Global alpha applies to everything in Dear ImGui. - float DisabledAlpha; // Additional alpha multiplier for disabled items (multiply over current value of Alpha). + float DisabledAlpha; // Additional alpha multiplier applied by BeginDisabled(). Multiply over current value of Alpha. ImVec2 WindowPadding; // Padding within a window. float WindowRounding; // Radius of window corners rounding. Set to 0.0f to have rectangular windows. Large values tend to lead to variety of artifacts and are not recommended. float WindowBorderSize; // Thickness of border around windows. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly). diff --git a/imgui_internal.h b/imgui_internal.h index 4c84a78b..b6908c31 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -1613,6 +1613,7 @@ struct ImGuiContext bool DragCurrentAccumDirty; float DragCurrentAccum; // Accumulator for dragging modification. Always high-precision, not rounded by end-user precision settings float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio + float DisabledAlphaBackup; // Backup for style.Alpha for BeginDisabled() float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? int TooltipOverrideCount; float TooltipSlowDelay; // Time before slow tooltips appears (FIXME: This is temporary until we merge in tooltip timer+priority work) @@ -1780,6 +1781,7 @@ struct ImGuiContext DragCurrentAccumDirty = false; DragCurrentAccum = 0.0f; DragSpeedDefaultRatio = 1.0f / 100.0f; + DisabledAlphaBackup = 0.0f; ScrollbarClickDeltaToGrabCenter = 0.0f; TooltipOverrideCount = 0; TooltipSlowDelay = 0.50f;