From 439dae53faef2b6a3679e6b587aaca9024aea738 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20=C5=A0toura=C4=8D?= <525032@mail.muni.cz>
Date: Mon, 20 May 2024 13:10:29 +0200
Subject: [PATCH] add module id to ui and change module id ui

---
 src/edit/include/edit/editor.hpp    |  9 +++--
 src/edit/src/editor.cpp             |  6 ++++
 src/gui/include/gui/ui.hpp          |  2 ++
 src/gui/src/ui.cpp                  | 53 +++++++++++++++++++++++++++++
 src/rofi/include/rofi/rofiworld.hpp |  4 +++
 src/rofi/src/rofiworld.cpp          | 10 ++++++
 6 files changed, 81 insertions(+), 3 deletions(-)

diff --git a/src/edit/include/edit/editor.hpp b/src/edit/include/edit/editor.hpp
index 8ba0315..e49af0c 100644
--- a/src/edit/include/edit/editor.hpp
+++ b/src/edit/include/edit/editor.hpp
@@ -41,6 +41,7 @@ class UIData
 {
 public:
     double current_second = 0;
+    double ui_error_start_second = 0;
     bool help_controls = false;
     bool face_highlight = false;
     bool vocal_error = false;
@@ -80,6 +81,7 @@ public:
     std::vector<module_ptr> &modules;
     module_ptr &selected_add_module;
     module_ptr &selected_module;
+    rofiworld_ptr &rofiworld;
     bool &voxel_selected;
     bool &connector_selected;
     bool &primary_axis;
@@ -108,7 +110,7 @@ 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_add_module, module_ptr &_selected_module, 
-           bool &_is_connecting_modules, bool &_can_connect_modules,
+           rofiworld_ptr &_rofiworld, 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, bool &_is_resetting_module_rotation,
@@ -129,6 +131,7 @@ public:
     modules{_modules},
     selected_add_module{_selected_add_module},
     selected_module{_selected_module},
+    rofiworld{_rofiworld},
     is_connecting_modules{_is_connecting_modules},
     can_connect_modules{_can_connect_modules},
     selected_cardinality{_selected_cardinality},
@@ -198,8 +201,8 @@ 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_add_module, selected_module, is_connecting_modules, can_connect_modules, 
-                       selected_cardinality, selected_connectors, selected_modules_connect, 
+                       chosen_component, chosen_con_type, allowed_components, modules, selected_add_module, selected_module, rofiworld, 
+                       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, is_resetting_module_rotation, module_rotation_angles, is_rotating_component, component_rotation_angle, 
                        is_creating_pad, selected_pad_width, selected_pad_height,
diff --git a/src/edit/src/editor.cpp b/src/edit/src/editor.cpp
index 111990f..a15fc86 100644
--- a/src/edit/src/editor.cpp
+++ b/src/edit/src/editor.cpp
@@ -814,6 +814,7 @@ void Editor::controlSpectate()
         scene->toggleVisibility(scene->getHighlight(), ui_visible);
         scene->toggleVisibility(scene->getGrid(), ui_visible);
         scene->toggleVisibility(scene->getObjectAxes(), ui_visible);
+        scene->toggleVisibility(scene->getCameraAxes(), ui_visible);
     }
 }
 
@@ -2391,6 +2392,7 @@ void Editor::updateSelectedConnectors()
 
     if (!checkSelectedConnectorsValid())
     {
+        selected_module = nullptr;
         scene->manageSelection(nullptr);
         scene->manageHighlight(glm::vec3(0.0f), nullptr);
         return;
@@ -2404,6 +2406,7 @@ void Editor::updateSelectedConnectors()
         selected_modules_connect = { nullptr, nullptr };
         first_connector_highlights = {glm::vec3(0.0f), nullptr};
         second_connector_highlights = {glm::vec3(0.0f), nullptr};
+        selected_module = nullptr;
         scene->manageSelection(nullptr);
         scene->manageHighlight(glm::vec3(0.0f), nullptr);
     }
@@ -2420,6 +2423,7 @@ void Editor::updateSelectedConnectors()
         selected_connectors.second = nullptr;
         selected_modules_connect.second = nullptr;
         second_connector_highlights = {glm::vec3(0.0f), nullptr};
+        selected_module = nullptr;
         scene->manageSelection(nullptr);
         scene->manageHighlight(glm::vec3(0.0f), nullptr);
     }
@@ -2472,6 +2476,7 @@ void Editor::updateSelectedComponent()
 
     if (!checkSelectedComponentValid())
     {
+        selected_module = nullptr;
         scene->manageSelection(nullptr);
         scene->manageHighlight(glm::vec3(0.0f), nullptr);
     }
@@ -2648,6 +2653,7 @@ void Editor::connectModule()
     selected_modules_connect = {nullptr, nullptr};
     first_connector_highlights = {glm::vec3(0.0f), nullptr};
     second_connector_highlights = {glm::vec3(0.0f), nullptr};
+    selected_module = nullptr;
     scene->manageSelection(nullptr);
     scene->manageHighlight(glm::vec3(0.0f), nullptr);
     scene->manageSecondarySelection({});
diff --git a/src/gui/include/gui/ui.hpp b/src/gui/include/gui/ui.hpp
index c07679c..193d172 100644
--- a/src/gui/include/gui/ui.hpp
+++ b/src/gui/include/gui/ui.hpp
@@ -44,6 +44,8 @@ void build_toolbar_ui(const osi::Window &window, edit::UIData &data);
 void build_toolbar_buttons(edit::UIData &data);
 void set_build_toolbar_button_colors(edit::UIData &data);
 void create_pad_ui(const osi::Window &window, edit::UIData &data);
+void module_id_ui(const osi::Window &window, edit::UIData &data);
+void module_change_id_ui(const osi::Window &window, edit::UIData &data);
 void module_selection_ui(const osi::Window &window, edit::UIData &data);
 void module_connection_ui(const osi::Window &window, edit::UIData &data);
 void module_movement_ui(const osi::Window &window, edit::UIData &data);
diff --git a/src/gui/src/ui.cpp b/src/gui/src/ui.cpp
index a81d0fd..fd8eb6f 100644
--- a/src/gui/src/ui.cpp
+++ b/src/gui/src/ui.cpp
@@ -246,6 +246,8 @@ void mode_I_build_ui(const osi::Window &window, edit::UIData &data)
 
 void mode_II_build_ui(const osi::Window &window, edit::UIData &data)
 {
+    module_id_ui(window, data);
+    module_change_id_ui(window, data);
     mode_II_build_selection_ui(window, data);
     module_selection_ui(window, data);
     module_connection_ui(window, data);
@@ -501,6 +503,57 @@ void create_pad_ui(const osi::Window &window, edit::UIData &data)
     data.selected_pad_height = height > 0 ? static_cast<std::size_t>(height) : 1;                        
 }
 
+void module_id_ui(const osi::Window &window, edit::UIData &data)
+{
+    if (!data.selected_module)
+        return;
+
+    float size_x = static_cast<float>(window.size().x);
+    float size_y = static_cast<float>(window.size().y);
+    ImGui::SetNextWindowPos(ImVec2(0, size_y), ImGuiCond_Always, ImVec2(0.0f, 1.0f));
+
+    ImGui::Begin("##ModuleID", NULL, ImGuiWindowFlags_NoDecoration 
+                                     | ImGuiWindowFlags_NoFocusOnAppearing 
+                                     | ImGuiWindowFlags_NoBackground 
+                                     | ImGuiWindowFlags_NoMove 
+                                     | ImGuiWindowFlags_AlwaysAutoResize);
+    ImGui::Text(std::string("Module ID: " + std::to_string(data.rofiworld->getModuleID(data.selected_module))).c_str());
+    ImGui::End();
+}
+
+void module_change_id_ui(const osi::Window &window, edit::UIData &data)
+{
+    if (data.editor_build_mode_II != edit::build_mode_II::add_module || !data.selected_module)
+        return;
+
+    float size_x = static_cast<float>(window.size().x);
+    float size_y = static_cast<float>(window.size().y);
+    ImGui::SetNextWindowPos(ImVec2(0, size_y - 40.0f), ImGuiCond_Always, ImVec2(0.0f, 1.0f));
+
+    auto ui_id = data.rofiworld->getModuleID(data.selected_module);
+
+    ImGui::Begin("Change ID##ChangeModuleID", NULL, ImGuiWindowFlags_NoResize 
+                                                    | ImGuiWindowFlags_NoMove 
+                                                    | ImGuiWindowFlags_NoCollapse 
+                                                    | ImGuiWindowFlags_AlwaysAutoResize);
+
+    bool input_change = ImGui::InputScalar("##ChangeModuleIDInput", ImGuiDataType_U64, &ui_id, NULL, NULL, NULL, ImGuiInputTextFlags_EnterReturnsTrue);
+    if(input_change)
+    {
+        if (data.rofiworld->containsID(ui_id))
+            data.ui_error_start_second = data.current_second;
+        else
+            data.rofiworld->changeModuleID(data.selected_module, ui_id);
+    }
+
+    if (data.current_second - data.ui_error_start_second < 2)
+        ImGui::Text("ID already used.");
+    else
+        ImGui::NewLine();
+
+    ImGui::End();
+}
+
 void module_selection_ui(const osi::Window &window, edit::UIData &data)
 {
     if (data.editor_build_mode_II != edit::build_mode_II::add_module)
diff --git a/src/rofi/include/rofi/rofiworld.hpp b/src/rofi/include/rofi/rofiworld.hpp
index 5fd8f6d..8d80e05 100644
--- a/src/rofi/include/rofi/rofiworld.hpp
+++ b/src/rofi/include/rofi/rofiworld.hpp
@@ -34,7 +34,11 @@ public:
 
     const std::vector<node_ptr> &getNodeTree() const { return node_tree; }
     voxel_graph_ptr getVoxelGraph() const { return voxel_graph; }
+
     const std::map<module_ptr, uint64_t> &getModulesMap() const { return modules_map; }
+    const uint64_t getModuleID(const module_ptr module) const { return modules_map.at(module); }
+    bool containsID(uint64_t id) const { return ids_set.contains(id); }
+    void changeModuleID(module_ptr module, uint64_t new_id);
     /* Returns module with given node that is either directly associated with the node or nearest parent node.
      * If no such module is found, returns nullptr. */
     module_ptr getModuleWithNode(const node_ptr node) const;
diff --git a/src/rofi/src/rofiworld.cpp b/src/rofi/src/rofiworld.cpp
index 25e0208..0cdf577 100644
--- a/src/rofi/src/rofiworld.cpp
+++ b/src/rofi/src/rofiworld.cpp
@@ -9,6 +9,16 @@
 namespace rofi
 {
 
+void RofiWorld::changeModuleID(module_ptr module, uint64_t new_id)
+{
+    ASSUMPTION(modules_map.contains(module));
+
+    const auto old_id = modules_map[module];
+    modules_map[module] = new_id;
+    ids_set.erase(old_id);
+    ids_set.emplace(new_id);
+}
+
 module_ptr RofiWorld::getModuleWithNode(const node_ptr node) const
 {
     auto curr_node = node;
-- 
GitLab