ScummVM API documentation
events.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef GRAPHICS_VIEWS_EVENTS_H
23#define GRAPHICS_VIEWS_EVENTS_H
24
25#include "common/array.h"
26#include "common/random.h"
27#include "common/stack.h"
28#include "graphics/font.h"
29#include "graphics/views/gfx_surface.h"
30#include "graphics/views/messages.h"
31#include "graphics/views/rect.h"
32#include "graphics/screen.h"
33
34namespace Graphics {
35namespace Views {
36
37class Events;
38
44struct Bounds {
45private:
46 Common::Rect _bounds;
47 Common::Rect &_innerBounds;
48 int _borderSize = 0;
49
50public:
51 const int16 &left;
52 const int16 &top;
53 const int16 &right;
54 const int16 &bottom;
55
56public:
57 Bounds(Common::Rect &innerBounds);
58 operator const Common::Rect &() const {
59 return _bounds;
60 }
61 Bounds &operator=(const Common::Rect &r);
62 void setBorderSize(size_t borderSize);
63 size_t borderSize() const {
64 return _borderSize;
65 }
66 int16 width() const {
67 return _bounds.width();
68 }
69 int16 height() const {
70 return _bounds.height();
71 }
72 bool contains(const Common::Rect &r) const {
73 return _bounds.contains(r);
74 }
75};
76
80class UIElement {
81 friend class Events;
82
83private:
84 int _timeoutCtr = 0;
85
86protected:
87 UIElement *_parent;
89 Common::Rect _innerBounds;
90 Bounds _bounds;
91 bool _needsRedraw = true;
92 Common::String _name;
93#ifdef USE_TTS
94 Common::String _previousSaid;
95#endif
96
97protected:
101 void delaySeconds(uint seconds);
102
106 void delayFrames(uint frames);
107
111 bool isDelayActive() const {
112 return _timeoutCtr != 0;
113 }
114
118 void cancelDelay() {
119 _timeoutCtr = 0;
120 }
121
125 virtual void timeout();
126
127private:
132 virtual void drawElements();
133
137 static UIElement *findViewGlobally(const Common::String &name);
138
139public:
140 UIElement(const Common::String &name, UIElement *uiParent);
141 UIElement(const Common::String &name);
142 virtual ~UIElement() {
143 }
144
148 bool needsRedraw() const {
149 return _needsRedraw;
150 }
151
155 void redraw();
156
161 virtual void close();
162
166 virtual void replaceView(UIElement *ui, bool replaceAllViews = false);
167 virtual void replaceView(const Common::String &name, bool replaceAllViews = false);
168
172 virtual void addView(UIElement *ui);
173 virtual void addView(const Common::String &name);
174 void addView();
175 void open() {
176 addView();
177 }
178
182 int getRandomNumber(int minNumber, int maxNumber);
183 int getRandomNumber(int maxNumber);
184
188 virtual void setBounds(const Common::Rect &r) {
189 _bounds = r;
190 }
191
196 return _bounds;
197 }
198
202 const Common::String &getName() const {
203 return _name;
204 }
205
209 GfxSurface getSurface(bool innerBounds = false) const;
210
214 virtual void draw();
215
219 virtual bool tick();
220
224 virtual UIElement *findView(const Common::String &name);
225
229 // Mouse move only has a minimal implementation for performance reasons
230protected:
231 virtual bool msgMouseMove(const MouseMoveMessage &msg) {
232 return false;
233 }
234
235#ifdef USE_TTS
240#endif
241
242public:
243 bool send(const MouseMoveMessage &msg) {
244 return msgMouseMove(msg);
245 }
246
247#define MESSAGE(NAME) \
248protected: \
249 virtual bool msg##NAME(const NAME##Message &e) { \
250 for (Common::Array<UIElement *>::iterator it = _children.begin(); \
251 it != _children.end(); ++it) { \
252 if ((*it)->msg##NAME(e)) \
253 return true; \
254 } \
255 return false; \
256 } \
257 \
258public: \
259 bool send(const Common::String &viewName, const NAME##Message &msg) { \
260 UIElement *view = UIElement::findViewGlobally(viewName); \
261 assert(view); \
262 return view->msg##NAME(msg); \
263 } \
264 bool send(const NAME##Message &msg) { \
265 return msg##NAME(msg); \
266 }
267
268 MESSAGE(Focus);
269 MESSAGE(Unfocus);
270 MESSAGE(MouseEnter);
271 MESSAGE(MouseLeave);
272 MESSAGE(Keypress);
273 MESSAGE(MouseDown);
274 MESSAGE(MouseUp);
275 MESSAGE(Action);
276 MESSAGE(Game);
277 MESSAGE(Value);
278#undef MESSAGE
279
280protected:
288 virtual bool msgCustom(const Message &msg) {
289 for (Common::Array<UIElement *>::iterator it = _children.begin();
290 it != _children.end(); ++it) {
291 if ((*it)->msgCustom(msg))
292 return true;
293 }
294 return false;
295 }
296
297public:
305 template<class T>
306 bool send(const T &msg) {
307 return msgCustom(msg);
308 }
309 template<class T>
310 bool send(const Common::String &viewName, const T &msg) {
311 UIElement *view = UIElement::findViewGlobally(viewName);
312 assert(view);
313 return view->msgCustom(msg);
314 }
315};
316
324class Events : public UIElement {
325private:
326 Graphics::Screen *_screen = nullptr;
327 Common::RandomSource _randomSource;
329 uint _frameRate;
330 uint _nextFrameTime = 0;
331
332 void nextFrame();
333
337 void processEvent(Common::Event &ev);
338
339protected:
343 virtual bool shouldQuit() const = 0;
344
348#define MESSAGE(NAME) \
349 bool msg##NAME(const NAME##Message &e) override { \
350 return !_views.empty() ? focusedView()->msg##NAME(e) : false; \
351 }
352 MESSAGE(Action);
353 MESSAGE(Focus);
354 MESSAGE(Unfocus);
355 MESSAGE(MouseEnter);
356 MESSAGE(MouseLeave);
357 MESSAGE(Keypress);
358 MESSAGE(MouseDown);
359 MESSAGE(MouseUp);
360 MESSAGE(MouseMove);
361#undef MESSAGE
362
366 bool msgCustom(const Message &msg) override {
367 return !_views.empty() ? focusedView()->msgCustom(msg) : false;
368 }
369
370public:
374 Events(const Common::String &engineName, uint frameRate = 20);
375 virtual ~Events();
376
380 uint getFrameRate() const {
381 return _frameRate;
382 }
383
387 void runGame();
388
394
398 int getRandomNumber(int minNumber, int maxNumber);
399 int getRandomNumber(int maxNumber);
400
404 void replaceView(UIElement *ui, bool replaceAllViews = false) override;
405 void replaceView(const Common::String &name, bool replaceAllViews = false) override;
406
410 void addView(UIElement *ui) override;
411 void addView(const Common::String &name) override;
412
417
421 void popView();
422
427 return _views.empty() ? nullptr : _views.top();
428 }
429
434 return _views.size() < 2 ? nullptr : _views[_views.size() - 2];
435 }
436
441 return _views.empty() ? nullptr : _views[0];
442 }
443
448 bool isPresent(const Common::String &name) const;
449
454 return _screen;
455 }
456
460 void drawElements() override;
461
465 void draw() override {
466 }
467
471 bool tick() override {
472 return !_views.empty() ? focusedView()->tick() : false;
473 }
474
478 void close() override {
479 focusedView()->close();
480 }
481
486
490 virtual Graphics::Font *createFont() const;
491};
492
493extern Events *g_events;
494
495} // namespace Views
496} // namespace Graphics
497
498#endif
Definition: array.h:52
iterator end()
Definition: array.h:380
T * iterator
Definition: array.h:54
iterator begin()
Definition: array.h:375
Definition: random.h:44
Definition: stack.h:102
Definition: str.h:59
Definition: font.h:83
Definition: screen.h:47
Definition: events.h:324
UIElement * priorView() const
Definition: events.h:433
uint getFrameRate() const
Definition: events.h:380
void drawElements() override
bool tick() override
Definition: events.h:471
UIElement * focusedView() const
Definition: events.h:426
Graphics::Screen * getScreen() const
Definition: events.h:453
void replaceView(UIElement *ui, bool replaceAllViews=false) override
virtual bool shouldQuit() const =0
int getRandomNumber(int minNumber, int maxNumber)
void draw() override
Definition: events.h:465
Events(const Common::String &engineName, uint frameRate=20)
virtual Graphics::Font * createFont() const
UIElement * baseView() const
Definition: events.h:440
void close() override
Definition: events.h:478
bool msgCustom(const Message &msg) override
Definition: events.h:366
void addView(UIElement *ui) override
bool isPresent(const Common::String &name) const
Definition: gfx_surface.h:34
Definition: events.h:80
virtual bool msgCustom(const Message &msg)
Definition: events.h:288
const Common::String & getName() const
Definition: events.h:202
GfxSurface getSurface(bool innerBounds=false) const
bool send(const T &msg)
Definition: events.h:306
int getRandomNumber(int minNumber, int maxNumber)
virtual bool msgMouseMove(const MouseMoveMessage &msg)
Definition: events.h:231
virtual void addView(UIElement *ui)
bool isDelayActive() const
Definition: events.h:111
void cancelDelay()
Definition: events.h:118
void delaySeconds(uint seconds)
bool needsRedraw() const
Definition: events.h:148
void delayFrames(uint frames)
virtual void replaceView(UIElement *ui, bool replaceAllViews=false)
virtual UIElement * findView(const Common::String &name)
virtual void setBounds(const Common::Rect &r)
Definition: events.h:188
Common::Rect getBounds() const
Definition: events.h:195
Definition: formatinfo.h:28
Definition: events.h:218
T height() const
Definition: rect.h:218
bool contains(T x, T y) const
Definition: rect.h:246
T width() const
Definition: rect.h:217
Definition: rect.h:536
Definition: events.h:44
Definition: messages.h:37
Definition: messages.h:61
Definition: lobject.h:59