Updated test headers in line with dali-core
[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::SetOnScene( Actor& actor )
314 {
315   if( !IsOnScene() )
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     DoSetOnScene( 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_SCENE; // Only sets the flag if renderer exists
336     }
337   }
338 }
339
340 void Visual::Base::SetOffScene( Actor& actor )
341 {
342   if( IsOnScene() )
343   {
344     DoSetOffScene( actor );
345     mImpl->mMixColorIndex = Property::INVALID_INDEX;
346     mImpl->mCornerRadiusIndex = Property::INVALID_INDEX;
347     mImpl->mFlags &= ~Impl::IS_ON_SCENE;
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
372   auto fittingModeString = Scripting::GetLinearEnumerationName< FittingMode >(
373     mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT );
374   map.Insert( Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString );
375
376   map.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius );
377   map.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, static_cast< int >( mImpl->mCornerRadiusPolicy ) );
378 }
379
380 void Visual::Base::CreateInstancePropertyMap( Property::Map& map ) const
381 {
382   DoCreateInstancePropertyMap( map );
383
384   if( mImpl->mCustomShader )
385   {
386     mImpl->mCustomShader->CreatePropertyMap( map );
387   }
388
389   //map.Insert( Toolkit::Visual::Property::DEPTH_INDEX, mImpl->mDepthIndex );
390   //map.Insert( Toolkit::Visual::Property::ENABLED, (bool) mImpl->mRenderer );
391 }
392
393
394 void Visual::Base::EnablePreMultipliedAlpha( bool preMultiplied )
395 {
396   if( preMultiplied )
397   {
398     mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
399   }
400   else
401   {
402     mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
403   }
404
405   if( mImpl->mRenderer )
406   {
407     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
408     mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, static_cast<float>( preMultiplied ) );
409   }
410 }
411
412 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
413 {
414   return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
415 }
416
417 void Visual::Base::DoSetOffScene( Actor& actor )
418 {
419   actor.RemoveRenderer( mImpl->mRenderer );
420   mImpl->mRenderer.Reset();
421 }
422
423 bool Visual::Base::IsOnScene() const
424 {
425   return mImpl->mFlags & Impl::IS_ON_SCENE;
426 }
427
428 bool Visual::Base::IsRoundedCornerRequired() const
429 {
430   return !EqualsZero( mImpl->mCornerRadius );
431 }
432
433 void Visual::Base::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
434 {
435   // May be overriden by derived class
436 }
437
438 void Visual::Base::RegisterMixColor()
439 {
440   // Only register if not already registered.
441   // (Color and Primitive visuals will register their own and save to this index)
442   if( mImpl->mMixColorIndex == Property::INVALID_INDEX )
443   {
444     mImpl->mMixColorIndex = DevelHandle::RegisterProperty(
445       mImpl->mRenderer,
446       Toolkit::Visual::Property::MIX_COLOR,
447       MIX_COLOR,
448       Vector3(mImpl->mMixColor) );
449   }
450
451   if( mImpl->mMixColor.a < 1.f )
452   {
453     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
454   }
455
456   mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, mImpl->mMixColor.a );
457
458   float preMultipliedAlpha = 0.0f;
459   if( IsPreMultipliedAlphaEnabled() )
460   {
461     preMultipliedAlpha = 1.0f;
462   }
463   mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, preMultipliedAlpha );
464 }
465
466 void Visual::Base::SetMixColor( const Vector4& color )
467 {
468   mImpl->mMixColor = color;
469
470   if( mImpl->mRenderer )
471   {
472     mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, Vector3(color) );
473     mImpl->mRenderer.SetProperty( DevelRenderer::Property::OPACITY, color.a );
474     if( color.a < 1.f )
475     {
476       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
477     }
478   }
479 }
480
481 void Visual::Base::SetMixColor( const Vector3& color )
482 {
483   mImpl->mMixColor.r = color.r;
484   mImpl->mMixColor.g = color.g;
485   mImpl->mMixColor.b = color.b;
486
487   if( mImpl->mRenderer )
488   {
489     mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
490   }
491 }
492
493 const Vector4& Visual::Base::GetMixColor() const
494 {
495   return mImpl->mMixColor;
496 }
497
498 void Visual::Base::AddEventObserver( Visual::EventObserver& observer)
499 {
500   mImpl->mEventObserver = &observer;
501 }
502
503 void Visual::Base::RemoveEventObserver( Visual::EventObserver& observer )
504 {
505   mImpl->mEventObserver = NULL;
506 }
507
508 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
509 {
510   if( mImpl->mResourceStatus != resourceStatus )
511   {
512     mImpl->mResourceStatus = resourceStatus;
513
514     if( mImpl->mEventObserver )
515     {
516       // observer is currently a control impl
517       mImpl->mEventObserver->ResourceReady( *this );
518     }
519   }
520 }
521
522 bool Visual::Base::IsResourceReady() const
523 {
524   return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY );
525 }
526
527 bool Visual::Base::IsSynchronousLoadingRequired() const
528 {
529   return ( mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING );
530 }
531
532 Toolkit::Visual::Type Visual::Base::GetType() const
533 {
534   return mImpl->mType;
535 }
536
537 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
538 {
539   return mImpl->mResourceStatus;
540 }
541
542 Visual::FittingMode Visual::Base::GetFittingMode() const
543 {
544   return mImpl->mFittingMode;
545 }
546
547 Visual::Base& Visual::Base::GetVisualObject()
548 {
549   return *this;
550 }
551
552 Renderer Visual::Base::GetRenderer()
553 {
554   return mImpl->mRenderer;
555 }
556
557 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
558 {
559   Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
560
561   if( index == Property::INVALID_INDEX )
562   {
563     // Is it a shader property?
564     Shader shader = mImpl->mRenderer.GetShader();
565     index = DevelHandle::GetPropertyIndex( shader, key );
566     if( index != Property::INVALID_INDEX )
567     {
568       // Yes - we should register it in the Renderer so it can be set / animated
569       // independently, as shaders are shared across multiple renderers.
570       std::string keyName;
571       Property::Index keyIndex( Property::INVALID_KEY );
572       if( key.type == Property::Key::INDEX )
573       {
574         keyName = shader.GetPropertyName( index );
575         keyIndex = key.indexKey;
576       }
577       else
578       {
579         keyName = key.stringKey;
580         // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
581       }
582       Property::Value value = shader.GetProperty( index );
583       index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
584     }
585   }
586   return index;
587 }
588
589 void Visual::Base::SetupTransition(
590   Dali::Animation& transition,
591   Internal::TransitionData::Animator& animator,
592   Property::Index index,
593   Property::Value& initialValue,
594   Property::Value& targetValue )
595 {
596   if( index != Property::INVALID_INDEX )
597   {
598     if( mImpl->mRenderer )
599     {
600       if( animator.animate == false )
601       {
602         mImpl->mRenderer.SetProperty( index, targetValue );
603       }
604       else
605       {
606         if( animator.initialValue.GetType() != Property::NONE )
607         {
608           mImpl->mRenderer.SetProperty( index, initialValue );
609         }
610
611         if( ! transition )
612         {
613           transition = Dali::Animation::New( 0.1f );
614         }
615
616         transition.AnimateTo( Property( mImpl->mRenderer, index ),
617                               targetValue,
618                               animator.alphaFunction,
619                               TimePeriod( animator.timePeriodDelay,
620                                           animator.timePeriodDuration ) );
621       }
622     }
623   }
624 }
625
626 void Visual::Base::AnimateProperty(
627   Dali::Animation& transition,
628   Internal::TransitionData::Animator& animator )
629 {
630 #if defined(DEBUG_ENABLED)
631   {
632     std::ostringstream oss;
633     oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
634     DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
635   }
636 #endif
637
638   Property::Map map;
639   DoCreatePropertyMap( map );
640   Property::Value* valuePtr = map.Find( Toolkit::Visual::Property::TYPE );
641   int visualType = -1;
642   if( valuePtr )
643   {
644     valuePtr->Get( visualType );
645   }
646
647   if( animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
648       animator.propertyKey == MIX_COLOR ||
649       ( visualType == Toolkit::Visual::COLOR &&
650         animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
651       ( visualType == Toolkit::Visual::PRIMITIVE &&
652         animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
653   {
654     AnimateMixColorProperty( transition, animator );
655   }
656   else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
657           animator.propertyKey == OPACITY )
658   {
659     AnimateOpacityProperty( transition, animator );
660   }
661   else if( mImpl->mRenderer )
662   {
663     AnimateRendererProperty( transition, animator );
664   }
665 }
666
667 void Visual::Base::AnimateOpacityProperty(
668   Dali::Animation& transition,
669   Internal::TransitionData::Animator& animator )
670 {
671   bool isOpaque = mImpl->mMixColor.a >= 1.0f;
672
673   float initialOpacity;
674   if( animator.initialValue.Get( initialOpacity ) )
675   {
676     isOpaque = (initialOpacity >= 1.0f);
677   }
678
679   float targetOpacity;
680   if( animator.targetValue.Get( targetOpacity ) )
681   {
682     mImpl->mMixColor.a = targetOpacity;
683   }
684
685   SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue );
686   SetupBlendMode( transition, isOpaque, animator.animate );
687 }
688
689 void Visual::Base::AnimateRendererProperty(
690   Dali::Animation& transition,
691   Internal::TransitionData::Animator& animator )
692 {
693   Property::Index index = GetPropertyIndex( animator.propertyKey );
694   if( index != Property::INVALID_INDEX )
695   {
696     if( animator.targetValue.GetType() != Property::NONE )
697     {
698       // Try writing target value into transform property map
699       // if it's not a valid key, then it won't alter mTransform
700       Property::Map map;
701       if( animator.propertyKey.type == Property::Key::INDEX )
702       {
703         map.Add( animator.propertyKey.indexKey, animator.targetValue );
704       }
705       else
706       {
707         map.Add( animator.propertyKey.stringKey, animator.targetValue );
708       }
709
710       mImpl->mTransform.UpdatePropertyMap( map );
711     }
712
713     SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
714   }
715 }
716
717 void Visual::Base::AnimateMixColorProperty(
718   Dali::Animation& transition,
719   Internal::TransitionData::Animator& animator )
720 {
721   Property::Index index = mImpl->mMixColorIndex;
722   bool animateOpacity = false;
723   bool isOpaque = true;
724
725   Property::Value initialOpacity;
726   Property::Value targetOpacity;
727   Property::Value initialMixColor;
728   Property::Value targetMixColor;
729
730   if( index != Property::INVALID_INDEX )
731   {
732     Vector4 initialColor;
733     if( animator.initialValue.Get(initialColor) )
734     {
735       if( animator.initialValue.GetType() == Property::VECTOR4 )
736       {
737         // if there is an initial color specifying alpha, test it
738         isOpaque = initialColor.a >= 1.0f;
739         initialOpacity = initialColor.a;
740       }
741       initialMixColor = Vector3( initialColor );
742     }
743
744     // Set target value into data store
745     if( animator.targetValue.GetType() != Property::NONE )
746     {
747       Vector4 mixColor;
748       animator.targetValue.Get(mixColor);
749       if( animator.targetValue.GetType() == Property::VECTOR4 )
750       {
751         mImpl->mMixColor.a = mixColor.a;
752         targetOpacity = mixColor.a;
753         animateOpacity = true;
754       }
755
756       mImpl->mMixColor.r = mixColor.r;
757       mImpl->mMixColor.g = mixColor.g;
758       mImpl->mMixColor.b = mixColor.b;
759       targetMixColor = Vector3(mixColor);
760     }
761
762     SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
763     if( animateOpacity )
764     {
765       SetupTransition( transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity );
766       SetupBlendMode( transition, isOpaque, animator.animate );
767     }
768   }
769 }
770
771 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
772 {
773   // Ensure the blend mode is turned on if we are animating opacity, and
774   // turned off after the animation ends if the final value is opaque
775   if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
776   {
777     if( mImpl->mRenderer )
778     {
779       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
780
781       if( animating == true && mImpl->mMixColor.a >= 1.0f )
782       {
783         // When it becomes opaque, set the blend mode back to automatically
784         if( ! mImpl->mBlendSlotDelegate )
785         {
786           mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
787         }
788         transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
789                                              &Visual::Base::OnMixColorFinished );
790       }
791     }
792   }
793 }
794
795 void Visual::Base::OnMixColorFinished( Animation& animation )
796 {
797   if( mImpl->mRenderer )
798   {
799     DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
800     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
801                                   ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
802   }
803   delete mImpl->mBlendSlotDelegate;
804   mImpl->mBlendSlotDelegate = NULL;
805 }
806
807 } // namespace Internal
808
809 } // namespace Toolkit
810
811 } // namespace Dali