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 bool Visual::Base::IsFromCache() const
339 return mImpl->mFlags & Impl::IS_FROM_CACHE;
342 void Visual::Base::RegisterMixColor()
344 // Only register if not already registered.
345 // (Color and Primitive visuals will register their own and save to this index)
346 if( mImpl->mMixColorIndex == Property::INVALID_INDEX )
348 mImpl->mMixColorIndex = DevelHandle::RegisterProperty(
350 Toolkit::DevelVisual::Property::MIX_COLOR,
352 Vector3(mImpl->mMixColor) );
355 if( mImpl->mMixColor.a < 1.f )
357 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
360 if( mImpl->mOpacityIndex == Property::INVALID_INDEX )
362 mImpl->mOpacityIndex = DevelHandle::RegisterProperty(
364 Toolkit::DevelVisual::Property::OPACITY,
366 mImpl->mMixColor.a );
369 float preMultipliedAlpha = 0.0f;
370 if( IsPreMultipliedAlphaEnabled() )
372 preMultipliedAlpha = 1.0f;
374 mImpl->mRenderer.RegisterProperty( "preMultipliedAlpha", preMultipliedAlpha );
377 void Visual::Base::SetMixColor( const Vector4& color )
379 mImpl->mMixColor = color;
381 if( mImpl->mRenderer )
383 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, Vector3(color) );
384 mImpl->mRenderer.SetProperty( mImpl->mOpacityIndex, color.a );
387 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
392 void Visual::Base::SetMixColor( const Vector3& color )
394 mImpl->mMixColor.r = color.r;
395 mImpl->mMixColor.g = color.g;
396 mImpl->mMixColor.b = color.b;
398 if( mImpl->mRenderer )
400 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
404 const Vector4& Visual::Base::GetMixColor() const
406 return mImpl->mMixColor;
409 Renderer Visual::Base::GetRenderer()
411 return mImpl->mRenderer;
414 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
416 Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
418 if( index == Property::INVALID_INDEX )
420 // Is it a shader property?
421 Shader shader = mImpl->mRenderer.GetShader();
422 index = DevelHandle::GetPropertyIndex( shader, key );
423 if( index != Property::INVALID_INDEX )
425 // Yes - we should register it in the Renderer so it can be set / animated
426 // independently, as shaders are shared across multiple renderers.
428 Property::Index keyIndex( Property::INVALID_KEY );
429 if( key.type == Property::Key::INDEX )
431 keyName = shader.GetPropertyName( index );
432 keyIndex = key.indexKey;
436 keyName = key.stringKey;
437 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
439 Property::Value value = shader.GetProperty( index );
440 index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
446 void Visual::Base::SetupTransition(
447 Dali::Animation& transition,
448 Internal::TransitionData::Animator& animator,
449 Property::Index index,
450 Property::Value& initialValue,
451 Property::Value& targetValue )
453 if( index != Property::INVALID_INDEX )
455 if( mImpl->mRenderer )
457 if( animator.animate == false )
459 mImpl->mRenderer.SetProperty( index, targetValue );
463 if( animator.initialValue.GetType() != Property::NONE )
465 mImpl->mRenderer.SetProperty( index, initialValue );
470 transition = Dali::Animation::New( 0.1f );
473 transition.AnimateTo( Property( mImpl->mRenderer, index ),
475 animator.alphaFunction,
476 TimePeriod( animator.timePeriodDelay,
477 animator.timePeriodDuration ) );
483 void Visual::Base::AnimateProperty(
484 Dali::Animation& transition,
485 Internal::TransitionData::Animator& animator )
487 #if defined(DEBUG_ENABLED)
489 std::ostringstream oss;
490 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
491 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
496 DoCreatePropertyMap( map );
497 Property::Value* valuePtr = map.Find( Toolkit::DevelVisual::Property::TYPE );
499 valuePtr->Get(visualType);
501 if( animator.propertyKey == Toolkit::DevelVisual::Property::MIX_COLOR ||
502 animator.propertyKey == MIX_COLOR ||
503 ( visualType == Toolkit::Visual::COLOR &&
504 animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
505 ( visualType == Toolkit::Visual::PRIMITIVE &&
506 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
508 AnimateMixColorProperty( transition, animator );
510 else if(animator.propertyKey == Toolkit::DevelVisual::Property::OPACITY ||
511 animator.propertyKey == OPACITY )
513 AnimateOpacityProperty( transition, animator );
515 else if( mImpl->mRenderer )
517 AnimateRendererProperty(transition, animator);
521 void Visual::Base::AnimateOpacityProperty(
522 Dali::Animation& transition,
523 Internal::TransitionData::Animator& animator )
525 Property::Index index = mImpl->mOpacityIndex;
527 bool isOpaque = mImpl->mMixColor.a >= 1.0f;
529 if( index != Property::INVALID_INDEX )
531 float initialOpacity;
532 if( animator.initialValue.Get( initialOpacity ) )
534 isOpaque = (initialOpacity >= 1.0f);
538 if( animator.targetValue.Get( targetOpacity ) )
540 mImpl->mMixColor.a = targetOpacity;
543 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
544 SetupBlendMode( transition, isOpaque, animator.animate );
548 void Visual::Base::AnimateRendererProperty(
549 Dali::Animation& transition,
550 Internal::TransitionData::Animator& animator )
552 Property::Index index = GetPropertyIndex( animator.propertyKey );
553 if( index != Property::INVALID_INDEX )
555 if( animator.targetValue.GetType() != Property::NONE )
557 // Try writing target value into transform property map
558 // if it's not a valid key, then it won't alter mTransform
560 if( animator.propertyKey.type == Property::Key::INDEX )
562 map.Add( animator.propertyKey.indexKey, animator.targetValue );
566 map.Add( animator.propertyKey.stringKey, animator.targetValue );
569 mImpl->mTransform.UpdatePropertyMap( map );
572 SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
576 void Visual::Base::AnimateMixColorProperty(
577 Dali::Animation& transition,
578 Internal::TransitionData::Animator& animator )
580 Property::Index index = mImpl->mMixColorIndex;
581 bool animateOpacity = false;
582 bool isOpaque = true;
584 Property::Value initialOpacity;
585 Property::Value targetOpacity;
586 Property::Value initialMixColor;
587 Property::Value targetMixColor;
589 if( index != Property::INVALID_INDEX )
591 Vector4 initialColor;
592 if( animator.initialValue.Get(initialColor) )
594 if( animator.initialValue.GetType() == Property::VECTOR4 )
596 // if there is an initial color specifying alpha, test it
597 isOpaque = initialColor.a >= 1.0f;
598 initialOpacity = initialColor.a;
600 initialMixColor = Vector3( initialColor );
603 // Set target value into data store
604 if( animator.targetValue.GetType() != Property::NONE )
607 animator.targetValue.Get(mixColor);
608 if( animator.targetValue.GetType() == Property::VECTOR4 )
610 mImpl->mMixColor.a = mixColor.a;
611 targetOpacity = mixColor.a;
612 animateOpacity = true;
615 mImpl->mMixColor.r = mixColor.r;
616 mImpl->mMixColor.g = mixColor.g;
617 mImpl->mMixColor.b = mixColor.b;
618 targetMixColor = Vector3(mixColor);
621 SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
624 SetupTransition( transition, animator, mImpl->mOpacityIndex, initialOpacity, targetOpacity );
625 SetupBlendMode( transition, isOpaque, animator.animate );
630 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
632 // Ensure the blend mode is turned on if we are animating opacity, and
633 // turned off after the animation ends if the final value is opaque
634 if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
636 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
638 if( animating == true && mImpl->mMixColor.a >= 1.0f )
640 // When it becomes opaque, set the blend mode back to automatically
641 if( ! mImpl->mBlendSlotDelegate )
643 mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
645 transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
646 &Visual::Base::OnMixColorFinished );
651 void Visual::Base::OnMixColorFinished( Animation& animation )
653 if( mImpl->mRenderer )
655 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
656 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
657 ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
659 delete mImpl->mBlendSlotDelegate;
660 mImpl->mBlendSlotDelegate = NULL;
663 } // namespace Internal
665 } // namespace Toolkit