c065b435524752314af991fa292684024550555e
[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   void AddEffect(CAnimEffect *effect);
182
183   enum ANIM_REPEAT { ANIM_REPEAT_NONE = 0, ANIM_REPEAT_PULSE, ANIM_REPEAT_LOOP };
184
185   // type of animation
186   ANIMATION_TYPE m_type;
187   bool m_reversible;
188   INFO::InfoPtr m_condition;
189
190   // conditional anims can repeat
191   ANIM_REPEAT m_repeatAnim;
192   bool m_lastCondition;
193
194   // state of animation
195   ANIMATION_PROCESS m_queuedProcess;
196   ANIMATION_PROCESS m_currentProcess;
197   ANIMATION_STATE m_currentState;
198
199   // timing of animation
200   unsigned int m_start;
201   unsigned int m_length;
202   unsigned int m_delay;
203   unsigned int m_amount;
204
205   std::vector<CAnimEffect *> m_effects;
206 };
207
208 /**
209  * Class used to handle scrolling, allow using tweeners.
210  * Usage:
211  *   start scrolling using ScrollTo() method / stop scrolling using Stop() method
212  *   update scroll value each frame with current time using Update() method
213  *   get/set scroll value using GetValue()/SetValue()
214  */
215 class CScroller
216 {
217 public:
218   CScroller(unsigned int duration = 200, boost::shared_ptr<Tweener> tweener = boost::shared_ptr<Tweener>());
219   CScroller(const CScroller& right);
220   CScroller& operator=(const CScroller &src);
221   ~CScroller();
222
223   /**
224    * Set target value scroller will be scrolling to
225    * @param endPos target 
226    */
227   void ScrollTo(float endPos);
228   
229   /**
230    * Immediately stop scrolling
231    */
232   void Stop() { m_delta = 0; };
233   /**
234    * Update the scroller to where it would be at the given time point, calculating a new Value.
235    * @param time time point
236    * @return True if we are scrolling at given time point
237    */
238   bool Update(unsigned int time);
239
240   /**
241    * Value of scroll
242    */
243   float GetValue() const { return m_scrollValue; };
244   void SetValue(float scrollValue) { m_scrollValue = scrollValue; };
245
246   bool IsScrolling() const { return m_delta != 0; };
247   bool IsScrollingUp() const { return m_delta < 0; };
248   bool IsScrollingDown() const { return m_delta > 0; };
249
250   unsigned int GetDuration() const { return m_duration; };
251 private:
252   float Tween(float progress);
253
254   float        m_scrollValue;
255   float        m_delta;                   //!< Brief distance that we have to travel during scroll
256   float        m_startPosition;           //!< Brief starting position of scroll
257   bool         m_hasResumePoint;          //!< Brief check if we should tween from middle of the tween
258   unsigned int m_startTime;               //!< Brief starting time of scroll
259   unsigned int m_lastTime;                //!< Brief last remember time (updated each time Scroll() method is called)
260
261   unsigned int m_duration;                //!< Brief duration of scroll
262   boost::shared_ptr<Tweener> m_pTweener;
263 };