From b9ce19b07c005e6ffc1ed4c2136c34008ef3e797 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Tue, 19 Jun 2018 09:46:26 +0200 Subject: [PATCH] Object updated by rotate gizmo --- lib/Slic3r/GUI/Plater.pm | 20 +++- xs/src/slic3r/GUI/3DScene.cpp | 10 ++ xs/src/slic3r/GUI/3DScene.hpp | 2 + xs/src/slic3r/GUI/GLCanvas3D.cpp | 119 ++++++++++++++++++------ xs/src/slic3r/GUI/GLCanvas3D.hpp | 13 ++- xs/src/slic3r/GUI/GLCanvas3DManager.cpp | 14 +++ xs/src/slic3r/GUI/GLCanvas3DManager.hpp | 2 + xs/src/slic3r/GUI/GLGizmo.cpp | 18 +++- xs/src/slic3r/GUI/GLGizmo.hpp | 5 +- xs/xsp/GUI_3DScene.xsp | 13 +++ 10 files changed, 180 insertions(+), 36 deletions(-) diff --git a/lib/Slic3r/GUI/Plater.pm b/lib/Slic3r/GUI/Plater.pm index d1ccf07d5..0185749a3 100644 --- a/lib/Slic3r/GUI/Plater.pm +++ b/lib/Slic3r/GUI/Plater.pm @@ -14,6 +14,7 @@ use Wx qw(:button :colour :cursor :dialog :filedialog :keycode :icon :font :id : use Wx::Event qw(EVT_BUTTON EVT_TOGGLEBUTTON EVT_COMMAND EVT_KEY_DOWN EVT_LIST_ITEM_ACTIVATED EVT_LIST_ITEM_DESELECTED EVT_LIST_ITEM_SELECTED EVT_LEFT_DOWN EVT_MOUSE_EVENTS EVT_PAINT EVT_TOOL EVT_CHOICE EVT_COMBOBOX EVT_TIMER EVT_NOTEBOOK_PAGE_CHANGED); +use Slic3r::Geometry qw(PI); use base 'Wx::Panel'; use constant TB_ADD => &Wx::NewId; @@ -134,6 +135,12 @@ sub new { $self->schedule_background_process; }; + # callback to react to gizmo rotate + my $on_gizmo_rotate = sub { + my ($angle_z) = @_; + $self->rotate(rad2deg($angle_z), Z, 'absolute'); + }; + # Initialize 3D plater if ($Slic3r::GUI::have_OpenGL) { $self->{canvas3D} = Slic3r::GUI::Plater::3D->new($self->{preview_notebook}, $self->{objects}, $self->{model}, $self->{print}, $self->{config}); @@ -151,6 +158,7 @@ sub new { Slic3r::GUI::_3DScene::register_on_instance_moved_callback($self->{canvas3D}, $on_instances_moved); Slic3r::GUI::_3DScene::register_on_enable_action_buttons_callback($self->{canvas3D}, $enable_action_buttons); Slic3r::GUI::_3DScene::register_on_gizmo_scale_uniformly_callback($self->{canvas3D}, $on_gizmo_scale_uniformly); + Slic3r::GUI::_3DScene::register_on_gizmo_rotate_callback($self->{canvas3D}, $on_gizmo_rotate); Slic3r::GUI::_3DScene::enable_gizmos($self->{canvas3D}, 1); Slic3r::GUI::_3DScene::enable_shader($self->{canvas3D}, 1); Slic3r::GUI::_3DScene::enable_force_zoom_to_bed($self->{canvas3D}, 1); @@ -1060,7 +1068,17 @@ sub rotate { if ($axis == Z) { my $new_angle = deg2rad($angle); - $_->set_rotation(($relative ? $_->rotation : 0.) + $new_angle) for @{ $model_object->instances }; + foreach my $inst (@{ $model_object->instances }) { + my $rotation = ($relative ? $inst->rotation : 0.) + $new_angle; + while ($rotation > 2.0 * PI) { + $rotation -= 2.0 * PI; + } + while ($rotation < 0.0) { + $rotation += 2.0 * PI; + } + $inst->set_rotation($rotation); + Slic3r::GUI::_3DScene::update_gizmos_data($self->{canvas3D}) if ($self->{canvas3D}); + } $object->transform_thumbnail($self->{model}, $obj_idx); } else { # rotation around X and Y needs to be performed on mesh diff --git a/xs/src/slic3r/GUI/3DScene.cpp b/xs/src/slic3r/GUI/3DScene.cpp index 1879b3082..352738c7c 100644 --- a/xs/src/slic3r/GUI/3DScene.cpp +++ b/xs/src/slic3r/GUI/3DScene.cpp @@ -1948,6 +1948,11 @@ void _3DScene::update_volumes_colors_by_extruder(wxGLCanvas* canvas) s_canvas_mgr.update_volumes_colors_by_extruder(canvas); } +void _3DScene::update_gizmos_data(wxGLCanvas* canvas) +{ + s_canvas_mgr.update_gizmos_data(canvas); +} + void _3DScene::render(wxGLCanvas* canvas) { s_canvas_mgr.render(canvas); @@ -2043,6 +2048,11 @@ void _3DScene::register_on_gizmo_scale_uniformly_callback(wxGLCanvas* canvas, vo s_canvas_mgr.register_on_gizmo_scale_uniformly_callback(canvas, callback); } +void _3DScene::register_on_gizmo_rotate_callback(wxGLCanvas* canvas, void* callback) +{ + s_canvas_mgr.register_on_gizmo_rotate_callback(canvas, callback); +} + static inline int hex_digit_to_int(const char c) { return diff --git a/xs/src/slic3r/GUI/3DScene.hpp b/xs/src/slic3r/GUI/3DScene.hpp index c6a166397..e7d43aee2 100644 --- a/xs/src/slic3r/GUI/3DScene.hpp +++ b/xs/src/slic3r/GUI/3DScene.hpp @@ -568,6 +568,7 @@ public: static void set_viewport_from_scene(wxGLCanvas* canvas, wxGLCanvas* other); static void update_volumes_colors_by_extruder(wxGLCanvas* canvas); + static void update_gizmos_data(wxGLCanvas* canvas); static void render(wxGLCanvas* canvas); @@ -590,6 +591,7 @@ public: static void register_on_wipe_tower_moved_callback(wxGLCanvas* canvas, void* callback); static void register_on_enable_action_buttons_callback(wxGLCanvas* canvas, void* callback); static void register_on_gizmo_scale_uniformly_callback(wxGLCanvas* canvas, void* callback); + static void register_on_gizmo_rotate_callback(wxGLCanvas* canvas, void* callback); static std::vector load_object(wxGLCanvas* canvas, const ModelObject* model_object, int obj_idx, std::vector instance_idxs); static std::vector load_object(wxGLCanvas* canvas, const Model* model, int obj_idx); diff --git a/xs/src/slic3r/GUI/GLCanvas3D.cpp b/xs/src/slic3r/GUI/GLCanvas3D.cpp index f9c10017e..b9a6ad218 100644 --- a/xs/src/slic3r/GUI/GLCanvas3D.cpp +++ b/xs/src/slic3r/GUI/GLCanvas3D.cpp @@ -1263,14 +1263,9 @@ void GLCanvas3D::Gizmos::update(const Pointf& mouse_pos) curr->update(mouse_pos); } -void GLCanvas3D::Gizmos::update_data(float scale) +GLCanvas3D::Gizmos::EType GLCanvas3D::Gizmos::get_current_type() const { - if (!m_enabled) - return; - - GizmosMap::const_iterator it = m_gizmos.find(Scale); - if (it != m_gizmos.end()) - reinterpret_cast(it->second)->set_scale(scale); + return m_current; } bool GLCanvas3D::Gizmos::is_running() const @@ -1309,6 +1304,35 @@ float GLCanvas3D::Gizmos::get_scale() const return (it != m_gizmos.end()) ? reinterpret_cast(it->second)->get_scale() : 1.0f; } +void GLCanvas3D::Gizmos::set_scale(float scale) +{ + if (!m_enabled) + return; + + GizmosMap::const_iterator it = m_gizmos.find(Scale); + if (it != m_gizmos.end()) + reinterpret_cast(it->second)->set_scale(scale); +} + +float GLCanvas3D::Gizmos::get_angle_z() const +{ + if (!m_enabled) + return 0.0f; + + GizmosMap::const_iterator it = m_gizmos.find(Rotate); + return (it != m_gizmos.end()) ? reinterpret_cast(it->second)->get_angle_z() : 0.0f; +} + +void GLCanvas3D::Gizmos::set_angle_z(float angle_z) +{ + if (!m_enabled) + return; + + GizmosMap::const_iterator it = m_gizmos.find(Rotate); + if (it != m_gizmos.end()) + reinterpret_cast(it->second)->set_angle_z(angle_z); +} + void GLCanvas3D::Gizmos::render(const GLCanvas3D& canvas, const BoundingBoxf3& box) const { if (!m_enabled) @@ -1823,6 +1847,38 @@ void GLCanvas3D::update_volumes_colors_by_extruder() m_volumes.update_colors_by_extruder(m_config); } +void GLCanvas3D::update_gizmos_data() +{ + if (!m_gizmos.is_running()) + return; + + int id = _get_first_selected_object_id(); + if ((id != -1) && (m_model != nullptr)) + { + ModelObject* model_object = m_model->objects[id]; + if (model_object != nullptr) + { + ModelInstance* model_instance = model_object->instances[0]; + if (model_instance != nullptr) + { + switch (m_gizmos.get_current_type()) + { + case Gizmos::Scale: + { + m_gizmos.set_scale(model_instance->scaling_factor); + break; + } + case Gizmos::Rotate: + { + m_gizmos.set_angle_z(model_instance->rotation); + break; + } + } + } + } + } +} + void GLCanvas3D::render() { if (m_canvas == nullptr) @@ -1930,6 +1986,7 @@ void GLCanvas3D::reload_scene(bool force) m_objects_volumes_idxs.push_back(load_object(*m_model, obj_idx)); } + update_gizmos_data(); update_volumes_selection(m_objects_selections); if (m_config->has("nozzle_diameter")) @@ -2477,6 +2534,12 @@ void GLCanvas3D::register_on_gizmo_scale_uniformly_callback(void* callback) m_on_gizmo_scale_uniformly_callback.register_callback(callback); } +void GLCanvas3D::register_on_gizmo_rotate_callback(void* callback) +{ + if (callback != nullptr) + m_on_gizmo_rotate_callback.register_callback(callback); +} + void GLCanvas3D::bind_event_handlers() { if (m_canvas != nullptr) @@ -2694,13 +2757,13 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) } else if ((selected_object_idx != -1) && gizmos_overlay_contains_mouse) { + update_gizmos_data(); m_gizmos.update_on_off_state(*this, m_mouse.position); - _update_gizmos_data(); m_dirty = true; } else if ((selected_object_idx != -1) && m_gizmos.grabber_contains_mouse()) - { - _update_gizmos_data(); + { + update_gizmos_data(); m_gizmos.start_dragging(); m_dirty = true; } @@ -2726,9 +2789,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) } } - if (m_gizmos.is_running()) - _update_gizmos_data(); - + update_gizmos_data(); m_dirty = true; } } @@ -2821,7 +2882,21 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) const Pointf3& cur_pos = _mouse_to_bed_3d(pos); m_gizmos.update(Pointf(cur_pos.x, cur_pos.y)); - m_on_gizmo_scale_uniformly_callback.call((double)m_gizmos.get_scale()); + switch (m_gizmos.get_current_type()) + { + case Gizmos::Scale: + { + m_on_gizmo_scale_uniformly_callback.call((double)m_gizmos.get_scale()); + break; + } + case Gizmos::Rotate: + { + m_on_gizmo_rotate_callback.call((double)m_gizmos.get_angle_z()); + break; + } + default: + break; + } m_dirty = true; } else if (evt.Dragging() && !gizmos_overlay_contains_mouse) @@ -3164,6 +3239,7 @@ void GLCanvas3D::_deregister_callbacks() m_on_wipe_tower_moved_callback.deregister_callback(); m_on_enable_action_buttons_callback.deregister_callback(); m_on_gizmo_scale_uniformly_callback.deregister_callback(); + m_on_gizmo_rotate_callback.deregister_callback(); } void GLCanvas3D::_mark_volumes_for_layer_height() const @@ -4264,21 +4340,6 @@ void GLCanvas3D::_on_select(int volume_idx) m_on_select_object_callback.call(id); } -void GLCanvas3D::_update_gizmos_data() -{ - int id = _get_first_selected_object_id(); - if ((id != -1) && (m_model != nullptr)) - { - ModelObject* model_object = m_model->objects[id]; - if (model_object != nullptr) - { - ModelInstance* model_instance = model_object->instances[0]; - if (model_instance != nullptr) - m_gizmos.update_data(model_instance->scaling_factor); - } - } -} - std::vector GLCanvas3D::_parse_colors(const std::vector& colors) { static const float INV_255 = 1.0f / 255.0f; diff --git a/xs/src/slic3r/GUI/GLCanvas3D.hpp b/xs/src/slic3r/GUI/GLCanvas3D.hpp index c503d1845..ba4d4d106 100644 --- a/xs/src/slic3r/GUI/GLCanvas3D.hpp +++ b/xs/src/slic3r/GUI/GLCanvas3D.hpp @@ -360,14 +360,20 @@ public: bool overlay_contains_mouse(const GLCanvas3D& canvas, const Pointf& mouse_pos) const; bool grabber_contains_mouse() const; void update(const Pointf& mouse_pos); - void update_data(float scale); + + EType get_current_type() const; bool is_running() const; + bool is_dragging() const; void start_dragging(); void stop_dragging(); float get_scale() const; + void set_scale(float scale); + + float get_angle_z() const; + void set_angle_z(float angle_z); void render(const GLCanvas3D& canvas, const BoundingBoxf3& box) const; void render_current_gizmo_for_picking_pass(const BoundingBoxf3& box) const; @@ -442,6 +448,7 @@ private: PerlCallback m_on_wipe_tower_moved_callback; PerlCallback m_on_enable_action_buttons_callback; PerlCallback m_on_gizmo_scale_uniformly_callback; + PerlCallback m_on_gizmo_rotate_callback; public: GLCanvas3D(wxGLCanvas* canvas, wxGLContext* context); @@ -510,6 +517,7 @@ public: void set_viewport_from_scene(const GLCanvas3D& other); void update_volumes_colors_by_extruder(); + void update_gizmos_data(); void render(); @@ -548,6 +556,7 @@ public: void register_on_wipe_tower_moved_callback(void* callback); void register_on_enable_action_buttons_callback(void* callback); void register_on_gizmo_scale_uniformly_callback(void* callback); + void register_on_gizmo_rotate_callback(void* callback); void bind_event_handlers(); void unbind_event_handlers(); @@ -628,8 +637,6 @@ private: void _on_move(const std::vector& volume_idxs); void _on_select(int volume_idx); - void _update_gizmos_data(); - static std::vector _parse_colors(const std::vector& colors); }; diff --git a/xs/src/slic3r/GUI/GLCanvas3DManager.cpp b/xs/src/slic3r/GUI/GLCanvas3DManager.cpp index f288ee456..8785c7a9d 100644 --- a/xs/src/slic3r/GUI/GLCanvas3DManager.cpp +++ b/xs/src/slic3r/GUI/GLCanvas3DManager.cpp @@ -494,6 +494,13 @@ void GLCanvas3DManager::update_volumes_colors_by_extruder(wxGLCanvas* canvas) it->second->update_volumes_colors_by_extruder(); } +void GLCanvas3DManager::update_gizmos_data(wxGLCanvas* canvas) +{ + CanvasesMap::const_iterator it = _get_canvas(canvas); + if (it != m_canvases.end()) + it->second->update_gizmos_data(); +} + void GLCanvas3DManager::render(wxGLCanvas* canvas) const { CanvasesMap::const_iterator it = _get_canvas(canvas); @@ -685,6 +692,13 @@ void GLCanvas3DManager::register_on_gizmo_scale_uniformly_callback(wxGLCanvas* c it->second->register_on_gizmo_scale_uniformly_callback(callback); } +void GLCanvas3DManager::register_on_gizmo_rotate_callback(wxGLCanvas* canvas, void* callback) +{ + CanvasesMap::iterator it = _get_canvas(canvas); + if (it != m_canvases.end()) + it->second->register_on_gizmo_rotate_callback(callback); +} + GLCanvas3DManager::CanvasesMap::iterator GLCanvas3DManager::_get_canvas(wxGLCanvas* canvas) { return (canvas == nullptr) ? m_canvases.end() : m_canvases.find(canvas); diff --git a/xs/src/slic3r/GUI/GLCanvas3DManager.hpp b/xs/src/slic3r/GUI/GLCanvas3DManager.hpp index 6989da791..518341f0d 100644 --- a/xs/src/slic3r/GUI/GLCanvas3DManager.hpp +++ b/xs/src/slic3r/GUI/GLCanvas3DManager.hpp @@ -121,6 +121,7 @@ public: void set_viewport_from_scene(wxGLCanvas* canvas, wxGLCanvas* other); void update_volumes_colors_by_extruder(wxGLCanvas* canvas); + void update_gizmos_data(wxGLCanvas* canvas); void render(wxGLCanvas* canvas) const; @@ -153,6 +154,7 @@ public: void register_on_wipe_tower_moved_callback(wxGLCanvas* canvas, void* callback); void register_on_enable_action_buttons_callback(wxGLCanvas* canvas, void* callback); void register_on_gizmo_scale_uniformly_callback(wxGLCanvas* canvas, void* callback); + void register_on_gizmo_rotate_callback(wxGLCanvas* canvas, void* callback); private: CanvasesMap::iterator _get_canvas(wxGLCanvas* canvas); diff --git a/xs/src/slic3r/GUI/GLGizmo.cpp b/xs/src/slic3r/GUI/GLGizmo.cpp index d3aae33e8..0b5f4b3b7 100644 --- a/xs/src/slic3r/GUI/GLGizmo.cpp +++ b/xs/src/slic3r/GUI/GLGizmo.cpp @@ -140,7 +140,7 @@ void GLGizmoBase::on_start_dragging() void GLGizmoBase::render_grabbers() const { - for (unsigned int i = 0; i < (unsigned int)m_grabbers.size(); ++i) + for (int i = 0; i < (int)m_grabbers.size(); ++i) { m_grabbers[i].render(m_hover_id == i); } @@ -165,6 +165,19 @@ GLGizmoRotate::GLGizmoRotate() { } +float GLGizmoRotate::get_angle_z() const +{ + return m_angle_z; +} + +void GLGizmoRotate::set_angle_z(float angle_z) +{ + if (std::abs(angle_z - 2.0f * PI) < EPSILON) + angle_z = 0.0f; + + m_angle_z = angle_z; +} + bool GLGizmoRotate::on_init() { std::string path = resources_dir() + "/icons/overlay/"; @@ -194,6 +207,7 @@ void GLGizmoRotate::on_update(const Pointf& mouse_pos) if (cross(orig_dir, new_dir) < 0.0) theta = 2.0 * (coordf_t)PI - theta; + // snap if (length(m_center.vector_to(mouse_pos)) < 2.0 * (double)m_radius / 3.0) { coordf_t step = 2.0 * (coordf_t)PI / (coordf_t)SnapRegionsCount; @@ -202,7 +216,7 @@ void GLGizmoRotate::on_update(const Pointf& mouse_pos) if (theta == 2.0 * (coordf_t)PI) theta = 0.0; - + m_angle_z = (float)theta; } diff --git a/xs/src/slic3r/GUI/GLGizmo.hpp b/xs/src/slic3r/GUI/GLGizmo.hpp index 2baec8f9b..d8a5517c1 100644 --- a/xs/src/slic3r/GUI/GLGizmo.hpp +++ b/xs/src/slic3r/GUI/GLGizmo.hpp @@ -100,6 +100,9 @@ class GLGizmoRotate : public GLGizmoBase public: GLGizmoRotate(); + float get_angle_z() const; + void set_angle_z(float angle_z); + protected: virtual bool on_init(); virtual void on_update(const Pointf& mouse_pos); @@ -120,9 +123,9 @@ class GLGizmoScale : public GLGizmoBase static const float Offset; float m_scale; + float m_starting_scale; Pointf m_starting_drag_position; - float m_starting_scale; public: GLGizmoScale(); diff --git a/xs/xsp/GUI_3DScene.xsp b/xs/xsp/GUI_3DScene.xsp index 29f35293b..f9c1f9f0f 100644 --- a/xs/xsp/GUI_3DScene.xsp +++ b/xs/xsp/GUI_3DScene.xsp @@ -470,6 +470,12 @@ update_volumes_colors_by_extruder(canvas) CODE: _3DScene::update_volumes_colors_by_extruder((wxGLCanvas*)wxPli_sv_2_object(aTHX_ canvas, "Wx::GLCanvas")); +void +update_gizmos_data(canvas) + SV *canvas; + CODE: + _3DScene::update_gizmos_data((wxGLCanvas*)wxPli_sv_2_object(aTHX_ canvas, "Wx::GLCanvas")); + void render(canvas) SV *canvas; @@ -605,6 +611,13 @@ register_on_gizmo_scale_uniformly_callback(canvas, callback) CODE: _3DScene::register_on_gizmo_scale_uniformly_callback((wxGLCanvas*)wxPli_sv_2_object(aTHX_ canvas, "Wx::GLCanvas"), (void*)callback); +void +register_on_gizmo_rotate_callback(canvas, callback) + SV *canvas; + SV *callback; + CODE: + _3DScene::register_on_gizmo_rotate_callback((wxGLCanvas*)wxPli_sv_2_object(aTHX_ canvas, "Wx::GLCanvas"), (void*)callback); + unsigned int finalize_legend_texture() CODE: