2 * Copyright (c) 2017 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/integration-api/debug.h>
27 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
28 #include <dali-toolkit/public-api/visuals/primitive-visual-properties.h>
29 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
30 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
31 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
35 #if defined(DEBUG_ENABLED)
36 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VISUAL_BASE" );
49 Visual::Base::Base( VisualFactoryCache& factoryCache )
50 : mImpl( new Impl() ),
51 mFactoryCache( factoryCache )
60 void Visual::Base::SetCustomShader( const Property::Map& shaderMap )
62 if( mImpl->mCustomShader )
64 mImpl->mCustomShader->SetPropertyMap( shaderMap );
68 mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
72 void Visual::Base::SetProperties( const Property::Map& propertyMap )
74 for( size_t i = 0; i < propertyMap.Count(); ++i )
76 const KeyValuePair& pair = propertyMap.GetKeyValue( i );
77 const Property::Key& key = pair.first;
78 const Property::Value& value = pair.second;
80 Property::Key matchKey = key;
81 if( matchKey.type == Property::Key::STRING )
83 if( matchKey == CUSTOM_SHADER )
85 matchKey = Property::Key( DevelVisual::Property::SHADER );
87 else if( matchKey == TRANSFORM )
89 matchKey = Property::Key( DevelVisual::Property::TRANSFORM );
91 else if( matchKey == PREMULTIPLIED_ALPHA )
93 matchKey = Property::Key( DevelVisual::Property::PREMULTIPLIED_ALPHA );
95 else if( matchKey == MIX_COLOR )
97 matchKey = Property::Key( DevelVisual::Property::MIX_COLOR );
99 else if( matchKey == OPACITY )
101 matchKey = Property::Key( DevelVisual::Property::OPACITY );
105 switch( matchKey.indexKey )
107 case DevelVisual::Property::SHADER:
109 Property::Map shaderMap;
110 if( value.Get( shaderMap ) )
112 SetCustomShader( shaderMap );
117 case DevelVisual::Property::TRANSFORM:
120 if( value.Get( map ) )
122 mImpl->mTransform.SetPropertyMap( map );
127 case DevelVisual::Property::PREMULTIPLIED_ALPHA:
129 bool premultipliedAlpha = false;
130 if( value.Get( premultipliedAlpha ) )
132 EnablePreMultipliedAlpha( premultipliedAlpha );
137 case DevelVisual::Property::MIX_COLOR:
140 if( value.Get( mixColor ) )
142 if( value.GetType() == Property::VECTOR4 )
144 SetMixColor( mixColor );
148 Vector3 mixColor3(mixColor);
149 SetMixColor( mixColor3 );
154 case DevelVisual::Property::OPACITY:
157 if( value.Get( opacity ) )
159 mImpl->mMixColor.a = opacity;
160 SetMixColor( mImpl->mMixColor );
167 DoSetProperties( propertyMap );
170 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
172 mImpl->mControlSize = controlSize;
173 mImpl->mTransform.UpdatePropertyMap( transform );
175 #if defined(DEBUG_ENABLED)
176 std::ostringstream oss;
178 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::SetTransformAndSize(%s) - [\e[1;32mtransform: %s controlSize: (%3.1f, %3.1f)]\e[0m\n",
179 GetName().c_str(), oss.str().c_str(), controlSize.x, controlSize.y );
185 void Visual::Base::SetName( const std::string& name )
190 const std::string& Visual::Base::GetName()
195 float Visual::Base::GetHeightForWidth( float width )
197 float aspectCorrectedHeight = 0.f;
199 GetNaturalSize( naturalSize );
200 if( naturalSize.width )
202 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
204 return aspectCorrectedHeight;
207 float Visual::Base::GetWidthForHeight( float height )
209 float aspectCorrectedWidth = 0.f;
211 GetNaturalSize( naturalSize );
212 if( naturalSize.height > 0.0f )
214 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
216 return aspectCorrectedWidth;
219 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
221 naturalSize = Vector2::ZERO;
224 void Visual::Base::SetDepthIndex( float index )
226 mImpl->mDepthIndex = index;
227 if( mImpl->mRenderer )
229 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
233 float Visual::Base::GetDepthIndex() const
235 return mImpl->mDepthIndex;
238 void Visual::Base::SetOnStage( Actor& actor )
242 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
243 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
244 DoSetOnStage( actor );
246 if( mImpl->mRenderer )
250 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
251 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
252 mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
257 void Visual::Base::SetOffStage( Actor& actor )
261 DoSetOffStage( actor );
262 mImpl->mMixColorIndex = Property::INVALID_INDEX;
263 mImpl->mOpacityIndex = Property::INVALID_INDEX;
264 mImpl->mFlags &= ~Impl::IS_ON_STAGE;
268 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
270 DoCreatePropertyMap( map );
272 if( mImpl->mCustomShader )
274 mImpl->mCustomShader->CreatePropertyMap( map );
277 Property::Map transform;
278 mImpl->mTransform.GetPropertyMap( transform );
279 map.Insert( DevelVisual::Property::TRANSFORM, transform );
281 bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
282 map.Insert( DevelVisual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
284 // Note, Color and Primitive will also insert their own mix color into the map
285 // which is ok, because they have a different key value range.
286 map.Insert( DevelVisual::Property::MIX_COLOR, mImpl->mMixColor ); // vec4
287 map.Insert( DevelVisual::Property::OPACITY, mImpl->mMixColor.a );
290 void Visual::Base::CreateInstancePropertyMap( Property::Map& map ) const
292 DoCreateInstancePropertyMap( map );
294 if( mImpl->mCustomShader )
296 mImpl->mCustomShader->CreatePropertyMap( map );
299 //map.Insert( DevelVisual::Property::DEPTH_INDEX, mImpl->mDepthIndex );
300 //map.Insert( DevelVisual::Property::ENABLED, (bool) mImpl->mRenderer );
304 void Visual::Base::EnablePreMultipliedAlpha( bool preMultipled )
308 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
312 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
315 if( mImpl->mRenderer )
317 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
321 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
323 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
326 void Visual::Base::DoSetOffStage( Actor& actor )
328 actor.RemoveRenderer( mImpl->mRenderer );
329 mImpl->mRenderer.Reset();
332 bool Visual::Base::IsOnStage() const
334 return mImpl->mFlags & Impl::IS_ON_STAGE;
337 void Visual::Base::RegisterMixColor()
339 // Only register if not already registered.
340 // (Color and Primitive visuals will register their own and save to this index)
341 if( mImpl->mMixColorIndex == Property::INVALID_INDEX )
343 mImpl->mMixColorIndex = DevelHandle::RegisterProperty(
345 Toolkit::DevelVisual::Property::MIX_COLOR,
347 Vector3(mImpl->mMixColor) );
350 if( mImpl->mMixColor.a < 1.f )
352 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
355 if( mImpl->mOpacityIndex == Property::INVALID_INDEX )
357 mImpl->mOpacityIndex = DevelHandle::RegisterProperty(
359 Toolkit::DevelVisual::Property::OPACITY,
361 mImpl->mMixColor.a );
364 float preMultipliedAlpha = 0.0f;
365 if( IsPreMultipliedAlphaEnabled() )
367 preMultipliedAlpha = 1.0f;
369 mImpl->mRenderer.RegisterProperty( "preMultipliedAlpha", preMultipliedAlpha );
372 void Visual::Base::SetMixColor( const Vector4& color )
374 mImpl->mMixColor = color;
376 if( mImpl->mRenderer )
378 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, Vector3(color) );
379 mImpl->mRenderer.SetProperty( mImpl->mOpacityIndex, color.a );
382 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
387 void Visual::Base::SetMixColor( const Vector3& color )
389 mImpl->mMixColor.r = color.r;
390 mImpl->mMixColor.g = color.g;
391 mImpl->mMixColor.b = color.b;
393 if( mImpl->mRenderer )
395 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
399 const Vector4& Visual::Base::GetMixColor() const
401 return mImpl->mMixColor;
404 void Visual::Base::AddResourceObserver( Visual::ResourceObserver& observer)
406 mImpl->mResourceObserver = &observer;
409 void Visual::Base::RemoveResourceObserver( Visual::ResourceObserver& observer )
411 mImpl->mResourceObserver = NULL;
414 void Visual::Base::ResourceReady()
416 if( mImpl->mResourceReady )
418 // only inform the observer the first time the resource is ready
421 mImpl->mResourceReady = true;
423 if( mImpl->mResourceObserver )
425 // observer is currently a control impl
426 mImpl->mResourceObserver->ResourceReady( *this );
430 bool Visual::Base::IsResourceReady() const
432 return mImpl->mResourceReady;
435 Renderer Visual::Base::GetRenderer()
437 return mImpl->mRenderer;
440 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
442 Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
444 if( index == Property::INVALID_INDEX )
446 // Is it a shader property?
447 Shader shader = mImpl->mRenderer.GetShader();
448 index = DevelHandle::GetPropertyIndex( shader, key );
449 if( index != Property::INVALID_INDEX )
451 // Yes - we should register it in the Renderer so it can be set / animated
452 // independently, as shaders are shared across multiple renderers.
454 Property::Index keyIndex( Property::INVALID_KEY );
455 if( key.type == Property::Key::INDEX )
457 keyName = shader.GetPropertyName( index );
458 keyIndex = key.indexKey;
462 keyName = key.stringKey;
463 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
465 Property::Value value = shader.GetProperty( index );
466 index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
472 void Visual::Base::SetupTransition(
473 Dali::Animation& transition,
474 Internal::TransitionData::Animator& animator,
475 Property::Index index,
476 Property::Value& initialValue,
477 Property::Value& targetValue )
479 if( index != Property::INVALID_INDEX )
481 if( mImpl->mRenderer )
483 if( animator.animate == false )
485 mImpl->mRenderer.SetProperty( index, targetValue );
489 if( animator.initialValue.GetType() != Property::NONE )
491 mImpl->mRenderer.SetProperty( index, initialValue );
496 transition = Dali::Animation::New( 0.1f );
499 transition.AnimateTo( Property( mImpl->mRenderer, index ),
501 animator.alphaFunction,
502 TimePeriod( animator.timePeriodDelay,
503 animator.timePeriodDuration ) );
509 void Visual::Base::AnimateProperty(
510 Dali::Animation& transition,
511 Internal::TransitionData::Animator& animator )
513 #if defined(DEBUG_ENABLED)
515 std::ostringstream oss;
516 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
517 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
522 DoCreatePropertyMap( map );
523 Property::Value* valuePtr = map.Find( Toolkit::DevelVisual::Property::TYPE );
527 valuePtr->Get( visualType );
530 if( animator.propertyKey == Toolkit::DevelVisual::Property::MIX_COLOR ||
531 animator.propertyKey == MIX_COLOR ||
532 ( visualType == Toolkit::Visual::COLOR &&
533 animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
534 ( visualType == Toolkit::Visual::PRIMITIVE &&
535 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
537 AnimateMixColorProperty( transition, animator );
539 else if(animator.propertyKey == Toolkit::DevelVisual::Property::OPACITY ||
540 animator.propertyKey == OPACITY )
542 AnimateOpacityProperty( transition, animator );
544 else if( mImpl->mRenderer )
546 AnimateRendererProperty( transition, animator );
550 void Visual::Base::AnimateOpacityProperty(
551 Dali::Animation& transition,
552 Internal::TransitionData::Animator& animator )
554 Property::Index index = mImpl->mOpacityIndex;
556 bool isOpaque = mImpl->mMixColor.a >= 1.0f;
558 if( index != Property::INVALID_INDEX )
560 float initialOpacity;
561 if( animator.initialValue.Get( initialOpacity ) )
563 isOpaque = (initialOpacity >= 1.0f);
567 if( animator.targetValue.Get( targetOpacity ) )
569 mImpl->mMixColor.a = targetOpacity;
572 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
573 SetupBlendMode( transition, isOpaque, animator.animate );
577 void Visual::Base::AnimateRendererProperty(
578 Dali::Animation& transition,
579 Internal::TransitionData::Animator& animator )
581 Property::Index index = GetPropertyIndex( animator.propertyKey );
582 if( index != Property::INVALID_INDEX )
584 if( animator.targetValue.GetType() != Property::NONE )
586 // Try writing target value into transform property map
587 // if it's not a valid key, then it won't alter mTransform
589 if( animator.propertyKey.type == Property::Key::INDEX )
591 map.Add( animator.propertyKey.indexKey, animator.targetValue );
595 map.Add( animator.propertyKey.stringKey, animator.targetValue );
598 mImpl->mTransform.UpdatePropertyMap( map );
601 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
605 void Visual::Base::AnimateMixColorProperty(
606 Dali::Animation& transition,
607 Internal::TransitionData::Animator& animator )
609 Property::Index index = mImpl->mMixColorIndex;
610 bool animateOpacity = false;
611 bool isOpaque = true;
613 Property::Value initialOpacity;
614 Property::Value targetOpacity;
615 Property::Value initialMixColor;
616 Property::Value targetMixColor;
618 if( index != Property::INVALID_INDEX )
620 Vector4 initialColor;
621 if( animator.initialValue.Get(initialColor) )
623 if( animator.initialValue.GetType() == Property::VECTOR4 )
625 // if there is an initial color specifying alpha, test it
626 isOpaque = initialColor.a >= 1.0f;
627 initialOpacity = initialColor.a;
629 initialMixColor = Vector3( initialColor );
632 // Set target value into data store
633 if( animator.targetValue.GetType() != Property::NONE )
636 animator.targetValue.Get(mixColor);
637 if( animator.targetValue.GetType() == Property::VECTOR4 )
639 mImpl->mMixColor.a = mixColor.a;
640 targetOpacity = mixColor.a;
641 animateOpacity = true;
644 mImpl->mMixColor.r = mixColor.r;
645 mImpl->mMixColor.g = mixColor.g;
646 mImpl->mMixColor.b = mixColor.b;
647 targetMixColor = Vector3(mixColor);
650 SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
653 SetupTransition( transition, animator, mImpl->mOpacityIndex, initialOpacity, targetOpacity );
654 SetupBlendMode( transition, isOpaque, animator.animate );
659 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
661 // Ensure the blend mode is turned on if we are animating opacity, and
662 // turned off after the animation ends if the final value is opaque
663 if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
665 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
667 if( animating == true && mImpl->mMixColor.a >= 1.0f )
669 // When it becomes opaque, set the blend mode back to automatically
670 if( ! mImpl->mBlendSlotDelegate )
672 mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
674 transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
675 &Visual::Base::OnMixColorFinished );
680 void Visual::Base::OnMixColorFinished( Animation& animation )
682 if( mImpl->mRenderer )
684 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
685 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
686 ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
688 delete mImpl->mBlendSlotDelegate;
689 mImpl->mBlendSlotDelegate = NULL;
692 } // namespace Internal
694 } // namespace Toolkit