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 MM1_EVENTS_H
23#define MM1_EVENTS_H
24
25#include "common/array.h"
26#include "common/stack.h"
27#include "graphics/screen.h"
28#include "mm/mm1/messages.h"
29#include "mm/mm1/utils/mouse.h"
30
31namespace MM {
32namespace MM1 {
33
34#define FRAME_RATE 20
35#define FRAME_DELAY (1000 / FRAME_RATE)
36
37class Events;
38
45struct Bounds {
46private:
47 Common::Rect _bounds;
48 Common::Rect &_innerBounds;
49 int _borderSize = 0;
50public:
51 const int16 &left;
52 const int16 &top;
53 const int16 &right;
54 const int16 &bottom;
55public:
56 Bounds(Common::Rect &innerBounds);
57 operator const Common::Rect &() const { return _bounds; }
58 Bounds &operator=(const Common::Rect &r);
59 void setBorderSize(size_t borderSize);
60 size_t borderSize() const { return _borderSize; }
61 int16 width() const { return _bounds.width(); }
62 int16 height() const { return _bounds.height(); }
63};
64
68class UIElement {
69 friend class Events;
70private:
71 int _timeoutCtr = 0;
72protected:
73 UIElement *_parent;
75 Common::Rect _innerBounds;
76 Bounds _bounds;
77 bool _needsRedraw = true;
78 Common::String _name;
79protected:
80 Common::Rect getLineBounds(int line1, int line2) const {
81 return Common::Rect(0, line1 * 8, 320, (line2 + 1) * 8);
82 }
83
87 void delaySeconds(uint seconds);
88
92 void delayFrames(uint frames);
93
97 bool isDelayActive() const {
98 return _timeoutCtr != 0;
99 }
100
104 void cancelDelay() {
105 _timeoutCtr = 0;
106 }
107
111 bool endDelay();
112
116 virtual void timeout();
117
118private:
123 void drawElements();
124
128 static UIElement *findViewGlobally(const Common::String &name);
129public:
130 UIElement(const Common::String &name, UIElement *uiParent);
131 virtual ~UIElement() {}
132
136 bool needsRedraw() const { return _needsRedraw; }
137
141 void redraw();
142
146 void focus();
147
152 virtual void close();
153
154 /*
155 * Returns true if the view is focused
156 */
157 bool isFocused() const;
158
162 void replaceView(UIElement *ui, bool replaceAllViews = false);
163 void replaceView(const Common::String &name, bool replaceAllViews = false);
164
169 void addView(const Common::String &name);
170 void addView();
171 void open() { addView(); }
172
176 int getRandomNumber(int minNumber, int maxNumber);
177 int getRandomNumber(int maxNumber);
178
182 Common::String getName() const { return _name; }
183
187 virtual void setBounds(const Common::Rect &r) {
188 _bounds = r;
189 }
190
195 return _bounds;
196 }
197
202
206 virtual void clearSurface();
207
211 virtual void draw();
212
216 virtual bool tick();
217
221 virtual UIElement *findView(const Common::String &name);
222
226 #define MESSAGE(NAME) \
227 protected: \
228 virtual bool msg##NAME(const NAME##Message &e) { \
229 for (Common::Array<UIElement *>::iterator it = _children.begin(); \
230 it != _children.end(); ++it) { \
231 if ((*it)->msg##NAME(e)) return true; \
232 } \
233 return false; \
234 } \
235 public: \
236 bool send(const Common::String &viewName, const NAME##Message &msg) { \
237 UIElement *view = UIElement::findViewGlobally(viewName); \
238 assert(view); \
239 return view->msg##NAME(msg); \
240 } \
241 bool send(const NAME##Message &msg) { \
242 return send("Root", msg); \
243 } \
244
245 MESSAGE(Focus);
246 MESSAGE(Unfocus);
247 MESSAGE(Keypress);
248 MESSAGE(MouseDown);
249 MESSAGE(MouseUp);
250 MESSAGE(MouseMove);
251 MESSAGE(Action);
252 MESSAGE(Game);
253 MESSAGE(Header);
254 MESSAGE(Info);
255 MESSAGE(DrawGraphic);
256 #undef MESSAGE
257};
258
260public:
261 ViewsBase() {}
262 virtual ~ViewsBase() {}
263};
264
268class Events : public UIElement, public Mouse {
269private:
270 Graphics::Screen *_screen = nullptr;
272 bool _enhancedMode;
273protected:
278
282 virtual bool shouldQuit() const = 0;
283
287 #define MESSAGE(NAME) \
288 bool msg##NAME(const NAME##Message &e) override { \
289 return !_views.empty() ? focusedView()->msg##NAME(e) : false; \
290 }
291 MESSAGE(Action);
292 MESSAGE(Focus);
293 MESSAGE(Unfocus);
294 MESSAGE(Keypress);
295 MESSAGE(MouseDown);
296 MESSAGE(MouseUp);
297 MESSAGE(MouseMove);
298 MESSAGE(DrawGraphic);
299 #undef MESSAGE
300public:
301 Events(bool enhancedMode);
302 virtual ~Events();
303
307 void runGame();
308
312 void replaceView(UIElement *ui, bool replaceAllViews = false);
313 void replaceView(const Common::String &name, bool replaceAllViews = false);
314
319 void addView(const Common::String &name);
320
325
329 void popView();
330
337
342 return _views.empty() ? nullptr : _views.top();
343 }
344
349 return _views.size() < 2 ? nullptr :
350 _views[_views.size() - 2];
351 }
352
357 bool isPresent(const Common::String &name) const;
358
362 bool isInCombat() const {
363 return isPresent("Combat");
364 }
365
366 Graphics::Screen *getScreen() const {
367 return _screen;
368 }
369
370 void drawElements() {
371 if (!_views.empty())
372 focusedView()->drawElements();
373 }
374
378 void addKeypress(const Common::KeyCode kc);
379
383 void addAction(KeybindingAction action);
384
388 bool isKeypressPending() const;
389
390 void draw() override {}
391
392 bool tick() override {
393 return !_views.empty() ? focusedView()->tick() : false;
394 }
395
399 void close() override {
400 focusedView()->close();
401 }
402};
403
404extern Events *g_events;
405
406} // namespace MM1
407} // namespace MM
408
409#endif
Definition: array.h:52
Definition: stack.h:102
Definition: str.h:59
Definition: managed_surface.h:51
Definition: screen.h:47
Definition: events.h:268
void addKeypress(const Common::KeyCode kc)
void addView(UIElement *ui)
bool isPresent(const Common::String &name) const
bool isKeypressPending() const
bool isInCombat() const
Definition: events.h:362
void replaceView(UIElement *ui, bool replaceAllViews=false)
void draw() override
Definition: events.h:390
bool tick() override
Definition: events.h:392
virtual bool shouldQuit() const =0
void processEvent(Common::Event &ev)
UIElement * focusedView() const
Definition: events.h:341
void addAction(KeybindingAction action)
void close() override
Definition: events.h:399
UIElement * priorView() const
Definition: events.h:348
Definition: mouse.h:30
Definition: events.h:68
Common::Rect getBounds() const
Definition: events.h:194
void addView(UIElement *ui)
virtual UIElement * findView(const Common::String &name)
virtual void timeout()
bool needsRedraw() const
Definition: events.h:136
virtual bool tick()
Graphics::ManagedSurface getSurface() const
virtual void close()
void delaySeconds(uint seconds)
void replaceView(UIElement *ui, bool replaceAllViews=false)
void cancelDelay()
Definition: events.h:104
void delayFrames(uint frames)
Common::String getName() const
Definition: events.h:182
virtual void draw()
bool isDelayActive() const
Definition: events.h:97
virtual void clearSurface()
int getRandomNumber(int minNumber, int maxNumber)
virtual void setBounds(const Common::Rect &r)
Definition: events.h:187
Definition: events.h:259
Definition: detection.h:27
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:45