@@ -9,15 +9,33 @@ namespace wde::scene {
99 logger::log (LogLevel::DEBUG, LogChannel::SCENE) << " Removing deleted game objects." << logger::endl;
1010 {
1111 WDE_PROFILE_SCOPE (" wde::scene::WdeSceneInstance::tick::deleteGameObjects" );
12- for (auto & go : _gameObjectsToDelete) {
13- /* _selectedGameObjectID = -1;
14- * TODO
12+
13+ if (!_gameObjectsToDelete.empty ()) {
14+ // Remove selected and active camera
15+ for (GameObject* go : _gameObjectsToDelete) {
16+ if (_selectedGameObject == go)
17+ _selectedGameObject = nullptr ;
18+ if (_activeCamera == go)
19+ _activeCamera = nullptr ;
20+ }
1521
1622 // Remove from static list
17- if (go->isStatic()) {
18- for (auto& go : _gameObjectsStatic)
19- _gameObjectsStatic.r
20- }*/
23+ _gameObjectsStatic.erase (std::remove_if (_gameObjectsStatic.begin (), _gameObjectsStatic.end (), [this ](const auto &x) {
24+ return std::find (_gameObjectsToDelete.begin (), _gameObjectsToDelete.end (), x.get ()) != _gameObjectsToDelete.end ();
25+ }), _gameObjectsStatic.end ());
26+
27+ // Remove from dynamic list
28+ _gameObjectsDynamic.erase (std::remove_if (_gameObjectsDynamic.begin (), _gameObjectsDynamic.end (), [this ](const auto &x) {
29+ return std::find (_gameObjectsToDelete.begin (), _gameObjectsToDelete.end (), x.get ()) != _gameObjectsToDelete.end ();
30+ }), _gameObjectsDynamic.end ());
31+
32+ // Remove from game objects
33+ _gameObjects.erase (std::remove_if (_gameObjects.begin (), _gameObjects.end (), [this ](const auto &x) {
34+ return std::find (_gameObjectsToDelete.begin (), _gameObjectsToDelete.end (), x.get ()) != _gameObjectsToDelete.end ();
35+ }), _gameObjects.end ());
36+
37+ // Clear game objects to delete
38+ _gameObjectsToDelete.clear ();
2139 }
2240 }
2341
@@ -38,6 +56,11 @@ namespace wde::scene {
3856 }
3957
4058 void WdeSceneInstance::cleanUpInstance () {
59+ // Remove references
60+ _selectedGameObject = nullptr ;
61+ _activeCamera = nullptr ;
62+
63+ // Remove game objects
4164 _gameObjectsDynamic.clear ();
4265 _gameObjectsStatic.clear ();
4366 _gameObjects.clear ();
@@ -88,25 +111,24 @@ namespace wde::scene {
88111
89112 // Scene game objects
90113 ImGuiTableFlags flags = ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_NoClip;
91- static int selected = - 1 ;
114+ GameObject* oldSelected = _selectedGameObject ;
92115 if (ImGui::BeginTable (" Game Objects List" , 3 , flags)) {
93116 // Draw game objects list
94117 for (auto & go : _gameObjects) {
95118 if (go->transform ->getParent () == nullptr ) {
96119 ImGui::TableNextRow ();
97- drawGUIForGo (go, &selected );
120+ drawGUIForGo (go, _selectedGameObject );
98121 }
99122 }
100123 ImGui::EndTable ();
101124 }
102125
103126 // Selected game object changed
104- if (selected != _selectedGameObjectID) {
105- if (_selectedGameObjectID >= 0 )
106- _gameObjects[_selectedGameObjectID]->setSelected (false );
107- _selectedGameObjectID = selected;
108- if (_selectedGameObjectID >= 0 )
109- _gameObjects[_selectedGameObjectID]->setSelected (true );
127+ if (oldSelected != _selectedGameObject) {
128+ if (oldSelected != nullptr )
129+ oldSelected->setSelected (false );
130+ if (_selectedGameObject != nullptr )
131+ _selectedGameObject->setSelected (true );
110132 }
111133
112134 ImGui::EndChild ();
@@ -120,15 +142,15 @@ namespace wde::scene {
120142 gui::GUIRenderer::popWindowTabStyle ();
121143 ImGui::PushFont (ImGui::GetIO ().FontDefault );
122144 ImGui::Dummy (ImVec2 (0 .0f , 0 .15f ));
123- if (_selectedGameObjectID != - 1 )
124- _gameObjects. at (_selectedGameObjectID) ->drawGUI ();
145+ if (_selectedGameObject != nullptr )
146+ _selectedGameObject ->drawGUI ();
125147 ImGui::End ();
126148 ImGui::PopFont ();
127149 }
128150#endif
129151 }
130152
131- void WdeSceneInstance::drawGUIForGo (const std::shared_ptr<GameObject> &go, int * selected) const {
153+ void WdeSceneInstance::drawGUIForGo (const std::shared_ptr<GameObject> &go, GameObject*& selected) const {
132154#ifdef WDE_GUI_ENABLED
133155 std::string typeName;
134156 if (go->getModule <MeshRendererModule>())
@@ -173,8 +195,8 @@ namespace wde::scene {
173195 // Draw tree node
174196 ImGui::SameLine ();
175197 ImGui::PushID (static_cast <int >(go->getID ()) + 216846353 );
176- if (ImGui::Selectable (buf3, * selected == go-> getID (), ImGuiSelectableFlags_SpanAllColumns))
177- * selected = static_cast < int >(go-> getID () );
198+ if (ImGui::Selectable (buf3, selected == go. get (), ImGuiSelectableFlags_SpanAllColumns))
199+ selected = go. get ( );
178200 ImGui::PopID ();
179201 ImGui::PopFont ();
180202
@@ -210,8 +232,8 @@ namespace wde::scene {
210232
211233 ImGui::SameLine ();
212234 ImGui::PushID (static_cast <int >(go->getID ()) + 216846354 );
213- if (ImGui::Selectable (buf2, * selected == go-> getID (), ImGuiSelectableFlags_SpanAllColumns))
214- * selected = static_cast < int >(go-> getID () );
235+ if (ImGui::Selectable (buf2, selected == go. get (), ImGuiSelectableFlags_SpanAllColumns))
236+ selected = go. get ( );
215237 ImGui::PopID ();
216238 ImGui::PopFont ();
217239
0 commit comments