1 #ifndef DALI_TOOLKIT_INTERNAL_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_VISUAL_H
5 * Copyright (c) 2016 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>
48 * Base class for all Control rendering logic. A control may have multiple visuals.
50 * Note: The visual responds to the the Actor::COLOR by blending it with the 'Multiply' operator.
52 * The following properties are optional
54 * | %Property Name | Type |
55 * |-------------------------|------------------|
56 * | customShader | MAP |
58 * where custom-shader is a map with the following properties:
59 * | %Property Name | Type |
60 * |-------------------------|------------------|
61 * | vertexShader | STRING |
62 * | fragmentShader | STRING |
63 * | subdivideGridX | INT |
64 * | subdivideGridY | INT |
65 * | shaderHints | INT |
67 class Base : public BaseObject
72 * Setting the properties of the visual, this API should only called by the VisualFactory
73 * @param[in] propertyMap The properties for the requested Visual object.
75 void SetProperties( const Property::Map& propertyMap );
78 * @copydoc Toolkit::Visual::Base::SetName
80 void SetName( const std::string& name );
83 * @copydoc Toolkit::Visual::Base::GetName
85 const std::string& GetName();
88 * @copydoc Toolkit::Visual::Base::SetSize
90 void SetTransformAndSize( const Property::Map& transform, Size controlSize );
93 * @copydoc Toolkit::Visual::Base::GetHeightForWidth
95 virtual float GetHeightForWidth( float width );
98 * @copydoc Toolkit::Visual::Base::GetWidthForHeight
100 virtual float GetWidthForHeight( float height );
103 * @copydoc Toolkit::Visual::Base::GetNaturalSize
105 virtual void GetNaturalSize( Vector2& naturalSize );
108 * @copydoc Toolkit::Visual::Base::SetDepthIndex
110 void SetDepthIndex( float index );
113 * @copydoc Toolkit::Visual::Base::GetDepthIndex
115 float GetDepthIndex() const;
118 * @copydoc Toolkit::Visual::Base::SetOnStage
119 * @pre Impl->mGeometry must be created before this method is called
121 void SetOnStage( Actor& actor );
124 * @copydoc Toolkit::Visual::Base::SetOffStage
126 void SetOffStage( Actor& actor );
129 * @copydoc Toolkit::Visual::Base::CreatePropertyMap
131 void CreatePropertyMap( Property::Map& map ) const;
134 * @brief Set whether the Pre-multiplied Alpha Blending is required
136 * @param[in] preMultipled whether alpha is pre-multiplied.
138 void EnablePreMultipliedAlpha( bool preMultipled );
141 * @brief Query whether alpha is pre-multiplied.
143 * @return True is alpha is pre-multiplied, false otherwise.
145 bool IsPreMultipliedAlphaEnabled() const;
148 * @brief Sets properties of custom shader
149 * @param[in] propertyMap Property map containing the custom shader data
151 void SetCustomShader( const Property::Map& propertyMap );
154 * @copydoc Toolkit::Visual::Base::SetProperty
156 void SetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
159 * @copydoc Toolkit::Visual::Base::GetProperty
161 Dali::Property::Value GetProperty( Dali::Property::Index index );
164 * Gets currently staged renderer, or an empty handle if not staged
166 Renderer GetRenderer();
169 * Sets the mix color ( including opacity ) of the visual.
170 * @param[in] mixColor The new mix color
172 void SetMixColor( const Vector4& color );
175 * Sets the mix color of the visual.
176 * @param[in] mixColor The new mix color
178 void SetMixColor( const Vector3& color );
181 * Gets the mix color of the visual.
182 * @return The mix color
184 const Vector4& GetMixColor() const;
187 * Animate the property if it exists in the visual or renderer.
189 * If it's a visual property such as mix color or a transform property,
190 * saves the target value to the local data.
192 * If the visual isn't staged (i.e. it doesn't have a renderer),
193 * then this will not add an animation.
195 * @param[in] transition The animation to create or attach to
196 * @param[in] animator The animation parameters of the property.
198 void AnimateProperty( Dali::Animation& transition,
199 Internal::TransitionData::Animator& animator );
204 * @brief Constructor.
206 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
208 Base( VisualFactoryCache& factoryCache );
211 * @brief A reference counted object may only be deleted by calling Unreference().
218 * @brief Called by CreatePropertyMap() allowing sub classes to respond to the CreatePropertyMap event
220 * @param[out] map The visual property map.
222 virtual void DoCreatePropertyMap( Property::Map& map ) const = 0;
225 * @brief Called by SetProperties() allowing sub classes to set their properties
227 * @param[in] propertyMap The properties for the requested Visual object.
229 virtual void DoSetProperties( const Property::Map& propertyMap ) = 0;
232 * @brief Called when transform or control size changes
233 * ( Of use to SVG and Text visuals )
235 virtual void OnSetTransform() = 0;
238 * @brief Called by SetOnStage() allowing sub classes to respond to the SetOnStage event
240 * @note The derived class is required to create the renderer, and add it to the actor when all the resources are in place.
242 * @param[in] actor The actor applying this visual.
244 virtual void DoSetOnStage( Actor& actor )=0;
247 * @brief Called by SetOffStage() allowing sub classes to respond to the SetOffStage event
249 * @param[in] actor The actor applying this visual.
251 virtual void DoSetOffStage( Actor& actor );
256 * @brief Gets the on stage state for this Visual
258 * @return Returns true if this Visual is on stage, false if it is off the stage
260 bool IsOnStage() const;
263 * @brief Gets whether the Dali::Renderer is from a shared cache (and therefore any modifications will affect other users of that renderer)
265 * @return Returns true if the renderer is from shared cache, false otherwise
267 bool IsFromCache() const;
272 * Register the mix color uniform on the Renderer and store the property index.
273 * Note, this is not used by Color or Primitive Visuals, which will use their
274 * own property index.
276 void RegisterMixColor();
279 * Find the matching property on the renderer or shader. If it's a shader
280 * property, register it on the renderer in order to animate it for this
281 * visual independently.
282 * @param[in] key The key to match.
283 * @return the matching index, or INVALID_INDEX if it's not found
285 Property::Index GetPropertyIndex( Property::Key key );
288 * Set up the transition. If no animation is required, then
289 * transition will be untouched.
291 * @param[in] transition The transition to use or set up.
292 * @param[in] animator The animation data to use
293 * @param[in] index The property index on the renderer to animate
294 * @param[in] initialValue The optional initial value
295 * @param[in] targetValue The target value to use
297 void SetupTransition( Dali::Animation& transition,
298 Internal::TransitionData::Animator& animator,
299 Property::Index index,
300 Property::Value& initialValue,
301 Property::Value& targetValue );
304 * Animate the opacity property - Special handling to
305 * ensure that the blend mode is set to ON whilst animating,
306 * and set back to AUTO if it's opaque at the end of the
309 * @param[in] transition The transition to use or set up.
310 * @param[in] animator The animation data to use
312 void AnimateOpacityProperty( Dali::Animation& transition,
313 Internal::TransitionData::Animator& animator );
316 * Animate the renderer property - no special handling
318 * @param[in] transition The transition to use or set up.
319 * @param[in] animator The animation data to use
321 void AnimateRendererProperty( Dali::Animation& transition,
322 Internal::TransitionData::Animator& animator );
325 * Animate the mix color property.
327 * If the animator is a vec3, then it only animates the color
328 * channels without animating the opacity. If it's a vec4, then it
329 * runs 2 animators, one for the the vec3 mixColor, and one for the
330 * opacity. (They are separate uniforms in the shader )
332 * @param[in] transition The transition to use or set up.
333 * @param[in] animator The animation data to use
335 void AnimateMixColorProperty( Dali::Animation& transition,
336 Internal::TransitionData::Animator& animator );
339 * Set up the right blend mode if the opacity is being animated.
340 * Also ensure that when the animation finishes, the blend mode is
341 * set to the appropriate value. It also uses the target value as
342 * set into mMixColor.
344 * @param[in] transition The transition to listen to
345 * @param[in] isInitialOpaque Whether the initial value is opaque
346 * @param[in] animating If the transition animates the value.
348 void SetupBlendMode( Dali::Animation& transition,
349 bool isInitialOpaque, bool animating );
352 * When a mix color animation has finished, ensure the blend mode is set back
353 * to the right value for the target opacity.
355 void OnMixColorFinished( Animation& animation );
358 Base( const Visual::Base& visual );
361 Base& operator=( const Visual::Base& visual );
366 VisualFactoryCache& mFactoryCache;
369 typedef IntrusivePtr<Base> BasePtr;
373 } // namespace Internal
375 inline const Internal::Visual::Base& GetImplementation(const Toolkit::Visual::Base& visualBase )
377 DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
379 const BaseObject& handle = visualBase.GetBaseObject();
381 return static_cast<const Internal::Visual::Base&>(handle);
384 inline Internal::Visual::Base& GetImplementation(Toolkit::Visual::Base& visualBase)
386 DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
388 BaseObject& handle = visualBase.GetBaseObject();
390 return static_cast<Internal::Visual::Base&>(handle);
393 } // namespace Toolkit
397 #endif // DALI_TOOLKIT_INTERNAL_VISUAL_H