From a94c952b40d36b1505fb77b87c0dd739e1034659 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Wed, 6 May 2020 20:45:57 +0200 Subject: [PATCH] Search: 1. To the imGui Dialog added icons instead of settings type 2. Fixed a search trough the options with # symbol in opt_key 3. Deleted "Type" checkbox from the Search Windows and added "Search in English" checkbox (but without implementation for this moment) Layouts: 1. Fixed a visibility of the "Switch to Settings" toolbar icon for the old layout mode --- src/imgui/imconfig.h | 7 +++ src/slic3r/GUI/GLCanvas3D.cpp | 7 +-- src/slic3r/GUI/ImGuiWrapper.cpp | 89 ++++++++++++++++++++++++++++++++- src/slic3r/GUI/ImGuiWrapper.hpp | 1 + src/slic3r/GUI/Plater.cpp | 2 + src/slic3r/GUI/Search.cpp | 61 ++++++++++++---------- src/slic3r/GUI/Search.hpp | 7 ++- 7 files changed, 142 insertions(+), 32 deletions(-) diff --git a/src/imgui/imconfig.h b/src/imgui/imconfig.h index c425bbef20..eb47459eab 100644 --- a/src/imgui/imconfig.h +++ b/src/imgui/imconfig.h @@ -107,6 +107,13 @@ namespace ImGui const char ColorMarkerStart = 0x2; // STX const char ColorMarkerEnd = 0x3; // ETX + // Special ASCII characters are used here as a ikons markers + const char PrintIconMarker = 0x4; + const char PrinterIconMarker = 0x5; + const char PrinterSlaIconMarker = 0x6; + const char FilamentIconMarker = 0x7; + const char MaterialIconMarker = 0x8; + // void MyFunction(const char* name, const MyMatrix44& v); } diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 6e397aef77..1abe6bf9ff 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -4909,7 +4909,8 @@ bool GLCanvas3D::_init_main_toolbar() "\n" + "[" + GUI::shortkey_ctrl_prefix() + "4] - " + _u8L("Printer Settings Tab") ; item.sprite_id = 10; item.enabling_callback = GLToolbarItem::Default_Enabling_Callback; - item.visibility_callback = [this]() { return wxGetApp().app_config->get("old_settings_layout_mode") != "1"; }; + item.visibility_callback = [this]() { return (wxGetApp().app_config->get("new_settings_layout_mode") == "1" || + wxGetApp().app_config->get("dlg_settings_layout_mode") == "1"); }; item.left.action_callback = [this]() { wxGetApp().mainframe->select_tab(); }; if (!m_main_toolbar.add_item(item)) return false; @@ -4920,7 +4921,7 @@ bool GLCanvas3D::_init_main_toolbar() item.name = "layersediting"; item.icon_filename = "layers_white.svg"; item.tooltip = _utf8(L("Variable layer height")); - item.sprite_id = /*10*/11; + item.sprite_id = 11; item.left.toggable = true; item.left.action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_LAYERSEDITING)); }; item.visibility_callback = [this]()->bool @@ -4942,7 +4943,7 @@ bool GLCanvas3D::_init_main_toolbar() item.name = "search"; item.icon_filename = "search_.svg"; item.tooltip = _utf8(L("Search")) + " [" + GUI::shortkey_ctrl_prefix() + "F]"; - item.sprite_id = /*11*/12; + item.sprite_id = 12; item.left.render_callback = [this](float left, float right, float, float) { if (m_canvas != nullptr) { diff --git a/src/slic3r/GUI/ImGuiWrapper.cpp b/src/slic3r/GUI/ImGuiWrapper.cpp index ea8b2afd38..f4788693e9 100644 --- a/src/slic3r/GUI/ImGuiWrapper.cpp +++ b/src/slic3r/GUI/ImGuiWrapper.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -27,10 +28,21 @@ #include "I18N.hpp" #include "Search.hpp" +#include "nanosvg/nanosvg.h" +#include "nanosvg/nanosvgrast.h" + namespace Slic3r { namespace GUI { +static const std::map font_icons = { + {ImGui::PrintIconMarker , "cog" }, + {ImGui::PrinterIconMarker , "printer" }, + {ImGui::PrinterSlaIconMarker, "sla_printer"}, + {ImGui::FilamentIconMarker , "spool" }, + {ImGui::MaterialIconMarker , "resin" } +}; + ImGuiWrapper::ImGuiWrapper() : m_glyph_ranges(nullptr) , m_font_cjk(false) @@ -735,9 +747,9 @@ void ImGuiWrapper::search_list(const ImVec2& size_, bool (*items_getter)(int, co // add checkboxes for show/hide Categories and Groups text(_L("Use for search")+":"); - check_box(_L("Type"), view_params.type); check_box(_L("Category"), view_params.category); check_box(_L("Group"), view_params.group); + check_box(_L("Search in English"), view_params.english); } void ImGuiWrapper::disabled_begin(bool disabled) @@ -791,6 +803,59 @@ static const ImWchar ranges_keyboard_shortcuts[] = }; #endif // __APPLE__ + +std::vector ImGuiWrapper::load_svg(const std::string& bitmap_name, unsigned target_width, unsigned target_height) +{ +#ifdef __APPLE__ + // Note: win->GetContentScaleFactor() is not used anymore here because it tends to + // return bogus results quite often (such as 1.0 on Retina or even 0.0). + // We're using the max scaling factor across all screens because it's very likely to be good enough. + double scale = mac_max_scaling_factor(); +#else + double scale = 1.0; +#endif + std::vector empty_vector; + +#ifdef __WXMSW__ + std::string folder = "white\\"; +#else + std::string folder = "white/"; +#endif + if (!boost::filesystem::exists(Slic3r::var(folder + bitmap_name + ".svg"))) + folder.clear(); + + NSVGimage* image = ::nsvgParseFromFile(Slic3r::var(folder + bitmap_name + ".svg").c_str(), "px", 96.0f); + if (image == nullptr) + return empty_vector; + + target_height != 0 ? target_height *= scale : target_width *= scale; + + float svg_scale = target_height != 0 ? + (float)target_height / image->height : target_width != 0 ? + (float)target_width / image->width : 1; + + int width = (int)(svg_scale * image->width + 0.5f); + int height = (int)(svg_scale * image->height + 0.5f); + int n_pixels = width * height; + if (n_pixels <= 0) { + ::nsvgDelete(image); + return empty_vector; + } + + NSVGrasterizer* rast = ::nsvgCreateRasterizer(); + if (rast == nullptr) { + ::nsvgDelete(image); + return empty_vector; + } + + std::vector data(n_pixels * 4, 0); + ::nsvgRasterize(rast, image, 0, 0, svg_scale, data.data(), width, height, width * 4); + ::nsvgDeleteRasterizer(rast); + ::nsvgDelete(image); + + return data; +} + void ImGuiWrapper::init_font(bool compress) { destroy_font(); @@ -829,11 +894,33 @@ void ImGuiWrapper::init_font(bool compress) } #endif + float font_scale = m_font_size/15; + int icon_sz = lround(16 * font_scale); // default size of icon is 16 px + + int rect_id = io.Fonts->CustomRects.Size; // id of the rectangle added next + // add rectangles for the icons to the font atlas + for (auto& icon : font_icons) + io.Fonts->AddCustomRectFontGlyph(font, icon.first, icon_sz, icon_sz, 3.0 * font_scale + icon_sz); + // Build texture atlas unsigned char* pixels; int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bits (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. + // Fill rectangles from the SVG-icons + for (auto icon : font_icons) { + if (const ImFontAtlas::CustomRect* rect = io.Fonts->GetCustomRectByIndex(rect_id)) { + std::vector raw_data = load_svg(icon.second, icon_sz, icon_sz); + const ImU32* pIn = (ImU32*)raw_data.data(); + for (int y = 0; y < icon_sz; y++) { + ImU32* pOut = (ImU32*)pixels + (rect->Y + y) * width + (rect->X); + for (int x = 0; x < icon_sz; x++) + *pOut++ = *pIn++; + } + } + rect_id++; + } + // Upload texture to graphics system GLint last_texture; glsafe(::glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture)); diff --git a/src/slic3r/GUI/ImGuiWrapper.hpp b/src/slic3r/GUI/ImGuiWrapper.hpp index 6a1e27dcbe..efb8acc9ae 100644 --- a/src/slic3r/GUI/ImGuiWrapper.hpp +++ b/src/slic3r/GUI/ImGuiWrapper.hpp @@ -96,6 +96,7 @@ private: void render_draw_data(ImDrawData *draw_data); bool display_initialized() const; void destroy_font(); + std::vector load_svg(const std::string& bitmap_name, unsigned target_width, unsigned target_height); static const char* clipboard_get(void* user_data); static void clipboard_set(void* user_data, const char* text); diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index da3cc7f013..fc9e5060bf 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -5275,6 +5275,8 @@ void Plater::set_printer_technology(PrinterTechnology printer_technology) wxGetApp().mainframe->update_menubar(); p->update_main_toolbar_tooltips(); + + p->sidebar->get_searcher().set_printer_technology(printer_technology); } void Plater::changed_object(int obj_idx) diff --git a/src/slic3r/GUI/Search.cpp b/src/slic3r/GUI/Search.cpp index 844d2244cf..33f097362a 100644 --- a/src/slic3r/GUI/Search.cpp +++ b/src/slic3r/GUI/Search.cpp @@ -39,6 +39,23 @@ static const std::vector& NameByType() return data; } +static char marker_by_type(Preset::Type type, PrinterTechnology pt) +{ + switch(type) { + case Preset::TYPE_PRINT: + case Preset::TYPE_SLA_PRINT: + return ImGui::PrintIconMarker; + case Preset::TYPE_FILAMENT: + return ImGui::FilamentIconMarker; + case Preset::TYPE_SLA_MATERIAL: + return ImGui::MaterialIconMarker; + case Preset::TYPE_PRINTER: + return pt == ptSLA ? ImGui::PrinterSlaIconMarker : ImGui::PrinterIconMarker; + default: + return ' '; + } +} + void FoundOption::get_marked_label_and_tooltip(const char** label_, const char** tooltip_) const { *label_ = marked_label.c_str(); @@ -106,17 +123,8 @@ void OptionsSearcher::append_options(DynamicPrintConfig* config, Preset::Type ty emplace(opt_key, label); else for (int i = 0; i < cnt; ++i) - emplace(opt_key + "[" + std::to_string(i) + "]", label); - - /*const GroupAndCategory& gc = groups_and_categories[opt_key]; - if (gc.group.IsEmpty() || gc.category.IsEmpty()) - continue; - - if (!label.IsEmpty()) - options.emplace_back(Option{opt_key, type, - label, _(label), - gc.group, _(gc.group), - gc.category, _(gc.category) });*/ + // ! It's very important to use "#". opt_key#n is a real option key used in GroupAndCategory + emplace(opt_key + "#" + std::to_string(i), label); } } @@ -183,19 +191,19 @@ bool OptionsSearcher::search(const std::string& search, bool force/* = false*/) bool full_list = search.empty(); std::wstring sep = L" : "; - const std::vector& name_by_type = NameByType(); - auto get_label = [this, &name_by_type, &sep](const Option& opt) + auto get_label = [this, &sep](const Option& opt) { std::wstring out; + out += marker_by_type(opt.type, printer_technology); const std::wstring *prev = nullptr; for (const std::wstring * const s : { - view_params.type ? &(name_by_type[opt.type]) : nullptr, view_params.category ? &opt.category_local : nullptr, view_params.group ? &opt.group_local : nullptr, &opt.label_local }) if (s != nullptr && (prev == nullptr || *prev != *s)) { - if (! out.empty()) +// if (! out.empty()) + if (out.size()>2) out += sep; out += *s; prev = s; @@ -203,17 +211,18 @@ bool OptionsSearcher::search(const std::string& search, bool force/* = false*/) return out; }; - auto get_label_english = [this, &name_by_type, &sep](const Option& opt) + auto get_label_english = [this, &sep](const Option& opt) { std::wstring out; + out += marker_by_type(opt.type, printer_technology); const std::wstring*prev = nullptr; for (const std::wstring * const s : { - view_params.type ? &name_by_type[opt.type] : nullptr, view_params.category ? &opt.category : nullptr, view_params.group ? &opt.group : nullptr, &opt.label }) if (s != nullptr && (prev == nullptr || *prev != *s)) { - if (! out.empty()) +// if (! out.empty()) + if (out.size()>2) out += sep; out += *s; prev = s; @@ -221,9 +230,9 @@ bool OptionsSearcher::search(const std::string& search, bool force/* = false*/) return out; }; - auto get_tooltip = [this, &name_by_type, &sep](const Option& opt) + auto get_tooltip = [this, &sep](const Option& opt) { - return name_by_type[opt.type] + sep + + return marker_by_type(opt.type, printer_technology) + opt.category_local + sep + opt.group_local + sep + opt.label_local; }; @@ -423,15 +432,15 @@ SearchDialog::SearchDialog(OptionsSearcher* searcher) wxBoxSizer* check_sizer = new wxBoxSizer(wxHORIZONTAL); - check_type = new wxCheckBox(this, wxID_ANY, _L("Type")); check_category = new wxCheckBox(this, wxID_ANY, _L("Category")); check_group = new wxCheckBox(this, wxID_ANY, _L("Group")); + check_english = new wxCheckBox(this, wxID_ANY, _L("Search in English")); wxStdDialogButtonSizer* cancel_btn = this->CreateStdDialogButtonSizer(wxCANCEL); - check_sizer->Add(check_type, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, border); check_sizer->Add(check_category, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, border); check_sizer->Add(check_group, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, border); + check_sizer->Add(check_english, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, border); check_sizer->AddStretchSpacer(border); check_sizer->Add(cancel_btn, 0, wxALIGN_CENTER_VERTICAL); @@ -450,7 +459,7 @@ SearchDialog::SearchDialog(OptionsSearcher* searcher) search_list->Bind(wxEVT_LEFT_UP, &SearchDialog::OnMouseClick, this); search_list->Bind(wxEVT_KEY_DOWN,&SearchDialog::OnKeyDown, this); - check_type ->Bind(wxEVT_CHECKBOX, &SearchDialog::OnCheck, this); + check_english ->Bind(wxEVT_CHECKBOX, &SearchDialog::OnCheck, this); check_category->Bind(wxEVT_CHECKBOX, &SearchDialog::OnCheck, this); check_group ->Bind(wxEVT_CHECKBOX, &SearchDialog::OnCheck, this); @@ -470,9 +479,9 @@ void SearchDialog::Popup(wxPoint position /*= wxDefaultPosition*/) update_list(); const OptionViewParameters& params = searcher->view_params; - check_type->SetValue(params.type); check_category->SetValue(params.category); check_group->SetValue(params.group); + check_english->SetValue(params.english); this->SetPosition(position); this->ShowModal(); @@ -538,7 +547,7 @@ void SearchDialog::update_list() const std::vector& filters = searcher->found_options(); for (const FoundOption& item : filters) - search_list->Append(from_u8(item.label)); + search_list->Append(from_u8(item.label).Remove(0, 1)); } void SearchDialog::OnKeyDown(wxKeyEvent& event) @@ -570,7 +579,7 @@ void SearchDialog::OnKeyDown(wxKeyEvent& event) void SearchDialog::OnCheck(wxCommandEvent& event) { OptionViewParameters& params = searcher->view_params; - params.type = check_type->GetValue(); + params.english = check_english->GetValue(); params.category = check_category->GetValue(); params.group = check_group->GetValue(); diff --git a/src/slic3r/GUI/Search.hpp b/src/slic3r/GUI/Search.hpp index 5fcb58f1ea..60ef16c0fb 100644 --- a/src/slic3r/GUI/Search.hpp +++ b/src/slic3r/GUI/Search.hpp @@ -66,9 +66,9 @@ struct FoundOption { struct OptionViewParameters { - bool type {false}; bool category {false}; bool group {true }; + bool english {false}; int hovered_id {-1}; }; @@ -77,6 +77,7 @@ class OptionsSearcher { std::string search_line; std::map groups_and_categories; + PrinterTechnology printer_technology; std::vector