2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "visual-base-impl.h"
22 #include <dali-toolkit/public-api/dali-toolkit-common.h>
23 #include <dali/devel-api/rendering/renderer-devel.h>
24 #include <dali/devel-api/scripting/enum-helper.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/public-api/rendering/decorated-visual-renderer.h>
27 #include <dali/public-api/rendering/visual-renderer.h>
30 #include <dali-toolkit/devel-api/visuals/color-visual-properties-devel.h>
31 #include <dali-toolkit/devel-api/visuals/visual-actions-devel.h>
32 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
33 #include <dali-toolkit/internal/helpers/property-helper.h>
34 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
35 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
36 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
37 #include <dali-toolkit/public-api/visuals/primitive-visual-properties.h>
38 #include <dali-toolkit/public-api/visuals/visual-properties.h>
42 #if defined(DEBUG_ENABLED)
43 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VISUAL_BASE");
46 // visual string constants contains OFFSET_SIZE_MODE instead
47 const char* const OFFSET_POLICY("offsetPolicy");
48 const char* const SIZE_POLICY("sizePolicy");
60 DALI_ENUM_TO_STRING_TABLE_BEGIN(VISUAL_FITTING_MODE)
61 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_KEEP_ASPECT_RATIO)
62 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FILL)
63 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, OVER_FIT_KEEP_ASPECT_RATIO)
64 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, CENTER)
65 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_WIDTH)
66 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_HEIGHT)
67 DALI_ENUM_TO_STRING_TABLE_END(VISUAL_FITTING_MODE)
70 * @brief Check whether this visual type can use corner radius feature or not.
71 * @param type VisualType that want to checkup
72 * @return true if type can use corner radius feature.
74 static bool IsTypeAvailableForCornerRadius(Toolkit::Visual::Type type)
76 switch(static_cast<Toolkit::DevelVisual::Type>(type))
78 case Toolkit::Visual::Type::COLOR:
79 case Toolkit::Visual::Type::GRADIENT:
80 case Toolkit::Visual::Type::IMAGE:
81 case Toolkit::Visual::Type::SVG:
82 case Toolkit::Visual::Type::ANIMATED_IMAGE:
83 case Toolkit::DevelVisual::Type::ANIMATED_VECTOR_IMAGE:
95 * @brief Check whether this visual type can use borderline feature or not.
96 * @param type VisualType that want to checkup
97 * @return true if type can use borderline feature.
99 static bool IsTypeAvailableForBorderline(Toolkit::Visual::Type type)
101 switch(static_cast<Toolkit::DevelVisual::Type>(type))
103 case Toolkit::Visual::Type::COLOR:
104 case Toolkit::Visual::Type::GRADIENT:
105 case Toolkit::Visual::Type::IMAGE:
106 case Toolkit::Visual::Type::SVG:
107 case Toolkit::Visual::Type::ANIMATED_IMAGE:
108 case Toolkit::DevelVisual::Type::ANIMATED_VECTOR_IMAGE:
119 struct StringProperty
121 const char* const name;
122 Property::Index index;
124 StringProperty PROPERTY_NAME_INDEX_TABLE[]=
126 {CUSTOM_SHADER, Toolkit::Visual::Property::SHADER},
127 {TRANSFORM, Toolkit::Visual::Property::TRANSFORM},
128 {PREMULTIPLIED_ALPHA, Toolkit::Visual::Property::PREMULTIPLIED_ALPHA},
129 {MIX_COLOR, Toolkit::Visual::Property::MIX_COLOR},
130 {OPACITY, Toolkit::Visual::Property::OPACITY},
131 {VISUAL_FITTING_MODE, Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE},
132 {BORDERLINE_WIDTH, Toolkit::DevelVisual::Property::BORDERLINE_WIDTH},
133 {BORDERLINE_COLOR, Toolkit::DevelVisual::Property::BORDERLINE_COLOR},
134 {BORDERLINE_OFFSET, Toolkit::DevelVisual::Property::BORDERLINE_OFFSET},
135 {CORNER_RADIUS, Toolkit::DevelVisual::Property::CORNER_RADIUS},
136 {CORNER_RADIUS_POLICY, Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY},
138 const uint16_t PROPERTY_NAME_INDEX_TABLE_COUNT = sizeof(PROPERTY_NAME_INDEX_TABLE) / sizeof(PROPERTY_NAME_INDEX_TABLE[0]);
140 Property::Index GetVisualPropertyIndex(Property::Key key)
142 if(key.type == Property::Key::STRING)
144 for(auto tableId = 0u; tableId < PROPERTY_NAME_INDEX_TABLE_COUNT; ++tableId)
146 if(key == PROPERTY_NAME_INDEX_TABLE[tableId].name)
148 return PROPERTY_NAME_INDEX_TABLE[tableId].index;
158 Visual::Base::Base(VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type)
159 : mImpl(new Impl(fittingMode, type)),
160 mFactoryCache(factoryCache)
164 Visual::Base::~Base()
169 void Visual::Base::Initialize()
171 // The Renderer should be created inside derived class here.
177 RegisterDecoration();
179 if(IsBorderlineRequired())
181 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
183 else if(IsRoundedCornerRequired())
185 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
190 void Visual::Base::SetCustomShader(const Property::Map& shaderMap)
192 if(mImpl->mCustomShader)
194 mImpl->mCustomShader->SetPropertyMap(shaderMap);
198 mImpl->mCustomShader = new Impl::CustomShader(shaderMap);
201 // Let derived class know
205 void Visual::Base::SetProperties(const Property::Map& propertyMap)
207 bool needUpdateShader = false;
208 for(size_t i = 0; i < propertyMap.Count(); ++i)
210 const KeyValuePair& pair = propertyMap.GetKeyValue(i);
211 const Property::Key& key = pair.first;
212 const Property::Value& value = pair.second;
214 switch(GetVisualPropertyIndex(key))
216 case Toolkit::Visual::Property::SHADER:
218 Property::Map shaderMap;
219 if(value.Get(shaderMap))
221 SetCustomShader(shaderMap);
226 case Toolkit::Visual::Property::TRANSFORM:
231 mImpl->mTransform.SetPropertyMap(map);
236 case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
238 bool premultipliedAlpha = false;
239 if(value.Get(premultipliedAlpha))
241 EnablePreMultipliedAlpha(premultipliedAlpha);
246 case Toolkit::Visual::Property::MIX_COLOR:
249 if(value.Get(mixColor))
251 if(value.GetType() == Property::VECTOR4)
253 SetMixColor(mixColor);
257 Vector3 mixColor3(mixColor);
258 SetMixColor(mixColor3);
263 case Toolkit::Visual::Property::OPACITY:
266 if(value.Get(opacity))
268 mImpl->mMixColor.a = opacity;
269 SetMixColor(mImpl->mMixColor);
273 case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
275 Scripting::GetEnumerationProperty<Visual::FittingMode>(
276 value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode);
279 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
282 if(value.Get(width) && (mImpl->mDecorationData != nullptr || !Dali::EqualsZero(width)))
284 mImpl->SetBorderlineWidth(width);
287 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
289 // Unusual case. SetProperty called after OnInitialize().
290 // Assume that DoAction call UPDATE_PROPERTY.
291 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
292 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
294 // Check whether we must update shader.
295 if(!mImpl->mAlwaysUsingBorderline && IsBorderlineRequired())
297 // Required to change shader mean, we didn't setup BORDERLINE_COLOR and BORDERLINE_OFFSET into mRenderer before. Set property now.
298 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
299 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
301 // Make Blend mode ON_WITHOUT_CULL for transparent mix color.
302 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
304 // Change the shader must not be occured many times. we always have to use borderline feature.
305 mImpl->mAlwaysUsingBorderline = true;
308 if(!mImpl->mCustomShader)
310 needUpdateShader = true;
316 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
319 if(value.Get(color) && (mImpl->mDecorationData != nullptr || color != Vector4::ZERO))
321 mImpl->SetBorderlineColor(color);
324 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
326 // Unusual case. SetProperty called after OnInitialize().
327 // Assume that DoAction call UPDATE_PROPERTY.
328 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
332 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
335 if(value.Get(offset) && (mImpl->mDecorationData != nullptr || !Dali::EqualsZero(offset)))
337 mImpl->SetBorderlineOffset(offset);
340 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForBorderline(mImpl->mType)))
342 // Unusual case. SetProperty called after OnInitialize().
343 // Assume that DoAction call UPDATE_PROPERTY.
344 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
348 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
350 if(value.GetType() == Property::VECTOR4)
352 // If CORNER_RADIUS Property is Vector4,
353 // Each values mean the radius of
354 // (top-left, top-right, bottom-right, bottom-left)
356 if(value.Get(radius) && (mImpl->mDecorationData != nullptr || radius != Vector4::ZERO))
358 mImpl->SetCornerRadius(radius);
363 // If CORNER_RADIUS Property is float,
364 // Every corner radius have same value
366 if(value.Get(radius) && (mImpl->mDecorationData != nullptr || !Dali::EqualsZero(radius)))
368 mImpl->SetCornerRadius(Vector4(radius, radius, radius, radius));
372 if(DALI_UNLIKELY(mImpl->mRenderer && IsTypeAvailableForCornerRadius(mImpl->mType)))
374 // Unusual case. SetProperty called after OnInitialize().
375 // Assume that DoAction call UPDATE_PROPERTY.
376 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
377 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
379 // Check whether we must update shader.
380 if(!mImpl->mAlwaysUsingCornerRadius && IsRoundedCornerRequired())
382 // Required to change shader mean, we didn't setup CORNER_RADIUS_POLICY into mRenderer before. Set property now.
383 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
385 // Change the shader must not be occured many times. we always have to use corner radius feature.
386 mImpl->mAlwaysUsingCornerRadius = true;
388 if(!IsBorderlineRequired())
390 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
391 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
395 if(!mImpl->mCustomShader)
397 needUpdateShader = true;
404 case Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY:
407 if(value.Get(policy))
411 case Toolkit::Visual::Transform::Policy::RELATIVE:
412 case Toolkit::Visual::Transform::Policy::ABSOLUTE:
414 mImpl->SetCornerRadiusPolicy(policy);
415 if(DALI_UNLIKELY(mImpl->mRenderer))
417 // Unusual case. SetProperty called after OnInitialize().
418 // Assume that DoAction call UPDATE_PROPERTY.
419 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
425 DALI_LOG_ERROR("Unsupported policy: %d\n", policy);
435 DoSetProperties(propertyMap);
437 if(DALI_UNLIKELY(needUpdateShader))
443 void Visual::Base::SetTransformAndSize(const Property::Map& transform, Size controlSize)
445 mImpl->mControlSize = controlSize;
446 mImpl->mTransform.UpdatePropertyMap(transform);
448 #if defined(DEBUG_ENABLED)
449 std::ostringstream oss;
451 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);
457 void Visual::Base::SetName(const std::string& name)
462 const std::string& Visual::Base::GetName() const
467 float Visual::Base::GetHeightForWidth(float width)
469 float aspectCorrectedHeight = 0.f;
471 GetNaturalSize(naturalSize);
472 if(naturalSize.width > 0.0f)
474 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
476 return aspectCorrectedHeight;
479 float Visual::Base::GetWidthForHeight(float height)
481 float aspectCorrectedWidth = 0.f;
483 GetNaturalSize(naturalSize);
484 if(naturalSize.height > 0.0f)
486 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
488 return aspectCorrectedWidth;
491 void Visual::Base::GetNaturalSize(Vector2& naturalSize)
493 naturalSize = Vector2::ZERO;
496 void Visual::Base::DoAction(const Property::Index actionId, const Property::Value attributes)
498 OnDoAction(actionId, attributes);
500 // Check if action is valid for this visual type and perform action if possible
503 case DevelVisual::Action::UPDATE_PROPERTY:
505 const Property::Map* map = attributes.GetMap();
515 void Visual::Base::DoActionExtension(const Dali::Property::Index actionId, const Dali::Any attributes)
517 OnDoActionExtension(actionId, attributes);
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(IsRoundedCornerRequired())
574 mImpl->SetCornerRadius(mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::CORNER_RADIUS));
576 if(IsBorderlineRequired())
578 mImpl->SetBorderlineWidth(mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH));
579 mImpl->SetBorderlineColor(mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::BORDERLINE_COLOR));
580 mImpl->SetBorderlineOffset(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->GetBorderlineWidth());
610 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
611 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
614 if(IsTypeAvailableForCornerRadius(mImpl->mType))
616 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
617 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, mImpl->GetCornerRadiusPolicy());
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);
674 Vector4 retValue = Vector4::ZERO;
675 if(value.Get(retValue))
677 if(mImpl->mDecorationData != nullptr || retValue != Vector4::ZERO)
679 mImpl->SetCornerRadius(retValue);
683 return mImpl->mAlwaysUsingCornerRadius || !(mImpl->GetCornerRadius() == Vector4::ZERO);
688 bool Visual::Base::IsBorderlineRequired() const
690 // If VisualType doesn't support borderline, always return false.
691 if(IsTypeAvailableForBorderline(mImpl->mType))
695 // Update values from Renderer
696 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH);
698 float retValue = 0.0f;
699 if(value.Get(retValue))
701 if(mImpl->mDecorationData != nullptr || !Dali::EqualsZero(retValue))
703 mImpl->SetBorderlineWidth(retValue);
707 return mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->GetBorderlineWidth());
712 void Visual::Base::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
714 // May be overriden by derived class
717 void Visual::Base::OnDoActionExtension(const Property::Index actionId, const Dali::Any attributes)
719 // May be overriden by derived class
722 void Visual::Base::RegisterMixColor()
726 // All visual renderers now use same mix color / opacity properties.
727 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
728 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, mImpl->mMixColor.a);
730 float preMultipliedAlpha = 0.0f;
731 if(IsPreMultipliedAlphaEnabled())
733 preMultipliedAlpha = 1.0f;
735 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultipliedAlpha);
739 void Visual::Base::RegisterDecoration()
743 if(IsTypeAvailableForCornerRadius(mImpl->mType))
745 if(mImpl->mAlwaysUsingCornerRadius || !(mImpl->GetCornerRadius() == Vector4::ZERO))
747 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
748 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
749 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
752 if(IsTypeAvailableForBorderline(mImpl->mType))
754 if(mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->GetBorderlineWidth()))
756 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
757 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
758 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
759 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
765 void Visual::Base::SetMixColor(const Vector4& color)
767 mImpl->mMixColor = color;
771 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(color));
772 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, color.a);
776 void Visual::Base::SetMixColor(const Vector3& color)
778 mImpl->mMixColor.r = color.r;
779 mImpl->mMixColor.g = color.g;
780 mImpl->mMixColor.b = color.b;
784 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, color);
788 void Visual::Base::AddEventObserver(Visual::EventObserver& observer)
790 mImpl->mEventObserver = &observer;
793 void Visual::Base::RemoveEventObserver(Visual::EventObserver& observer)
795 mImpl->mEventObserver = NULL;
798 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
800 if(mImpl->mResourceStatus != resourceStatus)
802 mImpl->mResourceStatus = resourceStatus;
804 if(mImpl->mEventObserver)
806 // observer is currently a control impl
807 mImpl->mEventObserver->ResourceReady(*this);
812 bool Visual::Base::IsResourceReady() const
814 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
815 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
818 bool Visual::Base::IsSynchronousLoadingRequired() const
820 return (mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING);
823 Toolkit::Visual::Type Visual::Base::GetType() const
828 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
830 return mImpl->mResourceStatus;
833 Visual::FittingMode Visual::Base::GetFittingMode() const
835 return mImpl->mFittingMode;
838 Visual::Base& Visual::Base::GetVisualObject()
843 Renderer Visual::Base::GetRenderer()
845 return mImpl->mRenderer;
848 Property::Index Visual::Base::GetIntKey(Property::Key key)
850 if(key.type == Property::Key::INDEX)
855 if(key.stringKey == ANCHOR_POINT)
857 return Toolkit::Visual::Transform::Property::ANCHOR_POINT;
859 else if(key.stringKey == EXTRA_SIZE)
861 return Toolkit::DevelVisual::Transform::Property::EXTRA_SIZE;
863 else if(key.stringKey == MIX_COLOR)
865 return Toolkit::Visual::Property::MIX_COLOR;
867 else if(key.stringKey == OPACITY)
869 return Toolkit::Visual::Property::OPACITY;
871 else if(key.stringKey == OFFSET)
873 return Toolkit::Visual::Transform::Property::OFFSET;
875 else if(key.stringKey == OFFSET_POLICY)
877 return Toolkit::Visual::Transform::Property::OFFSET_POLICY;
879 else if(key.stringKey == ORIGIN)
881 return Toolkit::Visual::Transform::Property::ORIGIN;
883 else if(key.stringKey == PREMULTIPLIED_ALPHA)
885 return Toolkit::Visual::Property::PREMULTIPLIED_ALPHA;
887 else if(key.stringKey == CUSTOM_SHADER)
889 return Toolkit::Visual::Property::SHADER;
891 else if(key.stringKey == SIZE)
893 return Toolkit::Visual::Transform::Property::SIZE;
895 else if(key.stringKey == SIZE_POLICY)
897 return Toolkit::Visual::Transform::Property::SIZE_POLICY;
899 else if(key.stringKey == TRANSFORM)
901 return Toolkit::Visual::Property::TRANSFORM;
903 else if(key.stringKey == VISUAL_FITTING_MODE)
905 return Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE;
907 else if(key.stringKey == CORNER_RADIUS)
909 return Toolkit::DevelVisual::Property::CORNER_RADIUS;
911 else if(key.stringKey == CORNER_RADIUS_POLICY)
913 return Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY;
915 else if(key.stringKey == BORDERLINE_WIDTH)
917 return Toolkit::DevelVisual::Property::BORDERLINE_WIDTH;
919 else if(key.stringKey == BORDERLINE_COLOR)
921 return Toolkit::DevelVisual::Property::BORDERLINE_COLOR;
923 else if(key.stringKey == BORDERLINE_OFFSET)
925 return Toolkit::DevelVisual::Property::BORDERLINE_OFFSET;
928 return Property::INVALID_INDEX;
931 Property::Index Visual::Base::GetPropertyIndex(Property::Key key)
933 switch(GetIntKey(key))
935 case Dali::Toolkit::Visual::Transform::Property::OFFSET:
937 return VisualRenderer::Property::TRANSFORM_OFFSET;
939 case Dali::Toolkit::Visual::Transform::Property::SIZE:
941 return VisualRenderer::Property::TRANSFORM_SIZE;
943 case Dali::Toolkit::Visual::Transform::Property::ORIGIN:
945 return VisualRenderer::Property::TRANSFORM_ORIGIN;
947 case Dali::Toolkit::Visual::Transform::Property::ANCHOR_POINT:
949 return VisualRenderer::Property::TRANSFORM_ANCHOR_POINT;
951 case Dali::Toolkit::Visual::Property::MIX_COLOR:
953 return VisualRenderer::Property::VISUAL_MIX_COLOR;
955 case Dali::Toolkit::Visual::Property::OPACITY:
957 return DevelRenderer::Property::OPACITY;
959 case Dali::Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
961 return VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA;
963 case Dali::Toolkit::DevelVisual::Property::CORNER_RADIUS:
965 return DecoratedVisualRenderer::Property::CORNER_RADIUS;
967 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
969 return DecoratedVisualRenderer::Property::BORDERLINE_WIDTH;
971 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
973 return DecoratedVisualRenderer::Property::BORDERLINE_COLOR;
975 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
977 return DecoratedVisualRenderer::Property::BORDERLINE_OFFSET;
981 Property::Index index = mImpl->mRenderer.GetPropertyIndex(key);
983 if(index == Property::INVALID_INDEX)
985 // Is it a shader property?
986 Shader shader = mImpl->mRenderer.GetShader();
987 index = shader.GetPropertyIndex(key);
988 if(index != Property::INVALID_INDEX)
990 // Yes - we should register it in the Renderer so it can be set / animated
991 // independently, as shaders are shared across multiple renderers.
993 Property::Index keyIndex(Property::INVALID_KEY);
994 if(key.type == Property::Key::INDEX)
996 keyName = shader.GetPropertyName(index);
997 keyIndex = key.indexKey;
1001 keyName = key.stringKey;
1002 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
1004 Property::Value value = shader.GetProperty(index);
1006 // We already know that mRenderer didn't have property. So we can assume that it is unique.
1007 index = mImpl->mRenderer.RegisterUniqueProperty(keyIndex, keyName, value);
1013 void Visual::Base::SetupTransition(
1014 Dali::Animation& transition,
1015 Internal::TransitionData::Animator& animator,
1016 Property::Index index,
1017 Property::Value& initialValue,
1018 Property::Value& targetValue)
1020 if(index != Property::INVALID_INDEX)
1022 if(mImpl->mRenderer)
1024 if(animator.animate == false)
1026 mImpl->mRenderer.SetProperty(index, targetValue);
1030 if(animator.initialValue.GetType() != Property::NONE)
1032 mImpl->mRenderer.SetProperty(index, initialValue);
1037 transition = Dali::Animation::New(0.1f);
1040 transition.AnimateTo(Property(mImpl->mRenderer, index),
1042 animator.alphaFunction,
1043 TimePeriod(animator.timePeriodDelay,
1044 animator.timePeriodDuration));
1050 void Visual::Base::AnimateProperty(
1051 Dali::Animation& transition,
1052 Internal::TransitionData::Animator& animator)
1054 #if defined(DEBUG_ENABLED)
1055 if(gVisualBaseLogFilter->IsEnabledFor(Debug::General))
1057 std::ostringstream oss;
1058 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
1059 DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, oss.str().c_str());
1063 if(animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
1064 animator.propertyKey == MIX_COLOR ||
1065 (mImpl->mType == Toolkit::Visual::COLOR &&
1066 animator.propertyKey == ColorVisual::Property::MIX_COLOR) ||
1067 (mImpl->mType == Toolkit::Visual::PRIMITIVE &&
1068 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR))
1070 AnimateMixColorProperty(transition, animator);
1072 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
1073 animator.propertyKey == OPACITY)
1075 AnimateOpacityProperty(transition, animator);
1077 else if(mImpl->mRenderer)
1079 AnimateRendererProperty(transition, animator);
1083 void Visual::Base::AnimateOpacityProperty(
1084 Dali::Animation& transition,
1085 Internal::TransitionData::Animator& animator)
1087 float targetOpacity;
1088 if(animator.targetValue.Get(targetOpacity))
1090 mImpl->mMixColor.a = targetOpacity;
1093 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue);
1096 void Visual::Base::AnimateRendererProperty(
1097 Dali::Animation& transition,
1098 Internal::TransitionData::Animator& animator)
1100 // Get actual renderer index (will convert transform keys into visualproperty indices)
1101 Property::Index index = GetPropertyIndex(animator.propertyKey);
1103 if(index != Property::INVALID_INDEX)
1105 if(animator.targetValue.GetType() != Property::NONE)
1107 // Try writing target value into transform property map
1108 // if it's not a valid key, then it won't alter mTransform
1110 if(animator.propertyKey.type == Property::Key::INDEX)
1112 map.Add(animator.propertyKey.indexKey, animator.targetValue);
1116 map.Add(animator.propertyKey.stringKey, animator.targetValue);
1119 mImpl->mTransform.UpdatePropertyMap(map);
1121 SetupTransition(transition, animator, index, animator.initialValue, animator.targetValue);
1125 void Visual::Base::AnimateMixColorProperty(
1126 Dali::Animation& transition,
1127 Internal::TransitionData::Animator& animator)
1129 bool animateOpacity = false;
1131 Property::Value initialOpacity;
1132 Property::Value targetOpacity;
1133 Property::Value initialMixColor;
1134 Property::Value targetMixColor;
1136 Vector4 initialColor;
1137 if(animator.initialValue.Get(initialColor))
1139 if(animator.initialValue.GetType() == Property::VECTOR4)
1141 // if there is an initial color specifying alpha, test it
1142 initialOpacity = initialColor.a;
1144 initialMixColor = Vector3(initialColor);
1147 // Set target value into data store
1148 if(animator.targetValue.GetType() != Property::NONE)
1151 animator.targetValue.Get(mixColor);
1152 if(animator.targetValue.GetType() == Property::VECTOR4)
1154 mImpl->mMixColor.a = mixColor.a;
1155 targetOpacity = mixColor.a;
1156 animateOpacity = true;
1159 mImpl->mMixColor.r = mixColor.r;
1160 mImpl->mMixColor.g = mixColor.g;
1161 mImpl->mMixColor.b = mixColor.b;
1162 targetMixColor = Vector3(mixColor);
1165 SetupTransition(transition, animator, VisualRenderer::Property::VISUAL_MIX_COLOR, initialMixColor, targetMixColor);
1169 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity);
1173 Dali::Property Visual::Base::GetPropertyObject(Dali::Property::Key key)
1175 if(!mImpl->mRenderer)
1178 return Dali::Property(handle, Property::INVALID_INDEX);
1181 switch(GetIntKey(key))
1183 // Default animatable properties from VisualRenderer
1184 case Toolkit::Visual::Property::MIX_COLOR:
1186 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1188 case Toolkit::Visual::Property::OPACITY:
1190 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1192 case Toolkit::Visual::Transform::Property::OFFSET:
1194 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_OFFSET);
1196 case Toolkit::Visual::Transform::Property::SIZE:
1198 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_SIZE);
1201 // Default animatable properties from DecoratedVisualRenderer
1202 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
1204 if(IsTypeAvailableForCornerRadius(mImpl->mType))
1206 const bool updateShader = !mImpl->mCustomShader && !IsRoundedCornerRequired();
1208 // CornerRadius is animated now. we always have to use corner radius feature.
1209 mImpl->mAlwaysUsingCornerRadius = true;
1213 // Update each values to renderer
1214 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
1215 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->GetCornerRadius());
1216 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, static_cast<float>(mImpl->GetCornerRadiusPolicy()));
1221 if(!IsBorderlineRequired())
1223 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
1224 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
1226 return Dali::Property(mImpl->mRenderer, DecoratedVisualRenderer::Property::CORNER_RADIUS);
1230 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
1231 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
1232 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
1234 if(IsTypeAvailableForBorderline(mImpl->mType))
1236 const bool updateShader = !mImpl->mCustomShader && !IsBorderlineRequired();
1238 // Borderline is animated now. we always have to use borderline feature.
1239 mImpl->mAlwaysUsingBorderline = true;
1243 // Update each values to renderer
1244 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
1245 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->GetBorderlineWidth());
1246 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->GetBorderlineColor());
1247 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->GetBorderlineOffset());
1252 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
1254 return Dali::Property(mImpl->mRenderer, GetPropertyIndex(key));
1260 // Special case for MIX_COLOR
1261 if(key.type == Property::Key::INDEX &&
1262 ((mImpl->mType == Toolkit::Visual::COLOR && key.indexKey == ColorVisual::Property::MIX_COLOR) ||
1263 (mImpl->mType == Toolkit::Visual::PRIMITIVE && key.indexKey == PrimitiveVisual::Property::MIX_COLOR)))
1265 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1268 // Special case for BLUR_RADIUS
1269 if(mImpl->mType == Toolkit::Visual::COLOR &&
1270 ((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) ||
1271 (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME)))
1273 // Request to color-visual class
1274 return OnGetPropertyObject(key);
1279 // If it is not VisualRenderer property, check registered Renderer and Shader property.
1280 Property::Index index = GetPropertyIndex(key);
1281 if(index != Property::INVALID_INDEX)
1283 return Dali::Property(mImpl->mRenderer, index);
1286 // We can't find the property in the base class.
1287 // Request to child class
1288 return OnGetPropertyObject(key);
1291 } // namespace Internal
1293 } // namespace Toolkit