1 #ifndef DALI_TOOLKIT_INTERNAL_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_VISUAL_H
5 * Copyright (c) 2022 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/direction-enums.h>
31 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
32 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
33 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
34 #include <dali-toolkit/internal/visuals/transition-data-impl.h>
35 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
36 #include <dali-toolkit/public-api/visuals/visual-properties.h>
48 using FittingMode = DevelVisual::FittingMode;
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
74 * Setting the properties of the visual, this API should only called by the VisualFactory
75 * @param[in] propertyMap The properties for the requested Visual object.
77 void SetProperties(const Property::Map& propertyMap);
80 * @copydoc Toolkit::Visual::Base::SetName
82 void SetName(const std::string& name);
85 * @copydoc Toolkit::Visual::Base::GetName
87 const std::string& GetName() const;
90 * @copydoc Toolkit::Visual::Base::SetSize
92 void SetTransformAndSize(const Property::Map& transform, Size controlSize);
95 * @brief Performs an action on the visual with the given action id and attributes.
97 * @param[in] actionId The id of the action to perform this API only takes an Index
98 * @param[in] attributes The list of attributes for the action. ( optional for this data structure to have content )
100 void DoAction(const Dali::Property::Index actionId, const Dali::Property::Value attributes);
103 * @brief Performs an action on the visual with the given action id and attributes.
105 * @param[in] actionId The id of the action to perform this API only takes an Index
106 * @param[in] attributes The list of attributes for the action. ( optional for this data structure to have content )
108 void DoActionExtension(const Dali::Property::Index actionId, const Dali::Any attributes);
111 * @copydoc Toolkit::Visual::Base::GetHeightForWidth
113 virtual float GetHeightForWidth(float width);
116 * @copydoc Toolkit::Visual::Base::GetWidthForHeight
118 virtual float GetWidthForHeight(float height);
121 * @copydoc Toolkit::Visual::Base::GetNaturalSize
123 virtual void GetNaturalSize(Vector2& naturalSize);
126 * @copydoc Toolkit::Visual::Base::SetDepthIndex
128 void SetDepthIndex(int index);
131 * @copydoc Toolkit::Visual::Base::GetDepthIndex
133 int GetDepthIndex() const;
136 * @copydoc Toolkit::Visual::Base::SetOnScene
137 * @pre Impl->mGeometry must be created before this method is called
139 void SetOnScene(Actor& actor);
142 * @copydoc Toolkit::Visual::Base::SetOffScene
144 void SetOffScene(Actor& actor);
147 * @copydoc Toolkit::Visual::Base::CreatePropertyMap
149 void CreatePropertyMap(Property::Map& map) const;
152 * @brief Create a property map containing per-instance visual properties.
154 * This will enable creation of new visuals on control state change with
155 * any alternative style properties and the relevant instance properties
156 * (e.g. for image visual, the desired size, and for text visual, the actual text).
157 * @param[in] map The property map into which to write
159 void CreateInstancePropertyMap(Property::Map& map) const;
162 * @brief Set whether the Pre-multiplied Alpha Blending is required
164 * @param[in] preMultiplied whether alpha is pre-multiplied.
166 virtual void EnablePreMultipliedAlpha(bool preMultiplied);
169 * @brief Query whether alpha is pre-multiplied.
171 * @return True is alpha is pre-multiplied, false otherwise.
173 bool IsPreMultipliedAlphaEnabled() const;
176 * @brief Sets properties of custom shader
177 * @param[in] propertyMap Property map containing the custom shader data
179 void SetCustomShader(const Property::Map& propertyMap);
182 * @copydoc Toolkit::Visual::Base::SetProperty
184 void SetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue);
187 * @copydoc Toolkit::Visual::Base::GetProperty
189 Dali::Property::Value GetProperty(Dali::Property::Index index);
192 * Gets currently staged renderer, or an empty handle if not staged
194 Renderer GetRenderer();
197 * Convert all string keys to int keys
198 * @param[in] key The key to convert
199 * @return the index key supplied or matching, or INVALID_INDEX if no match
201 static Property::Index GetIntKey(Property::Key key);
204 * Sets the mix color ( including opacity ) of the visual.
205 * @param[in] mixColor The new mix color
207 void SetMixColor(const Vector4& color);
210 * Sets the mix color of the visual.
211 * @param[in] mixColor The new mix color
213 void SetMixColor(const Vector3& color);
216 * Animate the property if it exists in the visual or renderer.
218 * If it's a visual property such as mix color or a transform property,
219 * saves the target value to the local data.
221 * If the visual isn't staged (i.e. it doesn't have a renderer),
222 * then this will not add an animation.
224 * If the animator is valid and the transition handle is empty - it will
227 * @param[in] transition The animation to create or attach to
228 * @param[in] animator The animation parameters of the property.
230 void AnimateProperty(Dali::Animation& transition,
231 Internal::TransitionData::Animator& animator);
234 * @brief Add an observer to watch for when the Visuals have events to notify
235 * Currently only supports a single observer
237 void AddEventObserver(Visual::EventObserver& observer);
240 * @brief Remove an observer
242 void RemoveEventObserver(Visual::EventObserver& observer);
245 * @brief Called when the visuals resources are loaded / ready
247 void ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus);
250 * @brief Called when the visuals resources are loaded / ready or failed (mean, broken image ready)
251 * @return true if ready or failed (mean, broken image ready), false otherwise
253 virtual bool IsResourceReady() const;
256 * @brief Get the loading state of the visual resource
257 * @return Return the loading status (PREPARING, READY and FAILED) of visual resource
259 Toolkit::Visual::ResourceStatus GetResourceStatus() const;
262 * @brief Get the fitting mode for the visual
264 FittingMode GetFittingMode() const;
267 * @brief Get the actual Visual Object.
268 * @return The actual visual object
269 * @note Should be overridden by deriving controls if they are acting as a proxy to other visual objects.
271 virtual Base& GetVisualObject();
274 * @brief Query whether resources requires to be loaded synchronously.
275 * @return Returns true if synchronous resource loading is required, false otherwise.
277 bool IsSynchronousLoadingRequired() const;
280 * @brief Get the type of this visual.
282 * @return The the type of this visual.
284 Toolkit::Visual::Type GetType() const;
287 * @brief Retrieve the property object associated with the property key.
289 * @param[in] key The Property key of the visual.
290 * @return The Property object
292 Dali::Property GetPropertyObject(Dali::Property::Key key);
296 * @brief Constructor.
298 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
299 * @param[in] fittingMode The value that determines how the visual should be fit to the view
300 * @param[in] type The type of the this visual
302 Base(VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type);
305 * @brief A reference counted object may only be deleted by calling Unreference().
310 * @brief Second-phase constructor.
316 * @brief Called by Initialize() allowing sub classes to initialize them.
318 virtual void OnInitialize() = 0;
321 * @brief Called by CreatePropertyMap() allowing sub classes to respond to the CreatePropertyMap event
323 * @param[out] map The visual property map.
325 virtual void DoCreatePropertyMap(Property::Map& map) const = 0;
328 * @brief Called by CreateInstancePropertyMap() allowing derived
329 * classes to store instanced data (separate to styled data) that
330 * needs copying between visuals on state change.
332 * @param[out] map The visual property map
334 virtual void DoCreateInstancePropertyMap(Property::Map& map) const = 0;
337 * @brief Called by SetProperties() allowing sub classes to set their properties
339 * @param[in] propertyMap The properties for the requested Visual object.
341 virtual void DoSetProperties(const Property::Map& propertyMap) = 0;
344 * @brief Called when transform or control size changes
345 * ( Of use to SVG and Text visuals )
347 virtual void OnSetTransform() = 0;
350 * @brief Called by SetOnScene() allowing sub classes to respond to the SetOnScene event
352 * @note The derived class is required to create the renderer, and add it to the actor when all the resources are in place.
354 * @param[in] actor The actor applying this visual.
356 virtual void DoSetOnScene(Actor& actor) = 0;
359 * @brief Called by SetOffScene() allowing sub classes to respond to the SetOffScene event
361 * @param[in] actor The actor applying this visual.
363 virtual void DoSetOffScene(Actor& actor);
366 * @brief Called by DoAction() allowing sub classes to do the given action.
368 * @param[in] actionId The action to perform
369 * @param[in] attributes The list of attributes for the action. ( optional for this data structure to have content )
371 virtual void OnDoAction(const Property::Index actionId, const Property::Value& attributes);
374 * @brief Called by DoActionExtension() allowing sub classes to do the given action.
376 * @param[in] actionId The action to perform
377 * @param[in] attributes The list of attributes for the action. ( optional for this data structure to have content )
379 virtual void OnDoActionExtension(const Property::Index actionId, Dali::Any attributes);
382 * @brief Update the shader when some properties are changed.
384 virtual void UpdateShader()
389 * @brief Generate a shader by the current properties from each sub classes's own shader-generate logic.
390 * @param[in] factoryCache The visual factory cache to store the generated shader.
391 * @return If the function defined, Generated shader by the current properties. Else, empty shader.
393 virtual Dali::Shader GenerateShader() const
395 return Dali::Shader();
399 * @brief Called by GetPropertyObject() allowing sub classes to respond to the GetPropertyObject event
400 * @note The derived class is required to register the given property.
401 * @param[in] key The key of the visual's property.
402 * @return The Property object
404 virtual Dali::Property OnGetPropertyObject(Dali::Property::Key key)
407 return Dali::Property(handle, Property::INVALID_INDEX);
412 * @brief Gets the on scene state for this Visual
414 * @return Returns true if this Visual is on the scene, false if it is off the scene
416 bool IsOnScene() const;
419 * @brief Query whether the corners of the visual requires to be rounded.
421 * @return Returns true if the rounded corner is required, false otherwise.
423 bool IsRoundedCornerRequired() const;
426 * @brief Query whether the borderline of the visual requires to be rendered.
428 * @return Returns true if the outline is required, false otherwise.
430 bool IsBorderlineRequired() const;
434 * Register the mix color uniform on the Renderer and store the property index.
435 * Note, this is not used by Color or Primitive Visuals, which will use their
436 * own property index.
438 void RegisterMixColor();
441 * Register the uniform on the DecoratedVisualRenderer and store the property index if necessary.
443 void RegisterDecoration();
446 * Find the matching property on the renderer or shader. If it's a shader
447 * property, register it on the renderer in order to animate it for this
448 * visual independently.
449 * @param[in] key The key to match.
450 * @return the matching index, or INVALID_INDEX if it's not found
452 Property::Index GetPropertyIndex(Property::Key key);
455 * Set up the transition. If no animation is required, then
456 * transition will be untouched.
458 * @param[in] transition The transition to use or set up.
459 * @param[in] animator The animation data to use
460 * @param[in] index The property index on the renderer to animate
461 * @param[in] initialValue The optional initial value
462 * @param[in] targetValue The target value to use
464 void SetupTransition(Dali::Animation& transition,
465 Internal::TransitionData::Animator& animator,
466 Property::Index index,
467 Property::Value& initialValue,
468 Property::Value& targetValue);
471 * Animate the opacity property - Special handling to
472 * ensure that the blend mode is set to ON whilst animating,
473 * and set back to AUTO if it's opaque at the end of the
476 * @param[in] transition The transition to use or set up.
477 * @param[in] animator The animation data to use
479 void AnimateOpacityProperty(Dali::Animation& transition,
480 Internal::TransitionData::Animator& animator);
483 * Animate the renderer property - no special handling
485 * @param[in] transition The transition to use or set up.
486 * @param[in] animator The animation data to use
488 void AnimateRendererProperty(Dali::Animation& transition,
489 Internal::TransitionData::Animator& animator);
492 * Animate the mix color property.
494 * If the animator is a vec3, then it only animates the color
495 * channels without animating the opacity. If it's a vec4, then it
496 * runs 2 animators, one for the the vec3 mixColor, and one for the
497 * opacity. (They are separate uniforms in the shader )
499 * @param[in] transition The transition to use or set up.
500 * @param[in] animator The animation data to use
502 void AnimateMixColorProperty(Dali::Animation& transition,
503 Internal::TransitionData::Animator& animator);
506 Base(const Visual::Base& visual);
509 Base& operator=(const Visual::Base& visual);
514 VisualFactoryCache& mFactoryCache;
517 typedef IntrusivePtr<Base> BasePtr;
519 } // namespace Visual
521 } // namespace Internal
523 inline const Internal::Visual::Base& GetImplementation(const Toolkit::Visual::Base& visualBase)
525 DALI_ASSERT_ALWAYS(visualBase && "visual base handle is empty");
527 const BaseObject& handle = visualBase.GetBaseObject();
529 return static_cast<const Internal::Visual::Base&>(handle);
532 inline Internal::Visual::Base& GetImplementation(Toolkit::Visual::Base& visualBase)
534 DALI_ASSERT_ALWAYS(visualBase && "visual base handle is empty");
536 BaseObject& handle = visualBase.GetBaseObject();
538 return static_cast<Internal::Visual::Base&>(handle);
541 } // namespace Toolkit
545 #endif // DALI_TOOLKIT_INTERNAL_VISUAL_H