[dali_1.2.64] Merge branch 'devel/master'
[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) 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/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 #include <dali-toolkit/devel-api/direction-enums.h>
35
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 namespace Internal
43 {
44
45 namespace Visual
46 {
47
48 class ResourceObserver;
49
50 /**
51  * Base class for all Control rendering logic. A control may have multiple visuals.
52  *
53  * Note: The visual responds to the the Actor::COLOR by blending it with the 'Multiply' operator.
54  *
55  * The following properties are optional
56  *
57  * | %Property Name          | Type             |
58  * |-------------------------|------------------|
59  * | customShader            | MAP              |
60  *
61  * where custom-shader is a map with the following properties:
62  * | %Property Name          | Type             |
63  * |-------------------------|------------------|
64  * | vertexShader            | STRING           |
65  * | fragmentShader          | STRING           |
66  * | subdivideGridX          | INT              |
67  * | subdivideGridY          | INT              |
68  * | shaderHints             | INT              |
69  */
70 class Base : public BaseObject
71 {
72 public:
73
74   /**
75    * Setting the properties of the visual, this API should only called by the VisualFactory
76    * @param[in] propertyMap The properties for the requested Visual object.
77    */
78   void SetProperties( const Property::Map& propertyMap );
79
80   /**
81    * @copydoc Toolkit::Visual::Base::SetName
82    */
83   void SetName( const std::string& name );
84
85   /**
86    * @copydoc Toolkit::Visual::Base::GetName
87    */
88   const std::string& GetName();
89
90   /**
91    * @copydoc Toolkit::Visual::Base::SetSize
92    */
93   void SetTransformAndSize( const Property::Map& transform, Size controlSize );
94
95   /**
96    * @copydoc Toolkit::Visual::Base::GetHeightForWidth
97    */
98   virtual float GetHeightForWidth( float width );
99
100   /**
101    * @copydoc Toolkit::Visual::Base::GetWidthForHeight
102    */
103   virtual float GetWidthForHeight( float height );
104
105   /**
106    * @copydoc Toolkit::Visual::Base::GetNaturalSize
107    */
108   virtual void GetNaturalSize( Vector2& naturalSize );
109
110   /**
111    * @copydoc Toolkit::Visual::Base::SetDepthIndex
112    */
113   void SetDepthIndex( int index );
114
115   /**
116    * @copydoc Toolkit::Visual::Base::GetDepthIndex
117    */
118   int GetDepthIndex() const;
119
120   /**
121    * @copydoc Toolkit::Visual::Base::SetOnStage
122    * @pre Impl->mGeometry must be created before this method is called
123    */
124   void SetOnStage( Actor& actor );
125
126   /**
127    * @copydoc Toolkit::Visual::Base::SetOffStage
128    */
129   void SetOffStage( Actor& actor );
130
131   /**
132    * @copydoc Toolkit::Visual::Base::CreatePropertyMap
133    */
134   void CreatePropertyMap( Property::Map& map ) const;
135
136   /**
137    * @brief Create a property map containing per-instance visual properties.
138    *
139    * This will enable creation of new visuals on control state change with
140    * any alternative style properties and the relevant instance properties
141    * (e.g. for image visual, the desired size, and for text visual, the actual text).
142    * @param[in] map The property map into which to write
143    */
144   void CreateInstancePropertyMap( Property::Map& map ) const;
145
146   /**
147    * @brief Set whether the Pre-multiplied Alpha Blending is required
148    *
149    * @param[in] preMultiplied whether alpha is pre-multiplied.
150    */
151   void EnablePreMultipliedAlpha( bool preMultiplied );
152
153   /**
154    * @brief Query whether alpha is pre-multiplied.
155    *
156    * @return True is alpha is pre-multiplied, false otherwise.
157    */
158   bool IsPreMultipliedAlphaEnabled() const;
159
160   /**
161    * @brief Sets properties of custom shader
162    * @param[in] propertyMap Property map containing the custom shader data
163    */
164   void SetCustomShader( const Property::Map& propertyMap );
165
166   /**
167    * @copydoc Toolkit::Visual::Base::SetProperty
168    */
169   void SetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
170
171   /**
172    * @copydoc Toolkit::Visual::Base::GetProperty
173    */
174   Dali::Property::Value GetProperty( Dali::Property::Index index );
175
176   /**
177    * Gets currently staged renderer, or an empty handle if not staged
178    */
179   Renderer GetRenderer();
180
181   /**
182    * Sets the mix color ( including opacity )  of the visual.
183    * @param[in] mixColor The new mix color
184    */
185   void SetMixColor( const Vector4& color );
186
187   /**
188    * Sets the mix color of the visual.
189    * @param[in] mixColor The new mix color
190    */
191   void SetMixColor( const Vector3& color );
192
193   /**
194    * Gets the mix color of the visual.
195    * @return The mix color
196    */
197   const Vector4& GetMixColor() const;
198
199   /**
200    * Animate the property if it exists in the visual or renderer.
201    *
202    * If it's a visual property such as mix color or a transform property,
203    * saves the target value to the local data.
204    *
205    * If the visual isn't staged (i.e. it doesn't have a renderer),
206    * then this will not add an animation.
207    *
208    * @param[in] transition The animation to create or attach to
209    * @param[in] animator The animation parameters of the property.
210    */
211   void AnimateProperty( Dali::Animation& transition,
212                         Internal::TransitionData::Animator& animator );
213
214   /**
215    * @brief Add an observer to watch for when the Visuals resources are loaded.
216    * Currently only supports a single observer
217    *
218    */
219   void AddResourceObserver( Visual::ResourceObserver& observer );
220
221   /**
222    * @brief Remove an observer
223    */
224   void RemoveResourceObserver( Visual::ResourceObserver& observer );
225
226   /**
227    * @brief Called when the visuals resources are loaded / ready
228    */
229   void ResourceReady( Toolkit::Visual::ResourceStatus resourceStatus );
230
231   /**
232    * @brief Called when the visuals resources are loaded / ready
233    * @return true if ready, false otherwise
234    */
235   virtual bool IsResourceReady() const;
236
237   /**
238    * @brief Get the loading state of the visual resource
239    * @return Return the loading status (PREPARING, READY and FAILED) of visual resource
240    */
241   Toolkit::Visual::ResourceStatus GetResourceStatus() const;
242
243 protected:
244
245   /**
246    * @brief Constructor.
247    *
248    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
249    */
250   Base( VisualFactoryCache& factoryCache );
251
252   /**
253    * @brief A reference counted object may only be deleted by calling Unreference().
254    */
255   virtual ~Base();
256
257 protected:
258
259   /**
260    * @brief Called by CreatePropertyMap() allowing sub classes to respond to the CreatePropertyMap event
261    *
262    * @param[out] map The visual property map.
263    */
264   virtual void DoCreatePropertyMap( Property::Map& map ) const = 0;
265
266   /**
267    * @brief Called by CreateInstancePropertyMap() allowing derived
268    * classes to store instanced data (separate to styled data) that
269    * needs copying between visuals on state change.
270    *
271    * @param[out] map The visual property map
272    */
273   virtual void DoCreateInstancePropertyMap( Property::Map& map ) const = 0;
274
275   /**
276    * @brief Called by SetProperties() allowing sub classes to set their properties
277    *
278    * @param[in] propertyMap The properties for the requested Visual object.
279    */
280   virtual void DoSetProperties( const Property::Map& propertyMap ) = 0;
281
282   /**
283    * @brief Called when transform or control size changes
284    * ( Of use to SVG and Text visuals )
285    */
286   virtual void OnSetTransform() = 0;
287
288   /**
289    * @brief Called by SetOnStage() allowing sub classes to respond to the SetOnStage event
290    *
291    * @note The derived class is required to create the renderer, and add it to the actor when all the resources are in place.
292    *
293    * @param[in] actor The actor applying this visual.
294    */
295   virtual void DoSetOnStage( Actor& actor )=0;
296
297   /**
298    * @brief Called by SetOffStage() allowing sub classes to respond to the SetOffStage event
299    *
300    * @param[in] actor The actor applying this visual.
301    */
302   virtual void DoSetOffStage( Actor& actor );
303
304 protected:
305
306   /**
307    * @brief Gets the on stage state for this Visual
308    *
309    * @return Returns true if this Visual is on stage, false if it is off the stage
310    */
311   bool IsOnStage() const;
312
313 private:
314
315   /**
316    * Register the mix color uniform on the Renderer and store the property index.
317    * Note, this is not used by Color or Primitive Visuals, which will use their
318    * own property index.
319    */
320   void RegisterMixColor();
321
322   /**
323    * Find the matching property on the renderer or shader. If it's a shader
324    * property, register it on the renderer in order to animate it for this
325    * visual independently.
326    * @param[in] key The key to match.
327    * @return the matching index, or INVALID_INDEX if it's not found
328    */
329   Property::Index GetPropertyIndex( Property::Key key );
330
331   /**
332    * Set up the transition. If no animation is required, then
333    * transition will be untouched.
334    *
335    * @param[in] transition The transition to use or set up.
336    * @param[in] animator The animation data to use
337    * @param[in] index The property index on the renderer to animate
338    * @param[in] initialValue The optional initial value
339    * @param[in] targetValue The target value to use
340    */
341   void SetupTransition( Dali::Animation& transition,
342                         Internal::TransitionData::Animator& animator,
343                         Property::Index index,
344                         Property::Value& initialValue,
345                         Property::Value& targetValue );
346
347   /**
348    * Animate the opacity property - Special handling to
349    * ensure that the blend mode is set to ON whilst animating,
350    * and set back to AUTO if it's opaque at the end of the
351    * animation.
352    *
353    * @param[in] transition The transition to use or set up.
354    * @param[in] animator The animation data to use
355    */
356   void AnimateOpacityProperty( Dali::Animation& transition,
357                                Internal::TransitionData::Animator& animator );
358
359   /**
360    * Animate the renderer property - no special handling
361    *
362    * @param[in] transition The transition to use or set up.
363    * @param[in] animator The animation data to use
364    */
365   void AnimateRendererProperty( Dali::Animation& transition,
366                                 Internal::TransitionData::Animator& animator );
367
368   /**
369    * Animate the mix color property.
370    *
371    * If the animator is a vec3, then it only animates the color
372    * channels without animating the opacity.  If it's a vec4, then it
373    * runs 2 animators, one for the the vec3 mixColor, and one for the
374    * opacity. (They are separate uniforms in the shader )
375    *
376    * @param[in] transition The transition to use or set up.
377    * @param[in] animator The animation data to use
378    */
379   void AnimateMixColorProperty( Dali::Animation& transition,
380                                 Internal::TransitionData::Animator& animator );
381
382   /**
383    * Set up the right blend mode if the opacity is being animated.
384    * Also ensure that when the animation finishes, the blend mode is
385    * set to the appropriate value. It also uses the target value as
386    * set into mMixColor.
387    *
388    * @param[in] transition The transition to listen to
389    * @param[in] isInitialOpaque Whether the initial value is opaque
390    * @param[in] animating If the transition animates the value.
391    */
392   void SetupBlendMode( Dali::Animation& transition,
393                        bool isInitialOpaque, bool animating );
394
395   /**
396    * When a mix color animation has finished, ensure the blend mode is set back
397    * to the right value for the target opacity.
398    */
399   void OnMixColorFinished( Animation& animation );
400
401   // Undefined
402   Base( const Visual::Base& visual );
403
404   // Undefined
405   Base& operator=( const Visual::Base& visual );
406
407 protected:
408   struct Impl;
409   Impl* mImpl;
410   VisualFactoryCache& mFactoryCache;
411 };
412
413 typedef IntrusivePtr<Base> BasePtr;
414
415 } // namspace Visual
416
417 } // namespace Internal
418
419 inline const Internal::Visual::Base& GetImplementation(const Toolkit::Visual::Base& visualBase )
420 {
421   DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
422
423   const BaseObject& handle = visualBase.GetBaseObject();
424
425   return static_cast<const Internal::Visual::Base&>(handle);
426 }
427
428 inline Internal::Visual::Base& GetImplementation(Toolkit::Visual::Base& visualBase)
429 {
430   DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
431
432   BaseObject& handle = visualBase.GetBaseObject();
433
434   return static_cast<Internal::Visual::Base&>(handle);
435 }
436
437 } // namespace Toolkit
438
439 } // namespace Dali
440
441 #endif // DALI_TOOLKIT_INTERNAL_VISUAL_H