diff options
Diffstat (limited to 'src/slic3r/GUI/GLToolbar.cpp')
-rw-r--r-- | src/slic3r/GUI/GLToolbar.cpp | 241 |
1 files changed, 124 insertions, 117 deletions
diff --git a/src/slic3r/GUI/GLToolbar.cpp b/src/slic3r/GUI/GLToolbar.cpp index c7b391cd6..79e7ea1c6 100644 --- a/src/slic3r/GUI/GLToolbar.cpp +++ b/src/slic3r/GUI/GLToolbar.cpp @@ -3,9 +3,10 @@ #include "GLToolbar.hpp" -#include "../../slic3r/GUI/GLCanvas3D.hpp" - -#include <GL/glew.h> +#include "slic3r/GUI/GLCanvas3D.hpp" +#include "slic3r/GUI/GUI_App.hpp" +#include "slic3r/GUI/Camera.hpp" +#include "slic3r/GUI/Plater.hpp" #include <wx/event.h> #include <wx/bitmap.h> @@ -16,7 +17,6 @@ namespace Slic3r { namespace GUI { - wxDEFINE_EVENT(EVT_GLTOOLBAR_ADD, SimpleEvent); wxDEFINE_EVENT(EVT_GLTOOLBAR_DELETE, SimpleEvent); wxDEFINE_EVENT(EVT_GLTOOLBAR_DELETE_ALL, SimpleEvent); @@ -70,8 +70,8 @@ bool GLToolbarItem::update_visibility() bool ret = (m_data.visible != visible); if (ret) m_data.visible = visible; - - return ret; + // Return false for separator as it would always return true. + return is_separator() ? false : ret; } bool GLToolbarItem::update_enabled_state() @@ -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,6 @@ GLToolbar::GLToolbar(GLToolbar::EType type, const std::string& name) , m_name(name) , m_enabled(false) , m_icons_texture_dirty(true) - , m_tooltip("") , m_pressed_toggable_id(-1) { } @@ -231,24 +230,13 @@ void GLToolbar::set_icons_size(float size) void GLToolbar::set_scale(float scale) { - if (m_layout.scale != scale) - { + if (m_layout.scale != scale) { m_layout.scale = scale; m_layout.dirty = true; m_icons_texture_dirty = true; } } -bool GLToolbar::is_enabled() const -{ - return m_enabled; -} - -void GLToolbar::set_enabled(bool enable) -{ - m_enabled = true; -} - bool GLToolbar::add_item(const GLToolbarItem::Data& data) { GLToolbarItem* item = new GLToolbarItem(GLToolbarItem::Action, data); @@ -358,29 +346,50 @@ int GLToolbar::get_item_id(const std::string& name) const return -1; } -void GLToolbar::get_additional_tooltip(int item_id, std::string& text) +std::string GLToolbar::get_tooltip() const { - if ((0 <= item_id) && (item_id < (int)m_items.size())) + std::string tooltip; + + for (GLToolbarItem* item : m_items) { - GLToolbarItem* item = m_items[item_id]; - if (item != nullptr) + if (item->is_hovered()) { - text = item->get_additional_tooltip(); - return; + 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; + } } } - text.clear(); + return tooltip; } -void GLToolbar::set_additional_tooltip(int item_id, const std::string& text) +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) - item->set_additional_tooltip(text); + text = m_items[item_id]->get_additional_tooltip(); + return; } + + text.clear(); +} + +void GLToolbar::set_additional_tooltip(int item_id, const std::string& text) +{ + if (0 <= item_id && item_id < (int)m_items.size()) + m_items[item_id]->set_additional_tooltip(text); +} + +void GLToolbar::set_tooltip(int item_id, const std::string& text) +{ + if (0 <= item_id && item_id < (int)m_items.size()) + m_items[item_id]->set_tooltip(text); } bool GLToolbar::update_items_state() @@ -425,17 +434,13 @@ 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 (contains_mouse(mouse_pos, parent) == -1) - // mouse is outside the toolbar - m_tooltip.clear(); return true; } m_mouse_capture.reset(); } -#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) @@ -474,27 +479,9 @@ bool GLToolbar::on_mouse(wxMouseEvent& evt, GLCanvas3D& parent) else return false; } -#else - if (evt.Moving()) - m_tooltip = update_hover_state(mouse_pos, parent); - else if (evt.LeftUp()) - m_mouse_capture.left = false; - else if (evt.MiddleUp()) - m_mouse_capture.middle = false; - else if (evt.RightUp()) - m_mouse_capture.right = false; - else if (evt.Dragging() && m_mouse_capture.any()) - // if the button down was done on this toolbar, prevent from dragging into the scene - processed = true; -#endif // ENABLE_MODIFIED_TOOLBAR_MOUSE_EVENT_HANDLING int item_id = contains_mouse(mouse_pos, parent); - if (item_id == -1) - { - // mouse is outside the toolbar - m_tooltip.clear(); - } - else + if (item_id != -1) { // mouse inside toolbar if (evt.LeftDown() || evt.LeftDClick()) @@ -502,7 +489,8 @@ bool GLToolbar::on_mouse(wxMouseEvent& evt, GLCanvas3D& parent) m_mouse_capture.left = true; m_mouse_capture.parent = &parent; processed = true; - if ((item_id != -2) && !m_items[item_id]->is_separator() && ((m_pressed_toggable_id == -1) || (m_items[item_id]->get_last_action_type() == GLToolbarItem::Left))) + if ((item_id != -2) && !m_items[item_id]->is_separator() && !m_items[item_id]->is_disabled() && + ((m_pressed_toggable_id == -1) || (m_items[item_id]->get_last_action_type() == GLToolbarItem::Left))) { // mouse is inside an icon do_action(GLToolbarItem::Left, item_id, parent, true); @@ -519,17 +507,14 @@ bool GLToolbar::on_mouse(wxMouseEvent& evt, GLCanvas3D& parent) m_mouse_capture.right = true; m_mouse_capture.parent = &parent; processed = true; - if ((item_id != -2) && !m_items[item_id]->is_separator() && ((m_pressed_toggable_id == -1) || (m_items[item_id]->get_last_action_type() == GLToolbarItem::Right))) + if ((item_id != -2) && !m_items[item_id]->is_separator() && !m_items[item_id]->is_disabled() && + ((m_pressed_toggable_id == -1) || (m_items[item_id]->get_last_action_type() == GLToolbarItem::Right))) { // mouse is inside an icon do_action(GLToolbarItem::Right, item_id, parent, true); parent.set_as_dirty(); } } -#if !ENABLE_MODIFIED_TOOLBAR_MOUSE_EVENT_HANDLING - else if (evt.LeftUp()) - processed = true; -#endif // !ENABLE_MODIFIED_TOOLBAR_MOUSE_EVENT_HANDLING } return processed; @@ -597,6 +582,16 @@ float GLToolbar::get_main_size() const return size * m_layout.scale; } +int GLToolbar::get_visible_items_cnt() const +{ + int cnt = 0; + for (unsigned int i = 0; i < (unsigned int)m_items.size(); ++i) + if (m_items[i]->is_visible() && !m_items[i]->is_separator()) + cnt++; + + return cnt; +} + void GLToolbar::do_action(GLToolbarItem::EActionType type, int item_id, GLCanvas3D& parent, bool check_hover) { if ((m_pressed_toggable_id == -1) || (m_pressed_toggable_id == item_id)) @@ -604,7 +599,7 @@ void GLToolbar::do_action(GLToolbarItem::EActionType type, int item_id, GLCanvas if ((0 <= item_id) && (item_id < (int)m_items.size())) { GLToolbarItem* item = m_items[item_id]; - if ((item != nullptr) && !item->is_separator() && (!check_hover || item->is_hovered())) + if ((item != nullptr) && !item->is_separator() && !item->is_disabled() && (!check_hover || item->is_hovered())) { if (((type == GLToolbarItem::Right) && item->is_right_toggable()) || ((type == GLToolbarItem::Left) && item->is_left_toggable())) @@ -658,24 +653,24 @@ void GLToolbar::do_action(GLToolbarItem::EActionType type, int item_id, GLCanvas } } -std::string GLToolbar::update_hover_state(const Vec2d& mouse_pos, GLCanvas3D& parent) +void GLToolbar::update_hover_state(const Vec2d& mouse_pos, GLCanvas3D& parent) { if (!m_enabled) - return ""; + return; switch (m_layout.type) { default: - case Layout::Horizontal: { return update_hover_state_horizontal(mouse_pos, parent); } - case Layout::Vertical: { return update_hover_state_vertical(mouse_pos, parent); } + case Layout::Horizontal: { update_hover_state_horizontal(mouse_pos, parent); break; } + case Layout::Vertical: { update_hover_state_vertical(mouse_pos, parent); break; } } } -std::string GLToolbar::update_hover_state_horizontal(const Vec2d& mouse_pos, GLCanvas3D& parent) +void GLToolbar::update_hover_state_horizontal(const Vec2d& mouse_pos, GLCanvas3D& parent) { // NB: mouse_pos is already scaled appropriately - float inv_zoom = (float)parent.get_camera().get_inv_zoom(); + float inv_zoom = (float)wxGetApp().plater()->get_camera().get_inv_zoom(); float factor = m_layout.scale * inv_zoom; Size cnv_size = parent.get_canvas_size(); @@ -692,8 +687,6 @@ 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 = ""; - for (GLToolbarItem* item : m_items) { if (!item->is_visible()) @@ -708,16 +701,6 @@ 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 (inside) - { - 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; - } - } switch (state) { @@ -761,9 +744,28 @@ std::string GLToolbar::update_hover_state_horizontal(const Vec2d& mouse_pos, GLC break; } - default: 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; } } @@ -771,15 +773,13 @@ std::string GLToolbar::update_hover_state_horizontal(const Vec2d& mouse_pos, GLC left += icon_stride; } } - - return tooltip; } -std::string GLToolbar::update_hover_state_vertical(const Vec2d& mouse_pos, GLCanvas3D& parent) +void GLToolbar::update_hover_state_vertical(const Vec2d& mouse_pos, GLCanvas3D& parent) { // NB: mouse_pos is already scaled appropriately - float inv_zoom = (float)parent.get_camera().get_inv_zoom(); + float inv_zoom = (float)wxGetApp().plater()->get_camera().get_inv_zoom(); float factor = m_layout.scale * inv_zoom; Size cnv_size = parent.get_canvas_size(); @@ -795,8 +795,6 @@ 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 = ""; - for (GLToolbarItem* item : m_items) { if (!item->is_visible()) @@ -811,16 +809,6 @@ 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 (inside) - { - 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; - } - } switch (state) { @@ -864,9 +852,28 @@ std::string GLToolbar::update_hover_state_vertical(const Vec2d& mouse_pos, GLCan break; } - default: 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; } } @@ -874,8 +881,6 @@ std::string GLToolbar::update_hover_state_vertical(const Vec2d& mouse_pos, GLCan top -= icon_stride; } } - - return tooltip; } int GLToolbar::contains_mouse(const Vec2d& mouse_pos, const GLCanvas3D& parent) const @@ -895,7 +900,7 @@ int GLToolbar::contains_mouse_horizontal(const Vec2d& mouse_pos, const GLCanvas3 { // NB: mouse_pos is already scaled appropriately - float inv_zoom = (float)parent.get_camera().get_inv_zoom(); + float inv_zoom = (float)wxGetApp().plater()->get_camera().get_inv_zoom(); float factor = m_layout.scale * inv_zoom; Size cnv_size = parent.get_canvas_size(); @@ -968,7 +973,7 @@ int GLToolbar::contains_mouse_vertical(const Vec2d& mouse_pos, const GLCanvas3D& { // NB: mouse_pos is already scaled appropriately - float inv_zoom = (float)parent.get_camera().get_inv_zoom(); + float inv_zoom = (float)wxGetApp().plater()->get_camera().get_inv_zoom(); float factor = m_layout.scale * inv_zoom; Size cnv_size = parent.get_canvas_size(); @@ -1120,7 +1125,7 @@ void GLToolbar::render_horizontal(const GLCanvas3D& parent) const int tex_width = m_icons_texture.get_width(); int tex_height = m_icons_texture.get_height(); - float inv_zoom = (float)parent.get_camera().get_inv_zoom(); + float inv_zoom = (float)wxGetApp().plater()->get_camera().get_inv_zoom(); float factor = inv_zoom * m_layout.scale; float scaled_icons_size = m_layout.icons_size * factor; @@ -1168,7 +1173,7 @@ void GLToolbar::render_vertical(const GLCanvas3D& parent) const int tex_width = m_icons_texture.get_width(); int tex_height = m_icons_texture.get_height(); - float inv_zoom = (float)parent.get_camera().get_inv_zoom(); + float inv_zoom = (float)wxGetApp().plater()->get_camera().get_inv_zoom(); float factor = inv_zoom * m_layout.scale; float scaled_icons_size = m_layout.icons_size * factor; @@ -1222,21 +1227,23 @@ bool GLToolbar::generate_icons_texture() const } std::vector<std::pair<int, bool>> states; - if (m_name == "Top") + if (m_type == Normal) { - 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)); + 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 if (m_name == "View") + 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)); + 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 } unsigned int sprite_size_px = (unsigned int)(m_layout.icons_size * m_layout.scale); |