ab480d980a78e50154f63fdb0c4b5cc54a90b714
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-gradient / animated-gradient-visual.h
1 #ifndef DALI_TOOLKIT_INTERNAL_ANIMATED_GRADIENT_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_ANIMATED_GRADIENT_VISUAL_H
3
4 /*
5  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 //EXTERNAL INCLUDES
22 #include <dali/public-api/common/dali-vector.h>
23 #include <dali/public-api/common/intrusive-ptr.h>
24 #include <dali/devel-api/common/owner-container.h>
25 #include <dali-toolkit/devel-api/visuals/animated-gradient-visual-properties-devel.h>
26
27 //INTERNAL INCLUDES
28 #include <dali/devel-api/scripting/enum-helper.h>
29 #include <dali/devel-api/scripting/scripting.h>
30 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40 class AnimatedGradientVisual;
41 typedef IntrusivePtr< AnimatedGradientVisual > AnimatedGradientVisualPtr;
42
43 /**
44  * This visual which renders smooth transition of colors to the control's quad.
45  * There are two types of properties: non-animated property and animated property
46  *
47  * The following properties are non-animated property.
48  *
49  * | %Property Name | Type         | Default                   |
50  * |----------------|--------------|---------------------------|
51  * | gradientType   | GradientType | Linear                    |
52  * | unitType       | UnitType     | Object bounding box       |
53  * | spreadType     | SpreadType   | Reflect                   |
54  *
55  * The following properties are animated property.
56  *
57  * | %Property Name | Type                                     | Default                         |
58  * |----------------|------------------------------------------|---------------------------------|
59  * | startPosition  | Vector2 or AnimationParameter< Vector2 > | (-0.5, 0)                       |
60  * | startColor     | Vector4 or AnimationParameter< Vector4 > | (143., 170., 220., 255.) / 255. |
61  * | endPosition    | Vector2 or AnimationParameter< Vector2 > | (0.5, 0)                        |
62  * | endColor       | Vector4 or AnimationParameter< Vector4 > | (255., 163., 163., 255.) / 255. |
63  * | rotateCenter   | Vector2 or AnimationParameter< Vector2 > | (0.0, 0.0)                      |
64  * | rotateAmount   | Float   or AnimationParameter< Float >   | 0.0                             |
65  * | offset         | Float   or AnimationParameter< Float >   | (explain details below)         |
66  *
67  * Each animated property can contain follow AnimationParameter
68  *
69  * | %AnimationParameter<T>::Propery Name | Type          | Default  |
70  * |--------------------------------------|---------------|----------|
71  * | start                                | T             | Zero     |
72  * | target                               | T             | Zero     |
73  * | direction                            | DirectionType | Forward  |
74  * | duration                             | Float         | 3.0      |
75  * | delay                                | Float         | 0.0      |
76  * | repeat                               | Integer       | 0        |
77  * | repeat_delay                         | Float         | 0.0      |
78  * | motion_type                          | MotionType    | Loop     |
79  * | easing_type                          | EasingType    | Linear   |
80  *
81  * T is animated property value's type. If property type is AnimationParameter< Vector2 >, start and target type is Vector2.
82  *
83  *
84  * gradientType decide the form of gradient
85  * unitType decide the coordinate of all positions
86  * spreadType decide how to make color where gradient_point is not 0 ~ 1
87  * visualSize decide this visual's size hardly. only use when unitType is SCREEN
88  *
89  * startPoint and startColor decide position and color where gradient_point = 0. if gradientType is RADIAL, here is center of circle.
90  * endPoint and endColor  decide position and color where gradient_point = 1.
91  * rotateCenter and rotateAmount are same job as its name
92  * rotateAmount is radian value
93  *
94  * offset is main feature of this visual.
95  * Image the points which has same gradient_point values. If gradientType is LINEAR, this form is line. If gradientTYPE IS RADIAL, this form is circle.
96  * without think about offset value, gradient_point = t color will be like this
97  *
98  *  color(t) = startColor * (1-t) + endColor * t (0 <= t <= 1)
99  *
100  * so color be smooth changed when gradient_point change smooth.
101  * offset value change the color of gradient_point = t like this
102  *
103  *  realColor(t) = color(t + offset)
104  *
105  * so If offset value increas (or decrease) gradient looks like "Flowing" effect
106  * default offset value is an unlimited simple loop animation from 0 to 2. duration is 3.0 second
107  *
108  * GradientType has two types : LINEAR / RADIAL
109  *  LINEAR draw gradient linear form
110  *  RADIAL draw gradietn circle form
111  * UnitType has two types : OBJECT_BOUNDING_BOX / USER_SPACE
112  *  OBJECT_BOUNDING_BOX use normalized coordinate, relate by Actor bounding box. bottom-left ~ top-right : (-0.5,-0.5) ~ (0.5, 0.5)
113  *  USER_SPACE use coordinate, relate by Actor Size. bottom-left ~ top-right : (ActorSize * -0.5) ~ (ActorSize * 0.5)
114  * SpreadType has three types : REFLECT / REPEAT / CLAMP
115  *  REFLECT use mirror warping
116  *  REPEAT use repeat warping
117  *  CLAMP use clamp warping
118  *
119  * DirectionType has two types : FORWARD / BACKWARD
120  *  FORWARD animate value from start to target
121  *  BACKWARD animate value from target to start
122  * MotionType has two types : LOOP / MIRROR
123  *  LOOP animate loopingmode restart
124  *  MIRROR animate loopingmode auto_reverse
125  * EasingType has four types : LINEAR / IN / OUT / IN_OUT
126  *  LINEAR easing animation linear
127  *  IN easing in (slow start -> fast finish)
128  *  OUT easing out (fast start -> slow finish)
129  *  IN_OUT easing in and out (slow start -> slow finish)
130  */
131 class AnimatedGradientVisual : public Visual::Base
132 {
133 public:
134
135   /**
136    * Animation informations what this visual using
137    */
138   struct GradientAnimationData
139   {
140    GradientAnimationData()
141     : index( Property::INVALID_INDEX ),
142       loop_count( 0 ),
143       delay( 0.0f ),
144       forward( false ),
145       auto_mirror( false )
146     {
147     }
148
149     Toolkit::TransitionData transition;
150     Animation animation;
151     Property::Index index;
152     int loop_count;   ///< if < 0, loop unlimited. else, loop loop_count times.
153     float delay;      ///< delay time. if > 0, wait 'delay' seconds. else, play animation at '-delay' seconds.
154     bool forward;     ///< True if AnimationParameter::DirectionType::Type is FORWARD
155     bool auto_mirror; ///< True if AnimationParameter::LoopType::Type is MIRROR
156   };
157
158   using GradientAnimationDataList =  Dali::OwnerContainer< GradientAnimationData* >;
159
160 public:
161
162   /**
163    * @brief Create a new animated gradient visual.
164    *
165    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
166    * @param[in] properties A Property::Map containing settings for this visual
167    * @return A smart-pointer to the newly allocated visual
168    */
169   static AnimatedGradientVisualPtr New( VisualFactoryCache& factoryCache, const Property::Map& properties );
170
171 private: //from Visual
172
173   /**
174    * @copydoc Visual::Base::CreatePropertyMap
175    */
176   virtual void DoCreatePropertyMap( Property::Map& map ) const;
177
178   /**
179    * @copydoc Visual::Base::CreateInstancePropertyMap
180    */
181   virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
182
183 protected:
184
185   /**
186    * @brief Constructor.
187    *
188    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
189    */
190   AnimatedGradientVisual( VisualFactoryCache& factoryCache );
191
192   /**
193    * @brief A reference counted object may only be deleted by calling Unrefecence()
194    */
195   virtual ~AnimatedGradientVisual();
196
197 protected: //from Visual
198
199   /**
200    * @copydoc Visual::Base::DoSetProperties
201    */
202   virtual void DoSetProperties( const Property::Map& propertyMap );
203
204   /**
205    * @copydoc Visual::Base::OnSetTransform
206    */
207   virtual void OnSetTransform();
208
209   /**
210    * @copydoc Visual::Base::DoSetOnStage
211    */
212   virtual void DoSetOnStage( Actor& actor );
213
214   /**
215    * @copydoc Visual::Base::DoSetOffStage
216    */
217   virtual void DoSetOffStage( Actor& actor );
218
219 private:
220
221   /**
222    * @brief Initialize the default value of properies.
223    */
224   void SetupDefaultValue();
225
226   /**
227    * @brief Initialize the rendere with the geometry from the cache, and shader which made by CreateShader()
228    */
229   void InitializeRenderer();
230
231   /**
232    * @brief Make animations with GradientAnimationData
233    */
234   void SetupAnimation();
235
236   /**
237   * @brief Play animations
238   */
239   void PlayAnimation();
240
241   /**
242   * @brief Stop animations
243   */
244   void StopAnimation();
245
246   /**
247    * @brief Clear all previous GradientAnimationData and Setup new GradientAnimationData information which made by animated properties
248    *
249    * param[in] propertyMap A Property::Map come from DoSetProperties
250    */
251   void SetupGradientAnimationData( const Property::Map& propertyMap );
252
253   /**
254    * @brief Create new shader
255    *
256    * return A Shader which made by non-animated properties
257    */
258   Shader CreateShader();
259
260   // Undefined
261   AnimatedGradientVisual( const AnimatedGradientVisual& gradientRenderer );
262
263   // Undefined
264   AnimatedGradientVisual& operator=( const AnimatedGradientVisual& gradientRenderer );
265
266 private:
267   GradientAnimationDataList mGradientAnimationDataList;
268   Property::Map mValueMap;
269
270   Dali::Toolkit::DevelAnimatedGradientVisual::GradientType::Type mGradientType;
271   Dali::Toolkit::DevelAnimatedGradientVisual::UnitType::Type mUnitType;
272   Dali::Toolkit::DevelAnimatedGradientVisual::SpreadType::Type mSpreadType;
273 };
274
275 }//namespace Internal
276
277 }//namespace Toolkit
278
279 }//namespace Dali
280
281 #endif