Updated visuals to add animatable mixColor property
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-base-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_VISUAL_H
3
4 /*
5  * Copyright (c) 2016 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/animation/animation.h>
23 #include <dali/public-api/common/intrusive-ptr.h>
24 #include <dali/public-api/images/image-operations.h>
25 #include <dali/public-api/object/base-object.h>
26 #include <dali/public-api/rendering/renderer.h>
27 #include <dali/public-api/rendering/shader.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
31 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
32 #include <dali-toolkit/internal/visuals/transition-data-impl.h>
33 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
34
35 namespace Dali
36 {
37
38 namespace Toolkit
39 {
40
41 namespace Internal
42 {
43
44 namespace Visual
45 {
46
47 /**
48  * Base class for all Control rendering logic. A control may have multiple visuals.
49  *
50  * Note: The visual responds to the the Actor::COLOR by blending it with the 'Multiply' operator.
51  *
52  * The following properties are optional
53  *
54  * | %Property Name          | Type             |
55  * |-------------------------|------------------|
56  * | customShader            | MAP              |
57  *
58  * where custom-shader is a map with the following properties:
59  * | %Property Name          | Type             |
60  * |-------------------------|------------------|
61  * | vertexShader            | STRING           |
62  * | fragmentShader          | STRING           |
63  * | subdivideGridX          | INT              |
64  * | subdivideGridY          | INT              |
65  * | shaderHints             | INT              |
66  */
67 class Base : public BaseObject
68 {
69 public:
70
71   /**
72    * Setting the properties of the visual, this API should only called by the VisualFactory
73    * @param[in] propertyMap The properties for the requested Visual object.
74    */
75   void SetProperties( const Property::Map& propertyMap );
76
77   /**
78    * @copydoc Toolkit::Visual::Base::SetName
79    */
80   void SetName( const std::string& name );
81
82   /**
83    * @copydoc Toolkit::Visual::Base::GetName
84    */
85   const std::string& GetName();
86
87   /**
88    * @copydoc Toolkit::Visual::Base::SetSize
89    */
90   void SetTransformAndSize( const Property::Map& transform, Size controlSize );
91
92   /**
93    * @copydoc Toolkit::Visual::Base::GetHeightForWidth
94    */
95   virtual float GetHeightForWidth( float width );
96
97   /**
98    * @copydoc Toolkit::Visual::Base::GetWidthForHeight
99    */
100   virtual float GetWidthForHeight( float height );
101
102   /**
103    * @copydoc Toolkit::Visual::Base::GetNaturalSize
104    */
105   virtual void GetNaturalSize( Vector2& naturalSize );
106
107   /**
108    * @copydoc Toolkit::Visual::Base::SetDepthIndex
109    */
110   void SetDepthIndex( float index );
111
112   /**
113    * @copydoc Toolkit::Visual::Base::GetDepthIndex
114    */
115   float GetDepthIndex() const;
116
117   /**
118    * @copydoc Toolkit::Visual::Base::SetOnStage
119    * @pre Impl->mGeometry must be created before this method is called
120    */
121   void SetOnStage( Actor& actor );
122
123   /**
124    * @copydoc Toolkit::Visual::Base::SetOffStage
125    */
126   void SetOffStage( Actor& actor );
127
128   /**
129    * @copydoc Toolkit::Visual::Base::CreatePropertyMap
130    */
131   void CreatePropertyMap( Property::Map& map ) const;
132
133   /**
134    * @brief Set whether the Pre-multiplied Alpha Blending is required
135    *
136    * @param[in] preMultipled whether alpha is pre-multiplied.
137    */
138   void EnablePreMultipliedAlpha( bool preMultipled );
139
140   /**
141    * @brief Query whether alpha is pre-multiplied.
142    *
143    * @return True is alpha is pre-multiplied, false otherwise.
144    */
145   bool IsPreMultipliedAlphaEnabled() const;
146
147   /**
148    * @brief Sets properties of custom shader
149    * @param[in] propertyMap Property map containing the custom shader data
150    */
151   void SetCustomShader( const Property::Map& propertyMap );
152
153   /**
154    * @copydoc Toolkit::Visual::Base::SetProperty
155    */
156   void SetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
157
158   /**
159    * @copydoc Toolkit::Visual::Base::GetProperty
160    */
161   Dali::Property::Value GetProperty( Dali::Property::Index index );
162
163   /**
164    * Gets currently staged renderer, or an empty handle if not staged
165    */
166   Renderer GetRenderer();
167
168   /**
169    * Sets the mix color of the visual.
170    * @param[in] mixColor The new mix color
171    */
172   void SetMixColor( const Vector4& color );
173
174   /**
175    * Gets the mix color of the visual.
176    * @return The mix color
177    */
178   const Vector4& GetMixColor() const;
179
180   /**
181    * Animate the property if it exists in the visual or renderer.
182    *
183    * If it's a visual property such as mix color or a transform property,
184    * saves the target value to the local data.
185    *
186    * If the visual isn't staged (i.e. it doesn't have a renderer),
187    * then this will not add an animation.
188    *
189    * @param[in] transition The animation to create or attach to
190    * @param[in] animator The animation parameters of the property.
191    */
192   void AnimateProperty( Dali::Animation& transition,
193                         Internal::TransitionData::Animator& animator );
194
195 protected:
196
197   /**
198    * @brief Constructor.
199    *
200    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
201    */
202   Base( VisualFactoryCache& factoryCache );
203
204   /**
205    * @brief A reference counted object may only be deleted by calling Unreference().
206    */
207   virtual ~Base();
208
209 protected:
210
211   /**
212    * @brief Called by CreatePropertyMap() allowing sub classes to respond to the CreatePropertyMap event
213    *
214    * @param[out] map The visual property map.
215    */
216   virtual void DoCreatePropertyMap( Property::Map& map ) const = 0;
217
218   /**
219    * @brief Called by SetProperties() allowing sub classes to set their properties
220    *
221    * @param[in] propertyMap The properties for the requested Visual object.
222    */
223   virtual void DoSetProperties( const Property::Map& propertyMap ) = 0;
224
225   /**
226    * @brief Called when transform or control size changes
227    * ( Of use to SVG and Text visuals )
228    */
229   virtual void OnSetTransform() = 0;
230
231   /**
232    * @brief Called by SetOnStage() allowing sub classes to respond to the SetOnStage event
233    *
234    * @note The derived class is required to create the renderer, and add it to the actor when all the resources are in place.
235    *
236    * @param[in] actor The actor applying this visual.
237    */
238   virtual void DoSetOnStage( Actor& actor )=0;
239
240   /**
241    * @brief Called by SetOffStage() allowing sub classes to respond to the SetOffStage event
242    *
243    * @param[in] actor The actor applying this visual.
244    */
245   virtual void DoSetOffStage( Actor& actor );
246
247 protected:
248
249   /**
250    * @brief Gets the on stage state for this Visual
251    *
252    * @return Returns true if this Visual is on stage, false if it is off the stage
253    */
254   bool IsOnStage() const;
255
256   /**
257    * @brief Gets whether the Dali::Renderer is from a shared cache (and therefore any modifications will affect other users of that renderer)
258    *
259    * @return Returns true if the renderer is from shared cache, false otherwise
260    */
261   bool IsFromCache() const;
262
263 private:
264
265   /**
266    * Register the mix color uniform on the Renderer and store the property index.
267    * Note, this is not used by Color or Primitive Visuals, which will use their
268    * own property index.
269    */
270   void RegisterMixColor();
271
272   /**
273    * When a mix color animation has finished, ensure the blend mode is set back
274    * to the right value for the target opacity.
275    */
276   void OnMixColorFinished( Animation& animation );
277
278   // Undefined
279   Base( const Visual::Base& visual );
280
281   // Undefined
282   Base& operator=( const Visual::Base& visual );
283
284 protected:
285   struct Impl;
286   Impl* mImpl;
287   VisualFactoryCache& mFactoryCache;
288 };
289
290 typedef IntrusivePtr<Base> BasePtr;
291
292 } // namspace Visual
293
294 } // namespace Internal
295
296 inline const Internal::Visual::Base& GetImplementation(const Toolkit::Visual::Base& visualBase )
297 {
298   DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
299
300   const BaseObject& handle = visualBase.GetBaseObject();
301
302   return static_cast<const Internal::Visual::Base&>(handle);
303 }
304
305 inline Internal::Visual::Base& GetImplementation(Toolkit::Visual::Base& visualBase)
306 {
307   DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
308
309   BaseObject& handle = visualBase.GetBaseObject();
310
311   return static_cast<Internal::Visual::Base&>(handle);
312 }
313
314 } // namespace Toolkit
315
316 } // namespace Dali
317
318 #endif // DALI_TOOLKIT_INTERNAL_VISUAL_H