Merge pull request #4580 from jmarshallnz/anim_length_fix
[vuplus_xbmc] / xbmc / guilib / VisibleEffect.h
1 #pragma once
2
3 /*
4  *      Copyright (C) 2005-2013 Team XBMC
5  *      http://xbmc.org
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 2, or (at your option)
10  *  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 XBMC; see the file COPYING.  If not, see
19  *  <http://www.gnu.org/licenses/>.
20  *
21  */
22
23 enum ANIMATION_PROCESS { ANIM_PROCESS_NONE = 0, ANIM_PROCESS_NORMAL, ANIM_PROCESS_REVERSE };
24 enum ANIMATION_STATE { ANIM_STATE_NONE = 0, ANIM_STATE_DELAYED, ANIM_STATE_IN_PROCESS, ANIM_STATE_APPLIED };
25
26 // forward definitions
27
28 class TiXmlElement;
29 class Tweener;
30 class CGUIListItem;
31
32 #include "TransformMatrix.h"  // needed for the TransformMatrix member
33 #include "Geometry.h"         // for CPoint, CRect
34 #include "utils/StdString.h"
35 #include "boost/shared_ptr.hpp"
36 #include "interfaces/info/InfoBool.h"
37
38 enum ANIMATION_TYPE
39 {
40   ANIM_TYPE_UNFOCUS = -3,
41   ANIM_TYPE_HIDDEN,
42   ANIM_TYPE_WINDOW_CLOSE,
43   ANIM_TYPE_NONE,
44   ANIM_TYPE_WINDOW_OPEN,
45   ANIM_TYPE_VISIBLE,
46   ANIM_TYPE_FOCUS,
47   ANIM_TYPE_CONDITIONAL       // for animations triggered by a condition change
48 };
49
50 class CAnimEffect
51 {
52 public:
53   enum EFFECT_TYPE { EFFECT_TYPE_NONE = 0, EFFECT_TYPE_FADE, EFFECT_TYPE_SLIDE, EFFECT_TYPE_ROTATE_X, EFFECT_TYPE_ROTATE_Y, EFFECT_TYPE_ROTATE_Z, EFFECT_TYPE_ZOOM };
54
55   CAnimEffect(const TiXmlElement *node, EFFECT_TYPE effect);
56   CAnimEffect(unsigned int delay, unsigned int length, EFFECT_TYPE effect);
57   CAnimEffect(const CAnimEffect &src);
58
59   virtual ~CAnimEffect();
60   CAnimEffect& operator=(const CAnimEffect &src);
61
62   void Calculate(unsigned int time, const CPoint &center);
63   void ApplyState(ANIMATION_STATE state, const CPoint &center);
64
65   unsigned int GetDelay() const { return m_delay; };
66   unsigned int GetLength() const { return m_delay + m_length; };
67   const TransformMatrix &GetTransform() const { return m_matrix; };
68   EFFECT_TYPE GetType() const { return m_effect; };
69
70   static boost::shared_ptr<Tweener> GetTweener(const TiXmlElement *pAnimationNode);
71 protected:
72   TransformMatrix m_matrix;
73   EFFECT_TYPE m_effect;
74
75 private:
76   virtual void ApplyEffect(float offset, const CPoint &center)=0;
77
78   // timing variables
79   unsigned int m_length;
80   unsigned int m_delay;
81
82   boost::shared_ptr<Tweener> m_pTweener;
83 };
84
85 class CFadeEffect : public CAnimEffect
86 {
87 public:
88   CFadeEffect(const TiXmlElement *node, bool reverseDefaults);
89   CFadeEffect(float start, float end, unsigned int delay, unsigned int length);
90   virtual ~CFadeEffect() {};
91 private:
92   virtual void ApplyEffect(float offset, const CPoint &center);
93
94   float m_startAlpha;
95   float m_endAlpha;
96 };
97
98 class CSlideEffect : public CAnimEffect
99 {
100 public:
101   CSlideEffect(const TiXmlElement *node);
102   virtual ~CSlideEffect() {};
103 private:
104   virtual void ApplyEffect(float offset, const CPoint &center);
105
106   float m_startX;
107   float m_startY;
108   float m_endX;
109   float m_endY;
110 };
111
112 class CRotateEffect : public CAnimEffect
113 {
114 public:
115   CRotateEffect(const TiXmlElement *node, EFFECT_TYPE effect);
116   virtual ~CRotateEffect() {};
117 private:
118   virtual void ApplyEffect(float offset, const CPoint &center);
119
120   float m_startAngle;
121   float m_endAngle;
122
123   bool m_autoCenter;
124   CPoint m_center;
125 };
126
127 class CZoomEffect : public CAnimEffect
128 {
129 public:
130   CZoomEffect(const TiXmlElement *node, const CRect &rect);
131   virtual ~CZoomEffect() {};
132 private:
133   virtual void ApplyEffect(float offset, const CPoint &center);
134
135   float m_startX;
136   float m_startY;
137   float m_endX;
138   float m_endY;
139
140   bool m_autoCenter;
141   CPoint m_center;
142 };
143
144 class CAnimation
145 {
146 public:
147   CAnimation();
148   CAnimation(const CAnimation &src);
149
150   virtual ~CAnimation();
151
152   CAnimation& operator=(const CAnimation &src);
153
154   static CAnimation CreateFader(float start, float end, unsigned int delay, unsigned int length, ANIMATION_TYPE type = ANIM_TYPE_NONE);
155
156   void Create(const TiXmlElement *node, const CRect &rect, int context);
157
158   void Animate(unsigned int time, bool startAnim);
159   void ResetAnimation();
160   void ApplyAnimation();
161   inline void RenderAnimation(TransformMatrix &matrix)
162   {
163     RenderAnimation(matrix, CPoint());
164   }
165   void RenderAnimation(TransformMatrix &matrix, const CPoint &center);
166   void QueueAnimation(ANIMATION_PROCESS process);
167
168   inline bool IsReversible() const { return m_reversible; };
169   inline ANIMATION_TYPE GetType() const { return m_type; };
170   inline ANIMATION_STATE GetState() const { return m_currentState; };
171   inline ANIMATION_PROCESS GetProcess() const { return m_currentProcess; };
172   inline ANIMATION_PROCESS GetQueuedProcess() const { return m_queuedProcess; };
173
174   bool CheckCondition();
175   void UpdateCondition(const CGUIListItem *item = NULL);
176   void SetInitialCondition();
177
178 private:
179   void Calculate(const CPoint &point);
180   void AddEffect(const CStdString &type, const TiXmlElement *node, const CRect &rect);
181
182   enum ANIM_REPEAT { ANIM_REPEAT_NONE = 0, ANIM_REPEAT_PULSE, ANIM_REPEAT_LOOP };
183
184   // type of animation
185   ANIMATION_TYPE m_type;
186   bool m_reversible;
187   INFO::InfoPtr m_condition;
188
189   // conditional anims can repeat
190   ANIM_REPEAT m_repeatAnim;
191   bool m_lastCondition;
192
193   // state of animation
194   ANIMATION_PROCESS m_queuedProcess;
195   ANIMATION_PROCESS m_currentProcess;
196   ANIMATION_STATE m_currentState;
197
198   // timing of animation
199   unsigned int m_start;
200   unsigned int m_length;
201   unsigned int m_delay;
202   unsigned int m_amount;
203
204   std::vector<CAnimEffect *> m_effects;
205 };
206
207 /**
208  * Class used to handle scrolling, allow using tweeners.
209  * Usage:
210  *   start scrolling using ScrollTo() method / stop scrolling using Stop() method
211  *   update scroll value each frame with current time using Update() method
212  *   get/set scroll value using GetValue()/SetValue()
213  */
214 class CScroller
215 {
216 public:
217   CScroller(unsigned int duration = 200, boost::shared_ptr<Tweener> tweener = boost::shared_ptr<Tweener>());
218   CScroller(const CScroller& right);
219   CScroller& operator=(const CScroller &src);
220   ~CScroller();
221
222   /**
223    * Set target value scroller will be scrolling to
224    * @param endPos target 
225    */
226   void ScrollTo(float endPos);
227   
228   /**
229    * Immediately stop scrolling
230    */
231   void Stop() { m_delta = 0; };
232   /**
233    * Update the scroller to where it would be at the given time point, calculating a new Value.
234    * @param time time point
235    * @return True if we are scrolling at given time point
236    */
237   bool Update(unsigned int time);
238
239   /**
240    * Value of scroll
241    */
242   float GetValue() const { return m_scrollValue; };
243   void SetValue(float scrollValue) { m_scrollValue = scrollValue; };
244
245   bool IsScrolling() const { return m_delta != 0; };
246   bool IsScrollingUp() const { return m_delta < 0; };
247   bool IsScrollingDown() const { return m_delta > 0; };
248
249   unsigned int GetDuration() const { return m_duration; };
250 private:
251   float Tween(float progress);
252
253   float        m_scrollValue;
254   float        m_delta;                   //!< Brief distance that we have to travel during scroll
255   float        m_startPosition;           //!< Brief starting position of scroll
256   bool         m_hasResumePoint;          //!< Brief check if we should tween from middle of the tween
257   unsigned int m_startTime;               //!< Brief starting time of scroll
258   unsigned int m_lastTime;                //!< Brief last remember time (updated each time Scroll() method is called)
259
260   unsigned int m_duration;                //!< Brief duration of scroll
261   boost::shared_ptr<Tweener> m_pTweener;
262 };