forked from Voidious/BerryBots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resultsdialog.cpp
365 lines (321 loc) · 11.5 KB
/
resultsdialog.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
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
/*
Copyright (C) 2013 - Voidious
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <string.h>
#include <wx/wx.h>
#include <wx/grid.h>
#include <wx/datetime.h>
#include "bbwx.h"
#include "basedir.h"
#include "ResourcePath.hpp"
#include "filemanager.h"
#include "sysexec.h"
#include "resultsdialog.h"
ResultsDialog::ResultsDialog(const char *stageName, Team **teams, int numTeams,
bool hasScores, wxPoint center, ReplayBuilder *replayBuilder,
ResultsDialogListener *listener)
: wxFrame(NULL, wxID_ANY, "Results", wxDefaultPosition, wxDefaultSize,
wxDEFAULT_FRAME_STYLE & ~ (wxMAXIMIZE_BOX | wxRESIZE_BORDER)) {
stageName_ = new char[strlen(stageName) + 1];
replayFilename_ = 0;
timestamp_ = 0;
savedReplay_ = false;
strcpy(stageName_, stageName);
replayBuilder_ = replayBuilder;
listener_ = listener;
fileManager_ = new FileManager();
systemExecutor_ = new SystemExecutor();
#ifdef __WINDOWS__
SetIcon(wxIcon(resourcePath() + BERRYBOTS_ICO, wxBITMAP_TYPE_ICO));
// The 8-9 point default font size in Windows is much smaller than Mac/Linux.
wxFont windowFont = GetFont();
if (windowFont.GetPointSize() <= 9) {
SetFont(windowFont.Larger());
}
#elif __WXGTK__
SetIcon(wxIcon(resourcePath() + BBICON_128, wxBITMAP_TYPE_PNG));
#endif
mainPanel_ = new wxPanel(this, wxID_ANY);
panelSizer_ = new wxBoxSizer(wxVERTICAL);
wxGrid *resultsGrid =
new wxGrid(mainPanel_, wxID_ANY, wxPoint(0, 0), wxDefaultSize);
int baseCols = (hasScores ? 3 : 2);
TeamResult *firstResult = &(teams[0]->result);
int numStats = firstResult->numStats;
char **statKeys = 0;
if (numStats > 0) {
statKeys = new char*[firstResult->numStats];
for (int x = 0; x < numStats; x++) {
statKeys[x] = new char[strlen(firstResult->stats[x]->key) + 1];
strcpy(statKeys[x], firstResult->stats[x]->key);
resultsGrid->SetColLabelValue(baseCols + x, statKeys[x]);
}
}
int numResults = 0;
for (int x = 0; x < numTeams; x++) {
if (teams[x]->result.showResult) {
numResults++;
}
}
resultsGrid->CreateGrid(numResults, baseCols + numStats);
resultsGrid->EnableEditing(false);
resultsGrid->SetColumnWidth(0, 50);
resultsGrid->SetColLabelValue(0, "Rank");
resultsGrid->SetColumnWidth(1, 150);
resultsGrid->SetColLabelValue(1, "Name");
if (hasScores) {
resultsGrid->SetColLabelValue(2, "Score");
}
for (int x = 0; x < numStats; x++) {
resultsGrid->SetColLabelValue(baseCols + x, statKeys[x]);
}
resultsGrid->HideRowLabels();
int resultIndex = 0;
for (int x = 0; x < numTeams; x++) {
TeamResult *result = &(teams[x]->result);
if (result->showResult) {
if (result->rank == 0) {
resultsGrid->SetCellValue(resultIndex, 0, "-");
} else {
resultsGrid->SetCellValue(resultIndex, 0,
wxString::Format(wxT("%i"), result->rank));
}
resultsGrid->SetCellAlignment(wxALIGN_CENTER, resultIndex, 0);
resultsGrid->SetCellValue(resultIndex, 1,
wxString::Format(wxT(" %s"), teams[x]->name));
if (hasScores) {
resultsGrid->SetCellValue(resultIndex, 2,
wxString::Format(wxT("%.2f "), result->score));
resultsGrid->SetCellAlignment(wxALIGN_RIGHT, resultIndex, 2);
}
for (int y = 0; y < numStats; y++) {
char *key = statKeys[y];
bool found = false;
for (int z = 0; z < result->numStats; z++) {
char *resultKey = result->stats[z]->key;
if (strcmp(key, resultKey) == 0) {
resultsGrid->SetCellValue(resultIndex, baseCols + y,
wxString::Format(wxT("%.2f "), result->stats[z]->value));
resultsGrid->SetCellAlignment(wxALIGN_RIGHT, resultIndex,
baseCols + y);
found = true;
break;
}
}
if (!found) {
resultsGrid->SetCellValue(resultIndex, baseCols + y, "-");
}
}
resultIndex++;
}
}
panelSizer_->Add(resultsGrid, 0, wxEXPAND);
panelSizer_->AddSpacer(3);
wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
saveButton_ = new wxButton(mainPanel_, wxID_ANY, " &Save Replay ");
viewButton_ = new wxButton(mainPanel_, wxID_ANY, " &View Replay (HTML) ");
restartButton_ = new wxButton(mainPanel_, wxID_ANY, " &Restart ");
buttonSizer->AddSpacer(5);
buttonSizer->Add(restartButton_, 0, wxEXPAND | wxALIGN_RIGHT);
buttonSizer->AddStretchSpacer(1);
buttonSizer->AddSpacer(5);
buttonSizer->AddStretchSpacer(1);
buttonSizer->Add(saveButton_, 0, wxEXPAND);
buttonSizer->AddSpacer(5);
buttonSizer->Add(viewButton_, 0, wxEXPAND);
buttonSizer->AddSpacer(5);
panelSizer_->Add(buttonSizer, 0, wxEXPAND, 50);
panelSizer_->AddSpacer(3);
dialogSizer_ = new wxBoxSizer(wxHORIZONTAL);
dialogSizer_->Add(mainPanel_);
mainPanel_->SetSizerAndFit(panelSizer_);
SetSizerAndFit(dialogSizer_);
wxSize windowSize = GetSize();
this->SetPosition(wxPoint(center.x - (windowSize.x / 2),
center.y - (windowSize.y / 2)));
Connect(this->GetId(), wxEVT_CLOSE_WINDOW,
wxCommandEventHandler(ResultsDialog::onClose));
Connect(saveButton_->GetId(), wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(ResultsDialog::onSaveReplay));
Connect(viewButton_->GetId(), wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(ResultsDialog::onViewReplay));
Connect(restartButton_->GetId(), wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(ResultsDialog::onRestart));
eventFilter_ = new ResultsEventFilter(this);
this->GetEventHandler()->AddFilter(eventFilter_);
}
ResultsDialog::~ResultsDialog() {
this->GetEventHandler()->RemoveFilter(eventFilter_);
delete eventFilter_;
delete stageName_;
delete fileManager_;
delete systemExecutor_;
if (replayFilename_ != 0) {
delete replayFilename_;
}
if (timestamp_ != 0) {
delete timestamp_;
}
delete replayBuilder_; // inherited from the engine
if (listener_ != 0) {
delete listener_;
}
}
void ResultsDialog::onClose(wxCommandEvent &event) {
Hide();
}
void ResultsDialog::onSaveReplay(wxCommandEvent &event) {
saveReplay();
}
void ResultsDialog::onViewReplay(wxCommandEvent &event) {
viewReplay();
}
void ResultsDialog::onRestart(wxCommandEvent &event) {
restart();
}
void ResultsDialog::saveReplay() {
if (!savedReplay_) {
replayFilename_ = generateFilename();
wxDateTime dateTime;
dateTime.SetToCurrent();
if (timestamp_ != 0) {
replayBuilder_->setTimestamp(timestamp_);
}
replayBuilder_->saveReplay(replayFilename_);
displayFilename(replayFilename_);
savedReplay_ = true;
saveButton_->Disable();
}
}
void ResultsDialog::viewReplay() {
saveReplay();
systemExecutor_->openHtmlFile(replayFilename_);
}
void ResultsDialog::restart() {
if (listener_ != 0) {
listener_->onRestart();
}
}
char* ResultsDialog::generateFilename() {
char *absFilename = 0;
do {
if (absFilename != 0) {
delete absFilename;
}
char *filename = newFilename();
absFilename = fileManager_->getFilePath(getReplaysDir().c_str(), filename);
delete filename;
} while (fileManager_->fileExists(absFilename));
return absFilename;
}
char* ResultsDialog::newFilename() {
std::string filename(stageName_);
filename.append("-");
wxDateTime dateTime;
dateTime.SetToCurrent();
std::string timestamp(dateTime.Format("%Y.%m.%d-%H.%M.%S"));
filename.append(timestamp);
filename.append(".html");
if (timestamp_ != 0) {
delete timestamp_;
}
timestamp_ = new char[timestamp.length() + 1];
strcpy(timestamp_, timestamp.c_str());
char *newFilename = new char[filename.length() + 1];
strcpy(newFilename, filename.c_str());
return newFilename;
}
void ResultsDialog::displayFilename(const char *filename) {
char *displayName;
if (strlen(filename) > MAX_DISPLAY_NAME_LENGTH) {
displayName = new char[MAX_DISPLAY_NAME_LENGTH + 1];
int first = ((MAX_DISPLAY_NAME_LENGTH - 3) / 6);
int last = MAX_DISPLAY_NAME_LENGTH - 3 - first;
strncpy(displayName, filename, first);
displayName[first] = displayName[first + 1] = displayName[first + 2] = '.';
strncpy(
&(displayName[first + 3]), &(filename[strlen(filename) - last]), last);
displayName[MAX_DISPLAY_NAME_LENGTH] = '\0';
} else {
displayName = new char[strlen(filename) + 1];
strcpy(displayName, filename);
}
wxBoxSizer *saveSizer = new wxBoxSizer(wxHORIZONTAL);
wxStaticText *replayFilenameText = new wxStaticText(
mainPanel_, wxID_ANY, wxString::Format("Saved: %s", displayName));
saveSizer->AddSpacer(3);
saveSizer->Add(replayFilenameText);
saveSizer->AddSpacer(3);
panelSizer_->Add(saveSizer, 0, wxALIGN_CENTER);
panelSizer_->AddSpacer(3);
mainPanel_->SetSizerAndFit(panelSizer_);
SetSizerAndFit(dialogSizer_);
}
void ResultsDialog::setMnemonicLabels(bool modifierDown) {
// TODO: I'd rather it look like the button was pressed when you hit the
// shortcut, if possible. For now having trouble figuring out the
// wxButton::Command() call.
if (modifierDown) {
#ifdef __WXOSX__
restartButton_->SetLabel("&Restart \u2318R");
saveButton_->SetLabel("&Save Replay \u2318S");
viewButton_->SetLabel("&View Replay \u2318V");
#else
restartButton_->SetLabel("&Restart alt-R");
saveButton_->SetLabel("&Save Replay alt-S");
viewButton_->SetLabel("&View Replay alt-V");
#endif
} else {
restartButton_->SetLabel(" &Restart ");
saveButton_->SetLabel(" &Save Replay ");
viewButton_->SetLabel(" &View Replay (HTML) ");
}
}
ResultsEventFilter::ResultsEventFilter(ResultsDialog *resultsDialog) {
resultsDialog_ = resultsDialog;
}
ResultsEventFilter::~ResultsEventFilter() {
}
int ResultsEventFilter::FilterEvent(wxEvent& event) {
bool modifierDown = false;
wxKeyEvent *keyEvent = ((wxKeyEvent*) &event);
#if defined(__WXOSX__)
modifierDown = keyEvent->ControlDown();
#elif defined(__WINDOWS__)
modifierDown = keyEvent->AltDown();
#endif
const wxEventType type = event.GetEventType();
if (resultsDialog_->IsActive() && type == wxEVT_KEY_DOWN) {
resultsDialog_->setMnemonicLabels(modifierDown);
wxKeyEvent *keyEvent = ((wxKeyEvent*) &event);
int keyCode = keyEvent->GetKeyCode();
if (keyCode == WXK_ESCAPE
|| (keyEvent->GetUnicodeKey() == 'W' && modifierDown)) {
resultsDialog_->Close();
return Event_Processed;
} else if (keyCode == WXK_BACK
|| (keyEvent->GetUnicodeKey() == 'R' && modifierDown)) {
resultsDialog_->restart();
return Event_Processed;
}
}
if (type == wxEVT_KEY_UP) {
resultsDialog_->setMnemonicLabels(modifierDown);
} else if (type == wxEVT_KILL_FOCUS) {
resultsDialog_->setMnemonicLabels(false);
}
return Event_Skip;
}