/
GameSetupDialog.cpp
211 lines (160 loc) · 5.48 KB
/
GameSetupDialog.cpp
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
#include "GameSetupDialog.h"
#include "imainframe.h"
#include "i18n.h"
#include "igame.h"
#include "modulesystem/ModuleRegistry.h"
#include "wxutil/dialog/MessageBox.h"
#include "registry/registry.h"
#include "GameSetupPageIdTech.h"
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/choicebk.h>
#include <wx/stattext.h>
namespace ui
{
GameSetupDialog::GameSetupDialog(wxWindow* parent) :
DialogBase(_("Game Setup"), parent),
_book(nullptr)
{
SetSizer(new wxBoxSizer(wxVERTICAL));
// 12-pixel spacer
wxBoxSizer* mainVbox = new wxBoxSizer(wxVERTICAL);
GetSizer()->Add(mainVbox, 1, wxEXPAND | wxALL, 12);
_book = new wxChoicebook(this, wxID_ANY);
_book->Connect(wxEVT_CHOICEBOOK_PAGE_CHANGED, wxBookCtrlEventHandler(GameSetupDialog::onPageChanged), nullptr, this);
wxStaticText* label = new wxStaticText(this, wxID_ANY, _("Please select a Game Type:"));
mainVbox->Add(label);
mainVbox->Add(_book, 1, wxEXPAND);
wxBoxSizer* buttonHBox = new wxBoxSizer(wxHORIZONTAL);
// Create the Save button
wxButton* saveButton = new wxButton(this, wxID_SAVE);
saveButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(GameSetupDialog::onSave), nullptr, this);
// Create the assign shortcut button
wxButton* cancelButton = new wxButton(this, wxID_CANCEL);
cancelButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(GameSetupDialog::onCancel), nullptr, this);
buttonHBox->Add(saveButton, 0, wxRIGHT, 6);
buttonHBox->Add(cancelButton, 0, wxRIGHT, 6);
mainVbox->Add(buttonHBox, 0, wxALIGN_RIGHT | wxTOP | wxBOTTOM, 12);
initialiseControls();
Layout();
Fit();
}
void GameSetupDialog::initialiseControls()
{
const game::IGameManager::GameList& games = GlobalGameManager().getSortedGameList();
for (const game::IGamePtr& game : games)
{
wxPanel* container = new wxPanel(_book, wxID_ANY);
container->SetSizer(new wxBoxSizer(wxVERTICAL));
// For each game type create a separate page in the choice book
GameSetupPage* page = GameSetupPage::CreatePageForGame(game, container);
page->SetName("GameSetupPage");
// Store the game value as client data into the page object
page->SetClientData(new wxStringClientData(game->getKeyValue("name")));
container->GetSizer()->Add(page, 1, wxEXPAND | wxALL, 12);
_book->AddPage(container, game->getKeyValue("name"));
}
// Select the currently active game type
setSelectedPage(registry::getValue<std::string>(RKEY_GAME_TYPE));
}
void GameSetupDialog::setSelectedPage(const std::string& name)
{
for (std::size_t i = 0; i < _book->GetPageCount(); ++i)
{
wxWindow* container = _book->GetPage(i);
GameSetupPage* page = dynamic_cast<GameSetupPage*>(wxWindow::FindWindowByName("GameSetupPage", container));
assert(page != nullptr);
wxStringClientData* data = static_cast<wxStringClientData*>(page->GetClientData());
if (data->GetData() == name)
{
_book->SetSelection(i);
page->onPageShown();
return;
}
}
}
GameSetupPage* GameSetupDialog::getSelectedPage()
{
return getPage(_book->GetSelection());
}
GameSetupPage* GameSetupDialog::getPage(int num)
{
// Find the actual GameSetupPage in the window hierarchy
wxWindow* container = _book->GetPage(_book->GetSelection());
return dynamic_cast<GameSetupPage*>(wxWindow::FindWindowByName("GameSetupPage", container));
}
std::string GameSetupDialog::getSelectedGameType()
{
// Extract the game type value from the current page and save it to the registry
GameSetupPage* page = getSelectedPage();
if (page == nullptr) return std::string();
wxStringClientData* data = static_cast<wxStringClientData*>(page->GetClientData());
return data->GetData().ToStdString();
}
void GameSetupDialog::onSave(wxCommandEvent& ev)
{
if (getSelectedGameType().empty())
{
// Ask the user to select a game type
wxutil::Messagebox::Show(_("Invalid Settings"),
_("Please select a game type"), ui::IDialog::MESSAGE_CONFIRM, nullptr);
return;
}
GameSetupPage* page = GameSetupDialog::getSelectedPage();
assert(page != nullptr);
try
{
page->validateSettings();
}
catch (GameSettingsInvalidException& ex)
{
std::string msg = fmt::format(_("Warning:\n{0}\nDo you want to correct these settings?"), ex.what());
if (wxutil::Messagebox::Show(_("Invalid Settings"),
msg, ui::IDialog::MESSAGE_ASK, nullptr) == wxutil::Messagebox::RESULT_YES)
{
// User wants to correct the settings, don't exit
return;
}
}
EndModal(wxID_OK);
}
void GameSetupDialog::onCancel(wxCommandEvent& ev)
{
EndModal(wxID_CANCEL);
}
void GameSetupDialog::onPageChanged(wxBookCtrlEvent& ev)
{
if (ev.GetSelection() != wxNOT_FOUND)
{
GameSetupPage* page = getPage(ev.GetSelection());
if (page != nullptr)
{
page->onPageShown();
}
}
}
GameSetupDialog::Result GameSetupDialog::Show(const cmd::ArgumentList& args)
{
GameSetupDialog::Result result;
// greebo: Check if the mainframe module is already "existing". It might be
// uninitialised if this dialog is shown during DarkRadiant startup
wxWindow* parent = module::GlobalModuleRegistry().moduleExists(MODULE_MAINFRAME) ?
GlobalMainFrame().getWxTopLevelWindow() : nullptr;
GameSetupDialog* dialog = new GameSetupDialog(parent);
if (dialog->ShowModal() == wxID_OK)
{
result.gameName = dialog->getSelectedGameType();
if (result.gameName.empty())
{
rError() << "Cannot save game paths, nothing selected" << std::endl;
return result;
}
GameSetupPage* page = dialog->getSelectedPage();
result.enginePath = page->getEnginePath();
result.modPath = page->getModPath();
result.modBasePath = page->getModBasePath();
}
dialog->Destroy();
return result;
}
}