2 * Copyright (c) 2021 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.RegisterProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
139 mImpl->mRenderer.RegisterProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
141 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
143 if(IsBorderlineRequired())
145 mImpl->mBorderlineWidthIndex = mImpl->mRenderer.RegisterProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
146 mImpl->mBorderlineColorIndex = mImpl->mRenderer.RegisterProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
147 mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterProperty(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. Regist property to renderer.
311 mImpl->mBorderlineWidthIndex = mImpl->mRenderer.RegisterProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
312 mImpl->mBorderlineColorIndex = mImpl->mRenderer.RegisterProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
313 mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterProperty(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.RegisterProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
390 mImpl->mRenderer.RegisterProperty(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.RegisterProperty(
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);
833 index = mImpl->mRenderer.RegisterProperty(keyIndex, keyName, value);
839 void Visual::Base::SetupTransition(
840 Dali::Animation& transition,
841 Internal::TransitionData::Animator& animator,
842 Property::Index index,
843 Property::Value& initialValue,
844 Property::Value& targetValue)
846 if(index != Property::INVALID_INDEX)
850 if(animator.animate == false)
852 mImpl->mRenderer.SetProperty(index, targetValue);
856 if(animator.initialValue.GetType() != Property::NONE)
858 mImpl->mRenderer.SetProperty(index, initialValue);
863 transition = Dali::Animation::New(0.1f);
866 transition.AnimateTo(Property(mImpl->mRenderer, index),
868 animator.alphaFunction,
869 TimePeriod(animator.timePeriodDelay,
870 animator.timePeriodDuration));
876 void Visual::Base::AnimateProperty(
877 Dali::Animation& transition,
878 Internal::TransitionData::Animator& animator)
880 #if defined(DEBUG_ENABLED)
882 std::ostringstream oss;
883 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
884 DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, oss.str().c_str());
888 if(animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
889 animator.propertyKey == MIX_COLOR ||
890 (mImpl->mType == Toolkit::Visual::COLOR &&
891 animator.propertyKey == ColorVisual::Property::MIX_COLOR) ||
892 (mImpl->mType == Toolkit::Visual::PRIMITIVE &&
893 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR))
895 AnimateMixColorProperty(transition, animator);
897 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
898 animator.propertyKey == OPACITY)
900 AnimateOpacityProperty(transition, animator);
902 else if(mImpl->mRenderer)
904 AnimateRendererProperty(transition, animator);
908 void Visual::Base::AnimateOpacityProperty(
909 Dali::Animation& transition,
910 Internal::TransitionData::Animator& animator)
913 if(animator.targetValue.Get(targetOpacity))
915 mImpl->mMixColor.a = targetOpacity;
918 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue);
921 void Visual::Base::AnimateRendererProperty(
922 Dali::Animation& transition,
923 Internal::TransitionData::Animator& animator)
925 Property::Index index = GetPropertyIndex(animator.propertyKey);
926 if(index != Property::INVALID_INDEX)
928 if(animator.targetValue.GetType() != Property::NONE)
930 // Try writing target value into transform property map
931 // if it's not a valid key, then it won't alter mTransform
933 if(animator.propertyKey.type == Property::Key::INDEX)
935 map.Add(animator.propertyKey.indexKey, animator.targetValue);
939 map.Add(animator.propertyKey.stringKey, animator.targetValue);
942 mImpl->mTransform.UpdatePropertyMap(map);
945 SetupTransition(transition, animator, index, animator.initialValue, animator.targetValue);
949 void Visual::Base::AnimateMixColorProperty(
950 Dali::Animation& transition,
951 Internal::TransitionData::Animator& animator)
953 Property::Index index = mImpl->mMixColorIndex;
954 bool animateOpacity = false;
956 Property::Value initialOpacity;
957 Property::Value targetOpacity;
958 Property::Value initialMixColor;
959 Property::Value targetMixColor;
961 if(index != Property::INVALID_INDEX)
963 Vector4 initialColor;
964 if(animator.initialValue.Get(initialColor))
966 if(animator.initialValue.GetType() == Property::VECTOR4)
968 // if there is an initial color specifying alpha, test it
969 initialOpacity = initialColor.a;
971 initialMixColor = Vector3(initialColor);
974 // Set target value into data store
975 if(animator.targetValue.GetType() != Property::NONE)
978 animator.targetValue.Get(mixColor);
979 if(animator.targetValue.GetType() == Property::VECTOR4)
981 mImpl->mMixColor.a = mixColor.a;
982 targetOpacity = mixColor.a;
983 animateOpacity = true;
986 mImpl->mMixColor.r = mixColor.r;
987 mImpl->mMixColor.g = mixColor.g;
988 mImpl->mMixColor.b = mixColor.b;
989 targetMixColor = Vector3(mixColor);
992 SetupTransition(transition, animator, index, initialMixColor, targetMixColor);
995 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity);
1000 Dali::Property Visual::Base::GetPropertyObject(Dali::Property::Key key)
1002 if(!mImpl->mRenderer)
1005 return Dali::Property(handle, Property::INVALID_INDEX);
1008 // Mix color or opacity cases
1009 if(key.type == Property::Key::INDEX)
1011 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))
1013 return Dali::Property(mImpl->mRenderer, mImpl->mMixColorIndex);
1015 else if(key.indexKey == Toolkit::Visual::Property::OPACITY)
1017 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1019 else if(key.indexKey == Toolkit::Visual::Transform::Property::OFFSET)
1021 return Dali::Property(mImpl->mRenderer, OFFSET);
1023 else if(key.indexKey == Toolkit::Visual::Transform::Property::SIZE)
1025 return Dali::Property(mImpl->mRenderer, SIZE);
1030 if(key.stringKey == MIX_COLOR)
1032 return Dali::Property(mImpl->mRenderer, mImpl->mMixColorIndex);
1034 else if(key.stringKey == OPACITY)
1036 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1038 else if(key.stringKey == OFFSET)
1040 return Dali::Property(mImpl->mRenderer, OFFSET);
1042 else if(key.stringKey == SIZE)
1044 return Dali::Property(mImpl->mRenderer, SIZE);
1049 Property::Index index = GetPropertyIndex(key);
1050 if(index == Property::INVALID_INDEX)
1052 if(IsTypeAvailableForBorderline(mImpl->mType) &&
1053 ((key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_WIDTH) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_WIDTH) ||
1054 (key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_COLOR) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_COLOR) ||
1055 (key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_OFFSET) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_OFFSET)))
1057 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
1059 // Register borderline properties
1060 mImpl->mBorderlineWidthIndex = mImpl->mRenderer.RegisterProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
1061 mImpl->mBorderlineColorIndex = mImpl->mRenderer.RegisterProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
1062 mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterProperty(DevelVisual::Property::BORDERLINE_OFFSET, BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
1064 // Borderline is animated now. we always have to use borderline feature.
1065 mImpl->mAlwaysUsingBorderline = true;
1067 index = mImpl->mRenderer.GetPropertyIndex(key);
1072 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)))
1074 // Register CORNER_RADIUS property
1075 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
1076 mImpl->mRenderer.RegisterProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
1078 // ConerRadius is animated now. we always have to use corner radius feature.
1079 mImpl->mAlwaysUsingCornerRadius = true;
1081 if(!IsBorderlineRequired())
1083 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
1084 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
1087 index = mImpl->mCornerRadiusIndex;
1094 // We can't find the property in the base class.
1095 // Request to child class
1096 return OnGetPropertyObject(key);
1100 return Dali::Property(mImpl->mRenderer, index);
1103 } // namespace Internal
1105 } // namespace Toolkit