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 )
71 : mImpl( new Impl(fittingMode) ),
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::ResourceStatus Visual::Base::GetResourceStatus() const
507 return mImpl->mResourceStatus;
510 Visual::FittingMode Visual::Base::GetFittingMode() const
512 return mImpl->mFittingMode;
515 Visual::Base& Visual::Base::GetVisualObject()
520 Renderer Visual::Base::GetRenderer()
522 return mImpl->mRenderer;
525 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
527 Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
529 if( index == Property::INVALID_INDEX )
531 // Is it a shader property?
532 Shader shader = mImpl->mRenderer.GetShader();
533 index = DevelHandle::GetPropertyIndex( shader, key );
534 if( index != Property::INVALID_INDEX )
536 // Yes - we should register it in the Renderer so it can be set / animated
537 // independently, as shaders are shared across multiple renderers.
539 Property::Index keyIndex( Property::INVALID_KEY );
540 if( key.type == Property::Key::INDEX )
542 keyName = shader.GetPropertyName( index );
543 keyIndex = key.indexKey;
547 keyName = key.stringKey;
548 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
550 Property::Value value = shader.GetProperty( index );
551 index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
557 void Visual::Base::SetupTransition(
558 Dali::Animation& transition,
559 Internal::TransitionData::Animator& animator,
560 Property::Index index,
561 Property::Value& initialValue,
562 Property::Value& targetValue )
564 if( index != Property::INVALID_INDEX )
566 if( mImpl->mRenderer )
568 if( animator.animate == false )
570 mImpl->mRenderer.SetProperty( index, targetValue );
574 if( animator.initialValue.GetType() != Property::NONE )
576 mImpl->mRenderer.SetProperty( index, initialValue );
581 transition = Dali::Animation::New( 0.1f );
584 transition.AnimateTo( Property( mImpl->mRenderer, index ),
586 animator.alphaFunction,
587 TimePeriod( animator.timePeriodDelay,
588 animator.timePeriodDuration ) );
594 void Visual::Base::AnimateProperty(
595 Dali::Animation& transition,
596 Internal::TransitionData::Animator& animator )
598 #if defined(DEBUG_ENABLED)
600 std::ostringstream oss;
601 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
602 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
607 DoCreatePropertyMap( map );
608 Property::Value* valuePtr = map.Find( Toolkit::Visual::Property::TYPE );
612 valuePtr->Get( visualType );
615 if( animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
616 animator.propertyKey == MIX_COLOR ||
617 ( visualType == Toolkit::Visual::COLOR &&
618 animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
619 ( visualType == Toolkit::Visual::PRIMITIVE &&
620 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
622 AnimateMixColorProperty( transition, animator );
624 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
625 animator.propertyKey == OPACITY )
627 AnimateOpacityProperty( transition, animator );
629 else if( mImpl->mRenderer )
631 AnimateRendererProperty( transition, animator );
635 void Visual::Base::AnimateOpacityProperty(
636 Dali::Animation& transition,
637 Internal::TransitionData::Animator& animator )
639 bool isOpaque = mImpl->mMixColor.a >= 1.0f;
641 float initialOpacity;
642 if( animator.initialValue.Get( initialOpacity ) )
644 isOpaque = (initialOpacity >= 1.0f);
648 if( animator.targetValue.Get( targetOpacity ) )
650 mImpl->mMixColor.a = targetOpacity;
653 SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue );
654 SetupBlendMode( transition, isOpaque, animator.animate );
657 void Visual::Base::AnimateRendererProperty(
658 Dali::Animation& transition,
659 Internal::TransitionData::Animator& animator )
661 Property::Index index = GetPropertyIndex( animator.propertyKey );
662 if( index != Property::INVALID_INDEX )
664 if( animator.targetValue.GetType() != Property::NONE )
666 // Try writing target value into transform property map
667 // if it's not a valid key, then it won't alter mTransform
669 if( animator.propertyKey.type == Property::Key::INDEX )
671 map.Add( animator.propertyKey.indexKey, animator.targetValue );
675 map.Add( animator.propertyKey.stringKey, animator.targetValue );
678 mImpl->mTransform.UpdatePropertyMap( map );
681 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
685 void Visual::Base::AnimateMixColorProperty(
686 Dali::Animation& transition,
687 Internal::TransitionData::Animator& animator )
689 Property::Index index = mImpl->mMixColorIndex;
690 bool animateOpacity = false;
691 bool isOpaque = true;
693 Property::Value initialOpacity;
694 Property::Value targetOpacity;
695 Property::Value initialMixColor;
696 Property::Value targetMixColor;
698 if( index != Property::INVALID_INDEX )
700 Vector4 initialColor;
701 if( animator.initialValue.Get(initialColor) )
703 if( animator.initialValue.GetType() == Property::VECTOR4 )
705 // if there is an initial color specifying alpha, test it
706 isOpaque = initialColor.a >= 1.0f;
707 initialOpacity = initialColor.a;
709 initialMixColor = Vector3( initialColor );
712 // Set target value into data store
713 if( animator.targetValue.GetType() != Property::NONE )
716 animator.targetValue.Get(mixColor);
717 if( animator.targetValue.GetType() == Property::VECTOR4 )
719 mImpl->mMixColor.a = mixColor.a;
720 targetOpacity = mixColor.a;
721 animateOpacity = true;
724 mImpl->mMixColor.r = mixColor.r;
725 mImpl->mMixColor.g = mixColor.g;
726 mImpl->mMixColor.b = mixColor.b;
727 targetMixColor = Vector3(mixColor);
730 SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
733 SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity );
734 SetupBlendMode( transition, isOpaque, animator.animate );
739 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
741 // Ensure the blend mode is turned on if we are animating opacity, and
742 // turned off after the animation ends if the final value is opaque
743 if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
745 if( mImpl->mRenderer )
747 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
749 if( animating == true && mImpl->mMixColor.a >= 1.0f )
751 // When it becomes opaque, set the blend mode back to automatically
752 if( ! mImpl->mBlendSlotDelegate )
754 mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
756 transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
757 &Visual::Base::OnMixColorFinished );
763 void Visual::Base::OnMixColorFinished( Animation& animation )
765 if( mImpl->mRenderer )
767 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
768 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
769 ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
771 delete mImpl->mBlendSlotDelegate;
772 mImpl->mBlendSlotDelegate = NULL;
775 } // namespace Internal
777 } // namespace Toolkit