-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbbguimain.cpp
More file actions
270 lines (231 loc) · 9.13 KB
/
Copy pathbbguimain.cpp
File metadata and controls
270 lines (231 loc) · 9.13 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
/*
Copyright (C) 2012-2015 - 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 <iostream>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sstream>
#include <math.h>
#include <SFML/Graphics.hpp>
#include <wx/wx.h>
#include "bbwx.h"
#include "stage.h"
#include "bbengine.h"
#include "printhandler.h"
#include "guimanager.h"
#include "bbutil.h"
#include "basedir.h"
#include "filemanager.h"
#include "sysexec.h"
using namespace std;
PrintHandler *printHandler = 0;
class BerryBotsApp : public wxApp {
GuiManager *guiManager_;
GuiListener *guiListener_;
FileManager *fileManager_;
SystemExecutor *systemExecutor_;
public:
virtual bool OnInit();
virtual void OnQuit(wxCommandEvent &event);
void quit();
virtual void OnNewMatch(wxCommandEvent &event);
virtual void OnPackageShip(wxCommandEvent &event);
virtual void OnPackageStage(wxCommandEvent &event);
virtual void OnGameRunner(wxCommandEvent &event);
virtual void OnErrorConsole(wxCommandEvent &event);
virtual void MacReopenApp();
void onChangeBaseDir(wxCommandEvent &event);
void onBrowseStages(wxCommandEvent &event);
void onBrowseShips(wxCommandEvent &event);
void onBrowseRunners(wxCommandEvent &event);
void onBrowseReplays(wxCommandEvent &event);
void onBrowseApidocs(wxCommandEvent &event);
private:
std::string dirName(const char *name);
};
class AppGuiListener : public GuiListener {
BerryBotsApp *app_;
public:
AppGuiListener(BerryBotsApp *app);
~AppGuiListener();
void onAllWindowsClosed();
};
wxIMPLEMENT_APP(BerryBotsApp);
bool BerryBotsApp::OnInit() {
// Needed for stage previews and dialog/button icons.
wxImage::AddHandler(new wxPNGHandler);
#ifdef __WXOSX__
// On OS X, it complains if we initialize our first SFML window after the
// wxWidgets windows have set their menu bars or after the base dir selector,
// so initialize one here first.
sf::RenderWindow *window = new sf::RenderWindow(
sf::VideoMode(800, 600), "BerryBots", sf::Style::Default,
sf::ContextSettings(0, 0, 0, 2, 0));
delete window;
#endif
#ifndef __WINDOWS__
// On Mac OS X and Linux, if a base directory has not yet been selected, show
// an informational dialog before the file chooser comes up.
if (!isConfigured()) {
std::stringstream configInfo;
configInfo << "TLDR: Before you can use BerryBots, you need to select a "
<< "base " << DIRECTORY << ". This is where BerryBots will store "
<< "files like ships and stages."
<< std::endl << std::endl
#ifdef __WXGTK__
<< "~/Documents/BerryBots is a reasonable choice."
#else
<< "Documents > BerryBots is a reasonable choice."
#endif
<< std::endl << std::endl
<< "--------"
<< std::endl << std::endl
<< "After selecting a " << DIRECTORY << ", sub" << DIRECTORIES
<< " will be created for ships (" << dirName("bots") << "), "
<< "stages (" << dirName("stages") << "), and Game Runners "
<< "(" << dirName("runners") << "). The samples will be copied "
<< "into these " << DIRECTORIES << ". Place your own programs "
<< "in them, too."
<< std::endl << std::endl
#ifdef __WXOSX__
// On Linux, we use the read-only copy in /usr/share/berrybots.
<< "The BerryBots Lua API documentation will be copied into the "
<< dirName("apidoc") << " sub" << DIRECTORY << "."
<< std::endl << std::endl
#endif
<< "As needed, BerryBots will also create a cache sub"
<< DIRECTORY << " (" << dirName("cache") << ") for unpackaged "
<< "ships and stages, a replays sub" << DIRECTORY << " ("
<< dirName("replays") << "), and a temp sub" << DIRECTORY << " ("
<< dirName(".tmp") << ") for working files used by BerryBots."
<< std::endl << std::endl
<< "Have fun!";
wxMessageDialog selectBaseDirMessage(NULL, configInfo.str(),
"BerryBots Setup", wxOK);
if (selectBaseDirMessage.ShowModal() != wxID_OK) {
return false;
}
}
#endif
fileManager_->recursiveDelete(getTmpDir().c_str());
guiListener_ = new AppGuiListener(this);
guiManager_ = new GuiManager(guiListener_);
fileManager_ = new FileManager();
systemExecutor_ = new SystemExecutor();
Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(BerryBotsApp::OnQuit));
Connect(NEW_MATCH_MENU_ID, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(BerryBotsApp::OnNewMatch));
Connect(PACKAGE_SHIP_MENU_ID, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(BerryBotsApp::OnPackageShip));
Connect(PACKAGE_STAGE_MENU_ID, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(BerryBotsApp::OnPackageStage));
Connect(GAME_RUNNER_MENU_ID, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(BerryBotsApp::OnGameRunner));
Connect(ERROR_CONSOLE_MENU_ID, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(BerryBotsApp::OnErrorConsole));
#ifndef __WXOSX__
Connect(FILE_QUIT_MENU_ID, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(BerryBotsApp::OnQuit));
#endif
Connect(CHANGE_BASE_DIR_MENU_ID, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(BerryBotsApp::onChangeBaseDir));
Connect(BROWSE_SHIPS_MENU_ID, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(BerryBotsApp::onBrowseShips));
Connect(BROWSE_STAGES_MENU_ID, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(BerryBotsApp::onBrowseStages));
Connect(BROWSE_RUNNERS_MENU_ID, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(BerryBotsApp::onBrowseRunners));
Connect(BROWSE_REPLAYS_MENU_ID, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(BerryBotsApp::onBrowseReplays));
Connect(BROWSE_API_DOCS_MENU_ID, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(BerryBotsApp::onBrowseApidocs));
return true;
}
void BerryBotsApp::OnQuit(wxCommandEvent &event) {
quit();
}
void BerryBotsApp::quit() {
guiManager_->quit();
ExitMainLoop();
delete guiListener_;
// TODO: delete guiManager_ without clobbering anyone else
}
void BerryBotsApp::OnNewMatch(wxCommandEvent &event) {
guiManager_->showNewMatchDialog();
guiManager_->newMatchInitialFocus();
}
void BerryBotsApp::OnPackageShip(wxCommandEvent &event) {
guiManager_->showPackageShipDialog();
guiManager_->packageShipInitialFocus();
}
void BerryBotsApp::OnPackageStage(wxCommandEvent &event) {
guiManager_->showPackageStageDialog();
guiManager_->packageStageInitialFocus();
}
void BerryBotsApp::OnGameRunner(wxCommandEvent &event) {
guiManager_->showGameRunnerDialog();
guiManager_->gameRunnerInitialFocus();
}
void BerryBotsApp::OnErrorConsole(wxCommandEvent &event) {
guiManager_->showErrorConsole();
}
void BerryBotsApp::MacReopenApp() {
// No-op - When SFML window is only one open and user switches to app via icon
// in Mac OS X dock, this stops wxWidgets from opening one of its windows
// unnecessarily.
}
void BerryBotsApp::onChangeBaseDir(wxCommandEvent &event) {
chooseNewRootDir();
guiManager_->reloadBaseDirs();
guiManager_->loadStages();
guiManager_->loadShips();
guiManager_->loadRunners();
}
void BerryBotsApp::onBrowseStages(wxCommandEvent &event) {
systemExecutor_->browseDirectory(getStagesDir().c_str());
}
void BerryBotsApp::onBrowseShips(wxCommandEvent &event) {
systemExecutor_->browseDirectory(getShipsDir().c_str());
}
void BerryBotsApp::onBrowseRunners(wxCommandEvent &event) {
systemExecutor_->browseDirectory(getRunnersDir().c_str());
}
void BerryBotsApp::onBrowseReplays(wxCommandEvent &event) {
fileManager_->createDirectoryIfNecessary(getReplaysDir().c_str());
systemExecutor_->browseDirectory(getReplaysDir().c_str());
}
void BerryBotsApp::onBrowseApidocs(wxCommandEvent &event) {
systemExecutor_->openHtmlFile(getApidocPath().c_str());
}
std::string BerryBotsApp::dirName(const char *name) {
std::stringstream dirName;
#ifdef __WXGTK__
dirName << name << "/";
#else
dirName << "\"" << name << "\"";
#endif
return dirName.str();
}
AppGuiListener::AppGuiListener(BerryBotsApp *app) {
app_ = app;
}
AppGuiListener::~AppGuiListener() {
}
void AppGuiListener::onAllWindowsClosed() {
app_->quit();
}