Merge "Add null check for currentFocusActor" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-base-impl.cpp
1 /*
2  * Copyright (c) 2022 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/rendering/renderer-devel.h>
24 #include <dali/devel-api/scripting/enum-helper.h>
25 #include <dali/integration-api/debug.h>
26
27 //INTERNAL HEARDER
28 #include <dali-toolkit/devel-api/visuals/visual-actions-devel.h>
29 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
30 #include <dali-toolkit/internal/helpers/property-helper.h>
31 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
32 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
33 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
34 #include <dali-toolkit/public-api/visuals/primitive-visual-properties.h>
35 #include <dali-toolkit/public-api/visuals/visual-properties.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 namespace Toolkit
50 {
51 namespace Internal
52 {
53 namespace
54 {
55 DALI_ENUM_TO_STRING_TABLE_BEGIN(VISUAL_FITTING_MODE)
56   DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_KEEP_ASPECT_RATIO)
57   DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FILL)
58   DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, OVER_FIT_KEEP_ASPECT_RATIO)
59   DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, CENTER)
60   DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_WIDTH)
61   DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_HEIGHT)
62 DALI_ENUM_TO_STRING_TABLE_END(VISUAL_FITTING_MODE)
63
64 /**
65  * @brief Check whether this visual type can use corner radius feature or not.
66  * @param type VisualType that want to checkup
67  * @return true if type can use corner radius feature.
68  */
69 static bool IsTypeAvailableForCornerRadius(Toolkit::Visual::Type type)
70 {
71   switch(static_cast<Toolkit::DevelVisual::Type>(type))
72   {
73     case Toolkit::Visual::Type::COLOR:
74     case Toolkit::Visual::Type::GRADIENT:
75     case Toolkit::Visual::Type::IMAGE:
76     case Toolkit::Visual::Type::SVG:
77     case Toolkit::Visual::Type::ANIMATED_IMAGE:
78     case Toolkit::DevelVisual::Type::ANIMATED_VECTOR_IMAGE:
79     {
80       return true;
81     }
82     default:
83     {
84       return false;
85     }
86   }
87 }
88
89 /**
90  * @brief Check whether this visual type can use borderline feature or not.
91  * @param type VisualType that want to checkup
92  * @return true if type can use borderline feature.
93  */
94 static bool IsTypeAvailableForBorderline(Toolkit::Visual::Type type)
95 {
96   switch(static_cast<Toolkit::DevelVisual::Type>(type))
97   {
98     case Toolkit::Visual::Type::COLOR:
99     case Toolkit::Visual::Type::GRADIENT:
100     case Toolkit::Visual::Type::IMAGE:
101     case Toolkit::Visual::Type::SVG:
102     case Toolkit::Visual::Type::ANIMATED_IMAGE:
103     case Toolkit::DevelVisual::Type::ANIMATED_VECTOR_IMAGE:
104     {
105       return true;
106     }
107     default:
108     {
109       return false;
110     }
111   }
112 }
113
114 } // namespace
115
116 Visual::Base::Base(VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type)
117 : mImpl(new Impl(fittingMode, type)),
118   mFactoryCache(factoryCache)
119 {
120 }
121
122 Visual::Base::~Base()
123 {
124   delete mImpl;
125 }
126
127 void Visual::Base::Initialize()
128 {
129   // The Renderer should be created inside derived class here.
130   OnInitialize();
131
132   if(mImpl->mRenderer)
133   {
134     RegisterMixColor();
135
136     if(IsRoundedCornerRequired())
137     {
138       mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
139       mImpl->mRenderer.RegisterUniqueProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
140
141       mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
142     }
143     if(IsBorderlineRequired())
144     {
145       mImpl->mBorderlineWidthIndex  = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
146       mImpl->mBorderlineColorIndex  = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
147       mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_OFFSET, BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
148
149       mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
150     }
151   }
152 }
153
154 void Visual::Base::SetCustomShader(const Property::Map& shaderMap)
155 {
156   if(mImpl->mCustomShader)
157   {
158     mImpl->mCustomShader->SetPropertyMap(shaderMap);
159   }
160   else
161   {
162     mImpl->mCustomShader = new Impl::CustomShader(shaderMap);
163   }
164
165   // Let derived class know
166   UpdateShader();
167 }
168
169 void Visual::Base::SetProperties(const Property::Map& propertyMap)
170 {
171   bool needUpdateShader = false;
172   for(size_t i = 0; i < propertyMap.Count(); ++i)
173   {
174     const KeyValuePair&    pair  = propertyMap.GetKeyValue(i);
175     const Property::Key&   key   = pair.first;
176     const Property::Value& value = pair.second;
177
178     Property::Key matchKey = key;
179     if(matchKey.type == Property::Key::STRING)
180     {
181       if(matchKey == CUSTOM_SHADER)
182       {
183         matchKey = Property::Key(Toolkit::Visual::Property::SHADER);
184       }
185       else if(matchKey == TRANSFORM)
186       {
187         matchKey = Property::Key(Toolkit::Visual::Property::TRANSFORM);
188       }
189       else if(matchKey == PREMULTIPLIED_ALPHA)
190       {
191         matchKey = Property::Key(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA);
192       }
193       else if(matchKey == MIX_COLOR)
194       {
195         matchKey = Property::Key(Toolkit::Visual::Property::MIX_COLOR);
196       }
197       else if(matchKey == OPACITY)
198       {
199         matchKey = Property::Key(Toolkit::Visual::Property::OPACITY);
200       }
201       else if(matchKey == VISUAL_FITTING_MODE)
202       {
203         matchKey = Property::Key(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE);
204       }
205       else if(matchKey == BORDERLINE_WIDTH)
206       {
207         matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH);
208       }
209       else if(matchKey == BORDERLINE_COLOR)
210       {
211         matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_COLOR);
212       }
213       else if(matchKey == BORDERLINE_OFFSET)
214       {
215         matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET);
216       }
217       else if(matchKey == CORNER_RADIUS)
218       {
219         matchKey = Property::Key(Toolkit::DevelVisual::Property::CORNER_RADIUS);
220       }
221       else if(matchKey == CORNER_RADIUS_POLICY)
222       {
223         matchKey = Property::Key(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY);
224       }
225     }
226
227     switch(matchKey.indexKey)
228     {
229       case Toolkit::Visual::Property::SHADER:
230       {
231         Property::Map shaderMap;
232         if(value.Get(shaderMap))
233         {
234           SetCustomShader(shaderMap);
235         }
236         break;
237       }
238
239       case Toolkit::Visual::Property::TRANSFORM:
240       {
241         Property::Map map;
242         if(value.Get(map))
243         {
244           mImpl->mTransform.SetPropertyMap(map);
245         }
246         break;
247       }
248
249       case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
250       {
251         bool premultipliedAlpha = false;
252         if(value.Get(premultipliedAlpha))
253         {
254           EnablePreMultipliedAlpha(premultipliedAlpha);
255         }
256         break;
257       }
258
259       case Toolkit::Visual::Property::MIX_COLOR:
260       {
261         Vector4 mixColor;
262         if(value.Get(mixColor))
263         {
264           if(value.GetType() == Property::VECTOR4)
265           {
266             SetMixColor(mixColor);
267           }
268           else
269           {
270             Vector3 mixColor3(mixColor);
271             SetMixColor(mixColor3);
272           }
273         }
274         break;
275       }
276       case Toolkit::Visual::Property::OPACITY:
277       {
278         float opacity;
279         if(value.Get(opacity))
280         {
281           mImpl->mMixColor.a = opacity;
282           SetMixColor(mImpl->mMixColor);
283         }
284         break;
285       }
286       case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
287       {
288         Scripting::GetEnumerationProperty<Visual::FittingMode>(
289           value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode);
290         break;
291       }
292       case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
293       {
294         float width;
295         if(value.Get(width))
296         {
297           mImpl->mBorderlineWidth = width;
298         }
299
300         if(mImpl->mBorderlineWidthIndex != Property::INVALID_INDEX)
301         {
302           mImpl->mRenderer.SetProperty(mImpl->mBorderlineWidthIndex, mImpl->mBorderlineWidth);
303         }
304         else if(DALI_UNLIKELY(mImpl->mRenderer && IsBorderlineRequired()))
305         {
306           // Unusual case. SetProperty called after OnInitialize().
307           // Assume that DoAction call UPDATE_PROPERTY.
308           // We must regist properies into renderer, and update shader.
309
310           // Borderline added by this action. Register property to renderer.
311           mImpl->mBorderlineWidthIndex  = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
312           mImpl->mBorderlineColorIndex  = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
313           mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_OFFSET, BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
314
315           // Make Blend mode ON_WITHOUT_CULL for transparent mix color.
316           mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
317
318           // Change the shader must not be occured many times. we always have to use borderline feature.
319           mImpl->mAlwaysUsingBorderline = true;
320
321           // Change shader
322           needUpdateShader = true;
323         }
324         break;
325       }
326       case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
327       {
328         Vector4 color;
329         if(value.Get(color))
330         {
331           mImpl->mBorderlineColor = color;
332         }
333
334         if(mImpl->mBorderlineColorIndex != Property::INVALID_INDEX)
335         {
336           mImpl->mRenderer.SetProperty(mImpl->mBorderlineColorIndex, mImpl->mBorderlineColor);
337         }
338         break;
339       }
340       case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
341       {
342         float offset;
343         if(value.Get(offset))
344         {
345           mImpl->mBorderlineOffset = offset;
346         }
347
348         if(mImpl->mBorderlineOffsetIndex != Property::INVALID_INDEX)
349         {
350           mImpl->mRenderer.SetProperty(mImpl->mBorderlineOffsetIndex, mImpl->mBorderlineOffset);
351         }
352         break;
353       }
354       case Toolkit::DevelVisual::Property::CORNER_RADIUS:
355       {
356         if(value.GetType() == Property::VECTOR4)
357         {
358           // If CORNER_RADIUS Property is Vector4,
359           // Each values mean the radius of
360           // (top-left, top-right, bottom-right, bottom-left)
361           Vector4 radius;
362           if(value.Get(radius))
363           {
364             mImpl->mCornerRadius = radius;
365           }
366         }
367         else
368         {
369           // If CORNER_RADIUS Property is float,
370           // Every corner radius have same value
371           float radius;
372           if(value.Get(radius))
373           {
374             mImpl->mCornerRadius = Vector4(radius, radius, radius, radius);
375           }
376         }
377
378         if(mImpl->mCornerRadiusIndex != Property::INVALID_INDEX)
379         {
380           mImpl->mRenderer.SetProperty(mImpl->mCornerRadiusIndex, mImpl->mCornerRadius);
381         }
382         else if(DALI_UNLIKELY(mImpl->mRenderer && IsRoundedCornerRequired()))
383         {
384           // Unusual case. SetProperty called after OnInitialize().
385           // Assume that DoAction call UPDATE_PROPERTY.
386           // We must regist properies into renderer, and update shader.
387
388           // CornerRadius added by this action. Regist property to renderer.
389           mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
390           mImpl->mRenderer.RegisterUniqueProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
391
392           // Change the shader must not be occured many times. we always have to use corner radius feature.
393           mImpl->mAlwaysUsingCornerRadius = true;
394
395           if(!IsBorderlineRequired())
396           {
397             // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
398             mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
399           }
400
401           // Change shader
402           needUpdateShader = true;
403         }
404         break;
405       }
406       case Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY:
407       {
408         int policy;
409         if(value.Get(policy))
410         {
411           switch(policy)
412           {
413             case Toolkit::Visual::Transform::Policy::RELATIVE:
414             case Toolkit::Visual::Transform::Policy::ABSOLUTE:
415             {
416               mImpl->mCornerRadiusPolicy = policy;
417               if(DALI_UNLIKELY(mImpl->mRenderer && mImpl->mCornerRadiusIndex != Property::INVALID_INDEX))
418               {
419                 // Unusual case. SetProperty called after OnInitialize().
420                 // Assume that DoAction call UPDATE_PROPERTY.
421                 // We must update properies result into renderer
422                 // Note : mImpl->mCornerRadiusIndex is not INVALID_INDEX.
423                 // So CornerRadiusPolicy property is already registed.
424                 mImpl->mRenderer.SetProperty(mImpl->mRenderer.GetPropertyIndex(CORNER_RADIUS_POLICY), mImpl->mCornerRadiusPolicy);
425               }
426               break;
427             }
428             default:
429             {
430               DALI_LOG_ERROR("Unsupported policy: %d\n", policy);
431               break;
432             }
433           }
434         }
435         break;
436       }
437     }
438   }
439
440   DoSetProperties(propertyMap);
441
442   if(DALI_UNLIKELY(needUpdateShader))
443   {
444     UpdateShader();
445   }
446 }
447
448 void Visual::Base::SetTransformAndSize(const Property::Map& transform, Size controlSize)
449 {
450   mImpl->mControlSize = controlSize;
451   mImpl->mTransform.UpdatePropertyMap(transform);
452
453 #if defined(DEBUG_ENABLED)
454   std::ostringstream oss;
455   oss << transform;
456   DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, "Visual::Base::SetTransformAndSize(%s) - [\e[1;32mtransform: %s  controlSize: (%3.1f, %3.1f)]\e[0m\n", GetName().c_str(), oss.str().c_str(), controlSize.x, controlSize.y);
457 #endif
458
459   OnSetTransform();
460 }
461
462 void Visual::Base::SetName(const std::string& name)
463 {
464   mImpl->mName = name;
465 }
466
467 const std::string& Visual::Base::GetName() const
468 {
469   return mImpl->mName;
470 }
471
472 float Visual::Base::GetHeightForWidth(float width)
473 {
474   float   aspectCorrectedHeight = 0.f;
475   Vector2 naturalSize;
476   GetNaturalSize(naturalSize);
477   if(naturalSize.width)
478   {
479     aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
480   }
481   return aspectCorrectedHeight;
482 }
483
484 float Visual::Base::GetWidthForHeight(float height)
485 {
486   float   aspectCorrectedWidth = 0.f;
487   Vector2 naturalSize;
488   GetNaturalSize(naturalSize);
489   if(naturalSize.height > 0.0f)
490   {
491     aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
492   }
493   return aspectCorrectedWidth;
494 }
495
496 void Visual::Base::GetNaturalSize(Vector2& naturalSize)
497 {
498   naturalSize = Vector2::ZERO;
499 }
500
501 void Visual::Base::DoAction(const Property::Index actionId, const Property::Value attributes)
502 {
503   OnDoAction(actionId, attributes);
504
505   // Check if action is valid for this visual type and perform action if possible
506   switch(actionId)
507   {
508     case DevelVisual::Action::UPDATE_PROPERTY:
509     {
510       const Property::Map* map = attributes.GetMap();
511       if(map)
512       {
513         SetProperties(*map);
514       }
515       break;
516     }
517   }
518 }
519
520 void Visual::Base::SetDepthIndex(int index)
521 {
522   mImpl->mDepthIndex = index;
523   if(mImpl->mRenderer)
524   {
525     mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
526   }
527 }
528
529 int Visual::Base::GetDepthIndex() const
530 {
531   return mImpl->mDepthIndex;
532 }
533
534 void Visual::Base::SetOnScene(Actor& actor)
535 {
536   if(!IsOnScene())
537   {
538     // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
539     // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
540     DoSetOnScene(actor);
541
542     if(mImpl->mRenderer)
543     {
544       mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
545       mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
546     }
547
548     mImpl->mFlags |= Impl::IS_ON_SCENE;
549   }
550 }
551
552 void Visual::Base::SetOffScene(Actor& actor)
553 {
554   if(IsOnScene())
555   {
556     DoSetOffScene(actor);
557     mImpl->mFlags &= ~Impl::IS_ON_SCENE;
558   }
559 }
560
561 void Visual::Base::CreatePropertyMap(Property::Map& map) const
562 {
563   if(mImpl->mRenderer)
564   {
565     // Update values from Renderer
566     mImpl->mMixColor   = mImpl->mRenderer.GetProperty<Vector3>(mImpl->mMixColorIndex);
567     mImpl->mMixColor.a = mImpl->mRenderer.GetProperty<float>(DevelRenderer::Property::OPACITY);
568     if(mImpl->mTransform.mOffsetIndex != Property::INVALID_INDEX)
569     {
570       mImpl->mTransform.mOffset = mImpl->mRenderer.GetProperty<Vector2>(mImpl->mTransform.mOffsetIndex);
571     }
572     if(mImpl->mTransform.mSizeIndex != Property::INVALID_INDEX)
573     {
574       mImpl->mTransform.mSize = mImpl->mRenderer.GetProperty<Vector2>(mImpl->mTransform.mSizeIndex);
575     }
576     if(mImpl->mCornerRadiusIndex != Property::INVALID_INDEX)
577     {
578       mImpl->mCornerRadius = mImpl->mRenderer.GetProperty<Vector4>(mImpl->mCornerRadiusIndex);
579     }
580     if(mImpl->mBorderlineWidthIndex != Property::INVALID_INDEX)
581     {
582       mImpl->mBorderlineWidth = mImpl->mRenderer.GetProperty<float>(mImpl->mBorderlineWidthIndex);
583     }
584     if(mImpl->mBorderlineColorIndex != Property::INVALID_INDEX)
585     {
586       mImpl->mBorderlineColor = mImpl->mRenderer.GetProperty<Vector4>(mImpl->mBorderlineColorIndex);
587     }
588     if(mImpl->mBorderlineOffsetIndex != Property::INVALID_INDEX)
589     {
590       mImpl->mBorderlineOffset = mImpl->mRenderer.GetProperty<float>(mImpl->mBorderlineOffsetIndex);
591     }
592   }
593
594   DoCreatePropertyMap(map);
595
596   if(mImpl->mCustomShader)
597   {
598     mImpl->mCustomShader->CreatePropertyMap(map);
599   }
600
601   Property::Map transform;
602   mImpl->mTransform.GetPropertyMap(transform);
603   map.Insert(Toolkit::Visual::Property::TRANSFORM, transform);
604
605   bool premultipliedAlpha(IsPreMultipliedAlphaEnabled());
606   map.Insert(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha);
607
608   // Note, Color and Primitive will also insert their own mix color into the map
609   // which is ok, because they have a different key value range.
610   map.Insert(Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor); // vec4
611   map.Insert(Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a);
612
613   auto fittingModeString = Scripting::GetLinearEnumerationName<FittingMode>(
614     mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT);
615   map.Insert(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString);
616
617   map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
618   map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
619   map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
620
621   map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius);
622   map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, static_cast<int>(mImpl->mCornerRadiusPolicy));
623 }
624
625 void Visual::Base::CreateInstancePropertyMap(Property::Map& map) const
626 {
627   DoCreateInstancePropertyMap(map);
628
629   if(mImpl->mCustomShader)
630   {
631     mImpl->mCustomShader->CreatePropertyMap(map);
632   }
633 }
634
635 void Visual::Base::EnablePreMultipliedAlpha(bool preMultiplied)
636 {
637   if(preMultiplied)
638   {
639     mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
640   }
641   else
642   {
643     mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
644   }
645
646   if(mImpl->mRenderer)
647   {
648     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
649     mImpl->mRenderer.RegisterProperty(PRE_MULTIPLIED_ALPHA_PROPERTY, static_cast<float>(preMultiplied));
650   }
651 }
652
653 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
654 {
655   return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
656 }
657
658 void Visual::Base::DoSetOffScene(Actor& actor)
659 {
660   actor.RemoveRenderer(mImpl->mRenderer);
661 }
662
663 bool Visual::Base::IsOnScene() const
664 {
665   return mImpl->mFlags & Impl::IS_ON_SCENE;
666 }
667
668 bool Visual::Base::IsRoundedCornerRequired() const
669 {
670   // If VisualType doesn't support rounded corner, always return false.
671   if(IsTypeAvailableForCornerRadius(mImpl->mType))
672   {
673     if(mImpl->mRenderer && mImpl->mCornerRadiusIndex != Property::INVALID_INDEX)
674     {
675       // Update values from Renderer
676       mImpl->mCornerRadius = mImpl->mRenderer.GetProperty<Vector4>(mImpl->mCornerRadiusIndex);
677     }
678     return !(mImpl->mCornerRadius == Vector4::ZERO) || mImpl->mAlwaysUsingCornerRadius;
679   }
680   return false;
681 }
682
683 bool Visual::Base::IsBorderlineRequired() const
684 {
685   // If VisualType doesn't support borderline, always return false.
686   if(IsTypeAvailableForBorderline(mImpl->mType))
687   {
688     if(mImpl->mRenderer && mImpl->mBorderlineWidthIndex != Property::INVALID_INDEX)
689     {
690       // Update values from Renderer
691       mImpl->mBorderlineWidth = mImpl->mRenderer.GetProperty<float>(mImpl->mBorderlineWidthIndex);
692     }
693     return !EqualsZero(mImpl->mBorderlineWidth) || mImpl->mAlwaysUsingBorderline;
694   }
695   return false;
696 }
697
698 void Visual::Base::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
699 {
700   // May be overriden by derived class
701 }
702
703 void Visual::Base::RegisterMixColor()
704 {
705   // Only register if not already registered.
706   // (Color and Primitive visuals will register their own and save to this index)
707   if(mImpl->mMixColorIndex == Property::INVALID_INDEX)
708   {
709     mImpl->mMixColorIndex = mImpl->mRenderer.RegisterUniqueProperty(
710       Toolkit::Visual::Property::MIX_COLOR,
711       MIX_COLOR,
712       Vector3(mImpl->mMixColor));
713   }
714
715   mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, mImpl->mMixColor.a);
716
717   float preMultipliedAlpha = 0.0f;
718   if(IsPreMultipliedAlphaEnabled())
719   {
720     preMultipliedAlpha = 1.0f;
721   }
722   mImpl->mRenderer.RegisterProperty(PRE_MULTIPLIED_ALPHA_PROPERTY, preMultipliedAlpha);
723 }
724
725 void Visual::Base::SetMixColor(const Vector4& color)
726 {
727   mImpl->mMixColor = color;
728
729   if(mImpl->mRenderer)
730   {
731     mImpl->mRenderer.SetProperty(mImpl->mMixColorIndex, Vector3(color));
732     mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, color.a);
733   }
734 }
735
736 void Visual::Base::SetMixColor(const Vector3& color)
737 {
738   mImpl->mMixColor.r = color.r;
739   mImpl->mMixColor.g = color.g;
740   mImpl->mMixColor.b = color.b;
741
742   if(mImpl->mRenderer)
743   {
744     mImpl->mRenderer.SetProperty(mImpl->mMixColorIndex, color);
745   }
746 }
747
748 void Visual::Base::AddEventObserver(Visual::EventObserver& observer)
749 {
750   mImpl->mEventObserver = &observer;
751 }
752
753 void Visual::Base::RemoveEventObserver(Visual::EventObserver& observer)
754 {
755   mImpl->mEventObserver = NULL;
756 }
757
758 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
759 {
760   if(mImpl->mResourceStatus != resourceStatus)
761   {
762     mImpl->mResourceStatus = resourceStatus;
763
764     if(mImpl->mEventObserver)
765     {
766       // observer is currently a control impl
767       mImpl->mEventObserver->ResourceReady(*this);
768     }
769   }
770 }
771
772 bool Visual::Base::IsResourceReady() const
773 {
774   return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY);
775 }
776
777 bool Visual::Base::IsSynchronousLoadingRequired() const
778 {
779   return (mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING);
780 }
781
782 Toolkit::Visual::Type Visual::Base::GetType() const
783 {
784   return mImpl->mType;
785 }
786
787 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
788 {
789   return mImpl->mResourceStatus;
790 }
791
792 Visual::FittingMode Visual::Base::GetFittingMode() const
793 {
794   return mImpl->mFittingMode;
795 }
796
797 Visual::Base& Visual::Base::GetVisualObject()
798 {
799   return *this;
800 }
801
802 Renderer Visual::Base::GetRenderer()
803 {
804   return mImpl->mRenderer;
805 }
806
807 Property::Index Visual::Base::GetPropertyIndex(Property::Key key)
808 {
809   Property::Index index = mImpl->mRenderer.GetPropertyIndex(key);
810
811   if(index == Property::INVALID_INDEX)
812   {
813     // Is it a shader property?
814     Shader shader = mImpl->mRenderer.GetShader();
815     index         = shader.GetPropertyIndex(key);
816     if(index != Property::INVALID_INDEX)
817     {
818       // Yes - we should register it in the Renderer so it can be set / animated
819       // independently, as shaders are shared across multiple renderers.
820       std::string     keyName;
821       Property::Index keyIndex(Property::INVALID_KEY);
822       if(key.type == Property::Key::INDEX)
823       {
824         keyName  = shader.GetPropertyName(index);
825         keyIndex = key.indexKey;
826       }
827       else
828       {
829         keyName = key.stringKey;
830         // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
831       }
832       Property::Value value = shader.GetProperty(index);
833       index                 = mImpl->mRenderer.RegisterProperty(keyIndex, keyName, value);
834     }
835   }
836   return index;
837 }
838
839 void Visual::Base::SetupTransition(
840   Dali::Animation&                    transition,
841   Internal::TransitionData::Animator& animator,
842   Property::Index                     index,
843   Property::Value&                    initialValue,
844   Property::Value&                    targetValue)
845 {
846   if(index != Property::INVALID_INDEX)
847   {
848     if(mImpl->mRenderer)
849     {
850       if(animator.animate == false)
851       {
852         mImpl->mRenderer.SetProperty(index, targetValue);
853       }
854       else
855       {
856         if(animator.initialValue.GetType() != Property::NONE)
857         {
858           mImpl->mRenderer.SetProperty(index, initialValue);
859         }
860
861         if(!transition)
862         {
863           transition = Dali::Animation::New(0.1f);
864         }
865
866         transition.AnimateTo(Property(mImpl->mRenderer, index),
867                              targetValue,
868                              animator.alphaFunction,
869                              TimePeriod(animator.timePeriodDelay,
870                                         animator.timePeriodDuration));
871       }
872     }
873   }
874 }
875
876 void Visual::Base::AnimateProperty(
877   Dali::Animation&                    transition,
878   Internal::TransitionData::Animator& animator)
879 {
880 #if defined(DEBUG_ENABLED)
881   {
882     std::ostringstream oss;
883     oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
884     DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, oss.str().c_str());
885   }
886 #endif
887
888   if(animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
889      animator.propertyKey == MIX_COLOR ||
890      (mImpl->mType == Toolkit::Visual::COLOR &&
891       animator.propertyKey == ColorVisual::Property::MIX_COLOR) ||
892      (mImpl->mType == Toolkit::Visual::PRIMITIVE &&
893       animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR))
894   {
895     AnimateMixColorProperty(transition, animator);
896   }
897   else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
898           animator.propertyKey == OPACITY)
899   {
900     AnimateOpacityProperty(transition, animator);
901   }
902   else if(mImpl->mRenderer)
903   {
904     AnimateRendererProperty(transition, animator);
905   }
906 }
907
908 void Visual::Base::AnimateOpacityProperty(
909   Dali::Animation&                    transition,
910   Internal::TransitionData::Animator& animator)
911 {
912   float targetOpacity;
913   if(animator.targetValue.Get(targetOpacity))
914   {
915     mImpl->mMixColor.a = targetOpacity;
916   }
917
918   SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue);
919 }
920
921 void Visual::Base::AnimateRendererProperty(
922   Dali::Animation&                    transition,
923   Internal::TransitionData::Animator& animator)
924 {
925   Property::Index index = GetPropertyIndex(animator.propertyKey);
926   if(index != Property::INVALID_INDEX)
927   {
928     if(animator.targetValue.GetType() != Property::NONE)
929     {
930       // Try writing target value into transform property map
931       // if it's not a valid key, then it won't alter mTransform
932       Property::Map map;
933       if(animator.propertyKey.type == Property::Key::INDEX)
934       {
935         map.Add(animator.propertyKey.indexKey, animator.targetValue);
936       }
937       else
938       {
939         map.Add(animator.propertyKey.stringKey, animator.targetValue);
940       }
941
942       mImpl->mTransform.UpdatePropertyMap(map);
943     }
944
945     SetupTransition(transition, animator, index, animator.initialValue, animator.targetValue);
946   }
947 }
948
949 void Visual::Base::AnimateMixColorProperty(
950   Dali::Animation&                    transition,
951   Internal::TransitionData::Animator& animator)
952 {
953   Property::Index index          = mImpl->mMixColorIndex;
954   bool            animateOpacity = false;
955
956   Property::Value initialOpacity;
957   Property::Value targetOpacity;
958   Property::Value initialMixColor;
959   Property::Value targetMixColor;
960
961   if(index != Property::INVALID_INDEX)
962   {
963     Vector4 initialColor;
964     if(animator.initialValue.Get(initialColor))
965     {
966       if(animator.initialValue.GetType() == Property::VECTOR4)
967       {
968         // if there is an initial color specifying alpha, test it
969         initialOpacity = initialColor.a;
970       }
971       initialMixColor = Vector3(initialColor);
972     }
973
974     // Set target value into data store
975     if(animator.targetValue.GetType() != Property::NONE)
976     {
977       Vector4 mixColor;
978       animator.targetValue.Get(mixColor);
979       if(animator.targetValue.GetType() == Property::VECTOR4)
980       {
981         mImpl->mMixColor.a = mixColor.a;
982         targetOpacity      = mixColor.a;
983         animateOpacity     = true;
984       }
985
986       mImpl->mMixColor.r = mixColor.r;
987       mImpl->mMixColor.g = mixColor.g;
988       mImpl->mMixColor.b = mixColor.b;
989       targetMixColor     = Vector3(mixColor);
990     }
991
992     SetupTransition(transition, animator, index, initialMixColor, targetMixColor);
993     if(animateOpacity)
994     {
995       SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity);
996     }
997   }
998 }
999
1000 Dali::Property Visual::Base::GetPropertyObject(Dali::Property::Key key)
1001 {
1002   if(!mImpl->mRenderer)
1003   {
1004     Handle handle;
1005     return Dali::Property(handle, Property::INVALID_INDEX);
1006   }
1007
1008   // Mix color or opacity cases
1009   if(key.type == Property::Key::INDEX)
1010   {
1011     if(key.indexKey == Toolkit::Visual::Property::MIX_COLOR || (mImpl->mType == Toolkit::Visual::COLOR && key.indexKey == ColorVisual::Property::MIX_COLOR) || (mImpl->mType == Toolkit::Visual::PRIMITIVE && key.indexKey == PrimitiveVisual::Property::MIX_COLOR))
1012     {
1013       return Dali::Property(mImpl->mRenderer, mImpl->mMixColorIndex);
1014     }
1015     else if(key.indexKey == Toolkit::Visual::Property::OPACITY)
1016     {
1017       return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1018     }
1019     else if(key.indexKey == Toolkit::Visual::Transform::Property::OFFSET)
1020     {
1021       return Dali::Property(mImpl->mRenderer, OFFSET);
1022     }
1023     else if(key.indexKey == Toolkit::Visual::Transform::Property::SIZE)
1024     {
1025       return Dali::Property(mImpl->mRenderer, SIZE);
1026     }
1027   }
1028   else
1029   {
1030     if(key.stringKey == MIX_COLOR)
1031     {
1032       return Dali::Property(mImpl->mRenderer, mImpl->mMixColorIndex);
1033     }
1034     else if(key.stringKey == OPACITY)
1035     {
1036       return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1037     }
1038     else if(key.stringKey == OFFSET)
1039     {
1040       return Dali::Property(mImpl->mRenderer, OFFSET);
1041     }
1042     else if(key.stringKey == SIZE)
1043     {
1044       return Dali::Property(mImpl->mRenderer, SIZE);
1045     }
1046   }
1047
1048   // Other cases
1049   Property::Index index = GetPropertyIndex(key);
1050   if(index == Property::INVALID_INDEX)
1051   {
1052     if(IsTypeAvailableForBorderline(mImpl->mType) &&
1053        ((key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_WIDTH) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_WIDTH) ||
1054         (key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_COLOR) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_COLOR) ||
1055         (key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_OFFSET) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_OFFSET)))
1056     {
1057       mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
1058
1059       // Register borderline properties
1060       mImpl->mBorderlineWidthIndex  = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
1061       mImpl->mBorderlineColorIndex  = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
1062       mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_OFFSET, BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
1063
1064       // Borderline is animated now. we always have to use borderline feature.
1065       mImpl->mAlwaysUsingBorderline = true;
1066
1067       index = mImpl->mRenderer.GetPropertyIndex(key);
1068
1069       // Change shader
1070       UpdateShader();
1071     }
1072     else if(IsTypeAvailableForCornerRadius(mImpl->mType) && ((key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::CORNER_RADIUS) || (key.type == Property::Key::STRING && key.stringKey == CORNER_RADIUS)))
1073     {
1074       // Register CORNER_RADIUS property
1075       mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
1076       mImpl->mRenderer.RegisterUniqueProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
1077
1078       // CornerRadius is animated now. we always have to use corner radius feature.
1079       mImpl->mAlwaysUsingCornerRadius = true;
1080
1081       if(!IsBorderlineRequired())
1082       {
1083         // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
1084         mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
1085       }
1086
1087       index = mImpl->mCornerRadiusIndex;
1088
1089       // Change shader
1090       UpdateShader();
1091     }
1092     else
1093     {
1094       // We can't find the property in the base class.
1095       // Request to child class
1096       return OnGetPropertyObject(key);
1097     }
1098   }
1099
1100   return Dali::Property(mImpl->mRenderer, index);
1101 }
1102
1103 } // namespace Internal
1104
1105 } // namespace Toolkit
1106
1107 } // namespace Dali