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