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