2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "visual-base-impl.h"
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 #include <dali/public-api/rendering/visual-renderer.h>
29 #include <dali-toolkit/devel-api/visuals/visual-actions-devel.h>
30 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
31 #include <dali-toolkit/internal/helpers/property-helper.h>
32 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
33 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
34 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
35 #include <dali-toolkit/public-api/visuals/primitive-visual-properties.h>
36 #include <dali-toolkit/public-api/visuals/visual-properties.h>
40 #if defined(DEBUG_ENABLED)
41 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VISUAL_BASE");
44 // visual string constants contains OFFSET_SIZE_MODE instead
45 const char* const OFFSET_POLICY("offsetPolicy");
46 const char* const SIZE_POLICY("sizePolicy");
58 DALI_ENUM_TO_STRING_TABLE_BEGIN(VISUAL_FITTING_MODE)
59 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_KEEP_ASPECT_RATIO)
60 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FILL)
61 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, OVER_FIT_KEEP_ASPECT_RATIO)
62 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, CENTER)
63 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_WIDTH)
64 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_HEIGHT)
65 DALI_ENUM_TO_STRING_TABLE_END(VISUAL_FITTING_MODE)
68 * @brief Check whether this visual type can use corner radius feature or not.
69 * @param type VisualType that want to checkup
70 * @return true if type can use corner radius feature.
72 static bool IsTypeAvailableForCornerRadius(Toolkit::Visual::Type type)
74 switch(static_cast<Toolkit::DevelVisual::Type>(type))
76 case Toolkit::Visual::Type::COLOR:
77 case Toolkit::Visual::Type::GRADIENT:
78 case Toolkit::Visual::Type::IMAGE:
79 case Toolkit::Visual::Type::SVG:
80 case Toolkit::Visual::Type::ANIMATED_IMAGE:
81 case Toolkit::DevelVisual::Type::ANIMATED_VECTOR_IMAGE:
93 * @brief Check whether this visual type can use borderline feature or not.
94 * @param type VisualType that want to checkup
95 * @return true if type can use borderline feature.
97 static bool IsTypeAvailableForBorderline(Toolkit::Visual::Type type)
99 switch(static_cast<Toolkit::DevelVisual::Type>(type))
101 case Toolkit::Visual::Type::COLOR:
102 case Toolkit::Visual::Type::GRADIENT:
103 case Toolkit::Visual::Type::IMAGE:
104 case Toolkit::Visual::Type::SVG:
105 case Toolkit::Visual::Type::ANIMATED_IMAGE:
106 case Toolkit::DevelVisual::Type::ANIMATED_VECTOR_IMAGE:
119 Visual::Base::Base(VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type)
120 : mImpl(new Impl(fittingMode, type)),
121 mFactoryCache(factoryCache)
125 Visual::Base::~Base()
130 void Visual::Base::Initialize()
132 // The Renderer should be created inside derived class here.
139 if(IsRoundedCornerRequired())
141 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
142 mImpl->mRenderer.RegisterUniqueProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
144 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
146 if(IsBorderlineRequired())
148 mImpl->mBorderlineWidthIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
149 mImpl->mBorderlineColorIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
150 mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_OFFSET, BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
152 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
157 void Visual::Base::SetCustomShader(const Property::Map& shaderMap)
159 if(mImpl->mCustomShader)
161 mImpl->mCustomShader->SetPropertyMap(shaderMap);
165 mImpl->mCustomShader = new Impl::CustomShader(shaderMap);
168 // Let derived class know
172 void Visual::Base::SetProperties(const Property::Map& propertyMap)
174 bool needUpdateShader = false;
175 for(size_t i = 0; i < propertyMap.Count(); ++i)
177 const KeyValuePair& pair = propertyMap.GetKeyValue(i);
178 const Property::Key& key = pair.first;
179 const Property::Value& value = pair.second;
181 Property::Key matchKey = key;
182 if(matchKey.type == Property::Key::STRING)
184 if(matchKey == CUSTOM_SHADER)
186 matchKey = Property::Key(Toolkit::Visual::Property::SHADER);
188 else if(matchKey == TRANSFORM)
190 matchKey = Property::Key(Toolkit::Visual::Property::TRANSFORM);
192 else if(matchKey == PREMULTIPLIED_ALPHA)
194 matchKey = Property::Key(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA);
196 else if(matchKey == MIX_COLOR)
198 matchKey = Property::Key(Toolkit::Visual::Property::MIX_COLOR);
200 else if(matchKey == OPACITY)
202 matchKey = Property::Key(Toolkit::Visual::Property::OPACITY);
204 else if(matchKey == VISUAL_FITTING_MODE)
206 matchKey = Property::Key(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE);
208 else if(matchKey == BORDERLINE_WIDTH)
210 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH);
212 else if(matchKey == BORDERLINE_COLOR)
214 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_COLOR);
216 else if(matchKey == BORDERLINE_OFFSET)
218 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET);
220 else if(matchKey == CORNER_RADIUS)
222 matchKey = Property::Key(Toolkit::DevelVisual::Property::CORNER_RADIUS);
224 else if(matchKey == CORNER_RADIUS_POLICY)
226 matchKey = Property::Key(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY);
230 switch(matchKey.indexKey)
232 case Toolkit::Visual::Property::SHADER:
234 Property::Map shaderMap;
235 if(value.Get(shaderMap))
237 SetCustomShader(shaderMap);
242 case Toolkit::Visual::Property::TRANSFORM:
247 mImpl->mTransform.SetPropertyMap(map);
252 case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
254 bool premultipliedAlpha = false;
255 if(value.Get(premultipliedAlpha))
257 EnablePreMultipliedAlpha(premultipliedAlpha);
262 case Toolkit::Visual::Property::MIX_COLOR:
265 if(value.Get(mixColor))
267 if(value.GetType() == Property::VECTOR4)
269 SetMixColor(mixColor);
273 Vector3 mixColor3(mixColor);
274 SetMixColor(mixColor3);
279 case Toolkit::Visual::Property::OPACITY:
282 if(value.Get(opacity))
284 mImpl->mMixColor.a = opacity;
285 SetMixColor(mImpl->mMixColor);
289 case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
291 Scripting::GetEnumerationProperty<Visual::FittingMode>(
292 value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode);
295 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
300 mImpl->mBorderlineWidth = width;
303 if(mImpl->mBorderlineWidthIndex != Property::INVALID_INDEX)
305 mImpl->mRenderer.SetProperty(mImpl->mBorderlineWidthIndex, mImpl->mBorderlineWidth);
307 else if(DALI_UNLIKELY(mImpl->mRenderer && IsBorderlineRequired()))
309 // Unusual case. SetProperty called after OnInitialize().
310 // Assume that DoAction call UPDATE_PROPERTY.
311 // We must regist properies into renderer, and update shader.
313 // Borderline added by this action. Register property to renderer.
314 mImpl->mBorderlineWidthIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
315 mImpl->mBorderlineColorIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
316 mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_OFFSET, BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
318 // Make Blend mode ON_WITHOUT_CULL for transparent mix color.
319 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
321 // Change the shader must not be occured many times. we always have to use borderline feature.
322 mImpl->mAlwaysUsingBorderline = true;
325 needUpdateShader = true;
329 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
334 mImpl->mBorderlineColor = color;
337 if(mImpl->mBorderlineColorIndex != Property::INVALID_INDEX)
339 mImpl->mRenderer.SetProperty(mImpl->mBorderlineColorIndex, mImpl->mBorderlineColor);
343 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
346 if(value.Get(offset))
348 mImpl->mBorderlineOffset = offset;
351 if(mImpl->mBorderlineOffsetIndex != Property::INVALID_INDEX)
353 mImpl->mRenderer.SetProperty(mImpl->mBorderlineOffsetIndex, mImpl->mBorderlineOffset);
357 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
359 if(value.GetType() == Property::VECTOR4)
361 // If CORNER_RADIUS Property is Vector4,
362 // Each values mean the radius of
363 // (top-left, top-right, bottom-right, bottom-left)
365 if(value.Get(radius))
367 mImpl->mCornerRadius = radius;
372 // If CORNER_RADIUS Property is float,
373 // Every corner radius have same value
375 if(value.Get(radius))
377 mImpl->mCornerRadius = Vector4(radius, radius, radius, radius);
381 if(mImpl->mCornerRadiusIndex != Property::INVALID_INDEX)
383 mImpl->mRenderer.SetProperty(mImpl->mCornerRadiusIndex, mImpl->mCornerRadius);
385 else if(DALI_UNLIKELY(mImpl->mRenderer && IsRoundedCornerRequired()))
387 // Unusual case. SetProperty called after OnInitialize().
388 // Assume that DoAction call UPDATE_PROPERTY.
389 // We must regist properies into renderer, and update shader.
391 // CornerRadius added by this action. Regist property to renderer.
392 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
393 mImpl->mRenderer.RegisterUniqueProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
395 // Change the shader must not be occured many times. we always have to use corner radius feature.
396 mImpl->mAlwaysUsingCornerRadius = true;
398 if(!IsBorderlineRequired())
400 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
401 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
405 needUpdateShader = true;
409 case Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY:
412 if(value.Get(policy))
416 case Toolkit::Visual::Transform::Policy::RELATIVE:
417 case Toolkit::Visual::Transform::Policy::ABSOLUTE:
419 mImpl->mCornerRadiusPolicy = policy;
420 if(DALI_UNLIKELY(mImpl->mRenderer && mImpl->mCornerRadiusIndex != Property::INVALID_INDEX))
422 // Unusual case. SetProperty called after OnInitialize().
423 // Assume that DoAction call UPDATE_PROPERTY.
424 // We must update properies result into renderer
425 // Note : mImpl->mCornerRadiusIndex is not INVALID_INDEX.
426 // So CornerRadiusPolicy property is already registed.
427 mImpl->mRenderer.SetProperty(mImpl->mRenderer.GetPropertyIndex(CORNER_RADIUS_POLICY), mImpl->mCornerRadiusPolicy);
433 DALI_LOG_ERROR("Unsupported policy: %d\n", policy);
443 DoSetProperties(propertyMap);
445 if(DALI_UNLIKELY(needUpdateShader))
451 void Visual::Base::SetTransformAndSize(const Property::Map& transform, Size controlSize)
453 mImpl->mControlSize = controlSize;
454 mImpl->mTransform.UpdatePropertyMap(transform);
456 #if defined(DEBUG_ENABLED)
457 std::ostringstream oss;
459 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);
465 void Visual::Base::SetName(const std::string& name)
470 const std::string& Visual::Base::GetName() const
475 float Visual::Base::GetHeightForWidth(float width)
477 float aspectCorrectedHeight = 0.f;
479 GetNaturalSize(naturalSize);
480 if(naturalSize.width)
482 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
484 return aspectCorrectedHeight;
487 float Visual::Base::GetWidthForHeight(float height)
489 float aspectCorrectedWidth = 0.f;
491 GetNaturalSize(naturalSize);
492 if(naturalSize.height > 0.0f)
494 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
496 return aspectCorrectedWidth;
499 void Visual::Base::GetNaturalSize(Vector2& naturalSize)
501 naturalSize = Vector2::ZERO;
504 void Visual::Base::DoAction(const Property::Index actionId, const Property::Value attributes)
506 OnDoAction(actionId, attributes);
508 // Check if action is valid for this visual type and perform action if possible
511 case DevelVisual::Action::UPDATE_PROPERTY:
513 const Property::Map* map = attributes.GetMap();
523 void Visual::Base::SetDepthIndex(int index)
525 mImpl->mDepthIndex = index;
528 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
532 int Visual::Base::GetDepthIndex() const
534 return mImpl->mDepthIndex;
537 void Visual::Base::SetOnScene(Actor& actor)
541 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
542 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
547 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
548 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
551 mImpl->mFlags |= Impl::IS_ON_SCENE;
555 void Visual::Base::SetOffScene(Actor& actor)
559 DoSetOffScene(actor);
560 mImpl->mFlags &= ~Impl::IS_ON_SCENE;
564 void Visual::Base::CreatePropertyMap(Property::Map& map) const
568 // Update values from Renderer
569 mImpl->mMixColor = mImpl->mRenderer.GetProperty<Vector3>(VisualRenderer::Property::VISUAL_MIX_COLOR);
570 mImpl->mMixColor.a = mImpl->mRenderer.GetProperty<float>(DevelRenderer::Property::OPACITY);
572 mImpl->mTransform.mOffset = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_OFFSET);
573 mImpl->mTransform.mSize = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_SIZE);
575 if(mImpl->mCornerRadiusIndex != Property::INVALID_INDEX)
577 mImpl->mCornerRadius = mImpl->mRenderer.GetProperty<Vector4>(mImpl->mCornerRadiusIndex);
579 if(mImpl->mBorderlineWidthIndex != Property::INVALID_INDEX)
581 mImpl->mBorderlineWidth = mImpl->mRenderer.GetProperty<float>(mImpl->mBorderlineWidthIndex);
583 if(mImpl->mBorderlineColorIndex != Property::INVALID_INDEX)
585 mImpl->mBorderlineColor = mImpl->mRenderer.GetProperty<Vector4>(mImpl->mBorderlineColorIndex);
587 if(mImpl->mBorderlineOffsetIndex != Property::INVALID_INDEX)
589 mImpl->mBorderlineOffset = mImpl->mRenderer.GetProperty<float>(mImpl->mBorderlineOffsetIndex);
593 DoCreatePropertyMap(map);
595 if(mImpl->mCustomShader)
597 mImpl->mCustomShader->CreatePropertyMap(map);
600 Property::Map transform;
601 mImpl->mTransform.GetPropertyMap(transform);
602 map.Insert(Toolkit::Visual::Property::TRANSFORM, transform);
604 bool premultipliedAlpha(IsPreMultipliedAlphaEnabled());
605 map.Insert(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha);
607 // Note, Color and Primitive will also insert their own mix color into the map
608 // which is ok, because they have a different key value range, but uses same cached value anyway.
609 map.Insert(Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor); // vec4
610 map.Insert(Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a);
612 auto fittingModeString = Scripting::GetLinearEnumerationName<FittingMode>(
613 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT);
614 map.Insert(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString);
616 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
617 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
618 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
620 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius);
621 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, static_cast<int>(mImpl->mCornerRadiusPolicy));
624 void Visual::Base::CreateInstancePropertyMap(Property::Map& map) const
626 DoCreateInstancePropertyMap(map);
628 if(mImpl->mCustomShader)
630 mImpl->mCustomShader->CreatePropertyMap(map);
634 void Visual::Base::EnablePreMultipliedAlpha(bool preMultiplied)
638 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
642 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
647 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
648 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultiplied);
652 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
654 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
657 void Visual::Base::DoSetOffScene(Actor& actor)
659 actor.RemoveRenderer(mImpl->mRenderer);
662 bool Visual::Base::IsOnScene() const
664 return mImpl->mFlags & Impl::IS_ON_SCENE;
667 bool Visual::Base::IsRoundedCornerRequired() const
669 // If VisualType doesn't support rounded corner, always return false.
670 if(IsTypeAvailableForCornerRadius(mImpl->mType))
672 if(mImpl->mRenderer && mImpl->mCornerRadiusIndex != Property::INVALID_INDEX)
674 // Update values from Renderer
675 mImpl->mCornerRadius = mImpl->mRenderer.GetProperty<Vector4>(mImpl->mCornerRadiusIndex);
677 return !(mImpl->mCornerRadius == Vector4::ZERO) || mImpl->mAlwaysUsingCornerRadius;
682 bool Visual::Base::IsBorderlineRequired() const
684 // If VisualType doesn't support borderline, always return false.
685 if(IsTypeAvailableForBorderline(mImpl->mType))
687 if(mImpl->mRenderer && mImpl->mBorderlineWidthIndex != Property::INVALID_INDEX)
689 // Update values from Renderer
690 mImpl->mBorderlineWidth = mImpl->mRenderer.GetProperty<float>(mImpl->mBorderlineWidthIndex);
692 return !EqualsZero(mImpl->mBorderlineWidth) || mImpl->mAlwaysUsingBorderline;
697 void Visual::Base::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
699 // May be overriden by derived class
702 void Visual::Base::RegisterMixColor()
706 // All visual renderers now use same mix color / opacity properties.
707 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
708 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, mImpl->mMixColor.a);
710 float preMultipliedAlpha = 0.0f;
711 if(IsPreMultipliedAlphaEnabled())
713 preMultipliedAlpha = 1.0f;
715 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultipliedAlpha);
719 void Visual::Base::SetMixColor(const Vector4& color)
721 mImpl->mMixColor = color;
725 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(color));
726 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, color.a);
730 void Visual::Base::SetMixColor(const Vector3& color)
732 mImpl->mMixColor.r = color.r;
733 mImpl->mMixColor.g = color.g;
734 mImpl->mMixColor.b = color.b;
738 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, color);
742 void Visual::Base::AddEventObserver(Visual::EventObserver& observer)
744 mImpl->mEventObserver = &observer;
747 void Visual::Base::RemoveEventObserver(Visual::EventObserver& observer)
749 mImpl->mEventObserver = NULL;
752 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
754 if(mImpl->mResourceStatus != resourceStatus)
756 mImpl->mResourceStatus = resourceStatus;
758 if(mImpl->mEventObserver)
760 // observer is currently a control impl
761 mImpl->mEventObserver->ResourceReady(*this);
766 bool Visual::Base::IsResourceReady() const
768 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
769 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
772 bool Visual::Base::IsSynchronousLoadingRequired() const
774 return (mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING);
777 Toolkit::Visual::Type Visual::Base::GetType() const
782 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
784 return mImpl->mResourceStatus;
787 Visual::FittingMode Visual::Base::GetFittingMode() const
789 return mImpl->mFittingMode;
792 Visual::Base& Visual::Base::GetVisualObject()
797 Renderer Visual::Base::GetRenderer()
799 return mImpl->mRenderer;
802 Property::Index Visual::Base::GetIntKey(Property::Key key)
804 if(key.type == Property::Key::INDEX)
809 if(key.stringKey == ANCHOR_POINT)
811 return Toolkit::Visual::Transform::Property::ANCHOR_POINT;
813 else if(key.stringKey == EXTRA_SIZE)
815 return Toolkit::DevelVisual::Transform::Property::EXTRA_SIZE;
817 else if(key.stringKey == MIX_COLOR)
819 return Toolkit::Visual::Property::MIX_COLOR;
821 else if(key.stringKey == OPACITY)
823 return Toolkit::Visual::Property::OPACITY;
825 else if(key.stringKey == OFFSET)
827 return Toolkit::Visual::Transform::Property::OFFSET;
829 else if(key.stringKey == OFFSET_POLICY)
831 return Toolkit::Visual::Transform::Property::OFFSET_POLICY;
833 else if(key.stringKey == ORIGIN)
835 return Toolkit::Visual::Transform::Property::ORIGIN;
837 else if(key.stringKey == PREMULTIPLIED_ALPHA)
839 return Toolkit::Visual::Property::PREMULTIPLIED_ALPHA;
841 else if(key.stringKey == CUSTOM_SHADER)
843 return Toolkit::Visual::Property::SHADER;
845 else if(key.stringKey == SIZE)
847 return Toolkit::Visual::Transform::Property::SIZE;
849 else if(key.stringKey == SIZE_POLICY)
851 return Toolkit::Visual::Transform::Property::SIZE_POLICY;
853 else if(key.stringKey == TRANSFORM)
855 return Toolkit::Visual::Property::TRANSFORM;
857 else if(key.stringKey == VISUAL_FITTING_MODE)
859 return Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE;
861 else if(key.stringKey == CORNER_RADIUS)
863 return Toolkit::DevelVisual::Property::CORNER_RADIUS;
865 else if(key.stringKey == CORNER_RADIUS_POLICY)
867 return Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY;
869 else if(key.stringKey == BORDERLINE_WIDTH)
871 return Toolkit::DevelVisual::Property::BORDERLINE_WIDTH;
873 else if(key.stringKey == BORDERLINE_COLOR)
875 return Toolkit::DevelVisual::Property::BORDERLINE_COLOR;
877 else if(key.stringKey == BORDERLINE_OFFSET)
879 return Toolkit::DevelVisual::Property::BORDERLINE_OFFSET;
882 return Property::INVALID_INDEX;
885 Property::Index Visual::Base::GetPropertyIndex(Property::Key key)
887 switch(GetIntKey(key))
889 case Dali::Toolkit::Visual::Transform::Property::OFFSET:
891 return VisualRenderer::Property::TRANSFORM_OFFSET;
893 case Dali::Toolkit::Visual::Transform::Property::SIZE:
895 return VisualRenderer::Property::TRANSFORM_SIZE;
897 case Dali::Toolkit::Visual::Transform::Property::ORIGIN:
899 return VisualRenderer::Property::TRANSFORM_ORIGIN;
901 case Dali::Toolkit::Visual::Transform::Property::ANCHOR_POINT:
903 return VisualRenderer::Property::TRANSFORM_ANCHOR_POINT;
905 case Dali::Toolkit::Visual::Property::MIX_COLOR:
907 return VisualRenderer::Property::VISUAL_MIX_COLOR;
909 case Dali::Toolkit::Visual::Property::OPACITY:
911 return DevelRenderer::Property::OPACITY;
913 case Dali::Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
915 return VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA;
919 Property::Index index = mImpl->mRenderer.GetPropertyIndex(key);
921 if(index == Property::INVALID_INDEX)
923 // Is it a shader property?
924 Shader shader = mImpl->mRenderer.GetShader();
925 index = shader.GetPropertyIndex(key);
926 if(index != Property::INVALID_INDEX)
928 // Yes - we should register it in the Renderer so it can be set / animated
929 // independently, as shaders are shared across multiple renderers.
931 Property::Index keyIndex(Property::INVALID_KEY);
932 if(key.type == Property::Key::INDEX)
934 keyName = shader.GetPropertyName(index);
935 keyIndex = key.indexKey;
939 keyName = key.stringKey;
940 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
942 Property::Value value = shader.GetProperty(index);
944 // We already know that mRenderer didn't have property. So we can assume that it is unique.
945 index = mImpl->mRenderer.RegisterUniqueProperty(keyIndex, keyName, value);
951 void Visual::Base::SetupTransition(
952 Dali::Animation& transition,
953 Internal::TransitionData::Animator& animator,
954 Property::Index index,
955 Property::Value& initialValue,
956 Property::Value& targetValue)
958 if(index != Property::INVALID_INDEX)
962 if(animator.animate == false)
964 mImpl->mRenderer.SetProperty(index, targetValue);
968 if(animator.initialValue.GetType() != Property::NONE)
970 mImpl->mRenderer.SetProperty(index, initialValue);
975 transition = Dali::Animation::New(0.1f);
978 transition.AnimateTo(Property(mImpl->mRenderer, index),
980 animator.alphaFunction,
981 TimePeriod(animator.timePeriodDelay,
982 animator.timePeriodDuration));
988 void Visual::Base::AnimateProperty(
989 Dali::Animation& transition,
990 Internal::TransitionData::Animator& animator)
992 #if defined(DEBUG_ENABLED)
994 std::ostringstream oss;
995 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
996 DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, oss.str().c_str());
1000 if(animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
1001 animator.propertyKey == MIX_COLOR ||
1002 (mImpl->mType == Toolkit::Visual::COLOR &&
1003 animator.propertyKey == ColorVisual::Property::MIX_COLOR) ||
1004 (mImpl->mType == Toolkit::Visual::PRIMITIVE &&
1005 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR))
1007 AnimateMixColorProperty(transition, animator);
1009 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
1010 animator.propertyKey == OPACITY)
1012 AnimateOpacityProperty(transition, animator);
1014 else if(mImpl->mRenderer)
1016 AnimateRendererProperty(transition, animator);
1020 void Visual::Base::AnimateOpacityProperty(
1021 Dali::Animation& transition,
1022 Internal::TransitionData::Animator& animator)
1024 float targetOpacity;
1025 if(animator.targetValue.Get(targetOpacity))
1027 mImpl->mMixColor.a = targetOpacity;
1030 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue);
1033 void Visual::Base::AnimateRendererProperty(
1034 Dali::Animation& transition,
1035 Internal::TransitionData::Animator& animator)
1037 // Get actual renderer index (will convert transform keys into visualproperty indices)
1038 Property::Index index = GetPropertyIndex(animator.propertyKey);
1040 if(index != Property::INVALID_INDEX)
1042 if(animator.targetValue.GetType() != Property::NONE)
1044 // Try writing target value into transform property map
1045 // if it's not a valid key, then it won't alter mTransform
1047 if(animator.propertyKey.type == Property::Key::INDEX)
1049 map.Add(animator.propertyKey.indexKey, animator.targetValue);
1053 map.Add(animator.propertyKey.stringKey, animator.targetValue);
1056 mImpl->mTransform.UpdatePropertyMap(map);
1058 SetupTransition(transition, animator, index, animator.initialValue, animator.targetValue);
1062 void Visual::Base::AnimateMixColorProperty(
1063 Dali::Animation& transition,
1064 Internal::TransitionData::Animator& animator)
1066 bool animateOpacity = false;
1068 Property::Value initialOpacity;
1069 Property::Value targetOpacity;
1070 Property::Value initialMixColor;
1071 Property::Value targetMixColor;
1073 Vector4 initialColor;
1074 if(animator.initialValue.Get(initialColor))
1076 if(animator.initialValue.GetType() == Property::VECTOR4)
1078 // if there is an initial color specifying alpha, test it
1079 initialOpacity = initialColor.a;
1081 initialMixColor = Vector3(initialColor);
1084 // Set target value into data store
1085 if(animator.targetValue.GetType() != Property::NONE)
1088 animator.targetValue.Get(mixColor);
1089 if(animator.targetValue.GetType() == Property::VECTOR4)
1091 mImpl->mMixColor.a = mixColor.a;
1092 targetOpacity = mixColor.a;
1093 animateOpacity = true;
1096 mImpl->mMixColor.r = mixColor.r;
1097 mImpl->mMixColor.g = mixColor.g;
1098 mImpl->mMixColor.b = mixColor.b;
1099 targetMixColor = Vector3(mixColor);
1102 SetupTransition(transition, animator, VisualRenderer::Property::VISUAL_MIX_COLOR, initialMixColor, targetMixColor);
1106 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity);
1110 Dali::Property Visual::Base::GetPropertyObject(Dali::Property::Key key)
1112 if(!mImpl->mRenderer)
1115 return Dali::Property(handle, Property::INVALID_INDEX);
1118 // Default animatable properties from VisualRenderer
1119 switch(GetIntKey(key))
1121 case Toolkit::Visual::Property::MIX_COLOR:
1123 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1125 case Toolkit::Visual::Property::OPACITY:
1127 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1129 case Toolkit::Visual::Transform::Property::OFFSET:
1131 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_OFFSET);
1133 case Toolkit::Visual::Transform::Property::SIZE:
1135 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_SIZE);
1139 if(key.type == Property::Key::INDEX &&
1140 ((mImpl->mType == Toolkit::Visual::COLOR && key.indexKey == ColorVisual::Property::MIX_COLOR) ||
1141 (mImpl->mType == Toolkit::Visual::PRIMITIVE && key.indexKey == PrimitiveVisual::Property::MIX_COLOR)))
1143 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1149 Property::Index index = GetPropertyIndex(key);
1150 if(index == Property::INVALID_INDEX)
1152 if(IsTypeAvailableForBorderline(mImpl->mType) &&
1153 ((key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_WIDTH) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_WIDTH) ||
1154 (key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_COLOR) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_COLOR) ||
1155 (key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_OFFSET) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_OFFSET)))
1157 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
1159 // Register borderline properties
1160 mImpl->mBorderlineWidthIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
1161 mImpl->mBorderlineColorIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
1162 mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_OFFSET, BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
1164 // Borderline is animated now. we always have to use borderline feature.
1165 mImpl->mAlwaysUsingBorderline = true;
1167 index = mImpl->mRenderer.GetPropertyIndex(key);
1172 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)))
1174 // Register CORNER_RADIUS property
1175 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
1176 mImpl->mRenderer.RegisterUniqueProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
1178 // CornerRadius is animated now. we always have to use corner radius feature.
1179 mImpl->mAlwaysUsingCornerRadius = true;
1181 if(!IsBorderlineRequired())
1183 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
1184 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
1187 index = mImpl->mCornerRadiusIndex;
1194 // We can't find the property in the base class.
1195 // Request to child class
1196 return OnGetPropertyObject(key);
1201 if(index == mImpl->mBorderlineWidthIndex ||
1202 index == mImpl->mBorderlineColorIndex ||
1203 index == mImpl->mBorderlineOffsetIndex)
1205 // Borderline is animated now. we always have to use borderline feature.
1206 mImpl->mAlwaysUsingBorderline = true;
1208 if(index == mImpl->mCornerRadiusIndex)
1210 // CornerRadius is animated now. we always have to use corner radius feature.
1211 mImpl->mAlwaysUsingCornerRadius = true;
1215 return Dali::Property(mImpl->mRenderer, index);
1218 } // namespace Internal
1220 } // namespace Toolkit