From c6d5ad1517ffea6aa30cdb86a185368bd406bd6a Mon Sep 17 00:00:00 2001 From: enricoturri1966 <enricoturri@seznam.cz> Date: Tue, 24 Mar 2020 12:26:30 +0100 Subject: [PATCH] Refactoring of toolbars' tooltip generation --- src/slic3r/GUI/GLCanvas3D.cpp | 8 +- src/slic3r/GUI/GLCanvas3D.hpp | 1 + src/slic3r/GUI/GLToolbar.cpp | 168 ++++++++++++++++++++++++++++++---- src/slic3r/GUI/GLToolbar.hpp | 21 +++++ src/slic3r/GUI/GUI_App.cpp | 6 ++ 5 files changed, 185 insertions(+), 19 deletions(-) diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 586f58e7e..360097ea7 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -3341,6 +3341,11 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) Point pos(evt.GetX(), evt.GetY()); ImGuiWrapper* imgui = wxGetApp().imgui(); +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI + if (m_tooltip.is_in_imgui() && evt.LeftUp()) + // ignore left up events coming from imgui windows and not processed by them + m_mouse.ignore_left_up = true; +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI m_tooltip.set_in_imgui(false); if (imgui->update_mouse_data(evt)) { m_mouse.position = evt.Leaving() ? Vec2d(-1.0, -1.0) : pos.cast<double>(); @@ -3352,10 +3357,11 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) // do not return if dragging or tooltip not empty to allow for tooltip update #if ENABLE_CANVAS_TOOLTIP_USING_IMGUI if (!m_mouse.dragging && m_tooltip.is_empty()) + return; #else if (!m_mouse.dragging && m_canvas->GetToolTipText().empty()) -#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI return; +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI } #ifdef __WXMSW__ diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index e3d29960b..fbdb8d3a4 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -413,6 +413,7 @@ private: #endif // ENABLE_CANVAS_DELAYED_TOOLTIP_USING_IMGUI // Indicates that the mouse is inside an ImGUI dialog, therefore the tooltip should be suppressed. void set_in_imgui(bool b) { m_in_imgui = b; } + bool is_in_imgui() const { return m_in_imgui; } }; #endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI diff --git a/src/slic3r/GUI/GLToolbar.cpp b/src/slic3r/GUI/GLToolbar.cpp index c7b391cd6..8b84d3875 100644 --- a/src/slic3r/GUI/GLToolbar.cpp +++ b/src/slic3r/GUI/GLToolbar.cpp @@ -86,7 +86,7 @@ bool GLToolbarItem::update_enabled_state() void GLToolbarItem::render(unsigned int tex_id, float left, float right, float bottom, float top, unsigned int tex_width, unsigned int tex_height, unsigned int icon_size) const { - auto uvs = [this](unsigned int tex_width, unsigned int tex_height, unsigned int icon_size) ->GLTexture::Quad_UVs + auto uvs = [this](unsigned int tex_width, unsigned int tex_height, unsigned int icon_size) -> GLTexture::Quad_UVs { assert((tex_width != 0) && (tex_height != 0)); GLTexture::Quad_UVs ret; @@ -154,7 +154,9 @@ GLToolbar::GLToolbar(GLToolbar::EType type, const std::string& name) , m_name(name) , m_enabled(false) , m_icons_texture_dirty(true) +#if !ENABLE_CANVAS_TOOLTIP_USING_IMGUI , m_tooltip("") +#endif // !ENABLE_CANVAS_TOOLTIP_USING_IMGUI , m_pressed_toggable_id(-1) { } @@ -358,16 +360,37 @@ int GLToolbar::get_item_id(const std::string& name) const return -1; } +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI +std::string GLToolbar::get_tooltip() const +{ + std::string tooltip; + + for (GLToolbarItem* item : m_items) + { + if (item->is_hovered()) + { + tooltip = item->get_tooltip(); + if (!item->is_pressed()) + { + const std::string& additional_tooltip = item->get_additional_tooltip(); + if (!additional_tooltip.empty()) + tooltip += "\n" + additional_tooltip; + + break; + } + } + } + + return tooltip; +} +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI + void GLToolbar::get_additional_tooltip(int item_id, std::string& text) { - if ((0 <= item_id) && (item_id < (int)m_items.size())) + if (0 <= item_id && item_id < (int)m_items.size()) { - GLToolbarItem* item = m_items[item_id]; - if (item != nullptr) - { - text = item->get_additional_tooltip(); - return; - } + text = m_items[item_id]->get_additional_tooltip(); + return; } text.clear(); @@ -375,12 +398,8 @@ void GLToolbar::get_additional_tooltip(int item_id, std::string& text) void GLToolbar::set_additional_tooltip(int item_id, const std::string& text) { - if ((0 <= item_id) && (item_id < (int)m_items.size())) - { - GLToolbarItem* item = m_items[item_id]; - if (item != nullptr) - item->set_additional_tooltip(text); - } + if (0 <= item_id && item_id < (int)m_items.size()) + m_items[item_id]->set_additional_tooltip(text); } bool GLToolbar::update_items_state() @@ -425,9 +444,11 @@ bool GLToolbar::on_mouse(wxMouseEvent& evt, GLCanvas3D& parent) // prevents loosing selection into the scene if mouse down was done inside the toolbar and mouse up was down outside it, // as when switching between views m_mouse_capture.reset(); +#if !ENABLE_CANVAS_TOOLTIP_USING_IMGUI if (contains_mouse(mouse_pos, parent) == -1) // mouse is outside the toolbar m_tooltip.clear(); +#endif // !ENABLE_CANVAS_TOOLTIP_USING_IMGUI return true; } m_mouse_capture.reset(); @@ -435,7 +456,7 @@ bool GLToolbar::on_mouse(wxMouseEvent& evt, GLCanvas3D& parent) #if ENABLE_MODIFIED_TOOLBAR_MOUSE_EVENT_HANDLING if (evt.Moving()) - m_tooltip = update_hover_state(mouse_pos, parent); + update_hover_state(mouse_pos, parent); else if (evt.LeftUp()) { if (m_mouse_capture.left) @@ -489,12 +510,16 @@ bool GLToolbar::on_mouse(wxMouseEvent& evt, GLCanvas3D& parent) #endif // ENABLE_MODIFIED_TOOLBAR_MOUSE_EVENT_HANDLING int item_id = contains_mouse(mouse_pos, parent); +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI + if (item_id != -1) +#else if (item_id == -1) { // mouse is outside the toolbar m_tooltip.clear(); } else +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI { // mouse inside toolbar if (evt.LeftDown() || evt.LeftDClick()) @@ -658,6 +683,20 @@ void GLToolbar::do_action(GLToolbarItem::EActionType type, int item_id, GLCanvas } } +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI +void GLToolbar::update_hover_state(const Vec2d& mouse_pos, GLCanvas3D& parent) +{ + if (!m_enabled) + return; + + switch (m_layout.type) + { + default: + case Layout::Horizontal: { update_hover_state_horizontal(mouse_pos, parent); break; } + case Layout::Vertical: { update_hover_state_vertical(mouse_pos, parent); break; } + } +} +#else std::string GLToolbar::update_hover_state(const Vec2d& mouse_pos, GLCanvas3D& parent) { if (!m_enabled) @@ -670,8 +709,13 @@ std::string GLToolbar::update_hover_state(const Vec2d& mouse_pos, GLCanvas3D& pa case Layout::Vertical: { return update_hover_state_vertical(mouse_pos, parent); } } } +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI +void GLToolbar::update_hover_state_horizontal(const Vec2d& mouse_pos, GLCanvas3D& parent) +#else std::string GLToolbar::update_hover_state_horizontal(const Vec2d& mouse_pos, GLCanvas3D& parent) +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI { // NB: mouse_pos is already scaled appropriately @@ -692,8 +736,10 @@ std::string GLToolbar::update_hover_state_horizontal(const Vec2d& mouse_pos, GLC float left = m_layout.left + scaled_border; float top = m_layout.top - scaled_border; - std::string tooltip = ""; - +#if !ENABLE_CANVAS_TOOLTIP_USING_IMGUI + std::string tooltip; +#endif // !ENABLE_CANVAS_TOOLTIP_USING_IMGUI + for (GLToolbarItem* item : m_items) { if (!item->is_visible()) @@ -708,6 +754,7 @@ std::string GLToolbar::update_hover_state_horizontal(const Vec2d& mouse_pos, GLC GLToolbarItem::EState state = item->get_state(); bool inside = (left <= (float)scaled_mouse_pos(0)) && ((float)scaled_mouse_pos(0) <= right) && (bottom <= (float)scaled_mouse_pos(1)) && ((float)scaled_mouse_pos(1) <= top); +#if !ENABLE_CANVAS_TOOLTIP_USING_IMGUI if (inside) { tooltip = item->get_tooltip(); @@ -718,6 +765,7 @@ std::string GLToolbar::update_hover_state_horizontal(const Vec2d& mouse_pos, GLC tooltip += "\n" + additional_tooltip; } } +#endif // !ENABLE_CANVAS_TOOLTIP_USING_IMGUI switch (state) { @@ -761,21 +809,54 @@ std::string GLToolbar::update_hover_state_horizontal(const Vec2d& mouse_pos, GLC break; } +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI + case GLToolbarItem::Disabled: + { + if (inside) + { + item->set_state(GLToolbarItem::HoverDisabled); + parent.set_as_dirty(); + } + + break; + } + case GLToolbarItem::HoverDisabled: + { + if (!inside) + { + item->set_state(GLToolbarItem::Disabled); + parent.set_as_dirty(); + } + + break; + } + default: + { + break; + } +#else default: case GLToolbarItem::Disabled: { break; } +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI } left += icon_stride; } } +#if !ENABLE_CANVAS_TOOLTIP_USING_IMGUI return tooltip; +#endif // !ENABLE_CANVAS_TOOLTIP_USING_IMGUI } +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI +void GLToolbar::update_hover_state_vertical(const Vec2d& mouse_pos, GLCanvas3D& parent) +#else std::string GLToolbar::update_hover_state_vertical(const Vec2d& mouse_pos, GLCanvas3D& parent) +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI { // NB: mouse_pos is already scaled appropriately @@ -795,7 +876,9 @@ std::string GLToolbar::update_hover_state_vertical(const Vec2d& mouse_pos, GLCan float left = m_layout.left + scaled_border; float top = m_layout.top - scaled_border; - std::string tooltip = ""; +#if !ENABLE_CANVAS_TOOLTIP_USING_IMGUI + std::string tooltip; +#endif // !ENABLE_CANVAS_TOOLTIP_USING_IMGUI for (GLToolbarItem* item : m_items) { @@ -811,6 +894,7 @@ std::string GLToolbar::update_hover_state_vertical(const Vec2d& mouse_pos, GLCan GLToolbarItem::EState state = item->get_state(); bool inside = (left <= (float)scaled_mouse_pos(0)) && ((float)scaled_mouse_pos(0) <= right) && (bottom <= (float)scaled_mouse_pos(1)) && ((float)scaled_mouse_pos(1) <= top); +#if !ENABLE_CANVAS_TOOLTIP_USING_IMGUI if (inside) { tooltip = item->get_tooltip(); @@ -821,6 +905,7 @@ std::string GLToolbar::update_hover_state_vertical(const Vec2d& mouse_pos, GLCan tooltip += "\n" + additional_tooltip; } } +#endif // !ENABLE_CANVAS_TOOLTIP_USING_IMGUI switch (state) { @@ -864,18 +949,47 @@ std::string GLToolbar::update_hover_state_vertical(const Vec2d& mouse_pos, GLCan break; } +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI + case GLToolbarItem::Disabled: + { + if (inside) + { + item->set_state(GLToolbarItem::HoverDisabled); + parent.set_as_dirty(); + } + + break; + } + case GLToolbarItem::HoverDisabled: + { + if (!inside) + { + item->set_state(GLToolbarItem::Disabled); + parent.set_as_dirty(); + } + + break; + } + default: + { + break; + } +#else default: case GLToolbarItem::Disabled: { break; } +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI } top -= icon_stride; } } +#if !ENABLE_CANVAS_TOOLTIP_USING_IMGUI return tooltip; +#endif // !ENABLE_CANVAS_TOOLTIP_USING_IMGUI } int GLToolbar::contains_mouse(const Vec2d& mouse_pos, const GLCanvas3D& parent) const @@ -1224,19 +1338,37 @@ bool GLToolbar::generate_icons_texture() const std::vector<std::pair<int, bool>> states; if (m_name == "Top") { +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI + states.push_back({ 1, false }); // Normal + states.push_back({ 0, false }); // Pressed + states.push_back({ 2, false }); // Disabled + states.push_back({ 0, false }); // Hover + states.push_back({ 0, false }); // HoverPressed + states.push_back({ 2, false }); // HoverDisabled +#else states.push_back(std::make_pair(1, false)); states.push_back(std::make_pair(0, false)); states.push_back(std::make_pair(2, false)); states.push_back(std::make_pair(0, false)); states.push_back(std::make_pair(0, false)); +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI } else if (m_name == "View") { +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI + states.push_back({ 1, false }); // Normal + states.push_back({ 1, true }); // Pressed + states.push_back({ 1, false }); // Disabled + states.push_back({ 0, false }); // Hover + states.push_back({ 1, true }); // HoverPressed + states.push_back({ 1, false }); // HoverDisabled +#else states.push_back(std::make_pair(1, false)); states.push_back(std::make_pair(1, true)); states.push_back(std::make_pair(1, false)); states.push_back(std::make_pair(0, false)); states.push_back(std::make_pair(1, true)); +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI } unsigned int sprite_size_px = (unsigned int)(m_layout.icons_size * m_layout.scale); diff --git a/src/slic3r/GUI/GLToolbar.hpp b/src/slic3r/GUI/GLToolbar.hpp index 6f24e22cf..b4aac9206 100644 --- a/src/slic3r/GUI/GLToolbar.hpp +++ b/src/slic3r/GUI/GLToolbar.hpp @@ -61,6 +61,9 @@ public: Disabled, Hover, HoverPressed, +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI + HoverDisabled, +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI Num_States }; @@ -119,9 +122,15 @@ public: void do_left_action() { m_last_action_type = Left; m_data.left.action_callback(); } void do_right_action() { m_last_action_type = Right; m_data.right.action_callback(); } +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI + bool is_enabled() const { return (m_state != Disabled) && (m_state != HoverDisabled); } + bool is_disabled() const { return (m_state == Disabled) || (m_state == HoverDisabled); } + bool is_hovered() const { return (m_state == Hover) || (m_state == HoverPressed) || (m_state == HoverDisabled); } +#else bool is_enabled() const { return m_state != Disabled; } bool is_disabled() const { return m_state == Disabled; } bool is_hovered() const { return (m_state == Hover) || (m_state == HoverPressed); } +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI bool is_pressed() const { return (m_state == Pressed) || (m_state == HoverPressed); } bool is_visible() const { return m_data.visible; } bool is_separator() const { return m_type == Separator; } @@ -252,7 +261,9 @@ private: }; MouseCapture m_mouse_capture; +#if !ENABLE_CANVAS_TOOLTIP_USING_IMGUI std::string m_tooltip; +#endif // !ENABLE_CANVAS_TOOLTIP_USING_IMGUI int m_pressed_toggable_id; public: @@ -298,7 +309,11 @@ public: void force_left_action(int item_id, GLCanvas3D& parent) { do_action(GLToolbarItem::Left, item_id, parent, false); } void force_right_action(int item_id, GLCanvas3D& parent) { do_action(GLToolbarItem::Right, item_id, parent, false); } +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI + std::string get_tooltip() const; +#else const std::string& get_tooltip() const { return m_tooltip; } +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI void get_additional_tooltip(int item_id, std::string& text); void set_additional_tooltip(int item_id, const std::string& text); @@ -318,9 +333,15 @@ private: float get_height_vertical() const; float get_main_size() const; void do_action(GLToolbarItem::EActionType type, int item_id, GLCanvas3D& parent, bool check_hover); +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI + void update_hover_state(const Vec2d& mouse_pos, GLCanvas3D& parent); + void update_hover_state_horizontal(const Vec2d& mouse_pos, GLCanvas3D& parent); + void update_hover_state_vertical(const Vec2d& mouse_pos, GLCanvas3D& parent); +#else std::string update_hover_state(const Vec2d& mouse_pos, GLCanvas3D& parent); std::string update_hover_state_horizontal(const Vec2d& mouse_pos, GLCanvas3D& parent); std::string update_hover_state_vertical(const Vec2d& mouse_pos, GLCanvas3D& parent); +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI // returns the id of the item under the given mouse position or -1 if none int contains_mouse(const Vec2d& mouse_pos, const GLCanvas3D& parent) const; int contains_mouse_horizontal(const Vec2d& mouse_pos, const GLCanvas3D& parent) const; diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index cc6c06690..f44efa130 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -594,6 +594,12 @@ void GUI_App::load_project(wxWindow *parent, wxString& input_file) const void GUI_App::import_model(wxWindow *parent, wxArrayString& input_files) const { +#if ENABLE_CANVAS_TOOLTIP_USING_IMGUI + if (this->plater_ != nullptr) + // hides the tooltip + plater_->get_current_canvas3D()->set_tooltip(""); +#endif // ENABLE_CANVAS_TOOLTIP_USING_IMGUI + input_files.Clear(); wxFileDialog dialog(parent ? parent : GetTopWindow(), _(L("Choose one or more files (STL/OBJ/AMF/3MF/PRUSA):")),