8fa2e5ef54626012fc1532b3c15171020ecb67af
[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 EventObserver;
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::SetOnScene
134    * @pre Impl->mGeometry must be created before this method is called
135    */
136   void SetOnScene( Actor& actor );
137
138   /**
139    * @copydoc Toolkit::Visual::Base::SetOffScene
140    */
141   void SetOffScene( 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 Query whether advanced blend equation is applied.
174    *
175    * @return True is advanced blend equation is applied, false otherwise.
176    */
177   bool IsAdvancedBlendEquationApplied() const;
178
179   /**
180    * @brief Sets properties of custom shader
181    * @param[in] propertyMap Property map containing the custom shader data
182    */
183   void SetCustomShader( const Property::Map& propertyMap );
184
185   /**
186    * @copydoc Toolkit::Visual::Base::SetProperty
187    */
188   void SetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
189
190   /**
191    * @copydoc Toolkit::Visual::Base::GetProperty
192    */
193   Dali::Property::Value GetProperty( Dali::Property::Index index );
194
195   /**
196    * Gets currently staged renderer, or an empty handle if not staged
197    */
198   Renderer GetRenderer();
199
200   /**
201    * Sets the mix color ( including opacity )  of the visual.
202    * @param[in] mixColor The new mix color
203    */
204   void SetMixColor( const Vector4& color );
205
206   /**
207    * Sets the mix color of the visual.
208    * @param[in] mixColor The new mix color
209    */
210   void SetMixColor( const Vector3& color );
211
212   /**
213    * Gets the mix color of the visual.
214    * @return The mix color
215    */
216   const Vector4& GetMixColor() const;
217
218   /**
219    * Animate the property if it exists in the visual or renderer.
220    *
221    * If it's a visual property such as mix color or a transform property,
222    * saves the target value to the local data.
223    *
224    * If the visual isn't staged (i.e. it doesn't have a renderer),
225    * then this will not add an animation.
226    *
227    * If the animator is valid and the transition handle is empty - it will
228    * be created.
229    *
230    * @param[in] transition The animation to create or attach to
231    * @param[in] animator The animation parameters of the property.
232    */
233   void AnimateProperty( Dali::Animation& transition,
234                         Internal::TransitionData::Animator& animator );
235
236   /**
237    * @brief Add an observer to watch for when the Visuals have events to notify
238    * Currently only supports a single observer
239    */
240   void AddEventObserver( Visual::EventObserver& observer );
241
242   /**
243    * @brief Remove an observer
244    */
245   void RemoveEventObserver( Visual::EventObserver& observer );
246
247   /**
248    * @brief Called when the visuals resources are loaded / ready
249    */
250   void ResourceReady( Toolkit::Visual::ResourceStatus resourceStatus );
251
252   /**
253    * @brief Called when the visuals resources are loaded / ready
254    * @return true if ready, false otherwise
255    */
256   virtual bool IsResourceReady() const;
257
258   /**
259    * @brief Get the loading state of the visual resource
260    * @return Return the loading status (PREPARING, READY and FAILED) of visual resource
261    */
262   Toolkit::Visual::ResourceStatus GetResourceStatus() const;
263
264   /**
265    * @brief Get the fitting mode for the visual
266    */
267   FittingMode GetFittingMode() const;
268
269   /**
270    * @brief Get the actual Visual Object.
271    * @return The actual visual object
272    * @note Should be overridden by deriving controls if they are acting as a proxy to other visual objects.
273    */
274   virtual Base& GetVisualObject();
275
276   /**
277    * @brief Query whether resources requires to be loaded synchronously.
278    * @return Returns true if synchronous resource loading is required, false otherwise.
279    */
280   bool IsSynchronousLoadingRequired() const;
281
282   /**
283    * @brief Get the type of this visual.
284    *
285    * @return The the type of this visual.
286    */
287   Toolkit::Visual::Type GetType() const;
288
289  protected:
290
291   /**
292    * @brief Constructor.
293    *
294    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
295    * @param[in] fittingMode The value that determines how the visual should be fit to the view
296    * @param[in] type The type of the this visual
297    */
298   Base( VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type );
299
300   /**
301    * @brief A reference counted object may only be deleted by calling Unreference().
302    */
303   ~Base() override;
304
305 protected:
306
307   /**
308    * @brief Called by CreatePropertyMap() allowing sub classes to respond to the CreatePropertyMap event
309    *
310    * @param[out] map The visual property map.
311    */
312   virtual void DoCreatePropertyMap( Property::Map& map ) const = 0;
313
314   /**
315    * @brief Called by CreateInstancePropertyMap() allowing derived
316    * classes to store instanced data (separate to styled data) that
317    * needs copying between visuals on state change.
318    *
319    * @param[out] map The visual property map
320    */
321   virtual void DoCreateInstancePropertyMap( Property::Map& map ) const = 0;
322
323   /**
324    * @brief Called by SetProperties() allowing sub classes to set their properties
325    *
326    * @param[in] propertyMap The properties for the requested Visual object.
327    */
328   virtual void DoSetProperties( const Property::Map& propertyMap ) = 0;
329
330   /**
331    * @brief Called when transform or control size changes
332    * ( Of use to SVG and Text visuals )
333    */
334   virtual void OnSetTransform() = 0;
335
336   /**
337    * @brief Called by SetOnScene() allowing sub classes to respond to the SetOnScene event
338    *
339    * @note The derived class is required to create the renderer, and add it to the actor when all the resources are in place.
340    *
341    * @param[in] actor The actor applying this visual.
342    */
343   virtual void DoSetOnScene( Actor& actor ) = 0;
344
345   /**
346    * @brief Called by SetOffScene() allowing sub classes to respond to the SetOffScene event
347    *
348    * @param[in] actor The actor applying this visual.
349    */
350   virtual void DoSetOffScene( Actor& actor );
351
352   /**
353    * @brief Called by DoAction() allowing sub classes to do the given action.
354    *
355    * @param[in] actionId The action to perform
356    * @param[in] attributes The list of attributes for the action. ( optional for this data structure to have content )
357    */
358   virtual void OnDoAction( const Property::Index actionId, const Property::Value& attributes );
359
360 protected:
361
362   /**
363    * @brief Gets the on scene state for this Visual
364    *
365    * @return Returns true if this Visual is on the scene, false if it is off the scene
366    */
367   bool IsOnScene() const;
368
369   /**
370    * @brief Query whether the corners of the visual requires to be rounded.
371    *
372    * @return Returns true if the rounded corner is required, false otherwise.
373    */
374   bool IsRoundedCornerRequired() const;
375
376 private:
377
378   /**
379    * Register the mix color uniform on the Renderer and store the property index.
380    * Note, this is not used by Color or Primitive Visuals, which will use their
381    * own property index.
382    */
383   void RegisterMixColor();
384
385   /**
386    * Find the matching property on the renderer or shader. If it's a shader
387    * property, register it on the renderer in order to animate it for this
388    * visual independently.
389    * @param[in] key The key to match.
390    * @return the matching index, or INVALID_INDEX if it's not found
391    */
392   Property::Index GetPropertyIndex( Property::Key key );
393
394   /**
395    * Set up the transition. If no animation is required, then
396    * transition will be untouched.
397    *
398    * @param[in] transition The transition to use or set up.
399    * @param[in] animator The animation data to use
400    * @param[in] index The property index on the renderer to animate
401    * @param[in] initialValue The optional initial value
402    * @param[in] targetValue The target value to use
403    */
404   void SetupTransition( Dali::Animation& transition,
405                         Internal::TransitionData::Animator& animator,
406                         Property::Index index,
407                         Property::Value& initialValue,
408                         Property::Value& targetValue );
409
410   /**
411    * Animate the opacity property - Special handling to
412    * ensure that the blend mode is set to ON whilst animating,
413    * and set back to AUTO if it's opaque at the end of the
414    * animation.
415    *
416    * @param[in] transition The transition to use or set up.
417    * @param[in] animator The animation data to use
418    */
419   void AnimateOpacityProperty( Dali::Animation& transition,
420                                Internal::TransitionData::Animator& animator );
421
422   /**
423    * Animate the renderer property - no special handling
424    *
425    * @param[in] transition The transition to use or set up.
426    * @param[in] animator The animation data to use
427    */
428   void AnimateRendererProperty( Dali::Animation& transition,
429                                 Internal::TransitionData::Animator& animator );
430
431   /**
432    * Animate the mix color property.
433    *
434    * If the animator is a vec3, then it only animates the color
435    * channels without animating the opacity.  If it's a vec4, then it
436    * runs 2 animators, one for the the vec3 mixColor, and one for the
437    * opacity. (They are separate uniforms in the shader )
438    *
439    * @param[in] transition The transition to use or set up.
440    * @param[in] animator The animation data to use
441    */
442   void AnimateMixColorProperty( Dali::Animation& transition,
443                                 Internal::TransitionData::Animator& animator );
444
445   /**
446    * Set up the right blend mode if the opacity is being animated.
447    * Also ensure that when the animation finishes, the blend mode is
448    * set to the appropriate value. It also uses the target value as
449    * set into mMixColor.
450    *
451    * @param[in] transition The transition to listen to
452    * @param[in] isInitialOpaque Whether the initial value is opaque
453    * @param[in] animating If the transition animates the value.
454    */
455   void SetupBlendMode( Dali::Animation& transition,
456                        bool isInitialOpaque, bool animating );
457
458   /**
459    * When a mix color animation has finished, ensure the blend mode is set back
460    * to the right value for the target opacity.
461    */
462   void OnMixColorFinished( Animation& animation );
463
464   // Undefined
465   Base( const Visual::Base& visual );
466
467   // Undefined
468   Base& operator=( const Visual::Base& visual );
469
470 protected:
471   struct Impl;
472   Impl* mImpl;
473   VisualFactoryCache& mFactoryCache;
474 };
475
476 typedef IntrusivePtr<Base> BasePtr;
477
478 } // namspace Visual
479
480 } // namespace Internal
481
482 inline const Internal::Visual::Base& GetImplementation(const Toolkit::Visual::Base& visualBase )
483 {
484   DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
485
486   const BaseObject& handle = visualBase.GetBaseObject();
487
488   return static_cast<const Internal::Visual::Base&>(handle);
489 }
490
491 inline Internal::Visual::Base& GetImplementation(Toolkit::Visual::Base& visualBase)
492 {
493   DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
494
495   BaseObject& handle = visualBase.GetBaseObject();
496
497   return static_cast<Internal::Visual::Base&>(handle);
498 }
499
500 } // namespace Toolkit
501
502 } // namespace Dali
503
504 #endif // DALI_TOOLKIT_INTERNAL_VISUAL_H