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 mImpl->mBorderlineWidth = width;
299 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
301 // Unusual case. SetProperty called after OnInitialize().
302 // Assume that DoAction call UPDATE_PROPERTY.
303 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
304 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
306 // Check whether we must update shader.
307 if(!mImpl->mAlwaysUsingBorderline && IsBorderlineRequired())
309 // Required to change shader mean, we didn't setup BORDERLINE_COLOR and BORDERLINE_OFFSET into mRenderer before. Set property now.
310 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
311 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
313 // Make Blend mode ON_WITHOUT_CULL for transparent mix color.
314 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
316 // Change the shader must not be occured many times. we always have to use borderline feature.
317 mImpl->mAlwaysUsingBorderline = true;
320 if(!mImpl->mCustomShader)
322 needUpdateShader = true;
328 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
333 mImpl->mBorderlineColor = color;
336 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
338 // Unusual case. SetProperty called after OnInitialize().
339 // Assume that DoAction call UPDATE_PROPERTY.
340 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
344 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
347 if(value.Get(offset))
349 mImpl->mBorderlineOffset = offset;
352 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
354 // Unusual case. SetProperty called after OnInitialize().
355 // Assume that DoAction call UPDATE_PROPERTY.
356 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
360 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
362 if(value.GetType() == Property::VECTOR4)
364 // If CORNER_RADIUS Property is Vector4,
365 // Each values mean the radius of
366 // (top-left, top-right, bottom-right, bottom-left)
368 if(value.Get(radius))
370 mImpl->mCornerRadius = radius;
375 // If CORNER_RADIUS Property is float,
376 // Every corner radius have same value
378 if(value.Get(radius))
380 mImpl->mCornerRadius = Vector4(radius, radius, radius, radius);
384 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForCornerRadius(mImpl->mType)))
386 // Unusual case. SetProperty called after OnInitialize().
387 // Assume that DoAction call UPDATE_PROPERTY.
388 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
389 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->mCornerRadius);
391 // Check whether we must update shader.
392 if(!mImpl->mAlwaysUsingCornerRadius && IsRoundedCornerRequired())
394 // Required to change shader mean, we didn't setup CORNER_RADIUS_POLICY into mRenderer before. Set property now.
395 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
397 // Change the shader must not be occured many times. we always have to use corner radius feature.
398 mImpl->mAlwaysUsingCornerRadius = true;
400 if(!IsBorderlineRequired())
402 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
403 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
407 if(!mImpl->mCustomShader)
409 needUpdateShader = true;
416 case Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY:
419 if(value.Get(policy))
423 case Toolkit::Visual::Transform::Policy::RELATIVE:
424 case Toolkit::Visual::Transform::Policy::ABSOLUTE:
426 mImpl->mCornerRadiusPolicy = policy;
427 if(DALI_UNLIKELY(mImpl->mRenderer))
429 // Unusual case. SetProperty called after OnInitialize().
430 // Assume that DoAction call UPDATE_PROPERTY.
431 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
437 DALI_LOG_ERROR("Unsupported policy: %d\n", policy);
447 DoSetProperties(propertyMap);
449 if(DALI_UNLIKELY(needUpdateShader))
455 void Visual::Base::SetTransformAndSize(const Property::Map& transform, Size controlSize)
457 mImpl->mControlSize = controlSize;
458 mImpl->mTransform.UpdatePropertyMap(transform);
460 #if defined(DEBUG_ENABLED)
461 std::ostringstream oss;
463 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);
469 void Visual::Base::SetName(const std::string& name)
474 const std::string& Visual::Base::GetName() const
479 float Visual::Base::GetHeightForWidth(float width)
481 float aspectCorrectedHeight = 0.f;
483 GetNaturalSize(naturalSize);
484 if(naturalSize.width)
486 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
488 return aspectCorrectedHeight;
491 float Visual::Base::GetWidthForHeight(float height)
493 float aspectCorrectedWidth = 0.f;
495 GetNaturalSize(naturalSize);
496 if(naturalSize.height > 0.0f)
498 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
500 return aspectCorrectedWidth;
503 void Visual::Base::GetNaturalSize(Vector2& naturalSize)
505 naturalSize = Vector2::ZERO;
508 void Visual::Base::DoAction(const Property::Index actionId, const Property::Value attributes)
510 OnDoAction(actionId, attributes);
512 // Check if action is valid for this visual type and perform action if possible
515 case DevelVisual::Action::UPDATE_PROPERTY:
517 const Property::Map* map = attributes.GetMap();
527 void Visual::Base::DoActionExtension(const Dali::Property::Index actionId, const Dali::Any attributes)
529 OnDoActionExtension(actionId, attributes);
532 void Visual::Base::SetDepthIndex(int index)
534 mImpl->mDepthIndex = index;
537 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
541 int Visual::Base::GetDepthIndex() const
543 return mImpl->mDepthIndex;
546 void Visual::Base::SetOnScene(Actor& actor)
550 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
551 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
556 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
557 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
560 mImpl->mFlags |= Impl::IS_ON_SCENE;
564 void Visual::Base::SetOffScene(Actor& actor)
568 DoSetOffScene(actor);
569 mImpl->mFlags &= ~Impl::IS_ON_SCENE;
573 void Visual::Base::CreatePropertyMap(Property::Map& map) const
577 // Update values from Renderer
578 mImpl->mMixColor = mImpl->mRenderer.GetProperty<Vector3>(VisualRenderer::Property::VISUAL_MIX_COLOR);
579 mImpl->mMixColor.a = mImpl->mRenderer.GetProperty<float>(DevelRenderer::Property::OPACITY);
581 mImpl->mTransform.mOffset = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_OFFSET);
582 mImpl->mTransform.mSize = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_SIZE);
584 if(IsRoundedCornerRequired())
586 mImpl->mCornerRadius = mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::CORNER_RADIUS);
588 if(IsBorderlineRequired())
590 mImpl->mBorderlineWidth = mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH);
591 mImpl->mBorderlineColor = mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::BORDERLINE_COLOR);
592 mImpl->mBorderlineOffset = mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET);
596 DoCreatePropertyMap(map);
598 if(mImpl->mCustomShader)
600 mImpl->mCustomShader->CreatePropertyMap(map);
603 Property::Map transform;
604 mImpl->mTransform.GetPropertyMap(transform);
605 map.Insert(Toolkit::Visual::Property::TRANSFORM, transform);
607 bool premultipliedAlpha(IsPreMultipliedAlphaEnabled());
608 map.Insert(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha);
610 // Note, Color and Primitive will also insert their own mix color into the map
611 // which is ok, because they have a different key value range, but uses same cached value anyway.
612 map.Insert(Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor); // vec4
613 map.Insert(Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a);
615 auto fittingModeString = Scripting::GetLinearEnumerationName<FittingMode>(
616 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT);
617 map.Insert(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString);
619 if(IsTypeAvailableForBorderline(mImpl->mType))
621 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
622 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
623 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
626 if(IsTypeAvailableForCornerRadius(mImpl->mType))
628 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius);
629 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, static_cast<int>(mImpl->mCornerRadiusPolicy));
633 void Visual::Base::CreateInstancePropertyMap(Property::Map& map) const
635 DoCreateInstancePropertyMap(map);
637 if(mImpl->mCustomShader)
639 mImpl->mCustomShader->CreatePropertyMap(map);
643 void Visual::Base::EnablePreMultipliedAlpha(bool preMultiplied)
647 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
651 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
656 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
657 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultiplied);
661 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
663 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
666 void Visual::Base::DoSetOffScene(Actor& actor)
668 actor.RemoveRenderer(mImpl->mRenderer);
671 bool Visual::Base::IsOnScene() const
673 return mImpl->mFlags & Impl::IS_ON_SCENE;
676 bool Visual::Base::IsRoundedCornerRequired() const
678 // If VisualType doesn't support rounded corner, always return false.
679 if(IsTypeAvailableForCornerRadius(mImpl->mType))
683 // Update values from Renderer
684 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS);
685 value.Get(mImpl->mCornerRadius);
687 return mImpl->mAlwaysUsingCornerRadius || !(mImpl->mCornerRadius == Vector4::ZERO);
692 bool Visual::Base::IsBorderlineRequired() const
694 // If VisualType doesn't support borderline, always return false.
695 if(IsTypeAvailableForBorderline(mImpl->mType))
699 // Update values from Renderer
700 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH);
701 value.Get(mImpl->mBorderlineWidth);
703 return mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->mBorderlineWidth);
708 void Visual::Base::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
710 // May be overriden by derived class
713 void Visual::Base::OnDoActionExtension(const Property::Index actionId, const Dali::Any attributes)
715 // May be overriden by derived class
718 void Visual::Base::RegisterMixColor()
722 // All visual renderers now use same mix color / opacity properties.
723 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
724 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, mImpl->mMixColor.a);
726 float preMultipliedAlpha = 0.0f;
727 if(IsPreMultipliedAlphaEnabled())
729 preMultipliedAlpha = 1.0f;
731 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultipliedAlpha);
735 void Visual::Base::RegisterDecoration()
739 if(IsTypeAvailableForCornerRadius(mImpl->mType))
741 if(mImpl->mAlwaysUsingCornerRadius || !(mImpl->mCornerRadius == Vector4::ZERO))
743 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
744 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->mCornerRadius);
745 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
748 if(IsTypeAvailableForBorderline(mImpl->mType))
750 if(mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->mBorderlineWidth))
752 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
753 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
754 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
755 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
761 void Visual::Base::SetMixColor(const Vector4& color)
763 mImpl->mMixColor = color;
767 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(color));
768 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, color.a);
772 void Visual::Base::SetMixColor(const Vector3& color)
774 mImpl->mMixColor.r = color.r;
775 mImpl->mMixColor.g = color.g;
776 mImpl->mMixColor.b = color.b;
780 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, color);
784 void Visual::Base::AddEventObserver(Visual::EventObserver& observer)
786 mImpl->mEventObserver = &observer;
789 void Visual::Base::RemoveEventObserver(Visual::EventObserver& observer)
791 mImpl->mEventObserver = NULL;
794 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
796 if(mImpl->mResourceStatus != resourceStatus)
798 mImpl->mResourceStatus = resourceStatus;
800 if(mImpl->mEventObserver)
802 // observer is currently a control impl
803 mImpl->mEventObserver->ResourceReady(*this);
808 bool Visual::Base::IsResourceReady() const
810 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
811 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
814 bool Visual::Base::IsSynchronousLoadingRequired() const
816 return (mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING);
819 Toolkit::Visual::Type Visual::Base::GetType() const
824 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
826 return mImpl->mResourceStatus;
829 Visual::FittingMode Visual::Base::GetFittingMode() const
831 return mImpl->mFittingMode;
834 Visual::Base& Visual::Base::GetVisualObject()
839 Renderer Visual::Base::GetRenderer()
841 return mImpl->mRenderer;
844 Property::Index Visual::Base::GetIntKey(Property::Key key)
846 if(key.type == Property::Key::INDEX)
851 if(key.stringKey == ANCHOR_POINT)
853 return Toolkit::Visual::Transform::Property::ANCHOR_POINT;
855 else if(key.stringKey == EXTRA_SIZE)
857 return Toolkit::DevelVisual::Transform::Property::EXTRA_SIZE;
859 else if(key.stringKey == MIX_COLOR)
861 return Toolkit::Visual::Property::MIX_COLOR;
863 else if(key.stringKey == OPACITY)
865 return Toolkit::Visual::Property::OPACITY;
867 else if(key.stringKey == OFFSET)
869 return Toolkit::Visual::Transform::Property::OFFSET;
871 else if(key.stringKey == OFFSET_POLICY)
873 return Toolkit::Visual::Transform::Property::OFFSET_POLICY;
875 else if(key.stringKey == ORIGIN)
877 return Toolkit::Visual::Transform::Property::ORIGIN;
879 else if(key.stringKey == PREMULTIPLIED_ALPHA)
881 return Toolkit::Visual::Property::PREMULTIPLIED_ALPHA;
883 else if(key.stringKey == CUSTOM_SHADER)
885 return Toolkit::Visual::Property::SHADER;
887 else if(key.stringKey == SIZE)
889 return Toolkit::Visual::Transform::Property::SIZE;
891 else if(key.stringKey == SIZE_POLICY)
893 return Toolkit::Visual::Transform::Property::SIZE_POLICY;
895 else if(key.stringKey == TRANSFORM)
897 return Toolkit::Visual::Property::TRANSFORM;
899 else if(key.stringKey == VISUAL_FITTING_MODE)
901 return Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE;
903 else if(key.stringKey == CORNER_RADIUS)
905 return Toolkit::DevelVisual::Property::CORNER_RADIUS;
907 else if(key.stringKey == CORNER_RADIUS_POLICY)
909 return Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY;
911 else if(key.stringKey == BORDERLINE_WIDTH)
913 return Toolkit::DevelVisual::Property::BORDERLINE_WIDTH;
915 else if(key.stringKey == BORDERLINE_COLOR)
917 return Toolkit::DevelVisual::Property::BORDERLINE_COLOR;
919 else if(key.stringKey == BORDERLINE_OFFSET)
921 return Toolkit::DevelVisual::Property::BORDERLINE_OFFSET;
924 return Property::INVALID_INDEX;
927 Property::Index Visual::Base::GetPropertyIndex(Property::Key key)
929 switch(GetIntKey(key))
931 case Dali::Toolkit::Visual::Transform::Property::OFFSET:
933 return VisualRenderer::Property::TRANSFORM_OFFSET;
935 case Dali::Toolkit::Visual::Transform::Property::SIZE:
937 return VisualRenderer::Property::TRANSFORM_SIZE;
939 case Dali::Toolkit::Visual::Transform::Property::ORIGIN:
941 return VisualRenderer::Property::TRANSFORM_ORIGIN;
943 case Dali::Toolkit::Visual::Transform::Property::ANCHOR_POINT:
945 return VisualRenderer::Property::TRANSFORM_ANCHOR_POINT;
947 case Dali::Toolkit::Visual::Property::MIX_COLOR:
949 return VisualRenderer::Property::VISUAL_MIX_COLOR;
951 case Dali::Toolkit::Visual::Property::OPACITY:
953 return DevelRenderer::Property::OPACITY;
955 case Dali::Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
957 return VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA;
959 case Dali::Toolkit::DevelVisual::Property::CORNER_RADIUS:
961 return DecoratedVisualRenderer::Property::CORNER_RADIUS;
963 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
965 return DecoratedVisualRenderer::Property::BORDERLINE_WIDTH;
967 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
969 return DecoratedVisualRenderer::Property::BORDERLINE_COLOR;
971 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
973 return DecoratedVisualRenderer::Property::BORDERLINE_OFFSET;
977 Property::Index index = mImpl->mRenderer.GetPropertyIndex(key);
979 if(index == Property::INVALID_INDEX)
981 // Is it a shader property?
982 Shader shader = mImpl->mRenderer.GetShader();
983 index = shader.GetPropertyIndex(key);
984 if(index != Property::INVALID_INDEX)
986 // Yes - we should register it in the Renderer so it can be set / animated
987 // independently, as shaders are shared across multiple renderers.
989 Property::Index keyIndex(Property::INVALID_KEY);
990 if(key.type == Property::Key::INDEX)
992 keyName = shader.GetPropertyName(index);
993 keyIndex = key.indexKey;
997 keyName = key.stringKey;
998 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
1000 Property::Value value = shader.GetProperty(index);
1002 // We already know that mRenderer didn't have property. So we can assume that it is unique.
1003 index = mImpl->mRenderer.RegisterUniqueProperty(keyIndex, keyName, value);
1009 void Visual::Base::SetupTransition(
1010 Dali::Animation& transition,
1011 Internal::TransitionData::Animator& animator,
1012 Property::Index index,
1013 Property::Value& initialValue,
1014 Property::Value& targetValue)
1016 if(index != Property::INVALID_INDEX)
1018 if(mImpl->mRenderer)
1020 if(animator.animate == false)
1022 mImpl->mRenderer.SetProperty(index, targetValue);
1026 if(animator.initialValue.GetType() != Property::NONE)
1028 mImpl->mRenderer.SetProperty(index, initialValue);
1033 transition = Dali::Animation::New(0.1f);
1036 transition.AnimateTo(Property(mImpl->mRenderer, index),
1038 animator.alphaFunction,
1039 TimePeriod(animator.timePeriodDelay,
1040 animator.timePeriodDuration));
1046 void Visual::Base::AnimateProperty(
1047 Dali::Animation& transition,
1048 Internal::TransitionData::Animator& animator)
1050 #if defined(DEBUG_ENABLED)
1051 if(gVisualBaseLogFilter->IsEnabledFor(Debug::General))
1053 std::ostringstream oss;
1054 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
1055 DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, oss.str().c_str());
1059 if(animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
1060 animator.propertyKey == MIX_COLOR ||
1061 (mImpl->mType == Toolkit::Visual::COLOR &&
1062 animator.propertyKey == ColorVisual::Property::MIX_COLOR) ||
1063 (mImpl->mType == Toolkit::Visual::PRIMITIVE &&
1064 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR))
1066 AnimateMixColorProperty(transition, animator);
1068 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
1069 animator.propertyKey == OPACITY)
1071 AnimateOpacityProperty(transition, animator);
1073 else if(mImpl->mRenderer)
1075 AnimateRendererProperty(transition, animator);
1079 void Visual::Base::AnimateOpacityProperty(
1080 Dali::Animation& transition,
1081 Internal::TransitionData::Animator& animator)
1083 float targetOpacity;
1084 if(animator.targetValue.Get(targetOpacity))
1086 mImpl->mMixColor.a = targetOpacity;
1089 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue);
1092 void Visual::Base::AnimateRendererProperty(
1093 Dali::Animation& transition,
1094 Internal::TransitionData::Animator& animator)
1096 // Get actual renderer index (will convert transform keys into visualproperty indices)
1097 Property::Index index = GetPropertyIndex(animator.propertyKey);
1099 if(index != Property::INVALID_INDEX)
1101 if(animator.targetValue.GetType() != Property::NONE)
1103 // Try writing target value into transform property map
1104 // if it's not a valid key, then it won't alter mTransform
1106 if(animator.propertyKey.type == Property::Key::INDEX)
1108 map.Add(animator.propertyKey.indexKey, animator.targetValue);
1112 map.Add(animator.propertyKey.stringKey, animator.targetValue);
1115 mImpl->mTransform.UpdatePropertyMap(map);
1117 SetupTransition(transition, animator, index, animator.initialValue, animator.targetValue);
1121 void Visual::Base::AnimateMixColorProperty(
1122 Dali::Animation& transition,
1123 Internal::TransitionData::Animator& animator)
1125 bool animateOpacity = false;
1127 Property::Value initialOpacity;
1128 Property::Value targetOpacity;
1129 Property::Value initialMixColor;
1130 Property::Value targetMixColor;
1132 Vector4 initialColor;
1133 if(animator.initialValue.Get(initialColor))
1135 if(animator.initialValue.GetType() == Property::VECTOR4)
1137 // if there is an initial color specifying alpha, test it
1138 initialOpacity = initialColor.a;
1140 initialMixColor = Vector3(initialColor);
1143 // Set target value into data store
1144 if(animator.targetValue.GetType() != Property::NONE)
1147 animator.targetValue.Get(mixColor);
1148 if(animator.targetValue.GetType() == Property::VECTOR4)
1150 mImpl->mMixColor.a = mixColor.a;
1151 targetOpacity = mixColor.a;
1152 animateOpacity = true;
1155 mImpl->mMixColor.r = mixColor.r;
1156 mImpl->mMixColor.g = mixColor.g;
1157 mImpl->mMixColor.b = mixColor.b;
1158 targetMixColor = Vector3(mixColor);
1161 SetupTransition(transition, animator, VisualRenderer::Property::VISUAL_MIX_COLOR, initialMixColor, targetMixColor);
1165 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity);
1169 Dali::Property Visual::Base::GetPropertyObject(Dali::Property::Key key)
1171 if(!mImpl->mRenderer)
1174 return Dali::Property(handle, Property::INVALID_INDEX);
1177 switch(GetIntKey(key))
1179 // Default animatable properties from VisualRenderer
1180 case Toolkit::Visual::Property::MIX_COLOR:
1182 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1184 case Toolkit::Visual::Property::OPACITY:
1186 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1188 case Toolkit::Visual::Transform::Property::OFFSET:
1190 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_OFFSET);
1192 case Toolkit::Visual::Transform::Property::SIZE:
1194 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_SIZE);
1197 // Default animatable properties from DecoratedVisualRenderer
1198 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
1200 if(IsTypeAvailableForCornerRadius(mImpl->mType))
1202 const bool updateShader = !mImpl->mCustomShader && !IsRoundedCornerRequired();
1204 // CornerRadius is animated now. we always have to use corner radius feature.
1205 mImpl->mAlwaysUsingCornerRadius = true;
1209 // Update each values to renderer
1210 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
1211 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->mCornerRadius);
1212 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
1217 if(!IsBorderlineRequired())
1219 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
1220 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
1222 return Dali::Property(mImpl->mRenderer, DecoratedVisualRenderer::Property::CORNER_RADIUS);
1226 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
1227 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
1228 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
1230 if(IsTypeAvailableForBorderline(mImpl->mType))
1232 const bool updateShader = !mImpl->mCustomShader && !IsBorderlineRequired();
1234 // Borderline is animated now. we always have to use borderline feature.
1235 mImpl->mAlwaysUsingBorderline = true;
1239 // Update each values to renderer
1240 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
1241 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
1242 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
1243 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
1248 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
1250 return Dali::Property(mImpl->mRenderer, GetPropertyIndex(key));
1256 // Special case for MIX_COLOR
1257 if(key.type == Property::Key::INDEX &&
1258 ((mImpl->mType == Toolkit::Visual::COLOR && key.indexKey == ColorVisual::Property::MIX_COLOR) ||
1259 (mImpl->mType == Toolkit::Visual::PRIMITIVE && key.indexKey == PrimitiveVisual::Property::MIX_COLOR)))
1261 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1264 // Special case for BLUR_RADIUS
1265 if(mImpl->mType == Toolkit::Visual::COLOR &&
1266 ((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) ||
1267 (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME)))
1269 // Request to color-visual class
1270 return OnGetPropertyObject(key);
1275 // If it is not VisualRenderer property, check registered Renderer and Shader property.
1276 Property::Index index = GetPropertyIndex(key);
1277 if(index != Property::INVALID_INDEX)
1279 return Dali::Property(mImpl->mRenderer, index);
1282 // We can't find the property in the base class.
1283 // Request to child class
1284 return OnGetPropertyObject(key);
1287 } // namespace Internal
1289 } // namespace Toolkit