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