Răsfoiți Sursa

MenuItem() draft for popups, with Selected option (wip #126)

ocornut 10 ani în urmă
părinte
comite
f46557d2d6
2 a modificat fișierele cu 74 adăugiri și 16 ștergeri
  1. 69 16
      imgui.cpp
  2. 5 0
      imgui.h

+ 69 - 16
imgui.cpp

@@ -7220,6 +7220,41 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v
     return value_changed;
 }
 
+bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected)
+{
+    (void)shortcut; // FIXME-MENU: Shortcut are not supported yet. Argument is reserved.
+
+    ImGuiState& g = *GImGui;
+    ImGuiWindow* window = GetCurrentWindow();
+    if (window->SkipItems)
+        return false;
+
+    ImVec2 pos = ImGui::GetCursorScreenPos();
+    const ImVec2 label_size = CalcTextSize(label, NULL, true);
+    const float symbol_spacing = (float)(int)(g.FontSize * 1.50f + 0.5f);
+    const float w = ImMax(label_size.x + symbol_spacing, window->Pos.x + ImGui::GetContentRegionMax().x - window->DC.CursorPos.x); // Feedback to next frame
+    bool ret = ImGui::Selectable(label, false, ImVec2(w, 0.0f));
+
+    if (selected)
+    {
+        pos.x = window->Pos.x + ImGui::GetContentRegionMax().x - g.FontSize;
+        RenderCheckMark(pos, window->Color(ImGuiCol_Text));
+    }
+
+    return ret;
+}
+
+bool ImGui::MenuItem(const char* label, const char* shortcut, bool* p_selected)
+{
+    if (ImGui::MenuItem(label, shortcut, p_selected ? *p_selected : false))
+    {
+        if (p_selected)
+            *p_selected = !*p_selected;
+        return true;
+    }
+    return false;
+}
+
 // A little colored square. Return true when clicked.
 bool ImGui::ColorButton(const ImVec4& col, bool small_height, bool outline_border)
 {
@@ -10007,30 +10042,48 @@ void ImGui::ShowTestWindow(bool* opened)
             ImGui::TreePop();
         }
 
-        if (ImGui::TreeNode("Popup"))
+        if (ImGui::TreeNode("Popup, Menus"))
         {
-            static bool popup_open = false;
             static int selected_fish = -1;
-            const char* fishes[] = { "Bream", "Haddock", "Mackerel", "Pollock", "Tilefish" };
-            if (ImGui::Button("Select.."))
-                popup_open = true;
-            ImGui::SameLine();
-            ImGui::Text(selected_fish == -1 ? "<None>" : fishes[selected_fish]);
-            if (popup_open)
+            const char* names[] = { "BreamXYZA", "Haddk", "Mackerel", "Pollock", "Tilefish" };
+            static bool toggles[] = { true, false, false, false, false };
+
             {
-                ImGui::BeginPopup(&popup_open);
-                ImGui::Text("Aquarium");
-                ImGui::Separator();
-                for (int i = 0; i < IM_ARRAYSIZE(fishes); i++)
+                static bool popup_open = false;
+                if (ImGui::Button("Select.."))
+                    popup_open = true;
+                ImGui::SameLine();
+                ImGui::Text(selected_fish == -1 ? "<None>" : names[selected_fish]);
+                if (popup_open)
                 {
-                    if (ImGui::Selectable(fishes[i], false))
+                    ImGui::BeginPopup(&popup_open);
+                    ImGui::Text("Aquarium");
+                    ImGui::Separator();
+                    for (int i = 0; i < IM_ARRAYSIZE(names); i++)
                     {
-                        selected_fish = i;
-                        popup_open = false;
+                        if (ImGui::Selectable(names[i]))
+                        {
+                            selected_fish = i;
+                            popup_open = false;
+                        }
                     }
+                    ImGui::EndPopup();
+                }
+            }
+            {
+                static bool popup_open = false;
+                if (ImGui::Button("Toggle.."))
+                    popup_open = true;
+                if (popup_open)
+                {
+                    ImGui::BeginPopup(&popup_open);
+                    for (int i = 0; i < IM_ARRAYSIZE(names); i++)
+                        if (ImGui::MenuItem(names[i], "", &toggles[i]))
+                            popup_open = false;
+                    ImGui::EndPopup();
                 }
-                ImGui::EndPopup();
             }
+
             ImGui::TreePop();
         }
 

+ 5 - 0
imgui.h

@@ -357,6 +357,11 @@ namespace ImGui
     IMGUI_API bool          ListBoxHeader(const char* label, int items_count, int height_in_items = -1); // "
     IMGUI_API void          ListBoxFooter();                                                    // terminate the scrolling region
 
+    // Widgets: Menus
+	// FIXME-WIP: v1.39 in development
+    IMGUI_API bool          MenuItem(const char* label, const char* shortcut = NULL, bool selected = NULL); // bool enabled = true
+    IMGUI_API bool          MenuItem(const char* label, const char* shortcut, bool* p_selected); // bool enabled = true
+
     // Widgets: Value() Helpers. Output single value in "name: value" format (tip: freely declare your own within the ImGui namespace!)
     IMGUI_API void          Value(const char* prefix, bool b);
     IMGUI_API void          Value(const char* prefix, int v);