imgui: Refactor combo boxes. Fix #1895 #1868

This commit is contained in:
Vojtech Kral 2019-03-04 15:40:06 +01:00
parent 8a29ec2204
commit 82352c1314
3 changed files with 26 additions and 39 deletions

View file

@ -2286,17 +2286,20 @@ RENDER_AGAIN:
m_imgui->text(_(L("Shift + Left (+ drag) - select point(s)"))); m_imgui->text(_(L("Shift + Left (+ drag) - select point(s)")));
m_imgui->text(" "); // vertical gap m_imgui->text(" "); // vertical gap
std::vector<wxString> options = {"0.2", "0.4", "0.6", "0.8", "1.0"}; static const std::vector<float> options = {0.2f, 0.4f, 0.6f, 0.8f, 1.0f};
std::stringstream ss; static const std::vector<std::string> options_str = {"0.2", "0.4", "0.6", "0.8", "1.0"};
ss << std::setprecision(1) << m_new_point_head_diameter; int selection = -1;
wxString str = ss.str(); for (size_t i = 0; i < options.size(); i++) {
if (options[i] == m_new_point_head_diameter) { selection = (int)i; }
}
bool old_combo_state = m_combo_box_open; bool old_combo_state = m_combo_box_open;
// The combo is commented out for now, until the feature is supported by backend. // The combo is commented out for now, until the feature is supported by backend.
// m_combo_box_open = m_imgui->combo(_(L("Head diameter")), options, str); // m_combo_box_open = m_imgui->combo(_(L("Head diameter")), options_str, selection);
force_refresh |= (old_combo_state != m_combo_box_open); force_refresh |= (old_combo_state != m_combo_box_open);
float current_number = atof(str); // float current_number = atof(str);
const float current_number = selection < options.size() ? options[selection] : m_new_point_head_diameter;
if (old_combo_state && !m_combo_box_open) // closing the combo must always change the sizes (even if the selection did not change) if (old_combo_state && !m_combo_box_open) // closing the combo must always change the sizes (even if the selection did not change)
for (auto& point_and_selection : m_editing_mode_cache) for (auto& point_and_selection : m_editing_mode_cache)
if (point_and_selection.second) { if (point_and_selection.second) {

View file

@ -254,44 +254,29 @@ void ImGuiWrapper::text(const wxString &label)
this->text(into_u8(label).c_str()); this->text(into_u8(label).c_str());
} }
bool ImGuiWrapper::combo(const wxString& label, const std::vector<std::string>& options, int& selection)
bool ImGuiWrapper::combo(const wxString& label, const std::vector<wxString>& options, wxString& selection)
{
std::string selection_u8 = into_u8(selection);
// this is to force the label to the left of the widget:
text(label);
ImGui::SameLine();
if (ImGui::BeginCombo("", selection_u8.c_str())) {
for (const wxString& option : options) {
std::string option_u8 = into_u8(option);
bool is_selected = (selection_u8.empty()) ? false : (option_u8 == selection_u8);
if (ImGui::Selectable(option_u8.c_str(), is_selected))
selection = option_u8;
}
ImGui::EndCombo();
return true;
}
return false;
}
bool ImGuiWrapper::combo(const wxString& label, const std::vector<std::string>& options, std::string& selection)
{ {
// this is to force the label to the left of the widget: // this is to force the label to the left of the widget:
text(label); text(label);
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::BeginCombo("", selection.c_str())) { int selection_out = -1;
for (const std::string& option : options) { bool res = false;
bool is_selected = (selection.empty()) ? false : (option == selection);
if (ImGui::Selectable(option.c_str(), is_selected)) const char *selection_str = selection < options.size() ? options[selection].c_str() : "";
selection = option; if (ImGui::BeginCombo("", selection_str)) {
for (int i = 0; i < options.size(); i++) {
if (ImGui::Selectable(options[i].c_str(), i == selection)) {
selection_out = i;
} }
}
ImGui::EndCombo(); ImGui::EndCombo();
return true; res = true;
} }
return false;
selection = selection_out;
return res;
} }
void ImGuiWrapper::disabled_begin(bool disabled) void ImGuiWrapper::disabled_begin(bool disabled)

View file

@ -60,8 +60,7 @@ public:
void text(const char *label); void text(const char *label);
void text(const std::string &label); void text(const std::string &label);
void text(const wxString &label); void text(const wxString &label);
bool combo(const wxString& label, const std::vector<std::string>& options, std::string& current_selection); bool combo(const wxString& label, const std::vector<std::string>& options, int& selection); // Use -1 to not mark any option as selected
bool combo(const wxString& label, const std::vector<wxString>& options, wxString& current_selection);
void disabled_begin(bool disabled); void disabled_begin(bool disabled);
void disabled_end(); void disabled_end();