#include "UI_combobox.hpp" UI_combobox::UI_combobox() : UI_x_boxes(UI_COMBOBOX_TYPE) { } UI_combobox::UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector options, size_t selected, std::function on_change) : UI_x_boxes(id, UI_COMBOBOX_TYPE, text, fg_color, bg_color, options), m_selected(selected), m_on_change(on_change) { } UI_combobox::UI_combobox(const json& j) : UI_x_boxes(UI_COMBOBOX_TYPE), m_on_change(nullptr) { from_json(j); } void UI_combobox::set_selected(size_t selected) { if (selected == m_selected) return; m_selected = selected; if (m_on_change != nullptr) { m_on_change(m_id, selected_text()); } } void UI_combobox::set_selected(std::string selected) { for (size_t i = 0; i < m_options.size(); i++) { if (m_options[i] == selected) { set_selected(i); return; } } } size_t UI_combobox::selected() const { return m_selected; } std::string UI_combobox::selected_text() const { return m_options[m_selected]; } void UI_combobox::from_json(const json& j) { UI_x_boxes::from_json(j); std::string selected_str = j.at("selected"); for (size_t i = 0; i < m_options.size(); i++) { if (m_options[i] == selected_str) { m_selected = i; break; } } } json UI_combobox::to_json() const { json j = UI_x_boxes::to_json(); j["selected"] = m_options[m_selected]; return j; }