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 * @copydoc Toolkit::Visual::Base::GetHeightForWidth
105 virtual float GetHeightForWidth(float width);
108 * @copydoc Toolkit::Visual::Base::GetWidthForHeight
110 virtual float GetWidthForHeight(float height);
113 * @copydoc Toolkit::Visual::Base::GetNaturalSize
115 virtual void GetNaturalSize(Vector2& naturalSize);
118 * @copydoc Toolkit::Visual::Base::SetDepthIndex
120 void SetDepthIndex(int index);
123 * @copydoc Toolkit::Visual::Base::GetDepthIndex
125 int GetDepthIndex() const;
128 * @copydoc Toolkit::Visual::Base::SetOnScene
129 * @pre Impl->mGeometry must be created before this method is called
131 void SetOnScene(Actor& actor);
134 * @copydoc Toolkit::Visual::Base::SetOffScene
136 void SetOffScene(Actor& actor);
139 * @copydoc Toolkit::Visual::Base::CreatePropertyMap
141 void CreatePropertyMap(Property::Map& map) const;
144 * @brief Create a property map containing per-instance visual properties.
146 * This will enable creation of new visuals on control state change with
147 * any alternative style properties and the relevant instance properties
148 * (e.g. for image visual, the desired size, and for text visual, the actual text).
149 * @param[in] map The property map into which to write
151 void CreateInstancePropertyMap(Property::Map& map) const;
154 * @brief Set whether the Pre-multiplied Alpha Blending is required
156 * @param[in] preMultiplied whether alpha is pre-multiplied.
158 virtual void EnablePreMultipliedAlpha(bool preMultiplied);
161 * @brief Query whether alpha is pre-multiplied.
163 * @return True is alpha is pre-multiplied, false otherwise.
165 bool IsPreMultipliedAlphaEnabled() const;
168 * @brief Sets properties of custom shader
169 * @param[in] propertyMap Property map containing the custom shader data
171 void SetCustomShader(const Property::Map& propertyMap);
174 * @copydoc Toolkit::Visual::Base::SetProperty
176 void SetProperty(Dali::Property::Index index, const Dali::Property::Value& propertyValue);
179 * @copydoc Toolkit::Visual::Base::GetProperty
181 Dali::Property::Value GetProperty(Dali::Property::Index index);
184 * Gets currently staged renderer, or an empty handle if not staged
186 Renderer GetRenderer();
189 * Convert all string keys to int keys
190 * @param[in] key The key to convert
191 * @return the index key supplied or matching, or INVALID_INDEX if no match
193 static Property::Index GetIntKey(Property::Key key);
196 * Sets the mix color ( including opacity ) of the visual.
197 * @param[in] mixColor The new mix color
199 void SetMixColor(const Vector4& color);
202 * Sets the mix color of the visual.
203 * @param[in] mixColor The new mix color
205 void SetMixColor(const Vector3& color);
208 * Animate the property if it exists in the visual or renderer.
210 * If it's a visual property such as mix color or a transform property,
211 * saves the target value to the local data.
213 * If the visual isn't staged (i.e. it doesn't have a renderer),
214 * then this will not add an animation.
216 * If the animator is valid and the transition handle is empty - it will
219 * @param[in] transition The animation to create or attach to
220 * @param[in] animator The animation parameters of the property.
222 void AnimateProperty(Dali::Animation& transition,
223 Internal::TransitionData::Animator& animator);
226 * @brief Add an observer to watch for when the Visuals have events to notify
227 * Currently only supports a single observer
229 void AddEventObserver(Visual::EventObserver& observer);
232 * @brief Remove an observer
234 void RemoveEventObserver(Visual::EventObserver& observer);
237 * @brief Called when the visuals resources are loaded / ready
239 void ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus);
242 * @brief Called when the visuals resources are loaded / ready or failed (mean, broken image ready)
243 * @return true if ready or failed (mean, broken image ready), false otherwise
245 virtual bool IsResourceReady() const;
248 * @brief Get the loading state of the visual resource
249 * @return Return the loading status (PREPARING, READY and FAILED) of visual resource
251 Toolkit::Visual::ResourceStatus GetResourceStatus() const;
254 * @brief Get the fitting mode for the visual
256 FittingMode GetFittingMode() const;
259 * @brief Get the actual Visual Object.
260 * @return The actual visual object
261 * @note Should be overridden by deriving controls if they are acting as a proxy to other visual objects.
263 virtual Base& GetVisualObject();
266 * @brief Query whether resources requires to be loaded synchronously.
267 * @return Returns true if synchronous resource loading is required, false otherwise.
269 bool IsSynchronousLoadingRequired() const;
272 * @brief Get the type of this visual.
274 * @return The the type of this visual.
276 Toolkit::Visual::Type GetType() const;
279 * @brief Retrieve the property object associated with the property key.
281 * @param[in] key The Property key of the visual.
282 * @return The Property object
284 Dali::Property GetPropertyObject(Dali::Property::Key key);
288 * @brief Constructor.
290 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
291 * @param[in] fittingMode The value that determines how the visual should be fit to the view
292 * @param[in] type The type of the this visual
294 Base(VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type);
297 * @brief A reference counted object may only be deleted by calling Unreference().
302 * @brief Second-phase constructor.
308 * @brief Called by Initialize() allowing sub classes to initialize them.
310 virtual void OnInitialize() = 0;
313 * @brief Called by CreatePropertyMap() allowing sub classes to respond to the CreatePropertyMap event
315 * @param[out] map The visual property map.
317 virtual void DoCreatePropertyMap(Property::Map& map) const = 0;
320 * @brief Called by CreateInstancePropertyMap() allowing derived
321 * classes to store instanced data (separate to styled data) that
322 * needs copying between visuals on state change.
324 * @param[out] map The visual property map
326 virtual void DoCreateInstancePropertyMap(Property::Map& map) const = 0;
329 * @brief Called by SetProperties() allowing sub classes to set their properties
331 * @param[in] propertyMap The properties for the requested Visual object.
333 virtual void DoSetProperties(const Property::Map& propertyMap) = 0;
336 * @brief Called when transform or control size changes
337 * ( Of use to SVG and Text visuals )
339 virtual void OnSetTransform() = 0;
342 * @brief Called by SetOnScene() allowing sub classes to respond to the SetOnScene event
344 * @note The derived class is required to create the renderer, and add it to the actor when all the resources are in place.
346 * @param[in] actor The actor applying this visual.
348 virtual void DoSetOnScene(Actor& actor) = 0;
351 * @brief Called by SetOffScene() allowing sub classes to respond to the SetOffScene event
353 * @param[in] actor The actor applying this visual.
355 virtual void DoSetOffScene(Actor& actor);
358 * @brief Called by DoAction() allowing sub classes to do the given action.
360 * @param[in] actionId The action to perform
361 * @param[in] attributes The list of attributes for the action. ( optional for this data structure to have content )
363 virtual void OnDoAction(const Property::Index actionId, const Property::Value& attributes);
366 * @brief Update the shader when some properties are changed.
368 virtual void UpdateShader()
373 * @brief Generate a shader by the current properties from each sub classes's own shader-generate logic.
374 * @param[in] factoryCache The visual factory cache to store the generated shader.
375 * @return If the function defined, Generated shader by the current properties. Else, empty shader.
377 virtual Dali::Shader GenerateShader() const
379 return Dali::Shader();
383 * @brief Called by GetPropertyObject() allowing sub classes to respond to the GetPropertyObject event
384 * @note The derived class is required to register the given property.
385 * @param[in] key The key of the visual's property.
386 * @return The Property object
388 virtual Dali::Property OnGetPropertyObject(Dali::Property::Key key)
391 return Dali::Property(handle, Property::INVALID_INDEX);
396 * @brief Gets the on scene state for this Visual
398 * @return Returns true if this Visual is on the scene, false if it is off the scene
400 bool IsOnScene() const;
403 * @brief Query whether the corners of the visual requires to be rounded.
405 * @return Returns true if the rounded corner is required, false otherwise.
407 bool IsRoundedCornerRequired() const;
410 * @brief Query whether the borderline of the visual requires to be rendered.
412 * @return Returns true if the outline is required, false otherwise.
414 bool IsBorderlineRequired() const;
418 * Register the mix color uniform on the Renderer and store the property index.
419 * Note, this is not used by Color or Primitive Visuals, which will use their
420 * own property index.
422 void RegisterMixColor();
425 * Register the uniform on the DecoratedVisualRenderer and store the property index if necessary.
427 void RegisterDecoration();
430 * Find the matching property on the renderer or shader. If it's a shader
431 * property, register it on the renderer in order to animate it for this
432 * visual independently.
433 * @param[in] key The key to match.
434 * @return the matching index, or INVALID_INDEX if it's not found
436 Property::Index GetPropertyIndex(Property::Key key);
439 * Set up the transition. If no animation is required, then
440 * transition will be untouched.
442 * @param[in] transition The transition to use or set up.
443 * @param[in] animator The animation data to use
444 * @param[in] index The property index on the renderer to animate
445 * @param[in] initialValue The optional initial value
446 * @param[in] targetValue The target value to use
448 void SetupTransition(Dali::Animation& transition,
449 Internal::TransitionData::Animator& animator,
450 Property::Index index,
451 Property::Value& initialValue,
452 Property::Value& targetValue);
455 * Animate the opacity property - Special handling to
456 * ensure that the blend mode is set to ON whilst animating,
457 * and set back to AUTO if it's opaque at the end of the
460 * @param[in] transition The transition to use or set up.
461 * @param[in] animator The animation data to use
463 void AnimateOpacityProperty(Dali::Animation& transition,
464 Internal::TransitionData::Animator& animator);
467 * Animate the renderer property - no special handling
469 * @param[in] transition The transition to use or set up.
470 * @param[in] animator The animation data to use
472 void AnimateRendererProperty(Dali::Animation& transition,
473 Internal::TransitionData::Animator& animator);
476 * Animate the mix color property.
478 * If the animator is a vec3, then it only animates the color
479 * channels without animating the opacity. If it's a vec4, then it
480 * runs 2 animators, one for the the vec3 mixColor, and one for the
481 * opacity. (They are separate uniforms in the shader )
483 * @param[in] transition The transition to use or set up.
484 * @param[in] animator The animation data to use
486 void AnimateMixColorProperty(Dali::Animation& transition,
487 Internal::TransitionData::Animator& animator);
490 Base(const Visual::Base& visual);
493 Base& operator=(const Visual::Base& visual);
498 VisualFactoryCache& mFactoryCache;
501 typedef IntrusivePtr<Base> BasePtr;
503 } // namespace Visual
505 } // namespace Internal
507 inline const Internal::Visual::Base& GetImplementation(const Toolkit::Visual::Base& visualBase)
509 DALI_ASSERT_ALWAYS(visualBase && "visual base handle is empty");
511 const BaseObject& handle = visualBase.GetBaseObject();
513 return static_cast<const Internal::Visual::Base&>(handle);
516 inline Internal::Visual::Base& GetImplementation(Toolkit::Visual::Base& visualBase)
518 DALI_ASSERT_ALWAYS(visualBase && "visual base handle is empty");
520 BaseObject& handle = visualBase.GetBaseObject();
522 return static_cast<Internal::Visual::Base&>(handle);
525 } // namespace Toolkit
529 #endif // DALI_TOOLKIT_INTERNAL_VISUAL_H