From f4726f738c38271aceebc5c99e4833485dff354a Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Mon, 7 Mar 2022 15:06:45 +0100 Subject: [PATCH] Follow-up of 005fef7bf68e2e589e8ecb3de80a43aa37043ce3 - Fixed highlight arrows for gizmos bar and toolbars --- resources/icons/toolbar_arrow_2.svg | 22 ++++++ src/slic3r/GUI/GLCanvas3D.cpp | 40 ++++++----- src/slic3r/GUI/GLToolbar.cpp | 88 ++++++++++++++++++++++- src/slic3r/GUI/GLToolbar.hpp | 8 +++ src/slic3r/GUI/Gizmos/GLGizmosManager.cpp | 74 ++++++++++++++++--- src/slic3r/GUI/Gizmos/GLGizmosManager.hpp | 16 +++-- 6 files changed, 215 insertions(+), 33 deletions(-) create mode 100644 resources/icons/toolbar_arrow_2.svg diff --git a/resources/icons/toolbar_arrow_2.svg b/resources/icons/toolbar_arrow_2.svg new file mode 100644 index 000000000..5ff1cfa2b --- /dev/null +++ b/resources/icons/toolbar_arrow_2.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 4212e9c83..b3bcf379d 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -4759,21 +4759,28 @@ bool GLCanvas3D::_init_main_toolbar() return true; } // init arrow +#if !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES BackgroundTexture::Metadata arrow_data; arrow_data.filename = "toolbar_arrow.svg"; arrow_data.left = 0; arrow_data.top = 0; arrow_data.right = 0; arrow_data.bottom = 0; +#endif // !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + if (!m_main_toolbar.init_arrow("toolbar_arrow_2.svg")) +#else if (!m_main_toolbar.init_arrow(arrow_data)) - { +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES BOOST_LOG_TRIVIAL(error) << "Main toolbar failed to load arrow texture."; - } + // m_gizmos is created at constructor, thus we can init arrow here. +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + if (!m_gizmos.init_arrow("toolbar_arrow_2.svg")) +#else if (!m_gizmos.init_arrow(arrow_data)) - { +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES BOOST_LOG_TRIVIAL(error) << "Gizmos manager failed to load arrow texture."; - } // m_main_toolbar.set_layout_type(GLToolbar::Layout::Vertical); m_main_toolbar.set_layout_type(GLToolbar::Layout::Horizontal); @@ -4973,24 +4980,27 @@ bool GLCanvas3D::_init_undoredo_toolbar() background_data.right = 16; background_data.bottom = 16; - if (!m_undoredo_toolbar.init(background_data)) - { + if (!m_undoredo_toolbar.init(background_data)) { // unable to init the toolbar texture, disable it m_undoredo_toolbar.set_enabled(false); return true; } // init arrow +#if !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES BackgroundTexture::Metadata arrow_data; arrow_data.filename = "toolbar_arrow.svg"; arrow_data.left = 0; arrow_data.top = 0; arrow_data.right = 0; arrow_data.bottom = 0; +#endif // !ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + if (!m_undoredo_toolbar.init_arrow("toolbar_arrow_2.svg")) +#else if (!m_undoredo_toolbar.init_arrow(arrow_data)) - { +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES BOOST_LOG_TRIVIAL(error) << "Undo/Redo toolbar failed to load arrow texture."; - } // m_undoredo_toolbar.set_layout_type(GLToolbar::Layout::Vertical); m_undoredo_toolbar.set_layout_type(GLToolbar::Layout::Horizontal); @@ -5010,8 +5020,7 @@ bool GLCanvas3D::_init_undoredo_toolbar() item.right.toggable = true; item.right.action_callback = [this]() { m_imgui_undo_redo_hovered_pos = -1; }; item.right.render_callback = [this](float left, float right, float, float) { - if (m_canvas != nullptr) - { + if (m_canvas != nullptr) { if (_render_undo_redo_stack(true, 0.5f * (left + right))) _deactivate_undo_redo_toolbar_items(); } @@ -5030,8 +5039,7 @@ bool GLCanvas3D::_init_undoredo_toolbar() new_additional_tooltip = (boost::format(_utf8(L("Next Undo action: %1%"))) % action).str(); } - if (new_additional_tooltip != curr_additional_tooltip) - { + if (new_additional_tooltip != curr_additional_tooltip) { m_undoredo_toolbar.set_additional_tooltip(id, new_additional_tooltip); set_tooltip(""); } @@ -5048,8 +5056,7 @@ bool GLCanvas3D::_init_undoredo_toolbar() item.left.action_callback = [this]() { post_event(SimpleEvent(EVT_GLCANVAS_REDO)); }; item.right.action_callback = [this]() { m_imgui_undo_redo_hovered_pos = -1; }; item.right.render_callback = [this](float left, float right, float, float) { - if (m_canvas != nullptr) - { + if (m_canvas != nullptr) { if (_render_undo_redo_stack(false, 0.5f * (left + right))) _deactivate_undo_redo_toolbar_items(); } @@ -5068,8 +5075,7 @@ bool GLCanvas3D::_init_undoredo_toolbar() new_additional_tooltip = (boost::format(_utf8(L("Next Redo action: %1%"))) % action).str(); } - if (new_additional_tooltip != curr_additional_tooltip) - { + if (new_additional_tooltip != curr_additional_tooltip) { m_undoredo_toolbar.set_additional_tooltip(id, new_additional_tooltip); set_tooltip(""); } @@ -5811,9 +5817,7 @@ void GLCanvas3D::_render_gizmos_overlay() m_gizmos.render_overlay(); if (m_gizmo_highlighter.m_render_arrow) - { m_gizmos.render_arrow(*this, m_gizmo_highlighter.m_gizmo_type); - } } void GLCanvas3D::_render_main_toolbar() diff --git a/src/slic3r/GUI/GLToolbar.cpp b/src/slic3r/GUI/GLToolbar.cpp index 64911d698..d35c0e060 100644 --- a/src/slic3r/GUI/GLToolbar.cpp +++ b/src/slic3r/GUI/GLToolbar.cpp @@ -185,6 +185,16 @@ bool GLToolbar::init(const BackgroundTexture::Metadata& background_texture) return res; } +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES +bool GLToolbar::init_arrow(const std::string& filename) +{ + if (m_arrow_texture.get_id() != 0) + return true; + + const std::string path = resources_dir() + "/icons/"; + return (!filename.empty()) ? m_arrow_texture.load_from_svg_file(path + filename, false, false, false, 512) : false; +} +#else bool GLToolbar::init_arrow(const BackgroundTexture::Metadata& arrow_texture) { if (m_arrow_texture.texture.get_id() != 0) @@ -201,6 +211,7 @@ bool GLToolbar::init_arrow(const BackgroundTexture::Metadata& arrow_texture) return res; } +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES GLToolbar::Layout::EType GLToolbar::get_layout_type() const { @@ -1408,9 +1419,9 @@ void GLToolbar::render_background(float left, float top, float right, float bott const unsigned int tex_id = m_background_texture.texture.get_id(); const float tex_width = (float)m_background_texture.texture.get_width(); const float tex_height = (float)m_background_texture.texture.get_height(); - if (tex_id != 0 && tex_width > 0 && tex_height > 0) { - const float inv_tex_width = (tex_width != 0.0f) ? 1.0f / tex_width : 0.0f; - const float inv_tex_height = (tex_height != 0.0f) ? 1.0f / tex_height : 0.0f; + if (tex_id != 0 && tex_width > 0.0f && tex_height > 0.0f) { + const float inv_tex_width = 1.0f / tex_width; + const float inv_tex_height = 1.0f / tex_height; const float internal_left = left + border_w; const float internal_right = right - border_w; @@ -1558,6 +1569,76 @@ void GLToolbar::render_background(float left, float top, float right, float bott } #endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES +void GLToolbar::render_arrow(const GLCanvas3D& parent, GLToolbarItem* highlighted_item) +{ + // arrow texture not initialized + if (m_arrow_texture.get_id() == 0) + return; + + const Size cnv_size = parent.get_canvas_size(); + const float cnv_w = (float)cnv_size.get_width(); + const float cnv_h = (float)cnv_size.get_height(); + + if (cnv_w == 0 || cnv_h == 0) + return; + + const float inv_cnv_w = 1.0f / cnv_w; + const float inv_cnv_h = 1.0f / cnv_h; + + const float icons_size_x = 2.0f * m_layout.icons_size * m_layout.scale * inv_cnv_w; + const float icons_size_y = 2.0f * m_layout.icons_size * m_layout.scale * inv_cnv_h; + const float separator_size = 2.0f * m_layout.separator_size * m_layout.scale * inv_cnv_w; + const float gap_size = 2.0f * m_layout.gap_size * m_layout.scale * inv_cnv_w; + const float border_x = 2.0f * m_layout.border * m_layout.scale * inv_cnv_w; + const float border_y = 2.0f * m_layout.border * m_layout.scale * inv_cnv_h; + + const float separator_stride = separator_size + gap_size; + const float icon_stride = icons_size_x + gap_size; + + float left = 2.0f * m_layout.left * inv_cnv_w + border_x; + float top = 2.0f * m_layout.top * inv_cnv_h - 2.0f * border_y - icons_size_y; + + bool found = false; + for (const GLToolbarItem* item : m_items) { + if (!item->is_visible()) + continue; + + if (item->is_separator()) + left += separator_stride; + else { + if (item->get_name() == highlighted_item->get_name()) { + found = true; + break; + } + left += icon_stride; + } + } + if (!found) + return; + + const float right = left + icons_size_x; + + const unsigned int tex_id = m_arrow_texture.get_id(); + // arrow width and height + const float arr_tex_width = (float)m_arrow_texture.get_width(); + const float arr_tex_height = (float)m_arrow_texture.get_height(); + if (tex_id != 0 && arr_tex_width > 0.0f && arr_tex_height > 0.0f) { + const float arrow_size_x = 2.0f * m_layout.scale * arr_tex_width * inv_cnv_w; + const float arrow_size_y = 2.0f * m_layout.scale * arr_tex_height * inv_cnv_h; + + const float left_uv = 0.0f; + const float right_uv = 1.0f; + const float top_uv = 1.0f; + const float bottom_uv = 0.0f; + + top -= border_y; + const float bottom = top - arrow_size_y * icons_size_x / arrow_size_x; + + GLTexture::render_sub_texture(tex_id, left, right, bottom, top, { { left_uv, top_uv }, { right_uv, top_uv }, { right_uv, bottom_uv }, { left_uv, bottom_uv } }); + } +} +#else void GLToolbar::render_arrow(const GLCanvas3D& parent, GLToolbarItem* highlighted_item) { // arrow texture not initialized @@ -1623,6 +1704,7 @@ void GLToolbar::render_arrow(const GLCanvas3D& parent, GLToolbarItem* highlighte GLTexture::render_sub_texture(tex_id, internal_left, internal_right, internal_bottom, internal_top, { { internal_left_uv, internal_top_uv }, { internal_right_uv, internal_top_uv }, { internal_right_uv, internal_bottom_uv }, { internal_left_uv, internal_bottom_uv } }); } } +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES #if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES void GLToolbar::render_horizontal(const GLCanvas3D& parent) diff --git a/src/slic3r/GUI/GLToolbar.hpp b/src/slic3r/GUI/GLToolbar.hpp index 862b473f6..aa0a5b1d9 100644 --- a/src/slic3r/GUI/GLToolbar.hpp +++ b/src/slic3r/GUI/GLToolbar.hpp @@ -246,7 +246,11 @@ private: GLTexture m_icons_texture; bool m_icons_texture_dirty; BackgroundTexture m_background_texture; +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + GLTexture m_arrow_texture; +#else BackgroundTexture m_arrow_texture; +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES Layout m_layout; ItemsList m_items; @@ -273,7 +277,11 @@ public: bool init(const BackgroundTexture::Metadata& background_texture); +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + bool init_arrow(const std::string& filename); +#else bool init_arrow(const BackgroundTexture::Metadata& arrow_texture); +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES Layout::EType get_layout_type() const; void set_layout_type(Layout::EType type); diff --git a/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp b/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp index 499a2fe49..6991c5356 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmosManager.cpp @@ -124,7 +124,17 @@ bool GLGizmosManager::init() return true; } -bool GLGizmosManager::init_arrow(const BackgroundTexture::Metadata& arrow_texture) +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES +bool GLGizmosManager::init_arrow(const std::string& filename) +{ + if (m_arrow_texture.get_id() != 0) + return true; + + const std::string path = resources_dir() + "/icons/"; + return (!filename.empty()) ? m_arrow_texture.load_from_svg_file(path + filename, false, false, false, 512) : false; +} +#else +bool GLGizmosManager::init_arrow(const BackgroundTexture::Metadata & arrow_texture) { if (m_arrow_texture.texture.get_id() != 0) return true; @@ -139,6 +149,7 @@ bool GLGizmosManager::init_arrow(const BackgroundTexture::Metadata& arrow_textur return res; } +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES void GLGizmosManager::set_overlay_icon_size(float size) { @@ -329,7 +340,7 @@ void GLGizmosManager::render_current_gizmo_for_picking_pass() const m_gizmos[m_current]->render_for_picking(); } -void GLGizmosManager::render_overlay() const +void GLGizmosManager::render_overlay() { if (!m_enabled) return; @@ -773,6 +784,54 @@ void GLGizmosManager::render_background(float left, float top, float right, floa } #endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES +void GLGizmosManager::render_arrow(const GLCanvas3D& parent, EType highlighted_type) const +{ + const std::vector selectable_idxs = get_selectable_idxs(); + if (selectable_idxs.empty()) + return; + + const Size cnv_size = m_parent.get_canvas_size(); + const float cnv_w = (float)cnv_size.get_width(); + const float cnv_h = (float)cnv_size.get_height(); + + if (cnv_w == 0 || cnv_h == 0) + return; + + const float inv_cnv_w = 1.0f / cnv_w; + const float inv_cnv_h = 1.0f / cnv_h; + + const float top_x = -1.0f; + float top_y = 0.5f * 2.0f * get_scaled_total_height() * inv_cnv_h; + + const float icons_size_x = 2.0f * m_layout.scaled_icons_size() * inv_cnv_w; + const float icons_size_y = 2.0f * m_layout.scaled_icons_size() * inv_cnv_h; + const float stride_y = 2.0f * m_layout.scaled_stride_y() * inv_cnv_h; + + for (size_t idx : selectable_idxs) { + if (idx == highlighted_type) { + const int tex_width = m_arrow_texture.get_width(); + const int tex_height = m_arrow_texture.get_height(); + const unsigned int tex_id = m_arrow_texture.get_id(); + + const float arrow_size_x = 2.0f * m_layout.scale * float(tex_height) * inv_cnv_w; + const float arrow_size_y = 2.0f * m_layout.scale * float(tex_width) * inv_cnv_h; + + const float left_uv = 0.0f; + const float right_uv = 1.0f; + const float top_uv = 1.0f; + const float bottom_uv = 0.0f; + + const float left = top_x + icons_size_x + 6.0f * m_layout.scaled_border() * inv_cnv_w; + const float right = left + arrow_size_x * icons_size_y / arrow_size_y; + + GLTexture::render_sub_texture(tex_id, left, right, top_y, top_y + icons_size_y, { { left_uv, bottom_uv }, { left_uv, top_uv }, { right_uv, top_uv }, { right_uv, bottom_uv } }); + break; + } + top_y -= stride_y; + } +} +#else void GLGizmosManager::render_arrow(const GLCanvas3D& parent, EType highlighted_type) const { std::vector selectable_idxs = get_selectable_idxs(); @@ -811,11 +870,12 @@ void GLGizmosManager::render_arrow(const GLCanvas3D& parent, EType highlighted_t zoomed_top_y -= zoomed_stride_y; } } +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES #if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES void GLGizmosManager::do_render_overlay() const { - std::vector selectable_idxs = get_selectable_idxs(); + const std::vector selectable_idxs = get_selectable_idxs(); if (selectable_idxs.empty()) return; @@ -989,14 +1049,12 @@ GLGizmosManager::EType GLGizmosManager::get_gizmo_from_name(const std::string& g return GLGizmosManager::EType::Undefined; } -bool GLGizmosManager::generate_icons_texture() const +bool GLGizmosManager::generate_icons_texture() { std::string path = resources_dir() + "/icons/"; std::vector filenames; - for (size_t idx=0; idxget_icon_filename(); if (!icon_filename.empty()) filenames.push_back(path + icon_filename); diff --git a/src/slic3r/GUI/Gizmos/GLGizmosManager.hpp b/src/slic3r/GUI/Gizmos/GLGizmosManager.hpp index 3bdcd0f65..28cd8780a 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmosManager.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmosManager.hpp @@ -102,10 +102,14 @@ private: GLCanvas3D& m_parent; bool m_enabled; std::vector> m_gizmos; - mutable GLTexture m_icons_texture; - mutable bool m_icons_texture_dirty; + GLTexture m_icons_texture; + bool m_icons_texture_dirty; BackgroundTexture m_background_texture; +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + GLTexture m_arrow_texture; +#else BackgroundTexture m_arrow_texture; +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES Layout m_layout; EType m_current; EType m_hover; @@ -133,7 +137,11 @@ public: bool init(); +#if ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES + bool init_arrow(const std::string& filename); +#else bool init_arrow(const BackgroundTexture::Metadata& arrow_texture); +#endif // ENABLE_GLBEGIN_GLEND_SHADERS_ATTRIBUTES template void load(Archive& ar) @@ -208,7 +216,7 @@ public: void render_current_gizmo_for_picking_pass() const; void render_painter_gizmo(); - void render_overlay() const; + void render_overlay(); void render_arrow(const GLCanvas3D& parent, EType highlighted_type) const; @@ -245,7 +253,7 @@ private: float get_scaled_total_height() const; float get_scaled_total_width() const; - bool generate_icons_texture() const; + bool generate_icons_texture(); void update_hover_state(const EType &type); bool grabber_contains_mouse() const;