Oxygine  1
2g game engine
Actor.h
1 #pragma once
2 #include "oxygine-include.h"
3 #include "oxygine-forwards.h"
4 #include "core/Object.h"
5 #include "core/Renderer.h"
6 #include "math/Rect.h"
7 #include "math/AffineTransform.h"
8 #include "utils/intrusive_list.h"
9 #include "Serializable.h"
10 #include "EventDispatcher.h"
11 #include "TouchEvent.h"
12 #include "Tween.h"
13 
14 namespace oxygine
15 {
17  {
18  public:
19  explicit TweenOptions(timeMS duration = 500) : _duration(duration), _delay(0), _ease(Tween::ease_linear), _globalEase(Tween::ease_linear), _loops(1), _twoSides(false), _detach(false) {}
20  TweenOptions& duration(timeMS duration) { _duration = duration; return *this; }
21  TweenOptions& delay(timeMS delay) { _delay = delay; return *this; }
22  TweenOptions& loops(int loops) { _loops = loops; return *this; }
23  TweenOptions& twoSides(bool enabled = true) { _twoSides = enabled; return *this; }
24  TweenOptions& ease(Tween::EASE ease) { _ease = ease; return *this; }
25  TweenOptions& detach(bool detach_ = true) { _detach = detach_; return *this; }
26  TweenOptions& globalEase(Tween::EASE ease) { _globalEase = ease; return *this; }
27  TweenOptions& doneCallback(const EventCallback& cb) { _callback = cb; return *this; }
28 
29 
30  EventCallback _callback;
31  timeMS _duration;
32  timeMS _delay;
33  Tween::EASE _ease;
34  Tween::EASE _globalEase;
35  int _loops;
36  bool _twoSides;
37  bool _detach;
38  };
39 
40  DECLARE_SMART(Actor, spActor);
41 
42  class Actor: public EventDispatcher, public intrusive_list_item<spActor>, public Serializable
43  {
45 
46  public:
47  Actor(const Actor& src, cloneOptions opt = 0);
48  virtual Actor* clone(cloneOptions opt = 0) const;
49  void copyFrom(const Actor& src, cloneOptions opt = 0);
50 
51  Actor();
52  virtual ~Actor();
53 
55  spActor getFirstChild() const {return _children._first;}
57  spActor getLastChild() const {return _children._last;}
59  spActor getNextSibling()const {return intr_list::_next;}
61  spActor getPrevSibling()const {return intr_list::_prev;}
62 
63 
65  Actor* getDescendant(const std::string& name, error_policy ep = ep_show_error);
67  template<class T>
68  T* getDescendantT(const std::string& name, error_policy ep = ep_show_error) { return safeCast<T*>(getDescendant(name, ep)); }
70  spActor getChild(const std::string& name, error_policy ep = ep_show_error) const;
72  template<class T>
73  T* getChildT(const std::string& name, error_policy ep = ep_show_error) const { return safeCast<T*>(getChild(name, ep).get()); }
74 
76  spTween getTween(const std::string& name, error_policy ep = ep_show_error);
78  spTween getFirstTween() const {return _tweens._first;}
80  spTween getLastTween() const {return _tweens._last;}
81 
82  const Vector2& getAnchor() const {return _anchor;}
83  float getAnchorX() const {return _anchor.x;}
84  float getAnchorY() const {return _anchor.y;}
85  bool getIsAnchorInPixels() {return (_flags & flag_anchorInPixels) != 0;}
86  const Vector2& getPosition() const {return _pos;}
87  float getX() const {return _pos.x;}
88  float getY() const {return _pos.y;}
89  const Vector2& getScale() const {return _scale;}
90  float getScaleX() const {return _scale.x;}
91  float getScaleY() const {return _scale.y;}
93  float getRotation() const {return _rotation;}
95  float getRotationDegrees() const {return _rotation / MATH_PI * 180.0f;}
96  int getPriority() const {return _zOrder;}
97  bool getVisible() const {return (_flags & flag_visible) != 0;}
98  Actor* getParent() {return _parent;}
99  const Actor* getParent() const {return _parent;}
100  Vector2 getSize() const {return _size;}
102  Vector2 getScaledSize() const { return _size.mult(_scale); }
103  float getWidth() const;
104  float getScaledWidth() const {return _size.x * _scale.x;}
105  float getHeight() const;
106  float getScaledHeight() const {return _size.y * _scale.y;}
107  unsigned char getAlpha() const;
108  const spClock& getClock() const;
109  virtual RectF getDestRect() const;
111  pointer_index getPressed(MouseButton b = MouseButton_Touch) const;
113  pointer_index getOvered() const;
114  bool getTouchEnabled() const { return (_flags & flag_touchEnabled) != 0; }
115  bool getTouchChildrenEnabled() const { return (_flags & flag_touchChildrenEnabled) != 0; }
116  UpdateCallback getCallbackDoUpdate() const {return _cbDoUpdate;}
117  Material* getMaterial() { return _material; }
118  //RenderCallback getCallbackDoRender() const {return _cbDoRender;}
119 
121  const Transform& getTransform() const;
122  const Transform& getTransformInvert() const;
123 
124 
126  Transform computeGlobalTransform(Actor* parent = 0) const;
128  RectF computeBounds(const Transform& transform = Transform::getIdentity()) const;
129 
133  void setAnchor(const Vector2& anchor);
134  void setAnchor(float ax, float ay);
135  void setAnchorInPixels(const Vector2& anchor);
136  void setAnchorInPixels(float x, float y);
137  void setAnchorX(float x);
138  void setAnchorY(float y);
139 
140  void setPosition(const Vector2& pos);
141  void setPosition(float x, float y);
142  void setX(float x);
143  void setY(float y);
144 
146  void setTransform(const AffineTransform& tr);
148  void setPriority(short zorder);
149  void setScale(float scale);
150  void setScale(const Vector2& scale);
151  void setScale(float scaleX, float scaleY);
152  void setScaleX(float sx);
153  void setScaleY(float sy);
155  void setRotation(float angle);
157  void setRotationDegrees(float angle);
158 
160  void setSize(const Vector2&);
161  void setSize(float w, float h);
162  void setWidth(float w);
163  void setHeight(float h);
165  void setExtendedClickArea(char add) {_extendedIsOn = add;}
166 
167  void setClock(spClock clock);
168  void setMaterial(Material* mat);
169 
171  void setVisible(bool vis) {_flags &= ~flag_visible; if (vis) _flags |= flag_visible;}
173  void setCull(bool enable) {_flags &= ~flag_cull; if (enable) _flags |= flag_cull;}
175  void setAlpha(unsigned char alpha);
176 
178  void setTouchEnabled(bool enabled) { _flags &= ~flag_touchEnabled; if (enabled) _flags |= flag_touchEnabled; }
180  void setTouchChildrenEnabled(bool enabled) { _flags &= ~flag_touchChildrenEnabled; if (enabled) _flags |= flag_touchChildrenEnabled; }
182  void setTouchEnabled(bool enabled, bool childrenEnabled) { setTouchEnabled(enabled); setTouchChildrenEnabled(childrenEnabled); }
183 
185  void setCallbackDoUpdate(UpdateCallback cb) {_cbDoUpdate = cb;}
187  //void setCallbackDoRender(RenderCallback cb){_cbDoRender = cb;}
188 
189  virtual bool isOn(const Vector2& localPosition, float localScale = 1.0f);
191  bool isDescendant(const spActor& actor) const;
192 
194  void insertSiblingBefore(spActor);
196  void insertSiblingAfter(spActor);
198  void prependChild(spActor actor);
200  void prependChild(Actor* actor);
201  void addChild(spActor actor);
202  void addChild(Actor* actor);//avoid conversion to spActor
203  void attachTo(spActor parent);
204  void attachTo(Actor* parent);//avoid conversion to spActor
206  void removeChild(spActor actor);
208  void removeChildren();
209 
210 
212  Actor* detach();
213 
215  void dispatchEvent(Event* event) override;
216 
217  virtual void updateStateOvered() {}
218  virtual void updateStatePressed() {}
219 
220  spTween addTween(spTween);
221  spTween addTween2(spTween, const TweenOptions& opt);
222 
223  template<class Prop>
224  spTween addTween(const Prop& prop, timeMS duration, int loops = 1, bool twoSides = false, timeMS delay = 0, Tween::EASE ease = Tween::ease_linear)
225  {return addTween(createTween(prop, duration, loops, twoSides, delay, ease));}
226 
227  template<class Prop>
228  spTween addTween(const Prop& prop, const TweenOptions& opt)
229  {return addTween(createTween2(prop, opt));}
230 
232  int addClickListener(const EventCallback& cb) { return addEventListener(TouchEvent::CLICK, cb); }
234  int addTouchDownListener(const EventCallback& cb) { return addEventListener(TouchEvent::TOUCH_DOWN, cb); }
236  int addTouchUpListener(const EventCallback& cb) { return addEventListener(TouchEvent::TOUCH_UP, cb); }
237 
238  void removeTween(spTween);
239  void removeTweensByName(const std::string& name);
241  void removeTweens(bool callComplete = false);
242 
244  virtual void update(const UpdateState& us);
246  virtual void render(const RenderState& rs);
247  virtual void handleEvent(Event* event);
248  virtual void doRender(const RenderState& rs) {}
249 
250  //converts position in parent space to local space
251  Vector2 parent2local(const Vector2& pos) const;
252  //converts local position to parent space
253  Vector2 local2parent(const Vector2& pos = Vector2(0, 0)) const;
254 
255  //converts local position to Stage
256  Vector2 local2stage(const Vector2& pos = Vector2(0, 0), Actor* stage = 0) const;
257  //converts global position (position in Stage space) to local space
258  Vector2 stage2local(const Vector2& pos, Actor* stage = 0) const;
259 
260  typedef Property2Args<float, Vector2, const Vector2&, Actor, &Actor::getPosition, &Actor::setPosition> TweenPosition;
261  typedef Property<float, float, Actor, &Actor::getX, &Actor::setX> TweenX;
262  typedef Property<float, float, Actor, &Actor::getY, &Actor::setY> TweenY;
263  typedef Property<float, float, Actor, &Actor::getWidth, &Actor::setWidth> TweenWidth;
264  typedef Property<float, float, Actor, &Actor::getHeight, &Actor::setHeight> TweenHeight;
265  typedef Property2Args2<float, Vector2, Vector2, const Vector2&, Actor, &Actor::getSize, &Actor::setSize>TweenSize;
266  typedef Property<float, float, Actor, &Actor::getRotation, &Actor::setRotation> TweenRotation;
267  typedef Property<float, float, Actor, &Actor::getRotationDegrees, &Actor::setRotationDegrees> TweenRotationDegrees;
268  typedef Property2Args1Arg<float, Vector2, const Vector2&, Actor, &Actor::getScale, &Actor::setScale> TweenScale;
269  typedef Property2Args1Arg<float, Vector2, const Vector2&, Actor, &Actor::getAnchor, &Actor::setAnchor> TweenAnchor;
270  typedef Property<float, float, Actor, &Actor::getScaleX, &Actor::setScaleX> TweenScaleX;
271  typedef Property<float, float, Actor, &Actor::getScaleY, &Actor::setScaleY> TweenScaleY;
272  typedef Property<unsigned char, unsigned char, Actor, &Actor::getAlpha, &Actor::setAlpha> TweenAlpha;
273 
274 
275  void serialize(serializedata*) override;
276  void deserialize(const deserializedata*) override;
277 
279  virtual std::string dump(const dumpOptions& opt) const;
280 
282  Stage* _getStage();
283 
284  void setNotPressed(MouseButton b);
285 
286  bool internalRender(RenderState& rs, const RenderState& parentRS);
287 
289  void clean();
290 
291  virtual bool getBounds(RectF&) const { return false; }
292 
293  protected:
294 
295  Material* _material;
296  Stage* _stage;
297 
298  void added2stage(Stage*);
299  void removedFromStage();
300  virtual void onAdded2Stage() {}
301  virtual void onRemovedFromStage() {}
302  virtual void transformUpdated() {}
303 
304  virtual void calcBounds2(RectF& bounds, const Transform& transform) const;
305 
306 
307  typedef intrusive_list<spActor> children;
308  static void setParent(Actor* actor, Actor* parent);
309  static children& getChildren(spActor& actor) { return actor->_children; }
310  static unsigned short& _getFlags(Actor* actor) { return actor->_flags; }
311 
312  void _onGlobalTouchUpEvent(Event*);
313  void _onGlobalTouchUpEvent1(Event*);
314  void _onGlobalTouchUpEvent2(Event*);
315  void _onGlobalTouchMoveEvent(Event*);
316 
317  const Vector2& _getSize() const { return _size; }
318  void _setSize(const Vector2&);
319  virtual void sizeChanged(const Vector2& size);
320  Actor* _getDescendant(const std::string& name);
321  spTween _addTween(spTween tween, bool rel);
322 
323  bool prepareRender(RenderState& rs, const RenderState& parentRS);
324  void completeRender(const RenderState& rs);
325 
326 
327  void updateTransform() const;
328  void internalUpdate(const UpdateState& us);
329 
331  virtual void doUpdate(const UpdateState& us);
332  UpdateCallback _cbDoUpdate;
333 
334  mutable Transform _transform;
335  mutable Transform _transformInvert;
336 
337 
338  enum flags
339  {
340  flag_anchorInPixels = 1,
341  flag_visible = 1 << 1,
342  flag_touchEnabled = 1 << 2,
343  flag_transformDirty = 1 << 3,
344  flag_transformInvertDirty = 1 << 4,
345  flag_touchChildrenEnabled = 1 << 5,
346  flag_cull = 1 << 6,
347  flag_fastTransform = 1 << 7,
348  flag_reserved = 1 << 8,
349  flag_last = flag_reserved
350  };
351 
352  mutable unsigned short _flags;
353  unsigned char _alpha;
354  char _extendedIsOn;
355 
356  spClock _clock;
357  Actor* _parent;
358 
359  typedef intrusive_list<spTween> tweens;
360  tweens _tweens;
361 
362  children _children;
363 
364  union
365  {
366  //dont change order!!! or brake statements: if (_pressedOvered == _overred)
367  struct
368  {
369  pointer_index _overred;
370  pointer_index _pressedButton[MouseButton_Num];
371  };
372  int32_t _pressedOvered;
373  };
374 
375 
376  private:
377 
378  Vector2 _pos;
379  Vector2 _anchor;
380  Vector2 _scale;
381  Vector2 _size;
382  float _rotation;
383  short _zOrder;
384  };
385 
386 
387 
388  /*Runs callback in time ms.Stage used as default actor*/
389  spTween setTimeout(timeMS dur, const EventCallback& cb, spActor root = 0);
390 
391  Vector2 convert_local2stage(spActor child, const Vector2& pos, spActor root = 0);
392  Vector2 convert_local2stage(const Actor* child, const Vector2& pos, const Actor* root = 0);
393  Vector2 convert_stage2local(spActor child, const Vector2& pos, spActor root = 0);
394  Vector2 convert_stage2local(const Actor* child, const Vector2& pos, const Actor* root = 0);
395 
396 
397  /*Tests 2 actors intersection and returns contact point in space of object1.*/
398  bool testIntersection(spActor obj1, spActor obj2, spActor commonParent = 0, Vector2* contact = 0);
399 
400 
401  RectF getActorTransformedDestRect(Actor* actor, const Transform& tr);
402 
404  void reattachActor(spActor actor, spActor newParent, spActor root = 0);
405 
406  void decompose(const Transform& t, Vector2& pos, float& angle, Vector2& scale);
407  void setDecomposedTransform(spActor& a, const Transform& t);
408 
413  {
414  public:
415  typedef Actor type;
416 
417  void init(Actor&) {}
418  void done(Actor&) {}
419  void update(Actor&, float p, const UpdateState& us) {}
420  };
421 }
422 
423 
424 EDITOR_INCLUDE(Actor);
Transform computeGlobalTransform(Actor *parent=0) const
float getRotation() const
Definition: Actor.h:93
pointer_index getPressed(MouseButton b=MouseButton_Touch) const
void setTransform(const AffineTransform &tr)
void setCallbackDoUpdate(UpdateCallback cb)
Definition: Actor.h:185
Definition: intrusive_list.h:183
void setAlpha(unsigned char alpha)
void setTouchChildrenEnabled(bool enabled)
Definition: Actor.h:180
void removeChild(spActor actor)
Definition: Material.h:6
Definition: Actor.h:42
Definition: Actor.h:412
Definition: Event.h:10
spActor getFirstChild() const
Definition: Actor.h:55
virtual void doUpdate(const UpdateState &us)
spActor getChild(const std::string &name, error_policy ep=ep_show_error) const
Actor * detach()
void insertSiblingBefore(spActor)
Definition: Actor.h:16
void removeTweens(bool callComplete=false)
void removeChildren()
void setRotationDegrees(float angle)
int addTouchDownListener(const EventCallback &cb)
Definition: Actor.h:234
virtual void render(const RenderState &rs)
void reattachActor(spActor actor, spActor newParent, spActor root=0)
Definition: RenderState.h:27
void dispatchEvent(Event *event) override
RectF computeBounds(const Transform &transform=Transform::getIdentity()) const
void setExtendedClickArea(char add)
Definition: Actor.h:165
bool isDescendant(const spActor &actor) const
void setTouchEnabled(bool enabled, bool childrenEnabled)
Definition: Actor.h:182
Definition: UpdateState.h:6
int addClickListener(const EventCallback &cb)
Definition: Actor.h:232
void setPriority(short zorder)
–oxgl-end–!
Definition: Actor.h:14
void insertSiblingAfter(spActor)
void setAnchor(const Vector2 &anchor)
Definition: Serializable.h:6
void setCull(bool enable)
Definition: Actor.h:173
void prependChild(spActor actor)
void setVisible(bool vis)
Definition: Actor.h:171
spTween getTween(const std::string &name, error_policy ep=ep_show_error)
virtual void update(const UpdateState &us)
T * getChildT(const std::string &name, error_policy ep=ep_show_error) const
Definition: Actor.h:73
spActor getNextSibling() const
Definition: Actor.h:59
pointer_index getOvered() const
Stage * _getStage()
void setSize(const Vector2 &)
spTween getLastTween() const
Definition: Actor.h:80
void setTouchEnabled(bool enabled)
Definition: Actor.h:178
Actor * getDescendant(const std::string &name, error_policy ep=ep_show_error)
spActor getLastChild() const
Definition: Actor.h:57
spTween getFirstTween() const
Definition: Actor.h:78
T * getDescendantT(const std::string &name, error_policy ep=ep_show_error)
Definition: Actor.h:68
virtual bool isOn(const Vector2 &localPosition, float localScale=1.0f)
Vector2 getScaledSize() const
Definition: Actor.h:102
const Transform & getTransform() const
Definition: EventDispatcher.h:27
float getRotationDegrees() const
Definition: Actor.h:95
spActor getPrevSibling() const
Definition: Actor.h:61
int addTouchUpListener(const EventCallback &cb)
Definition: Actor.h:236
void setRotation(float angle)
virtual std::string dump(const dumpOptions &opt) const