From 92c5899fa15a2996b7e9e0bde345e3dbf4be68e5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20=C5=A0toura=C4=8D?= <525032@mail.muni.cz>
Date: Tue, 14 May 2024 13:53:42 +0200
Subject: [PATCH] add ui notice when invalid connect orientation

---
 src/edit/include/edit/editor.hpp | 12 +++++++++---
 src/edit/src/editor.cpp          | 19 ++++++++++++++++++-
 src/gui/src/ui.cpp               | 17 ++++++++++++-----
 3 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/src/edit/include/edit/editor.hpp b/src/edit/include/edit/editor.hpp
index f4dd74c..4e322de 100644
--- a/src/edit/include/edit/editor.hpp
+++ b/src/edit/include/edit/editor.hpp
@@ -82,6 +82,7 @@ public:
     bool &primary_axis;
 
     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;
@@ -102,7 +103,8 @@ public:
 
     UIData(phase &_editor_phase, mode_I &_editor_mode_I, mode_II &_editor_mode_II, build_mode_I &_editor_build_mode_I, build_mode_II &_editor_build_mode_II, 
            state &_control_state, bool &_ui_visible, rofi::component &_chosen_component, rofi::connector_type &_chosen_con_type, 
-           std::vector<rofi::component> &_allowed_components, std::vector<module_ptr> &_modules, module_ptr &_selected_module, bool &_is_connecting_modules,
+           std::vector<rofi::component> &_allowed_components, std::vector<module_ptr> &_modules, module_ptr &_selected_module, 
+           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, 
@@ -122,6 +124,7 @@ public:
     modules{_modules},
     selected_module{_selected_module},
     is_connecting_modules{_is_connecting_modules},
+    can_connect_modules{_can_connect_modules},
     selected_cardinality{_selected_cardinality},
     selected_connectors{_selected_connectors},
     selected_modules_connect{_selected_modules_connect},
@@ -187,8 +190,9 @@ class Editor
     char module_type_buffer[256] = {0};
 
     UIData ui = UIData{editor_phase, editor_mode_I, editor_mode_II, editor_build_mode_I, editor_build_mode_II, control_state, ui_visible, 
-                       chosen_component, chosen_con_type, allowed_components, modules, selected_module, is_connecting_modules, selected_cardinality, selected_connectors,
-                       selected_modules_connect, connector_selected, voxel_selected, is_moving_module, module_position, coordinate_space_selection, 
+                       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,
                        error_msg, error_start_second, primary_axis};
 
@@ -228,6 +232,7 @@ class Editor
     std::pair<glm::vec3, node_ptr> first_connector_highlights = {glm::vec3(0.0f), nullptr};
     std::pair<glm::vec3, node_ptr> second_connector_highlights = {glm::vec3(0.0f), nullptr};
     std::pair<module_ptr, module_ptr> selected_modules_connect = { nullptr, nullptr };
+    bool can_connect_modules = true;
 
     std::vector<objectbase_ptr> selected_space_joints;
 
@@ -489,6 +494,7 @@ private:
 
     /* CONNECT MODULE ACTION HELPERS */
     void updateSelectedConnectors();
+    void updateConnectionHint();
     bool checkSelectedConnectorsValid();
 
     /* CONTROL PHASE II BUILD */
diff --git a/src/edit/src/editor.cpp b/src/edit/src/editor.cpp
index b9a2a58..c5901c3 100644
--- a/src/edit/src/editor.cpp
+++ b/src/edit/src/editor.cpp
@@ -2145,6 +2145,7 @@ void Editor::doConnectModuleAction()
     // selectObject();
     selectModule();
     updateSelectedConnectors();
+    updateConnectionHint();
     connectModule();
     detectCollision(is_connecting_modules || is_disconnecting_modules, rofiworld);
 }
@@ -2343,6 +2344,15 @@ void Editor::updateSelectedConnectors()
     scene->manageSecondaryHighlights({first_connector_highlights});
 }
 
+void Editor::updateConnectionHint()
+{
+    if (can_connect_modules)
+        return;
+
+    if (timer.passed_seconds() - error_start_second > 1)
+        can_connect_modules = true;
+}
+
 bool Editor::checkSelectedConnectorsValid()
 {
     auto selection = scene->getSelection();
@@ -2524,7 +2534,7 @@ void Editor::connectModule()
         disconnectModule();
     }
     // Connect
-    else if (checkConnectModuleValid(selected_connectors.first, selected_connectors.second))
+    else if (can_connect_modules = checkConnectModuleValid(selected_connectors.first, selected_connectors.second))
     {
         auto source_node = selected_connectors.first->getNode();
         rebuildRofiWorldNodeTree(rofiworld->getModuleWithNode(source_node), source_node, rofiworld);
@@ -2536,6 +2546,13 @@ void Editor::connectModule()
         setConnectorMesh(selected_connectors.first);
         setConnectorMesh(selected_connectors.second);
     }
+    
+    if (!can_connect_modules)
+    {
+        setError("Invalid mutual orientation, cannot connect modules.");
+        is_connecting_modules = false;
+        return;
+    }
 
     selected_connectors = {nullptr, nullptr};
     selected_modules_connect = {nullptr, nullptr};
diff --git a/src/gui/src/ui.cpp b/src/gui/src/ui.cpp
index dc95d49..a3dbe79 100644
--- a/src/gui/src/ui.cpp
+++ b/src/gui/src/ui.cpp
@@ -642,11 +642,18 @@ void module_connection_ui(const osi::Window &window, edit::UIData &data)
         ImGui::RadioButton("West", &selection, 3);
         data.selected_cardinality = selection == 0 ? North : selection == 1 ? East : selection == 2 ? South : West;
         
-        ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.0f, 0.0f, 0.7f, 1.0f));
-        data.is_connecting_modules = ImGui::Button("Connect");
-        if (ImGui::IsItemHovered())
-            ImGui::SetTooltip("Connect (M)");
-        ImGui::PopStyleColor();
+        if (!data.can_connect_modules)
+        {
+            ImGui::Text("Invalid Orientation");
+        }
+        else
+        {
+            ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.0f, 0.0f, 0.7f, 1.0f));
+            data.is_connecting_modules = ImGui::Button("Connect");
+            if (ImGui::IsItemHovered())
+                ImGui::SetTooltip("Connect (M)");
+            ImGui::PopStyleColor();
+        }
     }
 
 
-- 
GitLab