From c2ac70973e99848af9f3934e16d7568143b45ed8 Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 28 Aug 2023 17:36:59 +0200 Subject: [PATCH] RangeSelect/MultiSelect: Demo: Make ExampleSelection use ImGuiID. More self-explanatory. --- imgui_demo.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 6fb84c4b..0f10a723 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -2781,17 +2781,17 @@ struct ExampleSelection { // Data ImGuiStorage Storage; // Selection set - int Size; // Number of selected items (== number of 1 in the Storage, maintained by this class). // FIXME-MULTISELECT: Imply more difficult to track with intrusive selection schemes? + int Size; // Number of selected items (== number of 1 in the Storage, maintained by this class). bool QueueDeletion; // Request deleting selected items // Functions ExampleSelection() { Clear(); } void Clear() { Storage.Clear(); Size = 0; QueueDeletion = false; } void Swap(ExampleSelection& rhs) { Storage.Data.swap(rhs.Storage.Data); } - bool Contains(int n) const { return Storage.GetInt((ImGuiID)n, 0) != 0; } - void AddItem(int n) { int* p_int = Storage.GetIntRef((ImGuiID)n, 0); if (*p_int != 0) return; *p_int = 1; Size++; } - void RemoveItem(int n) { int* p_int = Storage.GetIntRef((ImGuiID)n, 0); if (*p_int == 0) return; *p_int = 0; Size--; } - void UpdateItem(int n, bool v) { if (v) AddItem(n); else RemoveItem(n); } + bool Contains(ImGuiID key) const { return Storage.GetInt(key, 0) != 0; } + void AddItem(ImGuiID key) { int* p_int = Storage.GetIntRef(key, 0); if (*p_int != 0) return; *p_int = 1; Size++; } + void RemoveItem(ImGuiID key) { int* p_int = Storage.GetIntRef(key, 0); if (*p_int == 0) return; *p_int = 0; Size--; } + void UpdateItem(ImGuiID key, bool v){ if (v) AddItem(key); else RemoveItem(key); } int GetSize() const { return Size; } void DebugTooltip() { if (ImGui::BeginTooltip()) { for (auto& pair : Storage.Data) if (pair.val_i) ImGui::Text("0x%03X (%d)", pair.key, pair.key); ImGui::EndTooltip(); } } @@ -2965,7 +2965,7 @@ static void ShowDemoWindowMultiSelect() { char label[64]; sprintf(label, "Object %05d: %s", n, random_names[n % IM_ARRAYSIZE(random_names)]); - bool item_is_selected = selection.Contains(n); + bool item_is_selected = selection.Contains((ImGuiID)n); ImGui::SetNextItemSelectionUserData(n); ImGui::Selectable(label, item_is_selected); } @@ -3006,7 +3006,7 @@ static void ShowDemoWindowMultiSelect() { char label[64]; sprintf(label, "Object %05d: %s", n, random_names[n % IM_ARRAYSIZE(random_names)]); - bool item_is_selected = selection.Contains(n); + bool item_is_selected = selection.Contains((ImGuiID)n); ImGui::SetNextItemSelectionUserData(n); ImGui::Selectable(label, item_is_selected); } @@ -3051,7 +3051,7 @@ static void ShowDemoWindowMultiSelect() items.push_back(items_next_id++); if (ImGui::SmallButton("Add 20 items")) { for (int n = 0; n < 20; n++) { items.push_back(items_next_id++); } } ImGui::SameLine(); - if (ImGui::SmallButton("Remove 20 items")) { for (int n = IM_MIN(20, items.Size); n > 0; n--) { selection.RemoveItem(items.Size - 1); items.pop_back(); } } // This is to test + if (ImGui::SmallButton("Remove 20 items")) { for (int n = IM_MIN(20, items.Size); n > 0; n--) { selection.RemoveItem((ImGuiID)(items.Size - 1)); items.pop_back(); } } // This is to test // (1) Extra to support deletion: Submit scrolling range to avoid glitches on deletion const float items_height = ImGui::GetTextLineHeightWithSpacing(); @@ -3077,7 +3077,7 @@ static void ShowDemoWindowMultiSelect() char label[64]; sprintf(label, "Object %05d: %s", item_id, random_names[item_id % IM_ARRAYSIZE(random_names)]); - bool item_is_selected = selection.Contains(n); + bool item_is_selected = selection.Contains((ImGuiID)n); ImGui::SetNextItemSelectionUserData(n); ImGui::Selectable(label, item_is_selected); if (next_focus_item_idx == n) @@ -3119,7 +3119,7 @@ static void ShowDemoWindowMultiSelect() { char label[64]; sprintf(label, "Object %05d: %s", n, random_names[n % IM_ARRAYSIZE(random_names)]); - bool item_is_selected = selection->Contains(n); + bool item_is_selected = selection->Contains((ImGuiID)n); ImGui::SetNextItemSelectionUserData(n); ImGui::Selectable(label, item_is_selected); } @@ -3241,7 +3241,7 @@ static void ShowDemoWindowMultiSelect() ImGui::SameLine(); } - bool item_is_selected = selection.Contains(n); + bool item_is_selected = selection.Contains((ImGuiID)n); ImGui::SetNextItemSelectionUserData(n); if (widget_type == WidgetType_Selectable) {