From 4d0b8679ebd35d8b5f51c5e01d374066a394fbc7 Mon Sep 17 00:00:00 2001 From: Filip Sykala - NTB T15p Date: Fri, 24 Feb 2023 16:03:55 +0100 Subject: [PATCH] Move functionality from emboss gizmo into icon manager --- src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp | 79 ++++-------------------- src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp | 6 +- src/slic3r/GUI/IconManager.cpp | 80 +++++++++++++++++++++---- src/slic3r/GUI/IconManager.hpp | 38 +++++++++--- 4 files changed, 111 insertions(+), 92 deletions(-) diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp index e54deb908..2992b957f 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp @@ -2798,7 +2798,7 @@ bool GLGizmoEmboss::draw_italic_button() const std::optional &wx_font_opt = m_style_manager.get_wx_font(); const auto& ff = m_style_manager.get_font_file_with_cache(); if (!wx_font_opt.has_value() || !ff.has_value()) { - draw_icon(IconType::italic, IconState::disabled); + draw(*m_icons[(int) IconType::italic][(int)IconState::disabled]); return false; } const wxFont& wx_font = *wx_font_opt; @@ -2807,8 +2807,8 @@ bool GLGizmoEmboss::draw_italic_button() bool is_font_italic = skew.has_value() || WxFontUtils::is_italic(wx_font); if (is_font_italic) { // unset italic - if (draw_clickable(IconType::italic, IconState::hovered, - IconType::unitalic, IconState::hovered)) { + if (clickable(get_icon(IconType::italic, IconState::hovered), + get_icon(IconType::unitalic, IconState::hovered))) { skew.reset(); if (wx_font.GetStyle() != wxFontStyle::wxFONTSTYLE_NORMAL) { wxFont new_wx_font = wx_font; // copy @@ -2845,7 +2845,7 @@ bool GLGizmoEmboss::draw_bold_button() { const std::optional &wx_font_opt = m_style_manager.get_wx_font(); const auto& ff = m_style_manager.get_font_file_with_cache(); if (!wx_font_opt.has_value() || !ff.has_value()) { - draw_icon(IconType::bold, IconState::disabled); + draw(get_icon(IconType::bold, IconState::disabled)); return false; } const wxFont &wx_font = *wx_font_opt; @@ -2854,8 +2854,8 @@ bool GLGizmoEmboss::draw_bold_button() { bool is_font_bold = boldness.has_value() || WxFontUtils::is_bold(wx_font); if (is_font_bold) { // unset bold - if (draw_clickable(IconType::bold, IconState::hovered, - IconType::unbold, IconState::hovered)) { + if (clickable(get_icon(IconType::bold, IconState::hovered), + get_icon(IconType::unbold, IconState::hovered))) { boldness.reset(); if (wx_font.GetWeight() != wxFontWeight::wxFONTWEIGHT_NORMAL) { wxFont new_wx_font = wx_font; // copy @@ -3800,67 +3800,14 @@ void GLGizmoEmboss::init_icons() m_icons = m_icon_manager.init(filenames, size, type); } -void GLGizmoEmboss::draw_icon(IconType icon, IconState state, ImVec2 size) +const IconManager::Icon &GLGizmoEmboss::get_icon(IconType type, IconState state) { return *m_icons[(unsigned) type][(unsigned) state]; } +bool GLGizmoEmboss::draw_button(IconType type, bool disable) { - // canot draw count - assert(icon != IconType::_count); - if (icon == IconType::_count) return; - - const auto &i = *m_icons[static_cast(icon)][static_cast(state)]; - IconManager::draw(i); -} - -void GLGizmoEmboss::draw_transparent_icon() -{ - // use top left corner of first icon - IconManager::Icon icon = *m_icons.front().front(); // copy - - if (!icon.is_valid()) { - ImGui::Text("▯"); - return; - } - - // size UV texture coors [in texture ratio] - ImVec2 size_uv( - icon.br.x-icon.tl.x, - icon.br.y-icon.tl.y); - ImVec2 one_px( - size_uv.x/icon.size.x, - size_uv.y/icon.size.y); - // reduce uv coors to one pixel - icon.tl = ImVec2(0, 0); - icon.br = one_px; - IconManager::draw(icon); -} - -bool GLGizmoEmboss::draw_clickable( - IconType icon, IconState state, - IconType hover_icon, IconState hover_state) -{ - // check of hover - float cursor_x = ImGui::GetCursorPosX(); - draw_transparent_icon(); - ImGui::SameLine(cursor_x); - - if (ImGui::IsItemHovered()) { - // redraw image - draw_icon(hover_icon, hover_state); - } else { - // redraw normal image - draw_icon(icon, state); - } - return ImGui::IsItemClicked(); -} - -bool GLGizmoEmboss::draw_button(IconType icon, bool disable) -{ - if (disable) { - draw_icon(icon, IconState::disabled); - return false; - } - return draw_clickable( - icon, IconState::activable, - icon, IconState::hovered + return Slic3r::GUI::button( + get_icon(type, IconState::activable), + get_icon(type, IconState::hovered), + get_icon(type, IconState::disabled), + disable ); } diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp index 76d870726..2bf50ce5e 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.hpp @@ -361,10 +361,8 @@ private: // automatic calc of icon's count _count }; - enum class IconState: unsigned { activable = 0, hovered /*1*/, disabled /*2*/}; - void draw_icon(IconType icon, IconState state, ImVec2 size = ImVec2(0,0)); - void draw_transparent_icon(); - bool draw_clickable(IconType icon, IconState state, IconType hover_icon, IconState hover_state); + enum class IconState : unsigned { activable = 0, hovered /*1*/, disabled /*2*/ }; + const IconManager::Icon& get_icon(IconType type, IconState state); bool draw_button(IconType icon, bool disable = false); // only temporary solution diff --git a/src/slic3r/GUI/IconManager.cpp b/src/slic3r/GUI/IconManager.cpp index 0cde271d0..78ae673ef 100644 --- a/src/slic3r/GUI/IconManager.cpp +++ b/src/slic3r/GUI/IconManager.cpp @@ -8,6 +8,7 @@ namespace priv { // set shared pointer to point on bad texture static void clear(IconManager::Icons &icons); static const std::vector>& get_states(IconManager::RasterType type); +static void draw_transparent_icon(const IconManager::Icon &icon); // only help function } IconManager::~IconManager() { @@ -94,19 +95,6 @@ void IconManager::release() { BOOST_LOG_TRIVIAL(error) << "Not implemented yet"; } -void IconManager::draw(const Icon &icon, const ImVec2 &size, const ImVec4 &tint_col, const ImVec4 &border_col) -{ - // is icon loaded - if (!icon.is_valid()) { - ImGui::Text("?"); - return; - } - - ImTextureID id = (void *) icon.tex_id; - const ImVec2 &s = (size.x < 1 || size.y < 1) ? icon.size : size; - ImGui::Image(id, s, icon.tl, icon.br, tint_col, border_col); -} - void priv::clear(IconManager::Icons &icons) { std::string message; for (auto &icon : icons) { @@ -148,3 +136,69 @@ const std::vector> &priv::get_states(IconManager::RasterTyp default: return color; } } + +void priv::draw_transparent_icon(const IconManager::Icon &icon) +{ + // Check input + if (!icon.is_valid()) { + assert(false); + BOOST_LOG_TRIVIAL(warning) << "Drawing invalid Icon."; + ImGui::Text("?"); + return; + } + + // size UV texture coors [in texture ratio] + ImVec2 size_uv(icon.br.x - icon.tl.x, icon.br.y - icon.tl.y); + ImVec2 one_px(size_uv.x / icon.size.x, size_uv.y / icon.size.y); + + // use top left corner of first icon + IconManager::Icon icon_px = icon; // copy + // reduce uv coors to one pixel + icon_px.tl = ImVec2(0, 0); + icon_px.br = one_px; + draw(icon_px); +} + +namespace Slic3r::GUI { + +void draw(const IconManager::Icon &icon, const ImVec2 &size, const ImVec4 &tint_col, const ImVec4 &border_col) +{ + // Check input + if (!icon.is_valid()) { + assert(false); + BOOST_LOG_TRIVIAL(warning) << "Drawing invalid Icon."; + ImGui::Text("?"); + return; + } + + ImTextureID id = (void *) icon.tex_id; + const ImVec2 &s = (size.x < 1 || size.y < 1) ? icon.size : size; + ImGui::Image(id, s, icon.tl, icon.br, tint_col, border_col); +} + +bool clickable(const IconManager::Icon &icon, const IconManager::Icon &icon_hover) +{ + // check of hover + float cursor_x = ImGui::GetCursorPosX(); + priv::draw_transparent_icon(icon); + ImGui::SameLine(cursor_x); + if (ImGui::IsItemHovered()) { + // redraw image + draw(icon_hover); + } else { + // redraw normal image + draw(icon); + } + return ImGui::IsItemClicked(); +} + +bool button(const IconManager::Icon &activ, const IconManager::Icon &hover, const IconManager::Icon &disable, bool disabled) +{ + if (disabled) { + draw(disable); + return false; + } + return clickable(activ, hover); +} + +} // namespace Slic3r::GUI diff --git a/src/slic3r/GUI/IconManager.hpp b/src/slic3r/GUI/IconManager.hpp index 91177fe8a..78b1395dc 100644 --- a/src/slic3r/GUI/IconManager.hpp +++ b/src/slic3r/GUI/IconManager.hpp @@ -91,20 +91,40 @@ public: /// void release(); - /// - /// Draw imgui image with icon - /// - /// Place in texture - /// [optional]Size of image, wen zero than use same size as stored texture - /// viz ImGui::Image - /// viz ImGui::Image - static void draw(const Icon &icon, const ImVec2 &size = ImVec2(0, 0), const ImVec4& tint_col = ImVec4(1,1,1,1), const ImVec4& border_col = ImVec4(0,0,0,0)); - private: // keep data stored on GPU GLTexture m_icons_texture; Icons m_icons; }; +/// +/// Draw imgui image with icon +/// +/// Place in texture +/// [optional]Size of image, wen zero than use same size as stored texture +/// viz ImGui::Image +/// viz ImGui::Image +void draw(const IconManager::Icon &icon, + const ImVec2 &size = ImVec2(0, 0), + const ImVec4 &tint_col = ImVec4(1, 1, 1, 1), + const ImVec4 &border_col = ImVec4(0, 0, 0, 0)); + +/// +/// Draw icon which change on hover +/// +/// Draw when no hover +/// Draw when hover +/// True when click, otherwise False +bool clickable(const IconManager::Icon &icon, const IconManager::Icon &icon_hover); + +/// +/// Use icon as button with 3 states activ hover and disabled +/// +/// Not disabled not hovered image +/// Hovered image +/// Disabled image +/// True when click on enabled, otherwise False +bool button(const IconManager::Icon &activ, const IconManager::Icon &hover, const IconManager::Icon &disable, bool disabled = false); + } // namespace Slic3r::GUI #endif // slic3r_IconManager_hpp_ \ No newline at end of file