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>
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 have events to notify
225 * Currently only supports a single observer
227 void AddEventObserver( Visual::EventObserver& observer );
230 * @brief Remove an observer
232 void RemoveEventObserver( Visual::EventObserver& observer );
235 * @brief Called when the visuals resources are loaded / ready
237 void ResourceReady( Toolkit::Visual::ResourceStatus resourceStatus );
240 * @brief Called when the visuals resources are loaded / ready
241 * @return true if ready, false otherwise
243 virtual bool IsResourceReady() const;
246 * @brief Get the loading state of the visual resource
247 * @return Return the loading status (PREPARING, READY and FAILED) of visual resource
249 Toolkit::Visual::ResourceStatus GetResourceStatus() const;
254 * @brief Constructor.
256 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
258 Base( VisualFactoryCache& factoryCache );
261 * @brief A reference counted object may only be deleted by calling Unreference().
268 * @brief Called by CreatePropertyMap() allowing sub classes to respond to the CreatePropertyMap event
270 * @param[out] map The visual property map.
272 virtual void DoCreatePropertyMap( Property::Map& map ) const = 0;
275 * @brief Called by CreateInstancePropertyMap() allowing derived
276 * classes to store instanced data (separate to styled data) that
277 * needs copying between visuals on state change.
279 * @param[out] map The visual property map
281 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const = 0;
284 * @brief Called by SetProperties() allowing sub classes to set their properties
286 * @param[in] propertyMap The properties for the requested Visual object.
288 virtual void DoSetProperties( const Property::Map& propertyMap ) = 0;
291 * @brief Called when transform or control size changes
292 * ( Of use to SVG and Text visuals )
294 virtual void OnSetTransform() = 0;
297 * @brief Called by SetOnStage() allowing sub classes to respond to the SetOnStage event
299 * @note The derived class is required to create the renderer, and add it to the actor when all the resources are in place.
301 * @param[in] actor The actor applying this visual.
303 virtual void DoSetOnStage( Actor& actor ) = 0;
306 * @brief Called by SetOffStage() allowing sub classes to respond to the SetOffStage event
308 * @param[in] actor The actor applying this visual.
310 virtual void DoSetOffStage( Actor& actor );
313 * @brief Called by DoAction() allowing sub classes to do the given action.
315 * @param[in] actionId The action to perform
316 * @param[in] attributes The list of attributes for the action. ( optional for this data structure to have content )
318 virtual void OnDoAction( const Property::Index actionId, const Property::Value& attributes );
323 * @brief Gets the on stage state for this Visual
325 * @return Returns true if this Visual is on stage, false if it is off the stage
327 bool IsOnStage() const;
332 * Register the mix color uniform on the Renderer and store the property index.
333 * Note, this is not used by Color or Primitive Visuals, which will use their
334 * own property index.
336 void RegisterMixColor();
339 * Find the matching property on the renderer or shader. If it's a shader
340 * property, register it on the renderer in order to animate it for this
341 * visual independently.
342 * @param[in] key The key to match.
343 * @return the matching index, or INVALID_INDEX if it's not found
345 Property::Index GetPropertyIndex( Property::Key key );
348 * Set up the transition. If no animation is required, then
349 * transition will be untouched.
351 * @param[in] transition The transition to use or set up.
352 * @param[in] animator The animation data to use
353 * @param[in] index The property index on the renderer to animate
354 * @param[in] initialValue The optional initial value
355 * @param[in] targetValue The target value to use
357 void SetupTransition( Dali::Animation& transition,
358 Internal::TransitionData::Animator& animator,
359 Property::Index index,
360 Property::Value& initialValue,
361 Property::Value& targetValue );
364 * Animate the opacity property - Special handling to
365 * ensure that the blend mode is set to ON whilst animating,
366 * and set back to AUTO if it's opaque at the end of the
369 * @param[in] transition The transition to use or set up.
370 * @param[in] animator The animation data to use
372 void AnimateOpacityProperty( Dali::Animation& transition,
373 Internal::TransitionData::Animator& animator );
376 * Animate the renderer property - no special handling
378 * @param[in] transition The transition to use or set up.
379 * @param[in] animator The animation data to use
381 void AnimateRendererProperty( Dali::Animation& transition,
382 Internal::TransitionData::Animator& animator );
385 * Animate the mix color property.
387 * If the animator is a vec3, then it only animates the color
388 * channels without animating the opacity. If it's a vec4, then it
389 * runs 2 animators, one for the the vec3 mixColor, and one for the
390 * opacity. (They are separate uniforms in the shader )
392 * @param[in] transition The transition to use or set up.
393 * @param[in] animator The animation data to use
395 void AnimateMixColorProperty( Dali::Animation& transition,
396 Internal::TransitionData::Animator& animator );
399 * Set up the right blend mode if the opacity is being animated.
400 * Also ensure that when the animation finishes, the blend mode is
401 * set to the appropriate value. It also uses the target value as
402 * set into mMixColor.
404 * @param[in] transition The transition to listen to
405 * @param[in] isInitialOpaque Whether the initial value is opaque
406 * @param[in] animating If the transition animates the value.
408 void SetupBlendMode( Dali::Animation& transition,
409 bool isInitialOpaque, bool animating );
412 * When a mix color animation has finished, ensure the blend mode is set back
413 * to the right value for the target opacity.
415 void OnMixColorFinished( Animation& animation );
418 Base( const Visual::Base& visual );
421 Base& operator=( const Visual::Base& visual );
426 VisualFactoryCache& mFactoryCache;
429 typedef IntrusivePtr<Base> BasePtr;
433 } // namespace Internal
435 inline const Internal::Visual::Base& GetImplementation(const Toolkit::Visual::Base& visualBase )
437 DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
439 const BaseObject& handle = visualBase.GetBaseObject();
441 return static_cast<const Internal::Visual::Base&>(handle);
444 inline Internal::Visual::Base& GetImplementation(Toolkit::Visual::Base& visualBase)
446 DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
448 BaseObject& handle = visualBase.GetBaseObject();
450 return static_cast<Internal::Visual::Base&>(handle);
453 } // namespace Toolkit
457 #endif // DALI_TOOLKIT_INTERNAL_VISUAL_H