Pārlūkot izejas kodu

Demo: Assets Browser: add hit spacing, requierd for box-select patterns.

ocornut 1 gadu atpakaļ
vecāks
revīzija
bf01795483
1 mainītis faili ar 21 papildinājumiem un 5 dzēšanām
  1. 21 5
      imgui_demo.cpp

+ 21 - 5
imgui_demo.cpp

@@ -3456,7 +3456,8 @@ static void ShowDemoWindowMultiSelect()
                         // Drag and Drop
                         // Drag and Drop
                         if (use_drag_drop && ImGui::BeginDragDropSource())
                         if (use_drag_drop && ImGui::BeginDragDropSource())
                         {
                         {
-                            // Write payload with full selection OR single unselected item (only possible with ImGuiMultiSelectFlags_SelectOnClickRelease)
+                            // Consider payload to be full selection OR single unselected item.
+                            // (the later is only possible when using ImGuiMultiSelectFlags_SelectOnClickRelease)
                             if (ImGui::GetDragDropPayload() == NULL)
                             if (ImGui::GetDragDropPayload() == NULL)
                             {
                             {
                                 ImVector<int> payload_items;
                                 ImVector<int> payload_items;
@@ -9690,8 +9691,10 @@ struct ExampleAssetsBrowser
 {
 {
     // Options
     // Options
     bool                    ShowTypeOverlay = true;
     bool                    ShowTypeOverlay = true;
+    bool                    AllowDragUnselected = false;
     float                   IconSize = 32.0f;
     float                   IconSize = 32.0f;
-    int                     IconSpacing = 7;
+    int                     IconSpacing = 10;
+    int                     IconHitSpacing = 4; // Increase hit-spacing if you want to make it possible to clear or box-select from gaps. Some spacing is required to able to amend with Shift+box-select. Value is small in Explorer.
     bool                    StretchSpacing = true;
     bool                    StretchSpacing = true;
 
 
     // State
     // State
@@ -9751,9 +9754,13 @@ struct ExampleAssetsBrowser
                 ImGui::SeparatorText("Contents");
                 ImGui::SeparatorText("Contents");
                 ImGui::Checkbox("Show Type Overlay", &ShowTypeOverlay);
                 ImGui::Checkbox("Show Type Overlay", &ShowTypeOverlay);
 
 
+                ImGui::SeparatorText("Selection Behavior");
+                ImGui::Checkbox("Allow dragging unselected item", &AllowDragUnselected);
+
                 ImGui::SeparatorText("Layout");
                 ImGui::SeparatorText("Layout");
                 ImGui::SliderFloat("Icon Size", &IconSize, 16.0f, 128.0f, "%.0f");
                 ImGui::SliderFloat("Icon Size", &IconSize, 16.0f, 128.0f, "%.0f");
                 ImGui::SliderInt("Icon Spacing", &IconSpacing, 0, 32);
                 ImGui::SliderInt("Icon Spacing", &IconSpacing, 0, 32);
+                ImGui::SliderInt("Icon Hit Spacing", &IconHitSpacing, 0, 32);
                 ImGui::Checkbox("Stretch Spacing", &StretchSpacing);
                 ImGui::Checkbox("Stretch Spacing", &StretchSpacing);
                 ImGui::PopItemWidth();
                 ImGui::PopItemWidth();
                 ImGui::EndMenu();
                 ImGui::EndMenu();
@@ -9820,6 +9827,9 @@ struct ExampleAssetsBrowser
 
 
             // Multi-select
             // Multi-select
             ImGuiMultiSelectFlags ms_flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_ClearOnClickWindowVoid;
             ImGuiMultiSelectFlags ms_flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_ClearOnClickWindowVoid;
+            if (AllowDragUnselected)
+                ms_flags |= ImGuiMultiSelectFlags_SelectOnClickRelease; // To allow dragging an unselected item without altering selection.
+
             ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(ms_flags);
             ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(ms_flags);
             ExampleSelectionAdapter selection_adapter;
             ExampleSelectionAdapter selection_adapter;
             selection_adapter.Data = this;
             selection_adapter.Data = this;
@@ -9830,7 +9840,8 @@ struct ExampleAssetsBrowser
             // But it is necessary for two reasons:
             // But it is necessary for two reasons:
             // - Selectables uses it by default to visually fill the space between two items.
             // - Selectables uses it by default to visually fill the space between two items.
             // - The vertical spacing would be measured by Clipper to calculate line height if we didn't provide it explicitly (here we do).
             // - The vertical spacing would be measured by Clipper to calculate line height if we didn't provide it explicitly (here we do).
-            ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(item_spacing, item_spacing));
+            const float selectable_spacing = IM_MAX(floorf(item_spacing) - IconHitSpacing, 0.0f);
+            ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(selectable_spacing, selectable_spacing));
 
 
             // Rendering parameters
             // Rendering parameters
             const ImU32 icon_bg_color = IM_COL32(48, 48, 48, 128);
             const ImU32 icon_bg_color = IM_COL32(48, 48, 48, 128);
@@ -9875,8 +9886,13 @@ struct ExampleAssetsBrowser
                         // Drag and drop
                         // Drag and drop
                         if (ImGui::BeginDragDropSource())
                         if (ImGui::BeginDragDropSource())
                         {
                         {
-                            ImGui::SetDragDropPayload("ASSETS_BROWSER_ITEMS", "Dummy", 5);
-                            ImGui::Text("%d assets", Selection.Size);
+                            // Consider payload to be full selection OR single unselected item
+                            // (the later is only possible when using ImGuiMultiSelectFlags_SelectOnClickRelease)
+                            int payload_size = item_is_selected ? Selection.Size : 1;
+                            if (ImGui::GetDragDropPayload() == NULL)
+                                ImGui::SetDragDropPayload("ASSETS_BROWSER_ITEMS", "Dummy", 5); // Dummy payload
+
+                            ImGui::Text("%d assets", payload_size);
                             ImGui::EndDragDropSource();
                             ImGui::EndDragDropSource();
                         }
                         }