1 #ifndef DALI_TOOLKIT_INTERNAL_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_VISUAL_H
5 * Copyright (c) 2018 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>
36 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
52 using FittingMode = DevelVisual::FittingMode;
55 * Base class for all Control rendering logic. A control may have multiple visuals.
57 * Note: The visual responds to the the Actor::COLOR by blending it with the 'Multiply' operator.
59 * The following properties are optional
61 * | %Property Name | Type |
62 * |-------------------------|------------------|
63 * | customShader | MAP |
65 * where custom-shader is a map with the following properties:
66 * | %Property Name | Type |
67 * |-------------------------|------------------|
68 * | vertexShader | STRING |
69 * | fragmentShader | STRING |
70 * | subdivideGridX | INT |
71 * | subdivideGridY | INT |
72 * | shaderHints | INT |
74 class Base : public BaseObject
79 * Setting the properties of the visual, this API should only called by the VisualFactory
80 * @param[in] propertyMap The properties for the requested Visual object.
82 void SetProperties( const Property::Map& propertyMap );
85 * @copydoc Toolkit::Visual::Base::SetName
87 void SetName( const std::string& name );
90 * @copydoc Toolkit::Visual::Base::GetName
92 const std::string& GetName() const;
95 * @copydoc Toolkit::Visual::Base::SetSize
97 void SetTransformAndSize( const Property::Map& transform, Size controlSize );
100 * @brief Performs an action on the visual with the given action name and attributes.
102 * @param[in] actionName The name of the action to perform this API only takes an Index
103 * @param[in] attributes The list of attributes for the action. ( optional for this data structure to have content )
105 void DoAction( const Dali::Property::Index actionName, const Dali::Property::Value attributes );
108 * @copydoc Toolkit::Visual::Base::GetHeightForWidth
110 virtual float GetHeightForWidth( float width );
113 * @copydoc Toolkit::Visual::Base::GetWidthForHeight
115 virtual float GetWidthForHeight( float height );
118 * @copydoc Toolkit::Visual::Base::GetNaturalSize
120 virtual void GetNaturalSize( Vector2& naturalSize );
123 * @copydoc Toolkit::Visual::Base::SetDepthIndex
125 void SetDepthIndex( int index );
128 * @copydoc Toolkit::Visual::Base::GetDepthIndex
130 int GetDepthIndex() const;
133 * @copydoc Toolkit::Visual::Base::SetOnScene
134 * @pre Impl->mGeometry must be created before this method is called
136 void SetOnScene( Actor& actor );
139 * @copydoc Toolkit::Visual::Base::SetOffScene
141 void SetOffScene( Actor& actor );
144 * @copydoc Toolkit::Visual::Base::CreatePropertyMap
146 void CreatePropertyMap( Property::Map& map ) const;
149 * @brief Create a property map containing per-instance visual properties.
151 * This will enable creation of new visuals on control state change with
152 * any alternative style properties and the relevant instance properties
153 * (e.g. for image visual, the desired size, and for text visual, the actual text).
154 * @param[in] map The property map into which to write
156 void CreateInstancePropertyMap( Property::Map& map ) const;
159 * @brief Set whether the Pre-multiplied Alpha Blending is required
161 * @param[in] preMultiplied whether alpha is pre-multiplied.
163 void EnablePreMultipliedAlpha( bool preMultiplied );
166 * @brief Query whether alpha is pre-multiplied.
168 * @return True is alpha is pre-multiplied, false otherwise.
170 bool IsPreMultipliedAlphaEnabled() const;
173 * @brief Sets properties of custom shader
174 * @param[in] propertyMap Property map containing the custom shader data
176 void SetCustomShader( const Property::Map& propertyMap );
179 * @copydoc Toolkit::Visual::Base::SetProperty
181 void SetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
184 * @copydoc Toolkit::Visual::Base::GetProperty
186 Dali::Property::Value GetProperty( Dali::Property::Index index );
189 * Gets currently staged renderer, or an empty handle if not staged
191 Renderer GetRenderer();
194 * Sets the mix color ( including opacity ) of the visual.
195 * @param[in] mixColor The new mix color
197 void SetMixColor( const Vector4& color );
200 * Sets the mix color of the visual.
201 * @param[in] mixColor The new mix color
203 void SetMixColor( const Vector3& color );
206 * Gets the mix color of the visual.
207 * @return The mix color
209 const Vector4& GetMixColor() const;
212 * Animate the property if it exists in the visual or renderer.
214 * If it's a visual property such as mix color or a transform property,
215 * saves the target value to the local data.
217 * If the visual isn't staged (i.e. it doesn't have a renderer),
218 * then this will not add an animation.
220 * If the animator is valid and the transition handle is empty - it will
223 * @param[in] transition The animation to create or attach to
224 * @param[in] animator The animation parameters of the property.
226 void AnimateProperty( Dali::Animation& transition,
227 Internal::TransitionData::Animator& animator );
230 * @brief Add an observer to watch for when the Visuals have events to notify
231 * Currently only supports a single observer
233 void AddEventObserver( Visual::EventObserver& observer );
236 * @brief Remove an observer
238 void RemoveEventObserver( Visual::EventObserver& observer );
241 * @brief Called when the visuals resources are loaded / ready
243 void ResourceReady( Toolkit::Visual::ResourceStatus resourceStatus );
246 * @brief Called when the visuals resources are loaded / ready
247 * @return true if ready, false otherwise
249 virtual bool IsResourceReady() const;
252 * @brief Get the loading state of the visual resource
253 * @return Return the loading status (PREPARING, READY and FAILED) of visual resource
255 Toolkit::Visual::ResourceStatus GetResourceStatus() const;
258 * @brief Get the fitting mode for the visual
260 FittingMode GetFittingMode() const;
263 * @brief Get the actual Visual Object.
264 * @return The actual visual object
265 * @note Should be overridden by deriving controls if they are acting as a proxy to other visual objects.
267 virtual Base& GetVisualObject();
270 * @brief Query whether resources requires to be loaded synchronously.
271 * @return Returns true if synchronous resource loading is required, false otherwise.
273 bool IsSynchronousLoadingRequired() const;
276 * @brief Get the type of this visual.
278 * @return The the type of this visual.
280 Toolkit::Visual::Type GetType() const;
285 * @brief Constructor.
287 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
288 * @param[in] fittingMode The value that determines how the visual should be fit to the view
289 * @param[in] type The type of the this visual
291 Base( VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type );
294 * @brief A reference counted object may only be deleted by calling Unreference().
301 * @brief Called by CreatePropertyMap() allowing sub classes to respond to the CreatePropertyMap event
303 * @param[out] map The visual property map.
305 virtual void DoCreatePropertyMap( Property::Map& map ) const = 0;
308 * @brief Called by CreateInstancePropertyMap() allowing derived
309 * classes to store instanced data (separate to styled data) that
310 * needs copying between visuals on state change.
312 * @param[out] map The visual property map
314 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const = 0;
317 * @brief Called by SetProperties() allowing sub classes to set their properties
319 * @param[in] propertyMap The properties for the requested Visual object.
321 virtual void DoSetProperties( const Property::Map& propertyMap ) = 0;
324 * @brief Called when transform or control size changes
325 * ( Of use to SVG and Text visuals )
327 virtual void OnSetTransform() = 0;
330 * @brief Called by SetOnScene() allowing sub classes to respond to the SetOnScene event
332 * @note The derived class is required to create the renderer, and add it to the actor when all the resources are in place.
334 * @param[in] actor The actor applying this visual.
336 virtual void DoSetOnScene( Actor& actor ) = 0;
339 * @brief Called by SetOffScene() allowing sub classes to respond to the SetOffScene event
341 * @param[in] actor The actor applying this visual.
343 virtual void DoSetOffScene( Actor& actor );
346 * @brief Called by DoAction() allowing sub classes to do the given action.
348 * @param[in] actionId The action to perform
349 * @param[in] attributes The list of attributes for the action. ( optional for this data structure to have content )
351 virtual void OnDoAction( const Property::Index actionId, const Property::Value& attributes );
356 * @brief Gets the on scene state for this Visual
358 * @return Returns true if this Visual is on the scene, false if it is off the scene
360 bool IsOnScene() const;
363 * @brief Query whether the corners of the visual requires to be rounded.
365 * @return Returns true if the rounded corner is required, false otherwise.
367 bool IsRoundedCornerRequired() const;
372 * Register the mix color uniform on the Renderer and store the property index.
373 * Note, this is not used by Color or Primitive Visuals, which will use their
374 * own property index.
376 void RegisterMixColor();
379 * Find the matching property on the renderer or shader. If it's a shader
380 * property, register it on the renderer in order to animate it for this
381 * visual independently.
382 * @param[in] key The key to match.
383 * @return the matching index, or INVALID_INDEX if it's not found
385 Property::Index GetPropertyIndex( Property::Key key );
388 * Set up the transition. If no animation is required, then
389 * transition will be untouched.
391 * @param[in] transition The transition to use or set up.
392 * @param[in] animator The animation data to use
393 * @param[in] index The property index on the renderer to animate
394 * @param[in] initialValue The optional initial value
395 * @param[in] targetValue The target value to use
397 void SetupTransition( Dali::Animation& transition,
398 Internal::TransitionData::Animator& animator,
399 Property::Index index,
400 Property::Value& initialValue,
401 Property::Value& targetValue );
404 * Animate the opacity property - Special handling to
405 * ensure that the blend mode is set to ON whilst animating,
406 * and set back to AUTO if it's opaque at the end of the
409 * @param[in] transition The transition to use or set up.
410 * @param[in] animator The animation data to use
412 void AnimateOpacityProperty( Dali::Animation& transition,
413 Internal::TransitionData::Animator& animator );
416 * Animate the renderer property - no special handling
418 * @param[in] transition The transition to use or set up.
419 * @param[in] animator The animation data to use
421 void AnimateRendererProperty( Dali::Animation& transition,
422 Internal::TransitionData::Animator& animator );
425 * Animate the mix color property.
427 * If the animator is a vec3, then it only animates the color
428 * channels without animating the opacity. If it's a vec4, then it
429 * runs 2 animators, one for the the vec3 mixColor, and one for the
430 * opacity. (They are separate uniforms in the shader )
432 * @param[in] transition The transition to use or set up.
433 * @param[in] animator The animation data to use
435 void AnimateMixColorProperty( Dali::Animation& transition,
436 Internal::TransitionData::Animator& animator );
439 * Set up the right blend mode if the opacity is being animated.
440 * Also ensure that when the animation finishes, the blend mode is
441 * set to the appropriate value. It also uses the target value as
442 * set into mMixColor.
444 * @param[in] transition The transition to listen to
445 * @param[in] isInitialOpaque Whether the initial value is opaque
446 * @param[in] animating If the transition animates the value.
448 void SetupBlendMode( Dali::Animation& transition,
449 bool isInitialOpaque, bool animating );
452 * When a mix color animation has finished, ensure the blend mode is set back
453 * to the right value for the target opacity.
455 void OnMixColorFinished( Animation& animation );
458 Base( const Visual::Base& visual );
461 Base& operator=( const Visual::Base& visual );
466 VisualFactoryCache& mFactoryCache;
469 typedef IntrusivePtr<Base> BasePtr;
473 } // namespace Internal
475 inline const Internal::Visual::Base& GetImplementation(const Toolkit::Visual::Base& visualBase )
477 DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
479 const BaseObject& handle = visualBase.GetBaseObject();
481 return static_cast<const Internal::Visual::Base&>(handle);
484 inline Internal::Visual::Base& GetImplementation(Toolkit::Visual::Base& visualBase)
486 DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
488 BaseObject& handle = visualBase.GetBaseObject();
490 return static_cast<Internal::Visual::Base&>(handle);
493 } // namespace Toolkit
497 #endif // DALI_TOOLKIT_INTERNAL_VISUAL_H