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_TABLE_END( VISUAL_FITTING_MODE )
66 Visual::Base::Base( VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type )
67 : mImpl( new Impl( fittingMode, type ) ),
68 mFactoryCache( factoryCache )
77 void Visual::Base::SetCustomShader( const Property::Map& shaderMap )
79 if( mImpl->mCustomShader )
81 mImpl->mCustomShader->SetPropertyMap( shaderMap );
85 mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
89 void Visual::Base::SetProperties( const Property::Map& propertyMap )
91 for( size_t i = 0; i < propertyMap.Count(); ++i )
93 const KeyValuePair& pair = propertyMap.GetKeyValue( i );
94 const Property::Key& key = pair.first;
95 const Property::Value& value = pair.second;
97 Property::Key matchKey = key;
98 if( matchKey.type == Property::Key::STRING )
100 if( matchKey == CUSTOM_SHADER )
102 matchKey = Property::Key( Toolkit::Visual::Property::SHADER );
104 else if( matchKey == TRANSFORM )
106 matchKey = Property::Key( Toolkit::Visual::Property::TRANSFORM );
108 else if( matchKey == PREMULTIPLIED_ALPHA )
110 matchKey = Property::Key( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA );
112 else if( matchKey == MIX_COLOR )
114 matchKey = Property::Key( Toolkit::Visual::Property::MIX_COLOR );
116 else if( matchKey == OPACITY )
118 matchKey = Property::Key( Toolkit::Visual::Property::OPACITY );
120 else if( matchKey == VISUAL_FITTING_MODE )
122 matchKey = Property::Key( Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE );
124 else if( matchKey == CORNER_RADIUS )
126 matchKey = Property::Key( Toolkit::DevelVisual::Property::CORNER_RADIUS );
130 switch( matchKey.indexKey )
132 case Toolkit::Visual::Property::SHADER:
134 Property::Map shaderMap;
135 if( value.Get( shaderMap ) )
137 SetCustomShader( shaderMap );
142 case Toolkit::Visual::Property::TRANSFORM:
145 if( value.Get( map ) )
147 mImpl->mTransform.SetPropertyMap( map );
152 case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
154 bool premultipliedAlpha = false;
155 if( value.Get( premultipliedAlpha ) )
157 EnablePreMultipliedAlpha( premultipliedAlpha );
162 case Toolkit::Visual::Property::MIX_COLOR:
165 if( value.Get( mixColor ) )
167 if( value.GetType() == Property::VECTOR4 )
169 SetMixColor( mixColor );
173 Vector3 mixColor3(mixColor);
174 SetMixColor( mixColor3 );
179 case Toolkit::Visual::Property::OPACITY:
182 if( value.Get( opacity ) )
184 mImpl->mMixColor.a = opacity;
185 SetMixColor( mImpl->mMixColor );
189 case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
191 Scripting::GetEnumerationProperty< Visual::FittingMode >(
192 value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode );
195 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
198 if( value.Get( radius ) )
200 mImpl->mCornerRadius = radius;
207 DoSetProperties( propertyMap );
210 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
212 mImpl->mControlSize = controlSize;
213 mImpl->mTransform.UpdatePropertyMap( transform );
215 #if defined(DEBUG_ENABLED)
216 std::ostringstream oss;
218 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::SetTransformAndSize(%s) - [\e[1;32mtransform: %s controlSize: (%3.1f, %3.1f)]\e[0m\n",
219 GetName().c_str(), oss.str().c_str(), controlSize.x, controlSize.y );
225 void Visual::Base::SetName( const std::string& name )
230 const std::string& Visual::Base::GetName() const
235 float Visual::Base::GetHeightForWidth( float width )
237 float aspectCorrectedHeight = 0.f;
239 GetNaturalSize( naturalSize );
240 if( naturalSize.width )
242 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
244 return aspectCorrectedHeight;
247 float Visual::Base::GetWidthForHeight( float height )
249 float aspectCorrectedWidth = 0.f;
251 GetNaturalSize( naturalSize );
252 if( naturalSize.height > 0.0f )
254 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
256 return aspectCorrectedWidth;
259 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
261 naturalSize = Vector2::ZERO;
264 void Visual::Base::DoAction( const Property::Index actionId, const Property::Value attributes )
266 OnDoAction( actionId, attributes );
269 void Visual::Base::SetDepthIndex( int index )
271 mImpl->mDepthIndex = index;
272 if( mImpl->mRenderer )
274 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
278 int Visual::Base::GetDepthIndex() const
280 return mImpl->mDepthIndex;
283 void Visual::Base::SetOnStage( Actor& actor )
287 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
288 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
289 DoSetOnStage( actor );
291 if( mImpl->mRenderer )
295 if( IsRoundedCornerRequired() )
297 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterProperty( CORNER_RADIUS, mImpl->mCornerRadius );
299 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
302 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
303 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
304 mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
309 void Visual::Base::SetOffStage( Actor& actor )
313 DoSetOffStage( actor );
314 mImpl->mMixColorIndex = Property::INVALID_INDEX;
315 mImpl->mCornerRadiusIndex = Property::INVALID_INDEX;
316 mImpl->mFlags &= ~Impl::IS_ON_STAGE;
320 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
322 DoCreatePropertyMap( map );
324 if( mImpl->mCustomShader )
326 mImpl->mCustomShader->CreatePropertyMap( map );
329 Property::Map transform;
330 mImpl->mTransform.GetPropertyMap( transform );
331 map.Insert( Toolkit::Visual::Property::TRANSFORM, transform );
333 bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
334 map.Insert( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
336 // Note, Color and Primitive will also insert their own mix color into the map
337 // which is ok, because they have a different key value range.
338 map.Insert( Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor ); // vec4
339 map.Insert( Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a );
341 auto fittingModeString = Scripting::GetLinearEnumerationName< FittingMode >(
342 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT );
343 map.Insert( Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString );
345 map.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius );
348 void Visual::Base::CreateInstancePropertyMap( Property::Map& map ) const
350 DoCreateInstancePropertyMap( map );
352 if( mImpl->mCustomShader )
354 mImpl->mCustomShader->CreatePropertyMap( map );
357 //map.Insert( Toolkit::Visual::Property::DEPTH_INDEX, mImpl->mDepthIndex );
358 //map.Insert( Toolkit::Visual::Property::ENABLED, (bool) mImpl->mRenderer );
362 void Visual::Base::EnablePreMultipliedAlpha( bool preMultiplied )
366 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
370 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
373 if( mImpl->mRenderer )
375 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
376 mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, static_cast<float>( preMultiplied ) );
380 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
382 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
385 void Visual::Base::DoSetOffStage( Actor& actor )
387 actor.RemoveRenderer( mImpl->mRenderer );
388 mImpl->mRenderer.Reset();
391 bool Visual::Base::IsOnStage() const
393 return mImpl->mFlags & Impl::IS_ON_STAGE;
396 bool Visual::Base::IsRoundedCornerRequired() const
398 return !EqualsZero( mImpl->mCornerRadius );
401 void Visual::Base::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
403 // May be overriden by derived class
406 void Visual::Base::RegisterMixColor()
408 // Only register if not already registered.
409 // (Color and Primitive visuals will register their own and save to this index)
410 if( mImpl->mMixColorIndex == Property::INVALID_INDEX )
412 mImpl->mMixColorIndex = DevelHandle::RegisterProperty(
414 Toolkit::Visual::Property::MIX_COLOR,
416 Vector3(mImpl->mMixColor) );
419 if( mImpl->mMixColor.a < 1.f )
421 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
424 mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, mImpl->mMixColor.a );
426 float preMultipliedAlpha = 0.0f;
427 if( IsPreMultipliedAlphaEnabled() )
429 preMultipliedAlpha = 1.0f;
431 mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, preMultipliedAlpha );
434 void Visual::Base::SetMixColor( const Vector4& color )
436 mImpl->mMixColor = color;
438 if( mImpl->mRenderer )
440 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, Vector3(color) );
441 mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, color.a );
444 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
449 void Visual::Base::SetMixColor( const Vector3& color )
451 mImpl->mMixColor.r = color.r;
452 mImpl->mMixColor.g = color.g;
453 mImpl->mMixColor.b = color.b;
455 if( mImpl->mRenderer )
457 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
461 const Vector4& Visual::Base::GetMixColor() const
463 return mImpl->mMixColor;
466 void Visual::Base::AddEventObserver( Visual::EventObserver& observer)
468 mImpl->mEventObserver = &observer;
471 void Visual::Base::RemoveEventObserver( Visual::EventObserver& observer )
473 mImpl->mEventObserver = NULL;
476 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
478 if( mImpl->mResourceStatus != resourceStatus )
480 mImpl->mResourceStatus = resourceStatus;
482 if( mImpl->mEventObserver )
484 // observer is currently a control impl
485 mImpl->mEventObserver->ResourceReady( *this );
490 bool Visual::Base::IsResourceReady() const
492 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY );
495 Toolkit::Visual::Type Visual::Base::GetType() const
500 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
502 return mImpl->mResourceStatus;
505 Visual::FittingMode Visual::Base::GetFittingMode() const
507 return mImpl->mFittingMode;
510 Visual::Base& Visual::Base::GetVisualObject()
515 Renderer Visual::Base::GetRenderer()
517 return mImpl->mRenderer;
520 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
522 Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
524 if( index == Property::INVALID_INDEX )
526 // Is it a shader property?
527 Shader shader = mImpl->mRenderer.GetShader();
528 index = DevelHandle::GetPropertyIndex( shader, key );
529 if( index != Property::INVALID_INDEX )
531 // Yes - we should register it in the Renderer so it can be set / animated
532 // independently, as shaders are shared across multiple renderers.
534 Property::Index keyIndex( Property::INVALID_KEY );
535 if( key.type == Property::Key::INDEX )
537 keyName = shader.GetPropertyName( index );
538 keyIndex = key.indexKey;
542 keyName = key.stringKey;
543 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
545 Property::Value value = shader.GetProperty( index );
546 index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
552 void Visual::Base::SetupTransition(
553 Dali::Animation& transition,
554 Internal::TransitionData::Animator& animator,
555 Property::Index index,
556 Property::Value& initialValue,
557 Property::Value& targetValue )
559 if( index != Property::INVALID_INDEX )
561 if( mImpl->mRenderer )
563 if( animator.animate == false )
565 mImpl->mRenderer.SetProperty( index, targetValue );
569 if( animator.initialValue.GetType() != Property::NONE )
571 mImpl->mRenderer.SetProperty( index, initialValue );
576 transition = Dali::Animation::New( 0.1f );
579 transition.AnimateTo( Property( mImpl->mRenderer, index ),
581 animator.alphaFunction,
582 TimePeriod( animator.timePeriodDelay,
583 animator.timePeriodDuration ) );
589 void Visual::Base::AnimateProperty(
590 Dali::Animation& transition,
591 Internal::TransitionData::Animator& animator )
593 #if defined(DEBUG_ENABLED)
595 std::ostringstream oss;
596 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
597 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
602 DoCreatePropertyMap( map );
603 Property::Value* valuePtr = map.Find( Toolkit::Visual::Property::TYPE );
607 valuePtr->Get( visualType );
610 if( animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
611 animator.propertyKey == MIX_COLOR ||
612 ( visualType == Toolkit::Visual::COLOR &&
613 animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
614 ( visualType == Toolkit::Visual::PRIMITIVE &&
615 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
617 AnimateMixColorProperty( transition, animator );
619 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
620 animator.propertyKey == OPACITY )
622 AnimateOpacityProperty( transition, animator );
624 else if( mImpl->mRenderer )
626 AnimateRendererProperty( transition, animator );
630 void Visual::Base::AnimateOpacityProperty(
631 Dali::Animation& transition,
632 Internal::TransitionData::Animator& animator )
634 bool isOpaque = mImpl->mMixColor.a >= 1.0f;
636 float initialOpacity;
637 if( animator.initialValue.Get( initialOpacity ) )
639 isOpaque = (initialOpacity >= 1.0f);
643 if( animator.targetValue.Get( targetOpacity ) )
645 mImpl->mMixColor.a = targetOpacity;
648 SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue );
649 SetupBlendMode( transition, isOpaque, animator.animate );
652 void Visual::Base::AnimateRendererProperty(
653 Dali::Animation& transition,
654 Internal::TransitionData::Animator& animator )
656 Property::Index index = GetPropertyIndex( animator.propertyKey );
657 if( index != Property::INVALID_INDEX )
659 if( animator.targetValue.GetType() != Property::NONE )
661 // Try writing target value into transform property map
662 // if it's not a valid key, then it won't alter mTransform
664 if( animator.propertyKey.type == Property::Key::INDEX )
666 map.Add( animator.propertyKey.indexKey, animator.targetValue );
670 map.Add( animator.propertyKey.stringKey, animator.targetValue );
673 mImpl->mTransform.UpdatePropertyMap( map );
676 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
680 void Visual::Base::AnimateMixColorProperty(
681 Dali::Animation& transition,
682 Internal::TransitionData::Animator& animator )
684 Property::Index index = mImpl->mMixColorIndex;
685 bool animateOpacity = false;
686 bool isOpaque = true;
688 Property::Value initialOpacity;
689 Property::Value targetOpacity;
690 Property::Value initialMixColor;
691 Property::Value targetMixColor;
693 if( index != Property::INVALID_INDEX )
695 Vector4 initialColor;
696 if( animator.initialValue.Get(initialColor) )
698 if( animator.initialValue.GetType() == Property::VECTOR4 )
700 // if there is an initial color specifying alpha, test it
701 isOpaque = initialColor.a >= 1.0f;
702 initialOpacity = initialColor.a;
704 initialMixColor = Vector3( initialColor );
707 // Set target value into data store
708 if( animator.targetValue.GetType() != Property::NONE )
711 animator.targetValue.Get(mixColor);
712 if( animator.targetValue.GetType() == Property::VECTOR4 )
714 mImpl->mMixColor.a = mixColor.a;
715 targetOpacity = mixColor.a;
716 animateOpacity = true;
719 mImpl->mMixColor.r = mixColor.r;
720 mImpl->mMixColor.g = mixColor.g;
721 mImpl->mMixColor.b = mixColor.b;
722 targetMixColor = Vector3(mixColor);
725 SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
728 SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity );
729 SetupBlendMode( transition, isOpaque, animator.animate );
734 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
736 // Ensure the blend mode is turned on if we are animating opacity, and
737 // turned off after the animation ends if the final value is opaque
738 if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
740 if( mImpl->mRenderer )
742 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
744 if( animating == true && mImpl->mMixColor.a >= 1.0f )
746 // When it becomes opaque, set the blend mode back to automatically
747 if( ! mImpl->mBlendSlotDelegate )
749 mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
751 transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
752 &Visual::Base::OnMixColorFinished );
758 void Visual::Base::OnMixColorFinished( Animation& animation )
760 if( mImpl->mRenderer )
762 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
763 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
764 ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
766 delete mImpl->mBlendSlotDelegate;
767 mImpl->mBlendSlotDelegate = NULL;
770 } // namespace Internal
772 } // namespace Toolkit