-
Notifications
You must be signed in to change notification settings - Fork 6
/
GUIApp.h
180 lines (123 loc) · 4.69 KB
/
GUIApp.h
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
//
// GUIApp.h
// Deep
//
// Created by Nathan Daly on 11/27/12.
// Copyright (c) 2012 Lions Entertainment. All rights reserved.
//
#ifndef Deep_GUIApp_h
#define Deep_GUIApp_h
#include "GUIExceptionHandling.h"
#include "GUITimer.h"
#include "GUIUtility.h"
#include <vector>
#include <list>
#include <set>
#include "Compatibility.h" // to ensure "SDL.h" is included by main
namespace GUI {
using namespace GUIExceptionHandling;
const int FPS_CAP_DEFAULT = 40; // Frames per second
// Throw an instance of GUI::Quit to safely tell the application to exit.
// (This is the same as calling GUI::App::quit())
class Quit {};
struct GUITimer_command {
GUITimer_command(double interval_) : interval(interval_) {
timer.start();
}
double interval;
GUITimer timer;
virtual void execute_command(){}
};
class Window;
class Controller;
class App {
public:
static App* get();
// Frames per second
void set_framerate_cap(int fps_cap_) { fps_cap = fps_cap_; cap_frame_rate = true;}
void disable_framerate_cap() { cap_frame_rate = false; }
void run(Window* window);
// struct GUITimer_command;
// Perform op after interval seconds. Repeat if repeat == true.
// returns a ptr to the timer command, so that it may be canceled later.
template <typename Operation>
GUITimer_command* repeat_on_timer(Operation op, double interval, bool repeat = true);
// cancel a timer operation that was set to repeat.
void cancel_timer_op(GUITimer_command* op);
// When any code executed within the run() loop throws an instance of
// Exception_t, any Handler_t's that have been registered for that
// Exception_t will be called, with the exception passed as the argument.
template <typename Exception_t, typename Handler_t>
void register_exception_handler(const Handler_t &handler);
// Provides a Controller with the ability to receive mouse/keyboard input.
// (Views receive mouse input by defualt when hovered over.)
void give_focus(Controller* view) { captured_focus.insert(view); }
bool has_focus(Controller* view) { return captured_focus.count(view) != 0; }
void release_focus(Controller* view) { captured_focus.erase(view); }
// Equivalent to a user clicking the "x" or pressing cmd-q.
void quit();
DispPoint get_screen_size();
Window* get_window() { return window; }
private:
Window* window;
int fps_cap;
bool cap_frame_rate;
bool running;
typedef std::set<Controller*> view_list_t;
view_list_t captured_focus;
// vector because it's fast to iterate through!
typedef std::vector<ExceptionHandler*> error_handler_list_t;
error_handler_list_t handler_list;
typedef std::vector<GUITimer_command*> timer_cmnds_list_t;
timer_cmnds_list_t timer_commands;
template <typename Operation>
class GUITimer_command_impl : public GUITimer_command {
public:
GUITimer_command_impl(Operation oper_, double interval_, bool repeat_ = true)
: GUITimer_command(interval_), oper(oper_), repeat(repeat_), executed(false) { }
virtual void execute_command(){
if (timer.get_time() > (1000*interval)){
if (!repeat && executed) return;
oper();
timer.reset();
executed = true;
}
}
private:
Operation oper;
bool repeat;
bool executed;
};
template <typename Operation>
GUITimer_command* create_timer_command(Operation oper, double interval_, bool repeat_ = true){
return new GUITimer_command_impl<Operation>(oper, interval_, repeat_);
}
timer_cmnds_list_t::iterator next_timer_cmd;
void cycle_timer_commands();
//SINGLETON MEMBERS:
static App * singleton_ptr;
friend class GUIApp_destroyer;
// no public creation/deletion
App();
// no copy or assignment allowed
App(const App&);
App& operator= (const App&);
struct App_destroyer {
~App_destroyer();
};
static App_destroyer the_App_destroyer;
};
// Perform op after interval seconds. Repeat if repeat == true.
template <typename Operation>
GUITimer_command* App::repeat_on_timer(Operation op, double interval, bool repeat) {
GUITimer_command* command = create_timer_command(op,interval, repeat);
timer_commands.push_back(command);
next_timer_cmd = --timer_commands.end();
return command;
}
template <typename Exception_t, typename Handler_t>
void App::register_exception_handler(const Handler_t &handler) {
handler_list.push_back(create_exception_handler<Exception_t>(handler));
}
} // namespace GUI
#endif