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>
28 #include <dali-toolkit/devel-api/visuals/visual-actions-devel.h>
29 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
30 #include <dali-toolkit/internal/helpers/property-helper.h>
31 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
32 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
33 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
34 #include <dali-toolkit/public-api/visuals/primitive-visual-properties.h>
35 #include <dali-toolkit/public-api/visuals/visual-properties.h>
39 #if defined(DEBUG_ENABLED)
40 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VISUAL_BASE");
43 const char* const PRE_MULTIPLIED_ALPHA_PROPERTY("preMultipliedAlpha");
55 DALI_ENUM_TO_STRING_TABLE_BEGIN(VISUAL_FITTING_MODE)
56 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_KEEP_ASPECT_RATIO)
57 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FILL)
58 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, OVER_FIT_KEEP_ASPECT_RATIO)
59 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, CENTER)
60 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_WIDTH)
61 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_HEIGHT)
62 DALI_ENUM_TO_STRING_TABLE_END(VISUAL_FITTING_MODE)
65 * @brief Check whether this visual type can use corner radius feature or not.
66 * @param type VisualType that want to checkup
67 * @return true if type can use corner radius feature.
69 static bool IsTypeAvailableForCornerRadius(Toolkit::Visual::Type type)
71 switch(static_cast<Toolkit::DevelVisual::Type>(type))
73 case Toolkit::Visual::Type::COLOR:
74 case Toolkit::Visual::Type::GRADIENT:
75 case Toolkit::Visual::Type::IMAGE:
76 case Toolkit::Visual::Type::SVG:
77 case Toolkit::Visual::Type::ANIMATED_IMAGE:
78 case Toolkit::DevelVisual::Type::ANIMATED_VECTOR_IMAGE:
90 * @brief Check whether this visual type can use borderline feature or not.
91 * @param type VisualType that want to checkup
92 * @return true if type can use borderline feature.
94 static bool IsTypeAvailableForBorderline(Toolkit::Visual::Type type)
96 switch(static_cast<Toolkit::DevelVisual::Type>(type))
98 case Toolkit::Visual::Type::COLOR:
99 case Toolkit::Visual::Type::GRADIENT:
100 case Toolkit::Visual::Type::IMAGE:
101 case Toolkit::Visual::Type::SVG:
102 case Toolkit::Visual::Type::ANIMATED_IMAGE:
103 case Toolkit::DevelVisual::Type::ANIMATED_VECTOR_IMAGE:
116 Visual::Base::Base(VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type)
117 : mImpl(new Impl(fittingMode, type)),
118 mFactoryCache(factoryCache)
122 Visual::Base::~Base()
127 void Visual::Base::Initialize()
129 // The Renderer should be created inside derived class here.
136 if(IsRoundedCornerRequired())
138 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
139 mImpl->mRenderer.RegisterUniqueProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
141 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
143 if(IsBorderlineRequired())
145 mImpl->mBorderlineWidthIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
146 mImpl->mBorderlineColorIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
147 mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_OFFSET, BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
149 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
154 void Visual::Base::SetCustomShader(const Property::Map& shaderMap)
156 if(mImpl->mCustomShader)
158 mImpl->mCustomShader->SetPropertyMap(shaderMap);
162 mImpl->mCustomShader = new Impl::CustomShader(shaderMap);
165 // Let derived class know
169 void Visual::Base::SetProperties(const Property::Map& propertyMap)
171 bool needUpdateShader = false;
172 for(size_t i = 0; i < propertyMap.Count(); ++i)
174 const KeyValuePair& pair = propertyMap.GetKeyValue(i);
175 const Property::Key& key = pair.first;
176 const Property::Value& value = pair.second;
178 Property::Key matchKey = key;
179 if(matchKey.type == Property::Key::STRING)
181 if(matchKey == CUSTOM_SHADER)
183 matchKey = Property::Key(Toolkit::Visual::Property::SHADER);
185 else if(matchKey == TRANSFORM)
187 matchKey = Property::Key(Toolkit::Visual::Property::TRANSFORM);
189 else if(matchKey == PREMULTIPLIED_ALPHA)
191 matchKey = Property::Key(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA);
193 else if(matchKey == MIX_COLOR)
195 matchKey = Property::Key(Toolkit::Visual::Property::MIX_COLOR);
197 else if(matchKey == OPACITY)
199 matchKey = Property::Key(Toolkit::Visual::Property::OPACITY);
201 else if(matchKey == VISUAL_FITTING_MODE)
203 matchKey = Property::Key(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE);
205 else if(matchKey == BORDERLINE_WIDTH)
207 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH);
209 else if(matchKey == BORDERLINE_COLOR)
211 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_COLOR);
213 else if(matchKey == BORDERLINE_OFFSET)
215 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET);
217 else if(matchKey == CORNER_RADIUS)
219 matchKey = Property::Key(Toolkit::DevelVisual::Property::CORNER_RADIUS);
221 else if(matchKey == CORNER_RADIUS_POLICY)
223 matchKey = Property::Key(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY);
227 switch(matchKey.indexKey)
229 case Toolkit::Visual::Property::SHADER:
231 Property::Map shaderMap;
232 if(value.Get(shaderMap))
234 SetCustomShader(shaderMap);
239 case Toolkit::Visual::Property::TRANSFORM:
244 mImpl->mTransform.SetPropertyMap(map);
249 case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
251 bool premultipliedAlpha = false;
252 if(value.Get(premultipliedAlpha))
254 EnablePreMultipliedAlpha(premultipliedAlpha);
259 case Toolkit::Visual::Property::MIX_COLOR:
262 if(value.Get(mixColor))
264 if(value.GetType() == Property::VECTOR4)
266 SetMixColor(mixColor);
270 Vector3 mixColor3(mixColor);
271 SetMixColor(mixColor3);
276 case Toolkit::Visual::Property::OPACITY:
279 if(value.Get(opacity))
281 mImpl->mMixColor.a = opacity;
282 SetMixColor(mImpl->mMixColor);
286 case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
288 Scripting::GetEnumerationProperty<Visual::FittingMode>(
289 value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode);
292 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
297 mImpl->mBorderlineWidth = width;
300 if(mImpl->mBorderlineWidthIndex != Property::INVALID_INDEX)
302 mImpl->mRenderer.SetProperty(mImpl->mBorderlineWidthIndex, mImpl->mBorderlineWidth);
304 else if(DALI_UNLIKELY(mImpl->mRenderer && IsBorderlineRequired()))
306 // Unusual case. SetProperty called after OnInitialize().
307 // Assume that DoAction call UPDATE_PROPERTY.
308 // We must regist properies into renderer, and update shader.
310 // Borderline added by this action. Register property to renderer.
311 mImpl->mBorderlineWidthIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
312 mImpl->mBorderlineColorIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
313 mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_OFFSET, BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
315 // Make Blend mode ON_WITHOUT_CULL for transparent mix color.
316 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
318 // Change the shader must not be occured many times. we always have to use borderline feature.
319 mImpl->mAlwaysUsingBorderline = true;
322 needUpdateShader = true;
326 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
331 mImpl->mBorderlineColor = color;
334 if(mImpl->mBorderlineColorIndex != Property::INVALID_INDEX)
336 mImpl->mRenderer.SetProperty(mImpl->mBorderlineColorIndex, mImpl->mBorderlineColor);
340 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
343 if(value.Get(offset))
345 mImpl->mBorderlineOffset = offset;
348 if(mImpl->mBorderlineOffsetIndex != Property::INVALID_INDEX)
350 mImpl->mRenderer.SetProperty(mImpl->mBorderlineOffsetIndex, mImpl->mBorderlineOffset);
354 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
356 if(value.GetType() == Property::VECTOR4)
358 // If CORNER_RADIUS Property is Vector4,
359 // Each values mean the radius of
360 // (top-left, top-right, bottom-right, bottom-left)
362 if(value.Get(radius))
364 mImpl->mCornerRadius = radius;
369 // If CORNER_RADIUS Property is float,
370 // Every corner radius have same value
372 if(value.Get(radius))
374 mImpl->mCornerRadius = Vector4(radius, radius, radius, radius);
378 if(mImpl->mCornerRadiusIndex != Property::INVALID_INDEX)
380 mImpl->mRenderer.SetProperty(mImpl->mCornerRadiusIndex, mImpl->mCornerRadius);
382 else if(DALI_UNLIKELY(mImpl->mRenderer && IsRoundedCornerRequired()))
384 // Unusual case. SetProperty called after OnInitialize().
385 // Assume that DoAction call UPDATE_PROPERTY.
386 // We must regist properies into renderer, and update shader.
388 // CornerRadius added by this action. Regist property to renderer.
389 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
390 mImpl->mRenderer.RegisterUniqueProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
392 // Change the shader must not be occured many times. we always have to use corner radius feature.
393 mImpl->mAlwaysUsingCornerRadius = true;
395 if(!IsBorderlineRequired())
397 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
398 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
402 needUpdateShader = true;
406 case Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY:
409 if(value.Get(policy))
413 case Toolkit::Visual::Transform::Policy::RELATIVE:
414 case Toolkit::Visual::Transform::Policy::ABSOLUTE:
416 mImpl->mCornerRadiusPolicy = policy;
417 if(DALI_UNLIKELY(mImpl->mRenderer && mImpl->mCornerRadiusIndex != Property::INVALID_INDEX))
419 // Unusual case. SetProperty called after OnInitialize().
420 // Assume that DoAction call UPDATE_PROPERTY.
421 // We must update properies result into renderer
422 // Note : mImpl->mCornerRadiusIndex is not INVALID_INDEX.
423 // So CornerRadiusPolicy property is already registed.
424 mImpl->mRenderer.SetProperty(mImpl->mRenderer.GetPropertyIndex(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>(mImpl->mMixColorIndex);
567 mImpl->mMixColor.a = mImpl->mRenderer.GetProperty<float>(DevelRenderer::Property::OPACITY);
568 if(mImpl->mTransform.mOffsetIndex != Property::INVALID_INDEX)
570 mImpl->mTransform.mOffset = mImpl->mRenderer.GetProperty<Vector2>(mImpl->mTransform.mOffsetIndex);
572 if(mImpl->mTransform.mSizeIndex != Property::INVALID_INDEX)
574 mImpl->mTransform.mSize = mImpl->mRenderer.GetProperty<Vector2>(mImpl->mTransform.mSizeIndex);
576 if(mImpl->mCornerRadiusIndex != Property::INVALID_INDEX)
578 mImpl->mCornerRadius = mImpl->mRenderer.GetProperty<Vector4>(mImpl->mCornerRadiusIndex);
580 if(mImpl->mBorderlineWidthIndex != Property::INVALID_INDEX)
582 mImpl->mBorderlineWidth = mImpl->mRenderer.GetProperty<float>(mImpl->mBorderlineWidthIndex);
584 if(mImpl->mBorderlineColorIndex != Property::INVALID_INDEX)
586 mImpl->mBorderlineColor = mImpl->mRenderer.GetProperty<Vector4>(mImpl->mBorderlineColorIndex);
588 if(mImpl->mBorderlineOffsetIndex != Property::INVALID_INDEX)
590 mImpl->mBorderlineOffset = mImpl->mRenderer.GetProperty<float>(mImpl->mBorderlineOffsetIndex);
594 DoCreatePropertyMap(map);
596 if(mImpl->mCustomShader)
598 mImpl->mCustomShader->CreatePropertyMap(map);
601 Property::Map transform;
602 mImpl->mTransform.GetPropertyMap(transform);
603 map.Insert(Toolkit::Visual::Property::TRANSFORM, transform);
605 bool premultipliedAlpha(IsPreMultipliedAlphaEnabled());
606 map.Insert(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha);
608 // Note, Color and Primitive will also insert their own mix color into the map
609 // which is ok, because they have a different key value range.
610 map.Insert(Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor); // vec4
611 map.Insert(Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a);
613 auto fittingModeString = Scripting::GetLinearEnumerationName<FittingMode>(
614 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT);
615 map.Insert(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString);
617 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
618 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
619 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
621 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius);
622 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, static_cast<int>(mImpl->mCornerRadiusPolicy));
625 void Visual::Base::CreateInstancePropertyMap(Property::Map& map) const
627 DoCreateInstancePropertyMap(map);
629 if(mImpl->mCustomShader)
631 mImpl->mCustomShader->CreatePropertyMap(map);
635 void Visual::Base::EnablePreMultipliedAlpha(bool preMultiplied)
639 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
643 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
648 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
649 mImpl->mRenderer.RegisterProperty(PRE_MULTIPLIED_ALPHA_PROPERTY, static_cast<float>(preMultiplied));
653 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
655 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
658 void Visual::Base::DoSetOffScene(Actor& actor)
660 actor.RemoveRenderer(mImpl->mRenderer);
663 bool Visual::Base::IsOnScene() const
665 return mImpl->mFlags & Impl::IS_ON_SCENE;
668 bool Visual::Base::IsRoundedCornerRequired() const
670 // If VisualType doesn't support rounded corner, always return false.
671 if(IsTypeAvailableForCornerRadius(mImpl->mType))
673 if(mImpl->mRenderer && mImpl->mCornerRadiusIndex != Property::INVALID_INDEX)
675 // Update values from Renderer
676 mImpl->mCornerRadius = mImpl->mRenderer.GetProperty<Vector4>(mImpl->mCornerRadiusIndex);
678 return !(mImpl->mCornerRadius == Vector4::ZERO) || mImpl->mAlwaysUsingCornerRadius;
683 bool Visual::Base::IsBorderlineRequired() const
685 // If VisualType doesn't support borderline, always return false.
686 if(IsTypeAvailableForBorderline(mImpl->mType))
688 if(mImpl->mRenderer && mImpl->mBorderlineWidthIndex != Property::INVALID_INDEX)
690 // Update values from Renderer
691 mImpl->mBorderlineWidth = mImpl->mRenderer.GetProperty<float>(mImpl->mBorderlineWidthIndex);
693 return !EqualsZero(mImpl->mBorderlineWidth) || mImpl->mAlwaysUsingBorderline;
698 void Visual::Base::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
700 // May be overriden by derived class
703 void Visual::Base::RegisterMixColor()
705 // Only register if not already registered.
706 // (Color and Primitive visuals will register their own and save to this index)
707 if(mImpl->mMixColorIndex == Property::INVALID_INDEX)
709 mImpl->mMixColorIndex = mImpl->mRenderer.RegisterUniqueProperty(
710 Toolkit::Visual::Property::MIX_COLOR,
712 Vector3(mImpl->mMixColor));
715 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, mImpl->mMixColor.a);
717 float preMultipliedAlpha = 0.0f;
718 if(IsPreMultipliedAlphaEnabled())
720 preMultipliedAlpha = 1.0f;
722 mImpl->mRenderer.RegisterProperty(PRE_MULTIPLIED_ALPHA_PROPERTY, preMultipliedAlpha);
725 void Visual::Base::SetMixColor(const Vector4& color)
727 mImpl->mMixColor = color;
731 mImpl->mRenderer.SetProperty(mImpl->mMixColorIndex, Vector3(color));
732 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, color.a);
736 void Visual::Base::SetMixColor(const Vector3& color)
738 mImpl->mMixColor.r = color.r;
739 mImpl->mMixColor.g = color.g;
740 mImpl->mMixColor.b = color.b;
744 mImpl->mRenderer.SetProperty(mImpl->mMixColorIndex, color);
748 void Visual::Base::AddEventObserver(Visual::EventObserver& observer)
750 mImpl->mEventObserver = &observer;
753 void Visual::Base::RemoveEventObserver(Visual::EventObserver& observer)
755 mImpl->mEventObserver = NULL;
758 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
760 if(mImpl->mResourceStatus != resourceStatus)
762 mImpl->mResourceStatus = resourceStatus;
764 if(mImpl->mEventObserver)
766 // observer is currently a control impl
767 mImpl->mEventObserver->ResourceReady(*this);
772 bool Visual::Base::IsResourceReady() const
774 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY);
777 bool Visual::Base::IsSynchronousLoadingRequired() const
779 return (mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING);
782 Toolkit::Visual::Type Visual::Base::GetType() const
787 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
789 return mImpl->mResourceStatus;
792 Visual::FittingMode Visual::Base::GetFittingMode() const
794 return mImpl->mFittingMode;
797 Visual::Base& Visual::Base::GetVisualObject()
802 Renderer Visual::Base::GetRenderer()
804 return mImpl->mRenderer;
807 Property::Index Visual::Base::GetPropertyIndex(Property::Key key)
809 Property::Index index = mImpl->mRenderer.GetPropertyIndex(key);
811 if(index == Property::INVALID_INDEX)
813 // Is it a shader property?
814 Shader shader = mImpl->mRenderer.GetShader();
815 index = shader.GetPropertyIndex(key);
816 if(index != Property::INVALID_INDEX)
818 // Yes - we should register it in the Renderer so it can be set / animated
819 // independently, as shaders are shared across multiple renderers.
821 Property::Index keyIndex(Property::INVALID_KEY);
822 if(key.type == Property::Key::INDEX)
824 keyName = shader.GetPropertyName(index);
825 keyIndex = key.indexKey;
829 keyName = key.stringKey;
830 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
832 Property::Value value = shader.GetProperty(index);
834 // We already know that mRenderer didn't have property. So we can assume that it is unique.
835 index = mImpl->mRenderer.RegisterUniqueProperty(keyIndex, keyName, value);
841 void Visual::Base::SetupTransition(
842 Dali::Animation& transition,
843 Internal::TransitionData::Animator& animator,
844 Property::Index index,
845 Property::Value& initialValue,
846 Property::Value& targetValue)
848 if(index != Property::INVALID_INDEX)
852 if(animator.animate == false)
854 mImpl->mRenderer.SetProperty(index, targetValue);
858 if(animator.initialValue.GetType() != Property::NONE)
860 mImpl->mRenderer.SetProperty(index, initialValue);
865 transition = Dali::Animation::New(0.1f);
868 transition.AnimateTo(Property(mImpl->mRenderer, index),
870 animator.alphaFunction,
871 TimePeriod(animator.timePeriodDelay,
872 animator.timePeriodDuration));
878 void Visual::Base::AnimateProperty(
879 Dali::Animation& transition,
880 Internal::TransitionData::Animator& animator)
882 #if defined(DEBUG_ENABLED)
884 std::ostringstream oss;
885 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
886 DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, oss.str().c_str());
890 if(animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
891 animator.propertyKey == MIX_COLOR ||
892 (mImpl->mType == Toolkit::Visual::COLOR &&
893 animator.propertyKey == ColorVisual::Property::MIX_COLOR) ||
894 (mImpl->mType == Toolkit::Visual::PRIMITIVE &&
895 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR))
897 AnimateMixColorProperty(transition, animator);
899 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
900 animator.propertyKey == OPACITY)
902 AnimateOpacityProperty(transition, animator);
904 else if(mImpl->mRenderer)
906 AnimateRendererProperty(transition, animator);
910 void Visual::Base::AnimateOpacityProperty(
911 Dali::Animation& transition,
912 Internal::TransitionData::Animator& animator)
915 if(animator.targetValue.Get(targetOpacity))
917 mImpl->mMixColor.a = targetOpacity;
920 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue);
923 void Visual::Base::AnimateRendererProperty(
924 Dali::Animation& transition,
925 Internal::TransitionData::Animator& animator)
927 Property::Index index = GetPropertyIndex(animator.propertyKey);
928 if(index != Property::INVALID_INDEX)
930 if(animator.targetValue.GetType() != Property::NONE)
932 // Try writing target value into transform property map
933 // if it's not a valid key, then it won't alter mTransform
935 if(animator.propertyKey.type == Property::Key::INDEX)
937 map.Add(animator.propertyKey.indexKey, animator.targetValue);
941 map.Add(animator.propertyKey.stringKey, animator.targetValue);
944 mImpl->mTransform.UpdatePropertyMap(map);
947 SetupTransition(transition, animator, index, animator.initialValue, animator.targetValue);
951 void Visual::Base::AnimateMixColorProperty(
952 Dali::Animation& transition,
953 Internal::TransitionData::Animator& animator)
955 Property::Index index = mImpl->mMixColorIndex;
956 bool animateOpacity = false;
958 Property::Value initialOpacity;
959 Property::Value targetOpacity;
960 Property::Value initialMixColor;
961 Property::Value targetMixColor;
963 if(index != Property::INVALID_INDEX)
965 Vector4 initialColor;
966 if(animator.initialValue.Get(initialColor))
968 if(animator.initialValue.GetType() == Property::VECTOR4)
970 // if there is an initial color specifying alpha, test it
971 initialOpacity = initialColor.a;
973 initialMixColor = Vector3(initialColor);
976 // Set target value into data store
977 if(animator.targetValue.GetType() != Property::NONE)
980 animator.targetValue.Get(mixColor);
981 if(animator.targetValue.GetType() == Property::VECTOR4)
983 mImpl->mMixColor.a = mixColor.a;
984 targetOpacity = mixColor.a;
985 animateOpacity = true;
988 mImpl->mMixColor.r = mixColor.r;
989 mImpl->mMixColor.g = mixColor.g;
990 mImpl->mMixColor.b = mixColor.b;
991 targetMixColor = Vector3(mixColor);
994 SetupTransition(transition, animator, index, initialMixColor, targetMixColor);
997 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity);
1002 Dali::Property Visual::Base::GetPropertyObject(Dali::Property::Key key)
1004 if(!mImpl->mRenderer)
1007 return Dali::Property(handle, Property::INVALID_INDEX);
1010 // Mix color or opacity cases
1011 if(key.type == Property::Key::INDEX)
1013 if(key.indexKey == Toolkit::Visual::Property::MIX_COLOR || (mImpl->mType == Toolkit::Visual::COLOR && key.indexKey == ColorVisual::Property::MIX_COLOR) || (mImpl->mType == Toolkit::Visual::PRIMITIVE && key.indexKey == PrimitiveVisual::Property::MIX_COLOR))
1015 return Dali::Property(mImpl->mRenderer, mImpl->mMixColorIndex);
1017 else if(key.indexKey == Toolkit::Visual::Property::OPACITY)
1019 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1021 else if(key.indexKey == Toolkit::Visual::Transform::Property::OFFSET)
1023 return Dali::Property(mImpl->mRenderer, OFFSET);
1025 else if(key.indexKey == Toolkit::Visual::Transform::Property::SIZE)
1027 return Dali::Property(mImpl->mRenderer, SIZE);
1032 if(key.stringKey == MIX_COLOR)
1034 return Dali::Property(mImpl->mRenderer, mImpl->mMixColorIndex);
1036 else if(key.stringKey == OPACITY)
1038 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1040 else if(key.stringKey == OFFSET)
1042 return Dali::Property(mImpl->mRenderer, OFFSET);
1044 else if(key.stringKey == SIZE)
1046 return Dali::Property(mImpl->mRenderer, SIZE);
1051 Property::Index index = GetPropertyIndex(key);
1052 if(index == Property::INVALID_INDEX)
1054 if(IsTypeAvailableForBorderline(mImpl->mType) &&
1055 ((key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_WIDTH) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_WIDTH) ||
1056 (key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_COLOR) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_COLOR) ||
1057 (key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_OFFSET) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_OFFSET)))
1059 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
1061 // Register borderline properties
1062 mImpl->mBorderlineWidthIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
1063 mImpl->mBorderlineColorIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
1064 mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_OFFSET, BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
1066 // Borderline is animated now. we always have to use borderline feature.
1067 mImpl->mAlwaysUsingBorderline = true;
1069 index = mImpl->mRenderer.GetPropertyIndex(key);
1074 else if(IsTypeAvailableForCornerRadius(mImpl->mType) && ((key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::CORNER_RADIUS) || (key.type == Property::Key::STRING && key.stringKey == CORNER_RADIUS)))
1076 // Register CORNER_RADIUS property
1077 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
1078 mImpl->mRenderer.RegisterUniqueProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
1080 // CornerRadius is animated now. we always have to use corner radius feature.
1081 mImpl->mAlwaysUsingCornerRadius = true;
1083 if(!IsBorderlineRequired())
1085 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
1086 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
1089 index = mImpl->mCornerRadiusIndex;
1096 // We can't find the property in the base class.
1097 // Request to child class
1098 return OnGetPropertyObject(key);
1103 if(index == mImpl->mBorderlineWidthIndex ||
1104 index == mImpl->mBorderlineColorIndex ||
1105 index == mImpl->mBorderlineOffsetIndex)
1107 // Borderline is animated now. we always have to use borderline feature.
1108 mImpl->mAlwaysUsingBorderline = true;
1110 if(index == mImpl->mCornerRadiusIndex)
1112 // CornerRadius is animated now. we always have to use corner radius feature.
1113 mImpl->mAlwaysUsingCornerRadius = true;
1117 return Dali::Property(mImpl->mRenderer, index);
1120 } // namespace Internal
1122 } // namespace Toolkit