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 );
134 switch( matchKey.indexKey )
136 case Toolkit::Visual::Property::SHADER:
138 Property::Map shaderMap;
139 if( value.Get( shaderMap ) )
141 SetCustomShader( shaderMap );
146 case Toolkit::Visual::Property::TRANSFORM:
149 if( value.Get( map ) )
151 mImpl->mTransform.SetPropertyMap( map );
156 case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
158 bool premultipliedAlpha = false;
159 if( value.Get( premultipliedAlpha ) )
161 EnablePreMultipliedAlpha( premultipliedAlpha );
166 case Toolkit::Visual::Property::MIX_COLOR:
169 if( value.Get( mixColor ) )
171 if( value.GetType() == Property::VECTOR4 )
173 SetMixColor( mixColor );
177 Vector3 mixColor3(mixColor);
178 SetMixColor( mixColor3 );
183 case Toolkit::Visual::Property::OPACITY:
186 if( value.Get( opacity ) )
188 mImpl->mMixColor.a = opacity;
189 SetMixColor( mImpl->mMixColor );
193 case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
195 Scripting::GetEnumerationProperty< Visual::FittingMode >(
196 value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode );
199 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
202 if( value.Get( radius ) )
204 mImpl->mCornerRadius = radius;
211 DoSetProperties( propertyMap );
214 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
216 mImpl->mControlSize = controlSize;
217 mImpl->mTransform.UpdatePropertyMap( transform );
219 #if defined(DEBUG_ENABLED)
220 std::ostringstream oss;
222 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::SetTransformAndSize(%s) - [\e[1;32mtransform: %s controlSize: (%3.1f, %3.1f)]\e[0m\n",
223 GetName().c_str(), oss.str().c_str(), controlSize.x, controlSize.y );
229 void Visual::Base::SetName( const std::string& name )
234 const std::string& Visual::Base::GetName() const
239 float Visual::Base::GetHeightForWidth( float width )
241 float aspectCorrectedHeight = 0.f;
243 GetNaturalSize( naturalSize );
244 if( naturalSize.width )
246 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
248 return aspectCorrectedHeight;
251 float Visual::Base::GetWidthForHeight( float height )
253 float aspectCorrectedWidth = 0.f;
255 GetNaturalSize( naturalSize );
256 if( naturalSize.height > 0.0f )
258 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
260 return aspectCorrectedWidth;
263 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
265 naturalSize = Vector2::ZERO;
268 void Visual::Base::DoAction( const Property::Index actionId, const Property::Value attributes )
270 OnDoAction( actionId, attributes );
273 void Visual::Base::SetDepthIndex( int index )
275 mImpl->mDepthIndex = index;
276 if( mImpl->mRenderer )
278 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
282 int Visual::Base::GetDepthIndex() const
284 return mImpl->mDepthIndex;
287 void Visual::Base::SetOnStage( Actor& actor )
291 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
292 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
293 DoSetOnStage( actor );
295 if( mImpl->mRenderer )
299 if( IsRoundedCornerRequired() )
301 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterProperty( CORNER_RADIUS, mImpl->mCornerRadius );
303 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
306 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
307 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
308 mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
313 void Visual::Base::SetOffStage( Actor& actor )
317 DoSetOffStage( actor );
318 mImpl->mMixColorIndex = Property::INVALID_INDEX;
319 mImpl->mCornerRadiusIndex = Property::INVALID_INDEX;
320 mImpl->mFlags &= ~Impl::IS_ON_STAGE;
324 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
326 DoCreatePropertyMap( map );
328 if( mImpl->mCustomShader )
330 mImpl->mCustomShader->CreatePropertyMap( map );
333 Property::Map transform;
334 mImpl->mTransform.GetPropertyMap( transform );
335 map.Insert( Toolkit::Visual::Property::TRANSFORM, transform );
337 bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
338 map.Insert( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
340 // Note, Color and Primitive will also insert their own mix color into the map
341 // which is ok, because they have a different key value range.
342 map.Insert( Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor ); // vec4
343 map.Insert( Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a );
344 map.Insert( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired() );
346 auto fittingModeString = Scripting::GetLinearEnumerationName< FittingMode >(
347 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT );
348 map.Insert( Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString );
350 map.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius );
353 void Visual::Base::CreateInstancePropertyMap( Property::Map& map ) const
355 DoCreateInstancePropertyMap( map );
357 if( mImpl->mCustomShader )
359 mImpl->mCustomShader->CreatePropertyMap( map );
362 //map.Insert( Toolkit::Visual::Property::DEPTH_INDEX, mImpl->mDepthIndex );
363 //map.Insert( Toolkit::Visual::Property::ENABLED, (bool) mImpl->mRenderer );
367 void Visual::Base::EnablePreMultipliedAlpha( bool preMultiplied )
371 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
375 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
378 if( mImpl->mRenderer )
380 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
381 mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, static_cast<float>( preMultiplied ) );
385 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
387 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
390 void Visual::Base::DoSetOffStage( Actor& actor )
392 actor.RemoveRenderer( mImpl->mRenderer );
393 mImpl->mRenderer.Reset();
396 bool Visual::Base::IsOnStage() const
398 return mImpl->mFlags & Impl::IS_ON_STAGE;
401 bool Visual::Base::IsRoundedCornerRequired() const
403 return !EqualsZero( mImpl->mCornerRadius );
406 void Visual::Base::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
408 // May be overriden by derived class
411 void Visual::Base::RegisterMixColor()
413 // Only register if not already registered.
414 // (Color and Primitive visuals will register their own and save to this index)
415 if( mImpl->mMixColorIndex == Property::INVALID_INDEX )
417 mImpl->mMixColorIndex = DevelHandle::RegisterProperty(
419 Toolkit::Visual::Property::MIX_COLOR,
421 Vector3(mImpl->mMixColor) );
424 if( mImpl->mMixColor.a < 1.f )
426 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
429 mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, mImpl->mMixColor.a );
431 float preMultipliedAlpha = 0.0f;
432 if( IsPreMultipliedAlphaEnabled() )
434 preMultipliedAlpha = 1.0f;
436 mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, preMultipliedAlpha );
439 void Visual::Base::SetMixColor( const Vector4& color )
441 mImpl->mMixColor = color;
443 if( mImpl->mRenderer )
445 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, Vector3(color) );
446 mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, color.a );
449 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
454 void Visual::Base::SetMixColor( const Vector3& color )
456 mImpl->mMixColor.r = color.r;
457 mImpl->mMixColor.g = color.g;
458 mImpl->mMixColor.b = color.b;
460 if( mImpl->mRenderer )
462 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
466 const Vector4& Visual::Base::GetMixColor() const
468 return mImpl->mMixColor;
471 void Visual::Base::AddEventObserver( Visual::EventObserver& observer)
473 mImpl->mEventObserver = &observer;
476 void Visual::Base::RemoveEventObserver( Visual::EventObserver& observer )
478 mImpl->mEventObserver = NULL;
481 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
483 if( mImpl->mResourceStatus != resourceStatus )
485 mImpl->mResourceStatus = resourceStatus;
487 if( mImpl->mEventObserver )
489 // observer is currently a control impl
490 mImpl->mEventObserver->ResourceReady( *this );
495 bool Visual::Base::IsResourceReady() const
497 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY );
500 bool Visual::Base::IsSynchronousLoadingRequired() const
502 return ( mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING );
505 Toolkit::Visual::Type Visual::Base::GetType() const
510 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
512 return mImpl->mResourceStatus;
515 Visual::FittingMode Visual::Base::GetFittingMode() const
517 return mImpl->mFittingMode;
520 Visual::Base& Visual::Base::GetVisualObject()
525 Renderer Visual::Base::GetRenderer()
527 return mImpl->mRenderer;
530 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
532 Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
534 if( index == Property::INVALID_INDEX )
536 // Is it a shader property?
537 Shader shader = mImpl->mRenderer.GetShader();
538 index = DevelHandle::GetPropertyIndex( shader, key );
539 if( index != Property::INVALID_INDEX )
541 // Yes - we should register it in the Renderer so it can be set / animated
542 // independently, as shaders are shared across multiple renderers.
544 Property::Index keyIndex( Property::INVALID_KEY );
545 if( key.type == Property::Key::INDEX )
547 keyName = shader.GetPropertyName( index );
548 keyIndex = key.indexKey;
552 keyName = key.stringKey;
553 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
555 Property::Value value = shader.GetProperty( index );
556 index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
562 void Visual::Base::SetupTransition(
563 Dali::Animation& transition,
564 Internal::TransitionData::Animator& animator,
565 Property::Index index,
566 Property::Value& initialValue,
567 Property::Value& targetValue )
569 if( index != Property::INVALID_INDEX )
571 if( mImpl->mRenderer )
573 if( animator.animate == false )
575 mImpl->mRenderer.SetProperty( index, targetValue );
579 if( animator.initialValue.GetType() != Property::NONE )
581 mImpl->mRenderer.SetProperty( index, initialValue );
586 transition = Dali::Animation::New( 0.1f );
589 transition.AnimateTo( Property( mImpl->mRenderer, index ),
591 animator.alphaFunction,
592 TimePeriod( animator.timePeriodDelay,
593 animator.timePeriodDuration ) );
599 void Visual::Base::AnimateProperty(
600 Dali::Animation& transition,
601 Internal::TransitionData::Animator& animator )
603 #if defined(DEBUG_ENABLED)
605 std::ostringstream oss;
606 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
607 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
612 DoCreatePropertyMap( map );
613 Property::Value* valuePtr = map.Find( Toolkit::Visual::Property::TYPE );
617 valuePtr->Get( visualType );
620 if( animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
621 animator.propertyKey == MIX_COLOR ||
622 ( visualType == Toolkit::Visual::COLOR &&
623 animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
624 ( visualType == Toolkit::Visual::PRIMITIVE &&
625 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
627 AnimateMixColorProperty( transition, animator );
629 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
630 animator.propertyKey == OPACITY )
632 AnimateOpacityProperty( transition, animator );
634 else if( mImpl->mRenderer )
636 AnimateRendererProperty( transition, animator );
640 void Visual::Base::AnimateOpacityProperty(
641 Dali::Animation& transition,
642 Internal::TransitionData::Animator& animator )
644 bool isOpaque = mImpl->mMixColor.a >= 1.0f;
646 float initialOpacity;
647 if( animator.initialValue.Get( initialOpacity ) )
649 isOpaque = (initialOpacity >= 1.0f);
653 if( animator.targetValue.Get( targetOpacity ) )
655 mImpl->mMixColor.a = targetOpacity;
658 SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue );
659 SetupBlendMode( transition, isOpaque, animator.animate );
662 void Visual::Base::AnimateRendererProperty(
663 Dali::Animation& transition,
664 Internal::TransitionData::Animator& animator )
666 Property::Index index = GetPropertyIndex( animator.propertyKey );
667 if( index != Property::INVALID_INDEX )
669 if( animator.targetValue.GetType() != Property::NONE )
671 // Try writing target value into transform property map
672 // if it's not a valid key, then it won't alter mTransform
674 if( animator.propertyKey.type == Property::Key::INDEX )
676 map.Add( animator.propertyKey.indexKey, animator.targetValue );
680 map.Add( animator.propertyKey.stringKey, animator.targetValue );
683 mImpl->mTransform.UpdatePropertyMap( map );
686 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
690 void Visual::Base::AnimateMixColorProperty(
691 Dali::Animation& transition,
692 Internal::TransitionData::Animator& animator )
694 Property::Index index = mImpl->mMixColorIndex;
695 bool animateOpacity = false;
696 bool isOpaque = true;
698 Property::Value initialOpacity;
699 Property::Value targetOpacity;
700 Property::Value initialMixColor;
701 Property::Value targetMixColor;
703 if( index != Property::INVALID_INDEX )
705 Vector4 initialColor;
706 if( animator.initialValue.Get(initialColor) )
708 if( animator.initialValue.GetType() == Property::VECTOR4 )
710 // if there is an initial color specifying alpha, test it
711 isOpaque = initialColor.a >= 1.0f;
712 initialOpacity = initialColor.a;
714 initialMixColor = Vector3( initialColor );
717 // Set target value into data store
718 if( animator.targetValue.GetType() != Property::NONE )
721 animator.targetValue.Get(mixColor);
722 if( animator.targetValue.GetType() == Property::VECTOR4 )
724 mImpl->mMixColor.a = mixColor.a;
725 targetOpacity = mixColor.a;
726 animateOpacity = true;
729 mImpl->mMixColor.r = mixColor.r;
730 mImpl->mMixColor.g = mixColor.g;
731 mImpl->mMixColor.b = mixColor.b;
732 targetMixColor = Vector3(mixColor);
735 SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
738 SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity );
739 SetupBlendMode( transition, isOpaque, animator.animate );
744 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
746 // Ensure the blend mode is turned on if we are animating opacity, and
747 // turned off after the animation ends if the final value is opaque
748 if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
750 if( mImpl->mRenderer )
752 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
754 if( animating == true && mImpl->mMixColor.a >= 1.0f )
756 // When it becomes opaque, set the blend mode back to automatically
757 if( ! mImpl->mBlendSlotDelegate )
759 mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
761 transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
762 &Visual::Base::OnMixColorFinished );
768 void Visual::Base::OnMixColorFinished( Animation& animation )
770 if( mImpl->mRenderer )
772 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
773 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
774 ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
776 delete mImpl->mBlendSlotDelegate;
777 mImpl->mBlendSlotDelegate = NULL;
780 } // namespace Internal
782 } // namespace Toolkit