From b3cf09aa1b23ee009aa7fb362e4c75ca14ed378c 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 21:20:02 +0200
Subject: [PATCH] fix modules with same name

---
 src/edit/src/editor.cpp | 10 ++++++++++
 src/gui/src/ui.cpp      |  3 ++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/edit/src/editor.cpp b/src/edit/src/editor.cpp
index 5f5078e..111990f 100644
--- a/src/edit/src/editor.cpp
+++ b/src/edit/src/editor.cpp
@@ -274,6 +274,8 @@ void Editor::enterPhaseOne()
         scene->manageCameraAxes(false);
         removeRofiWorldNodeTreeFromScene(rofiworld->getNodeTree());
         removeModuleFromScene(selected_module);
+        removeModuleFromScene(selected_add_module);
+        selected_add_module = nullptr;
         voxel_graph = nullptr;
     }
 
@@ -1268,6 +1270,14 @@ void Editor::addToModules(std::string name, std::string type)
     linkImportVoxelGraphToNodes(new_module->getParts());
     setModuleNodeHierarchy(new_module->getParts(), type);
     setRoFIMesh(new_module->getParts());
+
+    if (const auto it = std::find_if(modules.begin(), modules.end(), 
+                                     [&](const module_ptr module) { return module->getName() == name; } );
+        it != modules.end())
+    {
+        modules.erase(it);
+    }
+
     modules.emplace_back(new_module);
 }
 
diff --git a/src/gui/src/ui.cpp b/src/gui/src/ui.cpp
index b1d20b7..a81d0fd 100644
--- a/src/gui/src/ui.cpp
+++ b/src/gui/src/ui.cpp
@@ -4,6 +4,7 @@
 #include <utils/math.hpp>
 
 #include <iostream>
+#include <string>
 
 void show_ui(const osi::Window &window, edit::UIData &data, const double &current_second)
 {
@@ -524,7 +525,7 @@ void module_selection_ui(const osi::Window &window, edit::UIData &data)
         {
             auto & [name, type] = names_types[n];
             bool is_selected = (data.add_module_list_index == n);
-            const auto label_string = name + " (" + type + ")";
+            const auto label_string = name + " (" + type + ")" + "##ModuleSelectionItem" + std::to_string(n);
             const char* label = label_string.c_str();
             if (ImGui::Selectable(label, is_selected))
             {
-- 
GitLab