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 )
67 : mImpl( new Impl(fittingMode) ),
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 );
126 switch( matchKey.indexKey )
128 case Toolkit::Visual::Property::SHADER:
130 Property::Map shaderMap;
131 if( value.Get( shaderMap ) )
133 SetCustomShader( shaderMap );
138 case Toolkit::Visual::Property::TRANSFORM:
141 if( value.Get( map ) )
143 mImpl->mTransform.SetPropertyMap( map );
148 case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
150 bool premultipliedAlpha = false;
151 if( value.Get( premultipliedAlpha ) )
153 EnablePreMultipliedAlpha( premultipliedAlpha );
158 case Toolkit::Visual::Property::MIX_COLOR:
161 if( value.Get( mixColor ) )
163 if( value.GetType() == Property::VECTOR4 )
165 SetMixColor( mixColor );
169 Vector3 mixColor3(mixColor);
170 SetMixColor( mixColor3 );
175 case Toolkit::Visual::Property::OPACITY:
178 if( value.Get( opacity ) )
180 mImpl->mMixColor.a = opacity;
181 SetMixColor( mImpl->mMixColor );
185 case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
187 Scripting::GetEnumerationProperty< Visual::FittingMode >(
188 value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode );
194 DoSetProperties( propertyMap );
197 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
199 mImpl->mControlSize = controlSize;
200 mImpl->mTransform.UpdatePropertyMap( transform );
202 #if defined(DEBUG_ENABLED)
203 std::ostringstream oss;
205 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::SetTransformAndSize(%s) - [\e[1;32mtransform: %s controlSize: (%3.1f, %3.1f)]\e[0m\n",
206 GetName().c_str(), oss.str().c_str(), controlSize.x, controlSize.y );
212 void Visual::Base::SetName( const std::string& name )
217 const std::string& Visual::Base::GetName() const
222 float Visual::Base::GetHeightForWidth( float width )
224 float aspectCorrectedHeight = 0.f;
226 GetNaturalSize( naturalSize );
227 if( naturalSize.width )
229 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
231 return aspectCorrectedHeight;
234 float Visual::Base::GetWidthForHeight( float height )
236 float aspectCorrectedWidth = 0.f;
238 GetNaturalSize( naturalSize );
239 if( naturalSize.height > 0.0f )
241 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
243 return aspectCorrectedWidth;
246 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
248 naturalSize = Vector2::ZERO;
251 void Visual::Base::DoAction( const Property::Index actionId, const Property::Value attributes )
253 OnDoAction( actionId, attributes );
256 void Visual::Base::SetDepthIndex( int index )
258 mImpl->mDepthIndex = index;
259 if( mImpl->mRenderer )
261 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
265 int Visual::Base::GetDepthIndex() const
267 return mImpl->mDepthIndex;
270 void Visual::Base::SetOnStage( Actor& actor )
274 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
275 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
276 DoSetOnStage( actor );
278 if( mImpl->mRenderer )
282 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
283 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
284 mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
289 void Visual::Base::SetOffStage( Actor& actor )
293 DoSetOffStage( actor );
294 mImpl->mMixColorIndex = 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, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT );
322 map.Insert( Toolkit::DevelVisual::Property::VISUAL_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 mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, mImpl->mMixColor.a );
398 float preMultipliedAlpha = 0.0f;
399 if( IsPreMultipliedAlphaEnabled() )
401 preMultipliedAlpha = 1.0f;
403 mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, preMultipliedAlpha );
406 void Visual::Base::SetMixColor( const Vector4& color )
408 mImpl->mMixColor = color;
410 if( mImpl->mRenderer )
412 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, Vector3(color) );
413 mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, color.a );
416 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
421 void Visual::Base::SetMixColor( const Vector3& color )
423 mImpl->mMixColor.r = color.r;
424 mImpl->mMixColor.g = color.g;
425 mImpl->mMixColor.b = color.b;
427 if( mImpl->mRenderer )
429 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
433 const Vector4& Visual::Base::GetMixColor() const
435 return mImpl->mMixColor;
438 void Visual::Base::AddResourceObserver( Visual::ResourceObserver& observer)
440 mImpl->mResourceObserver = &observer;
443 void Visual::Base::RemoveResourceObserver( Visual::ResourceObserver& observer )
445 mImpl->mResourceObserver = NULL;
448 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
450 if( mImpl->mResourceStatus != resourceStatus )
452 mImpl->mResourceStatus = resourceStatus;
454 if( mImpl->mResourceObserver )
456 // observer is currently a control impl
457 mImpl->mResourceObserver->ResourceReady( *this );
462 bool Visual::Base::IsResourceReady() const
464 return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY );
467 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
469 return mImpl->mResourceStatus;
472 Visual::FittingMode Visual::Base::GetFittingMode() const
474 return mImpl->mFittingMode;
477 Visual::Base& Visual::Base::GetVisualObject()
482 Renderer Visual::Base::GetRenderer()
484 return mImpl->mRenderer;
487 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
489 Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
491 if( index == Property::INVALID_INDEX )
493 // Is it a shader property?
494 Shader shader = mImpl->mRenderer.GetShader();
495 index = DevelHandle::GetPropertyIndex( shader, key );
496 if( index != Property::INVALID_INDEX )
498 // Yes - we should register it in the Renderer so it can be set / animated
499 // independently, as shaders are shared across multiple renderers.
501 Property::Index keyIndex( Property::INVALID_KEY );
502 if( key.type == Property::Key::INDEX )
504 keyName = shader.GetPropertyName( index );
505 keyIndex = key.indexKey;
509 keyName = key.stringKey;
510 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
512 Property::Value value = shader.GetProperty( index );
513 index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
519 void Visual::Base::SetupTransition(
520 Dali::Animation& transition,
521 Internal::TransitionData::Animator& animator,
522 Property::Index index,
523 Property::Value& initialValue,
524 Property::Value& targetValue )
526 if( index != Property::INVALID_INDEX )
528 if( mImpl->mRenderer )
530 if( animator.animate == false )
532 mImpl->mRenderer.SetProperty( index, targetValue );
536 if( animator.initialValue.GetType() != Property::NONE )
538 mImpl->mRenderer.SetProperty( index, initialValue );
543 transition = Dali::Animation::New( 0.1f );
546 transition.AnimateTo( Property( mImpl->mRenderer, index ),
548 animator.alphaFunction,
549 TimePeriod( animator.timePeriodDelay,
550 animator.timePeriodDuration ) );
556 void Visual::Base::AnimateProperty(
557 Dali::Animation& transition,
558 Internal::TransitionData::Animator& animator )
560 #if defined(DEBUG_ENABLED)
562 std::ostringstream oss;
563 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
564 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
569 DoCreatePropertyMap( map );
570 Property::Value* valuePtr = map.Find( Toolkit::Visual::Property::TYPE );
574 valuePtr->Get( visualType );
577 if( animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
578 animator.propertyKey == MIX_COLOR ||
579 ( visualType == Toolkit::Visual::COLOR &&
580 animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
581 ( visualType == Toolkit::Visual::PRIMITIVE &&
582 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
584 AnimateMixColorProperty( transition, animator );
586 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
587 animator.propertyKey == OPACITY )
589 AnimateOpacityProperty( transition, animator );
591 else if( mImpl->mRenderer )
593 AnimateRendererProperty( transition, animator );
597 void Visual::Base::AnimateOpacityProperty(
598 Dali::Animation& transition,
599 Internal::TransitionData::Animator& animator )
601 bool isOpaque = mImpl->mMixColor.a >= 1.0f;
603 float initialOpacity;
604 if( animator.initialValue.Get( initialOpacity ) )
606 isOpaque = (initialOpacity >= 1.0f);
610 if( animator.targetValue.Get( targetOpacity ) )
612 mImpl->mMixColor.a = targetOpacity;
615 SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue );
616 SetupBlendMode( transition, isOpaque, animator.animate );
619 void Visual::Base::AnimateRendererProperty(
620 Dali::Animation& transition,
621 Internal::TransitionData::Animator& animator )
623 Property::Index index = GetPropertyIndex( animator.propertyKey );
624 if( index != Property::INVALID_INDEX )
626 if( animator.targetValue.GetType() != Property::NONE )
628 // Try writing target value into transform property map
629 // if it's not a valid key, then it won't alter mTransform
631 if( animator.propertyKey.type == Property::Key::INDEX )
633 map.Add( animator.propertyKey.indexKey, animator.targetValue );
637 map.Add( animator.propertyKey.stringKey, animator.targetValue );
640 mImpl->mTransform.UpdatePropertyMap( map );
643 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
647 void Visual::Base::AnimateMixColorProperty(
648 Dali::Animation& transition,
649 Internal::TransitionData::Animator& animator )
651 Property::Index index = mImpl->mMixColorIndex;
652 bool animateOpacity = false;
653 bool isOpaque = true;
655 Property::Value initialOpacity;
656 Property::Value targetOpacity;
657 Property::Value initialMixColor;
658 Property::Value targetMixColor;
660 if( index != Property::INVALID_INDEX )
662 Vector4 initialColor;
663 if( animator.initialValue.Get(initialColor) )
665 if( animator.initialValue.GetType() == Property::VECTOR4 )
667 // if there is an initial color specifying alpha, test it
668 isOpaque = initialColor.a >= 1.0f;
669 initialOpacity = initialColor.a;
671 initialMixColor = Vector3( initialColor );
674 // Set target value into data store
675 if( animator.targetValue.GetType() != Property::NONE )
678 animator.targetValue.Get(mixColor);
679 if( animator.targetValue.GetType() == Property::VECTOR4 )
681 mImpl->mMixColor.a = mixColor.a;
682 targetOpacity = mixColor.a;
683 animateOpacity = true;
686 mImpl->mMixColor.r = mixColor.r;
687 mImpl->mMixColor.g = mixColor.g;
688 mImpl->mMixColor.b = mixColor.b;
689 targetMixColor = Vector3(mixColor);
692 SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
695 SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity );
696 SetupBlendMode( transition, isOpaque, animator.animate );
701 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
703 // Ensure the blend mode is turned on if we are animating opacity, and
704 // turned off after the animation ends if the final value is opaque
705 if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
707 if( mImpl->mRenderer )
709 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
711 if( animating == true && mImpl->mMixColor.a >= 1.0f )
713 // When it becomes opaque, set the blend mode back to automatically
714 if( ! mImpl->mBlendSlotDelegate )
716 mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
718 transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
719 &Visual::Base::OnMixColorFinished );
725 void Visual::Base::OnMixColorFinished( Animation& animation )
727 if( mImpl->mRenderer )
729 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
730 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
731 ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
733 delete mImpl->mBlendSlotDelegate;
734 mImpl->mBlendSlotDelegate = NULL;
737 } // namespace Internal
739 } // namespace Toolkit