2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "visual-base-impl.h"
22 #include <dali-toolkit/public-api/dali-toolkit-common.h>
23 #include <dali/devel-api/scripting/enum-helper.h>
24 #include <dali/devel-api/rendering/renderer-devel.h>
25 #include <dali/integration-api/debug.h>
28 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
29 #include <dali-toolkit/public-api/visuals/primitive-visual-properties.h>
30 #include <dali-toolkit/public-api/visuals/visual-properties.h>
31 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
32 #include <dali-toolkit/internal/helpers/property-helper.h>
33 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
34 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
38 #if defined(DEBUG_ENABLED)
39 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VISUAL_BASE" );
42 const char * const PRE_MULTIPLIED_ALPHA_PROPERTY( "preMultipliedAlpha" );
58 DALI_ENUM_TO_STRING_TABLE_BEGIN( VISUAL_FITTING_MODE )
59 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, FIT_KEEP_ASPECT_RATIO )
60 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, FILL )
61 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, OVER_FIT_KEEP_ASPECT_RATIO )
62 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, CENTER )
63 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, FIT_WIDTH )
64 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, FIT_HEIGHT )
65 DALI_ENUM_TO_STRING_TABLE_END( VISUAL_FITTING_MODE )
69 Visual::Base::Base( VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type )
70 : mImpl( new Impl( fittingMode, type ) ),
71 mFactoryCache( factoryCache )
80 void Visual::Base::SetCustomShader( const Property::Map& shaderMap )
82 if( mImpl->mCustomShader )
84 mImpl->mCustomShader->SetPropertyMap( shaderMap );
88 mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
92 void Visual::Base::SetProperties( const Property::Map& propertyMap )
94 for( size_t i = 0; i < propertyMap.Count(); ++i )
96 const KeyValuePair& pair = propertyMap.GetKeyValue( i );
97 const Property::Key& key = pair.first;
98 const Property::Value& value = pair.second;
100 Property::Key matchKey = key;
101 if( matchKey.type == Property::Key::STRING )
103 if( matchKey == CUSTOM_SHADER )
105 matchKey = Property::Key( Toolkit::Visual::Property::SHADER );
107 else if( matchKey == TRANSFORM )
109 matchKey = Property::Key( Toolkit::Visual::Property::TRANSFORM );
111 else if( matchKey == PREMULTIPLIED_ALPHA )
113 matchKey = Property::Key( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA );
115 else if( matchKey == MIX_COLOR )
117 matchKey = Property::Key( Toolkit::Visual::Property::MIX_COLOR );
119 else if( matchKey == OPACITY )
121 matchKey = Property::Key( Toolkit::Visual::Property::OPACITY );
123 else if( matchKey == VISUAL_FITTING_MODE )
125 matchKey = Property::Key( Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE );
127 else if( matchKey == CORNER_RADIUS )
129 matchKey = Property::Key( Toolkit::DevelVisual::Property::CORNER_RADIUS );
131 else if( matchKey == CORNER_RADIUS_POLICY )
133 matchKey = Property::Key( Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY );
137 switch( matchKey.indexKey )
139 case Toolkit::Visual::Property::SHADER:
141 Property::Map shaderMap;
142 if( value.Get( shaderMap ) )
144 SetCustomShader( shaderMap );
149 case Toolkit::Visual::Property::TRANSFORM:
152 if( value.Get( map ) )
154 mImpl->mTransform.SetPropertyMap( map );
159 case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
161 bool premultipliedAlpha = false;
162 if( value.Get( premultipliedAlpha ) )
164 EnablePreMultipliedAlpha( premultipliedAlpha );
169 case Toolkit::Visual::Property::MIX_COLOR:
172 if( value.Get( mixColor ) )
174 if( value.GetType() == Property::VECTOR4 )
176 SetMixColor( mixColor );
180 Vector3 mixColor3(mixColor);
181 SetMixColor( mixColor3 );
186 case Toolkit::Visual::Property::OPACITY:
189 if( value.Get( opacity ) )
191 mImpl->mMixColor.a = opacity;
192 SetMixColor( mImpl->mMixColor );
196 case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
198 Scripting::GetEnumerationProperty< Visual::FittingMode >(
199 value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode );
202 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
205 if( value.Get( radius ) )
207 mImpl->mCornerRadius = radius;
211 case Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY:
214 if( value.Get( policy ) )
218 case Toolkit::Visual::Transform::Policy::RELATIVE:
219 case Toolkit::Visual::Transform::Policy::ABSOLUTE:
221 mImpl->mCornerRadiusPolicy = policy;
226 DALI_LOG_ERROR( "Unsupported policy: %d\n", policy );
236 DoSetProperties( propertyMap );
239 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
241 mImpl->mControlSize = controlSize;
242 mImpl->mTransform.UpdatePropertyMap( transform );
244 #if defined(DEBUG_ENABLED)
245 std::ostringstream oss;
247 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::SetTransformAndSize(%s) - [\e[1;32mtransform: %s controlSize: (%3.1f, %3.1f)]\e[0m\n",
248 GetName().c_str(), oss.str().c_str(), controlSize.x, controlSize.y );
254 void Visual::Base::SetName( const std::string& name )
259 const std::string& Visual::Base::GetName() const
264 float Visual::Base::GetHeightForWidth( float width )
266 float aspectCorrectedHeight = 0.f;
268 GetNaturalSize( naturalSize );
269 if( naturalSize.width )
271 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
273 return aspectCorrectedHeight;
276 float Visual::Base::GetWidthForHeight( float height )
278 float aspectCorrectedWidth = 0.f;
280 GetNaturalSize( naturalSize );
281 if( naturalSize.height > 0.0f )
283 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
285 return aspectCorrectedWidth;
288 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
290 naturalSize = Vector2::ZERO;
293 void Visual::Base::DoAction( const Property::Index actionId, const Property::Value attributes )
295 OnDoAction( actionId, attributes );
298 void Visual::Base::SetDepthIndex( int index )
300 mImpl->mDepthIndex = index;
301 if( mImpl->mRenderer )
303 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
307 int Visual::Base::GetDepthIndex() const
309 return mImpl->mDepthIndex;
312 void Visual::Base::SetOnScene( Actor& actor )
316 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
317 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
318 DoSetOnScene( actor );
320 if( mImpl->mRenderer )
324 if( IsRoundedCornerRequired() )
326 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterProperty( CORNER_RADIUS, mImpl->mCornerRadius );
327 mImpl->mRenderer.RegisterProperty( CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy );
329 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
332 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
333 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
334 mImpl->mFlags |= Impl::IS_ON_SCENE; // Only sets the flag if renderer exists
339 void Visual::Base::SetOffScene( Actor& actor )
343 DoSetOffScene( actor );
344 mImpl->mMixColorIndex = Property::INVALID_INDEX;
345 mImpl->mCornerRadiusIndex = Property::INVALID_INDEX;
346 mImpl->mFlags &= ~Impl::IS_ON_SCENE;
350 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
352 DoCreatePropertyMap( map );
354 if( mImpl->mCustomShader )
356 mImpl->mCustomShader->CreatePropertyMap( map );
359 Property::Map transform;
360 mImpl->mTransform.GetPropertyMap( transform );
361 map.Insert( Toolkit::Visual::Property::TRANSFORM, transform );
363 bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
364 map.Insert( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
366 // Note, Color and Primitive will also insert their own mix color into the map
367 // which is ok, because they have a different key value range.
368 map.Insert( Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor ); // vec4
369 map.Insert( Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a );
371 auto fittingModeString = Scripting::GetLinearEnumerationName< FittingMode >(
372 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT );
373 map.Insert( Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString );
375 map.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius );
376 map.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, static_cast< int >( mImpl->mCornerRadiusPolicy ) );
379 void Visual::Base::CreateInstancePropertyMap( Property::Map& map ) const
381 DoCreateInstancePropertyMap( map );
383 if( mImpl->mCustomShader )
385 mImpl->mCustomShader->CreatePropertyMap( map );
388 //map.Insert( Toolkit::Visual::Property::DEPTH_INDEX, mImpl->mDepthIndex );
389 //map.Insert( Toolkit::Visual::Property::ENABLED, (bool) mImpl->mRenderer );
393 void Visual::Base::EnablePreMultipliedAlpha( bool preMultiplied )
397 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
401 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
404 if( mImpl->mRenderer )
406 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
407 mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, static_cast<float>( preMultiplied ) );
411 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
413 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
416 bool Visual::Base::IsAdvancedBlendEquationApplied() const
418 return IsPreMultipliedAlphaEnabled() && DevelRenderer::IsAdvancedBlendEquationApplied( mImpl->mRenderer );
421 void Visual::Base::DoSetOffScene( Actor& actor )
423 actor.RemoveRenderer( mImpl->mRenderer );
424 mImpl->mRenderer.Reset();
427 bool Visual::Base::IsOnScene() const
429 return mImpl->mFlags & Impl::IS_ON_SCENE;
432 bool Visual::Base::IsRoundedCornerRequired() const
434 return !EqualsZero( mImpl->mCornerRadius );
437 void Visual::Base::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
439 // May be overriden by derived class
442 void Visual::Base::RegisterMixColor()
444 // Only register if not already registered.
445 // (Color and Primitive visuals will register their own and save to this index)
446 if( mImpl->mMixColorIndex == Property::INVALID_INDEX )
448 mImpl->mMixColorIndex = mImpl->mRenderer.RegisterProperty(
449 Toolkit::Visual::Property::MIX_COLOR,
451 Vector3(mImpl->mMixColor) );
454 mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, mImpl->mMixColor.a );
456 float preMultipliedAlpha = 0.0f;
457 if( IsPreMultipliedAlphaEnabled() )
459 preMultipliedAlpha = 1.0f;
461 mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, preMultipliedAlpha );
464 void Visual::Base::SetMixColor( const Vector4& color )
466 mImpl->mMixColor = color;
468 if( mImpl->mRenderer )
470 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, Vector3(color) );
471 mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, color.a );
475 void Visual::Base::SetMixColor( const Vector3& color )
477 mImpl->mMixColor.r = color.r;
478 mImpl->mMixColor.g = color.g;
479 mImpl->mMixColor.b = color.b;
481 if( mImpl->mRenderer )
483 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
487 const Vector4& Visual::Base::GetMixColor() const
489 return mImpl->mMixColor;
492 void Visual::Base::AddEventObserver( Visual::EventObserver& observer)
494 mImpl->mEventObserver = &observer;
497 void Visual::Base::RemoveEventObserver( Visual::EventObserver& observer )
499 mImpl->mEventObserver = NULL;
502 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
504 if( mImpl->mResourceStatus != resourceStatus )
506 mImpl->mResourceStatus = resourceStatus;
508 if( mImpl->mEventObserver )
510 // observer is currently a control impl
511 mImpl->mEventObserver->ResourceReady( *this );
516 bool Visual::Base::IsResourceReady() const
518 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY );
521 bool Visual::Base::IsSynchronousLoadingRequired() const
523 return ( mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING );
526 Toolkit::Visual::Type Visual::Base::GetType() const
531 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
533 return mImpl->mResourceStatus;
536 Visual::FittingMode Visual::Base::GetFittingMode() const
538 return mImpl->mFittingMode;
541 Visual::Base& Visual::Base::GetVisualObject()
546 Renderer Visual::Base::GetRenderer()
548 return mImpl->mRenderer;
551 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
553 Property::Index index = mImpl->mRenderer.GetPropertyIndex( key );
555 if( index == Property::INVALID_INDEX )
557 // Is it a shader property?
558 Shader shader = mImpl->mRenderer.GetShader();
559 index = shader.GetPropertyIndex( key );
560 if( index != Property::INVALID_INDEX )
562 // Yes - we should register it in the Renderer so it can be set / animated
563 // independently, as shaders are shared across multiple renderers.
565 Property::Index keyIndex( Property::INVALID_KEY );
566 if( key.type == Property::Key::INDEX )
568 keyName = shader.GetPropertyName( index );
569 keyIndex = key.indexKey;
573 keyName = key.stringKey;
574 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
576 Property::Value value = shader.GetProperty( index );
577 index = mImpl->mRenderer.RegisterProperty( keyIndex, keyName, value );
583 void Visual::Base::SetupTransition(
584 Dali::Animation& transition,
585 Internal::TransitionData::Animator& animator,
586 Property::Index index,
587 Property::Value& initialValue,
588 Property::Value& targetValue )
590 if( index != Property::INVALID_INDEX )
592 if( mImpl->mRenderer )
594 if( animator.animate == false )
596 mImpl->mRenderer.SetProperty( index, targetValue );
600 if( animator.initialValue.GetType() != Property::NONE )
602 mImpl->mRenderer.SetProperty( index, initialValue );
607 transition = Dali::Animation::New( 0.1f );
610 transition.AnimateTo( Property( mImpl->mRenderer, index ),
612 animator.alphaFunction,
613 TimePeriod( animator.timePeriodDelay,
614 animator.timePeriodDuration ) );
620 void Visual::Base::AnimateProperty(
621 Dali::Animation& transition,
622 Internal::TransitionData::Animator& animator )
624 #if defined(DEBUG_ENABLED)
626 std::ostringstream oss;
627 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
628 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
633 DoCreatePropertyMap( map );
634 Property::Value* valuePtr = map.Find( Toolkit::Visual::Property::TYPE );
638 valuePtr->Get( visualType );
641 if( animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
642 animator.propertyKey == MIX_COLOR ||
643 ( visualType == Toolkit::Visual::COLOR &&
644 animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
645 ( visualType == Toolkit::Visual::PRIMITIVE &&
646 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
648 AnimateMixColorProperty( transition, animator );
650 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
651 animator.propertyKey == OPACITY )
653 AnimateOpacityProperty( transition, animator );
655 else if( mImpl->mRenderer )
657 AnimateRendererProperty( transition, animator );
661 void Visual::Base::AnimateOpacityProperty(
662 Dali::Animation& transition,
663 Internal::TransitionData::Animator& animator )
666 if( animator.targetValue.Get( targetOpacity ) )
668 mImpl->mMixColor.a = targetOpacity;
671 SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue );
674 void Visual::Base::AnimateRendererProperty(
675 Dali::Animation& transition,
676 Internal::TransitionData::Animator& animator )
678 Property::Index index = GetPropertyIndex( animator.propertyKey );
679 if( index != Property::INVALID_INDEX )
681 if( animator.targetValue.GetType() != Property::NONE )
683 // Try writing target value into transform property map
684 // if it's not a valid key, then it won't alter mTransform
686 if( animator.propertyKey.type == Property::Key::INDEX )
688 map.Add( animator.propertyKey.indexKey, animator.targetValue );
692 map.Add( animator.propertyKey.stringKey, animator.targetValue );
695 mImpl->mTransform.UpdatePropertyMap( map );
698 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
702 void Visual::Base::AnimateMixColorProperty(
703 Dali::Animation& transition,
704 Internal::TransitionData::Animator& animator )
706 Property::Index index = mImpl->mMixColorIndex;
707 bool animateOpacity = false;
709 Property::Value initialOpacity;
710 Property::Value targetOpacity;
711 Property::Value initialMixColor;
712 Property::Value targetMixColor;
714 if( index != Property::INVALID_INDEX )
716 Vector4 initialColor;
717 if( animator.initialValue.Get(initialColor) )
719 if( animator.initialValue.GetType() == Property::VECTOR4 )
721 // if there is an initial color specifying alpha, test it
722 initialOpacity = initialColor.a;
724 initialMixColor = Vector3( initialColor );
727 // Set target value into data store
728 if( animator.targetValue.GetType() != Property::NONE )
731 animator.targetValue.Get(mixColor);
732 if( animator.targetValue.GetType() == Property::VECTOR4 )
734 mImpl->mMixColor.a = mixColor.a;
735 targetOpacity = mixColor.a;
736 animateOpacity = true;
739 mImpl->mMixColor.r = mixColor.r;
740 mImpl->mMixColor.g = mixColor.g;
741 mImpl->mMixColor.b = mixColor.b;
742 targetMixColor = Vector3(mixColor);
745 SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
748 SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity );
753 } // namespace Internal
755 } // namespace Toolkit