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>
47 class ResourceObserver;
50 * Base class for all Control rendering logic. A control may have multiple visuals.
52 * Note: The visual responds to the the Actor::COLOR by blending it with the 'Multiply' operator.
54 * The following properties are optional
56 * | %Property Name | Type |
57 * |-------------------------|------------------|
58 * | customShader | MAP |
60 * where custom-shader is a map with the following properties:
61 * | %Property Name | Type |
62 * |-------------------------|------------------|
63 * | vertexShader | STRING |
64 * | fragmentShader | STRING |
65 * | subdivideGridX | INT |
66 * | subdivideGridY | INT |
67 * | shaderHints | INT |
69 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();
90 * @copydoc Toolkit::Visual::Base::SetSize
92 void SetTransformAndSize( const Property::Map& transform, Size controlSize );
95 * @copydoc Toolkit::Visual::Base::GetHeightForWidth
97 virtual float GetHeightForWidth( float width );
100 * @copydoc Toolkit::Visual::Base::GetWidthForHeight
102 virtual float GetWidthForHeight( float height );
105 * @copydoc Toolkit::Visual::Base::GetNaturalSize
107 virtual void GetNaturalSize( Vector2& naturalSize );
110 * @copydoc Toolkit::Visual::Base::SetDepthIndex
112 void SetDepthIndex( float index );
115 * @copydoc Toolkit::Visual::Base::GetDepthIndex
117 float GetDepthIndex() const;
120 * @copydoc Toolkit::Visual::Base::SetOnStage
121 * @pre Impl->mGeometry must be created before this method is called
123 void SetOnStage( Actor& actor );
126 * @copydoc Toolkit::Visual::Base::SetOffStage
128 void SetOffStage( Actor& actor );
131 * @copydoc Toolkit::Visual::Base::CreatePropertyMap
133 void CreatePropertyMap( Property::Map& map ) const;
136 * @brief Create a property map containing per-instance visual properties.
138 * This will enable creation of new visuals on control state change with
139 * any alternative style properties and the relevant instance properties
140 * (e.g. for image visual, the desired size, and for text visual, the actual text).
141 * @param[in] map The property map into which to write
143 void CreateInstancePropertyMap( Property::Map& map ) const;
146 * @brief Set whether the Pre-multiplied Alpha Blending is required
148 * @param[in] preMultipled whether alpha is pre-multiplied.
150 void EnablePreMultipliedAlpha( bool preMultipled );
153 * @brief Query whether alpha is pre-multiplied.
155 * @return True is alpha is pre-multiplied, false otherwise.
157 bool IsPreMultipliedAlphaEnabled() const;
160 * @brief Sets properties of custom shader
161 * @param[in] propertyMap Property map containing the custom shader data
163 void SetCustomShader( const Property::Map& propertyMap );
166 * @copydoc Toolkit::Visual::Base::SetProperty
168 void SetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
171 * @copydoc Toolkit::Visual::Base::GetProperty
173 Dali::Property::Value GetProperty( Dali::Property::Index index );
176 * Gets currently staged renderer, or an empty handle if not staged
178 Renderer GetRenderer();
181 * Sets the mix color ( including opacity ) of the visual.
182 * @param[in] mixColor The new mix color
184 void SetMixColor( const Vector4& color );
187 * Sets the mix color of the visual.
188 * @param[in] mixColor The new mix color
190 void SetMixColor( const Vector3& color );
193 * Gets the mix color of the visual.
194 * @return The mix color
196 const Vector4& GetMixColor() const;
199 * Animate the property if it exists in the visual or renderer.
201 * If it's a visual property such as mix color or a transform property,
202 * saves the target value to the local data.
204 * If the visual isn't staged (i.e. it doesn't have a renderer),
205 * then this will not add an animation.
207 * @param[in] transition The animation to create or attach to
208 * @param[in] animator The animation parameters of the property.
210 void AnimateProperty( Dali::Animation& transition,
211 Internal::TransitionData::Animator& animator );
214 * @brief Add an observer to watch for when the Visuals resources are loaded.
215 * Currently only supports a single observer
218 void AddResourceObserver( Visual::ResourceObserver& observer );
221 * @brief Remove an observer
223 void RemoveResourceObserver( Visual::ResourceObserver& observer );
226 * @brief Called when the visuals resources are loaded / ready
228 void ResourceReady();
231 * @brief Called when the visuals resources are loaded / ready
232 * @return true if ready, false otherwise
234 bool IsResourceReady() const;
239 * @brief Constructor.
241 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
243 Base( VisualFactoryCache& factoryCache );
246 * @brief A reference counted object may only be deleted by calling Unreference().
253 * @brief Called by CreatePropertyMap() allowing sub classes to respond to the CreatePropertyMap event
255 * @param[out] map The visual property map.
257 virtual void DoCreatePropertyMap( Property::Map& map ) const = 0;
260 * @brief Called by CreateInstancePropertyMap() allowing derived
261 * classes to store instanced data (separate to styled data) that
262 * needs copying between visuals on state change.
264 * @param[out] map The visual property map
266 virtual void DoCreateInstancePropertyMap( Property::Map& map ) const = 0;
269 * @brief Called by SetProperties() allowing sub classes to set their properties
271 * @param[in] propertyMap The properties for the requested Visual object.
273 virtual void DoSetProperties( const Property::Map& propertyMap ) = 0;
276 * @brief Called when transform or control size changes
277 * ( Of use to SVG and Text visuals )
279 virtual void OnSetTransform() = 0;
282 * @brief Called by SetOnStage() allowing sub classes to respond to the SetOnStage event
284 * @note The derived class is required to create the renderer, and add it to the actor when all the resources are in place.
286 * @param[in] actor The actor applying this visual.
288 virtual void DoSetOnStage( Actor& actor )=0;
291 * @brief Called by SetOffStage() allowing sub classes to respond to the SetOffStage event
293 * @param[in] actor The actor applying this visual.
295 virtual void DoSetOffStage( Actor& actor );
300 * @brief Gets the on stage state for this Visual
302 * @return Returns true if this Visual is on stage, false if it is off the stage
304 bool IsOnStage() const;
309 * Register the mix color uniform on the Renderer and store the property index.
310 * Note, this is not used by Color or Primitive Visuals, which will use their
311 * own property index.
313 void RegisterMixColor();
316 * Find the matching property on the renderer or shader. If it's a shader
317 * property, register it on the renderer in order to animate it for this
318 * visual independently.
319 * @param[in] key The key to match.
320 * @return the matching index, or INVALID_INDEX if it's not found
322 Property::Index GetPropertyIndex( Property::Key key );
325 * Set up the transition. If no animation is required, then
326 * transition will be untouched.
328 * @param[in] transition The transition to use or set up.
329 * @param[in] animator The animation data to use
330 * @param[in] index The property index on the renderer to animate
331 * @param[in] initialValue The optional initial value
332 * @param[in] targetValue The target value to use
334 void SetupTransition( Dali::Animation& transition,
335 Internal::TransitionData::Animator& animator,
336 Property::Index index,
337 Property::Value& initialValue,
338 Property::Value& targetValue );
341 * Animate the opacity property - Special handling to
342 * ensure that the blend mode is set to ON whilst animating,
343 * and set back to AUTO if it's opaque at the end of the
346 * @param[in] transition The transition to use or set up.
347 * @param[in] animator The animation data to use
349 void AnimateOpacityProperty( Dali::Animation& transition,
350 Internal::TransitionData::Animator& animator );
353 * Animate the renderer property - no special handling
355 * @param[in] transition The transition to use or set up.
356 * @param[in] animator The animation data to use
358 void AnimateRendererProperty( Dali::Animation& transition,
359 Internal::TransitionData::Animator& animator );
362 * Animate the mix color property.
364 * If the animator is a vec3, then it only animates the color
365 * channels without animating the opacity. If it's a vec4, then it
366 * runs 2 animators, one for the the vec3 mixColor, and one for the
367 * opacity. (They are separate uniforms in the shader )
369 * @param[in] transition The transition to use or set up.
370 * @param[in] animator The animation data to use
372 void AnimateMixColorProperty( Dali::Animation& transition,
373 Internal::TransitionData::Animator& animator );
376 * Set up the right blend mode if the opacity is being animated.
377 * Also ensure that when the animation finishes, the blend mode is
378 * set to the appropriate value. It also uses the target value as
379 * set into mMixColor.
381 * @param[in] transition The transition to listen to
382 * @param[in] isInitialOpaque Whether the initial value is opaque
383 * @param[in] animating If the transition animates the value.
385 void SetupBlendMode( Dali::Animation& transition,
386 bool isInitialOpaque, bool animating );
389 * When a mix color animation has finished, ensure the blend mode is set back
390 * to the right value for the target opacity.
392 void OnMixColorFinished( Animation& animation );
395 Base( const Visual::Base& visual );
398 Base& operator=( const Visual::Base& visual );
403 VisualFactoryCache& mFactoryCache;
406 typedef IntrusivePtr<Base> BasePtr;
410 } // namespace Internal
412 inline const Internal::Visual::Base& GetImplementation(const Toolkit::Visual::Base& visualBase )
414 DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
416 const BaseObject& handle = visualBase.GetBaseObject();
418 return static_cast<const Internal::Visual::Base&>(handle);
421 inline Internal::Visual::Base& GetImplementation(Toolkit::Visual::Base& visualBase)
423 DALI_ASSERT_ALWAYS( visualBase && "visual base handle is empty" );
425 BaseObject& handle = visualBase.GetBaseObject();
427 return static_cast<Internal::Visual::Base&>(handle);
430 } // namespace Toolkit
434 #endif // DALI_TOOLKIT_INTERNAL_VISUAL_H