From 82352c1314cb96fd4baadaf5adc21ecdc0353901 Mon Sep 17 00:00:00 2001 From: Vojtech Kral Date: Mon, 4 Mar 2019 15:40:06 +0100 Subject: [PATCH] imgui: Refactor combo boxes. Fix #1895 #1868 --- src/slic3r/GUI/GLGizmo.cpp | 15 ++++++----- src/slic3r/GUI/ImGuiWrapper.cpp | 47 +++++++++++---------------------- src/slic3r/GUI/ImGuiWrapper.hpp | 3 +-- 3 files changed, 26 insertions(+), 39 deletions(-) diff --git a/src/slic3r/GUI/GLGizmo.cpp b/src/slic3r/GUI/GLGizmo.cpp index d4b2bf526..afffd670e 100644 --- a/src/slic3r/GUI/GLGizmo.cpp +++ b/src/slic3r/GUI/GLGizmo.cpp @@ -2286,17 +2286,20 @@ RENDER_AGAIN: m_imgui->text(_(L("Shift + Left (+ drag) - select point(s)"))); m_imgui->text(" "); // vertical gap - std::vector options = {"0.2", "0.4", "0.6", "0.8", "1.0"}; - std::stringstream ss; - ss << std::setprecision(1) << m_new_point_head_diameter; - wxString str = ss.str(); + static const std::vector options = {0.2f, 0.4f, 0.6f, 0.8f, 1.0f}; + static const std::vector options_str = {"0.2", "0.4", "0.6", "0.8", "1.0"}; + int selection = -1; + 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; // 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); - 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) for (auto& point_and_selection : m_editing_mode_cache) if (point_and_selection.second) { diff --git a/src/slic3r/GUI/ImGuiWrapper.cpp b/src/slic3r/GUI/ImGuiWrapper.cpp index 78a3f9db8..9e75eb2ff 100644 --- a/src/slic3r/GUI/ImGuiWrapper.cpp +++ b/src/slic3r/GUI/ImGuiWrapper.cpp @@ -254,44 +254,29 @@ void ImGuiWrapper::text(const wxString &label) this->text(into_u8(label).c_str()); } - -bool ImGuiWrapper::combo(const wxString& label, const std::vector& 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& options, std::string& selection) +bool ImGuiWrapper::combo(const wxString& label, const std::vector& options, int& selection) { // this is to force the label to the left of the widget: text(label); ImGui::SameLine(); - - if (ImGui::BeginCombo("", selection.c_str())) { - for (const std::string& option : options) { - bool is_selected = (selection.empty()) ? false : (option == selection); - if (ImGui::Selectable(option.c_str(), is_selected)) - selection = option; + + int selection_out = -1; + bool res = false; + + const char *selection_str = selection < options.size() ? options[selection].c_str() : ""; + 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(); - return true; + res = true; } - return false; + + selection = selection_out; + return res; } void ImGuiWrapper::disabled_begin(bool disabled) diff --git a/src/slic3r/GUI/ImGuiWrapper.hpp b/src/slic3r/GUI/ImGuiWrapper.hpp index 333b66ed4..019bb610e 100644 --- a/src/slic3r/GUI/ImGuiWrapper.hpp +++ b/src/slic3r/GUI/ImGuiWrapper.hpp @@ -60,8 +60,7 @@ public: void text(const char *label); void text(const std::string &label); void text(const wxString &label); - bool combo(const wxString& label, const std::vector& options, std::string& current_selection); - bool combo(const wxString& label, const std::vector& options, wxString& current_selection); + bool combo(const wxString& label, const std::vector& options, int& selection); // Use -1 to not mark any option as selected void disabled_begin(bool disabled); void disabled_end();