2 * Copyright (c) 2024 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/decorated-visual-renderer.h>
27 #include <dali/public-api/rendering/visual-renderer.h>
30 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
31 #include <dali-toolkit/devel-api/visuals/color-visual-properties-devel.h>
32 #include <dali-toolkit/devel-api/visuals/visual-actions-devel.h>
33 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
34 #include <dali-toolkit/internal/helpers/property-helper.h>
35 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
36 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
37 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
38 #include <dali-toolkit/public-api/visuals/primitive-visual-properties.h>
39 #include <dali-toolkit/public-api/visuals/visual-properties.h>
43 #if defined(DEBUG_ENABLED)
44 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VISUAL_BASE");
47 // visual string constants contains OFFSET_SIZE_MODE instead
48 const char* const OFFSET_POLICY("offsetPolicy");
49 const char* const SIZE_POLICY("sizePolicy");
61 DALI_ENUM_TO_STRING_TABLE_BEGIN(VISUAL_FITTING_MODE)
62 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_KEEP_ASPECT_RATIO)
63 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FILL)
64 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, OVER_FIT_KEEP_ASPECT_RATIO)
65 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, CENTER)
66 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_HEIGHT)
67 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_WIDTH)
68 DALI_ENUM_TO_STRING_TABLE_END(VISUAL_FITTING_MODE)
71 * @brief Check whether this visual type can use corner radius feature or not.
72 * @param type VisualType that want to checkup
73 * @return true if type can use corner radius feature.
75 static bool IsTypeAvailableForCornerRadius(Toolkit::Visual::Type type)
77 switch(static_cast<Toolkit::DevelVisual::Type>(type))
79 case Toolkit::Visual::Type::COLOR:
80 case Toolkit::Visual::Type::GRADIENT:
81 case Toolkit::Visual::Type::IMAGE:
82 case Toolkit::Visual::Type::SVG:
83 case Toolkit::Visual::Type::ANIMATED_IMAGE:
84 case Toolkit::DevelVisual::Type::ANIMATED_VECTOR_IMAGE:
96 * @brief Check whether this visual type can use borderline feature or not.
97 * @param type VisualType that want to checkup
98 * @return true if type can use borderline feature.
100 static bool IsTypeAvailableForBorderline(Toolkit::Visual::Type type)
102 switch(static_cast<Toolkit::DevelVisual::Type>(type))
104 case Toolkit::Visual::Type::COLOR:
105 case Toolkit::Visual::Type::GRADIENT:
106 case Toolkit::Visual::Type::IMAGE:
107 case Toolkit::Visual::Type::SVG:
108 case Toolkit::Visual::Type::ANIMATED_IMAGE:
109 case Toolkit::DevelVisual::Type::ANIMATED_VECTOR_IMAGE:
120 struct StringProperty
122 const char* const name;
123 Property::Index index;
125 StringProperty PROPERTY_NAME_INDEX_TABLE[] =
127 {CUSTOM_SHADER, Toolkit::Visual::Property::SHADER},
128 {TRANSFORM, Toolkit::Visual::Property::TRANSFORM},
129 {PREMULTIPLIED_ALPHA, Toolkit::Visual::Property::PREMULTIPLIED_ALPHA},
130 {MIX_COLOR, Toolkit::Visual::Property::MIX_COLOR},
131 {OPACITY, Toolkit::Visual::Property::OPACITY},
132 {VISUAL_FITTING_MODE, Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE},
133 {BORDERLINE_WIDTH, Toolkit::DevelVisual::Property::BORDERLINE_WIDTH},
134 {BORDERLINE_COLOR, Toolkit::DevelVisual::Property::BORDERLINE_COLOR},
135 {BORDERLINE_OFFSET, Toolkit::DevelVisual::Property::BORDERLINE_OFFSET},
136 {CORNER_RADIUS, Toolkit::DevelVisual::Property::CORNER_RADIUS},
137 {CORNER_RADIUS_POLICY, Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY},
139 const uint16_t PROPERTY_NAME_INDEX_TABLE_COUNT = sizeof(PROPERTY_NAME_INDEX_TABLE) / sizeof(PROPERTY_NAME_INDEX_TABLE[0]);
141 Property::Index GetVisualPropertyIndex(Property::Key key)
143 if(key.type == Property::Key::STRING)
145 for(auto tableId = 0u; tableId < PROPERTY_NAME_INDEX_TABLE_COUNT; ++tableId)
147 if(key == PROPERTY_NAME_INDEX_TABLE[tableId].name)
149 return PROPERTY_NAME_INDEX_TABLE[tableId].index;
159 Visual::Base::Base(VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type)
160 : mImpl(new Impl(fittingMode, type)),
161 mFactoryCache(factoryCache)
165 Visual::Base::~Base()
170 void Visual::Base::Initialize()
172 // The Renderer should be created inside derived class here.
178 RegisterDecoration();
180 if(IsBorderlineRequired())
182 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
184 else if(IsRoundedCornerRequired())
186 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
191 void Visual::Base::SetCustomShader(const Property::Map& shaderMap)
193 if(mImpl->mCustomShader)
195 mImpl->mCustomShader->SetPropertyMap(shaderMap);
199 mImpl->mCustomShader = new Impl::CustomShader(shaderMap);
202 // Let derived class know
206 void Visual::Base::SetProperties(const Property::Map& propertyMap)
208 bool needUpdateShader = false;
209 for(size_t i = 0; i < propertyMap.Count(); ++i)
211 const KeyValuePair& pair = propertyMap.GetKeyValue(i);
212 const Property::Key& key = pair.first;
213 const Property::Value& value = pair.second;
215 switch(GetVisualPropertyIndex(key))
217 case Toolkit::Visual::Property::SHADER:
219 Property::Map shaderMap;
220 if(value.Get(shaderMap))
222 SetCustomShader(shaderMap);
227 case Toolkit::Visual::Property::TRANSFORM:
232 mImpl->mTransform.SetPropertyMap(map);
237 case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
239 bool premultipliedAlpha = false;
240 if(value.Get(premultipliedAlpha))
242 EnablePreMultipliedAlpha(premultipliedAlpha);
247 case Toolkit::Visual::Property::MIX_COLOR:
250 if(value.Get(mixColor))
252 if(value.GetType() == Property::VECTOR4)
254 SetMixColor(mixColor);
258 Vector3 mixColor3(mixColor);
259 SetMixColor(mixColor3);
264 case Toolkit::Visual::Property::OPACITY:
267 if(value.Get(opacity))
269 mImpl->mMixColor.a = opacity;
270 SetMixColor(mImpl->mMixColor);
274 case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
276 Scripting::GetEnumerationProperty<Visual::FittingMode>(
277 value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode);
280 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
283 if(value.Get(width) && (mImpl->mDecorationData != nullptr || !Dali::EqualsZero(width)))
285 mImpl->SetBorderlineWidth(width);
288 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
290 // Unusual case. SetProperty called after OnInitialize().
291 // Assume that DoAction call UPDATE_PROPERTY.
292 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
293 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
295 // Check whether we must update shader.
296 if(!mImpl->mAlwaysUsingBorderline && IsBorderlineRequired())
298 // Required to change shader mean, we didn't setup BORDERLINE_COLOR and BORDERLINE_OFFSET into mRenderer before. Set property now.
299 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
300 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
302 // Make Blend mode ON_WITHOUT_CULL for transparent mix color.
303 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
305 // Change the shader must not be occured many times. we always have to use borderline feature.
306 mImpl->mAlwaysUsingBorderline = true;
309 if(!mImpl->mCustomShader)
311 needUpdateShader = true;
317 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
320 if(value.Get(color) && (mImpl->mDecorationData != nullptr || color != Vector4::ZERO))
322 mImpl->SetBorderlineColor(color);
325 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
327 // Unusual case. SetProperty called after OnInitialize().
328 // Assume that DoAction call UPDATE_PROPERTY.
329 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
333 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
336 if(value.Get(offset) && (mImpl->mDecorationData != nullptr || !Dali::EqualsZero(offset)))
338 mImpl->SetBorderlineOffset(offset);
341 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
343 // Unusual case. SetProperty called after OnInitialize().
344 // Assume that DoAction call UPDATE_PROPERTY.
345 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
349 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
351 if(value.GetType() == Property::VECTOR4)
353 // If CORNER_RADIUS Property is Vector4,
354 // Each values mean the radius of
355 // (top-left, top-right, bottom-right, bottom-left)
357 if(value.Get(radius) && (mImpl->mDecorationData != nullptr || radius != Vector4::ZERO))
359 mImpl->SetCornerRadius(radius);
364 // If CORNER_RADIUS Property is float,
365 // Every corner radius have same value
367 if(value.Get(radius) && (mImpl->mDecorationData != nullptr || !Dali::EqualsZero(radius)))
369 mImpl->SetCornerRadius(Vector4(radius, radius, radius, radius));
373 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForCornerRadius(mImpl->mType)))
375 // Unusual case. SetProperty called after OnInitialize().
376 // Assume that DoAction call UPDATE_PROPERTY.
377 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
378 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
380 // Check whether we must update shader.
381 if(!mImpl->mAlwaysUsingCornerRadius && IsRoundedCornerRequired())
383 // Required to change shader mean, we didn't setup CORNER_RADIUS_POLICY into mRenderer before. Set property now.
384 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
386 // Change the shader must not be occured many times. we always have to use corner radius feature.
387 mImpl->mAlwaysUsingCornerRadius = true;
389 if(!IsBorderlineRequired())
391 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
392 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
396 if(!mImpl->mCustomShader)
398 needUpdateShader = true;
405 case Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY:
408 if(value.Get(policy))
412 case Toolkit::Visual::Transform::Policy::RELATIVE:
413 case Toolkit::Visual::Transform::Policy::ABSOLUTE:
415 mImpl->SetCornerRadiusPolicy(policy);
416 if(DALI_UNLIKELY(mImpl->mRenderer))
418 // Unusual case. SetProperty called after OnInitialize().
419 // Assume that DoAction call UPDATE_PROPERTY.
420 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
426 DALI_LOG_ERROR("Unsupported policy: %d\n", policy);
436 DoSetProperties(propertyMap);
438 if(DALI_UNLIKELY(needUpdateShader))
444 void Visual::Base::SetTransformAndSize(const Property::Map& transform, Size controlSize)
446 mImpl->mControlSize = controlSize;
447 mImpl->mTransform.UpdatePropertyMap(transform);
449 #if defined(DEBUG_ENABLED)
450 std::ostringstream oss;
452 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);
458 void Visual::Base::SetName(const std::string& name)
463 const std::string& Visual::Base::GetName() const
468 float Visual::Base::GetHeightForWidth(float width)
470 float aspectCorrectedHeight = 0.f;
472 GetNaturalSize(naturalSize);
473 if(naturalSize.width > 0.0f)
475 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
477 return aspectCorrectedHeight;
480 float Visual::Base::GetWidthForHeight(float height)
482 float aspectCorrectedWidth = 0.f;
484 GetNaturalSize(naturalSize);
485 if(naturalSize.height > 0.0f)
487 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
489 return aspectCorrectedWidth;
492 void Visual::Base::GetNaturalSize(Vector2& naturalSize)
494 naturalSize = Vector2::ZERO;
497 void Visual::Base::DoAction(const Property::Index actionId, const Property::Value attributes)
499 OnDoAction(actionId, attributes);
501 // Check if action is valid for this visual type and perform action if possible
504 case DevelVisual::Action::UPDATE_PROPERTY:
506 const Property::Map* map = attributes.GetMap();
516 void Visual::Base::DoActionExtension(const Dali::Property::Index actionId, const Dali::Any attributes)
518 OnDoActionExtension(actionId, attributes);
521 void Visual::Base::SetDepthIndex(int index)
523 // Clamp input value as valid range.
524 Dali::ClampInPlace(index, static_cast<int>(Toolkit::DepthIndex::MINIMUM_DEPTH_INDEX), static_cast<int>(Toolkit::DepthIndex::MAXIMUM_DEPTH_INDEX));
526 if(mImpl->mDepthIndex != index)
528 mImpl->mDepthIndex = index;
531 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
536 int Visual::Base::GetDepthIndex() const
538 return mImpl->mDepthIndex;
541 void Visual::Base::SetOnScene(Actor& actor)
545 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
546 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
551 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
552 if(mImpl->mDepthIndex == static_cast<int>(DepthIndex::AUTO_INDEX))
554 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, static_cast<int>(DepthIndex::CONTENT));
558 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
562 mImpl->mFlags |= Impl::IS_ON_SCENE;
566 void Visual::Base::SetOffScene(Actor& actor)
570 DoSetOffScene(actor);
571 mImpl->mFlags &= ~Impl::IS_ON_SCENE;
575 void Visual::Base::CreatePropertyMap(Property::Map& map) const
579 // Update values from Renderer
580 mImpl->mMixColor = mImpl->mRenderer.GetProperty<Vector3>(VisualRenderer::Property::VISUAL_MIX_COLOR);
581 mImpl->mMixColor.a = mImpl->mRenderer.GetProperty<float>(DevelRenderer::Property::OPACITY);
583 mImpl->mTransform.mOffset = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_OFFSET);
584 mImpl->mTransform.mSize = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_SIZE);
586 if(IsRoundedCornerRequired())
588 mImpl->SetCornerRadius(mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::CORNER_RADIUS));
590 if(IsBorderlineRequired())
592 mImpl->SetBorderlineWidth(mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH));
593 mImpl->SetBorderlineColor(mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::BORDERLINE_COLOR));
594 mImpl->SetBorderlineOffset(mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET));
598 DoCreatePropertyMap(map);
600 if(mImpl->mCustomShader)
602 mImpl->mCustomShader->CreatePropertyMap(map);
605 Property::Map transform;
606 mImpl->mTransform.GetPropertyMap(transform);
607 map.Insert(Toolkit::Visual::Property::TRANSFORM, transform);
609 bool premultipliedAlpha(IsPreMultipliedAlphaEnabled());
610 map.Insert(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha);
612 // Note, Color and Primitive will also insert their own mix color into the map
613 // which is ok, because they have a different key value range, but uses same cached value anyway.
614 map.Insert(Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor); // vec4
615 map.Insert(Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a);
617 auto fittingModeString = Scripting::GetLinearEnumerationName<FittingMode>(
618 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT);
619 map.Insert(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString);
621 if(IsTypeAvailableForBorderline(mImpl->mType))
623 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
624 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
625 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
628 if(IsTypeAvailableForCornerRadius(mImpl->mType))
630 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
631 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, mImpl->GetCornerRadiusPolicy());
635 void Visual::Base::CreateInstancePropertyMap(Property::Map& map) const
637 DoCreateInstancePropertyMap(map);
639 if(mImpl->mCustomShader)
641 mImpl->mCustomShader->CreatePropertyMap(map);
645 void Visual::Base::EnablePreMultipliedAlpha(bool preMultiplied)
649 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
653 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
658 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
659 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultiplied);
663 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
665 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
668 void Visual::Base::DoSetOffScene(Actor& actor)
670 actor.RemoveRenderer(mImpl->mRenderer);
673 bool Visual::Base::IsOnScene() const
675 return mImpl->mFlags & Impl::IS_ON_SCENE;
678 bool Visual::Base::IsRoundedCornerRequired() const
680 // If VisualType doesn't support rounded corner, always return false.
681 if(IsTypeAvailableForCornerRadius(mImpl->mType))
685 // Update values from Renderer
686 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS);
688 Vector4 retValue = Vector4::ZERO;
689 if(value.Get(retValue))
691 if(mImpl->mDecorationData != nullptr || retValue != Vector4::ZERO)
693 mImpl->SetCornerRadius(retValue);
697 return mImpl->mAlwaysUsingCornerRadius || !(mImpl->GetCornerRadius() == Vector4::ZERO);
702 bool Visual::Base::IsBorderlineRequired() const
704 // If VisualType doesn't support borderline, always return false.
705 if(IsTypeAvailableForBorderline(mImpl->mType))
709 // Update values from Renderer
710 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH);
712 float retValue = 0.0f;
713 if(value.Get(retValue))
715 if(mImpl->mDecorationData != nullptr || !Dali::EqualsZero(retValue))
717 mImpl->SetBorderlineWidth(retValue);
721 return mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->GetBorderlineWidth());
726 void Visual::Base::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
728 // May be overriden by derived class
731 void Visual::Base::OnDoActionExtension(const Property::Index actionId, const Dali::Any attributes)
733 // May be overriden by derived class
736 void Visual::Base::RegisterMixColor()
740 // All visual renderers now use same mix color / opacity properties.
741 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
742 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, mImpl->mMixColor.a);
744 float preMultipliedAlpha = 0.0f;
745 if(IsPreMultipliedAlphaEnabled())
747 preMultipliedAlpha = 1.0f;
749 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultipliedAlpha);
753 void Visual::Base::RegisterDecoration()
757 if(IsTypeAvailableForCornerRadius(mImpl->mType))
759 if(mImpl->mAlwaysUsingCornerRadius || !(mImpl->GetCornerRadius() == Vector4::ZERO))
761 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
762 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
763 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
766 if(IsTypeAvailableForBorderline(mImpl->mType))
768 if(mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->GetBorderlineWidth()))
770 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
771 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
772 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
773 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
779 void Visual::Base::SetMixColor(const Vector4& color)
781 mImpl->mMixColor = color;
785 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(color));
786 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, color.a);
790 void Visual::Base::SetMixColor(const Vector3& color)
792 mImpl->mMixColor.r = color.r;
793 mImpl->mMixColor.g = color.g;
794 mImpl->mMixColor.b = color.b;
798 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, color);
802 void Visual::Base::AddEventObserver(Visual::EventObserver& observer)
804 mImpl->mEventObserver = &observer;
807 void Visual::Base::RemoveEventObserver(Visual::EventObserver& observer)
809 mImpl->mEventObserver = NULL;
812 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
814 if(mImpl->mResourceStatus != resourceStatus)
816 mImpl->mResourceStatus = resourceStatus;
818 if(mImpl->mEventObserver)
820 // observer is currently a control impl
821 mImpl->mEventObserver->ResourceReady(*this);
826 bool Visual::Base::IsResourceReady() const
828 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
829 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
832 bool Visual::Base::IsSynchronousLoadingRequired() const
834 return (mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING);
837 Toolkit::Visual::Type Visual::Base::GetType() const
842 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
844 return mImpl->mResourceStatus;
847 Visual::FittingMode Visual::Base::GetFittingMode() const
849 return mImpl->mFittingMode;
852 Visual::Base& Visual::Base::GetVisualObject()
857 Renderer Visual::Base::GetRenderer()
859 return mImpl->mRenderer;
862 Property::Index Visual::Base::GetIntKey(Property::Key key)
864 if(key.type == Property::Key::INDEX)
869 if(key.stringKey == ANCHOR_POINT)
871 return Toolkit::Visual::Transform::Property::ANCHOR_POINT;
873 else if(key.stringKey == EXTRA_SIZE)
875 return Toolkit::DevelVisual::Transform::Property::EXTRA_SIZE;
877 else if(key.stringKey == MIX_COLOR)
879 return Toolkit::Visual::Property::MIX_COLOR;
881 else if(key.stringKey == OPACITY)
883 return Toolkit::Visual::Property::OPACITY;
885 else if(key.stringKey == OFFSET)
887 return Toolkit::Visual::Transform::Property::OFFSET;
889 else if(key.stringKey == OFFSET_POLICY)
891 return Toolkit::Visual::Transform::Property::OFFSET_POLICY;
893 else if(key.stringKey == ORIGIN)
895 return Toolkit::Visual::Transform::Property::ORIGIN;
897 else if(key.stringKey == PREMULTIPLIED_ALPHA)
899 return Toolkit::Visual::Property::PREMULTIPLIED_ALPHA;
901 else if(key.stringKey == CUSTOM_SHADER)
903 return Toolkit::Visual::Property::SHADER;
905 else if(key.stringKey == SIZE)
907 return Toolkit::Visual::Transform::Property::SIZE;
909 else if(key.stringKey == SIZE_POLICY)
911 return Toolkit::Visual::Transform::Property::SIZE_POLICY;
913 else if(key.stringKey == TRANSFORM)
915 return Toolkit::Visual::Property::TRANSFORM;
917 else if(key.stringKey == VISUAL_FITTING_MODE)
919 return Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE;
921 else if(key.stringKey == CORNER_RADIUS)
923 return Toolkit::DevelVisual::Property::CORNER_RADIUS;
925 else if(key.stringKey == CORNER_RADIUS_POLICY)
927 return Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY;
929 else if(key.stringKey == BORDERLINE_WIDTH)
931 return Toolkit::DevelVisual::Property::BORDERLINE_WIDTH;
933 else if(key.stringKey == BORDERLINE_COLOR)
935 return Toolkit::DevelVisual::Property::BORDERLINE_COLOR;
937 else if(key.stringKey == BORDERLINE_OFFSET)
939 return Toolkit::DevelVisual::Property::BORDERLINE_OFFSET;
942 return Property::INVALID_INDEX;
945 Property::Index Visual::Base::GetPropertyIndex(Property::Key key)
947 switch(GetIntKey(key))
949 case Dali::Toolkit::Visual::Transform::Property::OFFSET:
951 return VisualRenderer::Property::TRANSFORM_OFFSET;
953 case Dali::Toolkit::Visual::Transform::Property::SIZE:
955 return VisualRenderer::Property::TRANSFORM_SIZE;
957 case Dali::Toolkit::Visual::Transform::Property::ORIGIN:
959 return VisualRenderer::Property::TRANSFORM_ORIGIN;
961 case Dali::Toolkit::Visual::Transform::Property::ANCHOR_POINT:
963 return VisualRenderer::Property::TRANSFORM_ANCHOR_POINT;
965 case Dali::Toolkit::Visual::Property::MIX_COLOR:
967 return VisualRenderer::Property::VISUAL_MIX_COLOR;
969 case Dali::Toolkit::Visual::Property::OPACITY:
971 return DevelRenderer::Property::OPACITY;
973 case Dali::Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
975 return VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA;
977 case Dali::Toolkit::DevelVisual::Property::CORNER_RADIUS:
979 return DecoratedVisualRenderer::Property::CORNER_RADIUS;
981 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
983 return DecoratedVisualRenderer::Property::BORDERLINE_WIDTH;
985 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
987 return DecoratedVisualRenderer::Property::BORDERLINE_COLOR;
989 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
991 return DecoratedVisualRenderer::Property::BORDERLINE_OFFSET;
995 Property::Index index = mImpl->mRenderer.GetPropertyIndex(key);
997 if(index == Property::INVALID_INDEX)
999 // Is it a shader property?
1000 Shader shader = mImpl->mRenderer.GetShader();
1001 index = shader.GetPropertyIndex(key);
1002 if(index != Property::INVALID_INDEX)
1004 // Yes - we should register it in the Renderer so it can be set / animated
1005 // independently, as shaders are shared across multiple renderers.
1006 std::string keyName;
1007 Property::Index keyIndex(Property::INVALID_KEY);
1008 if(key.type == Property::Key::INDEX)
1010 keyName = shader.GetPropertyName(index);
1011 keyIndex = key.indexKey;
1015 keyName = key.stringKey;
1016 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
1018 Property::Value value = shader.GetProperty(index);
1020 // We already know that mRenderer didn't have property. So we can assume that it is unique.
1021 index = mImpl->mRenderer.RegisterUniqueProperty(keyIndex, keyName, value);
1027 void Visual::Base::SetupTransition(
1028 Dali::Animation& transition,
1029 Internal::TransitionData::Animator& animator,
1030 Property::Index index,
1031 Property::Value& initialValue,
1032 Property::Value& targetValue)
1034 if(index != Property::INVALID_INDEX)
1036 if(mImpl->mRenderer)
1038 if(animator.animate == false)
1040 mImpl->mRenderer.SetProperty(index, targetValue);
1044 if(animator.initialValue.GetType() != Property::NONE)
1046 mImpl->mRenderer.SetProperty(index, initialValue);
1051 transition = Dali::Animation::New(0.1f);
1054 transition.AnimateTo(Property(mImpl->mRenderer, index),
1056 animator.alphaFunction,
1057 TimePeriod(animator.timePeriodDelay,
1058 animator.timePeriodDuration));
1064 void Visual::Base::AnimateProperty(
1065 Dali::Animation& transition,
1066 Internal::TransitionData::Animator& animator)
1068 #if defined(DEBUG_ENABLED)
1069 if(gVisualBaseLogFilter->IsEnabledFor(Debug::General))
1071 std::ostringstream oss;
1072 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
1073 DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, oss.str().c_str());
1077 if(animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
1078 animator.propertyKey == MIX_COLOR ||
1079 (mImpl->mType == Toolkit::Visual::COLOR &&
1080 animator.propertyKey == ColorVisual::Property::MIX_COLOR) ||
1081 (mImpl->mType == Toolkit::Visual::PRIMITIVE &&
1082 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR))
1084 AnimateMixColorProperty(transition, animator);
1086 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
1087 animator.propertyKey == OPACITY)
1089 AnimateOpacityProperty(transition, animator);
1091 else if(mImpl->mRenderer)
1093 AnimateRendererProperty(transition, animator);
1097 void Visual::Base::AnimateOpacityProperty(
1098 Dali::Animation& transition,
1099 Internal::TransitionData::Animator& animator)
1101 float targetOpacity;
1102 if(animator.targetValue.Get(targetOpacity))
1104 mImpl->mMixColor.a = targetOpacity;
1107 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue);
1110 void Visual::Base::AnimateRendererProperty(
1111 Dali::Animation& transition,
1112 Internal::TransitionData::Animator& animator)
1114 // Get actual renderer index (will convert transform keys into visualproperty indices)
1115 Property::Index index = GetPropertyIndex(animator.propertyKey);
1117 if(index != Property::INVALID_INDEX)
1119 if(animator.targetValue.GetType() != Property::NONE)
1121 // Try writing target value into transform property map
1122 // if it's not a valid key, then it won't alter mTransform
1124 if(animator.propertyKey.type == Property::Key::INDEX)
1126 map.Add(animator.propertyKey.indexKey, animator.targetValue);
1130 map.Add(animator.propertyKey.stringKey, animator.targetValue);
1133 mImpl->mTransform.UpdatePropertyMap(map);
1135 SetupTransition(transition, animator, index, animator.initialValue, animator.targetValue);
1139 void Visual::Base::AnimateMixColorProperty(
1140 Dali::Animation& transition,
1141 Internal::TransitionData::Animator& animator)
1143 bool animateOpacity = false;
1145 Property::Value initialOpacity;
1146 Property::Value targetOpacity;
1147 Property::Value initialMixColor;
1148 Property::Value targetMixColor;
1150 Vector4 initialColor;
1151 if(animator.initialValue.Get(initialColor))
1153 if(animator.initialValue.GetType() == Property::VECTOR4)
1155 // if there is an initial color specifying alpha, test it
1156 initialOpacity = initialColor.a;
1158 initialMixColor = Vector3(initialColor);
1161 // Set target value into data store
1162 if(animator.targetValue.GetType() != Property::NONE)
1165 animator.targetValue.Get(mixColor);
1166 if(animator.targetValue.GetType() == Property::VECTOR4)
1168 mImpl->mMixColor.a = mixColor.a;
1169 targetOpacity = mixColor.a;
1170 animateOpacity = true;
1173 mImpl->mMixColor.r = mixColor.r;
1174 mImpl->mMixColor.g = mixColor.g;
1175 mImpl->mMixColor.b = mixColor.b;
1176 targetMixColor = Vector3(mixColor);
1179 SetupTransition(transition, animator, VisualRenderer::Property::VISUAL_MIX_COLOR, initialMixColor, targetMixColor);
1183 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity);
1187 Dali::Property Visual::Base::GetPropertyObject(Dali::Property::Key key)
1189 if(!mImpl->mRenderer)
1192 return Dali::Property(handle, Property::INVALID_INDEX);
1195 switch(GetIntKey(key))
1197 // Default animatable properties from VisualRenderer
1198 case Toolkit::Visual::Property::MIX_COLOR:
1200 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1202 case Toolkit::Visual::Property::OPACITY:
1204 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1206 case Toolkit::Visual::Transform::Property::OFFSET:
1208 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_OFFSET);
1210 case Toolkit::Visual::Transform::Property::SIZE:
1212 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_SIZE);
1215 // Default animatable properties from DecoratedVisualRenderer
1216 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
1218 if(IsTypeAvailableForCornerRadius(mImpl->mType))
1220 const bool updateShader = !mImpl->mCustomShader && !IsRoundedCornerRequired();
1222 // CornerRadius is animated now. we always have to use corner radius feature.
1223 mImpl->mAlwaysUsingCornerRadius = true;
1227 // Update each values to renderer
1228 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
1229 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
1230 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
1235 if(!IsBorderlineRequired())
1237 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
1238 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
1240 return Dali::Property(mImpl->mRenderer, DecoratedVisualRenderer::Property::CORNER_RADIUS);
1244 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
1245 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
1246 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
1248 if(IsTypeAvailableForBorderline(mImpl->mType))
1250 const bool updateShader = !mImpl->mCustomShader && !IsBorderlineRequired();
1252 // Borderline is animated now. we always have to use borderline feature.
1253 mImpl->mAlwaysUsingBorderline = true;
1257 // Update each values to renderer
1258 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
1259 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
1260 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
1261 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
1266 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
1268 return Dali::Property(mImpl->mRenderer, GetPropertyIndex(key));
1274 // Special case for MIX_COLOR
1275 if(key.type == Property::Key::INDEX &&
1276 ((mImpl->mType == Toolkit::Visual::COLOR && key.indexKey == ColorVisual::Property::MIX_COLOR) ||
1277 (mImpl->mType == Toolkit::Visual::PRIMITIVE && key.indexKey == PrimitiveVisual::Property::MIX_COLOR)))
1279 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1282 // Special case for BLUR_RADIUS
1283 if(mImpl->mType == Toolkit::Visual::COLOR &&
1284 ((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) ||
1285 (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME)))
1287 // Request to color-visual class
1288 return OnGetPropertyObject(key);
1293 // If it is not VisualRenderer property, check registered Renderer and Shader property.
1294 Property::Index index = GetPropertyIndex(key);
1295 if(index != Property::INVALID_INDEX)
1297 return Dali::Property(mImpl->mRenderer, index);
1300 // We can't find the property in the base class.
1301 // Request to child class
1302 return OnGetPropertyObject(key);
1305 } // namespace Internal
1307 } // namespace Toolkit