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