diff --git a/client/render/ui_widgets.cpp b/client/render/ui_widgets.cpp index 9124177d..13043dae 100644 --- a/client/render/ui_widgets.cpp +++ b/client/render/ui_widgets.cpp @@ -696,7 +696,7 @@ ImVec2 popup_center = {0, 0}; float popup_available_height = 0; // one selectable option inside the combo modal, tall full-width touch target with an optional description -bool combo_row(const combo_item & item, bool selected) +bool combo_row(const combo_item & item, bool selected, bool checked) { ImGuiWindow * window = ImGui::GetCurrentWindow(); const theme & t = current(); @@ -725,6 +725,12 @@ bool combo_row(const combo_item & item, bool selected) if (selected) draw->AddRectFilled(bb.Min, {bb.Min.x + metrics::combo_padding.x * 0.25f, bb.Max.y}, t.col(t.accent), t.rounding, ImDrawFlags_RoundCornersLeft); + if (checked) + { + const ImVec2 cs = ImGui::CalcTextSize(ICON_FA_CHECK); + draw->AddText({bb.Max.x - metrics::combo_padding.x - cs.x, bb.Min.y + (row_h - cs.y) * 0.5f}, t.col(t.accent), ICON_FA_CHECK); + } + const float tx = bb.Min.x + metrics::combo_padding.x; const ImU32 name_col = t.col(t.text); const ImVec2 nts = ImGui::CalcTextSize(item.name); @@ -747,15 +753,8 @@ bool combo_row(const combo_item & item, bool selected) return pressed; } -} // namespace -void set_popup_center(const ImVec2 & center, float available_height) -{ - popup_center = center; - popup_available_height = available_height; -} - -bool combo(const char * id, const std::string & title, const std::vector & items, int * selected, float width, const int * default_value) +bool combo_impl(const char * id, const std::string & title, const char * preview, const std::vector & items, int * selected, std::vector * selected_multi, float width, const int * default_value, const std::vector * default_value_multi, bool close_on_click) { ImGuiWindow * window = ImGui::GetCurrentWindow(); if (window->SkipItems or items.empty()) @@ -767,7 +766,7 @@ bool combo(const char * id, const std::string & title, const std::vectorDC.CursorPos; const ImRect bb(pos, pos + size); @@ -790,10 +789,9 @@ bool combo(const char * id, const std::string & title, const std::vector 0) draw->AddRect(bb.Min, bb.Max, t.col(t.border), t.rounding, 0, t.border_size); - const int sel = ImClamp(*selected, 0, int(items.size()) - 1); + const int sel = selected ? ImClamp(*selected, 0, int(items.size()) - 1) : -1; // preview, left aligned and clipped to the box - const char * preview = items[sel].name; const ImVec2 ts = ImGui::CalcTextSize(preview); const float pad_x = metrics::combo_padding.x; draw->PushClipRect(bb.Min, bb.Max, true); @@ -847,14 +845,25 @@ bool combo(const char * id, const std::string & title, const std::vectorsize() and (*selected_multi)[i]; + if (combo_row(items[i], is_selected, is_selected_multi)) { - *selected = i; + if (selected) + *selected = i; + + if (selected_multi) + { + if (selected_multi->size() <= i) + selected_multi->resize(i + 1); + (*selected_multi)[i] = not(*selected_multi)[i]; + } + changed = true; - ImGui::CloseCurrentPopup(); + if (close_on_click) + ImGui::CloseCurrentPopup(); } - if (is_sel and ImGui::IsWindowAppearing()) + if (is_selected and ImGui::IsWindowAppearing()) ImGui::SetScrollHereY(0.5f); // reveal the current value when opening } ImGui::EndChild(); @@ -870,14 +879,42 @@ bool combo(const char * id, const std::string & title, const std::vector & items, int * selected, float width, const int * default_value) +{ + const int sel = ImClamp(*selected, 0, int(items.size()) - 1); + const char * preview = items[sel].name; + + return combo_impl(id, title, preview, items, selected, nullptr, width, default_value, nullptr, true); +} + +bool combo_multi(const char * id, const std::string & title, const std::string & preview, const std::vector & items, std::vector * selected, float width, const std::vector * default_value) +{ + return combo_impl(id, title, preview.c_str(), items, nullptr, selected, width, nullptr, default_value, false); +} void chip(const std::string & label, chip_style style, bool dot, float height) { diff --git a/client/render/ui_widgets.h b/client/render/ui_widgets.h index f3aeccaa..1d6d3b70 100644 --- a/client/render/ui_widgets.h +++ b/client/render/ui_widgets.h @@ -97,6 +97,9 @@ struct combo_item // Combo box whose options open in a modal list, *selected is the chosen index, title labels the modal bool combo(const char * id, const std::string & title, const std::vector & items, int * selected, float width = 0, const int * default_value = nullptr); +// Combo box with multiple selection +bool combo_multi(const char * id, const std::string & title, const std::string & preview, const std::vector & items, std::vector * selected, float width, const std::vector * default_value); + // Centre (display coords) where combo and begin_modal popups open, plus the popup-layer height // available_height of 0 leaves the list unbounded, set once per frame void set_popup_center(const ImVec2 & center, float available_height = 0);