Ember
Loading...
Searching...
No Matches
ProjectManagerDialog.cpp
Go to the documentation of this file.
6#include <fstream>
7#include <nlohmann/json.hpp>
8#include <wx/datetime.h>
9#include <wx/filedlg.h>
10#include <wx/filename.h>
11#include <wx/msgdlg.h>
12#include <wx/statbox.h>
13#include <wx/statline.h>
14#include <wx/stdpaths.h>
15
17 EVT_LIST_ITEM_SELECTED(ID_PROJECT_LIST, ProjectManagerDialog::OnProjectSelected)
18 EVT_LIST_ITEM_ACTIVATED(ID_PROJECT_LIST, ProjectManagerDialog::OnProjectActivated)
19 EVT_BUTTON(ID_OPEN, ProjectManagerDialog::OnOpen) EVT_BUTTON(ID_BROWSE, ProjectManagerDialog::OnBrowse)
20 EVT_BUTTON(ID_NEW_PROJECT, ProjectManagerDialog::OnNewProject)
21 EVT_BUTTON(ID_REMOVE_FROM_RECENT, ProjectManagerDialog::OnRemoveFromRecent)
22 EVT_BUTTON(ID_CLEAR_RECENT, ProjectManagerDialog::OnClearRecent)
24
26 : wxDialog(parent, wxID_ANY, "Open BehaviorTree Project", wxDefaultPosition, wxSize(1400, 1000),
27 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) {
28 CreateLayout();
29 LoadRecentProjects();
30 UpdateButtonStates();
31 Centre();
32}
33
35
37 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
38
39 // Header
40 wxStaticText *header = new wxStaticText(this, wxID_ANY, "Recent Projects");
41 wxFont headerFont = header->GetFont();
42 headerFont.SetPointSize(headerFont.GetPointSize() + 2);
43 headerFont.MakeBold();
44 header->SetFont(headerFont);
45 mainSizer->Add(header, 0, wxALL, 10);
46
47 // Splitter: Left = project list, Right = details
48 wxSplitterWindow *splitter =
49 new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_3D | wxSP_LIVE_UPDATE);
50
51 // Left panel - Recent projects list
52 wxPanel *leftPanel = new wxPanel(splitter);
53 wxBoxSizer *leftSizer = new wxBoxSizer(wxVERTICAL);
54
55 m_projectList = new wxListCtrl(leftPanel, ID_PROJECT_LIST, wxDefaultPosition, wxDefaultSize,
56 wxLC_REPORT | wxLC_SINGLE_SEL | wxBORDER_SUNKEN);
57 m_projectList->InsertColumn(0, "Project Name", wxLIST_FORMAT_LEFT, 200);
58 m_projectList->InsertColumn(1, "Path", wxLIST_FORMAT_LEFT, 200);
59
60 leftSizer->Add(m_projectList, 1, wxEXPAND | wxALL, 5);
61 leftPanel->SetSizer(leftSizer);
62
63 // Right panel - Project details
64 wxPanel *rightPanel = new wxPanel(splitter);
65 wxBoxSizer *rightSizer = new wxBoxSizer(wxVERTICAL);
66
67 wxStaticBox *detailsBox = new wxStaticBox(rightPanel, wxID_ANY, "Project Details");
68 wxStaticBoxSizer *detailsSizer = new wxStaticBoxSizer(detailsBox, wxVERTICAL);
69
70 // Project name
71 wxBoxSizer *nameSizer = new wxBoxSizer(wxHORIZONTAL);
72 nameSizer->Add(new wxStaticText(rightPanel, wxID_ANY, "Name:"), 0, wxRIGHT, 10);
73 m_projectNameLabel = new wxStaticText(rightPanel, wxID_ANY, "-");
74 wxFont boldFont = m_projectNameLabel->GetFont();
75 boldFont.MakeBold();
76 m_projectNameLabel->SetFont(boldFont);
77 nameSizer->Add(m_projectNameLabel, 1, wxEXPAND);
78 detailsSizer->Add(nameSizer, 0, wxEXPAND | wxALL, 5);
79
80 // Description
81 wxBoxSizer *descSizer = new wxBoxSizer(wxHORIZONTAL);
82 descSizer->Add(new wxStaticText(rightPanel, wxID_ANY, "Description:"), 0, wxRIGHT, 10);
83 m_projectDescLabel = new wxStaticText(rightPanel, wxID_ANY, "-");
84 descSizer->Add(m_projectDescLabel, 1, wxEXPAND);
85 detailsSizer->Add(descSizer, 0, wxEXPAND | wxALL, 5);
86
87 // Path
88 wxBoxSizer *pathSizer = new wxBoxSizer(wxHORIZONTAL);
89 pathSizer->Add(new wxStaticText(rightPanel, wxID_ANY, "Location:"), 0, wxRIGHT, 10);
90 m_projectPathLabel = new wxStaticText(rightPanel, wxID_ANY, "-");
91 pathSizer->Add(m_projectPathLabel, 1, wxEXPAND);
92 detailsSizer->Add(pathSizer, 0, wxEXPAND | wxALL, 5);
93
94 // File count
95 wxBoxSizer *filesSizer = new wxBoxSizer(wxHORIZONTAL);
96 filesSizer->Add(new wxStaticText(rightPanel, wxID_ANY, "Files:"), 0, wxRIGHT, 10);
97 m_fileCountLabel = new wxStaticText(rightPanel, wxID_ANY, "-");
98 filesSizer->Add(m_fileCountLabel, 1, wxEXPAND);
99 detailsSizer->Add(filesSizer, 0, wxEXPAND | wxALL, 5);
100
101 // Last modified
102 wxBoxSizer *modSizer = new wxBoxSizer(wxHORIZONTAL);
103 modSizer->Add(new wxStaticText(rightPanel, wxID_ANY, "Last Modified:"), 0, wxRIGHT, 10);
104 m_lastModifiedLabel = new wxStaticText(rightPanel, wxID_ANY, "-");
105 modSizer->Add(m_lastModifiedLabel, 1, wxEXPAND);
106 detailsSizer->Add(modSizer, 0, wxEXPAND | wxALL, 5);
107
108 rightSizer->Add(detailsSizer, 0, wxEXPAND | wxALL, 5);
109 rightSizer->AddStretchSpacer(1);
110 rightPanel->SetSizer(rightSizer);
111
112 // Split the window
113 splitter->SplitVertically(leftPanel, rightPanel, 350);
114 splitter->SetMinimumPaneSize(200);
115
116 mainSizer->Add(splitter, 1, wxEXPAND | wxLEFT | wxRIGHT, 10);
117
118 mainSizer->Add(new wxStaticLine(this), 0, wxEXPAND | wxALL, 10);
119
120 // Bottom buttons
121 wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
122
123 m_openBtn = new wxButton(this, ID_OPEN, "Open");
124 m_openBtn->SetMinSize(wxSize(100, -1));
125 buttonSizer->Add(m_openBtn, 0, wxRIGHT, 5);
126
127 m_browseBtn = new wxButton(this, ID_BROWSE, "Browse...");
128 buttonSizer->Add(m_browseBtn, 0, wxRIGHT, 5);
129
130 m_newProjectBtn = new wxButton(this, ID_NEW_PROJECT, "New Project...");
131 buttonSizer->Add(m_newProjectBtn, 0, wxRIGHT, 5);
132
133 buttonSizer->AddSpacer(20);
134
135 m_removeBtn = new wxButton(this, ID_REMOVE_FROM_RECENT, "Remove from List");
136 buttonSizer->Add(m_removeBtn, 0, wxRIGHT, 5);
137
138 m_clearBtn = new wxButton(this, ID_CLEAR_RECENT, "Clear All");
139 buttonSizer->Add(m_clearBtn, 0, wxRIGHT, 5);
140
141 buttonSizer->AddStretchSpacer(1);
142
143 m_cancelBtn = new wxButton(this, wxID_CANCEL, "Cancel");
144 buttonSizer->Add(m_cancelBtn, 0);
145
146 mainSizer->Add(buttonSizer, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 10);
147
148 SetSizer(mainSizer);
149}
150
152 m_projectList->DeleteAllItems();
153
154 auto &projectManager = EmberCore::ProjectManager::GetInstance();
155 const auto &recentProjects = projectManager.GetRecentProjects();
156
157 for (size_t i = 0; i < recentProjects.size(); ++i) {
158 const auto &path = recentProjects[i];
159
160 // Try to load project to get its name
161 wxString projectName = "Unknown";
162 wxFileName fn(wxString::FromUTF8(path));
163
164 // Check if file exists
165 if (wxFileExists(wxString::FromUTF8(path))) {
166 // Try to load just the name from the project file
167 std::ifstream file(path);
168 if (file.is_open()) {
169 try {
170 nlohmann::json json;
171 file >> json;
172 if (json.contains("project_name")) {
173 projectName = wxString::FromUTF8(json["project_name"].get<std::string>());
174 }
175 } catch (...) {
176 // Use filename as fallback
177 projectName = fn.GetName();
178 }
179 file.close();
180 }
181 } else {
182 projectName = fn.GetName() + " (not found)";
183 }
184
185 long item = m_projectList->InsertItem(i, projectName);
186 m_projectList->SetItem(item, 1, fn.GetPath());
187 m_projectList->SetItemData(item, i);
188 }
189
190 // Update button states
192}
193
194void ProjectManagerDialog::UpdateProjectDetails(const wxString &projectPath) {
195 // Try to load project details
196 std::ifstream file(projectPath.ToStdString());
197 if (!file.is_open()) {
199 m_projectNameLabel->SetLabel("File not found");
200 return;
201 }
202
203 try {
204 nlohmann::json json;
205 file >> json;
206 file.close();
207
208 // Name
209 if (json.contains("project_name")) {
210 m_projectNameLabel->SetLabel(wxString::FromUTF8(json["project_name"].get<std::string>()));
211 } else {
212 m_projectNameLabel->SetLabel("-");
213 }
214
215 // Description
216 if (json.contains("description")) {
217 wxString desc = wxString::FromUTF8(json["description"].get<std::string>());
218 if (desc.IsEmpty())
219 desc = "(none)";
220 m_projectDescLabel->SetLabel(desc);
221 } else {
222 m_projectDescLabel->SetLabel("(none)");
223 }
224
225 // Path
226 wxFileName fn(projectPath);
227 m_projectPathLabel->SetLabel(fn.GetPath());
228
229 // File count
230 if (json.contains("resources")) {
231 size_t count = json["resources"].size();
232 m_fileCountLabel->SetLabel(wxString::Format("%zu XML file(s)", count));
233 } else {
234 m_fileCountLabel->SetLabel("0 files");
235 }
236
237 // Last modified
238 if (json.contains("modified_timestamp")) {
239 int64_t timestamp = json["modified_timestamp"].get<int64_t>();
240 wxDateTime dt;
241 dt.Set(static_cast<time_t>(timestamp));
242 m_lastModifiedLabel->SetLabel(dt.Format("%Y-%m-%d %H:%M:%S"));
243 } else {
244 // Get file modification time
245 wxFileName fn2(projectPath);
246 if (fn2.FileExists()) {
247 wxDateTime dt = fn2.GetModificationTime();
248 m_lastModifiedLabel->SetLabel(dt.Format("%Y-%m-%d %H:%M:%S"));
249 } else {
250 m_lastModifiedLabel->SetLabel("-");
251 }
252 }
253
254 } catch (...) {
256 m_projectNameLabel->SetLabel("Error reading project");
257 }
258}
259
261 m_projectNameLabel->SetLabel("-");
262 m_projectDescLabel->SetLabel("-");
263 m_projectPathLabel->SetLabel("-");
264 m_fileCountLabel->SetLabel("-");
265 m_lastModifiedLabel->SetLabel("-");
266}
267
269 bool hasSelection = m_projectList->GetSelectedItemCount() > 0;
270 bool hasItems = m_projectList->GetItemCount() > 0;
271
272 m_openBtn->Enable(hasSelection);
273 m_removeBtn->Enable(hasSelection);
274 m_clearBtn->Enable(hasItems);
275}
276
278 long item = event.GetIndex();
279
280 auto &projectManager = EmberCore::ProjectManager::GetInstance();
281 const auto &recentProjects = projectManager.GetRecentProjects();
282
283 if (item < static_cast<long>(recentProjects.size())) {
284 wxString path = wxString::FromUTF8(recentProjects[item]);
286 m_selectedPath = path;
287 }
288
290}
291
293 // Double-click opens the project
294 wxCommandEvent dummyEvent;
295 OnOpen(dummyEvent);
296}
297
298void ProjectManagerDialog::OnOpen(wxCommandEvent &event) {
299 if (m_selectedPath.IsEmpty()) {
300 return;
301 }
302
303 // Check if file exists
304 if (!wxFileExists(m_selectedPath)) {
305 int result = wxMessageBox("The project file no longer exists. Remove it from the recent list?",
306 "File Not Found", wxYES_NO | wxICON_WARNING, this);
307
308 if (result == wxYES) {
309 auto &projectManager = EmberCore::ProjectManager::GetInstance();
310 projectManager.RemoveFromRecentProjects(m_selectedPath.ToStdString());
313 m_selectedPath.Clear();
314 }
315 return;
316 }
317
318 EndModal(wxID_OK);
319}
320
321void ProjectManagerDialog::OnBrowse(wxCommandEvent &event) {
322 wxFileDialog openFileDialog(this, "Open BehaviorTree Project", "", "",
323 "BehaviorTree Project (*.btproj)|*.btproj|All files (*.*)|*.*",
324 wxFD_OPEN | wxFD_FILE_MUST_EXIST);
325
326 if (openFileDialog.ShowModal() == wxID_CANCEL) {
327 return;
328 }
329
330 m_selectedPath = openFileDialog.GetPath();
331 EndModal(wxID_OK);
332}
333
334void ProjectManagerDialog::OnNewProject(wxCommandEvent &event) {
335 // Show the new project dialog
336 BehaviorTreeProjectDialog dialog(this, nullptr);
337 int result = dialog.ShowModal();
338
339 if (result == wxID_OK) {
340 auto project = dialog.GetProject();
341 if (project) {
342 // Get default my_projects directory
343 wxString defaultProjectsDir = EmberForge::ResourcePath::GetDir("my_projects");
344
345 // Create directory if it doesn't exist
346 if (!wxFileName::DirExists(defaultProjectsDir)) {
347 wxFileName::Mkdir(defaultProjectsDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
348 }
349
350 // Ask where to save the project
351 wxFileDialog saveDialog(this, "Save BehaviorTree Project", defaultProjectsDir,
352 wxString::FromUTF8(project->GetName()) + ".btproj",
353 "BehaviorTree Project (*.btproj)|*.btproj", wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
354
355 if (saveDialog.ShowModal() == wxID_OK) {
356 wxString projectPath = saveDialog.GetPath();
357 project->SetProjectFilePath(projectPath.ToStdString());
358
359 if (project->SaveToFile(projectPath.ToStdString())) {
360 // Add to recent projects
361 auto &projectManager = EmberCore::ProjectManager::GetInstance();
362 projectManager.AddToRecentProjects(projectPath.ToStdString());
363
364 // Set as selected and close dialog
365 m_selectedPath = projectPath;
366 EndModal(wxID_OK);
367 } else {
368 wxMessageBox("Failed to save project file.", "Error", wxOK | wxICON_ERROR, this);
369 }
370 }
371 }
372 }
373}
374
375void ProjectManagerDialog::OnRemoveFromRecent(wxCommandEvent &event) {
376 if (m_selectedPath.IsEmpty()) {
377 return;
378 }
379
380 auto &projectManager = EmberCore::ProjectManager::GetInstance();
381 projectManager.RemoveFromRecentProjects(m_selectedPath.ToStdString());
382
385 m_selectedPath.Clear();
387}
388
389void ProjectManagerDialog::OnClearRecent(wxCommandEvent &event) {
390 int result = wxMessageBox("Clear all recent projects from the list?\n\n"
391 "This will not delete the actual project files.",
392 "Confirm Clear", wxYES_NO | wxICON_QUESTION, this);
393
394 if (result != wxYES) {
395 return;
396 }
397
398 auto &projectManager = EmberCore::ProjectManager::GetInstance();
399 projectManager.ClearRecentProjects();
400
403 m_selectedPath.Clear();
405}
406
407void ProjectManagerDialog::OnCancel(wxCommandEvent &event) {
408 m_selectedPath.Clear();
409 EndModal(wxID_CANCEL);
410}
BehaviorTreeProjectDialog::OnProjectNameChanged BehaviorTreeProjectDialog::OnRemoveFiles wxEND_EVENT_TABLE() BehaviorTreeProjectDialog
MainFrame::OnExit MainFrame::OnNewProject MainFrame::OnCloseProject MainFrame::OnToggleMaximize MainFrame::OnPreviousScene MainFrame::OnPreferences MainFrame::OnEditorTool EVT_BUTTON(ID_MonitorTool, MainFrame::OnMonitorTool) EVT_PAINT(MainFrame
Definition MainFrame.cpp:63
wxBEGIN_EVENT_TABLE(ProjectManagerDialog, wxDialog) EVT_BUTTON(ID_OPEN
Centralized resource path management for EmberForge.
Dialog for creating and configuring BehaviorTree projects.
std::shared_ptr< EmberCore::BehaviorTreeProject > GetProject() const
Get the configured project (after dialog is closed with OK)
static ProjectManager & GetInstance()
static wxString GetDir(const wxString &relativeDir)
Get the full path to a resource directory.
Dialog for opening and managing BehaviorTree projects.
void OnClearRecent(wxCommandEvent &event)
wxStaticText * m_projectPathLabel
void OnProjectSelected(wxListEvent &event)
wxStaticText * m_projectDescLabel
wxStaticText * m_lastModifiedLabel
void OnOpen(wxCommandEvent &event)
void UpdateProjectDetails(const wxString &projectPath)
void OnProjectActivated(wxListEvent &event)
wxStaticText * m_projectNameLabel
void OnBrowse(wxCommandEvent &event)
wxStaticText * m_fileCountLabel
void OnCancel(wxCommandEvent &event)
void OnRemoveFromRecent(wxCommandEvent &event)
void OnNewProject(wxCommandEvent &event)