@@ -9,15 +9,33 @@ namespace wde::scene {
9
9
logger::log (LogLevel::DEBUG, LogChannel::SCENE) << " Removing deleted game objects." << logger::endl;
10
10
{
11
11
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
+ }
15
21
16
22
// 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 ();
21
39
}
22
40
}
23
41
@@ -38,6 +56,11 @@ namespace wde::scene {
38
56
}
39
57
40
58
void WdeSceneInstance::cleanUpInstance () {
59
+ // Remove references
60
+ _selectedGameObject = nullptr ;
61
+ _activeCamera = nullptr ;
62
+
63
+ // Remove game objects
41
64
_gameObjectsDynamic.clear ();
42
65
_gameObjectsStatic.clear ();
43
66
_gameObjects.clear ();
@@ -88,25 +111,24 @@ namespace wde::scene {
88
111
89
112
// Scene game objects
90
113
ImGuiTableFlags flags = ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_NoClip;
91
- static int selected = - 1 ;
114
+ GameObject* oldSelected = _selectedGameObject ;
92
115
if (ImGui::BeginTable (" Game Objects List" , 3 , flags)) {
93
116
// Draw game objects list
94
117
for (auto & go : _gameObjects) {
95
118
if (go->transform ->getParent () == nullptr ) {
96
119
ImGui::TableNextRow ();
97
- drawGUIForGo (go, &selected );
120
+ drawGUIForGo (go, _selectedGameObject );
98
121
}
99
122
}
100
123
ImGui::EndTable ();
101
124
}
102
125
103
126
// 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 );
110
132
}
111
133
112
134
ImGui::EndChild ();
@@ -120,15 +142,15 @@ namespace wde::scene {
120
142
gui::GUIRenderer::popWindowTabStyle ();
121
143
ImGui::PushFont (ImGui::GetIO ().FontDefault );
122
144
ImGui::Dummy (ImVec2 (0 .0f , 0 .15f ));
123
- if (_selectedGameObjectID != - 1 )
124
- _gameObjects. at (_selectedGameObjectID) ->drawGUI ();
145
+ if (_selectedGameObject != nullptr )
146
+ _selectedGameObject ->drawGUI ();
125
147
ImGui::End ();
126
148
ImGui::PopFont ();
127
149
}
128
150
#endif
129
151
}
130
152
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 {
132
154
#ifdef WDE_GUI_ENABLED
133
155
std::string typeName;
134
156
if (go->getModule <MeshRendererModule>())
@@ -173,8 +195,8 @@ namespace wde::scene {
173
195
// Draw tree node
174
196
ImGui::SameLine ();
175
197
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 ( );
178
200
ImGui::PopID ();
179
201
ImGui::PopFont ();
180
202
@@ -210,8 +232,8 @@ namespace wde::scene {
210
232
211
233
ImGui::SameLine ();
212
234
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 ( );
215
237
ImGui::PopID ();
216
238
ImGui::PopFont ();
217
239
0 commit comments