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 GOT_EVENTS_H
23#define GOT_EVENTS_H
24
25#include "common/array.h"
26#include "common/stack.h"
27#include "got/gfx/gfx_surface.h"
28#include "got/messages.h"
29#include "graphics/screen.h"
30
31namespace Got {
32
33#define FRAME_RATE 60
34#define FRAME_DELAY (1000 / FRAME_RATE)
35
36using Gfx::GfxSurface;
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};
73
77class UIElement {
78 friend class Events;
79
80private:
81 int _timeoutCtr = 0;
82
83protected:
84 UIElement *_parent;
86 Common::Rect _innerBounds;
87 Bounds _bounds;
88 bool _needsRedraw = true;
89 Common::String _name;
90#ifdef USE_TTS
91 Common::String _previousSaid;
92#endif
93
94protected:
98 bool isDelayActive() const {
99 return _timeoutCtr != 0;
100 }
101
105 void cancelDelay() {
106 _timeoutCtr = 0;
107 }
108
112 virtual void timeout();
113
114private:
119 virtual void drawElements();
120
124 static UIElement *findViewGlobally(const Common::String &name);
125
126public:
127 UIElement(const Common::String &name, UIElement *uiParent);
128 UIElement(const Common::String &name);
129 virtual ~UIElement() {}
130
134 bool needsRedraw() const {
135 return _needsRedraw;
136 }
137
141 void redraw();
142
147 virtual void close();
148
152 virtual void replaceView(UIElement *ui, bool replaceAllViews = false, bool fadeOutIn = false);
153 virtual void replaceView(const Common::String &name, bool replaceAllViews = false, bool fadeOutIn = false);
154
158 virtual void addView(UIElement *ui);
159 virtual void addView(const Common::String &name);
160 void addView();
161 void open() {
162 addView();
163 }
164
168 int getRandomNumber(int minNumber, int maxNumber);
169 int getRandomNumber(int maxNumber);
170
174 virtual void setBounds(const Common::Rect &r) {
175 _bounds = r;
176 }
177
182 return _bounds;
183 }
184
188 const Common::String &getName() const {
189 return _name;
190 }
191
195 Gfx::GfxSurface getSurface(bool innerBounds = false) const;
196
200 virtual void draw();
201
205 virtual bool tick();
206
210 virtual UIElement *findView(const Common::String &name);
211
215 // Mouse move only has a minimal implementation for performance reasons
216protected:
217 virtual bool msgMouseMove(const MouseMoveMessage &msg) {
218 return false;
219 }
220
221#ifdef USE_TTS
226#endif
227
228public:
229 bool send(const MouseMoveMessage &msg) {
230 return msgMouseMove(msg);
231 }
232
233#define MESSAGE(NAME) \
234protected: \
235 virtual bool msg##NAME(const NAME##Message &e) { \
236 for (Common::Array<UIElement *>::iterator it = _children.begin(); \
237 it != _children.end(); ++it) { \
238 if ((*it)->msg##NAME(e)) \
239 return true; \
240 } \
241 return false; \
242 } \
243 \
244public: \
245 bool send(const Common::String &viewName, const NAME##Message &msg) { \
246 UIElement *view = UIElement::findViewGlobally(viewName); \
247 assert(view); \
248 return view->msg##NAME(msg); \
249 } \
250 bool send(const NAME##Message &msg) { \
251 return msg##NAME(msg); \
252 }
253
254 MESSAGE(Focus);
255 MESSAGE(Unfocus);
256 MESSAGE(MouseEnter);
257 MESSAGE(MouseLeave);
258 MESSAGE(Keypress);
259 MESSAGE(MouseDown);
260 MESSAGE(MouseUp);
261 MESSAGE(Action);
262 MESSAGE(Game);
263 MESSAGE(Value);
264#undef MESSAGE
265};
266
274class Events : public UIElement {
275private:
276 Graphics::Screen *_screen = nullptr;
278 int _palLoop = 0;
279 int _palCnt1 = 0;
280 int _palCnt2 = 0;
281
282 void nextFrame();
283 int actionToKeyFlag(int action) const;
284 void rotatePalette();
285
289 void processEvent(Common::Event &ev);
290
294 void processDemoEvent(byte ev);
295
296protected:
300 virtual bool shouldQuit() const = 0;
301
305#define MESSAGE(NAME) \
306 bool msg##NAME(const NAME##Message &e) override { \
307 return !_views.empty() ? focusedView()->msg##NAME(e) : false; \
308 }
309 MESSAGE(Action);
310 MESSAGE(Focus);
311 MESSAGE(Unfocus);
312 MESSAGE(MouseEnter);
313 MESSAGE(MouseLeave);
314 MESSAGE(Keypress);
315 MESSAGE(MouseDown);
316 MESSAGE(MouseUp);
317 MESSAGE(MouseMove);
318#undef MESSAGE
319public:
320 Events();
321 virtual ~Events();
322
323 virtual bool isDemo() const = 0;
324
328 void runGame();
329
333 void replaceView(UIElement *ui, bool replaceAllViews = false, bool fadeOutIn = false) override;
334 void replaceView(const Common::String &name, bool replaceAllViews = false, bool fadeOutIn = false) override;
335
339 void addView(UIElement *ui) override;
340 void addView(const Common::String &name) override;
341
346
350 void popView();
351
356 return _views.empty() ? nullptr : _views.top();
357 }
358
363 return _views.size() < 2 ? nullptr : _views[_views.size() - 2];
364 }
365
370 return _views.empty() ? nullptr : _views[0];
371 }
372
377 bool isPresent(const Common::String &name) const;
378
382 bool isInCombat() const {
383 return isPresent("Combat");
384 }
385
390 return _screen;
391 }
392
396 void drawElements() override;
397
401 void draw() override {}
402
406 bool tick() override {
407 return !_views.empty() ? focusedView()->tick() : false;
408 }
409
413 void close() override {
414 focusedView()->close();
415 }
416};
417
418extern Events *g_events;
419
420} // namespace Got
421
422#endif
Definition: array.h:52
Definition: stack.h:102
Definition: str.h:59
Definition: events.h:274
void clearViews()
UIElement * firstView() const
Definition: events.h:369
UIElement * priorView() const
Definition: events.h:362
void drawElements() override
UIElement * focusedView() const
Definition: events.h:355
bool isInCombat() const
Definition: events.h:382
void runGame()
void replaceView(UIElement *ui, bool replaceAllViews=false, bool fadeOutIn=false) override
Graphics::Screen * getScreen() const
Definition: events.h:389
void close() override
Definition: events.h:413
bool tick() override
Definition: events.h:406
bool isPresent(const Common::String &name) const
void addView(UIElement *ui) override
void draw() override
Definition: events.h:401
void popView()
virtual bool shouldQuit() const =0
Definition: gfx_surface.h:30
Definition: events.h:77
Gfx::GfxSurface getSurface(bool innerBounds=false) const
virtual void close()
bool needsRedraw() const
Definition: events.h:134
virtual void timeout()
virtual void replaceView(UIElement *ui, bool replaceAllViews=false, bool fadeOutIn=false)
const Common::String & getName() const
Definition: events.h:188
virtual UIElement * findView(const Common::String &name)
void stopTextToSpeech()
virtual bool tick()
virtual void draw()
bool isDelayActive() const
Definition: events.h:98
virtual void setBounds(const Common::Rect &r)
Definition: events.h:174
virtual bool msgMouseMove(const MouseMoveMessage &msg)
Definition: events.h:217
void cancelDelay()
Definition: events.h:105
Common::Rect getBounds() const
Definition: events.h:181
int getRandomNumber(int minNumber, int maxNumber)
virtual void addView(UIElement *ui)
Definition: screen.h:47
Definition: events.h:218
T height() const
Definition: rect.h:218
T width() const
Definition: rect.h:217
Definition: rect.h:536
Definition: events.h:44
Definition: messages.h:49
Definition: lobject.h:59