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 
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 #ifdef USE_TTS
91  Common::String _previousSaid;
92 #endif
93 
94 protected:
98  bool isDelayActive() const {
99  return _timeoutCtr != 0;
100  }
101 
105  void cancelDelay() {
106  _timeoutCtr = 0;
107  }
108 
112  virtual void timeout();
113 
114 private:
119  virtual void drawElements();
120 
124  static UIElement *findViewGlobally(const Common::String &name);
125 
126 public:
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
216 protected:
217  virtual bool msgMouseMove(const MouseMoveMessage &msg) {
218  return false;
219  }
220 
221 #ifdef USE_TTS
222 
225  void stopTextToSpeech();
226 #endif
227 
228 public:
229  bool send(const MouseMoveMessage &msg) {
230  return msgMouseMove(msg);
231  }
232 
233 #define MESSAGE(NAME) \
234 protected: \
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  \
244 public: \
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 
274 class Events : public UIElement {
275 private:
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 
296 protected:
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
319 public:
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 
345  void clearViews();
346 
350  void popView();
351 
356  return _views.empty() ? nullptr : _views.top();
357  }
358 
362  UIElement *priorView() const {
363  return _views.size() < 2 ? nullptr : _views[_views.size() - 2];
364  }
365 
369  UIElement *firstView() const {
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 
418 extern Events *g_events;
419 
420 } // namespace Got
421 
422 #endif
const Common::String & getName() const
Definition: events.h:188
Definition: str.h:59
UIElement * priorView() const
Definition: events.h:362
Definition: array.h:52
Graphics::Screen * getScreen() const
Definition: events.h:389
UIElement * firstView() const
Definition: events.h:369
virtual void setBounds(const Common::Rect &r)
Definition: events.h:174
void cancelDelay()
Definition: events.h:105
Definition: rect.h:524
bool isInCombat() const
Definition: events.h:382
Definition: screen.h:48
T width() const
Definition: rect.h:217
Definition: events.h:274
bool isDelayActive() const
Definition: events.h:98
void draw() override
Definition: events.h:401
Definition: messages.h:49
T height() const
Definition: rect.h:218
Common::Rect getBounds() const
Definition: events.h:181
Definition: lobject.h:59
Definition: gfx_surface.h:30
bool needsRedraw() const
Definition: events.h:134
Definition: events.h:77
Definition: events.h:210
UIElement * focusedView() const
Definition: events.h:355
void close() override
Definition: events.h:413
bool tick() override
Definition: events.h:406
Definition: console.h:28
Definition: events.h:44
Definition: stack.h:102
virtual bool msgMouseMove(const MouseMoveMessage &msg)
Definition: events.h:217