diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp index 7a853e7187..766cb04ef1 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp @@ -329,6 +329,15 @@ bool GLGizmoEmboss::on_mouse_for_rotation(const wxMouseEvent &mouse_event) namespace priv { +/// +/// Access to model from gl_volume +/// TODO: it is more general function --> move to utils +/// +/// Volume to model belongs to +/// Object containing gl_volume +/// Model for volume +static ModelVolume *get_model_volume(const GLVolume *gl_volume, const ModelObject *object); + /// /// Access to model from gl_volume /// TODO: it is more general function --> move to utils @@ -343,9 +352,8 @@ static ModelVolume *get_model_volume(const GLVolume *gl_volume, const ModelObjec /// TODO: it is more general function --> move to select utils /// /// Actual selection -/// All objects /// Model from selection -static ModelVolume *get_selected_volume(const Selection &selection, const ModelObjectPtrs &objects); +static ModelVolume *get_selected_volume(const Selection &selection); /// /// Calculate offset from mouse position to center of text @@ -671,7 +679,7 @@ static void draw_mouse_offset(const std::optional &offset) void GLGizmoEmboss::on_render_input_window(float x, float y, float bottom_limit) { if (!m_gui_cfg.has_value()) initialize(); - check_selection(); + set_volume_by_selection(); // Do not render window for not selected text volume if (m_volume == nullptr || !m_volume->text_configuration.has_value()) { @@ -741,8 +749,8 @@ void GLGizmoEmboss::on_set_state() // to reload fonts from system, when install new one wxFontEnumerator::InvalidateCache(); - // Try(when exist) set text configuration by volume - load_configuration(get_selected_volume()); + // Try(when exist) set text configuration by volume + set_volume(priv::get_selected_volume(m_parent.get_selection())); // change position of just opened emboss window set_fine_position(); @@ -900,9 +908,9 @@ EmbossStyles GLGizmoEmboss::create_default_styles() void GLGizmoEmboss::set_default_text(){ m_text = _u8L("Embossed text"); } #include "imgui/imgui_internal.h" // to unfocus input --> ClearActiveID -void GLGizmoEmboss::check_selection() +void GLGizmoEmboss::set_volume_by_selection() { - ModelVolume *vol = get_selected_volume(); + ModelVolume *vol = priv::get_selected_volume(m_parent.get_selection()); // is same volume selected? if (vol != nullptr && m_volume == vol) return; @@ -913,27 +921,88 @@ void GLGizmoEmboss::check_selection() if (m_volume != nullptr) ImGui::ClearActiveID(); // is select embossed volume? - if (load_configuration(vol)) - // successfull load volume for editing - return; - - // behave like adding new text - m_volume = nullptr; - set_default_text(); + if (!set_volume(vol)) { + // Can't load so behave like adding new text + m_volume = nullptr; + set_default_text(); + } +} + +bool GLGizmoEmboss::set_volume(ModelVolume *volume) +{ + if (volume == nullptr) return false; + const std::optional tc_opt = volume->text_configuration; + if (!tc_opt.has_value()) return false; + const TextConfiguration &tc = *tc_opt; + const EmbossStyle &style = tc.style; + + auto has_same_name = [&style](const StyleManager::Item &style_item) -> bool { + const EmbossStyle &es = style_item.style; + return es.name == style.name; + }; + + wxFont wx_font; + bool is_path_changed = false; + if (style.type == WxFontUtils::get_actual_type()) + wx_font = WxFontUtils::load_wxFont(style.path); + if (!wx_font.IsOk()) { + create_notification_not_valid_font(tc); + // Try create similar wx font + wx_font = WxFontUtils::create_wxFont(style); + is_path_changed = wx_font.IsOk(); + } + + const auto& styles = m_style_manager.get_styles(); + auto it = std::find_if(styles.begin(), styles.end(), has_same_name); + if (it == styles.end()) { + // style was not found + if (wx_font.IsOk()) + m_style_manager.load_style(style, wx_font); + } else { + size_t style_index = it - styles.begin(); + if (!m_style_manager.load_style(style_index)) { + // can`t load stored style + m_style_manager.erase(style_index); + if (wx_font.IsOk()) + m_style_manager.load_style(style, wx_font); + + } else { + // stored style is loaded, now set modification of style + m_style_manager.get_style() = style; + m_style_manager.set_wx_font(wx_font); + } + } + + if (is_path_changed) { + std::string path = WxFontUtils::store_wxFont(wx_font); + m_style_manager.get_style().path = path; + } + + m_text = tc.text; + m_volume = volume; + + // store volume state before edit + m_unmodified_volume = {*volume->get_mesh_shared_ptr(), // copy + tc, volume->get_matrix(), volume->name}; + + return true; +} + +ModelVolume *priv::get_model_volume(const GLVolume *gl_volume, const ModelObject *object) +{ + int volume_id = gl_volume->volume_idx(); + if (volume_id < 0 || static_cast(volume_id) >= object->volumes.size()) return nullptr; + return object->volumes[volume_id]; } ModelVolume *priv::get_model_volume(const GLVolume *gl_volume, const ModelObjectPtrs &objects) { - const GLVolume::CompositeID &id = gl_volume->composite_id; - - if (id.object_id < 0 || static_cast(id.object_id) >= objects.size()) return nullptr; - ModelObject *object = objects[id.object_id]; - - if (id.volume_id < 0 || static_cast(id.volume_id) >= object->volumes.size()) return nullptr; - return object->volumes[id.volume_id]; + int object_id = gl_volume->object_idx(); + if (object_id < 0 || static_cast(object_id) >= objects.size()) return nullptr; + return get_model_volume(gl_volume, objects[object_id]); } -ModelVolume *priv::get_selected_volume(const Selection &selection, const ModelObjectPtrs &objects) +ModelVolume *priv::get_selected_volume(const Selection &selection) { int object_idx = selection.get_object_idx(); // is more object selected? @@ -944,15 +1013,10 @@ ModelVolume *priv::get_selected_volume(const Selection &selection, const ModelOb if (volume_idxs.size() != 1) return nullptr; unsigned int vol_id_gl = *volume_idxs.begin(); const GLVolume *vol_gl = selection.get_volume(vol_id_gl); + const ModelObjectPtrs &objects = selection.get_model()->objects; return get_model_volume(vol_gl, objects); } -ModelVolume *GLGizmoEmboss::get_selected_volume() -{ - return priv::get_selected_volume(m_parent.get_selection(), - wxGetApp().plater()->model().objects); -} - // Run Job on main thread (blocking) - ONLY DEBUG static inline void execute_job(std::shared_ptr j) { @@ -2985,66 +3049,6 @@ bool GLGizmoEmboss::choose_svg_file() //return add_volume(name, its); } -bool GLGizmoEmboss::load_configuration(ModelVolume *volume) -{ - if (volume == nullptr) return false; - const std::optional tc_opt = volume->text_configuration; - if (!tc_opt.has_value()) return false; - const TextConfiguration &tc = *tc_opt; - const EmbossStyle &style = tc.style; - - auto has_same_name = [&style](const StyleManager::Item &style_item) -> bool { - const EmbossStyle &es = style_item.style; - return es.name == style.name; - }; - - wxFont wx_font; - bool is_path_changed = false; - if (style.type == WxFontUtils::get_actual_type()) - wx_font = WxFontUtils::load_wxFont(style.path); - if (!wx_font.IsOk()) { - create_notification_not_valid_font(tc); - // Try create similar wx font - wx_font = WxFontUtils::create_wxFont(style); - is_path_changed = wx_font.IsOk(); - } - - const auto& styles = m_style_manager.get_styles(); - auto it = std::find_if(styles.begin(), styles.end(), has_same_name); - if (it == styles.end()) { - // style was not found - if (wx_font.IsOk()) - m_style_manager.load_style(style, wx_font); - } else { - size_t style_index = it - styles.begin(); - if (!m_style_manager.load_style(style_index)) { - // can`t load stored style - m_style_manager.erase(style_index); - if (wx_font.IsOk()) - m_style_manager.load_style(style, wx_font); - - } else { - // stored style is loaded, now set modification of style - m_style_manager.get_style() = style; - m_style_manager.set_wx_font(wx_font); - } - } - - if (is_path_changed) { - std::string path = WxFontUtils::store_wxFont(wx_font); - m_style_manager.get_style().path = path; - } - - m_text = tc.text; - m_volume = volume; - - // store volume state before edit - m_unmodified_volume = {*volume->get_mesh_shared_ptr(), // copy - tc, volume->get_matrix(), volume->name}; - - return true; -} - void GLGizmoEmboss::create_notification_not_valid_font( const TextConfiguration &tc) { diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp index 092041e96e..1df32150f4 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp @@ -91,8 +91,10 @@ private: // localized default text void set_default_text(); - void check_selection(); - ModelVolume *get_selected_volume(); + void set_volume_by_selection(); + // load text configuration from volume into gizmo + bool set_volume(ModelVolume *volume); + // create volume from text - main functionality bool process(); void close(); @@ -158,8 +160,6 @@ private: bool choose_true_type_file(); bool choose_svg_file(); - bool load_configuration(ModelVolume *volume); - // When open text loaded from .3mf it could be written with unknown font bool m_is_unknown_font; void create_notification_not_valid_font(const TextConfiguration& tc);