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 // Make Blend mode ON_WITHOUT_CULL for transparent mix color.
310 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
312 // Change the shader must not be occured many times. we always have to use borderline feature.
313 mImpl->mAlwaysUsingBorderline = true;
316 if(!mImpl->mCustomShader)
318 needUpdateShader = true;
324 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
329 mImpl->mBorderlineColor = color;
332 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
334 // Unusual case. SetProperty called after OnInitialize().
335 // Assume that DoAction call UPDATE_PROPERTY.
336 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
340 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
343 if(value.Get(offset))
345 mImpl->mBorderlineOffset = offset;
348 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
350 // Unusual case. SetProperty called after OnInitialize().
351 // Assume that DoAction call UPDATE_PROPERTY.
352 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
356 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
358 if(value.GetType() == Property::VECTOR4)
360 // If CORNER_RADIUS Property is Vector4,
361 // Each values mean the radius of
362 // (top-left, top-right, bottom-right, bottom-left)
364 if(value.Get(radius))
366 mImpl->mCornerRadius = radius;
371 // If CORNER_RADIUS Property is float,
372 // Every corner radius have same value
374 if(value.Get(radius))
376 mImpl->mCornerRadius = Vector4(radius, radius, radius, radius);
380 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForCornerRadius(mImpl->mType)))
382 // Unusual case. SetProperty called after OnInitialize().
383 // Assume that DoAction call UPDATE_PROPERTY.
384 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
385 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->mCornerRadius);
387 // Check whether we must update shader.
388 if(!mImpl->mAlwaysUsingCornerRadius && IsRoundedCornerRequired())
390 // Change the shader must not be occured many times. we always have to use corner radius feature.
391 mImpl->mAlwaysUsingCornerRadius = true;
393 if(!IsBorderlineRequired())
395 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
396 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
400 if(!mImpl->mCustomShader)
402 needUpdateShader = true;
409 case Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY:
412 if(value.Get(policy))
416 case Toolkit::Visual::Transform::Policy::RELATIVE:
417 case Toolkit::Visual::Transform::Policy::ABSOLUTE:
419 mImpl->mCornerRadiusPolicy = policy;
420 if(DALI_UNLIKELY(mImpl->mRenderer))
422 // Unusual case. SetProperty called after OnInitialize().
423 // Assume that DoAction call UPDATE_PROPERTY.
424 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
430 DALI_LOG_ERROR("Unsupported policy: %d\n", policy);
440 DoSetProperties(propertyMap);
442 if(DALI_UNLIKELY(needUpdateShader))
448 void Visual::Base::SetTransformAndSize(const Property::Map& transform, Size controlSize)
450 mImpl->mControlSize = controlSize;
451 mImpl->mTransform.UpdatePropertyMap(transform);
453 #if defined(DEBUG_ENABLED)
454 std::ostringstream oss;
456 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);
462 void Visual::Base::SetName(const std::string& name)
467 const std::string& Visual::Base::GetName() const
472 float Visual::Base::GetHeightForWidth(float width)
474 float aspectCorrectedHeight = 0.f;
476 GetNaturalSize(naturalSize);
477 if(naturalSize.width)
479 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
481 return aspectCorrectedHeight;
484 float Visual::Base::GetWidthForHeight(float height)
486 float aspectCorrectedWidth = 0.f;
488 GetNaturalSize(naturalSize);
489 if(naturalSize.height > 0.0f)
491 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
493 return aspectCorrectedWidth;
496 void Visual::Base::GetNaturalSize(Vector2& naturalSize)
498 naturalSize = Vector2::ZERO;
501 void Visual::Base::DoAction(const Property::Index actionId, const Property::Value attributes)
503 OnDoAction(actionId, attributes);
505 // Check if action is valid for this visual type and perform action if possible
508 case DevelVisual::Action::UPDATE_PROPERTY:
510 const Property::Map* map = attributes.GetMap();
520 void Visual::Base::SetDepthIndex(int index)
522 mImpl->mDepthIndex = index;
525 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
529 int Visual::Base::GetDepthIndex() const
531 return mImpl->mDepthIndex;
534 void Visual::Base::SetOnScene(Actor& actor)
538 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
539 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
544 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
545 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
548 mImpl->mFlags |= Impl::IS_ON_SCENE;
552 void Visual::Base::SetOffScene(Actor& actor)
556 DoSetOffScene(actor);
557 mImpl->mFlags &= ~Impl::IS_ON_SCENE;
561 void Visual::Base::CreatePropertyMap(Property::Map& map) const
565 // Update values from Renderer
566 mImpl->mMixColor = mImpl->mRenderer.GetProperty<Vector3>(VisualRenderer::Property::VISUAL_MIX_COLOR);
567 mImpl->mMixColor.a = mImpl->mRenderer.GetProperty<float>(DevelRenderer::Property::OPACITY);
569 mImpl->mTransform.mOffset = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_OFFSET);
570 mImpl->mTransform.mSize = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_SIZE);
572 if(IsTypeAvailableForCornerRadius(mImpl->mType))
574 mImpl->mCornerRadius = mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::CORNER_RADIUS);
576 if(IsTypeAvailableForBorderline(mImpl->mType))
578 mImpl->mBorderlineWidth = mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH);
579 mImpl->mBorderlineColor = mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::BORDERLINE_COLOR);
580 mImpl->mBorderlineOffset = mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET);
584 DoCreatePropertyMap(map);
586 if(mImpl->mCustomShader)
588 mImpl->mCustomShader->CreatePropertyMap(map);
591 Property::Map transform;
592 mImpl->mTransform.GetPropertyMap(transform);
593 map.Insert(Toolkit::Visual::Property::TRANSFORM, transform);
595 bool premultipliedAlpha(IsPreMultipliedAlphaEnabled());
596 map.Insert(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha);
598 // Note, Color and Primitive will also insert their own mix color into the map
599 // which is ok, because they have a different key value range, but uses same cached value anyway.
600 map.Insert(Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor); // vec4
601 map.Insert(Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a);
603 auto fittingModeString = Scripting::GetLinearEnumerationName<FittingMode>(
604 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT);
605 map.Insert(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString);
607 if(IsTypeAvailableForBorderline(mImpl->mType))
609 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
610 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
611 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
614 if(IsTypeAvailableForCornerRadius(mImpl->mType))
616 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius);
617 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, static_cast<int>(mImpl->mCornerRadiusPolicy));
621 void Visual::Base::CreateInstancePropertyMap(Property::Map& map) const
623 DoCreateInstancePropertyMap(map);
625 if(mImpl->mCustomShader)
627 mImpl->mCustomShader->CreatePropertyMap(map);
631 void Visual::Base::EnablePreMultipliedAlpha(bool preMultiplied)
635 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
639 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
644 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
645 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultiplied);
649 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
651 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
654 void Visual::Base::DoSetOffScene(Actor& actor)
656 actor.RemoveRenderer(mImpl->mRenderer);
659 bool Visual::Base::IsOnScene() const
661 return mImpl->mFlags & Impl::IS_ON_SCENE;
664 bool Visual::Base::IsRoundedCornerRequired() const
666 // If VisualType doesn't support rounded corner, always return false.
667 if(IsTypeAvailableForCornerRadius(mImpl->mType))
671 // Update values from Renderer
672 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS);
673 value.Get(mImpl->mCornerRadius);
675 return mImpl->mAlwaysUsingCornerRadius || !(mImpl->mCornerRadius == Vector4::ZERO);
680 bool Visual::Base::IsBorderlineRequired() const
682 // If VisualType doesn't support borderline, always return false.
683 if(IsTypeAvailableForBorderline(mImpl->mType))
687 // Update values from Renderer
688 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH);
689 value.Get(mImpl->mBorderlineWidth);
691 return mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->mBorderlineWidth);
696 void Visual::Base::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
698 // May be overriden by derived class
701 void Visual::Base::RegisterMixColor()
705 // All visual renderers now use same mix color / opacity properties.
706 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
707 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, mImpl->mMixColor.a);
709 float preMultipliedAlpha = 0.0f;
710 if(IsPreMultipliedAlphaEnabled())
712 preMultipliedAlpha = 1.0f;
714 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultipliedAlpha);
718 void Visual::Base::RegisterDecoration()
722 if(IsTypeAvailableForCornerRadius(mImpl->mType))
724 if(mImpl->mAlwaysUsingCornerRadius || !(mImpl->mCornerRadius == Vector4::ZERO))
726 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
727 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->mCornerRadius);
728 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
731 if(IsTypeAvailableForBorderline(mImpl->mType))
733 if(mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->mBorderlineWidth))
735 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
736 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
737 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
738 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
744 void Visual::Base::SetMixColor(const Vector4& color)
746 mImpl->mMixColor = color;
750 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(color));
751 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, color.a);
755 void Visual::Base::SetMixColor(const Vector3& color)
757 mImpl->mMixColor.r = color.r;
758 mImpl->mMixColor.g = color.g;
759 mImpl->mMixColor.b = color.b;
763 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, color);
767 void Visual::Base::AddEventObserver(Visual::EventObserver& observer)
769 mImpl->mEventObserver = &observer;
772 void Visual::Base::RemoveEventObserver(Visual::EventObserver& observer)
774 mImpl->mEventObserver = NULL;
777 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
779 if(mImpl->mResourceStatus != resourceStatus)
781 mImpl->mResourceStatus = resourceStatus;
783 if(mImpl->mEventObserver)
785 // observer is currently a control impl
786 mImpl->mEventObserver->ResourceReady(*this);
791 bool Visual::Base::IsResourceReady() const
793 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
794 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
797 bool Visual::Base::IsSynchronousLoadingRequired() const
799 return (mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING);
802 Toolkit::Visual::Type Visual::Base::GetType() const
807 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
809 return mImpl->mResourceStatus;
812 Visual::FittingMode Visual::Base::GetFittingMode() const
814 return mImpl->mFittingMode;
817 Visual::Base& Visual::Base::GetVisualObject()
822 Renderer Visual::Base::GetRenderer()
824 return mImpl->mRenderer;
827 Property::Index Visual::Base::GetIntKey(Property::Key key)
829 if(key.type == Property::Key::INDEX)
834 if(key.stringKey == ANCHOR_POINT)
836 return Toolkit::Visual::Transform::Property::ANCHOR_POINT;
838 else if(key.stringKey == EXTRA_SIZE)
840 return Toolkit::DevelVisual::Transform::Property::EXTRA_SIZE;
842 else if(key.stringKey == MIX_COLOR)
844 return Toolkit::Visual::Property::MIX_COLOR;
846 else if(key.stringKey == OPACITY)
848 return Toolkit::Visual::Property::OPACITY;
850 else if(key.stringKey == OFFSET)
852 return Toolkit::Visual::Transform::Property::OFFSET;
854 else if(key.stringKey == OFFSET_POLICY)
856 return Toolkit::Visual::Transform::Property::OFFSET_POLICY;
858 else if(key.stringKey == ORIGIN)
860 return Toolkit::Visual::Transform::Property::ORIGIN;
862 else if(key.stringKey == PREMULTIPLIED_ALPHA)
864 return Toolkit::Visual::Property::PREMULTIPLIED_ALPHA;
866 else if(key.stringKey == CUSTOM_SHADER)
868 return Toolkit::Visual::Property::SHADER;
870 else if(key.stringKey == SIZE)
872 return Toolkit::Visual::Transform::Property::SIZE;
874 else if(key.stringKey == SIZE_POLICY)
876 return Toolkit::Visual::Transform::Property::SIZE_POLICY;
878 else if(key.stringKey == TRANSFORM)
880 return Toolkit::Visual::Property::TRANSFORM;
882 else if(key.stringKey == VISUAL_FITTING_MODE)
884 return Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE;
886 else if(key.stringKey == CORNER_RADIUS)
888 return Toolkit::DevelVisual::Property::CORNER_RADIUS;
890 else if(key.stringKey == CORNER_RADIUS_POLICY)
892 return Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY;
894 else if(key.stringKey == BORDERLINE_WIDTH)
896 return Toolkit::DevelVisual::Property::BORDERLINE_WIDTH;
898 else if(key.stringKey == BORDERLINE_COLOR)
900 return Toolkit::DevelVisual::Property::BORDERLINE_COLOR;
902 else if(key.stringKey == BORDERLINE_OFFSET)
904 return Toolkit::DevelVisual::Property::BORDERLINE_OFFSET;
907 return Property::INVALID_INDEX;
910 Property::Index Visual::Base::GetPropertyIndex(Property::Key key)
912 switch(GetIntKey(key))
914 case Dali::Toolkit::Visual::Transform::Property::OFFSET:
916 return VisualRenderer::Property::TRANSFORM_OFFSET;
918 case Dali::Toolkit::Visual::Transform::Property::SIZE:
920 return VisualRenderer::Property::TRANSFORM_SIZE;
922 case Dali::Toolkit::Visual::Transform::Property::ORIGIN:
924 return VisualRenderer::Property::TRANSFORM_ORIGIN;
926 case Dali::Toolkit::Visual::Transform::Property::ANCHOR_POINT:
928 return VisualRenderer::Property::TRANSFORM_ANCHOR_POINT;
930 case Dali::Toolkit::Visual::Property::MIX_COLOR:
932 return VisualRenderer::Property::VISUAL_MIX_COLOR;
934 case Dali::Toolkit::Visual::Property::OPACITY:
936 return DevelRenderer::Property::OPACITY;
938 case Dali::Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
940 return VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA;
942 case Dali::Toolkit::DevelVisual::Property::CORNER_RADIUS:
944 return DecoratedVisualRenderer::Property::CORNER_RADIUS;
946 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
948 return DecoratedVisualRenderer::Property::BORDERLINE_WIDTH;
950 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
952 return DecoratedVisualRenderer::Property::BORDERLINE_COLOR;
954 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
956 return DecoratedVisualRenderer::Property::BORDERLINE_OFFSET;
960 Property::Index index = mImpl->mRenderer.GetPropertyIndex(key);
962 if(index == Property::INVALID_INDEX)
964 // Is it a shader property?
965 Shader shader = mImpl->mRenderer.GetShader();
966 index = shader.GetPropertyIndex(key);
967 if(index != Property::INVALID_INDEX)
969 // Yes - we should register it in the Renderer so it can be set / animated
970 // independently, as shaders are shared across multiple renderers.
972 Property::Index keyIndex(Property::INVALID_KEY);
973 if(key.type == Property::Key::INDEX)
975 keyName = shader.GetPropertyName(index);
976 keyIndex = key.indexKey;
980 keyName = key.stringKey;
981 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
983 Property::Value value = shader.GetProperty(index);
985 // We already know that mRenderer didn't have property. So we can assume that it is unique.
986 index = mImpl->mRenderer.RegisterUniqueProperty(keyIndex, keyName, value);
992 void Visual::Base::SetupTransition(
993 Dali::Animation& transition,
994 Internal::TransitionData::Animator& animator,
995 Property::Index index,
996 Property::Value& initialValue,
997 Property::Value& targetValue)
999 if(index != Property::INVALID_INDEX)
1001 if(mImpl->mRenderer)
1003 if(animator.animate == false)
1005 mImpl->mRenderer.SetProperty(index, targetValue);
1009 if(animator.initialValue.GetType() != Property::NONE)
1011 mImpl->mRenderer.SetProperty(index, initialValue);
1016 transition = Dali::Animation::New(0.1f);
1019 transition.AnimateTo(Property(mImpl->mRenderer, index),
1021 animator.alphaFunction,
1022 TimePeriod(animator.timePeriodDelay,
1023 animator.timePeriodDuration));
1029 void Visual::Base::AnimateProperty(
1030 Dali::Animation& transition,
1031 Internal::TransitionData::Animator& animator)
1033 #if defined(DEBUG_ENABLED)
1034 if(gVisualBaseLogFilter->IsEnabledFor(Debug::General))
1036 std::ostringstream oss;
1037 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
1038 DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, oss.str().c_str());
1042 if(animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
1043 animator.propertyKey == MIX_COLOR ||
1044 (mImpl->mType == Toolkit::Visual::COLOR &&
1045 animator.propertyKey == ColorVisual::Property::MIX_COLOR) ||
1046 (mImpl->mType == Toolkit::Visual::PRIMITIVE &&
1047 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR))
1049 AnimateMixColorProperty(transition, animator);
1051 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
1052 animator.propertyKey == OPACITY)
1054 AnimateOpacityProperty(transition, animator);
1056 else if(mImpl->mRenderer)
1058 AnimateRendererProperty(transition, animator);
1062 void Visual::Base::AnimateOpacityProperty(
1063 Dali::Animation& transition,
1064 Internal::TransitionData::Animator& animator)
1066 float targetOpacity;
1067 if(animator.targetValue.Get(targetOpacity))
1069 mImpl->mMixColor.a = targetOpacity;
1072 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue);
1075 void Visual::Base::AnimateRendererProperty(
1076 Dali::Animation& transition,
1077 Internal::TransitionData::Animator& animator)
1079 // Get actual renderer index (will convert transform keys into visualproperty indices)
1080 Property::Index index = GetPropertyIndex(animator.propertyKey);
1082 if(index != Property::INVALID_INDEX)
1084 if(animator.targetValue.GetType() != Property::NONE)
1086 // Try writing target value into transform property map
1087 // if it's not a valid key, then it won't alter mTransform
1089 if(animator.propertyKey.type == Property::Key::INDEX)
1091 map.Add(animator.propertyKey.indexKey, animator.targetValue);
1095 map.Add(animator.propertyKey.stringKey, animator.targetValue);
1098 mImpl->mTransform.UpdatePropertyMap(map);
1100 SetupTransition(transition, animator, index, animator.initialValue, animator.targetValue);
1104 void Visual::Base::AnimateMixColorProperty(
1105 Dali::Animation& transition,
1106 Internal::TransitionData::Animator& animator)
1108 bool animateOpacity = false;
1110 Property::Value initialOpacity;
1111 Property::Value targetOpacity;
1112 Property::Value initialMixColor;
1113 Property::Value targetMixColor;
1115 Vector4 initialColor;
1116 if(animator.initialValue.Get(initialColor))
1118 if(animator.initialValue.GetType() == Property::VECTOR4)
1120 // if there is an initial color specifying alpha, test it
1121 initialOpacity = initialColor.a;
1123 initialMixColor = Vector3(initialColor);
1126 // Set target value into data store
1127 if(animator.targetValue.GetType() != Property::NONE)
1130 animator.targetValue.Get(mixColor);
1131 if(animator.targetValue.GetType() == Property::VECTOR4)
1133 mImpl->mMixColor.a = mixColor.a;
1134 targetOpacity = mixColor.a;
1135 animateOpacity = true;
1138 mImpl->mMixColor.r = mixColor.r;
1139 mImpl->mMixColor.g = mixColor.g;
1140 mImpl->mMixColor.b = mixColor.b;
1141 targetMixColor = Vector3(mixColor);
1144 SetupTransition(transition, animator, VisualRenderer::Property::VISUAL_MIX_COLOR, initialMixColor, targetMixColor);
1148 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity);
1152 Dali::Property Visual::Base::GetPropertyObject(Dali::Property::Key key)
1154 if(!mImpl->mRenderer)
1157 return Dali::Property(handle, Property::INVALID_INDEX);
1160 switch(GetIntKey(key))
1162 // Default animatable properties from VisualRenderer
1163 case Toolkit::Visual::Property::MIX_COLOR:
1165 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1167 case Toolkit::Visual::Property::OPACITY:
1169 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1171 case Toolkit::Visual::Transform::Property::OFFSET:
1173 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_OFFSET);
1175 case Toolkit::Visual::Transform::Property::SIZE:
1177 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_SIZE);
1180 // Default animatable properties from DecoratedVisualRenderer
1181 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
1183 if(IsTypeAvailableForCornerRadius(mImpl->mType))
1185 const bool updateShader = !mImpl->mCustomShader && !IsRoundedCornerRequired();
1187 // CornerRadius is animated now. we always have to use corner radius feature.
1188 mImpl->mAlwaysUsingCornerRadius = true;
1192 // Update each values to renderer
1193 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
1194 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->mCornerRadius);
1195 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
1200 if(!IsBorderlineRequired())
1202 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
1203 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
1205 return Dali::Property(mImpl->mRenderer, DecoratedVisualRenderer::Property::CORNER_RADIUS);
1209 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
1210 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
1211 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
1213 if(IsTypeAvailableForBorderline(mImpl->mType))
1215 const bool updateShader = !mImpl->mCustomShader && !IsBorderlineRequired();
1217 // Borderline is animated now. we always have to use borderline feature.
1218 mImpl->mAlwaysUsingBorderline = true;
1222 // Update each values to renderer
1223 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
1224 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
1225 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
1226 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
1231 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
1233 return Dali::Property(mImpl->mRenderer, GetPropertyIndex(key));
1239 // Special case for MIX_COLOR
1240 if(key.type == Property::Key::INDEX &&
1241 ((mImpl->mType == Toolkit::Visual::COLOR && key.indexKey == ColorVisual::Property::MIX_COLOR) ||
1242 (mImpl->mType == Toolkit::Visual::PRIMITIVE && key.indexKey == PrimitiveVisual::Property::MIX_COLOR)))
1244 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1247 // Special case for BLUR_RADIUS
1248 if(mImpl->mType == Toolkit::Visual::COLOR &&
1249 ((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) ||
1250 (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME)))
1252 // Request to color-visual class
1253 return OnGetPropertyObject(key);
1258 // If it is not VisualRenderer property, check registered Renderer and Shader property.
1259 Property::Index index = GetPropertyIndex(key);
1260 if(index != Property::INVALID_INDEX)
1262 return Dali::Property(mImpl->mRenderer, index);
1265 // We can't find the property in the base class.
1266 // Request to child class
1267 return OnGetPropertyObject(key);
1270 } // namespace Internal
1272 } // namespace Toolkit