Expose PushDisabled() in public API. Add to demo.
diff --git a/imgui.cpp b/imgui.cpp index d5d33dd..16e6fe2 100644 --- a/imgui.cpp +++ b/imgui.cpp
@@ -988,7 +988,8 @@ ImGuiStyle::ImGuiStyle() { - Alpha = 1.0f; // Global alpha applies to everything in ImGui + 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). 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. @@ -2533,6 +2534,7 @@ static const ImGuiStyleVarInfo GStyleVarInfo[] = { { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, Alpha) }, // ImGuiStyleVar_Alpha + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, DisabledAlpha) }, // ImGuiStyleVar_DisabledAlpha { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowPadding) }, // ImGuiStyleVar_WindowPadding { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowRounding) }, // ImGuiStyleVar_WindowRounding { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowBorderSize) }, // ImGuiStyleVar_WindowBorderSize @@ -6617,17 +6619,14 @@ // PushDisabled()/PopDisabled() // - Those can be nested but this cannot be used to enable an already disabled section (a single PushDisabled(true) in the stack is enough to keep things disabled) -// - Those are not yet exposed in imgui.h because we are unsure of how to alter the style in a way that works for everyone. -// We may rework this. Hypothetically, a future styling system may set a flag which make widgets use different colors. +// - 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 -// - You may trivially implement your own variation of this if needed. -// Here we test (CurrentItemFlags & ImGuiItemFlags_Disabled) to allow nested PushDisabled() calls. void ImGui::PushDisabled(bool disabled) { ImGuiContext& g = *GImGui; bool was_disabled = (g.CurrentItemFlags & ImGuiItemFlags_Disabled) != 0; if (!was_disabled && disabled) - PushStyleVar(ImGuiStyleVar_Alpha, g.Style.Alpha * 0.6f); + PushStyleVar(ImGuiStyleVar_Alpha, g.Style.Alpha * g.Style.DisabledAlpha); PushItemFlag(ImGuiItemFlags_Disabled, was_disabled || disabled); }
diff --git a/imgui.h b/imgui.h index 30e3109..52c8424 100644 --- a/imgui.h +++ b/imgui.h
@@ -404,6 +404,8 @@ IMGUI_API float CalcItemWidth(); // width of item given pushed settings and current cursor position. NOT necessarily the width of last item unlike most 'Item' functions. IMGUI_API void PushTextWrapPos(float wrap_local_pos_x = 0.0f); // push word-wrapping position for Text*() commands. < 0.0f: no wrapping; 0.0f: wrap to end of window (or column); > 0.0f: wrap at 'wrap_pos_x' position in window local space IMGUI_API void PopTextWrapPos(); + IMGUI_API void PushDisabled(bool disabled = true); // disable all user interactions and apply an extra style.DisabledAlpha over current colors + IMGUI_API void PopDisabled(); // Style read access // - Use the style editor (ShowStyleEditor() function) to interactively see what the colors are) @@ -1492,6 +1494,7 @@ { // Enum name --------------------- // Member in ImGuiStyle structure (see ImGuiStyle for descriptions) ImGuiStyleVar_Alpha, // float Alpha + ImGuiStyleVar_DisabledAlpha, // float DisabledAlpha ImGuiStyleVar_WindowPadding, // ImVec2 WindowPadding ImGuiStyleVar_WindowRounding, // float WindowRounding ImGuiStyleVar_WindowBorderSize, // float WindowBorderSize @@ -1738,6 +1741,7 @@ 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). 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_demo.cpp b/imgui_demo.cpp index 2e91eb2..78b9a55 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp
@@ -533,6 +533,12 @@ if (!ImGui::CollapsingHeader("Widgets")) return; + static bool disable_all = false; + ImGui::Checkbox("Disable all widgets below", &disable_all); + ImGui::SameLine(); HelpMarker("Demonstrate using PushDisabled()/PopDisabled() across the rest of window."); + if (disable_all) + ImGui::PushDisabled(); + if (ImGui::TreeNode("Basic")) { static int clicked = 0; @@ -2190,15 +2196,19 @@ "InputFloat3", "ColorEdit4", "Selectable", "MenuItem", "TreeNode", "TreeNode (w/ double-click)", "Combo", "ListBox" }; static int item_type = 1; + static bool item_disabled = false; ImGui::Combo("Item Type", &item_type, item_names, IM_ARRAYSIZE(item_names), IM_ARRAYSIZE(item_names)); ImGui::SameLine(); HelpMarker("Testing how various types of items are interacting with the IsItemXXX functions. Note that the bool return value of most ImGui function is generally equivalent to calling ImGui::IsItemHovered()."); + ImGui::Checkbox("Item Disabled", &item_disabled); // Submit selected item item so we can query their status in the code following it. bool ret = false; static bool b = false; static float col4f[4] = { 1.0f, 0.5, 0.0f, 1.0f }; static char str[16] = {}; + if (item_disabled) + ImGui::PushDisabled(true); if (item_type == 0) { ImGui::Text("ITEM: Text"); } // Testing text items with no identifier/interaction if (item_type == 1) { ret = ImGui::Button("ITEM: Button"); } // Testing button if (item_type == 2) { ImGui::PushButtonRepeat(true); ret = ImGui::Button("ITEM: Button"); ImGui::PopButtonRepeat(); } // Testing button (with repeater) @@ -2226,6 +2236,7 @@ "IsItemHovered(_AllowWhenBlockedByPopup) = %d\n" "IsItemHovered(_AllowWhenBlockedByActiveItem) = %d\n" "IsItemHovered(_AllowWhenOverlapped) = %d\n" + "IsItemHovered(_AllowWhenDisabled) = %d\n" "IsItemHovered(_RectOnly) = %d\n" "IsItemActive() = %d\n" "IsItemEdited() = %d\n" @@ -2244,6 +2255,7 @@ ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup), ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem), ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenOverlapped), + ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled), ImGui::IsItemHovered(ImGuiHoveredFlags_RectOnly), ImGui::IsItemActive(), ImGui::IsItemEdited(), @@ -2258,6 +2270,9 @@ ImGui::GetItemRectSize().x, ImGui::GetItemRectSize().y ); + if (item_disabled) + ImGui::PopDisabled(); + static bool embed_all_inside_a_child_window = false; ImGui::Checkbox("Embed everything inside a child window (for additional testing)", &embed_all_inside_a_child_window); if (embed_all_inside_a_child_window) @@ -2327,6 +2342,9 @@ ImGui::TreePop(); } + + if (disable_all) + ImGui::PopDisabled(); } static void ShowDemoWindowLayout() @@ -3538,6 +3556,12 @@ if (!ImGui::CollapsingHeader("Tables & Columns")) return; + static bool disable_all = false; + ImGui::Checkbox("Disable all widgets below", &disable_all); + ImGui::SameLine(); HelpMarker("Demonstrate using PushDisabled()/PopDisabled() across the rest of window."); + if (disable_all) + ImGui::PushDisabled(); + // Using those as a base value to create width/height that are factor of the size of our font const float TEXT_BASE_WIDTH = ImGui::CalcTextSize("A").x; const float TEXT_BASE_HEIGHT = ImGui::GetTextLineHeightWithSpacing(); @@ -5228,6 +5252,9 @@ ShowDemoWindowColumns(); + if (disable_all) + ImGui::PopDisabled(); + if (disable_indent) ImGui::PopStyleVar(); } @@ -6041,6 +6068,7 @@ HelpMarker("When drawing circle primitives with \"num_segments == 0\" tesselation will be calculated automatically."); ImGui::DragFloat("Global Alpha", &style.Alpha, 0.005f, 0.20f, 1.0f, "%.2f"); // Not exposing zero here so user doesn't "lose" the UI (zero alpha clips all widgets). But application code could have a toggle to switch between zero and non-zero. + ImGui::DragFloat("Disabled Alpha", &style.DisabledAlpha, 0.005f, 0.0f, 1.0f, "%.2f"); ImGui::SameLine(); HelpMarker("Additional alpha multiplier for disabled items (multiply over current value of Alpha)."); ImGui::PopItemWidth(); ImGui::EndTabItem();
diff --git a/imgui_internal.h b/imgui_internal.h index a282ef1..273430f 100644 --- a/imgui_internal.h +++ b/imgui_internal.h
@@ -2420,8 +2420,6 @@ // Parameter stacks IMGUI_API void PushItemFlag(ImGuiItemFlags option, bool enabled); IMGUI_API void PopItemFlag(); - IMGUI_API void PushDisabled(bool disabled = true); - IMGUI_API void PopDisabled(); #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS // If you have old/custom copy-and-pasted widgets that used FocusableItemRegister():