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>
35 #include <dali-toolkit/public-api/visuals/visual-properties.h>
49 class ResourceObserver;
52 * Base class for all Control rendering logic. A control may have multiple visuals.
54 * Note: The visual responds to the the Actor::COLOR by blending it with the 'Multiply' operator.
56 * The following properties are optional
58 * | %Property Name | Type |
59 * |-------------------------|------------------|
60 * | customShader | MAP |
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 |
71 class Base : public BaseObject
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.
79 void SetProperties( const Property::Map& propertyMap );
82 * @copydoc Toolkit::Visual::Base::SetName
84 void SetName( const std::string& name );
87 * @copydoc Toolkit::Visual::Base::GetName
89 const std::string& GetName();
92 * @copydoc Toolkit::Visual::Base::SetSize
94 void SetTransformAndSize( const Property::Map& transform, Size controlSize );
97 * @brief Performs an action on the visual with the given action name and attributes.
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 )
102 void DoAction( const Dali::Property::Index actionName, const Dali::Property::Value attributes );
105 * @copydoc Toolkit::Visual::Base::GetHeightForWidth
107 virtual float GetHeightForWidth( float width );
110 * @copydoc Toolkit::Visual::Base::GetWidthForHeight
112 virtual float GetWidthForHeight( float height );
115 * @copydoc Toolkit::Visual::Base::GetNaturalSize
117 virtual void GetNaturalSize( Vector2& naturalSize );
120 * @copydoc Toolkit::Visual::Base::SetDepthIndex
122 void SetDepthIndex( int index );
125 * @copydoc Toolkit::Visual::Base::GetDepthIndex
127 int GetDepthIndex() const;
130 * @copydoc Toolkit::Visual::Base::SetOnStage
131 * @pre Impl->mGeometry must be created before this method is called
133 void SetOnStage( Actor& actor );
136 * @copydoc Toolkit::Visual::Base::SetOffStage
138 void SetOffStage( Actor& actor );
141 * @copydoc Toolkit::Visual::Base::CreatePropertyMap
143 void CreatePropertyMap( Property::Map& map ) const;
146 * @brief Create a property map containing per-instance visual properties.
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
153 void CreateInstancePropertyMap( Property::Map& map ) const;
156 * @brief Set whether the Pre-multiplied Alpha Blending is required
158 * @param[in] preMultiplied whether alpha is pre-multiplied.
160 void EnablePreMultipliedAlpha( bool preMultiplied );
163 * @brief Query whether alpha is pre-multiplied.
165 * @return True is alpha is pre-multiplied, false otherwise.
167 bool IsPreMultipliedAlphaEnabled() const;
170 * @brief Sets properties of custom shader
171 * @param[in] propertyMap Property map containing the custom shader data
173 void SetCustomShader( const Property::Map& propertyMap );
176 * @copydoc Toolkit::Visual::Base::SetProperty
178 void SetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
181 * @copydoc Toolkit::Visual::Base::GetProperty
183 Dali::Property::Value GetProperty( Dali::Property::Index index );
186 * Gets currently staged renderer, or an empty handle if not staged
188 Renderer GetRenderer();
191 * Sets the mix color ( including opacity ) of the visual.
192 * @param[in] mixColor The new mix color
194 void SetMixColor( const Vector4& color );
197 * Sets the mix color of the visual.
198 * @param[in] mixColor The new mix color
200 void SetMixColor( const Vector3& color );
203 * Gets the mix color of the visual.
204 * @return The mix color
206 const Vector4& GetMixColor() const;
209 * Animate the property if it exists in the visual or renderer.
211 * If it's a visual property such as mix color or a transform property,
212 * saves the target value to the local data.
214 * If the visual isn't staged (i.e. it doesn't have a renderer),
215 * then this will not add an animation.
217 * @param[in] transition The animation to create or attach to
218 * @param[in] animator The animation parameters of the property.
220 void AnimateProperty( Dali::Animation& transition,
221 Internal::TransitionData::Animator& animator );
224 * @brief Add an observer to watch for when the Visuals resources are loaded.
225 * Currently only supports a single observer
228 void AddResourceObserver( Visual::ResourceObserver& observer );
231 * @brief Remove an observer
233 void RemoveResourceObserver( Visual::ResourceObserver& observer );
236 * @brief Called when the visuals resources are loaded / ready
238 void ResourceReady( Toolkit::Visual::ResourceStatus resourceStatus );
241 * @brief Called when the visuals resources are loaded / ready
242 * @return true if ready, false otherwise
244 virtual bool IsResourceReady() const;
247 * @brief Get the loading state of the visual resource
248 * @return Return the loading status (PREPARING, READY and FAILED) of visual resource
250 Toolkit::Visual::ResourceStatus GetResourceStatus() const;
255 * @brief Constructor.
257 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
259 Base( VisualFactoryCache& factoryCache );
262 * @brief A reference counted object may only be deleted by calling Unreference().
269 * @brief Called by CreatePropertyMap() allowing sub classes to respond to the CreatePropertyMap event
271 * @param[out] map The visual property map.
273 virtual void DoCreatePropertyMap( Property::Map& map ) const = 0;
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.
280 * @param[out] map The visual property map
282 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const = 0;
285 * @brief Called by SetProperties() allowing sub classes to set their properties
287 * @param[in] propertyMap The properties for the requested Visual object.
289 virtual void DoSetProperties( const Property::Map& propertyMap ) = 0;
292 * @brief Called when transform or control size changes
293 * ( Of use to SVG and Text visuals )
295 virtual void OnSetTransform() = 0;
298 * @brief Called by SetOnStage() allowing sub classes to respond to the SetOnStage event
300 * @note The derived class is required to create the renderer, and add it to the actor when all the resources are in place.
302 * @param[in] actor The actor applying this visual.
304 virtual void DoSetOnStage( Actor& actor ) = 0;
307 * @brief Called by SetOffStage() allowing sub classes to respond to the SetOffStage event
309 * @param[in] actor The actor applying this visual.
311 virtual void DoSetOffStage( Actor& actor );
314 * @brief Called by DoAction() allowing sub classes to do the given action.
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 )
319 virtual void OnDoAction( const Property::Index actionId, const Property::Value& attributes );
324 * @brief Gets the on stage state for this Visual
326 * @return Returns true if this Visual is on stage, false if it is off the stage
328 bool IsOnStage() const;
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.
337 void RegisterMixColor();
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
346 Property::Index GetPropertyIndex( Property::Key key );
349 * Set up the transition. If no animation is required, then
350 * transition will be untouched.
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
358 void SetupTransition( Dali::Animation& transition,
359 Internal::TransitionData::Animator& animator,
360 Property::Index index,
361 Property::Value& initialValue,
362 Property::Value& targetValue );
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
370 * @param[in] transition The transition to use or set up.
371 * @param[in] animator The animation data to use
373 void AnimateOpacityProperty( Dali::Animation& transition,
374 Internal::TransitionData::Animator& animator );
377 * Animate the renderer property - no special handling
379 * @param[in] transition The transition to use or set up.
380 * @param[in] animator The animation data to use
382 void AnimateRendererProperty( Dali::Animation& transition,
383 Internal::TransitionData::Animator& animator );
386 * Animate the mix color property.
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 )
393 * @param[in] transition The transition to use or set up.
394 * @param[in] animator The animation data to use
396 void AnimateMixColorProperty( Dali::Animation& transition,
397 Internal::TransitionData::Animator& animator );
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.
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.
409 void SetupBlendMode( Dali::Animation& transition,
410 bool isInitialOpaque, bool animating );
413 * When a mix color animation has finished, ensure the blend mode is set back
414 * to the right value for the target opacity.
416 void OnMixColorFinished( Animation& animation );
419 Base( const Visual::Base& visual );
422 Base& operator=( const Visual::Base& visual );
427 VisualFactoryCache& mFactoryCache;
430 typedef IntrusivePtr<Base> BasePtr;
434 } // namespace Internal
436 inline const Internal::Visual::Base& GetImplementation(const Toolkit::Visual::Base& visualBase )
438 DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
440 const BaseObject& handle = visualBase.GetBaseObject();
442 return static_cast<const Internal::Visual::Base&>(handle);
445 inline Internal::Visual::Base& GetImplementation(Toolkit::Visual::Base& visualBase)
447 DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
449 BaseObject& handle = visualBase.GetBaseObject();
451 return static_cast<Internal::Visual::Base&>(handle);
454 } // namespace Toolkit
458 #endif // DALI_TOOLKIT_INTERNAL_VISUAL_H