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