From cafe9f88040c041d6c02517ed9e7b0277ab6b6ab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20=C5=A0toura=C4=8D?= <525032@mail.muni.cz>
Date: Sun, 19 May 2024 12:54:51 +0200
Subject: [PATCH] add resets to manipulateModule

---
 src/edit/include/edit/editor.hpp | 11 ++++++++---
 src/edit/src/editor.cpp          |  5 +++++
 src/gui/src/ui.cpp               | 18 ++++++++----------
 3 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/src/edit/include/edit/editor.hpp b/src/edit/include/edit/editor.hpp
index 77b741c..97625d4 100644
--- a/src/edit/include/edit/editor.hpp
+++ b/src/edit/include/edit/editor.hpp
@@ -91,6 +91,7 @@ public:
     bool &is_moving_module;
     glm::vec3 &module_position;
     bool &is_rotating_module;
+    bool &is_resetting_module_rotation;
     int &coordinate_space_selection;
     glm::vec3 &module_rotation_angles;
     bool &is_rotating_component;
@@ -109,8 +110,9 @@ public:
            bool &_is_connecting_modules, bool &_can_connect_modules,
            rofi::cardinal &_selected_cardinality, std::pair<connector_ptr, connector_ptr> &_selected_connectors, std::pair<module_ptr, module_ptr> &_selected_modules_connect, 
            bool &_connector_selected, bool &_voxel_selected, 
-           bool &_is_moving_module, glm::vec3 &_module_position, int &_coordinate_space_selection, bool &_is_rotating_module, glm::vec3 &_module_rotation_angles, 
-           bool &_is_rotating_component, float &_component_rotation_angle, bool &_is_creating_pad, std::size_t &_selected_pad_width, std::size_t &_selected_pad_height,
+           bool &_is_moving_module, glm::vec3 &_module_position, int &_coordinate_space_selection, bool &_is_rotating_module, bool &_is_resetting_module_rotation,
+           glm::vec3 &_module_rotation_angles, bool &_is_rotating_component, float &_component_rotation_angle, 
+           bool &_is_creating_pad, std::size_t &_selected_pad_width, std::size_t &_selected_pad_height,
            std::string &_error_msg, double &_error_start_second, bool &_primary_axis)
     :
     editor_phase{_editor_phase},
@@ -136,6 +138,7 @@ public:
     module_position{_module_position},
     coordinate_space_selection{_coordinate_space_selection},
     is_rotating_module{_is_rotating_module},
+    is_resetting_module_rotation{_is_resetting_module_rotation},
     is_rotating_component{_is_rotating_component},
     component_rotation_angle{_component_rotation_angle},
     module_rotation_angles{_module_rotation_angles},
@@ -171,6 +174,7 @@ class Editor
     bool is_disconnecting_modules = false;
     bool is_moving_module = false;
     bool is_rotating_module = false;
+    bool is_resetting_module_rotation = false;
     int coordinate_space_selection = 0;
     bool is_rotating_component = false;
 
@@ -195,7 +199,8 @@ class Editor
                        chosen_component, chosen_con_type, allowed_components, modules, selected_module, is_connecting_modules, can_connect_modules, 
                        selected_cardinality, selected_connectors, selected_modules_connect, 
                        connector_selected, voxel_selected, is_moving_module, module_position, coordinate_space_selection, 
-                       is_rotating_module, module_rotation_angles, is_rotating_component, component_rotation_angle, is_creating_pad, selected_pad_width, selected_pad_height,
+                       is_rotating_module, is_resetting_module_rotation, module_rotation_angles, is_rotating_component, component_rotation_angle, 
+                       is_creating_pad, selected_pad_width, selected_pad_height,
                        error_msg, error_start_second, primary_axis};
 
     node_ptr hover_nearest_node = nullptr;
diff --git a/src/edit/src/editor.cpp b/src/edit/src/editor.cpp
index e06ab20..41c7970 100644
--- a/src/edit/src/editor.cpp
+++ b/src/edit/src/editor.cpp
@@ -2700,6 +2700,11 @@ void Editor::teleportModulesByConnectors(const connector_ptr source_connector, c
 
 void Editor::rotateModule(module_ptr selected_module, glm::vec3 &rotation_angles)
 {
+    if (is_resetting_module_rotation)
+    {
+        selected_module->getNode()->setRotationQuat(glm::quat(1.0f, 0.0f, 0.0f, 0.0f));
+        return;
+    }
     const auto rotation_axes = coordinate_space_selection == 0 
                          ? std::vector<glm::vec3>{ selected_module->getNode()->getRotationAxisXLocal(), 
                                                    selected_module->getNode()->getRotationAxisYLocal(), 
diff --git a/src/gui/src/ui.cpp b/src/gui/src/ui.cpp
index 6e1be43..f34f6fa 100644
--- a/src/gui/src/ui.cpp
+++ b/src/gui/src/ui.cpp
@@ -687,6 +687,9 @@ void module_movement_ui(const osi::Window &window, edit::UIData &data)
         if (ImGui::InputFloat(input_labels[i], &position[i], data.module_translation_step, 1.0f, "%.3f"))
             continue;
 
+    if (ImGui::Button("Reset##ZeroModulePos"))
+        position = glm::vec3(0.0f);
+
     ImGui::End();
 
     data.module_position = convert_to_editor_coordinates(position - prev_position);
@@ -737,14 +740,13 @@ void module_rotation_ui(const osi::Window &window, edit::UIData &data)
     else
         module_rotation_manual(data, euler_angles);
 
-    ImGui::End();
+    data.is_resetting_module_rotation = ImGui::Button("Reset##ZeroModuleRotation");
 
-    // DragFloat still changes value when step is set to 0
-    if (data.module_rotation_step == 0)
-            euler_angles = prev_euler_angles;
+    ImGui::End();
 
     data.module_rotation_angles = convert_to_editor_coordinates(euler_angles - prev_euler_angles);
-    data.is_rotating_module = data.module_rotation_angles != glm::vec3(0.0f, 0.0f, 0.0f);
+    data.is_rotating_module = data.is_resetting_module_rotation 
+                              || (data.module_rotation_step != 0.0f && data.module_rotation_angles != glm::vec3(0.0f, 0.0f, 0.0f));
 }
 
 void module_rotation_drag(edit::UIData &data, glm::vec3 &euler_angles)
@@ -834,12 +836,8 @@ void component_rotation_ui(const osi::Window &window, edit::UIData &data)
 
     ImGui::End();
 
-    // DragFloat still changes value when step is set to 0
-    if (data.component_rotation_step == 0)
-        current_rotation = prev_rotation;
-
     data.component_rotation_angle = component_info == "Body A" ? prev_rotation - current_rotation : current_rotation - prev_rotation;
-    data.is_rotating_component = data.component_rotation_angle != 0;
+    data.is_rotating_component = data.component_rotation_step != 0.0f && data.component_rotation_angle != 0;
 }
 
 void save_module_ui(const osi::Window &window, edit::UIData &data)
-- 
GitLab