-
Notifications
You must be signed in to change notification settings - Fork 6
/
GUIView.cpp
386 lines (276 loc) · 9.5 KB
/
GUIView.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//
// GUIView.cpp
// Deep
//
// Created by Nathan Daly on 9/10/12.
// Copyright (c) 2012 Lions Entertainment. All rights reserved.
//
#include "GUIView.h"
#include "GUIApp.h"
#include "GUIImage.h"
#include "Compatibility.h"
#include SDL_SDLVIDEO_H
#include "GameDisplay.h" // For pixel manip.
#include "GUIWindow.h" // For Unhandled Click.
#include "GUIApp.h" // For capture focus
#include <iostream>
#include <algorithm> // For std::find.
using namespace std;
const SDL_Color default_clear_color = {255,0,255,0};
namespace GUI {
static SDL_Surface* prepare_SDL_surface(int w, int h);
View::View(int w_, int h_)
:changed(false), w(w_), h(h_), image(prepare_SDL_surface(w_, h_)),
display(prepare_SDL_surface(w_, h_)), is_alpha(false),
parent(0)
{
}
SDL_Surface* prepare_SDL_surface(int w, int h) {
Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
SDL_Surface *temp = SDL_CreateRGBSurface(SDL_HWSURFACE, w, h, 32,
rmask, gmask, bmask, amask);
if(temp == NULL) {
throw Error("CreateRGBSurface failed: \n" + string(SDL_GetError()));
}
SDL_Surface* image = SDL_DisplayFormat(temp);
if(!image) {
throw Error("updateimage in CreateRGBSurface failed: \n" + string(SDL_GetError()));
}
SDL_FreeSurface(temp);
return image;
}
View::~View() {
while (!children.empty()) {
delete children.front();
children.pop_front();
}
SDL_FreeSurface(image);
SDL_FreeSurface(display);
}
void View::draw_onto_self(const GUIImage &image_, DispPoint pos) {
mark_changed();
// Using SDL, perform a blit from view to self.
SDL_Rect dest_rect = {pos.x, pos.y, image_->w, image_->h};
SDL_BlitSurface(image_, 0, image, &dest_rect);
}
void View::fill_with_color(SDL_Color color) {
draw_onto_self(GUIImage::create_filled(w, h, color), DispPoint());
}
// Draws image onto display.
void View::render_image(SDL_Surface* source, int w, int h, DispPoint pos) {
// Using SDL, perform a blit from view to self.
SDL_Rect dest_rect = {pos.x, pos.y, w, h};
SDL_BlitSurface(source, 0, display, &dest_rect);
}
void View::set_clear_color(SDL_Color clear_color_) {
is_alpha = true;
clear_color = clear_color_;
Uint32 colorkey = SDL_MapRGBA(image->format, clear_color.r, clear_color.g, clear_color.b, clear_color.unused);
SDL_SetColorKey(display, SDL_SRCCOLORKEY, colorkey); // reset alpha
}
void View::clear_alpha() {
is_alpha = false;
SDL_SetColorKey(display, 0, 0); // reset alpha
}
bool View::point_is_clear(DispPoint coord) const {
if (!rel_point_is_on_me(coord) || !has_alpha_color()) {
return true;
}
Uint32 pixel = getpixel(image, coord.x, coord.y);
if (pixel == SDL_MapRGB(image->format, clear_color.r, clear_color.g, clear_color.b)) {
return true;
}
return false;
}
bool x_then_y_view_less_than(const View* a, const View* b) {
if (a->pos.x < b->pos.x) return true;
else if (a->pos.x == b->pos.x) return (a->pos.y < b->pos.y);
else /*(a->pos.x > b->pos.x)*/ return false;
}
void View::mark_changed() {
changed = true;
if (parent) parent->mark_changed();
if (children.size() > 1) {
/// @todo Perhaps the sorting method should be optional?
/// Either by x,y or by order attached?
// children.sort(x_then_y_view_less_than);
}
}
void View::refresh() {
if (!need_to_refresh()) return;
// Refresh self. (First display image, then each child.)
render_image(image, w, h, DispPoint(0,0));
Subview_list_t::iterator child;
for(child = children.begin(); child != children.end(); child++) {
(*child)->refresh();
render_image((*child)->display, (*child)->w, (*child)->h, (*child)->pos);
}
changed = false;
}
void View::attach_subview(View* view, DispPoint pos) {
if (view->parent)
throw Error("Candidate vew is already a subview of another view.");
if (view == this)
throw Error("Cannot attach a view to itself!");
/// @todo Check if out of bounds? Or maybe not..?
view->pos = pos;
children.push_back(view);
view->parent = this;
mark_changed();
}
// NOTE: Does not delete the view, only removes it from list!
void View::remove_subview(View* view) {
if (!is_subview(view))
throw Error("view is not a subview of this!");
children.remove(view);
view->parent = 0;
mark_changed();
}
View* View::remove_last_subview() {
if (children.empty())
throw Error("view has not subviews!");
View *view = children.back();
children.pop_back();
view->parent = 0;
mark_changed();
return view;
}
bool View::is_subview(View* view) const {
return (find(children.begin(), children.end(), view) != children.end());
}
void View::move_subview(View* view, DispPoint pos) {
if (!is_subview(view))
throw Error("view is not a subview of this!");
if (view->pos == pos) return; // no need to mark change if already there!
view->pos = pos;
mark_changed();
}
void View::mouse_down(DispPoint coord) {
cout << "mouse down!: " << coord.x <<", "<< coord.y << endl;
if (!handle_mouse_down(coord)) {
if (parent) parent->mouse_down(coord + pos);
// else throw Unhandled_Click(coord);
}
}
void View::mouse_up(DispPoint coord) {
cout << "mouse up!: " << coord.x <<", "<< coord.y << endl;
if (!handle_mouse_up(coord)) {
if (parent) parent->mouse_up(coord + pos);
// else throw Unhandled_Click(coord);
}
}
void View::mouse_motion(DispPoint coord, DispPoint rel_motion) {
// cout << "mouse motion!: " << rel_motion.x <<", "<< rel_motion.y << endl;
if (!handle_mouse_motion(coord, rel_motion)) {
if (parent) parent->mouse_motion(coord + pos, rel_motion);
// else throw Unhandled_Click(coord);
}
}
void View::mouse_scroll_start(bool up_down) {
cout << "mouse scroll!: " << (up_down ? "up" : "down") << endl;
if (!handle_mouse_scroll_start(up_down)) {
if (parent) parent->mouse_scroll_start(up_down);
// else throw Unhandled_Click(coord);
}
}void View::mouse_scroll_stop(bool up_down) {
cout << "mouse scroll!: " << (up_down ? "up" : "down") << endl;
if (!handle_mouse_scroll_stop(up_down)) {
if (parent) parent->mouse_scroll_stop(up_down);
// else throw Unhandled_Click(coord);
}
}
void View::key_down(SDL_keysym key) {
cout << "key down!: " << key.sym << endl;
if (!handle_key_down(key)) {
// throw Unhandled_Key(key);
}
}
void View::key_up(SDL_keysym key) {
cout << "key up!: " << key.sym << endl;
if (!handle_key_up(key)) {
// throw Unhandled_Key(key);
}
}
View* View::get_view_from_point(DispPoint coord) const {
// Can we cache this? Would that be faster?
coord = adjust_to_rel(coord);
if (!rel_point_is_on_me(coord)) return 0;
// At worst, we know the point is on this view.
const View* result = this;
// Check if any children have a deeper subview:
Subview_list_t::const_iterator child;
for (child = children.begin(); child != children.end(); ++child) {
// Can assume that Views are sorted, so any new best will be above old best.
View* new_best = (*child)->get_view_from_point(coord);
if (new_best) {
result = new_best;
}
}
return const_cast<View*>(result);
}
bool View::rel_point_is_on_me(DispPoint coord) const {
return (coord.x >= 0 && coord.y >= 0
&& coord.x < w && coord.y < h);
}
//bool View::rel_point_is_on_me(DispPoint coord) {
//
// return (coord.x >= pos.x && coord.y >= pos.y
// && coord.x < pos.x + w && coord.y < pos.y + h);
//}
bool View::abs_point_is_on_me(DispPoint coord) const {
DispPoint abs_pos = get_abs_pos();
return (coord.x >= abs_pos.x && coord.y >= abs_pos.y
&& coord.x < abs_pos.x + w && coord.y < abs_pos.y + h);
}
DispPoint View::abs_from_rel(DispPoint coord) const {
DispPoint abs_pos = get_abs_pos();
return DispPoint(abs_pos.x + coord.x,
abs_pos.y + coord.y);
}
DispPoint View::adjust_to_parent(DispPoint coord) const {
return DispPoint(coord.x + pos.x, coord.y + pos.y);
}
DispPoint View::adjust_to_rel(DispPoint coord) const {
return DispPoint(coord.x - pos.x, coord.y - pos.y);
}
DispPoint View::get_abs_pos() const {
if (parent == 0) return pos;
else {
DispPoint parent_abs_pos = parent->get_abs_pos();
return DispPoint(parent_abs_pos.x + pos.x,
parent_abs_pos.y + pos.y);
}
}
DispPoint View::get_rel_pos() const {
return pos;
}
void View::resize(int w_, int h_) {
if (w == w_ && h == h_) {
did_resize(w_,h_);
return;
}
w = w_; h = h_;
View temp(w,h);
std::swap(image, temp.image);
std::swap(display, temp.display);
if (is_alpha) {
set_clear_color(clear_color);
fill_with_color(clear_color);
}
SDL_Rect dest_rect = {0, 0, w, h};
SDL_BlitSurface(temp.image, 0, image, &dest_rect);
mark_changed();
did_resize(w_,h_);
}
} // namespace GUI