MultiSelect: Box-Select: added support for ImGuiMultiSelectFlags_BoxSelect.
(v11)
FIXME: broken on clipping demo.
diff --git a/imgui.h b/imgui.h
index 53a51c4..701d995 100644
--- a/imgui.h
+++ b/imgui.h
@@ -2733,12 +2733,13 @@
ImGuiMultiSelectFlags_None = 0,
ImGuiMultiSelectFlags_SingleSelect = 1 << 0, // Disable selecting more than one item. This is available to allow single-selection code to use same code/logic is desired, but may not be very useful.
ImGuiMultiSelectFlags_NoSelectAll = 1 << 1, // Disable CTRL+A shortcut to set RequestSelectAll
- ImGuiMultiSelectFlags_ClearOnEscape = 1 << 2, // Clear selection when pressing Escape while scope is focused.
- ImGuiMultiSelectFlags_ClearOnClickVoid = 1 << 3, // Clear selection when clicking on empty location within scope.
- ImGuiMultiSelectFlags_ScopeWindow = 1 << 4, // Scope for _ClearOnClickVoid and _BoxSelect is whole window (Default). Use if (use if BeginMultiSelect() covers a whole window.
- ImGuiMultiSelectFlags_ScopeRect = 1 << 5, // Scope for _ClearOnClickVoid and _BoxSelect is rectangle covering submitted items. Use if multiple BeginMultiSelect() are used in the same host window.
- ImGuiMultiSelectFlags_SelectOnClick = 1 << 7, // Apply selection on mouse down when clicking on unselected item. (Default)
- ImGuiMultiSelectFlags_SelectOnClickRelease = 1 << 8, // Apply selection on mouse release when clicking an unselected item. Allow dragging an unselected item without altering selection.
+ ImGuiMultiSelectFlags_BoxSelect = 1 << 2, // Enable box-selection. Box-selection works better with little bit of spacing between items hit-box in order to be able to aim at empty space.
+ ImGuiMultiSelectFlags_ClearOnEscape = 1 << 4, // Clear selection when pressing Escape while scope is focused.
+ ImGuiMultiSelectFlags_ClearOnClickVoid = 1 << 5, // Clear selection when clicking on empty location within scope.
+ ImGuiMultiSelectFlags_ScopeWindow = 1 << 6, // Scope for _ClearOnClickVoid and _BoxSelect is whole window (Default). Use if (use if BeginMultiSelect() covers a whole window.
+ ImGuiMultiSelectFlags_ScopeRect = 1 << 7, // Scope for _ClearOnClickVoid and _BoxSelect is rectangle covering submitted items. Use if multiple BeginMultiSelect() are used in the same host window.
+ ImGuiMultiSelectFlags_SelectOnClick = 1 << 8, // Apply selection on mouse down when clicking on unselected item. (Default)
+ ImGuiMultiSelectFlags_SelectOnClickRelease = 1 << 9, // Apply selection on mouse release when clicking an unselected item. Allow dragging an unselected item without altering selection.
};
// Multi-selection system
diff --git a/imgui_demo.cpp b/imgui_demo.cpp
index 00b02eb..a979726 100644
--- a/imgui_demo.cpp
+++ b/imgui_demo.cpp
@@ -3299,14 +3299,13 @@
for (int selection_scope_n = 0; selection_scope_n < SCOPES_COUNT; selection_scope_n++)
{
+ ImGui::PushID(selection_scope_n);
ExampleSelection* selection = &selections_data[selection_scope_n];
-
ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags);
selection->ApplyRequests(ms_io, &selection_adapter, ITEMS_COUNT);
ImGui::SeparatorText("Selection scope");
ImGui::Text("Selection size: %d/%d", selection->GetSize(), ITEMS_COUNT);
- ImGui::PushID(selection_scope_n);
for (int n = 0; n < ITEMS_COUNT; n++)
{
@@ -3325,6 +3324,7 @@
ImGui::TreePop();
}
+ // See ShowExampleAppAssetsBrowser()
if (ImGui::TreeNode("Multi-Select (tiled assets browser)"))
{
ImGui::BulletText("See 'Examples->Assets Browser' in menu");
@@ -3361,6 +3361,7 @@
ImGui::Checkbox("Show color button", &show_color_button);
ImGui::CheckboxFlags("ImGuiMultiSelectFlags_SingleSelect", &flags, ImGuiMultiSelectFlags_SingleSelect);
ImGui::CheckboxFlags("ImGuiMultiSelectFlags_NoSelectAll", &flags, ImGuiMultiSelectFlags_NoSelectAll);
+ ImGui::CheckboxFlags("ImGuiMultiSelectFlags_BoxSelect", &flags, ImGuiMultiSelectFlags_BoxSelect);
ImGui::CheckboxFlags("ImGuiMultiSelectFlags_ClearOnEscape", &flags, ImGuiMultiSelectFlags_ClearOnEscape);
ImGui::CheckboxFlags("ImGuiMultiSelectFlags_ClearOnClickVoid", &flags, ImGuiMultiSelectFlags_ClearOnClickVoid);
if (ImGui::CheckboxFlags("ImGuiMultiSelectFlags_ScopeWindow", &flags, ImGuiMultiSelectFlags_ScopeWindow) && (flags & ImGuiMultiSelectFlags_ScopeWindow))
diff --git a/imgui_internal.h b/imgui_internal.h
index 6f930ae..2ce16fb 100644
--- a/imgui_internal.h
+++ b/imgui_internal.h
@@ -1722,6 +1722,10 @@
ImGuiMultiSelectFlags Flags;
ImVec2 ScopeRectMin;
ImVec2 BackupCursorMaxPos;
+ ImGuiID BoxSelectId;
+ ImRect BoxSelectRectCurr; // Selection rectangle in absolute coordinates (derived from Storage->BoxSelectStartPosRel + MousePos)
+ ImRect BoxSelectRectPrev;
+ ImGuiSelectionUserData BoxSelectLastitem;
ImGuiKeyChord KeyMods;
bool LoopRequestClear;
bool LoopRequestSelectAll;
@@ -1733,7 +1737,7 @@
bool RangeDstPassedBy; // Set by the item that matches NavJustMovedToId when IsSetRange is set.
ImGuiMultiSelectTempData() { Clear(); }
- void Clear() { size_t io_sz = sizeof(IO); IO.Clear(); memset((void*)(&IO + 1), 0, sizeof(*this) - io_sz); } // Zero-clear except IO
+ void Clear() { size_t io_sz = sizeof(IO); IO.Clear(); memset((void*)(&IO + 1), 0, sizeof(*this) - io_sz); BoxSelectLastitem = -1; } // Zero-clear except IO
};
// Persistent storage for multi-select (as long as selection is alive)
@@ -1747,8 +1751,15 @@
ImGuiSelectionUserData RangeSrcItem; //
ImGuiSelectionUserData NavIdItem; // SetNextItemSelectionUserData() value for NavId (if part of submitted items)
+ bool BoxSelectActive;
+ bool BoxSelectStarting;
+ bool BoxSelectFromVoid;
+ ImGuiKeyChord BoxSelectKeyMods : 16; // Latched key-mods for box-select logic.
+ ImVec2 BoxSelectStartPosRel; // Start position in window-relative space (to support scrolling)
+ ImVec2 BoxSelectEndPosRel; // End position in window-relative space
+
ImGuiMultiSelectState() { Init(0); }
- void Init(ImGuiID id) { Window = NULL; ID = id; LastFrameActive = 0; RangeSelected = NavIdSelected = -1; RangeSrcItem = NavIdItem = ImGuiSelectionUserData_Invalid; }
+ void Init(ImGuiID id) { Window = NULL; ID = id; LastFrameActive = 0; RangeSelected = NavIdSelected = -1; RangeSrcItem = NavIdItem = ImGuiSelectionUserData_Invalid; BoxSelectActive = BoxSelectStarting = BoxSelectFromVoid = false; BoxSelectKeyMods = 0; }
};
#endif // #ifdef IMGUI_HAS_MULTI_SELECT
@@ -3087,6 +3098,7 @@
inline ImRect WindowRectAbsToRel(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x - off.x, r.Min.y - off.y, r.Max.x - off.x, r.Max.y - off.y); }
inline ImRect WindowRectRelToAbs(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x + off.x, r.Min.y + off.y, r.Max.x + off.x, r.Max.y + off.y); }
inline ImVec2 WindowPosRelToAbs(ImGuiWindow* window, const ImVec2& p) { ImVec2 off = window->DC.CursorStartPos; return ImVec2(p.x + off.x, p.y + off.y); }
+ inline ImVec2 WindowPosAbsToRel(ImGuiWindow* window, const ImVec2& p) { ImVec2 off = window->DC.CursorStartPos; return ImVec2(p.x - off.x, p.y - off.y); }
// Windows: Display Order and Focus Order
IMGUI_API void FocusWindow(ImGuiWindow* window, ImGuiFocusRequestFlags flags = 0);
diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp
index dade5fb..ec4f537 100644
--- a/imgui_widgets.cpp
+++ b/imgui_widgets.cpp
@@ -7103,6 +7103,8 @@
//-------------------------------------------------------------------------
// [SECTION] Widgets: Multi-Select support
//-------------------------------------------------------------------------
+// - DebugLogMultiSelectRequests() [Internal]
+// - BoxSelectStart() [Internal]
// - BeginMultiSelect()
// - EndMultiSelect()
// - SetNextItemSelectionUserData()
@@ -7122,6 +7124,15 @@
}
}
+static void BoxSelectStart(ImGuiMultiSelectState* storage, ImGuiSelectionUserData clicked_item)
+{
+ ImGuiContext& g = *GImGui;
+ storage->BoxSelectStarting = true; // Consider starting box-select.
+ storage->BoxSelectFromVoid = (clicked_item == ImGuiSelectionUserData_Invalid);
+ storage->BoxSelectKeyMods = g.IO.KeyMods;
+ storage->BoxSelectStartPosRel = storage->BoxSelectEndPosRel = ImGui::WindowPosAbsToRel(g.CurrentWindow, g.IO.MousePos);
+}
+
// Return ImGuiMultiSelectIO structure. Lifetime: valid until corresponding call to EndMultiSelect().
ImGuiMultiSelectIO* ImGui::BeginMultiSelect(ImGuiMultiSelectFlags flags)
{
@@ -7193,6 +7204,38 @@
request_select_all = true;
}
+ // Box-select handling: update active state.
+ if (flags & ImGuiMultiSelectFlags_BoxSelect)
+ {
+ ms->BoxSelectId = GetID("##BoxSelect");
+ KeepAliveID(ms->BoxSelectId);
+
+ // BoxSelectStarting is set by MultiSelectItemFooter() when considering a possible box-select. We validate it here and lock geometry.
+ if (storage->BoxSelectStarting && IsMouseDragPastThreshold(0))
+ {
+ storage->BoxSelectActive = true;
+ storage->BoxSelectStarting = false;
+ SetActiveID(ms->BoxSelectId, window);
+ if (storage->BoxSelectFromVoid && (storage->BoxSelectKeyMods & ImGuiMod_Shift) == 0)
+ request_clear = true;
+ }
+ else if ((storage->BoxSelectStarting || storage->BoxSelectActive) && g.IO.MouseDown[0] == false)
+ {
+ storage->BoxSelectActive = storage->BoxSelectStarting = false;
+ if (g.ActiveId == ms->BoxSelectId)
+ ClearActiveID();
+ }
+ if (storage->BoxSelectActive)
+ {
+ ImVec2 start_pos_abs = WindowPosRelToAbs(window, storage->BoxSelectStartPosRel);
+ ImVec2 prev_end_pos_abs = WindowPosRelToAbs(window, storage->BoxSelectEndPosRel);
+ ms->BoxSelectRectPrev.Min = ImMin(start_pos_abs, prev_end_pos_abs);
+ ms->BoxSelectRectPrev.Max = ImMax(start_pos_abs, prev_end_pos_abs);
+ ms->BoxSelectRectCurr.Min = ImMin(start_pos_abs, g.IO.MousePos);
+ ms->BoxSelectRectCurr.Max = ImMax(start_pos_abs, g.IO.MousePos);
+ }
+ }
+
if (request_clear || request_select_all)
ms->IO.Requests.push_back(ImGuiSelectionRequest(request_select_all ? ImGuiSelectionRequestType_SelectAll : ImGuiSelectionRequestType_Clear));
ms->LoopRequestClear = request_clear;
@@ -7228,6 +7271,15 @@
storage->NavIdItem = ImGuiSelectionUserData_Invalid;
storage->NavIdSelected = -1;
}
+
+ // Box-select: render selection rectangle
+ // FIXME-MULTISELECT: Scroll on box-select
+ if ((ms->Flags & ImGuiMultiSelectFlags_BoxSelect) && storage->BoxSelectActive)
+ {
+ ms->Storage->BoxSelectEndPosRel = WindowPosAbsToRel(window, g.IO.MousePos);
+ window->DrawList->AddRectFilled(ms->BoxSelectRectCurr.Min, ms->BoxSelectRectCurr.Max, GetColorU32(ImGuiCol_SeparatorHovered, 0.30f)); // FIXME-MULTISELECT: Styling
+ window->DrawList->AddRect(ms->BoxSelectRectCurr.Min, ms->BoxSelectRectCurr.Max, GetColorU32(ImGuiCol_NavHighlight)); // FIXME-MULTISELECT: Styling
+ }
}
if (ms->IsEndIO == false)
@@ -7240,6 +7292,10 @@
// We specifically test for IsMouseDragPastThreshold(0) == false to allow box-selection!
if (scope_hovered && g.HoveredId == 0)
{
+ if (ms->Flags & ImGuiMultiSelectFlags_BoxSelect)
+ if (!storage->BoxSelectActive && !storage->BoxSelectStarting && g.IO.MouseClickedCount[0] == 1)
+ BoxSelectStart(storage, ImGuiSelectionUserData_Invalid);
+
if (ms->Flags & ImGuiMultiSelectFlags_ClearOnClickVoid)
if (IsMouseReleased(0) && IsMouseDragPastThreshold(0) == false && g.IO.KeyMods == ImGuiMod_None)
{
@@ -7385,6 +7441,26 @@
selected = pressed = true;
}
+ // Box-select handling
+ if (ms->Storage->BoxSelectActive)
+ {
+ const bool rect_overlap_curr = ms->BoxSelectRectCurr.Overlaps(g.LastItemData.Rect);
+ const bool rect_overlap_prev = ms->BoxSelectRectPrev.Overlaps(g.LastItemData.Rect);
+ if ((rect_overlap_curr && !rect_overlap_prev && !selected) || (rect_overlap_prev && !rect_overlap_curr))
+ {
+ selected = !selected;
+ ImGuiSelectionRequest req(ImGuiSelectionRequestType_SetRange);
+ req.RangeFirstItem = req.RangeLastItem = item_data;
+ req.RangeSelected = selected;
+ ImGuiSelectionRequest* prev_req = (ms->IO.Requests.Size > 0) ? &ms->IO.Requests.Data[ms->IO.Requests.Size - 1] : NULL;
+ if (prev_req && prev_req->Type == ImGuiSelectionRequestType_SetRange && prev_req->RangeLastItem == ms->BoxSelectLastitem && prev_req->RangeSelected == selected)
+ prev_req->RangeLastItem = item_data; // Merge span into same request
+ else
+ ms->IO.Requests.push_back(req);
+ }
+ ms->BoxSelectLastitem = item_data;
+ }
+
// Right-click handling: this could be moved at the Selectable() level.
// FIXME-MULTISELECT: See https://github.com/ocornut/imgui/pull/5816
if (hovered && IsMouseClicked(1))
@@ -7407,6 +7483,12 @@
// Alter selection
if (pressed && (!enter_pressed || !selected))
{
+ // Box-select
+ ImGuiInputSource input_source = (g.NavJustMovedToId == id || g.NavActivateId == id) ? g.NavInputSource : ImGuiInputSource_Mouse;
+ if (ms->Flags & ImGuiMultiSelectFlags_BoxSelect)
+ if (selected == false && !storage->BoxSelectActive && !storage->BoxSelectStarting && input_source == ImGuiInputSource_Mouse && g.IO.MouseClickedCount[0] == 1)
+ BoxSelectStart(storage, item_data);
+
//----------------------------------------------------------------------------------------
// ACTION | Begin | Pressed/Activated | End
//----------------------------------------------------------------------------------------
@@ -7424,7 +7506,6 @@
// Mouse Pressed: Ctrl+Shift | n/a | Dst=item, Sel=!Sel => SetRange Src-Dst
//----------------------------------------------------------------------------------------
- const ImGuiInputSource input_source = (g.NavJustMovedToId == id || g.NavActivateId == id) ? g.NavInputSource : ImGuiInputSource_Mouse;
bool request_clear = false;
if (is_singleselect)
request_clear = true;
@@ -7492,6 +7573,7 @@
return;
Text("RangeSrcItem = %" IM_PRId64 " (0x%" IM_PRIX64 "), RangeSelected = %d", storage->RangeSrcItem, storage->RangeSrcItem, storage->RangeSelected);
Text("NavIdItem = %" IM_PRId64 " (0x%" IM_PRIX64 "), NavIdSelected = %d", storage->NavIdItem, storage->NavIdItem, storage->NavIdSelected);
+ Text("BoxSelect Starting = %d, Active %d", storage->BoxSelectStarting, storage->BoxSelectActive);
TreePop();
#else
IM_UNUSED(storage);