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/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:
121 Visual::Base::Base(VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type)
122 : mImpl(new Impl(fittingMode, type)),
123 mFactoryCache(factoryCache)
127 Visual::Base::~Base()
133 void Visual::Base::Initialize()
135 // The Renderer should be created inside derived class here.
141 RegisterDecoration();
143 if(IsBorderlineRequired())
145 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
147 else if(IsRoundedCornerRequired())
149 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
154 void Visual::Base::SetCustomShader(const Property::Map& shaderMap)
156 if(mImpl->mCustomShader)
158 mImpl->mCustomShader->SetPropertyMap(shaderMap);
162 mImpl->mCustomShader = new Impl::CustomShader(shaderMap);
165 // Let derived class know
169 void Visual::Base::SetProperties(const Property::Map& propertyMap)
171 bool needUpdateShader = false;
172 for(size_t i = 0; i < propertyMap.Count(); ++i)
174 const KeyValuePair& pair = propertyMap.GetKeyValue(i);
175 const Property::Key& key = pair.first;
176 const Property::Value& value = pair.second;
178 Property::Key matchKey = key;
179 if(matchKey.type == Property::Key::STRING)
181 if(matchKey == CUSTOM_SHADER)
183 matchKey = Property::Key(Toolkit::Visual::Property::SHADER);
185 else if(matchKey == TRANSFORM)
187 matchKey = Property::Key(Toolkit::Visual::Property::TRANSFORM);
189 else if(matchKey == PREMULTIPLIED_ALPHA)
191 matchKey = Property::Key(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA);
193 else if(matchKey == MIX_COLOR)
195 matchKey = Property::Key(Toolkit::Visual::Property::MIX_COLOR);
197 else if(matchKey == OPACITY)
199 matchKey = Property::Key(Toolkit::Visual::Property::OPACITY);
201 else if(matchKey == VISUAL_FITTING_MODE)
203 matchKey = Property::Key(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE);
205 else if(matchKey == BORDERLINE_WIDTH)
207 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH);
209 else if(matchKey == BORDERLINE_COLOR)
211 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_COLOR);
213 else if(matchKey == BORDERLINE_OFFSET)
215 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET);
217 else if(matchKey == CORNER_RADIUS)
219 matchKey = Property::Key(Toolkit::DevelVisual::Property::CORNER_RADIUS);
221 else if(matchKey == CORNER_RADIUS_POLICY)
223 matchKey = Property::Key(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY);
227 switch(matchKey.indexKey)
229 case Toolkit::Visual::Property::SHADER:
231 Property::Map shaderMap;
232 if(value.Get(shaderMap))
234 SetCustomShader(shaderMap);
239 case Toolkit::Visual::Property::TRANSFORM:
244 mImpl->mTransform.SetPropertyMap(map);
249 case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
251 bool premultipliedAlpha = false;
252 if(value.Get(premultipliedAlpha))
254 EnablePreMultipliedAlpha(premultipliedAlpha);
259 case Toolkit::Visual::Property::MIX_COLOR:
262 if(value.Get(mixColor))
264 if(value.GetType() == Property::VECTOR4)
266 SetMixColor(mixColor);
270 Vector3 mixColor3(mixColor);
271 SetMixColor(mixColor3);
276 case Toolkit::Visual::Property::OPACITY:
279 if(value.Get(opacity))
281 mImpl->mMixColor.a = opacity;
282 SetMixColor(mImpl->mMixColor);
286 case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
288 Scripting::GetEnumerationProperty<Visual::FittingMode>(
289 value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode);
292 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
297 if(mImpl->mDecorationData != nullptr || !Dali::EqualsZero(width))
299 mImpl->SetBorderlineWidth(width);
303 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
305 // Unusual case. SetProperty called after OnInitialize().
306 // Assume that DoAction call UPDATE_PROPERTY.
307 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
308 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
310 // Check whether we must update shader.
311 if(!mImpl->mAlwaysUsingBorderline && IsBorderlineRequired())
313 // Required to change shader mean, we didn't setup BORDERLINE_COLOR and BORDERLINE_OFFSET into mRenderer before. Set property now.
314 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
315 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
317 // Make Blend mode ON_WITHOUT_CULL for transparent mix color.
318 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
320 // Change the shader must not be occured many times. we always have to use borderline feature.
321 mImpl->mAlwaysUsingBorderline = true;
324 if(!mImpl->mCustomShader)
326 needUpdateShader = true;
332 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
337 if(mImpl->mDecorationData != nullptr || color != Vector4::ZERO)
339 mImpl->SetBorderlineColor(color);
343 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
345 // Unusual case. SetProperty called after OnInitialize().
346 // Assume that DoAction call UPDATE_PROPERTY.
347 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
351 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
354 if(value.Get(offset))
356 if(mImpl->mDecorationData != nullptr || !Dali::EqualsZero(offset))
358 mImpl->SetBorderlineOffset(offset);
362 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
364 // Unusual case. SetProperty called after OnInitialize().
365 // Assume that DoAction call UPDATE_PROPERTY.
366 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
370 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
372 if(value.GetType() == Property::VECTOR4)
374 // If CORNER_RADIUS Property is Vector4,
375 // Each values mean the radius of
376 // (top-left, top-right, bottom-right, bottom-left)
378 if(value.Get(radius))
380 if(mImpl->mDecorationData != nullptr || radius != Vector4::ZERO)
382 mImpl->SetCornerRadius(radius);
388 // If CORNER_RADIUS Property is float,
389 // Every corner radius have same value
391 if(value.Get(radius))
393 if(mImpl->mDecorationData != nullptr || !Dali::EqualsZero(radius))
395 mImpl->SetCornerRadius(Vector4(radius, radius, radius, radius));
400 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForCornerRadius(mImpl->mType)))
402 // Unusual case. SetProperty called after OnInitialize().
403 // Assume that DoAction call UPDATE_PROPERTY.
404 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
405 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
407 // Check whether we must update shader.
408 if(!mImpl->mAlwaysUsingCornerRadius && IsRoundedCornerRequired())
410 // Required to change shader mean, we didn't setup CORNER_RADIUS_POLICY into mRenderer before. Set property now.
411 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
413 // Change the shader must not be occured many times. we always have to use corner radius feature.
414 mImpl->mAlwaysUsingCornerRadius = true;
416 if(!IsBorderlineRequired())
418 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
419 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
423 if(!mImpl->mCustomShader)
425 needUpdateShader = true;
432 case Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY:
435 if(value.Get(policy))
439 case Toolkit::Visual::Transform::Policy::RELATIVE:
440 case Toolkit::Visual::Transform::Policy::ABSOLUTE:
442 mImpl->SetCornerRadiusPolicy(policy);
443 if(DALI_UNLIKELY(mImpl->mRenderer))
445 // Unusual case. SetProperty called after OnInitialize().
446 // Assume that DoAction call UPDATE_PROPERTY.
447 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
453 DALI_LOG_ERROR("Unsupported policy: %d\n", policy);
463 DoSetProperties(propertyMap);
465 if(DALI_UNLIKELY(needUpdateShader))
471 void Visual::Base::SetTransformAndSize(const Property::Map& transform, Size controlSize)
473 mImpl->mControlSize = controlSize;
474 mImpl->mTransform.UpdatePropertyMap(transform);
476 #if defined(DEBUG_ENABLED)
477 std::ostringstream oss;
479 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);
485 void Visual::Base::SetName(const std::string& name)
490 const std::string& Visual::Base::GetName() const
495 float Visual::Base::GetHeightForWidth(float width)
497 float aspectCorrectedHeight = 0.f;
499 GetNaturalSize(naturalSize);
500 if(naturalSize.width > 0.0f)
502 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
504 return aspectCorrectedHeight;
507 float Visual::Base::GetWidthForHeight(float height)
509 float aspectCorrectedWidth = 0.f;
511 GetNaturalSize(naturalSize);
512 if(naturalSize.height > 0.0f)
514 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
516 return aspectCorrectedWidth;
519 void Visual::Base::GetNaturalSize(Vector2& naturalSize)
521 naturalSize = Vector2::ZERO;
524 void Visual::Base::DoAction(const Property::Index actionId, const Property::Value attributes)
526 OnDoAction(actionId, attributes);
528 // Check if action is valid for this visual type and perform action if possible
531 case DevelVisual::Action::UPDATE_PROPERTY:
533 const Property::Map* map = attributes.GetMap();
543 void Visual::Base::DoActionExtension(const Dali::Property::Index actionId, const Dali::Any attributes)
545 OnDoActionExtension(actionId, attributes);
548 void Visual::Base::SetDepthIndex(int index)
550 mImpl->mDepthIndex = index;
553 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
557 int Visual::Base::GetDepthIndex() const
559 return mImpl->mDepthIndex;
562 void Visual::Base::SetOnScene(Actor& actor)
566 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
567 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
572 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
573 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
576 mImpl->mFlags |= Impl::IS_ON_SCENE;
580 void Visual::Base::SetOffScene(Actor& actor)
584 DoSetOffScene(actor);
585 mImpl->mFlags &= ~Impl::IS_ON_SCENE;
589 void Visual::Base::CreatePropertyMap(Property::Map& map) const
593 // Update values from Renderer
594 mImpl->mMixColor = mImpl->mRenderer.GetProperty<Vector3>(VisualRenderer::Property::VISUAL_MIX_COLOR);
595 mImpl->mMixColor.a = mImpl->mRenderer.GetProperty<float>(DevelRenderer::Property::OPACITY);
597 mImpl->mTransform.mOffset = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_OFFSET);
598 mImpl->mTransform.mSize = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_SIZE);
600 if(IsRoundedCornerRequired())
602 mImpl->SetCornerRadius(mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::CORNER_RADIUS));
604 if(IsBorderlineRequired())
606 mImpl->SetBorderlineWidth(mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH));
607 mImpl->SetBorderlineColor(mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::BORDERLINE_COLOR));
608 mImpl->SetBorderlineOffset(mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET));
612 DoCreatePropertyMap(map);
614 if(mImpl->mCustomShader)
616 mImpl->mCustomShader->CreatePropertyMap(map);
619 Property::Map transform;
620 mImpl->mTransform.GetPropertyMap(transform);
621 map.Insert(Toolkit::Visual::Property::TRANSFORM, transform);
623 bool premultipliedAlpha(IsPreMultipliedAlphaEnabled());
624 map.Insert(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha);
626 // Note, Color and Primitive will also insert their own mix color into the map
627 // which is ok, because they have a different key value range, but uses same cached value anyway.
628 map.Insert(Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor); // vec4
629 map.Insert(Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a);
631 auto fittingModeString = Scripting::GetLinearEnumerationName<FittingMode>(
632 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT);
633 map.Insert(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString);
635 if(IsTypeAvailableForBorderline(mImpl->mType))
637 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
638 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
639 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
642 if(IsTypeAvailableForCornerRadius(mImpl->mType))
644 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
645 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, mImpl->GetCornerRadiusPolicy());
649 void Visual::Base::CreateInstancePropertyMap(Property::Map& map) const
651 DoCreateInstancePropertyMap(map);
653 if(mImpl->mCustomShader)
655 mImpl->mCustomShader->CreatePropertyMap(map);
659 void Visual::Base::EnablePreMultipliedAlpha(bool preMultiplied)
663 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
667 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
672 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
673 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultiplied);
677 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
679 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
682 void Visual::Base::DoSetOffScene(Actor& actor)
684 actor.RemoveRenderer(mImpl->mRenderer);
687 bool Visual::Base::IsOnScene() const
689 return mImpl->mFlags & Impl::IS_ON_SCENE;
692 bool Visual::Base::IsRoundedCornerRequired() const
694 // If VisualType doesn't support rounded corner, always return false.
695 if(IsTypeAvailableForCornerRadius(mImpl->mType))
699 // Update values from Renderer
700 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS);
702 Vector4 retValue = Vector4::ZERO;
703 if(value.Get(retValue))
705 if(mImpl->mDecorationData != nullptr || retValue != Vector4::ZERO)
707 mImpl->SetCornerRadius(retValue);
711 return mImpl->mAlwaysUsingCornerRadius || !(mImpl->GetCornerRadius() == Vector4::ZERO);
716 bool Visual::Base::IsBorderlineRequired() const
718 // If VisualType doesn't support borderline, always return false.
719 if(IsTypeAvailableForBorderline(mImpl->mType))
723 // Update values from Renderer
724 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH);
726 float retValue = 0.0f;
727 if(value.Get(retValue))
729 if(mImpl->mDecorationData != nullptr || !Dali::EqualsZero(retValue))
731 mImpl->SetBorderlineWidth(retValue);
735 return mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->GetBorderlineWidth());
740 void Visual::Base::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
742 // May be overriden by derived class
745 void Visual::Base::OnDoActionExtension(const Property::Index actionId, const Dali::Any attributes)
747 // May be overriden by derived class
750 void Visual::Base::RegisterMixColor()
754 // All visual renderers now use same mix color / opacity properties.
755 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
756 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, mImpl->mMixColor.a);
758 float preMultipliedAlpha = 0.0f;
759 if(IsPreMultipliedAlphaEnabled())
761 preMultipliedAlpha = 1.0f;
763 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultipliedAlpha);
767 void Visual::Base::RegisterDecoration()
771 if(IsTypeAvailableForCornerRadius(mImpl->mType))
773 if(mImpl->mAlwaysUsingCornerRadius || !(mImpl->GetCornerRadius() == Vector4::ZERO))
775 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
776 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
777 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
780 if(IsTypeAvailableForBorderline(mImpl->mType))
782 if(mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->GetBorderlineWidth()))
784 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
785 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
786 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
787 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
793 void Visual::Base::SetMixColor(const Vector4& color)
795 mImpl->mMixColor = color;
799 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(color));
800 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, color.a);
804 void Visual::Base::SetMixColor(const Vector3& color)
806 mImpl->mMixColor.r = color.r;
807 mImpl->mMixColor.g = color.g;
808 mImpl->mMixColor.b = color.b;
812 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, color);
816 void Visual::Base::AddEventObserver(Visual::EventObserver& observer)
818 mImpl->mEventObserver = &observer;
821 void Visual::Base::RemoveEventObserver(Visual::EventObserver& observer)
823 mImpl->mEventObserver = NULL;
826 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
828 if(mImpl->mResourceStatus != resourceStatus)
830 mImpl->mResourceStatus = resourceStatus;
832 if(mImpl->mEventObserver)
834 // observer is currently a control impl
835 mImpl->mEventObserver->ResourceReady(*this);
840 bool Visual::Base::IsResourceReady() const
842 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
843 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
846 bool Visual::Base::IsSynchronousLoadingRequired() const
848 return (mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING);
851 Toolkit::Visual::Type Visual::Base::GetType() const
856 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
858 return mImpl->mResourceStatus;
861 Visual::FittingMode Visual::Base::GetFittingMode() const
863 return mImpl->mFittingMode;
866 Visual::Base& Visual::Base::GetVisualObject()
871 Renderer Visual::Base::GetRenderer()
873 return mImpl->mRenderer;
876 Property::Index Visual::Base::GetIntKey(Property::Key key)
878 if(key.type == Property::Key::INDEX)
883 if(key.stringKey == ANCHOR_POINT)
885 return Toolkit::Visual::Transform::Property::ANCHOR_POINT;
887 else if(key.stringKey == EXTRA_SIZE)
889 return Toolkit::DevelVisual::Transform::Property::EXTRA_SIZE;
891 else if(key.stringKey == MIX_COLOR)
893 return Toolkit::Visual::Property::MIX_COLOR;
895 else if(key.stringKey == OPACITY)
897 return Toolkit::Visual::Property::OPACITY;
899 else if(key.stringKey == OFFSET)
901 return Toolkit::Visual::Transform::Property::OFFSET;
903 else if(key.stringKey == OFFSET_POLICY)
905 return Toolkit::Visual::Transform::Property::OFFSET_POLICY;
907 else if(key.stringKey == ORIGIN)
909 return Toolkit::Visual::Transform::Property::ORIGIN;
911 else if(key.stringKey == PREMULTIPLIED_ALPHA)
913 return Toolkit::Visual::Property::PREMULTIPLIED_ALPHA;
915 else if(key.stringKey == CUSTOM_SHADER)
917 return Toolkit::Visual::Property::SHADER;
919 else if(key.stringKey == SIZE)
921 return Toolkit::Visual::Transform::Property::SIZE;
923 else if(key.stringKey == SIZE_POLICY)
925 return Toolkit::Visual::Transform::Property::SIZE_POLICY;
927 else if(key.stringKey == TRANSFORM)
929 return Toolkit::Visual::Property::TRANSFORM;
931 else if(key.stringKey == VISUAL_FITTING_MODE)
933 return Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE;
935 else if(key.stringKey == CORNER_RADIUS)
937 return Toolkit::DevelVisual::Property::CORNER_RADIUS;
939 else if(key.stringKey == CORNER_RADIUS_POLICY)
941 return Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY;
943 else if(key.stringKey == BORDERLINE_WIDTH)
945 return Toolkit::DevelVisual::Property::BORDERLINE_WIDTH;
947 else if(key.stringKey == BORDERLINE_COLOR)
949 return Toolkit::DevelVisual::Property::BORDERLINE_COLOR;
951 else if(key.stringKey == BORDERLINE_OFFSET)
953 return Toolkit::DevelVisual::Property::BORDERLINE_OFFSET;
956 return Property::INVALID_INDEX;
959 Property::Index Visual::Base::GetPropertyIndex(Property::Key key)
961 switch(GetIntKey(key))
963 case Dali::Toolkit::Visual::Transform::Property::OFFSET:
965 return VisualRenderer::Property::TRANSFORM_OFFSET;
967 case Dali::Toolkit::Visual::Transform::Property::SIZE:
969 return VisualRenderer::Property::TRANSFORM_SIZE;
971 case Dali::Toolkit::Visual::Transform::Property::ORIGIN:
973 return VisualRenderer::Property::TRANSFORM_ORIGIN;
975 case Dali::Toolkit::Visual::Transform::Property::ANCHOR_POINT:
977 return VisualRenderer::Property::TRANSFORM_ANCHOR_POINT;
979 case Dali::Toolkit::Visual::Property::MIX_COLOR:
981 return VisualRenderer::Property::VISUAL_MIX_COLOR;
983 case Dali::Toolkit::Visual::Property::OPACITY:
985 return DevelRenderer::Property::OPACITY;
987 case Dali::Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
989 return VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA;
991 case Dali::Toolkit::DevelVisual::Property::CORNER_RADIUS:
993 return DecoratedVisualRenderer::Property::CORNER_RADIUS;
995 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
997 return DecoratedVisualRenderer::Property::BORDERLINE_WIDTH;
999 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
1001 return DecoratedVisualRenderer::Property::BORDERLINE_COLOR;
1003 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
1005 return DecoratedVisualRenderer::Property::BORDERLINE_OFFSET;
1009 Property::Index index = mImpl->mRenderer.GetPropertyIndex(key);
1011 if(index == Property::INVALID_INDEX)
1013 // Is it a shader property?
1014 Shader shader = mImpl->mRenderer.GetShader();
1015 index = shader.GetPropertyIndex(key);
1016 if(index != Property::INVALID_INDEX)
1018 // Yes - we should register it in the Renderer so it can be set / animated
1019 // independently, as shaders are shared across multiple renderers.
1020 std::string keyName;
1021 Property::Index keyIndex(Property::INVALID_KEY);
1022 if(key.type == Property::Key::INDEX)
1024 keyName = shader.GetPropertyName(index);
1025 keyIndex = key.indexKey;
1029 keyName = key.stringKey;
1030 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
1032 Property::Value value = shader.GetProperty(index);
1034 // We already know that mRenderer didn't have property. So we can assume that it is unique.
1035 index = mImpl->mRenderer.RegisterUniqueProperty(keyIndex, keyName, value);
1041 void Visual::Base::SetupTransition(
1042 Dali::Animation& transition,
1043 Internal::TransitionData::Animator& animator,
1044 Property::Index index,
1045 Property::Value& initialValue,
1046 Property::Value& targetValue)
1048 if(index != Property::INVALID_INDEX)
1050 if(mImpl->mRenderer)
1052 if(animator.animate == false)
1054 mImpl->mRenderer.SetProperty(index, targetValue);
1058 if(animator.initialValue.GetType() != Property::NONE)
1060 mImpl->mRenderer.SetProperty(index, initialValue);
1065 transition = Dali::Animation::New(0.1f);
1068 transition.AnimateTo(Property(mImpl->mRenderer, index),
1070 animator.alphaFunction,
1071 TimePeriod(animator.timePeriodDelay,
1072 animator.timePeriodDuration));
1078 void Visual::Base::AnimateProperty(
1079 Dali::Animation& transition,
1080 Internal::TransitionData::Animator& animator)
1082 #if defined(DEBUG_ENABLED)
1083 if(gVisualBaseLogFilter->IsEnabledFor(Debug::General))
1085 std::ostringstream oss;
1086 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
1087 DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, oss.str().c_str());
1091 if(animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
1092 animator.propertyKey == MIX_COLOR ||
1093 (mImpl->mType == Toolkit::Visual::COLOR &&
1094 animator.propertyKey == ColorVisual::Property::MIX_COLOR) ||
1095 (mImpl->mType == Toolkit::Visual::PRIMITIVE &&
1096 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR))
1098 AnimateMixColorProperty(transition, animator);
1100 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
1101 animator.propertyKey == OPACITY)
1103 AnimateOpacityProperty(transition, animator);
1105 else if(mImpl->mRenderer)
1107 AnimateRendererProperty(transition, animator);
1111 void Visual::Base::AnimateOpacityProperty(
1112 Dali::Animation& transition,
1113 Internal::TransitionData::Animator& animator)
1115 float targetOpacity;
1116 if(animator.targetValue.Get(targetOpacity))
1118 mImpl->mMixColor.a = targetOpacity;
1121 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue);
1124 void Visual::Base::AnimateRendererProperty(
1125 Dali::Animation& transition,
1126 Internal::TransitionData::Animator& animator)
1128 // Get actual renderer index (will convert transform keys into visualproperty indices)
1129 Property::Index index = GetPropertyIndex(animator.propertyKey);
1131 if(index != Property::INVALID_INDEX)
1133 if(animator.targetValue.GetType() != Property::NONE)
1135 // Try writing target value into transform property map
1136 // if it's not a valid key, then it won't alter mTransform
1138 if(animator.propertyKey.type == Property::Key::INDEX)
1140 map.Add(animator.propertyKey.indexKey, animator.targetValue);
1144 map.Add(animator.propertyKey.stringKey, animator.targetValue);
1147 mImpl->mTransform.UpdatePropertyMap(map);
1149 SetupTransition(transition, animator, index, animator.initialValue, animator.targetValue);
1153 void Visual::Base::AnimateMixColorProperty(
1154 Dali::Animation& transition,
1155 Internal::TransitionData::Animator& animator)
1157 bool animateOpacity = false;
1159 Property::Value initialOpacity;
1160 Property::Value targetOpacity;
1161 Property::Value initialMixColor;
1162 Property::Value targetMixColor;
1164 Vector4 initialColor;
1165 if(animator.initialValue.Get(initialColor))
1167 if(animator.initialValue.GetType() == Property::VECTOR4)
1169 // if there is an initial color specifying alpha, test it
1170 initialOpacity = initialColor.a;
1172 initialMixColor = Vector3(initialColor);
1175 // Set target value into data store
1176 if(animator.targetValue.GetType() != Property::NONE)
1179 animator.targetValue.Get(mixColor);
1180 if(animator.targetValue.GetType() == Property::VECTOR4)
1182 mImpl->mMixColor.a = mixColor.a;
1183 targetOpacity = mixColor.a;
1184 animateOpacity = true;
1187 mImpl->mMixColor.r = mixColor.r;
1188 mImpl->mMixColor.g = mixColor.g;
1189 mImpl->mMixColor.b = mixColor.b;
1190 targetMixColor = Vector3(mixColor);
1193 SetupTransition(transition, animator, VisualRenderer::Property::VISUAL_MIX_COLOR, initialMixColor, targetMixColor);
1197 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity);
1201 Dali::Property Visual::Base::GetPropertyObject(Dali::Property::Key key)
1203 if(!mImpl->mRenderer)
1206 return Dali::Property(handle, Property::INVALID_INDEX);
1209 switch(GetIntKey(key))
1211 // Default animatable properties from VisualRenderer
1212 case Toolkit::Visual::Property::MIX_COLOR:
1214 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1216 case Toolkit::Visual::Property::OPACITY:
1218 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1220 case Toolkit::Visual::Transform::Property::OFFSET:
1222 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_OFFSET);
1224 case Toolkit::Visual::Transform::Property::SIZE:
1226 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_SIZE);
1229 // Default animatable properties from DecoratedVisualRenderer
1230 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
1232 if(IsTypeAvailableForCornerRadius(mImpl->mType))
1234 const bool updateShader = !mImpl->mCustomShader && !IsRoundedCornerRequired();
1236 // CornerRadius is animated now. we always have to use corner radius feature.
1237 mImpl->mAlwaysUsingCornerRadius = true;
1241 // Update each values to renderer
1242 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
1243 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
1244 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
1249 if(!IsBorderlineRequired())
1251 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
1252 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
1254 return Dali::Property(mImpl->mRenderer, DecoratedVisualRenderer::Property::CORNER_RADIUS);
1258 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
1259 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
1260 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
1262 if(IsTypeAvailableForBorderline(mImpl->mType))
1264 const bool updateShader = !mImpl->mCustomShader && !IsBorderlineRequired();
1266 // Borderline is animated now. we always have to use borderline feature.
1267 mImpl->mAlwaysUsingBorderline = true;
1271 // Update each values to renderer
1272 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
1273 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
1274 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
1275 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
1280 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
1282 return Dali::Property(mImpl->mRenderer, GetPropertyIndex(key));
1288 // Special case for MIX_COLOR
1289 if(key.type == Property::Key::INDEX &&
1290 ((mImpl->mType == Toolkit::Visual::COLOR && key.indexKey == ColorVisual::Property::MIX_COLOR) ||
1291 (mImpl->mType == Toolkit::Visual::PRIMITIVE && key.indexKey == PrimitiveVisual::Property::MIX_COLOR)))
1293 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1296 // Special case for BLUR_RADIUS
1297 if(mImpl->mType == Toolkit::Visual::COLOR &&
1298 ((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) ||
1299 (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME)))
1301 // Request to color-visual class
1302 return OnGetPropertyObject(key);
1307 // If it is not VisualRenderer property, check registered Renderer and Shader property.
1308 Property::Index index = GetPropertyIndex(key);
1309 if(index != Property::INVALID_INDEX)
1311 return Dali::Property(mImpl->mRenderer, index);
1314 // We can't find the property in the base class.
1315 // Request to child class
1316 return OnGetPropertyObject(key);
1319 } // namespace Internal
1321 } // namespace Toolkit