From 576a63cd202852bb015b806306b97cf53b7c2026 Mon Sep 17 00:00:00 2001
From: Filip Sykala <filip.sykala@prusa3d.cz>
Date: Thu, 10 Feb 2022 08:50:38 +0100
Subject: [PATCH] Fix for cut gizmo during move of object

---
 src/slic3r/GUI/Gizmos/GLGizmoBase.cpp |  5 +++--
 src/slic3r/GUI/Gizmos/GLGizmoCut.cpp  | 16 +++++++++++-----
 src/slic3r/GUI/Gizmos/GLGizmoCut.hpp  |  2 +-
 3 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/src/slic3r/GUI/Gizmos/GLGizmoBase.cpp b/src/slic3r/GUI/Gizmos/GLGizmoBase.cpp
index eb6bf3ca8..c24a2372d 100644
--- a/src/slic3r/GUI/Gizmos/GLGizmoBase.cpp
+++ b/src/slic3r/GUI/Gizmos/GLGizmoBase.cpp
@@ -159,8 +159,9 @@ bool GLGizmoBase::use_grabbers(const wxMouseEvent &mouse_event) {
     } 
 
     if (mouse_event.LeftDown()) {
-        Selection &selection = m_parent.get_selection();
-        if (!selection.is_empty() && m_hover_id != -1) {
+        Selection &selection = m_parent.get_selection();        
+        if (!selection.is_empty() && m_hover_id != -1 && 
+            (m_grabbers.empty() || m_hover_id < static_cast<int>(m_grabbers.size()))) {
             // TODO: investigate if it is neccessary -> there was no stop dragging
             selection.start_dragging();
 
diff --git a/src/slic3r/GUI/Gizmos/GLGizmoCut.cpp b/src/slic3r/GUI/Gizmos/GLGizmoCut.cpp
index 50d9697a5..65c1c775e 100644
--- a/src/slic3r/GUI/Gizmos/GLGizmoCut.cpp
+++ b/src/slic3r/GUI/Gizmos/GLGizmoCut.cpp
@@ -110,11 +110,15 @@ void GLGizmoCut::on_render()
     GLShaderProgram* shader = wxGetApp().get_shader("flat");
     if (shader != nullptr) {
         shader->start_using();
+        Vec3d diff = plane_center - m_old_center;
+        // Z changed when move with cut plane
+        // X and Y changed when move with cutted object
+        bool  is_changed = std::abs(diff.x()) > EPSILON ||
+                          std::abs(diff.y()) > EPSILON ||
+                          std::abs(diff.z()) > EPSILON;
+        m_old_center = plane_center;
 
-        const bool z_changed = std::abs(plane_center.z() - m_old_z) > EPSILON;
-        m_old_z = plane_center.z();
-
-        if (!m_plane.is_initialized() || z_changed) {
+        if (!m_plane.is_initialized() || is_changed) {
             m_plane.reset();
 
             GLModel::Geometry init_data;
@@ -159,7 +163,7 @@ void GLGizmoCut::on_render()
 
         glsafe(::glLineWidth(m_hover_id != -1 ? 2.0f : 1.5f));
 #if ENABLE_GLBEGIN_GLEND_REMOVAL
-        if (!m_grabber_connection.is_initialized() || z_changed) {
+        if (!m_grabber_connection.is_initialized() || is_changed) {
             m_grabber_connection.reset();
 
             GLModel::Geometry init_data;
@@ -334,6 +338,8 @@ BoundingBoxf3 GLGizmoCut::bounding_box() const
     BoundingBoxf3 ret;
     const Selection& selection = m_parent.get_selection();
     const Selection::IndicesList& idxs = selection.get_volume_idxs();
+    return selection.get_bounding_box();
+
     for (unsigned int i : idxs) {
         const GLVolume* volume = selection.get_volume(i);
         if (!volume->is_modifier)
diff --git a/src/slic3r/GUI/Gizmos/GLGizmoCut.hpp b/src/slic3r/GUI/Gizmos/GLGizmoCut.hpp
index 5d3fd5860..feb23bf45 100644
--- a/src/slic3r/GUI/Gizmos/GLGizmoCut.hpp
+++ b/src/slic3r/GUI/Gizmos/GLGizmoCut.hpp
@@ -26,7 +26,7 @@ class GLGizmoCut : public GLGizmoBase
 #if ENABLE_GLBEGIN_GLEND_REMOVAL
     GLModel m_plane;
     GLModel m_grabber_connection;
-    float m_old_z{ 0.0f };
+    Vec3d   m_old_center;
 #endif // ENABLE_GLBEGIN_GLEND_REMOVAL
 
     struct CutContours