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()
132 void Visual::Base::Initialize()
134 // The Renderer should be created inside derived class here.
140 RegisterDecoration();
142 if(IsBorderlineRequired())
144 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
146 else if(IsRoundedCornerRequired())
148 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
153 void Visual::Base::SetCustomShader(const Property::Map& shaderMap)
155 if(mImpl->mCustomShader)
157 mImpl->mCustomShader->SetPropertyMap(shaderMap);
161 mImpl->mCustomShader = new Impl::CustomShader(shaderMap);
164 // Let derived class know
168 void Visual::Base::SetProperties(const Property::Map& propertyMap)
170 bool needUpdateShader = false;
171 for(size_t i = 0; i < propertyMap.Count(); ++i)
173 const KeyValuePair& pair = propertyMap.GetKeyValue(i);
174 const Property::Key& key = pair.first;
175 const Property::Value& value = pair.second;
177 Property::Key matchKey = key;
178 if(matchKey.type == Property::Key::STRING)
180 if(matchKey == CUSTOM_SHADER)
182 matchKey = Property::Key(Toolkit::Visual::Property::SHADER);
184 else if(matchKey == TRANSFORM)
186 matchKey = Property::Key(Toolkit::Visual::Property::TRANSFORM);
188 else if(matchKey == PREMULTIPLIED_ALPHA)
190 matchKey = Property::Key(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA);
192 else if(matchKey == MIX_COLOR)
194 matchKey = Property::Key(Toolkit::Visual::Property::MIX_COLOR);
196 else if(matchKey == OPACITY)
198 matchKey = Property::Key(Toolkit::Visual::Property::OPACITY);
200 else if(matchKey == VISUAL_FITTING_MODE)
202 matchKey = Property::Key(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE);
204 else if(matchKey == BORDERLINE_WIDTH)
206 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH);
208 else if(matchKey == BORDERLINE_COLOR)
210 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_COLOR);
212 else if(matchKey == BORDERLINE_OFFSET)
214 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET);
216 else if(matchKey == CORNER_RADIUS)
218 matchKey = Property::Key(Toolkit::DevelVisual::Property::CORNER_RADIUS);
220 else if(matchKey == CORNER_RADIUS_POLICY)
222 matchKey = Property::Key(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY);
226 switch(matchKey.indexKey)
228 case Toolkit::Visual::Property::SHADER:
230 Property::Map shaderMap;
231 if(value.Get(shaderMap))
233 SetCustomShader(shaderMap);
238 case Toolkit::Visual::Property::TRANSFORM:
243 mImpl->mTransform.SetPropertyMap(map);
248 case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
250 bool premultipliedAlpha = false;
251 if(value.Get(premultipliedAlpha))
253 EnablePreMultipliedAlpha(premultipliedAlpha);
258 case Toolkit::Visual::Property::MIX_COLOR:
261 if(value.Get(mixColor))
263 if(value.GetType() == Property::VECTOR4)
265 SetMixColor(mixColor);
269 Vector3 mixColor3(mixColor);
270 SetMixColor(mixColor3);
275 case Toolkit::Visual::Property::OPACITY:
278 if(value.Get(opacity))
280 mImpl->mMixColor.a = opacity;
281 SetMixColor(mImpl->mMixColor);
285 case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
287 Scripting::GetEnumerationProperty<Visual::FittingMode>(
288 value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode);
291 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
296 if(mImpl->mDecorationData != nullptr || !Dali::EqualsZero(width))
298 mImpl->SetBorderlineWidth(width);
302 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
304 // Unusual case. SetProperty called after OnInitialize().
305 // Assume that DoAction call UPDATE_PROPERTY.
306 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
307 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
309 // Check whether we must update shader.
310 if(!mImpl->mAlwaysUsingBorderline && IsBorderlineRequired())
312 // Required to change shader mean, we didn't setup BORDERLINE_COLOR and BORDERLINE_OFFSET into mRenderer before. Set property now.
313 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
314 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
316 // Make Blend mode ON_WITHOUT_CULL for transparent mix color.
317 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
319 // Change the shader must not be occured many times. we always have to use borderline feature.
320 mImpl->mAlwaysUsingBorderline = true;
323 if(!mImpl->mCustomShader)
325 needUpdateShader = true;
331 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
336 if(mImpl->mDecorationData != nullptr || color != Vector4::ZERO)
338 mImpl->SetBorderlineColor(color);
342 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
344 // Unusual case. SetProperty called after OnInitialize().
345 // Assume that DoAction call UPDATE_PROPERTY.
346 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
350 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
353 if(value.Get(offset))
355 if(mImpl->mDecorationData != nullptr || !Dali::EqualsZero(offset))
357 mImpl->SetBorderlineOffset(offset);
361 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
363 // Unusual case. SetProperty called after OnInitialize().
364 // Assume that DoAction call UPDATE_PROPERTY.
365 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
369 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
371 if(value.GetType() == Property::VECTOR4)
373 // If CORNER_RADIUS Property is Vector4,
374 // Each values mean the radius of
375 // (top-left, top-right, bottom-right, bottom-left)
377 if(value.Get(radius))
379 if(mImpl->mDecorationData != nullptr || radius != Vector4::ZERO)
381 mImpl->SetCornerRadius(radius);
387 // If CORNER_RADIUS Property is float,
388 // Every corner radius have same value
390 if(value.Get(radius))
392 if(mImpl->mDecorationData != nullptr || !Dali::EqualsZero(radius))
394 mImpl->SetCornerRadius(Vector4(radius, radius, radius, radius));
399 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForCornerRadius(mImpl->mType)))
401 // Unusual case. SetProperty called after OnInitialize().
402 // Assume that DoAction call UPDATE_PROPERTY.
403 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
404 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
406 // Check whether we must update shader.
407 if(!mImpl->mAlwaysUsingCornerRadius && IsRoundedCornerRequired())
409 // Required to change shader mean, we didn't setup CORNER_RADIUS_POLICY into mRenderer before. Set property now.
410 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
412 // Change the shader must not be occured many times. we always have to use corner radius feature.
413 mImpl->mAlwaysUsingCornerRadius = true;
415 if(!IsBorderlineRequired())
417 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
418 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
422 if(!mImpl->mCustomShader)
424 needUpdateShader = true;
431 case Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY:
434 if(value.Get(policy))
438 case Toolkit::Visual::Transform::Policy::RELATIVE:
439 case Toolkit::Visual::Transform::Policy::ABSOLUTE:
441 mImpl->SetCornerRadiusPolicy(policy);
442 if(DALI_UNLIKELY(mImpl->mRenderer))
444 // Unusual case. SetProperty called after OnInitialize().
445 // Assume that DoAction call UPDATE_PROPERTY.
446 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
452 DALI_LOG_ERROR("Unsupported policy: %d\n", policy);
462 DoSetProperties(propertyMap);
464 if(DALI_UNLIKELY(needUpdateShader))
470 void Visual::Base::SetTransformAndSize(const Property::Map& transform, Size controlSize)
472 mImpl->mControlSize = controlSize;
473 mImpl->mTransform.UpdatePropertyMap(transform);
475 #if defined(DEBUG_ENABLED)
476 std::ostringstream oss;
478 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);
484 void Visual::Base::SetName(const std::string& name)
489 const std::string& Visual::Base::GetName() const
494 float Visual::Base::GetHeightForWidth(float width)
496 float aspectCorrectedHeight = 0.f;
498 GetNaturalSize(naturalSize);
499 if(naturalSize.width)
501 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
503 return aspectCorrectedHeight;
506 float Visual::Base::GetWidthForHeight(float height)
508 float aspectCorrectedWidth = 0.f;
510 GetNaturalSize(naturalSize);
511 if(naturalSize.height > 0.0f)
513 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
515 return aspectCorrectedWidth;
518 void Visual::Base::GetNaturalSize(Vector2& naturalSize)
520 naturalSize = Vector2::ZERO;
523 void Visual::Base::DoAction(const Property::Index actionId, const Property::Value attributes)
525 OnDoAction(actionId, attributes);
527 // Check if action is valid for this visual type and perform action if possible
530 case DevelVisual::Action::UPDATE_PROPERTY:
532 const Property::Map* map = attributes.GetMap();
542 void Visual::Base::DoActionExtension(const Dali::Property::Index actionId, const Dali::Any attributes)
544 OnDoActionExtension(actionId, attributes);
547 void Visual::Base::SetDepthIndex(int index)
549 mImpl->mDepthIndex = index;
552 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
556 int Visual::Base::GetDepthIndex() const
558 return mImpl->mDepthIndex;
561 void Visual::Base::SetOnScene(Actor& actor)
565 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
566 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
571 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
572 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
575 mImpl->mFlags |= Impl::IS_ON_SCENE;
579 void Visual::Base::SetOffScene(Actor& actor)
583 DoSetOffScene(actor);
584 mImpl->mFlags &= ~Impl::IS_ON_SCENE;
588 void Visual::Base::CreatePropertyMap(Property::Map& map) const
592 // Update values from Renderer
593 mImpl->mMixColor = mImpl->mRenderer.GetProperty<Vector3>(VisualRenderer::Property::VISUAL_MIX_COLOR);
594 mImpl->mMixColor.a = mImpl->mRenderer.GetProperty<float>(DevelRenderer::Property::OPACITY);
596 mImpl->mTransform.mOffset = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_OFFSET);
597 mImpl->mTransform.mSize = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_SIZE);
599 if(IsRoundedCornerRequired())
601 mImpl->SetCornerRadius(mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::CORNER_RADIUS));
603 if(IsBorderlineRequired())
605 mImpl->SetBorderlineWidth(mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH));
606 mImpl->SetBorderlineColor(mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::BORDERLINE_COLOR));
607 mImpl->SetBorderlineOffset(mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET));
611 DoCreatePropertyMap(map);
613 if(mImpl->mCustomShader)
615 mImpl->mCustomShader->CreatePropertyMap(map);
618 Property::Map transform;
619 mImpl->mTransform.GetPropertyMap(transform);
620 map.Insert(Toolkit::Visual::Property::TRANSFORM, transform);
622 bool premultipliedAlpha(IsPreMultipliedAlphaEnabled());
623 map.Insert(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha);
625 // Note, Color and Primitive will also insert their own mix color into the map
626 // which is ok, because they have a different key value range, but uses same cached value anyway.
627 map.Insert(Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor); // vec4
628 map.Insert(Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a);
630 auto fittingModeString = Scripting::GetLinearEnumerationName<FittingMode>(
631 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT);
632 map.Insert(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString);
634 if(IsTypeAvailableForBorderline(mImpl->mType))
636 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
637 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
638 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
641 if(IsTypeAvailableForCornerRadius(mImpl->mType))
643 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
644 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, mImpl->GetCornerRadiusPolicy());
648 void Visual::Base::CreateInstancePropertyMap(Property::Map& map) const
650 DoCreateInstancePropertyMap(map);
652 if(mImpl->mCustomShader)
654 mImpl->mCustomShader->CreatePropertyMap(map);
658 void Visual::Base::EnablePreMultipliedAlpha(bool preMultiplied)
662 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
666 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
671 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
672 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultiplied);
676 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
678 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
681 void Visual::Base::DoSetOffScene(Actor& actor)
683 actor.RemoveRenderer(mImpl->mRenderer);
686 bool Visual::Base::IsOnScene() const
688 return mImpl->mFlags & Impl::IS_ON_SCENE;
691 bool Visual::Base::IsRoundedCornerRequired() const
693 // If VisualType doesn't support rounded corner, always return false.
694 if(IsTypeAvailableForCornerRadius(mImpl->mType))
698 // Update values from Renderer
699 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS);
701 Vector4 retValue = Vector4::ZERO;
702 if(value.Get(retValue))
704 if(mImpl->mDecorationData != nullptr || retValue != Vector4::ZERO)
706 mImpl->SetCornerRadius(retValue);
710 return mImpl->mAlwaysUsingCornerRadius || !(mImpl->GetCornerRadius() == Vector4::ZERO);
715 bool Visual::Base::IsBorderlineRequired() const
717 // If VisualType doesn't support borderline, always return false.
718 if(IsTypeAvailableForBorderline(mImpl->mType))
722 // Update values from Renderer
723 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH);
725 float retValue = 0.0f;
726 if(value.Get(retValue))
728 if(mImpl->mDecorationData != nullptr || !Dali::EqualsZero(retValue))
730 mImpl->SetBorderlineWidth(retValue);
734 return mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->GetBorderlineWidth());
739 void Visual::Base::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
741 // May be overriden by derived class
744 void Visual::Base::OnDoActionExtension(const Property::Index actionId, const Dali::Any attributes)
746 // May be overriden by derived class
749 void Visual::Base::RegisterMixColor()
753 // All visual renderers now use same mix color / opacity properties.
754 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
755 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, mImpl->mMixColor.a);
757 float preMultipliedAlpha = 0.0f;
758 if(IsPreMultipliedAlphaEnabled())
760 preMultipliedAlpha = 1.0f;
762 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultipliedAlpha);
766 void Visual::Base::RegisterDecoration()
770 if(IsTypeAvailableForCornerRadius(mImpl->mType))
772 if(mImpl->mAlwaysUsingCornerRadius || !(mImpl->GetCornerRadius() == Vector4::ZERO))
774 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
775 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
776 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
779 if(IsTypeAvailableForBorderline(mImpl->mType))
781 if(mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->GetBorderlineWidth()))
783 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
784 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
785 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
786 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
792 void Visual::Base::SetMixColor(const Vector4& color)
794 mImpl->mMixColor = color;
798 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(color));
799 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, color.a);
803 void Visual::Base::SetMixColor(const Vector3& color)
805 mImpl->mMixColor.r = color.r;
806 mImpl->mMixColor.g = color.g;
807 mImpl->mMixColor.b = color.b;
811 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, color);
815 void Visual::Base::AddEventObserver(Visual::EventObserver& observer)
817 mImpl->mEventObserver = &observer;
820 void Visual::Base::RemoveEventObserver(Visual::EventObserver& observer)
822 mImpl->mEventObserver = NULL;
825 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
827 if(mImpl->mResourceStatus != resourceStatus)
829 mImpl->mResourceStatus = resourceStatus;
831 if(mImpl->mEventObserver)
833 // observer is currently a control impl
834 mImpl->mEventObserver->ResourceReady(*this);
839 bool Visual::Base::IsResourceReady() const
841 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
842 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
845 bool Visual::Base::IsSynchronousLoadingRequired() const
847 return (mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING);
850 Toolkit::Visual::Type Visual::Base::GetType() const
855 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
857 return mImpl->mResourceStatus;
860 Visual::FittingMode Visual::Base::GetFittingMode() const
862 return mImpl->mFittingMode;
865 Visual::Base& Visual::Base::GetVisualObject()
870 Renderer Visual::Base::GetRenderer()
872 return mImpl->mRenderer;
875 Property::Index Visual::Base::GetIntKey(Property::Key key)
877 if(key.type == Property::Key::INDEX)
882 if(key.stringKey == ANCHOR_POINT)
884 return Toolkit::Visual::Transform::Property::ANCHOR_POINT;
886 else if(key.stringKey == EXTRA_SIZE)
888 return Toolkit::DevelVisual::Transform::Property::EXTRA_SIZE;
890 else if(key.stringKey == MIX_COLOR)
892 return Toolkit::Visual::Property::MIX_COLOR;
894 else if(key.stringKey == OPACITY)
896 return Toolkit::Visual::Property::OPACITY;
898 else if(key.stringKey == OFFSET)
900 return Toolkit::Visual::Transform::Property::OFFSET;
902 else if(key.stringKey == OFFSET_POLICY)
904 return Toolkit::Visual::Transform::Property::OFFSET_POLICY;
906 else if(key.stringKey == ORIGIN)
908 return Toolkit::Visual::Transform::Property::ORIGIN;
910 else if(key.stringKey == PREMULTIPLIED_ALPHA)
912 return Toolkit::Visual::Property::PREMULTIPLIED_ALPHA;
914 else if(key.stringKey == CUSTOM_SHADER)
916 return Toolkit::Visual::Property::SHADER;
918 else if(key.stringKey == SIZE)
920 return Toolkit::Visual::Transform::Property::SIZE;
922 else if(key.stringKey == SIZE_POLICY)
924 return Toolkit::Visual::Transform::Property::SIZE_POLICY;
926 else if(key.stringKey == TRANSFORM)
928 return Toolkit::Visual::Property::TRANSFORM;
930 else if(key.stringKey == VISUAL_FITTING_MODE)
932 return Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE;
934 else if(key.stringKey == CORNER_RADIUS)
936 return Toolkit::DevelVisual::Property::CORNER_RADIUS;
938 else if(key.stringKey == CORNER_RADIUS_POLICY)
940 return Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY;
942 else if(key.stringKey == BORDERLINE_WIDTH)
944 return Toolkit::DevelVisual::Property::BORDERLINE_WIDTH;
946 else if(key.stringKey == BORDERLINE_COLOR)
948 return Toolkit::DevelVisual::Property::BORDERLINE_COLOR;
950 else if(key.stringKey == BORDERLINE_OFFSET)
952 return Toolkit::DevelVisual::Property::BORDERLINE_OFFSET;
955 return Property::INVALID_INDEX;
958 Property::Index Visual::Base::GetPropertyIndex(Property::Key key)
960 switch(GetIntKey(key))
962 case Dali::Toolkit::Visual::Transform::Property::OFFSET:
964 return VisualRenderer::Property::TRANSFORM_OFFSET;
966 case Dali::Toolkit::Visual::Transform::Property::SIZE:
968 return VisualRenderer::Property::TRANSFORM_SIZE;
970 case Dali::Toolkit::Visual::Transform::Property::ORIGIN:
972 return VisualRenderer::Property::TRANSFORM_ORIGIN;
974 case Dali::Toolkit::Visual::Transform::Property::ANCHOR_POINT:
976 return VisualRenderer::Property::TRANSFORM_ANCHOR_POINT;
978 case Dali::Toolkit::Visual::Property::MIX_COLOR:
980 return VisualRenderer::Property::VISUAL_MIX_COLOR;
982 case Dali::Toolkit::Visual::Property::OPACITY:
984 return DevelRenderer::Property::OPACITY;
986 case Dali::Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
988 return VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA;
990 case Dali::Toolkit::DevelVisual::Property::CORNER_RADIUS:
992 return DecoratedVisualRenderer::Property::CORNER_RADIUS;
994 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
996 return DecoratedVisualRenderer::Property::BORDERLINE_WIDTH;
998 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
1000 return DecoratedVisualRenderer::Property::BORDERLINE_COLOR;
1002 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
1004 return DecoratedVisualRenderer::Property::BORDERLINE_OFFSET;
1008 Property::Index index = mImpl->mRenderer.GetPropertyIndex(key);
1010 if(index == Property::INVALID_INDEX)
1012 // Is it a shader property?
1013 Shader shader = mImpl->mRenderer.GetShader();
1014 index = shader.GetPropertyIndex(key);
1015 if(index != Property::INVALID_INDEX)
1017 // Yes - we should register it in the Renderer so it can be set / animated
1018 // independently, as shaders are shared across multiple renderers.
1019 std::string keyName;
1020 Property::Index keyIndex(Property::INVALID_KEY);
1021 if(key.type == Property::Key::INDEX)
1023 keyName = shader.GetPropertyName(index);
1024 keyIndex = key.indexKey;
1028 keyName = key.stringKey;
1029 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
1031 Property::Value value = shader.GetProperty(index);
1033 // We already know that mRenderer didn't have property. So we can assume that it is unique.
1034 index = mImpl->mRenderer.RegisterUniqueProperty(keyIndex, keyName, value);
1040 void Visual::Base::SetupTransition(
1041 Dali::Animation& transition,
1042 Internal::TransitionData::Animator& animator,
1043 Property::Index index,
1044 Property::Value& initialValue,
1045 Property::Value& targetValue)
1047 if(index != Property::INVALID_INDEX)
1049 if(mImpl->mRenderer)
1051 if(animator.animate == false)
1053 mImpl->mRenderer.SetProperty(index, targetValue);
1057 if(animator.initialValue.GetType() != Property::NONE)
1059 mImpl->mRenderer.SetProperty(index, initialValue);
1064 transition = Dali::Animation::New(0.1f);
1067 transition.AnimateTo(Property(mImpl->mRenderer, index),
1069 animator.alphaFunction,
1070 TimePeriod(animator.timePeriodDelay,
1071 animator.timePeriodDuration));
1077 void Visual::Base::AnimateProperty(
1078 Dali::Animation& transition,
1079 Internal::TransitionData::Animator& animator)
1081 #if defined(DEBUG_ENABLED)
1082 if(gVisualBaseLogFilter->IsEnabledFor(Debug::General))
1084 std::ostringstream oss;
1085 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
1086 DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, oss.str().c_str());
1090 if(animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
1091 animator.propertyKey == MIX_COLOR ||
1092 (mImpl->mType == Toolkit::Visual::COLOR &&
1093 animator.propertyKey == ColorVisual::Property::MIX_COLOR) ||
1094 (mImpl->mType == Toolkit::Visual::PRIMITIVE &&
1095 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR))
1097 AnimateMixColorProperty(transition, animator);
1099 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
1100 animator.propertyKey == OPACITY)
1102 AnimateOpacityProperty(transition, animator);
1104 else if(mImpl->mRenderer)
1106 AnimateRendererProperty(transition, animator);
1110 void Visual::Base::AnimateOpacityProperty(
1111 Dali::Animation& transition,
1112 Internal::TransitionData::Animator& animator)
1114 float targetOpacity;
1115 if(animator.targetValue.Get(targetOpacity))
1117 mImpl->mMixColor.a = targetOpacity;
1120 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue);
1123 void Visual::Base::AnimateRendererProperty(
1124 Dali::Animation& transition,
1125 Internal::TransitionData::Animator& animator)
1127 // Get actual renderer index (will convert transform keys into visualproperty indices)
1128 Property::Index index = GetPropertyIndex(animator.propertyKey);
1130 if(index != Property::INVALID_INDEX)
1132 if(animator.targetValue.GetType() != Property::NONE)
1134 // Try writing target value into transform property map
1135 // if it's not a valid key, then it won't alter mTransform
1137 if(animator.propertyKey.type == Property::Key::INDEX)
1139 map.Add(animator.propertyKey.indexKey, animator.targetValue);
1143 map.Add(animator.propertyKey.stringKey, animator.targetValue);
1146 mImpl->mTransform.UpdatePropertyMap(map);
1148 SetupTransition(transition, animator, index, animator.initialValue, animator.targetValue);
1152 void Visual::Base::AnimateMixColorProperty(
1153 Dali::Animation& transition,
1154 Internal::TransitionData::Animator& animator)
1156 bool animateOpacity = false;
1158 Property::Value initialOpacity;
1159 Property::Value targetOpacity;
1160 Property::Value initialMixColor;
1161 Property::Value targetMixColor;
1163 Vector4 initialColor;
1164 if(animator.initialValue.Get(initialColor))
1166 if(animator.initialValue.GetType() == Property::VECTOR4)
1168 // if there is an initial color specifying alpha, test it
1169 initialOpacity = initialColor.a;
1171 initialMixColor = Vector3(initialColor);
1174 // Set target value into data store
1175 if(animator.targetValue.GetType() != Property::NONE)
1178 animator.targetValue.Get(mixColor);
1179 if(animator.targetValue.GetType() == Property::VECTOR4)
1181 mImpl->mMixColor.a = mixColor.a;
1182 targetOpacity = mixColor.a;
1183 animateOpacity = true;
1186 mImpl->mMixColor.r = mixColor.r;
1187 mImpl->mMixColor.g = mixColor.g;
1188 mImpl->mMixColor.b = mixColor.b;
1189 targetMixColor = Vector3(mixColor);
1192 SetupTransition(transition, animator, VisualRenderer::Property::VISUAL_MIX_COLOR, initialMixColor, targetMixColor);
1196 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity);
1200 Dali::Property Visual::Base::GetPropertyObject(Dali::Property::Key key)
1202 if(!mImpl->mRenderer)
1205 return Dali::Property(handle, Property::INVALID_INDEX);
1208 switch(GetIntKey(key))
1210 // Default animatable properties from VisualRenderer
1211 case Toolkit::Visual::Property::MIX_COLOR:
1213 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1215 case Toolkit::Visual::Property::OPACITY:
1217 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1219 case Toolkit::Visual::Transform::Property::OFFSET:
1221 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_OFFSET);
1223 case Toolkit::Visual::Transform::Property::SIZE:
1225 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_SIZE);
1228 // Default animatable properties from DecoratedVisualRenderer
1229 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
1231 if(IsTypeAvailableForCornerRadius(mImpl->mType))
1233 const bool updateShader = !mImpl->mCustomShader && !IsRoundedCornerRequired();
1235 // CornerRadius is animated now. we always have to use corner radius feature.
1236 mImpl->mAlwaysUsingCornerRadius = true;
1240 // Update each values to renderer
1241 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
1242 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
1243 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
1248 if(!IsBorderlineRequired())
1250 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
1251 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
1253 return Dali::Property(mImpl->mRenderer, DecoratedVisualRenderer::Property::CORNER_RADIUS);
1257 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
1258 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
1259 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
1261 if(IsTypeAvailableForBorderline(mImpl->mType))
1263 const bool updateShader = !mImpl->mCustomShader && !IsBorderlineRequired();
1265 // Borderline is animated now. we always have to use borderline feature.
1266 mImpl->mAlwaysUsingBorderline = true;
1270 // Update each values to renderer
1271 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
1272 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
1273 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
1274 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
1279 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
1281 return Dali::Property(mImpl->mRenderer, GetPropertyIndex(key));
1287 // Special case for MIX_COLOR
1288 if(key.type == Property::Key::INDEX &&
1289 ((mImpl->mType == Toolkit::Visual::COLOR && key.indexKey == ColorVisual::Property::MIX_COLOR) ||
1290 (mImpl->mType == Toolkit::Visual::PRIMITIVE && key.indexKey == PrimitiveVisual::Property::MIX_COLOR)))
1292 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1295 // Special case for BLUR_RADIUS
1296 if(mImpl->mType == Toolkit::Visual::COLOR &&
1297 ((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) ||
1298 (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME)))
1300 // Request to color-visual class
1301 return OnGetPropertyObject(key);
1306 // If it is not VisualRenderer property, check registered Renderer and Shader property.
1307 Property::Index index = GetPropertyIndex(key);
1308 if(index != Property::INVALID_INDEX)
1310 return Dali::Property(mImpl->mRenderer, index);
1313 // We can't find the property in the base class.
1314 // Request to child class
1315 return OnGetPropertyObject(key);
1318 } // namespace Internal
1320 } // namespace Toolkit