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/public-api/common/dali-common.h>
23 #include <dali/devel-api/object/handle-devel.h>
24 #include <dali/devel-api/scripting/enum-helper.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( 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_TABLE_END( FITTING_MODE )
65 Visual::Base::Base( VisualFactoryCache& factoryCache, FittingMode fittingMode )
66 : mImpl( new Impl(fittingMode) ),
67 mFactoryCache( factoryCache )
76 void Visual::Base::SetCustomShader( const Property::Map& shaderMap )
78 if( mImpl->mCustomShader )
80 mImpl->mCustomShader->SetPropertyMap( shaderMap );
84 mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
88 void Visual::Base::SetProperties( const Property::Map& propertyMap )
90 for( size_t i = 0; i < propertyMap.Count(); ++i )
92 const KeyValuePair& pair = propertyMap.GetKeyValue( i );
93 const Property::Key& key = pair.first;
94 const Property::Value& value = pair.second;
96 Property::Key matchKey = key;
97 if( matchKey.type == Property::Key::STRING )
99 if( matchKey == CUSTOM_SHADER )
101 matchKey = Property::Key( Toolkit::Visual::Property::SHADER );
103 else if( matchKey == TRANSFORM )
105 matchKey = Property::Key( Toolkit::Visual::Property::TRANSFORM );
107 else if( matchKey == PREMULTIPLIED_ALPHA )
109 matchKey = Property::Key( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA );
111 else if( matchKey == MIX_COLOR )
113 matchKey = Property::Key( Toolkit::Visual::Property::MIX_COLOR );
115 else if( matchKey == OPACITY )
117 matchKey = Property::Key( Toolkit::Visual::Property::OPACITY );
119 else if( matchKey == FITTING_MODE )
121 matchKey = Property::Key( Toolkit::DevelVisual::Property::FITTING_MODE );
125 switch( matchKey.indexKey )
127 case Toolkit::Visual::Property::SHADER:
129 Property::Map shaderMap;
130 if( value.Get( shaderMap ) )
132 SetCustomShader( shaderMap );
137 case Toolkit::Visual::Property::TRANSFORM:
140 if( value.Get( map ) )
142 mImpl->mTransform.SetPropertyMap( map );
147 case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
149 bool premultipliedAlpha = false;
150 if( value.Get( premultipliedAlpha ) )
152 EnablePreMultipliedAlpha( premultipliedAlpha );
157 case Toolkit::Visual::Property::MIX_COLOR:
160 if( value.Get( mixColor ) )
162 if( value.GetType() == Property::VECTOR4 )
164 SetMixColor( mixColor );
168 Vector3 mixColor3(mixColor);
169 SetMixColor( mixColor3 );
174 case Toolkit::Visual::Property::OPACITY:
177 if( value.Get( opacity ) )
179 mImpl->mMixColor.a = opacity;
180 SetMixColor( mImpl->mMixColor );
184 case Toolkit::DevelVisual::Property::FITTING_MODE:
186 Scripting::GetEnumerationProperty< Visual::FittingMode >(
187 value, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode );
193 DoSetProperties( propertyMap );
196 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
198 mImpl->mControlSize = controlSize;
199 mImpl->mTransform.UpdatePropertyMap( transform );
201 #if defined(DEBUG_ENABLED)
202 std::ostringstream oss;
204 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::SetTransformAndSize(%s) - [\e[1;32mtransform: %s controlSize: (%3.1f, %3.1f)]\e[0m\n",
205 GetName().c_str(), oss.str().c_str(), controlSize.x, controlSize.y );
211 void Visual::Base::SetName( const std::string& name )
216 const std::string& Visual::Base::GetName()
221 float Visual::Base::GetHeightForWidth( float width )
223 float aspectCorrectedHeight = 0.f;
225 GetNaturalSize( naturalSize );
226 if( naturalSize.width )
228 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
230 return aspectCorrectedHeight;
233 float Visual::Base::GetWidthForHeight( float height )
235 float aspectCorrectedWidth = 0.f;
237 GetNaturalSize( naturalSize );
238 if( naturalSize.height > 0.0f )
240 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
242 return aspectCorrectedWidth;
245 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
247 naturalSize = Vector2::ZERO;
250 void Visual::Base::DoAction( const Property::Index actionId, const Property::Value attributes )
252 OnDoAction( actionId, attributes );
255 void Visual::Base::SetDepthIndex( int index )
257 mImpl->mDepthIndex = index;
258 if( mImpl->mRenderer )
260 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
264 int Visual::Base::GetDepthIndex() const
266 return mImpl->mDepthIndex;
269 void Visual::Base::SetOnStage( Actor& actor )
273 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
274 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
275 DoSetOnStage( actor );
277 if( mImpl->mRenderer )
281 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
282 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
283 mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
288 void Visual::Base::SetOffStage( Actor& actor )
292 DoSetOffStage( actor );
293 mImpl->mMixColorIndex = Property::INVALID_INDEX;
294 mImpl->mOpacityIndex = Property::INVALID_INDEX;
295 mImpl->mFlags &= ~Impl::IS_ON_STAGE;
299 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
301 DoCreatePropertyMap( map );
303 if( mImpl->mCustomShader )
305 mImpl->mCustomShader->CreatePropertyMap( map );
308 Property::Map transform;
309 mImpl->mTransform.GetPropertyMap( transform );
310 map.Insert( Toolkit::Visual::Property::TRANSFORM, transform );
312 bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
313 map.Insert( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
315 // Note, Color and Primitive will also insert their own mix color into the map
316 // which is ok, because they have a different key value range.
317 map.Insert( Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor ); // vec4
318 map.Insert( Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a );
320 auto fittingModeString = Scripting::GetLinearEnumerationName< FittingMode >(
321 mImpl->mFittingMode, FITTING_MODE_TABLE, FITTING_MODE_TABLE_COUNT );
322 map.Insert( Toolkit::DevelVisual::Property::FITTING_MODE, fittingModeString );
325 void Visual::Base::CreateInstancePropertyMap( Property::Map& map ) const
327 DoCreateInstancePropertyMap( map );
329 if( mImpl->mCustomShader )
331 mImpl->mCustomShader->CreatePropertyMap( map );
334 //map.Insert( Toolkit::Visual::Property::DEPTH_INDEX, mImpl->mDepthIndex );
335 //map.Insert( Toolkit::Visual::Property::ENABLED, (bool) mImpl->mRenderer );
339 void Visual::Base::EnablePreMultipliedAlpha( bool preMultiplied )
343 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
347 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
350 if( mImpl->mRenderer )
352 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
353 mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, static_cast<float>( preMultiplied ) );
357 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
359 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
362 void Visual::Base::DoSetOffStage( Actor& actor )
364 actor.RemoveRenderer( mImpl->mRenderer );
365 mImpl->mRenderer.Reset();
368 bool Visual::Base::IsOnStage() const
370 return mImpl->mFlags & Impl::IS_ON_STAGE;
373 void Visual::Base::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
375 // May be overriden by derived class
378 void Visual::Base::RegisterMixColor()
380 // Only register if not already registered.
381 // (Color and Primitive visuals will register their own and save to this index)
382 if( mImpl->mMixColorIndex == Property::INVALID_INDEX )
384 mImpl->mMixColorIndex = DevelHandle::RegisterProperty(
386 Toolkit::Visual::Property::MIX_COLOR,
388 Vector3(mImpl->mMixColor) );
391 if( mImpl->mMixColor.a < 1.f )
393 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
396 if( mImpl->mOpacityIndex == Property::INVALID_INDEX )
398 mImpl->mOpacityIndex = DevelHandle::RegisterProperty(
400 Toolkit::Visual::Property::OPACITY,
402 mImpl->mMixColor.a );
405 float preMultipliedAlpha = 0.0f;
406 if( IsPreMultipliedAlphaEnabled() )
408 preMultipliedAlpha = 1.0f;
410 mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, preMultipliedAlpha );
413 void Visual::Base::SetMixColor( const Vector4& color )
415 mImpl->mMixColor = color;
417 if( mImpl->mRenderer )
419 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, Vector3(color) );
420 mImpl->mRenderer.SetProperty( mImpl->mOpacityIndex, color.a );
423 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
428 void Visual::Base::SetMixColor( const Vector3& color )
430 mImpl->mMixColor.r = color.r;
431 mImpl->mMixColor.g = color.g;
432 mImpl->mMixColor.b = color.b;
434 if( mImpl->mRenderer )
436 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
440 const Vector4& Visual::Base::GetMixColor() const
442 return mImpl->mMixColor;
445 void Visual::Base::AddResourceObserver( Visual::ResourceObserver& observer)
447 mImpl->mResourceObserver = &observer;
450 void Visual::Base::RemoveResourceObserver( Visual::ResourceObserver& observer )
452 mImpl->mResourceObserver = NULL;
455 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
457 if( mImpl->mResourceStatus != resourceStatus )
459 mImpl->mResourceStatus = resourceStatus;
461 if( mImpl->mResourceObserver )
463 // observer is currently a control impl
464 mImpl->mResourceObserver->ResourceReady( *this );
469 bool Visual::Base::IsResourceReady() const
471 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY );
474 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
476 return mImpl->mResourceStatus;
479 Visual::FittingMode Visual::Base::GetFittingMode() const
481 return mImpl->mFittingMode;
484 Renderer Visual::Base::GetRenderer()
486 return mImpl->mRenderer;
489 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
491 Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
493 if( index == Property::INVALID_INDEX )
495 // Is it a shader property?
496 Shader shader = mImpl->mRenderer.GetShader();
497 index = DevelHandle::GetPropertyIndex( shader, key );
498 if( index != Property::INVALID_INDEX )
500 // Yes - we should register it in the Renderer so it can be set / animated
501 // independently, as shaders are shared across multiple renderers.
503 Property::Index keyIndex( Property::INVALID_KEY );
504 if( key.type == Property::Key::INDEX )
506 keyName = shader.GetPropertyName( index );
507 keyIndex = key.indexKey;
511 keyName = key.stringKey;
512 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
514 Property::Value value = shader.GetProperty( index );
515 index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
521 void Visual::Base::SetupTransition(
522 Dali::Animation& transition,
523 Internal::TransitionData::Animator& animator,
524 Property::Index index,
525 Property::Value& initialValue,
526 Property::Value& targetValue )
528 if( index != Property::INVALID_INDEX )
530 if( mImpl->mRenderer )
532 if( animator.animate == false )
534 mImpl->mRenderer.SetProperty( index, targetValue );
538 if( animator.initialValue.GetType() != Property::NONE )
540 mImpl->mRenderer.SetProperty( index, initialValue );
545 transition = Dali::Animation::New( 0.1f );
548 transition.AnimateTo( Property( mImpl->mRenderer, index ),
550 animator.alphaFunction,
551 TimePeriod( animator.timePeriodDelay,
552 animator.timePeriodDuration ) );
558 void Visual::Base::AnimateProperty(
559 Dali::Animation& transition,
560 Internal::TransitionData::Animator& animator )
562 #if defined(DEBUG_ENABLED)
564 std::ostringstream oss;
565 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
566 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
571 DoCreatePropertyMap( map );
572 Property::Value* valuePtr = map.Find( Toolkit::Visual::Property::TYPE );
576 valuePtr->Get( visualType );
579 if( animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
580 animator.propertyKey == MIX_COLOR ||
581 ( visualType == Toolkit::Visual::COLOR &&
582 animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
583 ( visualType == Toolkit::Visual::PRIMITIVE &&
584 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
586 AnimateMixColorProperty( transition, animator );
588 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
589 animator.propertyKey == OPACITY )
591 AnimateOpacityProperty( transition, animator );
593 else if( mImpl->mRenderer )
595 AnimateRendererProperty( transition, animator );
599 void Visual::Base::AnimateOpacityProperty(
600 Dali::Animation& transition,
601 Internal::TransitionData::Animator& animator )
603 Property::Index index = mImpl->mOpacityIndex;
605 bool isOpaque = mImpl->mMixColor.a >= 1.0f;
607 if( index != Property::INVALID_INDEX )
609 float initialOpacity;
610 if( animator.initialValue.Get( initialOpacity ) )
612 isOpaque = (initialOpacity >= 1.0f);
616 if( animator.targetValue.Get( targetOpacity ) )
618 mImpl->mMixColor.a = targetOpacity;
621 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
622 SetupBlendMode( transition, isOpaque, animator.animate );
626 void Visual::Base::AnimateRendererProperty(
627 Dali::Animation& transition,
628 Internal::TransitionData::Animator& animator )
630 Property::Index index = GetPropertyIndex( animator.propertyKey );
631 if( index != Property::INVALID_INDEX )
633 if( animator.targetValue.GetType() != Property::NONE )
635 // Try writing target value into transform property map
636 // if it's not a valid key, then it won't alter mTransform
638 if( animator.propertyKey.type == Property::Key::INDEX )
640 map.Add( animator.propertyKey.indexKey, animator.targetValue );
644 map.Add( animator.propertyKey.stringKey, animator.targetValue );
647 mImpl->mTransform.UpdatePropertyMap( map );
650 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
654 void Visual::Base::AnimateMixColorProperty(
655 Dali::Animation& transition,
656 Internal::TransitionData::Animator& animator )
658 Property::Index index = mImpl->mMixColorIndex;
659 bool animateOpacity = false;
660 bool isOpaque = true;
662 Property::Value initialOpacity;
663 Property::Value targetOpacity;
664 Property::Value initialMixColor;
665 Property::Value targetMixColor;
667 if( index != Property::INVALID_INDEX )
669 Vector4 initialColor;
670 if( animator.initialValue.Get(initialColor) )
672 if( animator.initialValue.GetType() == Property::VECTOR4 )
674 // if there is an initial color specifying alpha, test it
675 isOpaque = initialColor.a >= 1.0f;
676 initialOpacity = initialColor.a;
678 initialMixColor = Vector3( initialColor );
681 // Set target value into data store
682 if( animator.targetValue.GetType() != Property::NONE )
685 animator.targetValue.Get(mixColor);
686 if( animator.targetValue.GetType() == Property::VECTOR4 )
688 mImpl->mMixColor.a = mixColor.a;
689 targetOpacity = mixColor.a;
690 animateOpacity = true;
693 mImpl->mMixColor.r = mixColor.r;
694 mImpl->mMixColor.g = mixColor.g;
695 mImpl->mMixColor.b = mixColor.b;
696 targetMixColor = Vector3(mixColor);
699 SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
702 SetupTransition( transition, animator, mImpl->mOpacityIndex, initialOpacity, targetOpacity );
703 SetupBlendMode( transition, isOpaque, animator.animate );
708 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
710 // Ensure the blend mode is turned on if we are animating opacity, and
711 // turned off after the animation ends if the final value is opaque
712 if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
714 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
716 if( animating == true && mImpl->mMixColor.a >= 1.0f )
718 // When it becomes opaque, set the blend mode back to automatically
719 if( ! mImpl->mBlendSlotDelegate )
721 mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
723 transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
724 &Visual::Base::OnMixColorFinished );
729 void Visual::Base::OnMixColorFinished( Animation& animation )
731 if( mImpl->mRenderer )
733 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
734 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
735 ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
737 delete mImpl->mBlendSlotDelegate;
738 mImpl->mBlendSlotDelegate = NULL;
741 } // namespace Internal
743 } // namespace Toolkit