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