ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
31 namespace Got {
32 
33 #define FRAME_RATE 60
34 #define FRAME_DELAY (1000 / FRAME_RATE)
35 
36 using Gfx::GfxSurface;
37 class Events;
38 
44 struct Bounds {
45 private:
46  Common::Rect _bounds;
47  Common::Rect &_innerBounds;
48  int _borderSize = 0;
49 
50 public:
51  const int16 &left;
52  const int16 &top;
53  const int16 &right;
54  const int16 &bottom;
55 
56 public:
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 
77 class UIElement {
78  friend class Events;
79 
80 private:
81  int _timeoutCtr = 0;
82 
83 protected:
84  UIElement *_parent;
86  Common::Rect _innerBounds;
87  Bounds _bounds;
88  bool _needsRedraw = true;
89  Common::String _name;
90 
91 protected:
95  bool isDelayActive() const {
96  return _timeoutCtr != 0;
97  }
98 
102  void cancelDelay() {
103  _timeoutCtr = 0;
104  }
105 
109  virtual void timeout();
110 
111 private:
116  virtual void drawElements();
117 
121  static UIElement *findViewGlobally(const Common::String &name);
122 
123 public:
124  UIElement(const Common::String &name, UIElement *uiParent);
125  UIElement(const Common::String &name);
126  virtual ~UIElement() {}
127 
131  bool needsRedraw() const {
132  return _needsRedraw;
133  }
134 
138  void redraw();
139 
144  virtual void close();
145 
149  virtual void replaceView(UIElement *ui, bool replaceAllViews = false, bool fadeOutIn = false);
150  virtual void replaceView(const Common::String &name, bool replaceAllViews = false, bool fadeOutIn = false);
151 
155  virtual void addView(UIElement *ui);
156  virtual void addView(const Common::String &name);
157  void addView();
158  void open() {
159  addView();
160  }
161 
165  int getRandomNumber(int minNumber, int maxNumber);
166  int getRandomNumber(int maxNumber);
167 
171  virtual void setBounds(const Common::Rect &r) {
172  _bounds = r;
173  }
174 
179  return _bounds;
180  }
181 
185  const Common::String &getName() const {
186  return _name;
187  }
188 
192  Gfx::GfxSurface getSurface(bool innerBounds = false) const;
193 
197  virtual void draw();
198 
202  virtual bool tick();
203 
207  virtual UIElement *findView(const Common::String &name);
208 
212  // Mouse move only has a minimal implementation for performance reasons
213 protected:
214  virtual bool msgMouseMove(const MouseMoveMessage &msg) {
215  return false;
216  }
217 
218 public:
219  bool send(const MouseMoveMessage &msg) {
220  return msgMouseMove(msg);
221  }
222 
223 #define MESSAGE(NAME) \
224 protected: \
225  virtual bool msg##NAME(const NAME##Message &e) { \
226  for (Common::Array<UIElement *>::iterator it = _children.begin(); \
227  it != _children.end(); ++it) { \
228  if ((*it)->msg##NAME(e)) \
229  return true; \
230  } \
231  return false; \
232  } \
233  \
234 public: \
235  bool send(const Common::String &viewName, const NAME##Message &msg) { \
236  UIElement *view = UIElement::findViewGlobally(viewName); \
237  assert(view); \
238  return view->msg##NAME(msg); \
239  } \
240  bool send(const NAME##Message &msg) { \
241  return msg##NAME(msg); \
242  }
243 
244  MESSAGE(Focus);
245  MESSAGE(Unfocus);
246  MESSAGE(MouseEnter);
247  MESSAGE(MouseLeave);
248  MESSAGE(Keypress);
249  MESSAGE(MouseDown);
250  MESSAGE(MouseUp);
251  MESSAGE(Action);
252  MESSAGE(Game);
253  MESSAGE(Value);
254 #undef MESSAGE
255 };
256 
264 class Events : public UIElement {
265 private:
266  Graphics::Screen *_screen = nullptr;
268  int _palLoop = 0;
269  int _palCnt1 = 0;
270  int _palCnt2 = 0;
271 
272  void nextFrame();
273  int actionToKeyFlag(int action) const;
274  void rotatePalette();
275 
279  void processEvent(Common::Event &ev);
280 
284  void processDemoEvent(byte ev);
285 
286 protected:
290  virtual bool shouldQuit() const = 0;
291 
295 #define MESSAGE(NAME) \
296  bool msg##NAME(const NAME##Message &e) override { \
297  return !_views.empty() ? focusedView()->msg##NAME(e) : false; \
298  }
299  MESSAGE(Action);
300  MESSAGE(Focus);
301  MESSAGE(Unfocus);
302  MESSAGE(MouseEnter);
303  MESSAGE(MouseLeave);
304  MESSAGE(Keypress);
305  MESSAGE(MouseDown);
306  MESSAGE(MouseUp);
307  MESSAGE(MouseMove);
308 #undef MESSAGE
309 public:
310  Events();
311  virtual ~Events();
312 
313  virtual bool isDemo() const = 0;
314 
318  void runGame();
319 
323  void replaceView(UIElement *ui, bool replaceAllViews = false, bool fadeOutIn = false) override;
324  void replaceView(const Common::String &name, bool replaceAllViews = false, bool fadeOutIn = false) override;
325 
329  void addView(UIElement *ui) override;
330  void addView(const Common::String &name) override;
331 
335  void clearViews();
336 
340  void popView();
341 
346  return _views.empty() ? nullptr : _views.top();
347  }
348 
352  UIElement *priorView() const {
353  return _views.size() < 2 ? nullptr : _views[_views.size() - 2];
354  }
355 
359  UIElement *firstView() const {
360  return _views.empty() ? nullptr : _views[0];
361  }
362 
367  bool isPresent(const Common::String &name) const;
368 
372  bool isInCombat() const {
373  return isPresent("Combat");
374  }
375 
380  return _screen;
381  }
382 
386  void drawElements() override;
387 
391  void draw() override {}
392 
396  bool tick() override {
397  return !_views.empty() ? focusedView()->tick() : false;
398  }
399 
403  void close() override {
404  focusedView()->close();
405  }
406 };
407 
408 extern Events *g_events;
409 
410 } // namespace Got
411 
412 #endif
const Common::String & getName() const
Definition: events.h:185
Definition: str.h:59
UIElement * priorView() const
Definition: events.h:352
Definition: array.h:52
Graphics::Screen * getScreen() const
Definition: events.h:379
UIElement * firstView() const
Definition: events.h:359
virtual void setBounds(const Common::Rect &r)
Definition: events.h:171
void cancelDelay()
Definition: events.h:102
Definition: rect.h:144
bool isInCombat() const
Definition: events.h:372
Definition: screen.h:48
Definition: events.h:264
bool isDelayActive() const
Definition: events.h:95
void draw() override
Definition: events.h:391
Definition: messages.h:49
Common::Rect getBounds() const
Definition: events.h:178
Definition: lobject.h:59
Definition: gfx_surface.h:30
bool needsRedraw() const
Definition: events.h:131
int16 width() const
Definition: rect.h:192
Definition: events.h:77
Definition: events.h:199
UIElement * focusedView() const
Definition: events.h:345
void close() override
Definition: events.h:403
bool tick() override
Definition: events.h:396
Definition: console.h:28
Definition: events.h:44
Definition: stack.h:102
virtual bool msgMouseMove(const MouseMoveMessage &msg)
Definition: events.h:214
int16 height() const
Definition: rect.h:193