2 * Copyright (c) 2023 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/visuals/color-visual-properties-devel.h>
31 #include <dali-toolkit/devel-api/visuals/visual-actions-devel.h>
32 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
33 #include <dali-toolkit/internal/helpers/property-helper.h>
34 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
35 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
36 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
37 #include <dali-toolkit/public-api/visuals/primitive-visual-properties.h>
38 #include <dali-toolkit/public-api/visuals/visual-properties.h>
42 #if defined(DEBUG_ENABLED)
43 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VISUAL_BASE");
46 // visual string constants contains OFFSET_SIZE_MODE instead
47 const char* const OFFSET_POLICY("offsetPolicy");
48 const char* const SIZE_POLICY("sizePolicy");
60 DALI_ENUM_TO_STRING_TABLE_BEGIN(VISUAL_FITTING_MODE)
61 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_KEEP_ASPECT_RATIO)
62 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FILL)
63 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, OVER_FIT_KEEP_ASPECT_RATIO)
64 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, CENTER)
65 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_WIDTH)
66 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_HEIGHT)
67 DALI_ENUM_TO_STRING_TABLE_END(VISUAL_FITTING_MODE)
70 * @brief Check whether this visual type can use corner radius feature or not.
71 * @param type VisualType that want to checkup
72 * @return true if type can use corner radius feature.
74 static bool IsTypeAvailableForCornerRadius(Toolkit::Visual::Type type)
76 switch(static_cast<Toolkit::DevelVisual::Type>(type))
78 case Toolkit::Visual::Type::COLOR:
79 case Toolkit::Visual::Type::GRADIENT:
80 case Toolkit::Visual::Type::IMAGE:
81 case Toolkit::Visual::Type::SVG:
82 case Toolkit::Visual::Type::ANIMATED_IMAGE:
83 case Toolkit::DevelVisual::Type::ANIMATED_VECTOR_IMAGE:
95 * @brief Check whether this visual type can use borderline feature or not.
96 * @param type VisualType that want to checkup
97 * @return true if type can use borderline feature.
99 static bool IsTypeAvailableForBorderline(Toolkit::Visual::Type type)
101 switch(static_cast<Toolkit::DevelVisual::Type>(type))
103 case Toolkit::Visual::Type::COLOR:
104 case Toolkit::Visual::Type::GRADIENT:
105 case Toolkit::Visual::Type::IMAGE:
106 case Toolkit::Visual::Type::SVG:
107 case Toolkit::Visual::Type::ANIMATED_IMAGE:
108 case Toolkit::DevelVisual::Type::ANIMATED_VECTOR_IMAGE:
119 struct StringProperty
121 const char* const name;
122 Property::Index index;
124 StringProperty PROPERTY_NAME_INDEX_TABLE[] =
126 {CUSTOM_SHADER, Toolkit::Visual::Property::SHADER},
127 {TRANSFORM, Toolkit::Visual::Property::TRANSFORM},
128 {PREMULTIPLIED_ALPHA, Toolkit::Visual::Property::PREMULTIPLIED_ALPHA},
129 {MIX_COLOR, Toolkit::Visual::Property::MIX_COLOR},
130 {OPACITY, Toolkit::Visual::Property::OPACITY},
131 {VISUAL_FITTING_MODE, Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE},
132 {BORDERLINE_WIDTH, Toolkit::DevelVisual::Property::BORDERLINE_WIDTH},
133 {BORDERLINE_COLOR, Toolkit::DevelVisual::Property::BORDERLINE_COLOR},
134 {BORDERLINE_OFFSET, Toolkit::DevelVisual::Property::BORDERLINE_OFFSET},
135 {CORNER_RADIUS, Toolkit::DevelVisual::Property::CORNER_RADIUS},
136 {CORNER_RADIUS_POLICY, Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY},
138 const uint16_t PROPERTY_NAME_INDEX_TABLE_COUNT = sizeof(PROPERTY_NAME_INDEX_TABLE) / sizeof(PROPERTY_NAME_INDEX_TABLE[0]);
140 Property::Index GetVisualPropertyIndex(Property::Key key)
142 if(key.type == Property::Key::STRING)
144 for(auto tableId = 0u; tableId < PROPERTY_NAME_INDEX_TABLE_COUNT; ++tableId)
146 if(key == PROPERTY_NAME_INDEX_TABLE[tableId].name)
148 return PROPERTY_NAME_INDEX_TABLE[tableId].index;
158 Visual::Base::Base(VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type)
159 : mImpl(new Impl(fittingMode, type)),
160 mFactoryCache(factoryCache)
164 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 mImpl->mDepthIndex = index;
526 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
530 int Visual::Base::GetDepthIndex() const
532 return mImpl->mDepthIndex;
535 void Visual::Base::SetOnScene(Actor& actor)
539 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
540 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
545 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
546 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
549 mImpl->mFlags |= Impl::IS_ON_SCENE;
553 void Visual::Base::SetOffScene(Actor& actor)
557 DoSetOffScene(actor);
558 mImpl->mFlags &= ~Impl::IS_ON_SCENE;
562 void Visual::Base::CreatePropertyMap(Property::Map& map) const
566 // Update values from Renderer
567 mImpl->mMixColor = mImpl->mRenderer.GetProperty<Vector3>(VisualRenderer::Property::VISUAL_MIX_COLOR);
568 mImpl->mMixColor.a = mImpl->mRenderer.GetProperty<float>(DevelRenderer::Property::OPACITY);
570 mImpl->mTransform.mOffset = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_OFFSET);
571 mImpl->mTransform.mSize = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_SIZE);
573 if(IsRoundedCornerRequired())
575 mImpl->SetCornerRadius(mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::CORNER_RADIUS));
577 if(IsBorderlineRequired())
579 mImpl->SetBorderlineWidth(mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH));
580 mImpl->SetBorderlineColor(mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::BORDERLINE_COLOR));
581 mImpl->SetBorderlineOffset(mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET));
585 DoCreatePropertyMap(map);
587 if(mImpl->mCustomShader)
589 mImpl->mCustomShader->CreatePropertyMap(map);
592 Property::Map transform;
593 mImpl->mTransform.GetPropertyMap(transform);
594 map.Insert(Toolkit::Visual::Property::TRANSFORM, transform);
596 bool premultipliedAlpha(IsPreMultipliedAlphaEnabled());
597 map.Insert(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha);
599 // Note, Color and Primitive will also insert their own mix color into the map
600 // which is ok, because they have a different key value range, but uses same cached value anyway.
601 map.Insert(Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor); // vec4
602 map.Insert(Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a);
604 auto fittingModeString = Scripting::GetLinearEnumerationName<FittingMode>(
605 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT);
606 map.Insert(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString);
608 if(IsTypeAvailableForBorderline(mImpl->mType))
610 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
611 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
612 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
615 if(IsTypeAvailableForCornerRadius(mImpl->mType))
617 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
618 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, mImpl->GetCornerRadiusPolicy());
622 void Visual::Base::CreateInstancePropertyMap(Property::Map& map) const
624 DoCreateInstancePropertyMap(map);
626 if(mImpl->mCustomShader)
628 mImpl->mCustomShader->CreatePropertyMap(map);
632 void Visual::Base::EnablePreMultipliedAlpha(bool preMultiplied)
636 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
640 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
645 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
646 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultiplied);
650 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
652 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
655 void Visual::Base::DoSetOffScene(Actor& actor)
657 actor.RemoveRenderer(mImpl->mRenderer);
660 bool Visual::Base::IsOnScene() const
662 return mImpl->mFlags & Impl::IS_ON_SCENE;
665 bool Visual::Base::IsRoundedCornerRequired() const
667 // If VisualType doesn't support rounded corner, always return false.
668 if(IsTypeAvailableForCornerRadius(mImpl->mType))
672 // Update values from Renderer
673 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS);
675 Vector4 retValue = Vector4::ZERO;
676 if(value.Get(retValue))
678 if(mImpl->mDecorationData != nullptr || retValue != Vector4::ZERO)
680 mImpl->SetCornerRadius(retValue);
684 return mImpl->mAlwaysUsingCornerRadius || !(mImpl->GetCornerRadius() == Vector4::ZERO);
689 bool Visual::Base::IsBorderlineRequired() const
691 // If VisualType doesn't support borderline, always return false.
692 if(IsTypeAvailableForBorderline(mImpl->mType))
696 // Update values from Renderer
697 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH);
699 float retValue = 0.0f;
700 if(value.Get(retValue))
702 if(mImpl->mDecorationData != nullptr || !Dali::EqualsZero(retValue))
704 mImpl->SetBorderlineWidth(retValue);
708 return mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->GetBorderlineWidth());
713 void Visual::Base::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
715 // May be overriden by derived class
718 void Visual::Base::OnDoActionExtension(const Property::Index actionId, const Dali::Any attributes)
720 // May be overriden by derived class
723 void Visual::Base::RegisterMixColor()
727 // All visual renderers now use same mix color / opacity properties.
728 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
729 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, mImpl->mMixColor.a);
731 float preMultipliedAlpha = 0.0f;
732 if(IsPreMultipliedAlphaEnabled())
734 preMultipliedAlpha = 1.0f;
736 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultipliedAlpha);
740 void Visual::Base::RegisterDecoration()
744 if(IsTypeAvailableForCornerRadius(mImpl->mType))
746 if(mImpl->mAlwaysUsingCornerRadius || !(mImpl->GetCornerRadius() == Vector4::ZERO))
748 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
749 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
750 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
753 if(IsTypeAvailableForBorderline(mImpl->mType))
755 if(mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->GetBorderlineWidth()))
757 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
758 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
759 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
760 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
766 void Visual::Base::SetMixColor(const Vector4& color)
768 mImpl->mMixColor = color;
772 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(color));
773 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, color.a);
777 void Visual::Base::SetMixColor(const Vector3& color)
779 mImpl->mMixColor.r = color.r;
780 mImpl->mMixColor.g = color.g;
781 mImpl->mMixColor.b = color.b;
785 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, color);
789 void Visual::Base::AddEventObserver(Visual::EventObserver& observer)
791 mImpl->mEventObserver = &observer;
794 void Visual::Base::RemoveEventObserver(Visual::EventObserver& observer)
796 mImpl->mEventObserver = NULL;
799 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
801 if(mImpl->mResourceStatus != resourceStatus)
803 mImpl->mResourceStatus = resourceStatus;
805 if(mImpl->mEventObserver)
807 // observer is currently a control impl
808 mImpl->mEventObserver->ResourceReady(*this);
813 bool Visual::Base::IsResourceReady() const
815 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
816 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
819 bool Visual::Base::IsSynchronousLoadingRequired() const
821 return (mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING);
824 Toolkit::Visual::Type Visual::Base::GetType() const
829 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
831 return mImpl->mResourceStatus;
834 Visual::FittingMode Visual::Base::GetFittingMode() const
836 return mImpl->mFittingMode;
839 Visual::Base& Visual::Base::GetVisualObject()
844 Renderer Visual::Base::GetRenderer()
846 return mImpl->mRenderer;
849 Property::Index Visual::Base::GetIntKey(Property::Key key)
851 if(key.type == Property::Key::INDEX)
856 if(key.stringKey == ANCHOR_POINT)
858 return Toolkit::Visual::Transform::Property::ANCHOR_POINT;
860 else if(key.stringKey == EXTRA_SIZE)
862 return Toolkit::DevelVisual::Transform::Property::EXTRA_SIZE;
864 else if(key.stringKey == MIX_COLOR)
866 return Toolkit::Visual::Property::MIX_COLOR;
868 else if(key.stringKey == OPACITY)
870 return Toolkit::Visual::Property::OPACITY;
872 else if(key.stringKey == OFFSET)
874 return Toolkit::Visual::Transform::Property::OFFSET;
876 else if(key.stringKey == OFFSET_POLICY)
878 return Toolkit::Visual::Transform::Property::OFFSET_POLICY;
880 else if(key.stringKey == ORIGIN)
882 return Toolkit::Visual::Transform::Property::ORIGIN;
884 else if(key.stringKey == PREMULTIPLIED_ALPHA)
886 return Toolkit::Visual::Property::PREMULTIPLIED_ALPHA;
888 else if(key.stringKey == CUSTOM_SHADER)
890 return Toolkit::Visual::Property::SHADER;
892 else if(key.stringKey == SIZE)
894 return Toolkit::Visual::Transform::Property::SIZE;
896 else if(key.stringKey == SIZE_POLICY)
898 return Toolkit::Visual::Transform::Property::SIZE_POLICY;
900 else if(key.stringKey == TRANSFORM)
902 return Toolkit::Visual::Property::TRANSFORM;
904 else if(key.stringKey == VISUAL_FITTING_MODE)
906 return Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE;
908 else if(key.stringKey == CORNER_RADIUS)
910 return Toolkit::DevelVisual::Property::CORNER_RADIUS;
912 else if(key.stringKey == CORNER_RADIUS_POLICY)
914 return Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY;
916 else if(key.stringKey == BORDERLINE_WIDTH)
918 return Toolkit::DevelVisual::Property::BORDERLINE_WIDTH;
920 else if(key.stringKey == BORDERLINE_COLOR)
922 return Toolkit::DevelVisual::Property::BORDERLINE_COLOR;
924 else if(key.stringKey == BORDERLINE_OFFSET)
926 return Toolkit::DevelVisual::Property::BORDERLINE_OFFSET;
929 return Property::INVALID_INDEX;
932 Property::Index Visual::Base::GetPropertyIndex(Property::Key key)
934 switch(GetIntKey(key))
936 case Dali::Toolkit::Visual::Transform::Property::OFFSET:
938 return VisualRenderer::Property::TRANSFORM_OFFSET;
940 case Dali::Toolkit::Visual::Transform::Property::SIZE:
942 return VisualRenderer::Property::TRANSFORM_SIZE;
944 case Dali::Toolkit::Visual::Transform::Property::ORIGIN:
946 return VisualRenderer::Property::TRANSFORM_ORIGIN;
948 case Dali::Toolkit::Visual::Transform::Property::ANCHOR_POINT:
950 return VisualRenderer::Property::TRANSFORM_ANCHOR_POINT;
952 case Dali::Toolkit::Visual::Property::MIX_COLOR:
954 return VisualRenderer::Property::VISUAL_MIX_COLOR;
956 case Dali::Toolkit::Visual::Property::OPACITY:
958 return DevelRenderer::Property::OPACITY;
960 case Dali::Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
962 return VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA;
964 case Dali::Toolkit::DevelVisual::Property::CORNER_RADIUS:
966 return DecoratedVisualRenderer::Property::CORNER_RADIUS;
968 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
970 return DecoratedVisualRenderer::Property::BORDERLINE_WIDTH;
972 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
974 return DecoratedVisualRenderer::Property::BORDERLINE_COLOR;
976 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
978 return DecoratedVisualRenderer::Property::BORDERLINE_OFFSET;
982 Property::Index index = mImpl->mRenderer.GetPropertyIndex(key);
984 if(index == Property::INVALID_INDEX)
986 // Is it a shader property?
987 Shader shader = mImpl->mRenderer.GetShader();
988 index = shader.GetPropertyIndex(key);
989 if(index != Property::INVALID_INDEX)
991 // Yes - we should register it in the Renderer so it can be set / animated
992 // independently, as shaders are shared across multiple renderers.
994 Property::Index keyIndex(Property::INVALID_KEY);
995 if(key.type == Property::Key::INDEX)
997 keyName = shader.GetPropertyName(index);
998 keyIndex = key.indexKey;
1002 keyName = key.stringKey;
1003 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
1005 Property::Value value = shader.GetProperty(index);
1007 // We already know that mRenderer didn't have property. So we can assume that it is unique.
1008 index = mImpl->mRenderer.RegisterUniqueProperty(keyIndex, keyName, value);
1014 void Visual::Base::SetupTransition(
1015 Dali::Animation& transition,
1016 Internal::TransitionData::Animator& animator,
1017 Property::Index index,
1018 Property::Value& initialValue,
1019 Property::Value& targetValue)
1021 if(index != Property::INVALID_INDEX)
1023 if(mImpl->mRenderer)
1025 if(animator.animate == false)
1027 mImpl->mRenderer.SetProperty(index, targetValue);
1031 if(animator.initialValue.GetType() != Property::NONE)
1033 mImpl->mRenderer.SetProperty(index, initialValue);
1038 transition = Dali::Animation::New(0.1f);
1041 transition.AnimateTo(Property(mImpl->mRenderer, index),
1043 animator.alphaFunction,
1044 TimePeriod(animator.timePeriodDelay,
1045 animator.timePeriodDuration));
1051 void Visual::Base::AnimateProperty(
1052 Dali::Animation& transition,
1053 Internal::TransitionData::Animator& animator)
1055 #if defined(DEBUG_ENABLED)
1056 if(gVisualBaseLogFilter->IsEnabledFor(Debug::General))
1058 std::ostringstream oss;
1059 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
1060 DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, oss.str().c_str());
1064 if(animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
1065 animator.propertyKey == MIX_COLOR ||
1066 (mImpl->mType == Toolkit::Visual::COLOR &&
1067 animator.propertyKey == ColorVisual::Property::MIX_COLOR) ||
1068 (mImpl->mType == Toolkit::Visual::PRIMITIVE &&
1069 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR))
1071 AnimateMixColorProperty(transition, animator);
1073 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
1074 animator.propertyKey == OPACITY)
1076 AnimateOpacityProperty(transition, animator);
1078 else if(mImpl->mRenderer)
1080 AnimateRendererProperty(transition, animator);
1084 void Visual::Base::AnimateOpacityProperty(
1085 Dali::Animation& transition,
1086 Internal::TransitionData::Animator& animator)
1088 float targetOpacity;
1089 if(animator.targetValue.Get(targetOpacity))
1091 mImpl->mMixColor.a = targetOpacity;
1094 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue);
1097 void Visual::Base::AnimateRendererProperty(
1098 Dali::Animation& transition,
1099 Internal::TransitionData::Animator& animator)
1101 // Get actual renderer index (will convert transform keys into visualproperty indices)
1102 Property::Index index = GetPropertyIndex(animator.propertyKey);
1104 if(index != Property::INVALID_INDEX)
1106 if(animator.targetValue.GetType() != Property::NONE)
1108 // Try writing target value into transform property map
1109 // if it's not a valid key, then it won't alter mTransform
1111 if(animator.propertyKey.type == Property::Key::INDEX)
1113 map.Add(animator.propertyKey.indexKey, animator.targetValue);
1117 map.Add(animator.propertyKey.stringKey, animator.targetValue);
1120 mImpl->mTransform.UpdatePropertyMap(map);
1122 SetupTransition(transition, animator, index, animator.initialValue, animator.targetValue);
1126 void Visual::Base::AnimateMixColorProperty(
1127 Dali::Animation& transition,
1128 Internal::TransitionData::Animator& animator)
1130 bool animateOpacity = false;
1132 Property::Value initialOpacity;
1133 Property::Value targetOpacity;
1134 Property::Value initialMixColor;
1135 Property::Value targetMixColor;
1137 Vector4 initialColor;
1138 if(animator.initialValue.Get(initialColor))
1140 if(animator.initialValue.GetType() == Property::VECTOR4)
1142 // if there is an initial color specifying alpha, test it
1143 initialOpacity = initialColor.a;
1145 initialMixColor = Vector3(initialColor);
1148 // Set target value into data store
1149 if(animator.targetValue.GetType() != Property::NONE)
1152 animator.targetValue.Get(mixColor);
1153 if(animator.targetValue.GetType() == Property::VECTOR4)
1155 mImpl->mMixColor.a = mixColor.a;
1156 targetOpacity = mixColor.a;
1157 animateOpacity = true;
1160 mImpl->mMixColor.r = mixColor.r;
1161 mImpl->mMixColor.g = mixColor.g;
1162 mImpl->mMixColor.b = mixColor.b;
1163 targetMixColor = Vector3(mixColor);
1166 SetupTransition(transition, animator, VisualRenderer::Property::VISUAL_MIX_COLOR, initialMixColor, targetMixColor);
1170 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity);
1174 Dali::Property Visual::Base::GetPropertyObject(Dali::Property::Key key)
1176 if(!mImpl->mRenderer)
1179 return Dali::Property(handle, Property::INVALID_INDEX);
1182 switch(GetIntKey(key))
1184 // Default animatable properties from VisualRenderer
1185 case Toolkit::Visual::Property::MIX_COLOR:
1187 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1189 case Toolkit::Visual::Property::OPACITY:
1191 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1193 case Toolkit::Visual::Transform::Property::OFFSET:
1195 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_OFFSET);
1197 case Toolkit::Visual::Transform::Property::SIZE:
1199 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_SIZE);
1202 // Default animatable properties from DecoratedVisualRenderer
1203 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
1205 if(IsTypeAvailableForCornerRadius(mImpl->mType))
1207 const bool updateShader = !mImpl->mCustomShader && !IsRoundedCornerRequired();
1209 // CornerRadius is animated now. we always have to use corner radius feature.
1210 mImpl->mAlwaysUsingCornerRadius = true;
1214 // Update each values to renderer
1215 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
1216 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
1217 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
1222 if(!IsBorderlineRequired())
1224 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
1225 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
1227 return Dali::Property(mImpl->mRenderer, DecoratedVisualRenderer::Property::CORNER_RADIUS);
1231 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
1232 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
1233 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
1235 if(IsTypeAvailableForBorderline(mImpl->mType))
1237 const bool updateShader = !mImpl->mCustomShader && !IsBorderlineRequired();
1239 // Borderline is animated now. we always have to use borderline feature.
1240 mImpl->mAlwaysUsingBorderline = true;
1244 // Update each values to renderer
1245 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
1246 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
1247 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
1248 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
1253 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
1255 return Dali::Property(mImpl->mRenderer, GetPropertyIndex(key));
1261 // Special case for MIX_COLOR
1262 if(key.type == Property::Key::INDEX &&
1263 ((mImpl->mType == Toolkit::Visual::COLOR && key.indexKey == ColorVisual::Property::MIX_COLOR) ||
1264 (mImpl->mType == Toolkit::Visual::PRIMITIVE && key.indexKey == PrimitiveVisual::Property::MIX_COLOR)))
1266 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1269 // Special case for BLUR_RADIUS
1270 if(mImpl->mType == Toolkit::Visual::COLOR &&
1271 ((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) ||
1272 (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME)))
1274 // Request to color-visual class
1275 return OnGetPropertyObject(key);
1280 // If it is not VisualRenderer property, check registered Renderer and Shader property.
1281 Property::Index index = GetPropertyIndex(key);
1282 if(index != Property::INVALID_INDEX)
1284 return Dali::Property(mImpl->mRenderer, index);
1287 // We can't find the property in the base class.
1288 // Request to child class
1289 return OnGetPropertyObject(key);
1292 } // namespace Internal
1294 } // namespace Toolkit