[dali_2.3.19] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-base-impl.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "visual-base-impl.h"
20
21 // EXTERNAL HEADER
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>
27
28 //INTERNAL HEARDER
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>
36
37 namespace
38 {
39 #if defined(DEBUG_ENABLED)
40 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VISUAL_BASE" );
41 #endif
42
43 const char * const PRE_MULTIPLIED_ALPHA_PROPERTY( "preMultipliedAlpha" );
44
45 } // namespace
46
47 namespace Dali
48 {
49
50 namespace Toolkit
51 {
52
53 namespace Internal
54 {
55
56 namespace
57 {
58
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_WITH_SCOPE( Visual::FittingMode, OVER_FIT_KEEP_ASPECT_RATIO )
63 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, CENTER )
64 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, FIT_WIDTH )
65 DALI_ENUM_TO_STRING_WITH_SCOPE( Visual::FittingMode, FIT_HEIGHT )
66 DALI_ENUM_TO_STRING_TABLE_END( VISUAL_FITTING_MODE )
67
68 } // namespace
69
70 Visual::Base::Base( VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type )
71 : mImpl( new Impl( fittingMode, type ) ),
72   mFactoryCache( factoryCache )
73 {
74 }
75
76 Visual::Base::~Base()
77 {
78   delete mImpl;
79 }
80
81 void Visual::Base::SetCustomShader( const Property::Map& shaderMap )
82 {
83   if( mImpl->mCustomShader )
84   {
85     mImpl->mCustomShader->SetPropertyMap( shaderMap );
86   }
87   else
88   {
89     mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
90   }
91 }
92
93 void Visual::Base::SetProperties( const Property::Map& propertyMap )
94 {
95   for( size_t i = 0; i < propertyMap.Count(); ++i )
96   {
97     const KeyValuePair& pair = propertyMap.GetKeyValue( i );
98     const Property::Key& key = pair.first;
99     const Property::Value& value = pair.second;
100
101     Property::Key matchKey = key;
102     if( matchKey.type == Property::Key::STRING )
103     {
104       if( matchKey == CUSTOM_SHADER )
105       {
106         matchKey = Property::Key( Toolkit::Visual::Property::SHADER );
107       }
108       else if( matchKey == TRANSFORM )
109       {
110         matchKey = Property::Key( Toolkit::Visual::Property::TRANSFORM );
111       }
112       else if( matchKey == PREMULTIPLIED_ALPHA )
113       {
114         matchKey = Property::Key( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA );
115       }
116       else if( matchKey == MIX_COLOR )
117       {
118         matchKey = Property::Key( Toolkit::Visual::Property::MIX_COLOR );
119       }
120       else if( matchKey == OPACITY )
121       {
122         matchKey = Property::Key( Toolkit::Visual::Property::OPACITY );
123       }
124       else if( matchKey == VISUAL_FITTING_MODE )
125       {
126         matchKey = Property::Key( Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE );
127       }
128       else if( matchKey == CORNER_RADIUS )
129       {
130         matchKey = Property::Key( Toolkit::DevelVisual::Property::CORNER_RADIUS );
131       }
132     }
133
134     switch( matchKey.indexKey )
135     {
136       case Toolkit::Visual::Property::SHADER:
137       {
138         Property::Map shaderMap;
139         if( value.Get( shaderMap ) )
140         {
141           SetCustomShader( shaderMap );
142         }
143         break;
144       }
145
146       case Toolkit::Visual::Property::TRANSFORM:
147       {
148         Property::Map map;
149         if( value.Get( map ) )
150         {
151           mImpl->mTransform.SetPropertyMap( map );
152         }
153         break;
154       }
155
156       case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
157       {
158         bool premultipliedAlpha = false;
159         if( value.Get( premultipliedAlpha ) )
160         {
161           EnablePreMultipliedAlpha( premultipliedAlpha );
162         }
163         break;
164       }
165
166       case Toolkit::Visual::Property::MIX_COLOR:
167       {
168         Vector4 mixColor;
169         if( value.Get( mixColor ) )
170         {
171           if( value.GetType() == Property::VECTOR4 )
172           {
173             SetMixColor( mixColor );
174           }
175           else
176           {
177             Vector3 mixColor3(mixColor);
178             SetMixColor( mixColor3 );
179           }
180         }
181         break;
182       }
183       case Toolkit::Visual::Property::OPACITY:
184       {
185         float opacity;
186         if( value.Get( opacity ) )
187         {
188           mImpl->mMixColor.a = opacity;
189           SetMixColor( mImpl->mMixColor );
190         }
191         break;
192       }
193       case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
194       {
195         Scripting::GetEnumerationProperty< Visual::FittingMode >(
196           value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode );
197         break;
198       }
199       case Toolkit::DevelVisual::Property::CORNER_RADIUS:
200       {
201         float radius;
202         if( value.Get( radius ) )
203         {
204           mImpl->mCornerRadius = radius;
205         }
206         break;
207       }
208     }
209   }
210
211   DoSetProperties( propertyMap );
212 }
213
214 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
215 {
216   mImpl->mControlSize = controlSize;
217   mImpl->mTransform.UpdatePropertyMap( transform );
218
219 #if defined(DEBUG_ENABLED)
220   std::ostringstream oss;
221   oss << transform;
222   DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::SetTransformAndSize(%s) - [\e[1;32mtransform: %s  controlSize: (%3.1f, %3.1f)]\e[0m\n",
223                  GetName().c_str(), oss.str().c_str(), controlSize.x, controlSize.y );
224 #endif
225
226   OnSetTransform();
227 }
228
229 void Visual::Base::SetName( const std::string& name )
230 {
231   mImpl->mName = name;
232 }
233
234 const std::string& Visual::Base::GetName() const
235 {
236   return mImpl->mName;
237 }
238
239 float Visual::Base::GetHeightForWidth( float width )
240 {
241   float aspectCorrectedHeight = 0.f;
242   Vector2 naturalSize;
243   GetNaturalSize( naturalSize );
244   if( naturalSize.width )
245   {
246     aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
247   }
248   return aspectCorrectedHeight;
249 }
250
251 float Visual::Base::GetWidthForHeight( float height )
252 {
253   float aspectCorrectedWidth = 0.f;
254   Vector2 naturalSize;
255   GetNaturalSize( naturalSize );
256   if( naturalSize.height > 0.0f )
257   {
258     aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
259   }
260   return aspectCorrectedWidth;
261 }
262
263 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
264 {
265   naturalSize = Vector2::ZERO;
266 }
267
268 void Visual::Base::DoAction( const Property::Index actionId, const Property::Value attributes )
269 {
270   OnDoAction( actionId, attributes );
271 }
272
273 void Visual::Base::SetDepthIndex( int index )
274 {
275   mImpl->mDepthIndex = index;
276   if( mImpl->mRenderer )
277   {
278     mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
279   }
280 }
281
282 int Visual::Base::GetDepthIndex() const
283 {
284   return mImpl->mDepthIndex;
285 }
286
287 void Visual::Base::SetOnStage( Actor& actor )
288 {
289   if( !IsOnStage() )
290   {
291     // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
292     // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
293     DoSetOnStage( actor );
294
295     if( mImpl->mRenderer )
296     {
297       RegisterMixColor();
298
299       if( IsRoundedCornerRequired() )
300       {
301         mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterProperty( CORNER_RADIUS, mImpl->mCornerRadius );
302
303         mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
304       }
305
306       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
307       mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
308       mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
309     }
310   }
311 }
312
313 void Visual::Base::SetOffStage( Actor& actor )
314 {
315   if( IsOnStage() )
316   {
317     DoSetOffStage( actor );
318     mImpl->mMixColorIndex = Property::INVALID_INDEX;
319     mImpl->mCornerRadiusIndex = Property::INVALID_INDEX;
320     mImpl->mFlags &= ~Impl::IS_ON_STAGE;
321   }
322 }
323
324 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
325 {
326   DoCreatePropertyMap( map );
327
328   if( mImpl->mCustomShader )
329   {
330     mImpl->mCustomShader->CreatePropertyMap( map );
331   }
332
333   Property::Map transform;
334   mImpl->mTransform.GetPropertyMap( transform );
335   map.Insert( Toolkit::Visual::Property::TRANSFORM, transform );
336
337   bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
338   map.Insert( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
339
340   // Note, Color and Primitive will also insert their own mix color into the map
341   // which is ok, because they have a different key value range.
342   map.Insert( Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor ); // vec4
343   map.Insert( Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a );
344   map.Insert( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, IsSynchronousLoadingRequired() );
345
346   auto fittingModeString = Scripting::GetLinearEnumerationName< FittingMode >(
347     mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT );
348   map.Insert( Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString );
349
350   map.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius );
351 }
352
353 void Visual::Base::CreateInstancePropertyMap( Property::Map& map ) const
354 {
355   DoCreateInstancePropertyMap( map );
356
357   if( mImpl->mCustomShader )
358   {
359     mImpl->mCustomShader->CreatePropertyMap( map );
360   }
361
362   //map.Insert( Toolkit::Visual::Property::DEPTH_INDEX, mImpl->mDepthIndex );
363   //map.Insert( Toolkit::Visual::Property::ENABLED, (bool) mImpl->mRenderer );
364 }
365
366
367 void Visual::Base::EnablePreMultipliedAlpha( bool preMultiplied )
368 {
369   if( preMultiplied )
370   {
371     mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
372   }
373   else
374   {
375     mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
376   }
377
378   if( mImpl->mRenderer )
379   {
380     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
381     mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, static_cast<float>( preMultiplied ) );
382   }
383 }
384
385 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
386 {
387   return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
388 }
389
390 void Visual::Base::DoSetOffStage( Actor& actor )
391 {
392   actor.RemoveRenderer( mImpl->mRenderer );
393   mImpl->mRenderer.Reset();
394 }
395
396 bool Visual::Base::IsOnStage() const
397 {
398   return mImpl->mFlags & Impl::IS_ON_STAGE;
399 }
400
401 bool Visual::Base::IsRoundedCornerRequired() const
402 {
403   return !EqualsZero( mImpl->mCornerRadius );
404 }
405
406 void Visual::Base::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
407 {
408   // May be overriden by derived class
409 }
410
411 void Visual::Base::RegisterMixColor()
412 {
413   // Only register if not already registered.
414   // (Color and Primitive visuals will register their own and save to this index)
415   if( mImpl->mMixColorIndex == Property::INVALID_INDEX )
416   {
417     mImpl->mMixColorIndex = DevelHandle::RegisterProperty(
418       mImpl->mRenderer,
419       Toolkit::Visual::Property::MIX_COLOR,
420       MIX_COLOR,
421       Vector3(mImpl->mMixColor) );
422   }
423
424   if( mImpl->mMixColor.a < 1.f )
425   {
426     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
427   }
428
429   mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, mImpl->mMixColor.a );
430
431   float preMultipliedAlpha = 0.0f;
432   if( IsPreMultipliedAlphaEnabled() )
433   {
434     preMultipliedAlpha = 1.0f;
435   }
436   mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, preMultipliedAlpha );
437 }
438
439 void Visual::Base::SetMixColor( const Vector4& color )
440 {
441   mImpl->mMixColor = color;
442
443   if( mImpl->mRenderer )
444   {
445     mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, Vector3(color) );
446     mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, color.a );
447     if( color.a < 1.f )
448     {
449       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
450     }
451   }
452 }
453
454 void Visual::Base::SetMixColor( const Vector3& color )
455 {
456   mImpl->mMixColor.r = color.r;
457   mImpl->mMixColor.g = color.g;
458   mImpl->mMixColor.b = color.b;
459
460   if( mImpl->mRenderer )
461   {
462     mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
463   }
464 }
465
466 const Vector4& Visual::Base::GetMixColor() const
467 {
468   return mImpl->mMixColor;
469 }
470
471 void Visual::Base::AddEventObserver( Visual::EventObserver& observer)
472 {
473   mImpl->mEventObserver = &observer;
474 }
475
476 void Visual::Base::RemoveEventObserver( Visual::EventObserver& observer )
477 {
478   mImpl->mEventObserver = NULL;
479 }
480
481 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
482 {
483   if( mImpl->mResourceStatus != resourceStatus )
484   {
485     mImpl->mResourceStatus = resourceStatus;
486
487     if( mImpl->mEventObserver )
488     {
489       // observer is currently a control impl
490       mImpl->mEventObserver->ResourceReady( *this );
491     }
492   }
493 }
494
495 bool Visual::Base::IsResourceReady() const
496 {
497   return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY );
498 }
499
500 bool Visual::Base::IsSynchronousLoadingRequired() const
501 {
502   return ( mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING );
503 }
504
505 Toolkit::Visual::Type Visual::Base::GetType() const
506 {
507   return mImpl->mType;
508 }
509
510 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
511 {
512   return mImpl->mResourceStatus;
513 }
514
515 Visual::FittingMode Visual::Base::GetFittingMode() const
516 {
517   return mImpl->mFittingMode;
518 }
519
520 Visual::Base& Visual::Base::GetVisualObject()
521 {
522   return *this;
523 }
524
525 Renderer Visual::Base::GetRenderer()
526 {
527   return mImpl->mRenderer;
528 }
529
530 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
531 {
532   Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
533
534   if( index == Property::INVALID_INDEX )
535   {
536     // Is it a shader property?
537     Shader shader = mImpl->mRenderer.GetShader();
538     index = DevelHandle::GetPropertyIndex( shader, key );
539     if( index != Property::INVALID_INDEX )
540     {
541       // Yes - we should register it in the Renderer so it can be set / animated
542       // independently, as shaders are shared across multiple renderers.
543       std::string keyName;
544       Property::Index keyIndex( Property::INVALID_KEY );
545       if( key.type == Property::Key::INDEX )
546       {
547         keyName = shader.GetPropertyName( index );
548         keyIndex = key.indexKey;
549       }
550       else
551       {
552         keyName = key.stringKey;
553         // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
554       }
555       Property::Value value = shader.GetProperty( index );
556       index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
557     }
558   }
559   return index;
560 }
561
562 void Visual::Base::SetupTransition(
563   Dali::Animation& transition,
564   Internal::TransitionData::Animator& animator,
565   Property::Index index,
566   Property::Value& initialValue,
567   Property::Value& targetValue )
568 {
569   if( index != Property::INVALID_INDEX )
570   {
571     if( mImpl->mRenderer )
572     {
573       if( animator.animate == false )
574       {
575         mImpl->mRenderer.SetProperty( index, targetValue );
576       }
577       else
578       {
579         if( animator.initialValue.GetType() != Property::NONE )
580         {
581           mImpl->mRenderer.SetProperty( index, initialValue );
582         }
583
584         if( ! transition )
585         {
586           transition = Dali::Animation::New( 0.1f );
587         }
588
589         transition.AnimateTo( Property( mImpl->mRenderer, index ),
590                               targetValue,
591                               animator.alphaFunction,
592                               TimePeriod( animator.timePeriodDelay,
593                                           animator.timePeriodDuration ) );
594       }
595     }
596   }
597 }
598
599 void Visual::Base::AnimateProperty(
600   Dali::Animation& transition,
601   Internal::TransitionData::Animator& animator )
602 {
603 #if defined(DEBUG_ENABLED)
604   {
605     std::ostringstream oss;
606     oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
607     DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
608   }
609 #endif
610
611   Property::Map map;
612   DoCreatePropertyMap( map );
613   Property::Value* valuePtr = map.Find( Toolkit::Visual::Property::TYPE );
614   int visualType = -1;
615   if( valuePtr )
616   {
617     valuePtr->Get( visualType );
618   }
619
620   if( animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
621       animator.propertyKey == MIX_COLOR ||
622       ( visualType == Toolkit::Visual::COLOR &&
623         animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
624       ( visualType == Toolkit::Visual::PRIMITIVE &&
625         animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
626   {
627     AnimateMixColorProperty( transition, animator );
628   }
629   else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
630           animator.propertyKey == OPACITY )
631   {
632     AnimateOpacityProperty( transition, animator );
633   }
634   else if( mImpl->mRenderer )
635   {
636     AnimateRendererProperty( transition, animator );
637   }
638 }
639
640 void Visual::Base::AnimateOpacityProperty(
641   Dali::Animation& transition,
642   Internal::TransitionData::Animator& animator )
643 {
644   bool isOpaque = mImpl->mMixColor.a >= 1.0f;
645
646   float initialOpacity;
647   if( animator.initialValue.Get( initialOpacity ) )
648   {
649     isOpaque = (initialOpacity >= 1.0f);
650   }
651
652   float targetOpacity;
653   if( animator.targetValue.Get( targetOpacity ) )
654   {
655     mImpl->mMixColor.a = targetOpacity;
656   }
657
658   SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue );
659   SetupBlendMode( transition, isOpaque, animator.animate );
660 }
661
662 void Visual::Base::AnimateRendererProperty(
663   Dali::Animation& transition,
664   Internal::TransitionData::Animator& animator )
665 {
666   Property::Index index = GetPropertyIndex( animator.propertyKey );
667   if( index != Property::INVALID_INDEX )
668   {
669     if( animator.targetValue.GetType() != Property::NONE )
670     {
671       // Try writing target value into transform property map
672       // if it's not a valid key, then it won't alter mTransform
673       Property::Map map;
674       if( animator.propertyKey.type == Property::Key::INDEX )
675       {
676         map.Add( animator.propertyKey.indexKey, animator.targetValue );
677       }
678       else
679       {
680         map.Add( animator.propertyKey.stringKey, animator.targetValue );
681       }
682
683       mImpl->mTransform.UpdatePropertyMap( map );
684     }
685
686     SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
687   }
688 }
689
690 void Visual::Base::AnimateMixColorProperty(
691   Dali::Animation& transition,
692   Internal::TransitionData::Animator& animator )
693 {
694   Property::Index index = mImpl->mMixColorIndex;
695   bool animateOpacity = false;
696   bool isOpaque = true;
697
698   Property::Value initialOpacity;
699   Property::Value targetOpacity;
700   Property::Value initialMixColor;
701   Property::Value targetMixColor;
702
703   if( index != Property::INVALID_INDEX )
704   {
705     Vector4 initialColor;
706     if( animator.initialValue.Get(initialColor) )
707     {
708       if( animator.initialValue.GetType() == Property::VECTOR4 )
709       {
710         // if there is an initial color specifying alpha, test it
711         isOpaque = initialColor.a >= 1.0f;
712         initialOpacity = initialColor.a;
713       }
714       initialMixColor = Vector3( initialColor );
715     }
716
717     // Set target value into data store
718     if( animator.targetValue.GetType() != Property::NONE )
719     {
720       Vector4 mixColor;
721       animator.targetValue.Get(mixColor);
722       if( animator.targetValue.GetType() == Property::VECTOR4 )
723       {
724         mImpl->mMixColor.a = mixColor.a;
725         targetOpacity = mixColor.a;
726         animateOpacity = true;
727       }
728
729       mImpl->mMixColor.r = mixColor.r;
730       mImpl->mMixColor.g = mixColor.g;
731       mImpl->mMixColor.b = mixColor.b;
732       targetMixColor = Vector3(mixColor);
733     }
734
735     SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
736     if( animateOpacity )
737     {
738       SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity );
739       SetupBlendMode( transition, isOpaque, animator.animate );
740     }
741   }
742 }
743
744 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
745 {
746   // Ensure the blend mode is turned on if we are animating opacity, and
747   // turned off after the animation ends if the final value is opaque
748   if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
749   {
750     if( mImpl->mRenderer )
751     {
752       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
753
754       if( animating == true && mImpl->mMixColor.a >= 1.0f )
755       {
756         // When it becomes opaque, set the blend mode back to automatically
757         if( ! mImpl->mBlendSlotDelegate )
758         {
759           mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
760         }
761         transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
762                                              &Visual::Base::OnMixColorFinished );
763       }
764     }
765   }
766 }
767
768 void Visual::Base::OnMixColorFinished( Animation& animation )
769 {
770   if( mImpl->mRenderer )
771   {
772     DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
773     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
774                                   ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
775   }
776   delete mImpl->mBlendSlotDelegate;
777   mImpl->mBlendSlotDelegate = NULL;
778 }
779
780 } // namespace Internal
781
782 } // namespace Toolkit
783
784 } // namespace Dali