Oxygine  1
2g game engine
Tween.h
1 #pragma once
2 #include "oxygine-include.h"
3 #include "core/Object.h"
4 #include "closure/closure.h"
5 #include "utils/intrusive_list.h"
6 #include "UpdateState.h"
7 
8 #include "EventDispatcher.h"
9 #include "Event.h"
10 #include <stdint.h>
11 #include <limits>
12 #include "Property.h"
13 
14 namespace oxygine
15 {
16  class TweenEvent : public Event
17  {
18  public:
19  enum
20  {
21  DONE = Event::COMPLETE,
22  };
23 
24  TweenEvent(Tween* tween_, const UpdateState* us_) : Event(DONE, false), tween(tween_), us(us_) {}
25  Actor* getActor() const;
26 
27  Tween* tween;
28  const UpdateState* us;
29  };
30 
31 
32 
33  const int TWEEN_COMPLETE_DT = std::numeric_limits<int>::max() / 2;
34 
35 
36  DECLARE_SMART(Tween, spTween);
37  class Tween : public EventDispatcher, public intrusive_list_item<spTween>
38  {
40  public:
41  enum EASE
42  {
43  ease_unknown,
44  ease_linear,
45 
46  ease_inQuad,
47  ease_outQuad,
48  ease_inOutQuad,
49  ease_outInQuad,
50 
51  ease_inCubic,
52  ease_outCubic,
53  ease_inOutCubic,
54  ease_outInCubic,
55 
56  ease_inQuart,
57  ease_outQuart,
58  ease_inOutQuart,
59  ease_outInQuart,
60 
61  ease_inQuint,
62  ease_outQuint,
63  ease_inOutQuint,
64  ease_outInQuint,
65 
66  ease_inSin,
67  ease_outSin,
68  ease_inOutSin,
69  ease_outInSin,
70 
71  ease_inExpo,
72  ease_outExpo,
73  ease_inOutExpo,
74  ease_outInExpo,
75 
76  ease_inCirc,
77  ease_outCirc,
78  ease_inOutCirc,
79  ease_outInCirc,
80 
81  ease_inBack,
82  ease_outBack,
83  ease_inOutBack,
84  ease_outInBack,
85 
86  ease_inBounce,
87  ease_outBounce,
88  ease_inOutBounce,
89  ease_outInBounce,
90 
91  ease_count
92  };
93 
94  Tween();
95  ~Tween();
96 
97  void init(timeMS duration, int loops = 1, bool twoSides = false, timeMS delay = 0, EASE ease = Tween::ease_linear);//todo twoSide find better name
98  void init2(const TweenOptions& opt);
100  void reset();
101 
102  int getLoops() const { return _loops; }
103  timeMS getDuration() const { return _duration; }
104  timeMS getElapsed() const { return _elapsed; }
105  EASE getEase() const { return _ease; }
106  EASE getGlobalEase() const { return _globalEase; }
107  timeMS getDelay() const { return _delay; }
108  Actor* getClient() const { return _client; }
109  float getPercent() const { return _percent; }
110  spObject getDataObject() const { return _data; }
111  spTween getNextSibling() { return intr_list::getNextSibling(); }
112  spTween getPrevSibling() { return intr_list::getPrevSibling(); }
113  const EventCallback& getDoneCallback() const { return _cbDone; }
114 
115  bool isStarted() const { return _status != status_not_started; }
116  bool isDone() const { return _status == status_remove; }
117 
119  void setDataObject(spObject data) { _data = data; }
122  void addDoneCallback(const EventCallback& cb);
124  void setEase(EASE ease) { _ease = ease; }
126  void setGlobalEase(EASE ease) { _globalEase = ease; }
128  void setDelay(timeMS delay) { _delay = delay; }
130  void setLoops(int loops) { _loops = loops; }
131  /*set Duration of tween**/
132  void setDuration(timeMS duration) { _duration = duration; }
133  void setClient(Actor* client) { _client = client; }
134  void setTwoSides(bool ts) { _twoSides = ts; }
135 
137  void detachWhenDone(bool detach = true) { _detach = detach; }
138 
140  virtual void complete(timeMS deltaTime = TWEEN_COMPLETE_DT);
141 
143  void remove();
144 
145  void start(Actor& actor);
146  void update(Actor& actor, const UpdateState& us);
147 
148  static float calcEase(EASE ease, float v);
149  typedef float (*easeHandler)(EASE ease, float v);
150  static void setCustomEaseHandler(easeHandler);
151 
153  void setDoneCallback(const EventCallback& cb);
154 
155  protected:
156  void done(Actor&, const UpdateState& us);
157 
158  virtual void _start(Actor& actor) {}
159  virtual void _update(Actor& actor, const UpdateState& us) {}
160  virtual void _done(Actor& actor, const UpdateState& us) {}
161  virtual void _loopDone(Actor& actor, const UpdateState& us) {}
162  virtual float _calcEase(float v);
163 
164  enum status
165  {
166  status_not_started,
167  status_delayed,
168  status_started,
169  status_done,
170  status_remove,
171  };
172  status _status;
173  timeMS _elapsed;
174 
175  timeMS _duration;
176  timeMS _delay;
177  int _loops;
178  int _loopsDone;
179  EASE _ease;
180  EASE _globalEase;
181  bool _twoSides;
182 
183  float _percent;
184  bool _detach;
185 
186  EventCallback _cbDone;
187  Actor* _client;
188 
189  spObject _data;
190  };
191 
192  template<class GS>
193  class TweenT : public Tween
194  {
195  public:
196  typedef typename GS::type type;
197 
198  TweenT(const GS& gs) : _gs(gs) {}
199 
200  void _update(Actor& actor, const UpdateState& us)
201  {
202  type& t = *safeCast<type*>(&actor);
203  _gs.update(t, _percent, us);//todo fix cast
204  }
205 
206  void _start(Actor& actor)
207  {
208  type& t = *safeCast<type*>(&actor);
209  _gs.init(t);
210  UpdateState us;
211  us.iteration = -1;
212  _gs.update(t, _calcEase(0.0f), us);
213  }
214 
215  void _done(Actor& actor, const UpdateState& us)
216  {
217  type& t = *safeCast<type*>(&actor);
218  _gs.done(t);
219  }
220 
221  GS& getGS() { return _gs; }
222 
223  private:
224  GS _gs;
225  };
226 
227 
228  template <typename GS>
229  TweenT<GS>* createTween(const GS& gs, timeMS duration, int loops = 1, bool twoSides = false, timeMS delay = 0, Tween::EASE ease = Tween::ease_linear)
230  {
231  TweenT<GS>* p = new TweenT<GS>(gs);
232  p->init(duration, loops, twoSides, delay, ease);
233  return p;
234  }
235 
236  template <typename GS>
237  TweenT<GS>* createTween2(const GS& gs, const TweenOptions& opt)
238  {
239  TweenT<GS>* p = new TweenT<GS>(gs);
240  p->init2(opt);
241  return p;
242  }
243 
244  std::string ease2String(Tween::EASE ease);
245 
246 
247 
248 
249 
250 
251  DECLARE_SMART(TweenObj, spTweenObj);
252  class TweenObj : public Object
253  {
254  public:
255  typedef Actor type;
256 
257  virtual void init(Actor&) {}
258  virtual void done(Actor&) {}
259  virtual void update(Actor&, float p, const UpdateState& us) {}
260  };
261 
262 
264  {
265  public:
266  typedef Actor type;
267 
268  TweenProxy(spTweenObj o) { _obj = o; }
269  void init(Actor& a) { _obj->init(a); }
270  void done(Actor& a) { _obj->done(a); }
271  void update(Actor& a, float p, const UpdateState& us) { _obj->update(a, p, us); }
272 
273  spTweenObj _obj;
274  };
275 }
276 
277 
void detachWhenDone(bool detach=true)
Definition: Tween.h:137
void setDoneCallback(const EventCallback &cb)
void addDoneCallback(const EventCallback &cb)
Definition: intrusive_list.h:183
void setGlobalEase(EASE ease)
Definition: Tween.h:126
Definition: Actor.h:42
Definition: Event.h:10
void setLoops(int loops)
Definition: Tween.h:130
Definition: Tween.h:193
Definition: Actor.h:16
void setDelay(timeMS delay)
Definition: Tween.h:128
Definition: UpdateState.h:6
virtual void complete(timeMS deltaTime=TWEEN_COMPLETE_DT)
–oxgl-end–!
Definition: Actor.h:14
Definition: Object.h:83
Definition: Tween.h:252
Definition: Tween.h:263
int iteration
Definition: UpdateState.h:18
void setEase(EASE ease)
Definition: Tween.h:124
Definition: Tween.h:37
void setDataObject(spObject data)
Definition: Tween.h:119
Definition: EventDispatcher.h:27
Definition: Tween.h:16