From 6b3ccd3edfcd0ff9fb44060bea092f91b1522795 Mon Sep 17 00:00:00 2001 From: ocornut Date: Tue, 26 May 2015 18:02:42 +0100 Subject: [PATCH] WIP Menus: Added support for disabled sub-menu (#126) --- imgui.cpp | 16 ++++++++++++---- imgui.h | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 3825607f9..8bd962003 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -7343,6 +7343,8 @@ static bool SelectableEx(const char* label, bool selected, const ImVec2& size_ar bool hovered, held; bool pressed = ButtonBehavior(bb_with_spacing, id, &hovered, &held, true, ((flags & ImGuiSelectableFlags_MenuItem) ? ImGuiButtonFlags_PressedOnClick : 0) | ((flags & ImGuiSelectableFlags_Disabled) ? ImGuiButtonFlags_Disabled : 0)); + if (flags & ImGuiSelectableFlags_Disabled) + selected = false; // Render if (hovered || selected) @@ -7574,7 +7576,7 @@ void ImGui::EndMenuBar() window->DC.MenuBarAppending = false; } -bool ImGui::BeginMenu(const char* label) +bool ImGui::BeginMenu(const char* label, bool enabled) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) @@ -7600,7 +7602,7 @@ bool ImGui::BeginMenu(const char* label) window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, style.ItemSpacing * 2.0f); float w = label_size.x; - pressed = SelectableEx(label, opened, ImVec2(w, 0.0f), ImVec2(w, 0.0f), ImGuiSelectableFlags_MenuItem | ImGuiSelectableFlags_DontClosePopups); + pressed = SelectableEx(label, opened, ImVec2(w, 0.0f), ImVec2(w, 0.0f), ImGuiSelectableFlags_MenuItem | ImGuiSelectableFlags_DontClosePopups | (!enabled ? ImGuiSelectableFlags_Disabled : 0)); ImGui::PopStyleVar(); ImGui::SameLine(); window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f); @@ -7610,11 +7612,13 @@ bool ImGui::BeginMenu(const char* label) popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y); float w = window->MenuColumns.DeclColumns(label_size.x, 0.0f, (float)(int)(g.FontSize * 1.20f)); // Feedback to next frame float extra_w = ImMax(0.0f, window->Pos.x + ImGui::GetContentRegionMax().x - pos.x - w); - pressed = SelectableEx(label, opened, ImVec2(w, 0.0f), ImVec2(0.0f, 0.0f), ImGuiSelectableFlags_MenuItem | ImGuiSelectableFlags_DontClosePopups); + pressed = SelectableEx(label, opened, ImVec2(w, 0.0f), ImVec2(0.0f, 0.0f), ImGuiSelectableFlags_MenuItem | ImGuiSelectableFlags_DontClosePopups | (!enabled ? ImGuiSelectableFlags_Disabled : 0)); + if (!enabled) ImGui::PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]); RenderCollapseTriangle(pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * 0.20f, 0.0f), false); + if (!enabled) ImGui::PopStyleColor(); } - bool hovered = IsHovered(window->DC.LastItemRect, id); + bool hovered = enabled && IsHovered(window->DC.LastItemRect, id); if (menuset_opened) g.FocusedWindow = backed_focused_window; @@ -11415,6 +11419,10 @@ static void ShowExampleMenuFile() ImGui::MenuItem(ImGui::GetStyleColName((ImGuiCol)i)); ImGui::EndMenu(); } + if (ImGui::BeginMenu("Disabled", false)) // Disabled + { + IM_ASSERT(0); + } if (ImGui::MenuItem("Quit", "Alt+F4")) {} } diff --git a/imgui.h b/imgui.h index b3a5b9d0b..9c828a5c0 100644 --- a/imgui.h +++ b/imgui.h @@ -304,7 +304,7 @@ namespace ImGui IMGUI_API void EndMainMenuBar(); IMGUI_API bool BeginMenuBar(); // append to menu-bar of current window. only call EndMenuBar() if this returns true! IMGUI_API void EndMenuBar(); - IMGUI_API bool BeginMenu(const char* label); // create a sub-menu entry. only call EndMenu() if this returns true! + IMGUI_API bool BeginMenu(const char* label, bool enabled = true); // create a sub-menu entry. only call EndMenu() if this returns true! IMGUI_API void EndMenu(); IMGUI_API bool MenuItem(const char* label, const char* shortcut = NULL, bool selected = false, bool enabled = true); // return true when activated. shortcuts are displayed for convenience but not processed by ImGui IMGUI_API bool MenuItem(const char* label, const char* shortcut, bool* p_selected, bool enabled = true); // return true when activated + toggle (*p_selected) if p_selected != NULL