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::EnablePreMultipliedAlpha( bool preMultipled )
294 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
298 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
301 if( mImpl->mRenderer )
303 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
307 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
309 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
312 void Visual::Base::DoSetOffStage( Actor& actor )
314 actor.RemoveRenderer( mImpl->mRenderer );
315 mImpl->mRenderer.Reset();
318 bool Visual::Base::IsOnStage() const
320 return mImpl->mFlags & Impl::IS_ON_STAGE;
323 bool Visual::Base::IsFromCache() const
325 return mImpl->mFlags & Impl::IS_FROM_CACHE;
328 void Visual::Base::RegisterMixColor()
330 // Only register if not already registered.
331 // (Color and Primitive visuals will register their own and save to this index)
332 if( mImpl->mMixColorIndex == Property::INVALID_INDEX )
334 mImpl->mMixColorIndex = DevelHandle::RegisterProperty(
336 Toolkit::DevelVisual::Property::MIX_COLOR,
338 Vector3(mImpl->mMixColor) );
341 if( mImpl->mMixColor.a < 1.f )
343 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
346 if( mImpl->mOpacityIndex == Property::INVALID_INDEX )
348 mImpl->mOpacityIndex = DevelHandle::RegisterProperty(
350 Toolkit::DevelVisual::Property::OPACITY,
352 mImpl->mMixColor.a );
355 float preMultipliedAlpha = 0.0f;
356 if( IsPreMultipliedAlphaEnabled() )
358 preMultipliedAlpha = 1.0f;
360 mImpl->mRenderer.RegisterProperty( "preMultipliedAlpha", preMultipliedAlpha );
363 void Visual::Base::SetMixColor( const Vector4& color )
365 mImpl->mMixColor = color;
367 if( mImpl->mRenderer )
369 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, Vector3(color) );
370 mImpl->mRenderer.SetProperty( mImpl->mOpacityIndex, color.a );
373 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
378 void Visual::Base::SetMixColor( const Vector3& color )
380 mImpl->mMixColor.r = color.r;
381 mImpl->mMixColor.g = color.g;
382 mImpl->mMixColor.b = color.b;
384 if( mImpl->mRenderer )
386 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
390 const Vector4& Visual::Base::GetMixColor() const
392 return mImpl->mMixColor;
395 Renderer Visual::Base::GetRenderer()
397 return mImpl->mRenderer;
400 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
402 Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
404 if( index == Property::INVALID_INDEX )
406 // Is it a shader property?
407 Shader shader = mImpl->mRenderer.GetShader();
408 index = DevelHandle::GetPropertyIndex( shader, key );
409 if( index != Property::INVALID_INDEX )
411 // Yes - we should register it in the Renderer so it can be set / animated
412 // independently, as shaders are shared across multiple renderers.
414 Property::Index keyIndex( Property::INVALID_KEY );
415 if( key.type == Property::Key::INDEX )
417 keyName = shader.GetPropertyName( index );
418 keyIndex = key.indexKey;
422 keyName = key.stringKey;
423 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
425 Property::Value value = shader.GetProperty( index );
426 index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
432 void Visual::Base::SetupTransition(
433 Dali::Animation& transition,
434 Internal::TransitionData::Animator& animator,
435 Property::Index index,
436 Property::Value& initialValue,
437 Property::Value& targetValue )
439 if( index != Property::INVALID_INDEX )
441 if( mImpl->mRenderer )
443 if( animator.animate == false )
445 mImpl->mRenderer.SetProperty( index, targetValue );
449 if( animator.initialValue.GetType() != Property::NONE )
451 mImpl->mRenderer.SetProperty( index, initialValue );
456 transition = Dali::Animation::New( 0.1f );
459 transition.AnimateTo( Property( mImpl->mRenderer, index ),
461 animator.alphaFunction,
462 TimePeriod( animator.timePeriodDelay,
463 animator.timePeriodDuration ) );
469 void Visual::Base::AnimateProperty(
470 Dali::Animation& transition,
471 Internal::TransitionData::Animator& animator )
473 #if defined(DEBUG_ENABLED)
475 std::ostringstream oss;
476 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
477 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
482 DoCreatePropertyMap( map );
483 Property::Value* valuePtr = map.Find( Toolkit::DevelVisual::Property::TYPE );
485 valuePtr->Get(visualType);
487 if( animator.propertyKey == Toolkit::DevelVisual::Property::MIX_COLOR ||
488 animator.propertyKey == MIX_COLOR ||
489 ( visualType == Toolkit::Visual::COLOR &&
490 animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
491 ( visualType == Toolkit::Visual::PRIMITIVE &&
492 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
494 AnimateMixColorProperty( transition, animator );
496 else if(animator.propertyKey == Toolkit::DevelVisual::Property::OPACITY ||
497 animator.propertyKey == OPACITY )
499 AnimateOpacityProperty( transition, animator );
501 else if( mImpl->mRenderer )
503 AnimateRendererProperty(transition, animator);
507 void Visual::Base::AnimateOpacityProperty(
508 Dali::Animation& transition,
509 Internal::TransitionData::Animator& animator )
511 Property::Index index = mImpl->mOpacityIndex;
513 bool isOpaque = mImpl->mMixColor.a >= 1.0f;
515 if( index != Property::INVALID_INDEX )
517 float initialOpacity;
518 if( animator.initialValue.Get( initialOpacity ) )
520 isOpaque = (initialOpacity >= 1.0f);
524 if( animator.targetValue.Get( targetOpacity ) )
526 mImpl->mMixColor.a = targetOpacity;
529 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
530 SetupBlendMode( transition, isOpaque, animator.animate );
534 void Visual::Base::AnimateRendererProperty(
535 Dali::Animation& transition,
536 Internal::TransitionData::Animator& animator )
538 Property::Index index = GetPropertyIndex( animator.propertyKey );
539 if( index != Property::INVALID_INDEX )
541 if( animator.targetValue.GetType() != Property::NONE )
543 // Try writing target value into transform property map
544 // if it's not a valid key, then it won't alter mTransform
546 if( animator.propertyKey.type == Property::Key::INDEX )
548 map.Add( animator.propertyKey.indexKey, animator.targetValue );
552 map.Add( animator.propertyKey.stringKey, animator.targetValue );
555 mImpl->mTransform.UpdatePropertyMap( map );
558 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
562 void Visual::Base::AnimateMixColorProperty(
563 Dali::Animation& transition,
564 Internal::TransitionData::Animator& animator )
566 Property::Index index = mImpl->mMixColorIndex;
567 bool animateOpacity = false;
568 bool isOpaque = true;
570 Property::Value initialOpacity;
571 Property::Value targetOpacity;
572 Property::Value initialMixColor;
573 Property::Value targetMixColor;
575 if( index != Property::INVALID_INDEX )
577 Vector4 initialColor;
578 if( animator.initialValue.Get(initialColor) )
580 if( animator.initialValue.GetType() == Property::VECTOR4 )
582 // if there is an initial color specifying alpha, test it
583 isOpaque = initialColor.a >= 1.0f;
584 initialOpacity = initialColor.a;
586 initialMixColor = Vector3( initialColor );
589 // Set target value into data store
590 if( animator.targetValue.GetType() != Property::NONE )
593 animator.targetValue.Get(mixColor);
594 if( animator.targetValue.GetType() == Property::VECTOR4 )
596 mImpl->mMixColor.a = mixColor.a;
597 targetOpacity = mixColor.a;
598 animateOpacity = true;
601 mImpl->mMixColor.r = mixColor.r;
602 mImpl->mMixColor.g = mixColor.g;
603 mImpl->mMixColor.b = mixColor.b;
604 targetMixColor = Vector3(mixColor);
607 SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
610 SetupTransition( transition, animator, mImpl->mOpacityIndex, initialOpacity, targetOpacity );
611 SetupBlendMode( transition, isOpaque, animator.animate );
616 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
618 // Ensure the blend mode is turned on if we are animating opacity, and
619 // turned off after the animation ends if the final value is opaque
620 if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
622 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
624 if( animating == true && mImpl->mMixColor.a >= 1.0f )
626 // When it becomes opaque, set the blend mode back to automatically
627 if( ! mImpl->mBlendSlotDelegate )
629 mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
631 transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
632 &Visual::Base::OnMixColorFinished );
637 void Visual::Base::OnMixColorFinished( Animation& animation )
639 if( mImpl->mRenderer )
641 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
642 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
643 ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
645 delete mImpl->mBlendSlotDelegate;
646 mImpl->mBlendSlotDelegate = NULL;
649 } // namespace Internal
651 } // namespace Toolkit