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 );
372 auto fittingModeString = Scripting::GetLinearEnumerationName< FittingMode >(
373 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT );
374 map.Insert( Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString );
376 map.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius );
377 map.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, static_cast< int >( mImpl->mCornerRadiusPolicy ) );
380 void Visual::Base::CreateInstancePropertyMap( Property::Map& map ) const
382 DoCreateInstancePropertyMap( map );
384 if( mImpl->mCustomShader )
386 mImpl->mCustomShader->CreatePropertyMap( map );
389 //map.Insert( Toolkit::Visual::Property::DEPTH_INDEX, mImpl->mDepthIndex );
390 //map.Insert( Toolkit::Visual::Property::ENABLED, (bool) mImpl->mRenderer );
394 void Visual::Base::EnablePreMultipliedAlpha( bool preMultiplied )
398 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
402 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
405 if( mImpl->mRenderer )
407 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
408 mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, static_cast<float>( preMultiplied ) );
412 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
414 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
417 void Visual::Base::DoSetOffScene( Actor& actor )
419 actor.RemoveRenderer( mImpl->mRenderer );
420 mImpl->mRenderer.Reset();
423 bool Visual::Base::IsOnScene() const
425 return mImpl->mFlags & Impl::IS_ON_SCENE;
428 bool Visual::Base::IsRoundedCornerRequired() const
430 return !EqualsZero( mImpl->mCornerRadius );
433 void Visual::Base::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
435 // May be overriden by derived class
438 void Visual::Base::RegisterMixColor()
440 // Only register if not already registered.
441 // (Color and Primitive visuals will register their own and save to this index)
442 if( mImpl->mMixColorIndex == Property::INVALID_INDEX )
444 mImpl->mMixColorIndex = DevelHandle::RegisterProperty(
446 Toolkit::Visual::Property::MIX_COLOR,
448 Vector3(mImpl->mMixColor) );
451 if( mImpl->mMixColor.a < 1.f )
453 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
456 mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, mImpl->mMixColor.a );
458 float preMultipliedAlpha = 0.0f;
459 if( IsPreMultipliedAlphaEnabled() )
461 preMultipliedAlpha = 1.0f;
463 mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, preMultipliedAlpha );
466 void Visual::Base::SetMixColor( const Vector4& color )
468 mImpl->mMixColor = color;
470 if( mImpl->mRenderer )
472 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, Vector3(color) );
473 mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, color.a );
476 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
481 void Visual::Base::SetMixColor( const Vector3& color )
483 mImpl->mMixColor.r = color.r;
484 mImpl->mMixColor.g = color.g;
485 mImpl->mMixColor.b = color.b;
487 if( mImpl->mRenderer )
489 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
493 const Vector4& Visual::Base::GetMixColor() const
495 return mImpl->mMixColor;
498 void Visual::Base::AddEventObserver( Visual::EventObserver& observer)
500 mImpl->mEventObserver = &observer;
503 void Visual::Base::RemoveEventObserver( Visual::EventObserver& observer )
505 mImpl->mEventObserver = NULL;
508 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
510 if( mImpl->mResourceStatus != resourceStatus )
512 mImpl->mResourceStatus = resourceStatus;
514 if( mImpl->mEventObserver )
516 // observer is currently a control impl
517 mImpl->mEventObserver->ResourceReady( *this );
522 bool Visual::Base::IsResourceReady() const
524 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY );
527 bool Visual::Base::IsSynchronousLoadingRequired() const
529 return ( mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING );
532 Toolkit::Visual::Type Visual::Base::GetType() const
537 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
539 return mImpl->mResourceStatus;
542 Visual::FittingMode Visual::Base::GetFittingMode() const
544 return mImpl->mFittingMode;
547 Visual::Base& Visual::Base::GetVisualObject()
552 Renderer Visual::Base::GetRenderer()
554 return mImpl->mRenderer;
557 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
559 Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
561 if( index == Property::INVALID_INDEX )
563 // Is it a shader property?
564 Shader shader = mImpl->mRenderer.GetShader();
565 index = DevelHandle::GetPropertyIndex( shader, key );
566 if( index != Property::INVALID_INDEX )
568 // Yes - we should register it in the Renderer so it can be set / animated
569 // independently, as shaders are shared across multiple renderers.
571 Property::Index keyIndex( Property::INVALID_KEY );
572 if( key.type == Property::Key::INDEX )
574 keyName = shader.GetPropertyName( index );
575 keyIndex = key.indexKey;
579 keyName = key.stringKey;
580 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
582 Property::Value value = shader.GetProperty( index );
583 index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
589 void Visual::Base::SetupTransition(
590 Dali::Animation& transition,
591 Internal::TransitionData::Animator& animator,
592 Property::Index index,
593 Property::Value& initialValue,
594 Property::Value& targetValue )
596 if( index != Property::INVALID_INDEX )
598 if( mImpl->mRenderer )
600 if( animator.animate == false )
602 mImpl->mRenderer.SetProperty( index, targetValue );
606 if( animator.initialValue.GetType() != Property::NONE )
608 mImpl->mRenderer.SetProperty( index, initialValue );
613 transition = Dali::Animation::New( 0.1f );
616 transition.AnimateTo( Property( mImpl->mRenderer, index ),
618 animator.alphaFunction,
619 TimePeriod( animator.timePeriodDelay,
620 animator.timePeriodDuration ) );
626 void Visual::Base::AnimateProperty(
627 Dali::Animation& transition,
628 Internal::TransitionData::Animator& animator )
630 #if defined(DEBUG_ENABLED)
632 std::ostringstream oss;
633 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
634 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
639 DoCreatePropertyMap( map );
640 Property::Value* valuePtr = map.Find( Toolkit::Visual::Property::TYPE );
644 valuePtr->Get( visualType );
647 if( animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
648 animator.propertyKey == MIX_COLOR ||
649 ( visualType == Toolkit::Visual::COLOR &&
650 animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
651 ( visualType == Toolkit::Visual::PRIMITIVE &&
652 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
654 AnimateMixColorProperty( transition, animator );
656 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
657 animator.propertyKey == OPACITY )
659 AnimateOpacityProperty( transition, animator );
661 else if( mImpl->mRenderer )
663 AnimateRendererProperty( transition, animator );
667 void Visual::Base::AnimateOpacityProperty(
668 Dali::Animation& transition,
669 Internal::TransitionData::Animator& animator )
671 bool isOpaque = mImpl->mMixColor.a >= 1.0f;
673 float initialOpacity;
674 if( animator.initialValue.Get( initialOpacity ) )
676 isOpaque = (initialOpacity >= 1.0f);
680 if( animator.targetValue.Get( targetOpacity ) )
682 mImpl->mMixColor.a = targetOpacity;
685 SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue );
686 SetupBlendMode( transition, isOpaque, animator.animate );
689 void Visual::Base::AnimateRendererProperty(
690 Dali::Animation& transition,
691 Internal::TransitionData::Animator& animator )
693 Property::Index index = GetPropertyIndex( animator.propertyKey );
694 if( index != Property::INVALID_INDEX )
696 if( animator.targetValue.GetType() != Property::NONE )
698 // Try writing target value into transform property map
699 // if it's not a valid key, then it won't alter mTransform
701 if( animator.propertyKey.type == Property::Key::INDEX )
703 map.Add( animator.propertyKey.indexKey, animator.targetValue );
707 map.Add( animator.propertyKey.stringKey, animator.targetValue );
710 mImpl->mTransform.UpdatePropertyMap( map );
713 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
717 void Visual::Base::AnimateMixColorProperty(
718 Dali::Animation& transition,
719 Internal::TransitionData::Animator& animator )
721 Property::Index index = mImpl->mMixColorIndex;
722 bool animateOpacity = false;
723 bool isOpaque = true;
725 Property::Value initialOpacity;
726 Property::Value targetOpacity;
727 Property::Value initialMixColor;
728 Property::Value targetMixColor;
730 if( index != Property::INVALID_INDEX )
732 Vector4 initialColor;
733 if( animator.initialValue.Get(initialColor) )
735 if( animator.initialValue.GetType() == Property::VECTOR4 )
737 // if there is an initial color specifying alpha, test it
738 isOpaque = initialColor.a >= 1.0f;
739 initialOpacity = initialColor.a;
741 initialMixColor = Vector3( initialColor );
744 // Set target value into data store
745 if( animator.targetValue.GetType() != Property::NONE )
748 animator.targetValue.Get(mixColor);
749 if( animator.targetValue.GetType() == Property::VECTOR4 )
751 mImpl->mMixColor.a = mixColor.a;
752 targetOpacity = mixColor.a;
753 animateOpacity = true;
756 mImpl->mMixColor.r = mixColor.r;
757 mImpl->mMixColor.g = mixColor.g;
758 mImpl->mMixColor.b = mixColor.b;
759 targetMixColor = Vector3(mixColor);
762 SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
765 SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity );
766 SetupBlendMode( transition, isOpaque, animator.animate );
771 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
773 // Ensure the blend mode is turned on if we are animating opacity, and
774 // turned off after the animation ends if the final value is opaque
775 if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
777 if( mImpl->mRenderer )
779 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
781 if( animating == true && mImpl->mMixColor.a >= 1.0f )
783 // When it becomes opaque, set the blend mode back to automatically
784 if( ! mImpl->mBlendSlotDelegate )
786 mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
788 transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
789 &Visual::Base::OnMixColorFinished );
795 void Visual::Base::OnMixColorFinished( Animation& animation )
797 if( mImpl->mRenderer )
799 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
800 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
801 ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
803 delete mImpl->mBlendSlotDelegate;
804 mImpl->mBlendSlotDelegate = NULL;
807 } // namespace Internal
809 } // namespace Toolkit