Ember
Loading...
Searching...
No Matches
MainPanel.cpp
Go to the documentation of this file.
1#include "Panels/MainPanel.h"
2#include "Core/Node.h"
6#include <wx/artprov.h>
7#include <wx/textdlg.h>
8
9wxBEGIN_EVENT_TABLE(MainPanel, Panel) EVT_AUINOTEBOOK_PAGE_CHANGED(ID_SCENE_NOTEBOOK, MainPanel::OnTabChanged)
10 EVT_AUINOTEBOOK_PAGE_CLOSE(ID_SCENE_NOTEBOOK, MainPanel::OnTabClosed)
11 EVT_TOOL(ID_NEW_SCENE_BUTTON, MainPanel::OnNewSceneButton) wxEND_EVENT_TABLE()
12
13 MainPanel::MainPanel(wxWindow *parent)
14 : Panel(parent, "MainPanel"), m_activeSceneIndex(-1), m_nextSceneNumber(1) {
15 SetTitle("Scene");
16 SetPanelType("Scene");
17 SetBackgroundColour(wxColour(50, 50, 50)); // Dark like most editors
18 DoCreateLayout(); // Non-virtual: safe to call from constructor
19}
20
22 DoCreateLayout(); // Virtual dispatch works when called at runtime (e.g. from Panel::Initialize)
23}
24
26 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
27
28 // Create toolbar
30
31 // Create tabbed interface for multiple scenes with close buttons
32 // Now properly handling the close event without double-deletion
33 m_sceneNotebook = new wxAuiNotebook(this, ID_SCENE_NOTEBOOK, wxDefaultPosition, wxDefaultSize,
34 wxAUI_NB_DEFAULT_STYLE | wxAUI_NB_CLOSE_ON_ACTIVE_TAB);
35 m_sceneNotebook->SetBackgroundColour(wxColour(50, 50, 50));
36
37 // Set custom tab art to use accent color
38 m_sceneNotebook->SetArtProvider(new EmberForge::CustomTabArt());
39
40 // Add to main sizer
41 sizer->Add(m_toolbar, 0, wxEXPAND | wxALL, 2);
42 sizer->Add(m_sceneNotebook, 1, wxEXPAND | wxALL, 5);
43
44 SetSizer(sizer);
45}
46
48 DoCreateToolbar(); // Virtual dispatch works when called at runtime
49}
50
52 m_toolbar =
53 new wxToolBar(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL | wxTB_FLAT | wxTB_NODIVIDER);
54 m_toolbar->SetBackgroundColour(wxColour(60, 60, 60));
55
56 // Add new scene button
57 m_toolbar->AddTool(ID_NEW_SCENE_BUTTON, "New Scene", wxArtProvider::GetBitmap(wxART_NEW, wxART_TOOLBAR),
58 "Create a new empty behavior tree scene");
59
60 m_toolbar->Realize();
61}
62
63int MainPanel::AddScene(std::unique_ptr<IScene> scene) {
64 if (!scene)
65 return -1;
66
67 // Check scene limit from preferences
69 int maxScenes = prefs.GetMainPanelSettings().maxScenes;
70
71 if (maxScenes > 0 && static_cast<int>(m_scenes.size()) >= maxScenes) {
72 wxMessageBox(
73 wxString::Format("Maximum number of scenes (%d) reached.\nClose some scenes before creating new ones.",
74 maxScenes),
75 "Scene Limit Reached", wxOK | wxICON_WARNING);
76 return -1;
77 }
78
79 // Add the scene's panel to the notebook
80 int index = m_sceneNotebook->GetPageCount();
81 m_sceneNotebook->AddPage(scene->GetPanel(), scene->GetTitle(), true); // true = make active
82
83 // Store the scene
84 m_scenes.push_back(std::move(scene));
85
86 // If this is the first scene, make it active
87 if (m_scenes.size() == 1) {
89 m_scenes[0]->OnActivated();
90 } else {
91 // Update active scene index to the new scene
92 m_activeSceneIndex = index;
93 m_scenes[m_activeSceneIndex]->OnActivated();
94 }
95
96 return index;
97}
98
99bool MainPanel::RemoveScene(int index) {
100 if (index < 0 || index >= static_cast<int>(m_scenes.size())) {
101 return false;
102 }
103
104 // Check if scene has unsaved changes
105 if (m_scenes[index]->HasUnsavedChanges()) {
106 wxMessageDialog dialog(
107 this, wxString::Format("Scene '%s' has unsaved changes. Close anyway?", m_scenes[index]->GetTitle()),
108 "Unsaved Changes", wxYES_NO | wxICON_QUESTION);
109 if (dialog.ShowModal() != wxID_YES) {
110 return false;
111 }
112 }
113
114 // CRITICAL: Store scene before removal to ensure proper cleanup
115 std::unique_ptr<IScene> sceneToRemove = std::move(m_scenes[index]);
116
117 // Deactivate the scene if it's currently active
118 if (index == m_activeSceneIndex) {
119 sceneToRemove->OnDeactivated();
120 }
121
122 // IMPORTANT: Remove from scenes vector FIRST to prevent callback access during destruction
123 m_scenes.erase(m_scenes.begin() + index);
124
125 // Update active scene index BEFORE destroying UI to prevent accessing invalid indices
126 if (m_scenes.empty()) {
128 } else if (index == m_activeSceneIndex) {
129 // The active scene was removed, activate the next available scene
130 if (index >= static_cast<int>(m_scenes.size())) {
131 m_activeSceneIndex = static_cast<int>(m_scenes.size()) - 1;
132 } else {
133 m_activeSceneIndex = index;
134 }
135 if (m_activeSceneIndex >= 0) {
136 m_scenes[m_activeSceneIndex]->OnActivated();
137 }
138 } else if (index < m_activeSceneIndex) {
139 // Adjust active scene index since we removed a scene before it
141 }
142
143 // NOW remove from notebook (this destroys the UI and can trigger events)
144 // But scenes vector is already updated, so callbacks won't access invalid data
145 m_sceneNotebook->DeletePage(index);
146
147 // Scene will be automatically destroyed when sceneToRemove goes out of scope
148 // This ensures proper destructor cleanup of callbacks
149
150 return true;
151}
152
154 if (m_activeSceneIndex >= 0 && m_activeSceneIndex < static_cast<int>(m_scenes.size())) {
155 return m_scenes[m_activeSceneIndex].get();
156 }
157 return nullptr;
158}
159
160IScene *MainPanel::GetScene(int index) const {
161 if (index >= 0 && index < static_cast<int>(m_scenes.size())) {
162 return m_scenes[index].get();
163 }
164 return nullptr;
165}
166
168 if (index < 0 || index >= static_cast<int>(m_scenes.size())) {
169 return false;
170 }
171
172 if (index == m_activeSceneIndex) {
173 return true; // Already active
174 }
175
176 // Deactivate current scene
177 if (m_activeSceneIndex >= 0 && m_activeSceneIndex < static_cast<int>(m_scenes.size())) {
178 m_scenes[m_activeSceneIndex]->OnDeactivated();
179 }
180
181 // Set new active scene
182 m_activeSceneIndex = index;
183 m_sceneNotebook->SetSelection(index);
184 m_scenes[m_activeSceneIndex]->OnActivated();
185
186 return true;
187}
188
190 // Create a new empty behavior tree scene
191 wxString title = GenerateSceneTitle();
192 auto newScene = std::make_unique<BehaviorTreeScene>(m_sceneNotebook, title);
193
194 // Add the scene to the panel
195 int index = AddScene(std::move(newScene));
196
197 // Only log if scene was successfully added
198 if (index != -1) {
199 wxLogMessage("Created new scene: %s", title);
200 }
201
202 return index;
203}
204
206 wxString title;
207 bool titleExists = false;
208
209 do {
210 title = wxString::Format("Scene %d", m_nextSceneNumber);
211 titleExists = false;
212
213 // Check if title already exists
214 for (const auto &scene : m_scenes) {
215 if (scene->GetTitle() == title) {
216 titleExists = true;
217 break;
218 }
219 }
220
221 if (titleExists) {
222 const_cast<MainPanel *>(this)->m_nextSceneNumber++;
223 }
224 } while (titleExists);
225
226 const_cast<MainPanel *>(this)->m_nextSceneNumber++;
227 return title;
228}
229
230bool MainPanel::SceneTitleExists(const wxString &title) const {
231 for (const auto &scene : m_scenes) {
232 if (scene->GetTitle() == title) {
233 return true;
234 }
235 }
236 return false;
237}
238
239int MainPanel::CreateNewScene(const wxString &customTitle) {
240 // Create a new empty behavior tree scene with custom title
241 auto newScene = std::make_unique<BehaviorTreeScene>(m_sceneNotebook, customTitle);
242
243 // Add the scene to the panel
244 int index = AddScene(std::move(newScene));
245
246 // Only log if scene was successfully added
247 if (index != -1) {
248 wxLogMessage("Created new scene: %s", customTitle);
249 }
250
251 return index;
252}
253
254void MainPanel::UpdateActiveSceneTitle(const wxString &newTitle) {
255 if (m_activeSceneIndex < 0 || m_activeSceneIndex >= static_cast<int>(m_scenes.size())) {
256 return;
257 }
258
259 // Update the scene's internal title
260 auto *behaviorTreeScene = dynamic_cast<BehaviorTreeScene *>(m_scenes[m_activeSceneIndex].get());
261 if (behaviorTreeScene) {
262 behaviorTreeScene->SetTitle(newTitle);
263 }
264
265 // Update the notebook tab text
266 m_sceneNotebook->SetPageText(m_activeSceneIndex, newTitle);
267
268 wxLogMessage("Updated scene title to: %s", newTitle);
269}
270
271void MainPanel::OnTabChanged(wxAuiNotebookEvent &event) {
272 int newIndex = event.GetSelection();
273
274 // Deactivate current scene
275 if (m_activeSceneIndex >= 0 && m_activeSceneIndex < static_cast<int>(m_scenes.size())) {
276 m_scenes[m_activeSceneIndex]->OnDeactivated();
277 }
278
279 // Activate new scene
280 m_activeSceneIndex = newIndex;
281 IScene *newScene = nullptr;
282 if (m_activeSceneIndex >= 0 && m_activeSceneIndex < static_cast<int>(m_scenes.size())) {
283 m_scenes[m_activeSceneIndex]->OnActivated();
284 newScene = m_scenes[m_activeSceneIndex].get();
285 }
286
287 // Notify listeners about scene change
290 }
291
292 event.Skip();
293}
294
295void MainPanel::OnNewSceneButton(wxCommandEvent &event) {
296 // Show dialog to get scene name from user
297 wxTextEntryDialog dialog(this, "Enter a name for the new scene:", "New Scene", GenerateSceneTitle());
298
299 if (dialog.ShowModal() == wxID_OK) {
300 wxString sceneName = dialog.GetValue().Trim();
301
302 // Validate the scene name
303 if (sceneName.IsEmpty()) {
304 wxMessageBox("Scene name cannot be empty!", "Invalid Name", wxOK | wxICON_WARNING, this);
305 return;
306 }
307
308 // Check if name already exists
309 if (SceneTitleExists(sceneName)) {
310 wxMessageBox(
311 wxString::Format("A scene named '%s' already exists!\nPlease choose a different name.", sceneName),
312 "Duplicate Name", wxOK | wxICON_WARNING, this);
313 return;
314 }
315
316 // Create the scene with the custom name
317 CreateNewScene(sceneName);
318 }
319}
320
321void MainPanel::OnTabClosed(wxAuiNotebookEvent &event) {
322 int index = event.GetSelection();
323
324 // CRITICAL: Validate the index before proceeding
325 if (index < 0 || index >= static_cast<int>(m_scenes.size())) {
326 wxLogError("Invalid tab close index: %d (have %zu scenes)", index, m_scenes.size());
327 event.Veto();
328 return;
329 }
330
331 // Check close confirmation preference
333 auto closeConfirmMode = prefs.GetMainPanelSettings().closeConfirmation;
334
335 bool shouldAsk = false;
336 wxString message;
337
339 shouldAsk = true;
340 if (m_scenes[index]->HasUnsavedChanges()) {
341 message = wxString::Format("Scene '%s' has unsaved changes. Close anyway?", m_scenes[index]->GetTitle());
342 } else {
343 message = wxString::Format("Close scene '%s'?", m_scenes[index]->GetTitle());
344 }
346 if (m_scenes[index]->HasUnsavedChanges()) {
347 shouldAsk = true;
348 message = wxString::Format("Scene '%s' has unsaved changes. Close anyway?", m_scenes[index]->GetTitle());
349 }
350 }
351 // NeverAsk mode: shouldAsk remains false
352
353 if (shouldAsk) {
354 wxMessageDialog dialog(this, message, "Close Scene", wxYES_NO | wxICON_QUESTION);
355 if (dialog.ShowModal() != wxID_YES) {
356 event.Veto();
357 return;
358 }
359 }
360
361 // CRITICAL: Don't call DeletePage() - let wxWidgets handle UI deletion
362 // Store the scene before wxWidgets deletes the page
363 std::unique_ptr<IScene> sceneToCleanup = std::move(m_scenes[index]);
364
365 // Deactivate the scene before cleanup
366 if (index == m_activeSceneIndex) {
367 sceneToCleanup->OnDeactivated();
368 }
369
370 // Remove from our vector (but don't call DeletePage)
371 m_scenes.erase(m_scenes.begin() + index);
372
373 // Update active scene index
374 if (m_scenes.empty()) {
376 } else if (index == m_activeSceneIndex) {
377 if (index >= static_cast<int>(m_scenes.size())) {
378 m_activeSceneIndex = static_cast<int>(m_scenes.size()) - 1;
379 } else {
380 m_activeSceneIndex = index;
381 }
382 if (m_activeSceneIndex >= 0) {
383 m_scenes[m_activeSceneIndex]->OnActivated();
384 }
385 } else if (index < m_activeSceneIndex) {
387 }
388
389 // Scene cleanup happens automatically when sceneToCleanup is destroyed
390 // DON'T VETO - let wxWidgets handle the DeletePage() call
391}
392
393void MainPanel::ApplyCanvasBackgroundColor(const wxColour &color) {
394 // Apply to all existing scenes
395 for (auto &scene : m_scenes) {
396 if (scene) {
397 BehaviorTreeScene *btScene = dynamic_cast<BehaviorTreeScene *>(scene.get());
398 if (btScene) {
399 btScene->SetCanvasBackgroundColor(color);
400 }
401 }
402 }
403}
404
406 if (m_scenes.empty() || !m_sceneNotebook)
407 return;
408
409 int currentSelection = m_sceneNotebook->GetSelection();
410 int nextSelection = (currentSelection + 1) % m_sceneNotebook->GetPageCount();
411 m_sceneNotebook->SetSelection(nextSelection);
412}
413
415 if (m_scenes.empty() || !m_sceneNotebook)
416 return;
417
418 int currentSelection = m_sceneNotebook->GetSelection();
419 int pageCount = m_sceneNotebook->GetPageCount();
420 int prevSelection = (currentSelection - 1 + pageCount) % pageCount;
421 m_sceneNotebook->SetSelection(prevSelection);
422}
BehaviorTreeProjectDialog::OnProjectNameChanged BehaviorTreeProjectDialog::OnRemoveFiles wxEND_EVENT_TABLE() BehaviorTreeProjectDialog
MainPanel::OnTabChanged EVT_TOOL(ID_NEW_SCENE_BUTTON, MainPanel::OnNewSceneButton) wxEND_EVENT_TABLE() MainPanel
Definition MainPanel.cpp:11
wxBEGIN_EVENT_TABLE(MainPanel, Panel) EVT_AUINOTEBOOK_PAGE_CHANGED(ID_SCENE_NOTEBOOK
Scene implementation for behavior tree visualization.
void SetCanvasBackgroundColor(const wxColour &color)
Set the canvas background color.
static AppPreferencesManager & GetInstance()
MainPanelSettings & GetMainPanelSettings()
Custom tab art provider that uses the application's accent color.
Abstract interface for different scene types in the MainPanel.
Definition IScene.h:25
Central panel for managing and displaying behavior tree scenes.
Definition MainPanel.h:41
bool SetActiveScene(int index)
Set the active scene by index.
void UpdateActiveSceneTitle(const wxString &newTitle)
Update the title of the active scene.
void DoCreateLayout()
Definition MainPanel.cpp:25
bool RemoveScene(int index)
Remove a scene by index.
Definition MainPanel.cpp:99
std::vector< std::unique_ptr< IScene > > m_scenes
Definition MainPanel.h:156
@ ID_SCENE_NOTEBOOK
Definition MainPanel.h:133
@ ID_NEW_SCENE_BUTTON
Definition MainPanel.h:133
void DoCreateToolbar()
Definition MainPanel.cpp:51
void OnNewSceneButton(wxCommandEvent &event)
void CreateToolbar()
Definition MainPanel.cpp:47
int m_nextSceneNumber
Definition MainPanel.h:158
wxAuiNotebook * m_sceneNotebook
Definition MainPanel.h:153
int m_activeSceneIndex
Definition MainPanel.h:157
SceneChangedCallback m_sceneChangedCallback
Definition MainPanel.h:159
wxString GetTitle() const override
Get the display title for this panel.
Definition MainPanel.h:46
int AddScene(std::unique_ptr< IScene > scene)
Add a new scene to the panel.
Definition MainPanel.cpp:63
void SwitchToNextScene()
Switch to the next scene (cyclic)
MainPanel(wxWindow *parent)
void CreateLayout() override
Create the panel's UI layout Called during initialization.
Definition MainPanel.cpp:21
void OnTabChanged(wxAuiNotebookEvent &event)
wxString GenerateSceneTitle() const
void OnTabClosed(wxAuiNotebookEvent &event)
IScene * GetActiveScene() const
Get the currently active scene.
bool SceneTitleExists(const wxString &title) const
void ApplyCanvasBackgroundColor(const wxColour &color)
Apply canvas background color to all scenes.
IScene * GetScene(int index) const
Get a scene by index.
void SwitchToPreviousScene()
Switch to the previous scene (cyclic)
wxToolBar * m_toolbar
Definition MainPanel.h:152
int CreateNewScene()
Create a new empty behavior tree scene.
Base implementation of IPanel interface.
Definition Panel.h:28
bool HasUnsavedChanges() const override
Check if this panel has unsaved changes.
Definition Panel.h:59