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 MACS2_EVENTS_H
23#define MACS2_EVENTS_H
24
25#include "common/array.h"
26#include "common/stack.h"
27#include "graphics/screen.h"
28#include "macs2/messages.h"
29
30namespace Macs2 {
31
32class Events;
33
39struct Bounds {
40private:
41 Common::Rect _bounds;
42 Common::Rect &_innerBounds;
43
44public:
45 Bounds(Common::Rect &innerBounds);
46 operator const Common::Rect &() const { return _bounds; }
47 Bounds &operator=(const Common::Rect &r);
48 int16 height() const { return _bounds.height(); }
49};
50
54class UIElement {
55 friend class Events;
56
57protected:
59 Common::Rect _innerBounds;
60 Bounds _bounds;
61 bool _needsRedraw = true;
62 Common::String _name;
63
64private:
68 void drawElements();
69
70public:
71 UIElement(const Common::String &name, UIElement *uiParent);
72 UIElement(const Common::String &name);
73 virtual ~UIElement() {}
74
78 void redraw();
79
84
88 virtual void draw();
89
93 virtual bool tick();
94
98 virtual UIElement *findView(const Common::String &name);
99
103#define MESSAGE(NAME) \
104protected: \
105 virtual bool msg##NAME(const NAME##Message &e) { \
106 for (Common::Array<UIElement *>::iterator it = _children.begin(); \
107 it != _children.end(); ++it) { \
108 if ((*it)->msg##NAME(e)) \
109 return true; \
110 } \
111 return false; \
112 }
113
114 MESSAGE(Focus);
115 MESSAGE(Unfocus);
116 MESSAGE(Keypress);
117 MESSAGE(MouseDown);
118 MESSAGE(MouseUp);
119 MESSAGE(Action);
120 MESSAGE(MouseMove);
121#undef MESSAGE
122};
123
131class Events : public UIElement {
132private:
133 Graphics::Screen *_screen = nullptr;
135
136protected:
141
145 virtual bool shouldQuit() const = 0;
146
150#define MESSAGE(NAME) \
151 bool msg##NAME(const NAME##Message &e) override { \
152 return !_views.empty() ? focusedView()->msg##NAME(e) : false; \
153 }
154 MESSAGE(Action);
155 MESSAGE(Focus);
156 MESSAGE(Unfocus);
157 MESSAGE(Keypress);
158 MESSAGE(MouseDown);
159 MESSAGE(MouseUp);
160#undef MESSAGE
161public:
162 Events();
163 virtual ~Events();
164
165 // TODO: Consider a better place
166 uint32 currentMillis = 0;
167
171 void runGame();
172
177
182 return _views.empty() ? nullptr : _views.top();
183 }
184
189 return _screen;
190 }
191
196 if (!_views.empty())
197 focusedView()->drawElements();
198 }
199
203 void draw() override {}
204
208 bool tick() override {
209 return !_views.empty() ? focusedView()->tick() : false;
210 }
211};
212
213extern Events *g_events;
214
215} // namespace Macs2
216
217#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:131
bool tick() override
Definition: events.h:208
virtual bool shouldQuit() const =0
void processEvent(Common::Event &ev)
void drawElements()
Definition: events.h:195
UIElement * focusedView() const
Definition: events.h:181
void draw() override
Definition: events.h:203
void addView(UIElement *ui)
Graphics::Screen * getScreen() const
Definition: events.h:188
Definition: events.h:54
Graphics::ManagedSurface getSurface() const
virtual bool tick()
virtual void draw()
virtual UIElement * findView(const Common::String &name)
Definition: events.h:218
T height() const
Definition: rect.h:218
Definition: rect.h:536
Definition: events.h:39