1 #ifndef DALI_TOOLKIT_INTERNAL_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_VISUAL_H
5 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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>
48 class ResourceObserver;
51 * Base class for all Control rendering logic. A control may have multiple visuals.
53 * Note: The visual responds to the the Actor::COLOR by blending it with the 'Multiply' operator.
55 * The following properties are optional
57 * | %Property Name | Type |
58 * |-------------------------|------------------|
59 * | customShader | MAP |
61 * where custom-shader is a map with the following properties:
62 * | %Property Name | Type |
63 * |-------------------------|------------------|
64 * | vertexShader | STRING |
65 * | fragmentShader | STRING |
66 * | subdivideGridX | INT |
67 * | subdivideGridY | INT |
68 * | shaderHints | INT |
70 class Base : public BaseObject
75 * Setting the properties of the visual, this API should only called by the VisualFactory
76 * @param[in] propertyMap The properties for the requested Visual object.
78 void SetProperties( const Property::Map& propertyMap );
81 * @copydoc Toolkit::Visual::Base::SetName
83 void SetName( const std::string& name );
86 * @copydoc Toolkit::Visual::Base::GetName
88 const std::string& GetName();
91 * @copydoc Toolkit::Visual::Base::SetSize
93 void SetTransformAndSize( const Property::Map& transform, Size controlSize );
96 * @copydoc Toolkit::Visual::Base::GetHeightForWidth
98 virtual float GetHeightForWidth( float width );
101 * @copydoc Toolkit::Visual::Base::GetWidthForHeight
103 virtual float GetWidthForHeight( float height );
106 * @copydoc Toolkit::Visual::Base::GetNaturalSize
108 virtual void GetNaturalSize( Vector2& naturalSize );
111 * @copydoc Toolkit::Visual::Base::SetDepthIndex
113 void SetDepthIndex( int index );
116 * @copydoc Toolkit::Visual::Base::GetDepthIndex
118 int GetDepthIndex() const;
121 * @copydoc Toolkit::Visual::Base::SetOnStage
122 * @pre Impl->mGeometry must be created before this method is called
124 void SetOnStage( Actor& actor );
127 * @copydoc Toolkit::Visual::Base::SetOffStage
129 void SetOffStage( Actor& actor );
132 * @copydoc Toolkit::Visual::Base::CreatePropertyMap
134 void CreatePropertyMap( Property::Map& map ) const;
137 * @brief Create a property map containing per-instance visual properties.
139 * This will enable creation of new visuals on control state change with
140 * any alternative style properties and the relevant instance properties
141 * (e.g. for image visual, the desired size, and for text visual, the actual text).
142 * @param[in] map The property map into which to write
144 void CreateInstancePropertyMap( Property::Map& map ) const;
147 * @brief Set whether the Pre-multiplied Alpha Blending is required
149 * @param[in] preMultiplied whether alpha is pre-multiplied.
151 void EnablePreMultipliedAlpha( bool preMultiplied );
154 * @brief Query whether alpha is pre-multiplied.
156 * @return True is alpha is pre-multiplied, false otherwise.
158 bool IsPreMultipliedAlphaEnabled() const;
161 * @brief Sets properties of custom shader
162 * @param[in] propertyMap Property map containing the custom shader data
164 void SetCustomShader( const Property::Map& propertyMap );
167 * @copydoc Toolkit::Visual::Base::SetProperty
169 void SetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
172 * @copydoc Toolkit::Visual::Base::GetProperty
174 Dali::Property::Value GetProperty( Dali::Property::Index index );
177 * Gets currently staged renderer, or an empty handle if not staged
179 Renderer GetRenderer();
182 * Sets the mix color ( including opacity ) of the visual.
183 * @param[in] mixColor The new mix color
185 void SetMixColor( const Vector4& color );
188 * Sets the mix color of the visual.
189 * @param[in] mixColor The new mix color
191 void SetMixColor( const Vector3& color );
194 * Gets the mix color of the visual.
195 * @return The mix color
197 const Vector4& GetMixColor() const;
200 * Animate the property if it exists in the visual or renderer.
202 * If it's a visual property such as mix color or a transform property,
203 * saves the target value to the local data.
205 * If the visual isn't staged (i.e. it doesn't have a renderer),
206 * then this will not add an animation.
208 * @param[in] transition The animation to create or attach to
209 * @param[in] animator The animation parameters of the property.
211 void AnimateProperty( Dali::Animation& transition,
212 Internal::TransitionData::Animator& animator );
215 * @brief Add an observer to watch for when the Visuals resources are loaded.
216 * Currently only supports a single observer
219 void AddResourceObserver( Visual::ResourceObserver& observer );
222 * @brief Remove an observer
224 void RemoveResourceObserver( Visual::ResourceObserver& observer );
227 * @brief Called when the visuals resources are loaded / ready
229 void ResourceReady();
232 * @brief Called when the visuals resources are loaded / ready
233 * @return true if ready, false otherwise
235 bool IsResourceReady() const;
240 * @brief Constructor.
242 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
244 Base( VisualFactoryCache& factoryCache );
247 * @brief A reference counted object may only be deleted by calling Unreference().
254 * @brief Called by CreatePropertyMap() allowing sub classes to respond to the CreatePropertyMap event
256 * @param[out] map The visual property map.
258 virtual void DoCreatePropertyMap( Property::Map& map ) const = 0;
261 * @brief Called by CreateInstancePropertyMap() allowing derived
262 * classes to store instanced data (separate to styled data) that
263 * needs copying between visuals on state change.
265 * @param[out] map The visual property map
267 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const = 0;
270 * @brief Called by SetProperties() allowing sub classes to set their properties
272 * @param[in] propertyMap The properties for the requested Visual object.
274 virtual void DoSetProperties( const Property::Map& propertyMap ) = 0;
277 * @brief Called when transform or control size changes
278 * ( Of use to SVG and Text visuals )
280 virtual void OnSetTransform() = 0;
283 * @brief Called by SetOnStage() allowing sub classes to respond to the SetOnStage event
285 * @note The derived class is required to create the renderer, and add it to the actor when all the resources are in place.
287 * @param[in] actor The actor applying this visual.
289 virtual void DoSetOnStage( Actor& actor )=0;
292 * @brief Called by SetOffStage() allowing sub classes to respond to the SetOffStage event
294 * @param[in] actor The actor applying this visual.
296 virtual void DoSetOffStage( Actor& actor );
301 * @brief Gets the on stage state for this Visual
303 * @return Returns true if this Visual is on stage, false if it is off the stage
305 bool IsOnStage() const;
310 * Register the mix color uniform on the Renderer and store the property index.
311 * Note, this is not used by Color or Primitive Visuals, which will use their
312 * own property index.
314 void RegisterMixColor();
317 * Find the matching property on the renderer or shader. If it's a shader
318 * property, register it on the renderer in order to animate it for this
319 * visual independently.
320 * @param[in] key The key to match.
321 * @return the matching index, or INVALID_INDEX if it's not found
323 Property::Index GetPropertyIndex( Property::Key key );
326 * Set up the transition. If no animation is required, then
327 * transition will be untouched.
329 * @param[in] transition The transition to use or set up.
330 * @param[in] animator The animation data to use
331 * @param[in] index The property index on the renderer to animate
332 * @param[in] initialValue The optional initial value
333 * @param[in] targetValue The target value to use
335 void SetupTransition( Dali::Animation& transition,
336 Internal::TransitionData::Animator& animator,
337 Property::Index index,
338 Property::Value& initialValue,
339 Property::Value& targetValue );
342 * Animate the opacity property - Special handling to
343 * ensure that the blend mode is set to ON whilst animating,
344 * and set back to AUTO if it's opaque at the end of the
347 * @param[in] transition The transition to use or set up.
348 * @param[in] animator The animation data to use
350 void AnimateOpacityProperty( Dali::Animation& transition,
351 Internal::TransitionData::Animator& animator );
354 * Animate the renderer property - no special handling
356 * @param[in] transition The transition to use or set up.
357 * @param[in] animator The animation data to use
359 void AnimateRendererProperty( Dali::Animation& transition,
360 Internal::TransitionData::Animator& animator );
363 * Animate the mix color property.
365 * If the animator is a vec3, then it only animates the color
366 * channels without animating the opacity. If it's a vec4, then it
367 * runs 2 animators, one for the the vec3 mixColor, and one for the
368 * opacity. (They are separate uniforms in the shader )
370 * @param[in] transition The transition to use or set up.
371 * @param[in] animator The animation data to use
373 void AnimateMixColorProperty( Dali::Animation& transition,
374 Internal::TransitionData::Animator& animator );
377 * Set up the right blend mode if the opacity is being animated.
378 * Also ensure that when the animation finishes, the blend mode is
379 * set to the appropriate value. It also uses the target value as
380 * set into mMixColor.
382 * @param[in] transition The transition to listen to
383 * @param[in] isInitialOpaque Whether the initial value is opaque
384 * @param[in] animating If the transition animates the value.
386 void SetupBlendMode( Dali::Animation& transition,
387 bool isInitialOpaque, bool animating );
390 * When a mix color animation has finished, ensure the blend mode is set back
391 * to the right value for the target opacity.
393 void OnMixColorFinished( Animation& animation );
396 Base( const Visual::Base& visual );
399 Base& operator=( const Visual::Base& visual );
404 VisualFactoryCache& mFactoryCache;
407 typedef IntrusivePtr<Base> BasePtr;
411 } // namespace Internal
413 inline const Internal::Visual::Base& GetImplementation(const Toolkit::Visual::Base& visualBase )
415 DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
417 const BaseObject& handle = visualBase.GetBaseObject();
419 return static_cast<const Internal::Visual::Base&>(handle);
422 inline Internal::Visual::Base& GetImplementation(Toolkit::Visual::Base& visualBase)
424 DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
426 BaseObject& handle = visualBase.GetBaseObject();
428 return static_cast<Internal::Visual::Base&>(handle);
431 } // namespace Toolkit
435 #endif // DALI_TOOLKIT_INTERNAL_VISUAL_H