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