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 Renderer Visual::Base::GetRenderer()
406 return mImpl->mRenderer;
409 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
411 Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
413 if( index == Property::INVALID_INDEX )
415 // Is it a shader property?
416 Shader shader = mImpl->mRenderer.GetShader();
417 index = DevelHandle::GetPropertyIndex( shader, key );
418 if( index != Property::INVALID_INDEX )
420 // Yes - we should register it in the Renderer so it can be set / animated
421 // independently, as shaders are shared across multiple renderers.
423 Property::Index keyIndex( Property::INVALID_KEY );
424 if( key.type == Property::Key::INDEX )
426 keyName = shader.GetPropertyName( index );
427 keyIndex = key.indexKey;
431 keyName = key.stringKey;
432 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
434 Property::Value value = shader.GetProperty( index );
435 index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
441 void Visual::Base::SetupTransition(
442 Dali::Animation& transition,
443 Internal::TransitionData::Animator& animator,
444 Property::Index index,
445 Property::Value& initialValue,
446 Property::Value& targetValue )
448 if( index != Property::INVALID_INDEX )
450 if( mImpl->mRenderer )
452 if( animator.animate == false )
454 mImpl->mRenderer.SetProperty( index, targetValue );
458 if( animator.initialValue.GetType() != Property::NONE )
460 mImpl->mRenderer.SetProperty( index, initialValue );
465 transition = Dali::Animation::New( 0.1f );
468 transition.AnimateTo( Property( mImpl->mRenderer, index ),
470 animator.alphaFunction,
471 TimePeriod( animator.timePeriodDelay,
472 animator.timePeriodDuration ) );
478 void Visual::Base::AnimateProperty(
479 Dali::Animation& transition,
480 Internal::TransitionData::Animator& animator )
482 #if defined(DEBUG_ENABLED)
484 std::ostringstream oss;
485 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
486 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
491 DoCreatePropertyMap( map );
492 Property::Value* valuePtr = map.Find( Toolkit::DevelVisual::Property::TYPE );
494 valuePtr->Get(visualType);
496 if( animator.propertyKey == Toolkit::DevelVisual::Property::MIX_COLOR ||
497 animator.propertyKey == MIX_COLOR ||
498 ( visualType == Toolkit::Visual::COLOR &&
499 animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
500 ( visualType == Toolkit::Visual::PRIMITIVE &&
501 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
503 AnimateMixColorProperty( transition, animator );
505 else if(animator.propertyKey == Toolkit::DevelVisual::Property::OPACITY ||
506 animator.propertyKey == OPACITY )
508 AnimateOpacityProperty( transition, animator );
510 else if( mImpl->mRenderer )
512 AnimateRendererProperty(transition, animator);
516 void Visual::Base::AnimateOpacityProperty(
517 Dali::Animation& transition,
518 Internal::TransitionData::Animator& animator )
520 Property::Index index = mImpl->mOpacityIndex;
522 bool isOpaque = mImpl->mMixColor.a >= 1.0f;
524 if( index != Property::INVALID_INDEX )
526 float initialOpacity;
527 if( animator.initialValue.Get( initialOpacity ) )
529 isOpaque = (initialOpacity >= 1.0f);
533 if( animator.targetValue.Get( targetOpacity ) )
535 mImpl->mMixColor.a = targetOpacity;
538 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
539 SetupBlendMode( transition, isOpaque, animator.animate );
543 void Visual::Base::AnimateRendererProperty(
544 Dali::Animation& transition,
545 Internal::TransitionData::Animator& animator )
547 Property::Index index = GetPropertyIndex( animator.propertyKey );
548 if( index != Property::INVALID_INDEX )
550 if( animator.targetValue.GetType() != Property::NONE )
552 // Try writing target value into transform property map
553 // if it's not a valid key, then it won't alter mTransform
555 if( animator.propertyKey.type == Property::Key::INDEX )
557 map.Add( animator.propertyKey.indexKey, animator.targetValue );
561 map.Add( animator.propertyKey.stringKey, animator.targetValue );
564 mImpl->mTransform.UpdatePropertyMap( map );
567 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
571 void Visual::Base::AnimateMixColorProperty(
572 Dali::Animation& transition,
573 Internal::TransitionData::Animator& animator )
575 Property::Index index = mImpl->mMixColorIndex;
576 bool animateOpacity = false;
577 bool isOpaque = true;
579 Property::Value initialOpacity;
580 Property::Value targetOpacity;
581 Property::Value initialMixColor;
582 Property::Value targetMixColor;
584 if( index != Property::INVALID_INDEX )
586 Vector4 initialColor;
587 if( animator.initialValue.Get(initialColor) )
589 if( animator.initialValue.GetType() == Property::VECTOR4 )
591 // if there is an initial color specifying alpha, test it
592 isOpaque = initialColor.a >= 1.0f;
593 initialOpacity = initialColor.a;
595 initialMixColor = Vector3( initialColor );
598 // Set target value into data store
599 if( animator.targetValue.GetType() != Property::NONE )
602 animator.targetValue.Get(mixColor);
603 if( animator.targetValue.GetType() == Property::VECTOR4 )
605 mImpl->mMixColor.a = mixColor.a;
606 targetOpacity = mixColor.a;
607 animateOpacity = true;
610 mImpl->mMixColor.r = mixColor.r;
611 mImpl->mMixColor.g = mixColor.g;
612 mImpl->mMixColor.b = mixColor.b;
613 targetMixColor = Vector3(mixColor);
616 SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
619 SetupTransition( transition, animator, mImpl->mOpacityIndex, initialOpacity, targetOpacity );
620 SetupBlendMode( transition, isOpaque, animator.animate );
625 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
627 // Ensure the blend mode is turned on if we are animating opacity, and
628 // turned off after the animation ends if the final value is opaque
629 if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
631 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
633 if( animating == true && mImpl->mMixColor.a >= 1.0f )
635 // When it becomes opaque, set the blend mode back to automatically
636 if( ! mImpl->mBlendSlotDelegate )
638 mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
640 transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
641 &Visual::Base::OnMixColorFinished );
646 void Visual::Base::OnMixColorFinished( Animation& animation )
648 if( mImpl->mRenderer )
650 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
651 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
652 ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
654 delete mImpl->mBlendSlotDelegate;
655 mImpl->mBlendSlotDelegate = NULL;
658 } // namespace Internal
660 } // namespace Toolkit