19 namespace visualization {
23 static int g_next_combobox_id = 1;
25 int CalcItemHeight(
const Theme&
theme) {
26 auto em = ImGui::GetTextLineHeight();
27 auto padding = ImGui::GetStyle().FramePadding.y;
28 return int(
std::ceil(em + 2.0 * padding));
40 impl_->imgui_id_ =
"##combobox_" +
std::to_string(g_next_combobox_id++);
44 for (
auto& item : items) {
52 impl_->items_.clear();
53 impl_->current_index_ = 0;
57 impl_->items_.push_back(
name);
58 return int(impl_->items_.size()) - 1;
62 impl_->items_[index] = new_name;
66 for (
size_t i = 0; i < impl_->items_.size(); ++i) {
67 if (impl_->items_[i] == orig_name) {
68 impl_->items_[i] = new_name;
75 for (
size_t i = 0; i < impl_->items_.size(); ++i) {
76 if (impl_->items_[i] ==
name) {
84 if (index >= 0 && index <
int(impl_->items_.size())) {
85 impl_->items_.erase(impl_->items_.begin() + index);
86 if (impl_->current_index_ >=
int(impl_->items_.size())) {
87 impl_->current_index_ = int(impl_->items_.size()) - 1;
93 return static_cast<int>(impl_->items_.size());
97 return impl_->items_[index].c_str();
103 if (impl_->current_index_ >= 0 &&
104 impl_->current_index_ <
int(impl_->items_.size())) {
105 return impl_->items_[impl_->current_index_].c_str();
112 if (index >= 0 && index <
int(impl_->items_.size())) {
113 impl_->current_index_ = index;
118 std::string svalue = value;
119 for (
size_t i = 0; i < impl_->items_.size(); ++i) {
120 if (impl_->items_[i] == svalue) {
129 std::function<
void(
const char*,
int)> on_value_changed) {
130 impl_->on_value_changed_ = on_value_changed;
135 auto button_width = ImGui::GetFrameHeight();
136 auto padding = ImGui::GetStyle().FramePadding;
138 for (
auto& item : impl_->items_) {
139 auto size = ImGui::GetFont()->CalcTextSizeA(
140 float(
context.theme.font_size),
float(constraints.
width),
141 10000.0f, item.c_str());
144 return Size(
width +
int(std::round(button_width + 2.0 * padding.x)),
145 CalcItemHeight(
context.theme));
149 bool value_changed =
false;
152 ImGui::SetCursorScreenPos(
153 ImVec2(
float(
frame.x),
float(
frame.y) - ImGui::GetScrollY()));
155 ImGui::PushStyleColor(
158 ImGui::PushStyleColor(
159 ImGuiCol_ButtonHovered,
161 ImGui::PushStyleColor(
162 ImGuiCol_ButtonActive,
166 ImGui::PushItemWidth(
float(
frame.width));
169 for (
size_t i = 0; i < impl_->items_.size(); ++i) {
170 bool isSelected =
false;
171 if (ImGui::Selectable(impl_->items_[i].c_str(), &isSelected, 0)) {
172 impl_->current_index_ = int(i);
173 value_changed =
true;
174 if (impl_->on_value_changed_) {
179 ImGui::SetItemDefaultFocus();
185 ImGui::PopItemWidth();
188 ImGui::PopStyleColor(3);
int GetNumberOfItems() const
void RemoveItem(const char *name)
Removes the first item matching the given text.
int GetSelectedIndex() const
const char * GetSelectedValue() const
Returns the text of the selected value, or "" if nothing is selected.
int AddItem(const char *name)
Size CalcPreferredSize(const LayoutContext &context, const Constraints &constraints) const override
DrawResult Draw(const DrawContext &context) override
bool SetSelectedValue(const char *value)
void ChangeItem(int index, const char *name)
const char * GetItem(int index) const
void SetOnValueChanged(std::function< void(const char *, int)> on_value_changed)
void SetSelectedIndex(int index)
MiniVec< float, N > ceil(const MiniVec< float, N > &a)
ImVec4 colorToImgui(const Color &color)
Generic file read and write utility for python interface.
std::string to_string(const T &n)
std::function< void(const char *, int)> on_value_changed_
std::vector< std::string > items_