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 
30 namespace Macs2 {
31 
32 class Events;
33 
39 struct Bounds {
40 private:
41  Common::Rect _bounds;
42  Common::Rect &_innerBounds;
43 
44 public:
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 
54 class UIElement {
55  friend class Events;
56 
57 protected:
59  Common::Rect _innerBounds;
60  Bounds _bounds;
61  bool _needsRedraw = true;
62  Common::String _name;
63 
64 private:
68  void drawElements();
69 
70 public:
71  UIElement(const Common::String &name, UIElement *uiParent);
72  UIElement(const Common::String &name);
73  virtual ~UIElement() {}
74 
78  void redraw();
79 
83  Graphics::ManagedSurface getSurface() const;
84 
88  virtual void draw();
89 
93  virtual bool tick();
94 
98  virtual UIElement *findView(const Common::String &name);
99 
103 #define MESSAGE(NAME) \
104 protected: \
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 
131 class Events : public UIElement {
132 private:
133  Graphics::Screen *_screen = nullptr;
135 
136 protected:
140  void processEvent(Common::Event &ev);
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
161 public:
162  Events();
163  virtual ~Events();
164 
165  // TODO: Consider a better place
166  uint32 currentMillis = 0;
167 
171  void runGame();
172 
176  void addView(UIElement *ui);
177 
182  return _views.empty() ? nullptr : _views.top();
183  }
184 
189  return _screen;
190  }
191 
195  void drawElements() {
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 
213 extern Events *g_events;
214 
215 } // namespace Macs2
216 
217 #endif
Definition: managed_surface.h:51
Definition: str.h:59
Definition: array.h:52
Definition: events.h:54
Definition: rect.h:536
Definition: screen.h:47
Definition: events.h:131
UIElement * focusedView() const
Definition: events.h:181
void draw() override
Definition: events.h:203
T height() const
Definition: rect.h:218
Definition: events.h:39
Definition: events.h:218
Graphics::Screen * getScreen() const
Definition: events.h:188
void drawElements()
Definition: events.h:195
bool tick() override
Definition: events.h:208
Definition: debugtools.h:25
Definition: stack.h:102