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::DoActionExtension(const Dali::Property::Index actionId, const Dali::Any attributes)
522 OnDoActionExtension(actionId, attributes);
525 void Visual::Base::SetDepthIndex(int index)
527 mImpl->mDepthIndex = index;
530 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
534 int Visual::Base::GetDepthIndex() const
536 return mImpl->mDepthIndex;
539 void Visual::Base::SetOnScene(Actor& actor)
543 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
544 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
549 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
550 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
553 mImpl->mFlags |= Impl::IS_ON_SCENE;
557 void Visual::Base::SetOffScene(Actor& actor)
561 DoSetOffScene(actor);
562 mImpl->mFlags &= ~Impl::IS_ON_SCENE;
566 void Visual::Base::CreatePropertyMap(Property::Map& map) const
570 // Update values from Renderer
571 mImpl->mMixColor = mImpl->mRenderer.GetProperty<Vector3>(VisualRenderer::Property::VISUAL_MIX_COLOR);
572 mImpl->mMixColor.a = mImpl->mRenderer.GetProperty<float>(DevelRenderer::Property::OPACITY);
574 mImpl->mTransform.mOffset = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_OFFSET);
575 mImpl->mTransform.mSize = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_SIZE);
577 if(IsTypeAvailableForCornerRadius(mImpl->mType))
579 mImpl->mCornerRadius = mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::CORNER_RADIUS);
581 if(IsTypeAvailableForBorderline(mImpl->mType))
583 mImpl->mBorderlineWidth = mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH);
584 mImpl->mBorderlineColor = mImpl->mRenderer.GetProperty<Vector4>(DecoratedVisualRenderer::Property::BORDERLINE_COLOR);
585 mImpl->mBorderlineOffset = mImpl->mRenderer.GetProperty<float>(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET);
589 DoCreatePropertyMap(map);
591 if(mImpl->mCustomShader)
593 mImpl->mCustomShader->CreatePropertyMap(map);
596 Property::Map transform;
597 mImpl->mTransform.GetPropertyMap(transform);
598 map.Insert(Toolkit::Visual::Property::TRANSFORM, transform);
600 bool premultipliedAlpha(IsPreMultipliedAlphaEnabled());
601 map.Insert(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha);
603 // Note, Color and Primitive will also insert their own mix color into the map
604 // which is ok, because they have a different key value range, but uses same cached value anyway.
605 map.Insert(Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor); // vec4
606 map.Insert(Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a);
608 auto fittingModeString = Scripting::GetLinearEnumerationName<FittingMode>(
609 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT);
610 map.Insert(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString);
612 if(IsTypeAvailableForBorderline(mImpl->mType))
614 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
615 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
616 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
619 if(IsTypeAvailableForCornerRadius(mImpl->mType))
621 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius);
622 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, static_cast<int>(mImpl->mCornerRadiusPolicy));
626 void Visual::Base::CreateInstancePropertyMap(Property::Map& map) const
628 DoCreateInstancePropertyMap(map);
630 if(mImpl->mCustomShader)
632 mImpl->mCustomShader->CreatePropertyMap(map);
636 void Visual::Base::EnablePreMultipliedAlpha(bool preMultiplied)
640 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
644 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
649 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
650 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultiplied);
654 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
656 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
659 void Visual::Base::DoSetOffScene(Actor& actor)
661 actor.RemoveRenderer(mImpl->mRenderer);
664 bool Visual::Base::IsOnScene() const
666 return mImpl->mFlags & Impl::IS_ON_SCENE;
669 bool Visual::Base::IsRoundedCornerRequired() const
671 // If VisualType doesn't support rounded corner, always return false.
672 if(IsTypeAvailableForCornerRadius(mImpl->mType))
676 // Update values from Renderer
677 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS);
678 value.Get(mImpl->mCornerRadius);
680 return mImpl->mAlwaysUsingCornerRadius || !(mImpl->mCornerRadius == Vector4::ZERO);
685 bool Visual::Base::IsBorderlineRequired() const
687 // If VisualType doesn't support borderline, always return false.
688 if(IsTypeAvailableForBorderline(mImpl->mType))
692 // Update values from Renderer
693 Property::Value value = mImpl->mRenderer.GetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH);
694 value.Get(mImpl->mBorderlineWidth);
696 return mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->mBorderlineWidth);
701 void Visual::Base::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
703 // May be overriden by derived class
706 void Visual::Base::OnDoActionExtension(const Property::Index actionId, const Dali::Any attributes)
708 // May be overriden by derived class
711 void Visual::Base::RegisterMixColor()
715 // All visual renderers now use same mix color / opacity properties.
716 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
717 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, mImpl->mMixColor.a);
719 float preMultipliedAlpha = 0.0f;
720 if(IsPreMultipliedAlphaEnabled())
722 preMultipliedAlpha = 1.0f;
724 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultipliedAlpha);
728 void Visual::Base::RegisterDecoration()
732 if(IsTypeAvailableForCornerRadius(mImpl->mType))
734 if(mImpl->mAlwaysUsingCornerRadius || !(mImpl->mCornerRadius == Vector4::ZERO))
736 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
737 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->mCornerRadius);
738 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
741 if(IsTypeAvailableForBorderline(mImpl->mType))
743 if(mImpl->mAlwaysUsingBorderline || !EqualsZero(mImpl->mBorderlineWidth))
745 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
746 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
747 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
748 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
754 void Visual::Base::SetMixColor(const Vector4& color)
756 mImpl->mMixColor = color;
760 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(color));
761 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, color.a);
765 void Visual::Base::SetMixColor(const Vector3& color)
767 mImpl->mMixColor.r = color.r;
768 mImpl->mMixColor.g = color.g;
769 mImpl->mMixColor.b = color.b;
773 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, color);
777 void Visual::Base::AddEventObserver(Visual::EventObserver& observer)
779 mImpl->mEventObserver = &observer;
782 void Visual::Base::RemoveEventObserver(Visual::EventObserver& observer)
784 mImpl->mEventObserver = NULL;
787 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
789 if(mImpl->mResourceStatus != resourceStatus)
791 mImpl->mResourceStatus = resourceStatus;
793 if(mImpl->mEventObserver)
795 // observer is currently a control impl
796 mImpl->mEventObserver->ResourceReady(*this);
801 bool Visual::Base::IsResourceReady() const
803 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY ||
804 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
807 bool Visual::Base::IsSynchronousLoadingRequired() const
809 return (mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING);
812 Toolkit::Visual::Type Visual::Base::GetType() const
817 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
819 return mImpl->mResourceStatus;
822 Visual::FittingMode Visual::Base::GetFittingMode() const
824 return mImpl->mFittingMode;
827 Visual::Base& Visual::Base::GetVisualObject()
832 Renderer Visual::Base::GetRenderer()
834 return mImpl->mRenderer;
837 Property::Index Visual::Base::GetIntKey(Property::Key key)
839 if(key.type == Property::Key::INDEX)
844 if(key.stringKey == ANCHOR_POINT)
846 return Toolkit::Visual::Transform::Property::ANCHOR_POINT;
848 else if(key.stringKey == EXTRA_SIZE)
850 return Toolkit::DevelVisual::Transform::Property::EXTRA_SIZE;
852 else if(key.stringKey == MIX_COLOR)
854 return Toolkit::Visual::Property::MIX_COLOR;
856 else if(key.stringKey == OPACITY)
858 return Toolkit::Visual::Property::OPACITY;
860 else if(key.stringKey == OFFSET)
862 return Toolkit::Visual::Transform::Property::OFFSET;
864 else if(key.stringKey == OFFSET_POLICY)
866 return Toolkit::Visual::Transform::Property::OFFSET_POLICY;
868 else if(key.stringKey == ORIGIN)
870 return Toolkit::Visual::Transform::Property::ORIGIN;
872 else if(key.stringKey == PREMULTIPLIED_ALPHA)
874 return Toolkit::Visual::Property::PREMULTIPLIED_ALPHA;
876 else if(key.stringKey == CUSTOM_SHADER)
878 return Toolkit::Visual::Property::SHADER;
880 else if(key.stringKey == SIZE)
882 return Toolkit::Visual::Transform::Property::SIZE;
884 else if(key.stringKey == SIZE_POLICY)
886 return Toolkit::Visual::Transform::Property::SIZE_POLICY;
888 else if(key.stringKey == TRANSFORM)
890 return Toolkit::Visual::Property::TRANSFORM;
892 else if(key.stringKey == VISUAL_FITTING_MODE)
894 return Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE;
896 else if(key.stringKey == CORNER_RADIUS)
898 return Toolkit::DevelVisual::Property::CORNER_RADIUS;
900 else if(key.stringKey == CORNER_RADIUS_POLICY)
902 return Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY;
904 else if(key.stringKey == BORDERLINE_WIDTH)
906 return Toolkit::DevelVisual::Property::BORDERLINE_WIDTH;
908 else if(key.stringKey == BORDERLINE_COLOR)
910 return Toolkit::DevelVisual::Property::BORDERLINE_COLOR;
912 else if(key.stringKey == BORDERLINE_OFFSET)
914 return Toolkit::DevelVisual::Property::BORDERLINE_OFFSET;
917 return Property::INVALID_INDEX;
920 Property::Index Visual::Base::GetPropertyIndex(Property::Key key)
922 switch(GetIntKey(key))
924 case Dali::Toolkit::Visual::Transform::Property::OFFSET:
926 return VisualRenderer::Property::TRANSFORM_OFFSET;
928 case Dali::Toolkit::Visual::Transform::Property::SIZE:
930 return VisualRenderer::Property::TRANSFORM_SIZE;
932 case Dali::Toolkit::Visual::Transform::Property::ORIGIN:
934 return VisualRenderer::Property::TRANSFORM_ORIGIN;
936 case Dali::Toolkit::Visual::Transform::Property::ANCHOR_POINT:
938 return VisualRenderer::Property::TRANSFORM_ANCHOR_POINT;
940 case Dali::Toolkit::Visual::Property::MIX_COLOR:
942 return VisualRenderer::Property::VISUAL_MIX_COLOR;
944 case Dali::Toolkit::Visual::Property::OPACITY:
946 return DevelRenderer::Property::OPACITY;
948 case Dali::Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
950 return VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA;
952 case Dali::Toolkit::DevelVisual::Property::CORNER_RADIUS:
954 return DecoratedVisualRenderer::Property::CORNER_RADIUS;
956 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
958 return DecoratedVisualRenderer::Property::BORDERLINE_WIDTH;
960 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
962 return DecoratedVisualRenderer::Property::BORDERLINE_COLOR;
964 case Dali::Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
966 return DecoratedVisualRenderer::Property::BORDERLINE_OFFSET;
970 Property::Index index = mImpl->mRenderer.GetPropertyIndex(key);
972 if(index == Property::INVALID_INDEX)
974 // Is it a shader property?
975 Shader shader = mImpl->mRenderer.GetShader();
976 index = shader.GetPropertyIndex(key);
977 if(index != Property::INVALID_INDEX)
979 // Yes - we should register it in the Renderer so it can be set / animated
980 // independently, as shaders are shared across multiple renderers.
982 Property::Index keyIndex(Property::INVALID_KEY);
983 if(key.type == Property::Key::INDEX)
985 keyName = shader.GetPropertyName(index);
986 keyIndex = key.indexKey;
990 keyName = key.stringKey;
991 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
993 Property::Value value = shader.GetProperty(index);
995 // We already know that mRenderer didn't have property. So we can assume that it is unique.
996 index = mImpl->mRenderer.RegisterUniqueProperty(keyIndex, keyName, value);
1002 void Visual::Base::SetupTransition(
1003 Dali::Animation& transition,
1004 Internal::TransitionData::Animator& animator,
1005 Property::Index index,
1006 Property::Value& initialValue,
1007 Property::Value& targetValue)
1009 if(index != Property::INVALID_INDEX)
1011 if(mImpl->mRenderer)
1013 if(animator.animate == false)
1015 mImpl->mRenderer.SetProperty(index, targetValue);
1019 if(animator.initialValue.GetType() != Property::NONE)
1021 mImpl->mRenderer.SetProperty(index, initialValue);
1026 transition = Dali::Animation::New(0.1f);
1029 transition.AnimateTo(Property(mImpl->mRenderer, index),
1031 animator.alphaFunction,
1032 TimePeriod(animator.timePeriodDelay,
1033 animator.timePeriodDuration));
1039 void Visual::Base::AnimateProperty(
1040 Dali::Animation& transition,
1041 Internal::TransitionData::Animator& animator)
1043 #if defined(DEBUG_ENABLED)
1044 if(gVisualBaseLogFilter->IsEnabledFor(Debug::General))
1046 std::ostringstream oss;
1047 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
1048 DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, oss.str().c_str());
1052 if(animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
1053 animator.propertyKey == MIX_COLOR ||
1054 (mImpl->mType == Toolkit::Visual::COLOR &&
1055 animator.propertyKey == ColorVisual::Property::MIX_COLOR) ||
1056 (mImpl->mType == Toolkit::Visual::PRIMITIVE &&
1057 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR))
1059 AnimateMixColorProperty(transition, animator);
1061 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
1062 animator.propertyKey == OPACITY)
1064 AnimateOpacityProperty(transition, animator);
1066 else if(mImpl->mRenderer)
1068 AnimateRendererProperty(transition, animator);
1072 void Visual::Base::AnimateOpacityProperty(
1073 Dali::Animation& transition,
1074 Internal::TransitionData::Animator& animator)
1076 float targetOpacity;
1077 if(animator.targetValue.Get(targetOpacity))
1079 mImpl->mMixColor.a = targetOpacity;
1082 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue);
1085 void Visual::Base::AnimateRendererProperty(
1086 Dali::Animation& transition,
1087 Internal::TransitionData::Animator& animator)
1089 // Get actual renderer index (will convert transform keys into visualproperty indices)
1090 Property::Index index = GetPropertyIndex(animator.propertyKey);
1092 if(index != Property::INVALID_INDEX)
1094 if(animator.targetValue.GetType() != Property::NONE)
1096 // Try writing target value into transform property map
1097 // if it's not a valid key, then it won't alter mTransform
1099 if(animator.propertyKey.type == Property::Key::INDEX)
1101 map.Add(animator.propertyKey.indexKey, animator.targetValue);
1105 map.Add(animator.propertyKey.stringKey, animator.targetValue);
1108 mImpl->mTransform.UpdatePropertyMap(map);
1110 SetupTransition(transition, animator, index, animator.initialValue, animator.targetValue);
1114 void Visual::Base::AnimateMixColorProperty(
1115 Dali::Animation& transition,
1116 Internal::TransitionData::Animator& animator)
1118 bool animateOpacity = false;
1120 Property::Value initialOpacity;
1121 Property::Value targetOpacity;
1122 Property::Value initialMixColor;
1123 Property::Value targetMixColor;
1125 Vector4 initialColor;
1126 if(animator.initialValue.Get(initialColor))
1128 if(animator.initialValue.GetType() == Property::VECTOR4)
1130 // if there is an initial color specifying alpha, test it
1131 initialOpacity = initialColor.a;
1133 initialMixColor = Vector3(initialColor);
1136 // Set target value into data store
1137 if(animator.targetValue.GetType() != Property::NONE)
1140 animator.targetValue.Get(mixColor);
1141 if(animator.targetValue.GetType() == Property::VECTOR4)
1143 mImpl->mMixColor.a = mixColor.a;
1144 targetOpacity = mixColor.a;
1145 animateOpacity = true;
1148 mImpl->mMixColor.r = mixColor.r;
1149 mImpl->mMixColor.g = mixColor.g;
1150 mImpl->mMixColor.b = mixColor.b;
1151 targetMixColor = Vector3(mixColor);
1154 SetupTransition(transition, animator, VisualRenderer::Property::VISUAL_MIX_COLOR, initialMixColor, targetMixColor);
1158 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity);
1162 Dali::Property Visual::Base::GetPropertyObject(Dali::Property::Key key)
1164 if(!mImpl->mRenderer)
1167 return Dali::Property(handle, Property::INVALID_INDEX);
1170 switch(GetIntKey(key))
1172 // Default animatable properties from VisualRenderer
1173 case Toolkit::Visual::Property::MIX_COLOR:
1175 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1177 case Toolkit::Visual::Property::OPACITY:
1179 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1181 case Toolkit::Visual::Transform::Property::OFFSET:
1183 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_OFFSET);
1185 case Toolkit::Visual::Transform::Property::SIZE:
1187 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_SIZE);
1190 // Default animatable properties from DecoratedVisualRenderer
1191 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
1193 if(IsTypeAvailableForCornerRadius(mImpl->mType))
1195 const bool updateShader = !mImpl->mCustomShader && !IsRoundedCornerRequired();
1197 // CornerRadius is animated now. we always have to use corner radius feature.
1198 mImpl->mAlwaysUsingCornerRadius = true;
1202 // Update each values to renderer
1203 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterCornerRadiusUniform();
1204 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS, mImpl->mCornerRadius);
1205 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
1210 if(!IsBorderlineRequired())
1212 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
1213 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
1215 return Dali::Property(mImpl->mRenderer, DecoratedVisualRenderer::Property::CORNER_RADIUS);
1219 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
1220 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
1221 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
1223 if(IsTypeAvailableForBorderline(mImpl->mType))
1225 const bool updateShader = !mImpl->mCustomShader && !IsBorderlineRequired();
1227 // Borderline is animated now. we always have to use borderline feature.
1228 mImpl->mAlwaysUsingBorderline = true;
1232 // Update each values to renderer
1233 DownCast<DecoratedVisualRenderer>(mImpl->mRenderer).RegisterBorderlineUniform();
1234 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
1235 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
1236 mImpl->mRenderer.SetProperty(DecoratedVisualRenderer::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
1241 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
1243 return Dali::Property(mImpl->mRenderer, GetPropertyIndex(key));
1249 // Special case for MIX_COLOR
1250 if(key.type == Property::Key::INDEX &&
1251 ((mImpl->mType == Toolkit::Visual::COLOR && key.indexKey == ColorVisual::Property::MIX_COLOR) ||
1252 (mImpl->mType == Toolkit::Visual::PRIMITIVE && key.indexKey == PrimitiveVisual::Property::MIX_COLOR)))
1254 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1257 // Special case for BLUR_RADIUS
1258 if(mImpl->mType == Toolkit::Visual::COLOR &&
1259 ((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) ||
1260 (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME)))
1262 // Request to color-visual class
1263 return OnGetPropertyObject(key);
1268 // If it is not VisualRenderer property, check registered Renderer and Shader property.
1269 Property::Index index = GetPropertyIndex(key);
1270 if(index != Property::INVALID_INDEX)
1272 return Dali::Property(mImpl->mRenderer, index);
1275 // We can't find the property in the base class.
1276 // Request to child class
1277 return OnGetPropertyObject(key);
1280 } // namespace Internal
1282 } // namespace Toolkit