Merge "Add FittingMode for image" into 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 )
71 : mImpl( new Impl(fittingMode) ),
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::ResourceStatus Visual::Base::GetResourceStatus() const
506 {
507   return mImpl->mResourceStatus;
508 }
509
510 Visual::FittingMode Visual::Base::GetFittingMode() const
511 {
512   return mImpl->mFittingMode;
513 }
514
515 Visual::Base& Visual::Base::GetVisualObject()
516 {
517   return *this;
518 }
519
520 Renderer Visual::Base::GetRenderer()
521 {
522   return mImpl->mRenderer;
523 }
524
525 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
526 {
527   Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
528
529   if( index == Property::INVALID_INDEX )
530   {
531     // Is it a shader property?
532     Shader shader = mImpl->mRenderer.GetShader();
533     index = DevelHandle::GetPropertyIndex( shader, key );
534     if( index != Property::INVALID_INDEX )
535     {
536       // Yes - we should register it in the Renderer so it can be set / animated
537       // independently, as shaders are shared across multiple renderers.
538       std::string keyName;
539       Property::Index keyIndex( Property::INVALID_KEY );
540       if( key.type == Property::Key::INDEX )
541       {
542         keyName = shader.GetPropertyName( index );
543         keyIndex = key.indexKey;
544       }
545       else
546       {
547         keyName = key.stringKey;
548         // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
549       }
550       Property::Value value = shader.GetProperty( index );
551       index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
552     }
553   }
554   return index;
555 }
556
557 void Visual::Base::SetupTransition(
558   Dali::Animation& transition,
559   Internal::TransitionData::Animator& animator,
560   Property::Index index,
561   Property::Value& initialValue,
562   Property::Value& targetValue )
563 {
564   if( index != Property::INVALID_INDEX )
565   {
566     if( mImpl->mRenderer )
567     {
568       if( animator.animate == false )
569       {
570         mImpl->mRenderer.SetProperty( index, targetValue );
571       }
572       else
573       {
574         if( animator.initialValue.GetType() != Property::NONE )
575         {
576           mImpl->mRenderer.SetProperty( index, initialValue );
577         }
578
579         if( ! transition )
580         {
581           transition = Dali::Animation::New( 0.1f );
582         }
583
584         transition.AnimateTo( Property( mImpl->mRenderer, index ),
585                               targetValue,
586                               animator.alphaFunction,
587                               TimePeriod( animator.timePeriodDelay,
588                                           animator.timePeriodDuration ) );
589       }
590     }
591   }
592 }
593
594 void Visual::Base::AnimateProperty(
595   Dali::Animation& transition,
596   Internal::TransitionData::Animator& animator )
597 {
598 #if defined(DEBUG_ENABLED)
599   {
600     std::ostringstream oss;
601     oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
602     DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
603   }
604 #endif
605
606   Property::Map map;
607   DoCreatePropertyMap( map );
608   Property::Value* valuePtr = map.Find( Toolkit::Visual::Property::TYPE );
609   int visualType = -1;
610   if( valuePtr )
611   {
612     valuePtr->Get( visualType );
613   }
614
615   if( animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
616       animator.propertyKey == MIX_COLOR ||
617       ( visualType == Toolkit::Visual::COLOR &&
618         animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
619       ( visualType == Toolkit::Visual::PRIMITIVE &&
620         animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
621   {
622     AnimateMixColorProperty( transition, animator );
623   }
624   else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
625           animator.propertyKey == OPACITY )
626   {
627     AnimateOpacityProperty( transition, animator );
628   }
629   else if( mImpl->mRenderer )
630   {
631     AnimateRendererProperty( transition, animator );
632   }
633 }
634
635 void Visual::Base::AnimateOpacityProperty(
636   Dali::Animation& transition,
637   Internal::TransitionData::Animator& animator )
638 {
639   bool isOpaque = mImpl->mMixColor.a >= 1.0f;
640
641   float initialOpacity;
642   if( animator.initialValue.Get( initialOpacity ) )
643   {
644     isOpaque = (initialOpacity >= 1.0f);
645   }
646
647   float targetOpacity;
648   if( animator.targetValue.Get( targetOpacity ) )
649   {
650     mImpl->mMixColor.a = targetOpacity;
651   }
652
653   SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue );
654   SetupBlendMode( transition, isOpaque, animator.animate );
655 }
656
657 void Visual::Base::AnimateRendererProperty(
658   Dali::Animation& transition,
659   Internal::TransitionData::Animator& animator )
660 {
661   Property::Index index = GetPropertyIndex( animator.propertyKey );
662   if( index != Property::INVALID_INDEX )
663   {
664     if( animator.targetValue.GetType() != Property::NONE )
665     {
666       // Try writing target value into transform property map
667       // if it's not a valid key, then it won't alter mTransform
668       Property::Map map;
669       if( animator.propertyKey.type == Property::Key::INDEX )
670       {
671         map.Add( animator.propertyKey.indexKey, animator.targetValue );
672       }
673       else
674       {
675         map.Add( animator.propertyKey.stringKey, animator.targetValue );
676       }
677
678       mImpl->mTransform.UpdatePropertyMap( map );
679     }
680
681     SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
682   }
683 }
684
685 void Visual::Base::AnimateMixColorProperty(
686   Dali::Animation& transition,
687   Internal::TransitionData::Animator& animator )
688 {
689   Property::Index index = mImpl->mMixColorIndex;
690   bool animateOpacity = false;
691   bool isOpaque = true;
692
693   Property::Value initialOpacity;
694   Property::Value targetOpacity;
695   Property::Value initialMixColor;
696   Property::Value targetMixColor;
697
698   if( index != Property::INVALID_INDEX )
699   {
700     Vector4 initialColor;
701     if( animator.initialValue.Get(initialColor) )
702     {
703       if( animator.initialValue.GetType() == Property::VECTOR4 )
704       {
705         // if there is an initial color specifying alpha, test it
706         isOpaque = initialColor.a >= 1.0f;
707         initialOpacity = initialColor.a;
708       }
709       initialMixColor = Vector3( initialColor );
710     }
711
712     // Set target value into data store
713     if( animator.targetValue.GetType() != Property::NONE )
714     {
715       Vector4 mixColor;
716       animator.targetValue.Get(mixColor);
717       if( animator.targetValue.GetType() == Property::VECTOR4 )
718       {
719         mImpl->mMixColor.a = mixColor.a;
720         targetOpacity = mixColor.a;
721         animateOpacity = true;
722       }
723
724       mImpl->mMixColor.r = mixColor.r;
725       mImpl->mMixColor.g = mixColor.g;
726       mImpl->mMixColor.b = mixColor.b;
727       targetMixColor = Vector3(mixColor);
728     }
729
730     SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
731     if( animateOpacity )
732     {
733       SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity );
734       SetupBlendMode( transition, isOpaque, animator.animate );
735     }
736   }
737 }
738
739 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
740 {
741   // Ensure the blend mode is turned on if we are animating opacity, and
742   // turned off after the animation ends if the final value is opaque
743   if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
744   {
745     if( mImpl->mRenderer )
746     {
747       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
748
749       if( animating == true && mImpl->mMixColor.a >= 1.0f )
750       {
751         // When it becomes opaque, set the blend mode back to automatically
752         if( ! mImpl->mBlendSlotDelegate )
753         {
754           mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
755         }
756         transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
757                                              &Visual::Base::OnMixColorFinished );
758       }
759     }
760   }
761 }
762
763 void Visual::Base::OnMixColorFinished( Animation& animation )
764 {
765   if( mImpl->mRenderer )
766   {
767     DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
768     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
769                                   ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
770   }
771   delete mImpl->mBlendSlotDelegate;
772   mImpl->mBlendSlotDelegate = NULL;
773 }
774
775 } // namespace Internal
776
777 } // namespace Toolkit
778
779 } // namespace Dali