-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGUIApp.cpp
318 lines (230 loc) · 10.3 KB
/
GUIApp.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
//
// GUIApp.cpp
// Deep
//
// Created by Nathan Daly on 11/27/12.
// Copyright (c) 2012 Lions Entertainment. All rights reserved.
//
#include "GUIApp.h"
#include "GUIView.h"
#include "GUIWindow.h"
#include "GUIUtility.h"
#include "Compatibility.h"
#include <iostream>
#include TR1_FUNCTIONAL_H
using std::cout; using std::endl;
using std::list;
using std::tr1::bind;
using namespace std::tr1::placeholders;
using namespace GUIExceptionHandling;
namespace GUI {
// SINGLETON MEMBERS
App* App::singleton_ptr = 0;
App::App_destroyer App::the_App_destroyer;
App* App::get(){
if (!singleton_ptr) {
singleton_ptr = new App;
}
return singleton_ptr;
}
App::App_destroyer::~App_destroyer(){
delete App::singleton_ptr;
}
// Forward Declarations
void print_msg(const Error &e);
void unhandled_click(const Unhandled_Click &e);
// App Implementation:
void print_msg(const Error &e) {
cout << e.msg << endl;
}
void unhandled_click(const Unhandled_Click&) {
cout << "unhandled click" << endl;
}
struct App_Quitter {
App_Quitter(bool &running_) : running(running_) {}
void operator()(Quit) { running = false; }
bool &running;
};
App::App()
:window(0), fps_cap(FPS_CAP_DEFAULT), cap_frame_rate(true), running(false)
{ }
DispPoint App::get_screen_size() { return window->get_dim(); }
void App::quit() {
running = false;
}
void App::run(Window* window_) {
window = window_;
register_exception_handler<Error>(&print_msg);
running = true;
register_exception_handler<Quit>(App_Quitter(running));
window->refresh();
if (timer_commands.size())
next_timer_cmd = timer_commands.begin();
while(running) {
SDL_Event event;
try {
FrameRateCapper capper(fps_cap);
if (cap_frame_rate)
capper.cap_frame_rate();
while (SDL_PollEvent(&event) && running){
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEMOTION: {
// Send mouse event to correct view.
DispPoint click_pos(event.button.x, event.button.y);
DispPoint rel_pos(event.motion.xrel, event.motion.yrel);
list<Controller*> focus_copy(captured_focus.begin(), captured_focus.end());
for (list<Controller*>::iterator it = focus_copy.begin();
it != focus_copy.end(); ++it) {
Controller *captured = *it;
DispPoint new_pos(click_pos);
// If the Controller is a view, adjust pos for view.
if (View *view = dynamic_cast<View*>(captured)) {
new_pos.x -= view->get_abs_pos().x;
new_pos.y -= view->get_abs_pos().y;
}
bool handled;
if (event.button.button == SDL_BUTTON_X1) {
cout << "SIDEWAYS SCROLL!" << endl;
}
if (event.button.button == SDL_BUTTON_WHEELUP) {
if (event.button.type == SDL_MOUSEBUTTONDOWN) {
handled = captured->handle_mouse_scroll_start(true);
}
else if (event.button.type == SDL_MOUSEBUTTONUP) {
handled = captured->handle_mouse_scroll_stop(true);
}
}
else if (event.button.button == SDL_BUTTON_WHEELDOWN) {
if (event.button.type == SDL_MOUSEBUTTONDOWN) {
handled = captured->handle_mouse_scroll_start(false);
}
else if (event.button.type == SDL_MOUSEBUTTONUP) {
handled = captured->handle_mouse_scroll_stop(false);
}
}
else {
if (event.button.type == SDL_MOUSEBUTTONDOWN) {
handled = captured->handle_mouse_down(new_pos);
}
else if (event.button.type == SDL_MOUSEBUTTONUP) {
handled = captured->handle_mouse_up(new_pos);
}
else if (event.button.type == SDL_MOUSEMOTION) {
handled = captured->handle_mouse_motion(new_pos, rel_pos);
}
}
// if (!handled) {
// //...
// }
}
View* hovered_view =
window->get_main_view()->get_view_from_point(click_pos);
if (hovered_view) {
DispPoint new_pos(click_pos);
new_pos.x -= hovered_view->get_abs_pos().x;
new_pos.y -= hovered_view->get_abs_pos().y;
if (event.button.button == SDL_BUTTON_WHEELUP) {
if (event.button.type == SDL_MOUSEBUTTONDOWN) {
hovered_view->mouse_scroll_start(true);
}
else if (event.button.type == SDL_MOUSEBUTTONUP) {
hovered_view->mouse_scroll_stop(true);
}
}
else if (event.button.button == SDL_BUTTON_WHEELDOWN) {
if (event.button.type == SDL_MOUSEBUTTONDOWN) {
hovered_view->mouse_scroll_start(false);
}
else if (event.button.type == SDL_MOUSEBUTTONUP) {
hovered_view->mouse_scroll_stop(false);
}
}
else {
if (event.button.type == SDL_MOUSEBUTTONDOWN) {
hovered_view->mouse_down(new_pos);
}
else if (event.button.type == SDL_MOUSEBUTTONUP) {
hovered_view->mouse_up(new_pos);
}
else if (event.button.type == SDL_MOUSEMOTION) {
hovered_view->mouse_motion(new_pos, rel_pos);
}
}
}
break;
}
case SDL_KEYDOWN: {
cout << "KEYDOWN" << endl;
for (view_list_t::iterator it = captured_focus.begin();
it != captured_focus.end(); ++it) {
Controller *captured = *it;
bool handled = captured->handle_key_down(event.key.keysym);
if (!handled) {}
}
break;
}
case SDL_KEYUP: {
cout << "KEYUP" << endl;
// Quit Key
if (event.key.keysym.sym == SDLK_q){
#ifdef _MSC_VER // Windows
if (event.key.keysym.mod == KMOD_LCTRL
|| event.key.keysym.mod == KMOD_RCTRL
|| event.key.keysym.mod == KMOD_CTRL){
throw Quit();
}
#else // Mac
if (event.key.keysym.mod == KMOD_LMETA
|| event.key.keysym.mod == KMOD_RMETA
|| event.key.keysym.mod == KMOD_META){
throw Quit();
}
#endif
}
for (view_list_t::iterator it = captured_focus.begin();
it != captured_focus.end(); ++it) {
Controller *captured = *it;
bool handled = captured->handle_key_up(event.key.keysym);
if (!handled) {}
}
break;
}
case SDL_QUIT:
throw Quit();
break;
default:
break;
}
}
cycle_timer_commands();
}
catch(...) {
call_exception_handlers(handler_list.begin(), handler_list.end());
}
window->refresh();
}
}
void App::cycle_timer_commands() {
for (size_t i = 0; i < timer_commands.size(); i++) {
GUITimer_command *cmd = *next_timer_cmd;
if (++next_timer_cmd == timer_commands.end()) {
next_timer_cmd = timer_commands.begin();
}
cmd->execute_command();
}
}
void App::cancel_timer_op(GUITimer_command* op) {
std::vector<GUITimer_command*>::iterator it
= std::find(timer_commands.begin(), timer_commands.end(), op);
if (it != timer_commands.end()) {
delete *it;
timer_commands.erase(it);
}
else {
throw Error("command not found!");
}
next_timer_cmd = timer_commands.begin();
}
} // namespace GUI