-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgui_main.cpp
More file actions
413 lines (352 loc) · 17.1 KB
/
gui_main.cpp
File metadata and controls
413 lines (352 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <wx/dirdlg.h>
#include <wx/simplebook.h>
#include <wx/scrolwin.h>
#include <wx/statline.h>
#include <thread>
#include <vector>
#include "core_logic.h"
// --- Custom Events for Thread Communication ---
wxDEFINE_EVENT(wxEVT_DISCOVERY_COMPLETE, wxThreadEvent);
wxDEFINE_EVENT(wxEVT_RESOLVE_COMPLETE, wxCommandEvent);
wxDEFINE_EVENT(wxEVT_INSTALL_UPDATE, wxThreadEvent);
wxDEFINE_EVENT(wxEVT_INSTALL_COMPLETE, wxThreadEvent);
// --- Forward Declarations ---
class MyFrame;
class ProgressPanel;
// --- Main Application Class ---
class MyApp : public wxApp {
public:
virtual bool OnInit();
};
// --- Main Frame Class ---
class MyFrame : public wxFrame {
public:
MyFrame();
void StartInstallation();
void OnStartOver(wxCommandEvent& event);
fs::path projectPath;
MRTKToolCore tool;
wxSimplebook* book;
private:
void OnDiscoveryComplete(wxThreadEvent& event);
void OnResolveComplete(wxCommandEvent& event);
void OnInstallUpdate(wxThreadEvent& event);
void OnInstallComplete(wxThreadEvent& event);
ProgressPanel* progressPanel;
};
// --- Panel Classes ---
wxSizer* CreatePackageListSizer(wxWindow* parent, const std::map<std::string, std::string>& packages) {
auto* sizer = new wxBoxSizer(wxVERTICAL);
wxFont itemFont = parent->GetFont();
itemFont.SetPointSize(itemFont.GetPointSize() + 1);
for (const auto& [name, version] : packages) {
auto* itemSizer = new wxBoxSizer(wxHORIZONTAL);
auto* cb = new wxCheckBox(parent, wxID_ANY, "");
cb->SetValue(true);
cb->Disable();
auto* label = new wxStaticText(parent, wxID_ANY, name + " " + version);
label->SetFont(itemFont);
label->SetForegroundColour(*wxWHITE);
itemSizer->Add(cb, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 8);
itemSizer->Add(label, 0, wxALIGN_CENTER_VERTICAL);
sizer->Add(itemSizer, 0, wxEXPAND | wxBOTTOM, 5);
}
return sizer;
}
class ProjectSelectPanel : public wxPanel {
public:
ProjectSelectPanel(wxWindow* parent);
private:
void OnBrowse(wxCommandEvent&);
void OnPathChanged(wxCommandEvent&);
void OnDiscover(wxCommandEvent&);
void ValidateProjectPath();
wxTextCtrl* pathTextCtrl;
wxStaticText* versionText;
wxButton* discoverButton;
};
class FeatureSelectPanel : public wxPanel {
public:
FeatureSelectPanel(wxWindow* parent, const std::vector<SelectablePackage>& packages);
private:
void OnGetFeatures(wxCommandEvent&);
std::vector<wxCheckBox*> checkBoxes;
};
class ImportPanel : public wxPanel {
public:
ImportPanel(wxWindow* parent, MyFrame* mainFrame);
private:
void OnImport(wxCommandEvent& event) {
m_mainFrame->book->SetSelection(3);
m_mainFrame->StartInstallation();
}
void OnGoBack(wxCommandEvent& event) {
m_mainFrame->book->SetSelection(1);
}
MyFrame* m_mainFrame;
};
class ProgressPanel : public wxPanel {
public:
ProgressPanel(wxWindow* parent) : wxPanel(parent) {
SetBackgroundColour(wxColour(40, 40, 40));
auto* sizer = new wxBoxSizer(wxVERTICAL);
auto* title = new wxStaticText(this, wxID_ANY, "Importing Features");
wxFont titleFont(24, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
title->SetFont(titleFont);
title->SetForegroundColour(*wxWHITE);
logOutput = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2);
logOutput->SetBackgroundColour(*wxBLACK);
logOutput->SetForegroundColour(wxColour(200, 200, 200));
sizer->Add(title, 0, wxALL, 20);
sizer->Add(logOutput, 1, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 20);
SetSizer(sizer);
}
void AppendLog(const wxString& text) {
logOutput->AppendText(text);
}
private:
wxTextCtrl* logOutput;
};
class CompletionPanel : public wxPanel {
public:
CompletionPanel(wxWindow* parent, MyFrame* mainFrame) : wxPanel(parent) {
SetBackgroundColour(wxColour(40, 40, 40));
auto* sizer = new wxBoxSizer(wxVERTICAL);
auto* title = new wxStaticText(this, wxID_ANY, "Unity Project Updated");
wxFont titleFont(24, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
title->SetFont(titleFont);
title->SetForegroundColour(*wxWHITE);
std::string message = mainFrame->projectPath.filename().string() + " has been updated.\nPlease return to Unity to load the imported features.";
auto* body = new wxStaticText(this, wxID_ANY, message);
body->SetForegroundColour(wxColour(220, 220, 220));
auto* btnSizer = new wxBoxSizer(wxHORIZONTAL);
auto* startOverBtn = new wxButton(this, wxID_ANY, "Start Over");
auto* exitBtn = new wxButton(this, wxID_EXIT, "Exit");
btnSizer->Add(startOverBtn, 0, wxRIGHT, 10);
btnSizer->Add(exitBtn);
sizer->Add(title, 0, wxALL, 20);
sizer->Add(body, 0, wxLEFT | wxRIGHT | wxBOTTOM, 20);
sizer->AddStretchSpacer(1);
sizer->Add(btnSizer, 0, wxALIGN_RIGHT | wxALL, 20);
SetSizer(sizer);
startOverBtn->Bind(wxEVT_BUTTON, &MyFrame::OnStartOver, mainFrame);
// <<< FIX: Bind the Exit button's click event to close the main window.
exitBtn->Bind(wxEVT_BUTTON, [mainFrame](wxCommandEvent& event){
mainFrame->Close();
});
}
};
// --- Implementation ---
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit() {
MyFrame *frame = new MyFrame();
frame->Show(true);
return true;
}
MyFrame::MyFrame() : wxFrame(NULL, wxID_ANY, "MRTK Feature Tool", wxDefaultPosition, wxSize(700, 550)) {
SetBackgroundColour(wxColour(40, 40, 40));
book = new wxSimplebook(this, wxID_ANY);
book->AddPage(new ProjectSelectPanel(book), "Select Project");
Bind(wxEVT_DISCOVERY_COMPLETE, &MyFrame::OnDiscoveryComplete, this);
Bind(wxEVT_RESOLVE_COMPLETE, &MyFrame::OnResolveComplete, this);
Bind(wxEVT_INSTALL_UPDATE, &MyFrame::OnInstallUpdate, this);
Bind(wxEVT_INSTALL_COMPLETE, &MyFrame::OnInstallComplete, this);
}
void MyFrame::OnDiscoveryComplete(wxThreadEvent& event) {
auto discoveredPackages = tool.getAvailablePackages();
if (discoveredPackages.empty()) {
wxMessageBox("Failed to discover any components.", "Discovery Failed", wxICON_ERROR);
auto* psp = static_cast<ProjectSelectPanel*>(book->GetPage(0));
psp->FindWindowByLabel("Discover Features")->Enable();
psp->FindWindowByLabel("Discover Features")->SetLabel("Discover Features");
} else {
book->AddPage(new FeatureSelectPanel(book, discoveredPackages), "Select Features");
book->SetSelection(1);
}
}
void MyFrame::OnResolveComplete(wxCommandEvent& event) {
book->AddPage(new ImportPanel(book, this), "Import Features");
progressPanel = new ProgressPanel(book);
book->AddPage(progressPanel, "Progress");
book->AddPage(new CompletionPanel(book, this), "Complete");
book->SetSelection(2);
}
void MyFrame::StartInstallation() {
std::thread worker_thread([this]() {
auto* old_cout_buf = std::cout.rdbuf();
std::stringstream new_cout_stream;
std::cout.rdbuf(new_cout_stream.rdbuf());
auto post_update = [&]() {
std::string text = new_cout_stream.str();
new_cout_stream.str("");
if (!text.empty()) {
wxThreadEvent* update_event = new wxThreadEvent(wxEVT_INSTALL_UPDATE);
update_event->SetString(text);
wxQueueEvent(this, update_event);
}
};
tool.downloadAndRepackage();
post_update();
tool.installPackagesToProject(projectPath);
post_update();
std::cout.rdbuf(old_cout_buf);
wxQueueEvent(this, new wxThreadEvent(wxEVT_INSTALL_COMPLETE));
});
worker_thread.detach();
}
void MyFrame::OnInstallUpdate(wxThreadEvent& event) {
if (progressPanel) {
progressPanel->AppendLog(event.GetString());
}
}
void MyFrame::OnInstallComplete(wxThreadEvent& event) {
book->SetSelection(4);
}
void MyFrame::OnStartOver(wxCommandEvent& event) {
while(book->GetPageCount() > 1) {
book->DeletePage(book->GetPageCount() - 1);
}
book->ChangeSelection(0);
}
// --- ProjectSelectPanel Implementation ---
ProjectSelectPanel::ProjectSelectPanel(wxWindow* parent) : wxPanel(parent) {
SetBackgroundColour(wxColour(40, 40, 40));
wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
wxFont titleFont(24, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
auto* titleLabel = new wxStaticText(this, wxID_ANY, "Select Project");
titleLabel->SetFont(titleFont);
titleLabel->SetForegroundColour(*wxWHITE);
wxBoxSizer* pathSizer = new wxBoxSizer(wxHORIZONTAL);
pathSizer->Add(new wxStaticText(this, wxID_ANY, "Project Path:"), 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 8);
pathTextCtrl = new wxTextCtrl(this, wxID_ANY, "");
pathSizer->Add(pathTextCtrl, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL);
auto* browseButton = new wxButton(this, wxID_ANY, "...", wxDefaultPosition, wxSize(40, -1));
pathSizer->Add(browseButton, 0, wxALIGN_CENTER_VERTICAL | wxLEFT, 8);
wxBoxSizer* versionSizer = new wxBoxSizer(wxHORIZONTAL);
versionSizer->Add(new wxStaticText(this, wxID_ANY, "Unity Version:"), 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 8);
versionText = new wxStaticText(this, wxID_ANY, "<None>");
versionSizer->Add(versionText, 1, wxALIGN_CENTER_VERTICAL);
discoverButton = new wxButton(this, wxID_ANY, "Discover Features");
discoverButton->Disable();
mainSizer->Add(titleLabel, 0, wxEXPAND | wxALL, 20);
mainSizer->Add(pathSizer, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 20);
mainSizer->Add(versionSizer, 0, wxEXPAND | wxLEFT | wxRIGHT, 20);
mainSizer->AddStretchSpacer(1);
mainSizer->Add(discoverButton, 0, wxALIGN_RIGHT | wxALL, 20);
SetSizer(mainSizer);
for (auto child : this->GetChildren()) { if (auto* staticChild = dynamic_cast<wxStaticText*>(child)) { staticChild->SetForegroundColour(wxColour(220, 220, 220)); } }
titleLabel->SetForegroundColour(*wxWHITE);
browseButton->Bind(wxEVT_BUTTON, &ProjectSelectPanel::OnBrowse, this);
pathTextCtrl->Bind(wxEVT_TEXT, &ProjectSelectPanel::OnPathChanged, this);
discoverButton->Bind(wxEVT_BUTTON, &ProjectSelectPanel::OnDiscover, this);
}
void ProjectSelectPanel::OnBrowse(wxCommandEvent& event) { wxDirDialog d(this, "Select a Unity Project Folder", "", wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST); if (d.ShowModal() == wxID_OK) { pathTextCtrl->SetValue(d.GetPath()); } }
void ProjectSelectPanel::OnPathChanged(wxCommandEvent& event) { ValidateProjectPath(); }
void ProjectSelectPanel::ValidateProjectPath() { fs::path p(pathTextCtrl->GetValue().ToStdString()); MyFrame* mf = static_cast<MyFrame*>(GetParent()->GetParent()); if (MRTKToolCore::isValidUnityProject(p)) { versionText->SetLabel(MRTKToolCore::getUnityVersion(p)); versionText->SetForegroundColour(wxColour(150, 255, 150)); discoverButton->Enable(); mf->projectPath = p; } else { versionText->SetLabel("<Invalid Project Path>"); versionText->SetForegroundColour(wxColour(255, 150, 150)); discoverButton->Disable(); } Layout(); }
void ProjectSelectPanel::OnDiscover(wxCommandEvent& event) { discoverButton->SetLabel("Discovering..."); discoverButton->Disable(); MyFrame* mf = static_cast<MyFrame*>(GetParent()->GetParent()); std::thread t([mf]() { mf->tool.fetchAvailablePackages(); wxQueueEvent(mf, new wxThreadEvent(wxEVT_DISCOVERY_COMPLETE)); }); t.detach(); }
// --- FeatureSelectPanel Implementation ---
FeatureSelectPanel::FeatureSelectPanel(wxWindow* parent, const std::vector<SelectablePackage>& packages) : wxPanel(parent) {
SetBackgroundColour(wxColour(40, 40, 40));
auto* mainSizer = new wxBoxSizer(wxVERTICAL);
auto* titleLabel = new wxStaticText(this, wxID_ANY, "Discover Features");
wxFont titleFont(24, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
titleLabel->SetFont(titleFont);
titleLabel->SetForegroundColour(*wxWHITE);
mainSizer->Add(titleLabel, 0, wxEXPAND | wxALL, 20);
auto* scrollWindow = new wxScrolled<wxPanel>(this, wxID_ANY);
scrollWindow->SetScrollRate(0, 10);
scrollWindow->SetBackgroundColour(wxColour(50, 50, 50));
auto* contentSizer = new wxBoxSizer(wxVERTICAL);
wxFont sectionFont = GetFont();
sectionFont.MakeBold();
auto* mrtkLabel = new wxStaticText(scrollWindow, wxID_ANY, "Mixed Reality Toolkit");
mrtkLabel->SetFont(sectionFont);
mrtkLabel->SetForegroundColour(*wxWHITE);
contentSizer->Add(mrtkLabel, 0, wxLEFT | wxTOP, 10);
contentSizer->Add(new wxStaticLine(scrollWindow), 0, wxEXPAND | wxALL, 5);
auto* mrtkSizer = new wxBoxSizer(wxVERTICAL);
contentSizer->Add(mrtkSizer, 0, wxEXPAND | wxALL, 5);
auto* openxrLabel = new wxStaticText(scrollWindow, wxID_ANY, "OpenXR Runtimes");
openxrLabel->SetFont(sectionFont);
openxrLabel->SetForegroundColour(*wxWHITE);
contentSizer->Add(openxrLabel, 0, wxLEFT | wxTOP, 20);
contentSizer->Add(new wxStaticLine(scrollWindow), 0, wxEXPAND | wxALL, 5);
auto* openxrSizer = new wxBoxSizer(wxVERTICAL);
contentSizer->Add(openxrSizer, 0, wxEXPAND | wxALL, 5);
wxFont itemFont = GetFont();
itemFont.SetPointSize(itemFont.GetPointSize() + 2);
for (const auto& pkg : packages) {
auto* itemSizer = new wxBoxSizer(wxHORIZONTAL);
auto* cb = new wxCheckBox(scrollWindow, wxID_ANY, "");
checkBoxes.push_back(cb);
auto* label = new wxStaticText(scrollWindow, wxID_ANY, pkg.displayName);
label->SetFont(itemFont);
label->SetForegroundColour(*wxWHITE);
itemSizer->Add(cb, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 8);
itemSizer->Add(label, 1, wxALIGN_CENTER_VERTICAL);
label->Bind(wxEVT_LEFT_DOWN, [cb](wxMouseEvent& event){
cb->SetValue(!cb->GetValue());
});
if (pkg.type == PackageType::MRTK) mrtkSizer->Add(itemSizer, 0, wxEXPAND | wxALL, 5);
else openxrSizer->Add(itemSizer, 0, wxEXPAND | wxALL, 5);
}
scrollWindow->SetSizer(contentSizer);
mainSizer->Add(scrollWindow, 1, wxEXPAND | wxLEFT | wxRIGHT, 20);
auto* getFeaturesButton = new wxButton(this, wxID_ANY, "Get Features");
mainSizer->Add(getFeaturesButton, 0, wxALIGN_RIGHT | wxALL, 20);
SetSizer(mainSizer);
getFeaturesButton->Bind(wxEVT_BUTTON, &FeatureSelectPanel::OnGetFeatures, this);
}
void FeatureSelectPanel::OnGetFeatures(wxCommandEvent& event) {
std::vector<int> selectedIndices;
for (int i = 0; i < checkBoxes.size(); ++i) {
if (checkBoxes[i]->IsChecked()) selectedIndices.push_back(i);
}
if (selectedIndices.empty()) { wxMessageBox("No features were selected.", "Warning", wxOK | wxICON_WARNING); return; }
MyFrame* mainFrame = static_cast<MyFrame*>(GetParent()->GetParent());
std::thread worker_thread([mainFrame, selectedIndices]() {
mainFrame->tool.resolveDependencies(selectedIndices);
wxQueueEvent(mainFrame, new wxCommandEvent(wxEVT_RESOLVE_COMPLETE));
});
worker_thread.detach();
}
// --- ImportPanel Implementation ---
ImportPanel::ImportPanel(wxWindow* parent, MyFrame* mainFrame) : wxPanel(parent), m_mainFrame(mainFrame) {
SetBackgroundColour(wxColour(40, 40, 40));
auto* sizer = new wxBoxSizer(wxVERTICAL);
auto* title = new wxStaticText(this, wxID_ANY, "Import Features");
wxFont titleFont(24, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
title->SetFont(titleFont);
title->SetForegroundColour(*wxWHITE);
auto* body = new wxStaticText(this, wxID_ANY, "The Mixed Reality Feature Tool has identified the packages that are required to import your chosen features.");
body->SetForegroundColour(wxColour(220, 220, 220));
body->Wrap(600);
auto* gridSizer = new wxFlexGridSizer(2, 20, 20);
gridSizer->AddGrowableCol(0, 1);
gridSizer->AddGrowableCol(1, 1);
auto* featuresLabel = new wxStaticText(this, wxID_ANY, "Features");
featuresLabel->SetForegroundColour(wxColour(200, 200, 200));
gridSizer->Add(featuresLabel, 0, wxBOTTOM, 5);
auto* depsLabel = new wxStaticText(this, wxID_ANY, "Required dependencies");
depsLabel->SetForegroundColour(wxColour(200, 200, 200));
gridSizer->Add(depsLabel, 0, wxBOTTOM, 5);
gridSizer->Add(CreatePackageListSizer(this, mainFrame->tool.resolvedUserSelections), 1, wxEXPAND);
gridSizer->Add(CreatePackageListSizer(this, mainFrame->tool.resolvedDependencies), 1, wxEXPAND);
auto* btnSizer = new wxBoxSizer(wxHORIZONTAL);
auto* goBackBtn = new wxButton(this, wxID_ANY, "Go back");
auto* importBtn = new wxButton(this, wxID_ANY, "Import");
btnSizer->Add(goBackBtn);
btnSizer->AddStretchSpacer(1);
btnSizer->Add(importBtn);
sizer->Add(title, 0, wxALL, 20);
sizer->Add(body, 0, wxLEFT | wxRIGHT | wxBOTTOM, 20);
sizer->Add(gridSizer, 1, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 20);
sizer->Add(btnSizer, 0, wxEXPAND | wxALL, 20);
SetSizer(sizer);
goBackBtn->Bind(wxEVT_BUTTON, &ImportPanel::OnGoBack, this);
importBtn->Bind(wxEVT_BUTTON, &ImportPanel::OnImport, this);
}