2 * Copyright (c) 2018 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/object/handle-devel.h>
24 #include <dali/devel-api/scripting/enum-helper.h>
25 #include <dali/devel-api/rendering/renderer-devel.h>
26 #include <dali/integration-api/debug.h>
29 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
30 #include <dali-toolkit/public-api/visuals/primitive-visual-properties.h>
31 #include <dali-toolkit/public-api/visuals/visual-properties.h>
32 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
33 #include <dali-toolkit/internal/helpers/property-helper.h>
34 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
35 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
39 #if defined(DEBUG_ENABLED)
40 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VISUAL_BASE" );
43 const char * const PRE_MULTIPLIED_ALPHA_PROPERTY( "preMultipliedAlpha" );
59 DALI_ENUM_TO_STRING_TABLE_BEGIN( VISUAL_FITTING_MODE )
60 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, FIT_KEEP_ASPECT_RATIO )
61 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, FILL )
62 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, OVER_FIT_KEEP_ASPECT_RATIO )
63 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, CENTER )
64 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, FIT_WIDTH )
65 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, FIT_HEIGHT )
66 DALI_ENUM_TO_STRING_TABLE_END( VISUAL_FITTING_MODE )
70 Visual::Base::Base( VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type )
71 : mImpl( new Impl( fittingMode, type ) ),
72 mFactoryCache( factoryCache )
81 void Visual::Base::SetCustomShader( const Property::Map& shaderMap )
83 if( mImpl->mCustomShader )
85 mImpl->mCustomShader->SetPropertyMap( shaderMap );
89 mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
93 void Visual::Base::SetProperties( const Property::Map& propertyMap )
95 for( size_t i = 0; i < propertyMap.Count(); ++i )
97 const KeyValuePair& pair = propertyMap.GetKeyValue( i );
98 const Property::Key& key = pair.first;
99 const Property::Value& value = pair.second;
101 Property::Key matchKey = key;
102 if( matchKey.type == Property::Key::STRING )
104 if( matchKey == CUSTOM_SHADER )
106 matchKey = Property::Key( Toolkit::Visual::Property::SHADER );
108 else if( matchKey == TRANSFORM )
110 matchKey = Property::Key( Toolkit::Visual::Property::TRANSFORM );
112 else if( matchKey == PREMULTIPLIED_ALPHA )
114 matchKey = Property::Key( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA );
116 else if( matchKey == MIX_COLOR )
118 matchKey = Property::Key( Toolkit::Visual::Property::MIX_COLOR );
120 else if( matchKey == OPACITY )
122 matchKey = Property::Key( Toolkit::Visual::Property::OPACITY );
124 else if( matchKey == VISUAL_FITTING_MODE )
126 matchKey = Property::Key( Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE );
128 else if( matchKey == CORNER_RADIUS )
130 matchKey = Property::Key( Toolkit::DevelVisual::Property::CORNER_RADIUS );
132 else if( matchKey == CORNER_RADIUS_POLICY )
134 matchKey = Property::Key( Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY );
138 switch( matchKey.indexKey )
140 case Toolkit::Visual::Property::SHADER:
142 Property::Map shaderMap;
143 if( value.Get( shaderMap ) )
145 SetCustomShader( shaderMap );
150 case Toolkit::Visual::Property::TRANSFORM:
153 if( value.Get( map ) )
155 mImpl->mTransform.SetPropertyMap( map );
160 case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
162 bool premultipliedAlpha = false;
163 if( value.Get( premultipliedAlpha ) )
165 EnablePreMultipliedAlpha( premultipliedAlpha );
170 case Toolkit::Visual::Property::MIX_COLOR:
173 if( value.Get( mixColor ) )
175 if( value.GetType() == Property::VECTOR4 )
177 SetMixColor( mixColor );
181 Vector3 mixColor3(mixColor);
182 SetMixColor( mixColor3 );
187 case Toolkit::Visual::Property::OPACITY:
190 if( value.Get( opacity ) )
192 mImpl->mMixColor.a = opacity;
193 SetMixColor( mImpl->mMixColor );
197 case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
199 Scripting::GetEnumerationProperty< Visual::FittingMode >(
200 value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode );
203 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
206 if( value.Get( radius ) )
208 mImpl->mCornerRadius = radius;
212 case Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY:
215 if( value.Get( policy ) )
219 case Toolkit::Visual::Transform::Policy::RELATIVE:
220 case Toolkit::Visual::Transform::Policy::ABSOLUTE:
222 mImpl->mCornerRadiusPolicy = policy;
227 DALI_LOG_ERROR( "Unsupported policy: %d\n", policy );
237 DoSetProperties( propertyMap );
240 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
242 mImpl->mControlSize = controlSize;
243 mImpl->mTransform.UpdatePropertyMap( transform );
245 #if defined(DEBUG_ENABLED)
246 std::ostringstream oss;
248 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::SetTransformAndSize(%s) - [\e[1;32mtransform: %s controlSize: (%3.1f, %3.1f)]\e[0m\n",
249 GetName().c_str(), oss.str().c_str(), controlSize.x, controlSize.y );
255 void Visual::Base::SetName( const std::string& name )
260 const std::string& Visual::Base::GetName() const
265 float Visual::Base::GetHeightForWidth( float width )
267 float aspectCorrectedHeight = 0.f;
269 GetNaturalSize( naturalSize );
270 if( naturalSize.width )
272 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
274 return aspectCorrectedHeight;
277 float Visual::Base::GetWidthForHeight( float height )
279 float aspectCorrectedWidth = 0.f;
281 GetNaturalSize( naturalSize );
282 if( naturalSize.height > 0.0f )
284 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
286 return aspectCorrectedWidth;
289 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
291 naturalSize = Vector2::ZERO;
294 void Visual::Base::DoAction( const Property::Index actionId, const Property::Value attributes )
296 OnDoAction( actionId, attributes );
299 void Visual::Base::SetDepthIndex( int index )
301 mImpl->mDepthIndex = index;
302 if( mImpl->mRenderer )
304 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
308 int Visual::Base::GetDepthIndex() const
310 return mImpl->mDepthIndex;
313 void Visual::Base::SetOnScene( Actor& actor )
317 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
318 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
319 DoSetOnScene( actor );
321 if( mImpl->mRenderer )
325 if( IsRoundedCornerRequired() )
327 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterProperty( CORNER_RADIUS, mImpl->mCornerRadius );
328 mImpl->mRenderer.RegisterProperty( CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy );
330 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
333 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
334 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
335 mImpl->mFlags |= Impl::IS_ON_SCENE; // Only sets the flag if renderer exists
340 void Visual::Base::SetOffScene( Actor& actor )
344 DoSetOffScene( actor );
345 mImpl->mMixColorIndex = Property::INVALID_INDEX;
346 mImpl->mCornerRadiusIndex = Property::INVALID_INDEX;
347 mImpl->mFlags &= ~Impl::IS_ON_SCENE;
351 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
353 DoCreatePropertyMap( map );
355 if( mImpl->mCustomShader )
357 mImpl->mCustomShader->CreatePropertyMap( map );
360 Property::Map transform;
361 mImpl->mTransform.GetPropertyMap( transform );
362 map.Insert( Toolkit::Visual::Property::TRANSFORM, transform );
364 bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
365 map.Insert( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
367 // Note, Color and Primitive will also insert their own mix color into the map
368 // which is ok, because they have a different key value range.
369 map.Insert( Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor ); // vec4
370 map.Insert( Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a );
371 map.Insert( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired() );
373 auto fittingModeString = Scripting::GetLinearEnumerationName< FittingMode >(
374 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT );
375 map.Insert( Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString );
377 map.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius );
378 map.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, static_cast< int >( mImpl->mCornerRadiusPolicy ) );
381 void Visual::Base::CreateInstancePropertyMap( Property::Map& map ) const
383 DoCreateInstancePropertyMap( map );
385 if( mImpl->mCustomShader )
387 mImpl->mCustomShader->CreatePropertyMap( map );
390 //map.Insert( Toolkit::Visual::Property::DEPTH_INDEX, mImpl->mDepthIndex );
391 //map.Insert( Toolkit::Visual::Property::ENABLED, (bool) mImpl->mRenderer );
395 void Visual::Base::EnablePreMultipliedAlpha( bool preMultiplied )
399 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
403 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
406 if( mImpl->mRenderer )
408 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
409 mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, static_cast<float>( preMultiplied ) );
413 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
415 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
418 void Visual::Base::DoSetOffScene( Actor& actor )
420 actor.RemoveRenderer( mImpl->mRenderer );
421 mImpl->mRenderer.Reset();
424 bool Visual::Base::IsOnScene() const
426 return mImpl->mFlags & Impl::IS_ON_SCENE;
429 bool Visual::Base::IsRoundedCornerRequired() const
431 return !EqualsZero( mImpl->mCornerRadius );
434 void Visual::Base::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
436 // May be overriden by derived class
439 void Visual::Base::RegisterMixColor()
441 // Only register if not already registered.
442 // (Color and Primitive visuals will register their own and save to this index)
443 if( mImpl->mMixColorIndex == Property::INVALID_INDEX )
445 mImpl->mMixColorIndex = DevelHandle::RegisterProperty(
447 Toolkit::Visual::Property::MIX_COLOR,
449 Vector3(mImpl->mMixColor) );
452 if( mImpl->mMixColor.a < 1.f )
454 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
457 mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, mImpl->mMixColor.a );
459 float preMultipliedAlpha = 0.0f;
460 if( IsPreMultipliedAlphaEnabled() )
462 preMultipliedAlpha = 1.0f;
464 mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, preMultipliedAlpha );
467 void Visual::Base::SetMixColor( const Vector4& color )
469 mImpl->mMixColor = color;
471 if( mImpl->mRenderer )
473 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, Vector3(color) );
474 mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, color.a );
477 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
482 void Visual::Base::SetMixColor( const Vector3& color )
484 mImpl->mMixColor.r = color.r;
485 mImpl->mMixColor.g = color.g;
486 mImpl->mMixColor.b = color.b;
488 if( mImpl->mRenderer )
490 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
494 const Vector4& Visual::Base::GetMixColor() const
496 return mImpl->mMixColor;
499 void Visual::Base::AddEventObserver( Visual::EventObserver& observer)
501 mImpl->mEventObserver = &observer;
504 void Visual::Base::RemoveEventObserver( Visual::EventObserver& observer )
506 mImpl->mEventObserver = NULL;
509 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
511 if( mImpl->mResourceStatus != resourceStatus )
513 mImpl->mResourceStatus = resourceStatus;
515 if( mImpl->mEventObserver )
517 // observer is currently a control impl
518 mImpl->mEventObserver->ResourceReady( *this );
523 bool Visual::Base::IsResourceReady() const
525 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY );
528 bool Visual::Base::IsSynchronousLoadingRequired() const
530 return ( mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING );
533 Toolkit::Visual::Type Visual::Base::GetType() const
538 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
540 return mImpl->mResourceStatus;
543 Visual::FittingMode Visual::Base::GetFittingMode() const
545 return mImpl->mFittingMode;
548 Visual::Base& Visual::Base::GetVisualObject()
553 Renderer Visual::Base::GetRenderer()
555 return mImpl->mRenderer;
558 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
560 Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
562 if( index == Property::INVALID_INDEX )
564 // Is it a shader property?
565 Shader shader = mImpl->mRenderer.GetShader();
566 index = DevelHandle::GetPropertyIndex( shader, key );
567 if( index != Property::INVALID_INDEX )
569 // Yes - we should register it in the Renderer so it can be set / animated
570 // independently, as shaders are shared across multiple renderers.
572 Property::Index keyIndex( Property::INVALID_KEY );
573 if( key.type == Property::Key::INDEX )
575 keyName = shader.GetPropertyName( index );
576 keyIndex = key.indexKey;
580 keyName = key.stringKey;
581 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
583 Property::Value value = shader.GetProperty( index );
584 index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
590 void Visual::Base::SetupTransition(
591 Dali::Animation& transition,
592 Internal::TransitionData::Animator& animator,
593 Property::Index index,
594 Property::Value& initialValue,
595 Property::Value& targetValue )
597 if( index != Property::INVALID_INDEX )
599 if( mImpl->mRenderer )
601 if( animator.animate == false )
603 mImpl->mRenderer.SetProperty( index, targetValue );
607 if( animator.initialValue.GetType() != Property::NONE )
609 mImpl->mRenderer.SetProperty( index, initialValue );
614 transition = Dali::Animation::New( 0.1f );
617 transition.AnimateTo( Property( mImpl->mRenderer, index ),
619 animator.alphaFunction,
620 TimePeriod( animator.timePeriodDelay,
621 animator.timePeriodDuration ) );
627 void Visual::Base::AnimateProperty(
628 Dali::Animation& transition,
629 Internal::TransitionData::Animator& animator )
631 #if defined(DEBUG_ENABLED)
633 std::ostringstream oss;
634 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
635 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
640 DoCreatePropertyMap( map );
641 Property::Value* valuePtr = map.Find( Toolkit::Visual::Property::TYPE );
645 valuePtr->Get( visualType );
648 if( animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
649 animator.propertyKey == MIX_COLOR ||
650 ( visualType == Toolkit::Visual::COLOR &&
651 animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
652 ( visualType == Toolkit::Visual::PRIMITIVE &&
653 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
655 AnimateMixColorProperty( transition, animator );
657 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
658 animator.propertyKey == OPACITY )
660 AnimateOpacityProperty( transition, animator );
662 else if( mImpl->mRenderer )
664 AnimateRendererProperty( transition, animator );
668 void Visual::Base::AnimateOpacityProperty(
669 Dali::Animation& transition,
670 Internal::TransitionData::Animator& animator )
672 bool isOpaque = mImpl->mMixColor.a >= 1.0f;
674 float initialOpacity;
675 if( animator.initialValue.Get( initialOpacity ) )
677 isOpaque = (initialOpacity >= 1.0f);
681 if( animator.targetValue.Get( targetOpacity ) )
683 mImpl->mMixColor.a = targetOpacity;
686 SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue );
687 SetupBlendMode( transition, isOpaque, animator.animate );
690 void Visual::Base::AnimateRendererProperty(
691 Dali::Animation& transition,
692 Internal::TransitionData::Animator& animator )
694 Property::Index index = GetPropertyIndex( animator.propertyKey );
695 if( index != Property::INVALID_INDEX )
697 if( animator.targetValue.GetType() != Property::NONE )
699 // Try writing target value into transform property map
700 // if it's not a valid key, then it won't alter mTransform
702 if( animator.propertyKey.type == Property::Key::INDEX )
704 map.Add( animator.propertyKey.indexKey, animator.targetValue );
708 map.Add( animator.propertyKey.stringKey, animator.targetValue );
711 mImpl->mTransform.UpdatePropertyMap( map );
714 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
718 void Visual::Base::AnimateMixColorProperty(
719 Dali::Animation& transition,
720 Internal::TransitionData::Animator& animator )
722 Property::Index index = mImpl->mMixColorIndex;
723 bool animateOpacity = false;
724 bool isOpaque = true;
726 Property::Value initialOpacity;
727 Property::Value targetOpacity;
728 Property::Value initialMixColor;
729 Property::Value targetMixColor;
731 if( index != Property::INVALID_INDEX )
733 Vector4 initialColor;
734 if( animator.initialValue.Get(initialColor) )
736 if( animator.initialValue.GetType() == Property::VECTOR4 )
738 // if there is an initial color specifying alpha, test it
739 isOpaque = initialColor.a >= 1.0f;
740 initialOpacity = initialColor.a;
742 initialMixColor = Vector3( initialColor );
745 // Set target value into data store
746 if( animator.targetValue.GetType() != Property::NONE )
749 animator.targetValue.Get(mixColor);
750 if( animator.targetValue.GetType() == Property::VECTOR4 )
752 mImpl->mMixColor.a = mixColor.a;
753 targetOpacity = mixColor.a;
754 animateOpacity = true;
757 mImpl->mMixColor.r = mixColor.r;
758 mImpl->mMixColor.g = mixColor.g;
759 mImpl->mMixColor.b = mixColor.b;
760 targetMixColor = Vector3(mixColor);
763 SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
766 SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity );
767 SetupBlendMode( transition, isOpaque, animator.animate );
772 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
774 // Ensure the blend mode is turned on if we are animating opacity, and
775 // turned off after the animation ends if the final value is opaque
776 if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
778 if( mImpl->mRenderer )
780 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
782 if( animating == true && mImpl->mMixColor.a >= 1.0f )
784 // When it becomes opaque, set the blend mode back to automatically
785 if( ! mImpl->mBlendSlotDelegate )
787 mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
789 transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
790 &Visual::Base::OnMixColorFinished );
796 void Visual::Base::OnMixColorFinished( Animation& animation )
798 if( mImpl->mRenderer )
800 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
801 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
802 ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
804 delete mImpl->mBlendSlotDelegate;
805 mImpl->mBlendSlotDelegate = NULL;
808 } // namespace Internal
810 } // namespace Toolkit