ScummVM API documentation
scenegraph.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 TWP_SCENEGRAPH_H
23 #define TWP_SCENEGRAPH_H
24 
25 #include "common/array.h"
26 #include "common/str.h"
27 #include "math/vector2d.h"
28 #include "math/matrix4.h"
29 #include "twp/gfx.h"
30 #include "twp/rectf.h"
31 #include "twp/font.h"
32 #include "twp/spritesheet.h"
33 
34 #define NUMOBJECTS 8
35 
36 namespace Twp {
37 
38 class Motor;
39 
40 // Represents a node in a scene graph.
41 class Node {
42 public:
43  Node(const Common::String &name, const Math::Vector2d &scale = Math::Vector2d(1, 1), const Color &color = Color());
44  virtual ~Node();
45 
46  void setName(const Common::String &name) { _name = name; }
47  const Common::String &getName() const { return _name; }
48 
49  virtual void setVisible(bool visible) { _visible = visible; }
50  bool isVisible() const { return _visible; }
51 
52  // Adds new child in current node.
53  //
54  // Arguments:
55  // - `child`: child node to add.
56  void addChild(Node *child);
57  void removeChild(Node *node);
58  void clear();
59  // Removes this node from its parent.
60  void remove();
61  const Common::Array<Node *> &getChildren() const { return _children; }
62 
63  Node *getParent() const { return _parent; }
64  const Node *getRoot() const;
65  // Finds a node in its children and returns its position.
66  int find(Node *other);
67 
68  // Gets the local position for this node (relative to its parent)
69  void setPos(const Math::Vector2d &pos) { _pos = pos; }
70  Math::Vector2d getPos() const { return _pos; }
71 
72  void setOffset(const Math::Vector2d &offset) { _offset = offset; }
73  Math::Vector2d getOffset() const { return _offset; }
74 
75  void setShakeOffset(const Math::Vector2d &offset) { _shakeOffset = offset; }
76  Math::Vector2d getShakeOffset() const { return _shakeOffset; }
77 
78  void setRenderOffset(const Math::Vector2d &offset) { _renderOffset = offset; }
79  Math::Vector2d getRenderOffset() const { return _renderOffset; }
80 
81  void setScale(const Math::Vector2d &scale) { _scale = scale; }
82  virtual Math::Vector2d getScale() const { return _scale; }
83 
84  // Gets the absolute position for this node.
85  Math::Vector2d getAbsPos() const;
86 
87  // Gets the full transformation for this node.
88  virtual Math::Matrix4 getTrsf(const Math::Matrix4 &parentTrsf);
89 
90  void setColor(const Color &color);
91  Color getColor() const { return _color; }
92  Color getComputedColor() const { return _computedColor; }
93 
94  void setAlpha(float alpha);
95  float getAlpha() const { return _color.rgba.a; }
96 
97  void setZSort(int zsort) { _zOrder = zsort; }
98  virtual int getZSort() const { return _zOrder; }
99 
100  void setRotation(float rotation) { _rotation = rotation; }
101  float getRotation() const { return _rotation; }
102 
103  void setRotationOffset(float rotationOffset) { _rotationOffset = rotationOffset; }
104  float getRotationOffset() const { return _rotationOffset; }
105 
106  void setAnchor(const Math::Vector2d &anchor);
107  void setAnchorNorm(const Math::Vector2d &anchorNorm);
108  void setSize(const Math::Vector2d &size);
109  Math::Vector2d getSize() const { return _size; }
110  virtual Rectf getRect() const;
111 
112  void draw(const Math::Matrix4 &parent = Math::Matrix4());
113 
114 protected:
115  virtual void onDrawChildren(const Math::Matrix4 &trsf);
116  virtual void onColorUpdated(const Color &c) {}
117  virtual void drawCore(const Math::Matrix4 &trsf) {}
118 
119 private:
120  // Gets the location transformation = translation * rotation * scale.
121  Math::Matrix4 getLocalTrsf();
122  void updateColor();
123  void updateColor(const Color &parentColor);
124  void updateAlpha();
125  void updateAlpha(float parentAlpha);
126 
127 protected:
128  Common::String _name;
129  Math::Vector2d _pos;
130  int _zOrder = 0;
131  Node *_parent = nullptr;
132  Common::Array<Node *> _children;
133  Math::Vector2d _offset, _shakeOffset, _renderOffset, _anchor, _anchorNorm, _scale, _size;
134  Color _color, _computedColor;
135  bool _visible = true;
136  float _rotation = 0.f;
137  float _rotationOffset = 0.f;
138 };
139 
140 class ParallaxNode final : public Node {
141 public:
142  ParallaxNode(const Math::Vector2d &parallax, const Common::String &sheet, const Common::StringArray &frames);
143  virtual ~ParallaxNode();
144 
145  Math::Matrix4 getTrsf(const Math::Matrix4 &parentTrsf) override final;
146 
147 protected:
148  void onDrawChildren(const Math::Matrix4 &trsf) override;
149  void drawCore(const Math::Matrix4 &trsf) override final;
150 
151 private:
152  Math::Vector2d _parallax;
153  Common::String _sheet;
154  Common::StringArray _frames;
155 };
156 
157 struct ObjectAnimation;
158 
159 class Object;
160 class Anim : public Node {
161 public:
162  Anim(Object *obj);
163 
164  void clearFrames();
165  void setAnim(const ObjectAnimation *anim, float fps = 0.f, bool loop = false, bool instant = false);
166  void update(float elapsed);
167  void disable() { _disabled = true; }
168  void trigSound();
169 
170 private:
171  virtual void drawCore(const Math::Matrix4 &trsf) override final;
172 
173 public:
174  const ObjectAnimation *_anim = nullptr;
175  bool _disabled = false;
176 
177 private:
178  Common::String _sheet;
180  size_t _frameIndex = 0;
181  float _elapsed = 0.f;
182  float _frameDuration = 0.f;
183  bool _loop = false;
184  bool _instant = false;
185  Object *_obj = nullptr;
187 };
188 
189 class ActorNode final : public Node {
190 public:
192 
193  int getZSort() const override final;
194  Math::Vector2d getScale() const override final;
195 
196 private:
198 };
199 
200 class TextNode final : public Node {
201 public:
202  TextNode();
203  virtual ~TextNode() final;
204 
205  void setText(const Text &text);
206  void updateBounds();
207  virtual Rectf getRect() const override final;
208 
209 private:
210  virtual void onColorUpdated(const Color &color) override final;
211  virtual void drawCore(const Math::Matrix4 &trsf) override final;
212 
213 private:
214  Text _text;
215 };
216 
217 class Scene final : public Node {
218 public:
219  Scene();
220  virtual ~Scene() final;
221 };
222 
223 enum class CursorShape {
224  Normal,
225  Front,
226  Back,
227  Left,
228  Right,
229  Pause
230 };
231 
232 enum InputStateFlag {
233  II_FLAGS_UI_INPUT_ON = 1,
234  II_FLAGS_UI_INPUT_OFF = 2,
235  II_FLAGS_UI_VERBS_ON = 4,
236  II_FLAGS_UI_VERBS_OFF = 8,
237  II_FLAGS_UI_HUDOBJECTS_ON = 0x10,
238  II_FLAGS_UI_HUDOBJECTS_OFF = 0x20,
239  II_FLAGS_UI_CURSOR_ON = 0x40,
240  II_FLAGS_UI_CURSOR_OFF = 0x80
241 };
242 
243 class InputState final : public Node {
244 public:
245  InputState();
246  virtual ~InputState() final;
247 
248  InputStateFlag getState() const;
249  void setState(InputStateFlag state);
250 
251  void setInputHUD(bool value) { _inputHUD = value; }
252  bool getInputHUD() const { return _inputHUD; }
253 
254  void setInputActive(bool value) { _inputActive = value; }
255  bool getInputActive() const { return _inputActive; }
256 
257  void setShowCursor(bool value) { _showCursor = value; }
258  bool getShowCursor() const { return _showCursor; }
259 
260  void setInputVerbsActive(bool value) { _inputVerbsActive = value; }
261  bool getInputVerbsActive() const { return _inputVerbsActive; }
262 
263  void setHotspot(bool value) { _hotspot = value; }
264  bool getHotspot() const { return _hotspot; }
265 
266  void setCursorShape(CursorShape shape);
267 
268 private:
269  virtual void drawCore(const Math::Matrix4 &trsf) override final;
270  Common::String getCursorName() const;
271 
272 public:
273  bool _inputHUD = false;
274  bool _inputActive = false;
275  bool _showCursor = false;
276  bool _inputVerbsActive = false;
277  CursorShape _cursorShape = CursorShape::Normal;
278  bool _hotspot = false;
279 };
280 
281 class OverlayNode final : public Node {
282 public:
283  OverlayNode();
284 
285  void setOverlayColor(const Color &color) { _ovlColor = color; }
286  Color getOverlayColor() const { return _ovlColor; }
287 
288 private:
289  virtual void drawCore(const Math::Matrix4 &trsf) override final;
290 
291 private:
292  Color _ovlColor;
293 };
294 
295 class Inventory : public Node {
296 public:
297  Inventory();
298  void update(float elapsed, Common::SharedPtr<Object> actor = nullptr, const Color &backColor = Color(0, 0, 0), const Color &verbNormal = Color(0, 0, 0));
299 
300  bool isOver() const { return _over; }
301  Common::SharedPtr<Object> getObject() const { return _obj; }
302  Math::Vector2d getPos(Common::SharedPtr<Object> inv) const;
303  Math::Vector2d getPos(int index) const;
304  int getOverIndex() const;
305 
306  void setVisible(bool visible) override;
307 
308 private:
309  virtual void drawCore(const Math::Matrix4 &trsf) override final;
310  void drawArrows(const Math::Matrix4 &trsf);
311  void drawBack(const Math::Matrix4 &trsf);
312  void drawItems(const Math::Matrix4 &trsf);
313  void drawSprite(const SpriteSheetFrame &sf, Texture *texture, const Color &color, const Math::Matrix4 &trsf);
314 
315 public:
316  bool _active = false;
317 
318 private:
320  Color _backColor, _verbNormal;
321  bool _down = false;
323  Common::Rect _itemRects[NUMOBJECTS];
324  Common::ScopedPtr<Motor> _shake[NUMOBJECTS];
325  bool _inventoryOver[NUMOBJECTS];
326  Math::Vector2d _shakeOffset[NUMOBJECTS];
327  float _shakeTime[NUMOBJECTS];
328  Common::Rect _arrowUpRect;
329  Common::Rect _arrowDnRect;
330  float _jiggleTime = 0.f;
331  float _fadeTime = 0.f;
332  bool _fadeIn = false;
333  bool _over = false;
334 };
335 
336 class SentenceNode : public Node {
337 public:
338  SentenceNode();
339  virtual ~SentenceNode();
340 
341  void setText(const Common::String &text);
342 
343 private:
344  void drawCore(const Math::Matrix4 &trsf) override final;
345 
346 private:
347  Common::String _text;
348 };
349 
350 class SpriteNode : public Node {
351 public:
352  SpriteNode();
353  virtual ~SpriteNode();
354 
355  void setSprite(const Common::String &sheet, const Common::String &frame);
356 
357 private:
358  void drawCore(const Math::Matrix4 &trsf) override final;
359 
360 private:
361  Common::String _sheet;
362  Common::String _frame;
363 };
364 
365 class NoOverrideNode : public Node {
366 public:
367  NoOverrideNode();
368  virtual ~NoOverrideNode();
369 
370  void reset();
371  bool update(float elapsed);
372 
373 private:
374  SpriteNode _icon;
375  float _elapsed = 0.f;
376 };
377 
378 class HotspotMarkerNode : public Node {
379 public:
381  virtual ~HotspotMarkerNode();
382 
383 private:
384  void drawSprite(const SpriteSheetFrame &sf, Texture *texture, const Color &color, const Math::Matrix4 &trsf);
385  void drawCore(const Math::Matrix4 &trsf) override final;
386 };
387 
388 } // End of namespace Twp
389 
390 #endif
Definition: scenegraph.h:281
Definition: objectanimation.h:31
Definition: scenegraph.h:350
Definition: str.h:59
Definition: rectf.h:30
Definition: array.h:52
Definition: font.h:144
Definition: rect.h:144
Definition: scenegraph.h:336
Definition: ptr.h:572
Definition: scenegraph.h:160
Definition: gfx.h:35
Definition: scenegraph.h:200
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Definition: scenegraph.h:217
Definition: object.h:115
Definition: scenegraph.h:295
Definition: scenegraph.h:189
Definition: scenegraph.h:41
Definition: scenegraph.h:140
Definition: ptr.h:159
Definition: scenegraph.h:365
Definition: scenegraph.h:243
Definition: scenegraph.h:378
Definition: gfx.h:101
Definition: achievements_tables.h:27
Definition: spritesheet.h:37