forked from Voidious/BerryBots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputconsole.cpp
272 lines (235 loc) · 8.05 KB
/
outputconsole.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
/*
Copyright (C) 2012-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 <algorithm>
#include <wx/wx.h>
#include "bbwx.h"
#include "ResourcePath.hpp"
#include "outputconsole.h"
OutputConsole::OutputConsole(const char *title, int style,
MenuBarMaker *menuBarMaker)
: wxFrame(NULL, wxID_ANY, title, wxPoint(50, 50), wxDefaultSize,
wxDEFAULT_FRAME_STYLE) {
menuBarMaker_ = menuBarMaker;
outerSizer_ = new wxBoxSizer(wxVERTICAL);
output_ = new wxTextCtrl(this, wxID_ANY, "", wxPoint(0, 0),
wxSize(850, 550), wxTE_MULTILINE | wxTE_READONLY | wxTE_DONTWRAP,
wxDefaultValidator);
outerSizer_->Add(output_, 1, wxEXPAND);
style_ = style;
gfxCheckBox_ = 0;
abortButton_ = 0;
if (style != CONSOLE_PLAIN) {
wxBoxSizer *bottomSizer = new wxBoxSizer(wxHORIZONTAL);
wxPanel *bottomPanel = new wxPanel(this);
bottomSizer->AddStretchSpacer(1);
if (style == CONSOLE_SHIP_STAGE) {
gfxCheckBox_ = new wxCheckBox(bottomPanel, wxID_ANY, "Enable Gfx");
bottomSizer->Add(gfxCheckBox_, 0, wxALIGN_RIGHT | wxALL, 4);
} else if (style == CONSOLE_RUNNER) {
// On Windows/Mac, it's not picking up the cmd/alt up to hide the hotkey,
// so just displaying it all the time for now.
#ifdef __WXOSX__
abortButton_ = new wxButton(bottomPanel, wxID_ANY, "Abo&rt \u2318R");
#elif defined(__WINDOWS__)
abortButton_ = new wxButton(bottomPanel, wxID_ANY, "Abo&rt alt-R");
#else
abortButton_ = new wxButton(bottomPanel, wxID_ANY, " Abo&rt ");
#endif
bottomSizer->Add(abortButton_, 0, wxALIGN_RIGHT | wxALL, 4);
}
bottomPanel->SetSizerAndFit(bottomSizer);
outerSizer_->Add(bottomPanel, 0, wxEXPAND);
}
listener_ = 0;
menusInitialized_ = false;
#ifdef __WINDOWS__
SetIcon(wxIcon(resourcePath() + BERRYBOTS_ICO, wxBITMAP_TYPE_ICO));
#elif defined(__WXGTK__)
SetIcon(wxIcon(resourcePath() + BBICON_128, wxBITMAP_TYPE_PNG));
#endif
#ifdef __WXOSX__
defaultFontSize_ = 12;
#elif defined(__WINDOWS__)
defaultFontSize_ = 9;
#else
defaultFontSize_ = 10;
#endif
fontSize_ = defaultFontSize_;
output_->SetFont(wxFont(fontSize_, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL,
wxFONTWEIGHT_NORMAL));
SetSizerAndFit(outerSizer_);
Connect(this->GetId(), wxEVT_ACTIVATE,
wxActivateEventHandler(OutputConsole::onActivate));
Connect(this->GetId(), wxEVT_CLOSE_WINDOW,
wxCommandEventHandler(OutputConsole::onClose));
if (style == CONSOLE_SHIP_STAGE) {
Connect(gfxCheckBox_->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED,
wxCommandEventHandler(OutputConsole::onCheck));
} else if (style == CONSOLE_RUNNER) {
Connect(abortButton_->GetId(), wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(OutputConsole::onAbort));
}
eventFilter_ = new OutputConsoleEventFilter(this);
this->GetEventHandler()->AddFilter(eventFilter_);
}
OutputConsole::~OutputConsole() {
this->GetEventHandler()->RemoveFilter(eventFilter_);
delete eventFilter_;
if (listener_ != 0) {
delete listener_;
}
}
void OutputConsole::setListener(ConsoleListener *listener) {
if (listener_ != 0) {
delete listener_;
}
listener_ = listener;
}
void OutputConsole::onActivate(wxActivateEvent &event) {
if (!menusInitialized_) {
this->SetMenuBar(menuBarMaker_->getNewMenuBar());
menusInitialized_ = true;
outerSizer_->Layout();
Fit();
}
if (event.GetActive() && listener_ != 0) {
listener_->onActive();
}
if (gfxCheckBox_ != 0) {
gfxCheckBox_->SetFocus();
}
}
// This is the only wxWidgets dialog that needs to be drawn concurrently with
// the main SFML window. We get a lot of flickering in the main window if we
// don't override this.
void OutputConsole::OnEraseBackground(wxEraseEvent&) {
}
void OutputConsole::print(const char *text) {
output_->SetInsertionPointEnd();
output_->WriteText(text);
}
void OutputConsole::println(const char *text) {
output_->SetInsertionPointEnd();
output_->WriteText(text);
output_->WriteText("\n");
}
void OutputConsole::println() {
output_->SetInsertionPointEnd();
output_->WriteText("\n");
}
void OutputConsole::clear() {
output_->Clear();
}
void OutputConsole::onClose(wxCommandEvent &event) {
Hide();
if (listener_ != 0) {
listener_->onClose();
}
}
void OutputConsole::onCheck(wxCommandEvent &event) {
if (listener_ != 0 && gfxCheckBox_ != 0) {
listener_->onCheck(gfxCheckBox_->IsChecked());
}
}
bool OutputConsole::isChecked() {
return gfxCheckBox_->IsChecked();
}
void OutputConsole::onAbort(wxCommandEvent &event) {
abort();
}
void OutputConsole::abort() {
if (listener_ != 0 && abortButton_ != 0) {
listener_->onAbort();
}
}
// TODO: delta based on current text size, eg 36 => 42, not 38
void OutputConsole::increaseTextSize() {
fontSize_ += 2;
wxFont font = output_->GetFont();
font.SetPointSize(fontSize_);
output_->SetFont(font);
}
void OutputConsole::decreaseTextSize() {
fontSize_ = std::max(4, fontSize_ -2);
wxFont font = output_->GetFont();
font.SetPointSize(fontSize_);
output_->SetFont(font);
}
void OutputConsole::defaultTextSize() {
fontSize_ = defaultFontSize_;
wxFont font = output_->GetFont();
font.SetPointSize(fontSize_);
output_->SetFont(font);
}
int OutputConsole::getStyle() {
return style_;
}
void OutputConsole::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.
#ifdef __WXGTK__
if (abortButton_ != 0) {
if (modifierDown) {
abortButton_->SetLabel("Abo&rt alt-R");
} else {
abortButton_->SetLabel(" Abo&rt ");
}
}
#endif
}
OutputConsoleEventFilter::OutputConsoleEventFilter(
OutputConsole *outputConsole) {
outputConsole_ = outputConsole;
}
OutputConsoleEventFilter::~OutputConsoleEventFilter() {
}
int OutputConsoleEventFilter::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 (outputConsole_->IsActive() && type == wxEVT_KEY_DOWN) {
outputConsole_->setMnemonicLabels(modifierDown);
wxKeyEvent *keyEvent = ((wxKeyEvent*) &event);
int keyCode = keyEvent->GetKeyCode();
if ((outputConsole_->getStyle() != CONSOLE_RUNNER && keyCode == WXK_ESCAPE)
|| (keyEvent->GetUnicodeKey() == 'W' && keyEvent->ControlDown())) {
outputConsole_->Close();
return Event_Processed;
} else if (keyEvent->GetUnicodeKey() == '=' && keyEvent->ControlDown()) {
outputConsole_->increaseTextSize();
return Event_Processed;
} else if (keyEvent->GetUnicodeKey() == '-' && keyEvent->ControlDown()) {
outputConsole_->decreaseTextSize();
return Event_Processed;
} else if (keyEvent->GetUnicodeKey() == '0' && keyEvent->ControlDown()) {
outputConsole_->defaultTextSize();
return Event_Processed;
}
}
if (type == wxEVT_KEY_UP) {
outputConsole_->setMnemonicLabels(modifierDown);
} else if (type == wxEVT_KILL_FOCUS) {
outputConsole_->setMnemonicLabels(false);
}
return Event_Skip;
}