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/visual-renderer.h>
29 #include <dali-toolkit/devel-api/visuals/visual-actions-devel.h>
30 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
31 #include <dali-toolkit/internal/helpers/property-helper.h>
32 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
33 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
34 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
35 #include <dali-toolkit/public-api/visuals/primitive-visual-properties.h>
36 #include <dali-toolkit/public-api/visuals/visual-properties.h>
40 #if defined(DEBUG_ENABLED)
41 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_VISUAL_BASE");
44 // visual string constants contains OFFSET_SIZE_MODE instead
45 const char* const OFFSET_POLICY("offsetPolicy");
46 const char* const SIZE_POLICY("sizePolicy");
58 DALI_ENUM_TO_STRING_TABLE_BEGIN(VISUAL_FITTING_MODE)
59 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_KEEP_ASPECT_RATIO)
60 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FILL)
61 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, OVER_FIT_KEEP_ASPECT_RATIO)
62 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, CENTER)
63 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_WIDTH)
64 DALI_ENUM_TO_STRING_WITH_SCOPE(Visual::FittingMode, FIT_HEIGHT)
65 DALI_ENUM_TO_STRING_TABLE_END(VISUAL_FITTING_MODE)
68 * @brief Check whether this visual type can use corner radius feature or not.
69 * @param type VisualType that want to checkup
70 * @return true if type can use corner radius feature.
72 static bool IsTypeAvailableForCornerRadius(Toolkit::Visual::Type type)
74 switch(static_cast<Toolkit::DevelVisual::Type>(type))
76 case Toolkit::Visual::Type::COLOR:
77 case Toolkit::Visual::Type::GRADIENT:
78 case Toolkit::Visual::Type::IMAGE:
79 case Toolkit::Visual::Type::SVG:
80 case Toolkit::Visual::Type::ANIMATED_IMAGE:
81 case Toolkit::DevelVisual::Type::ANIMATED_VECTOR_IMAGE:
93 * @brief Check whether this visual type can use borderline feature or not.
94 * @param type VisualType that want to checkup
95 * @return true if type can use borderline feature.
97 static bool IsTypeAvailableForBorderline(Toolkit::Visual::Type type)
99 switch(static_cast<Toolkit::DevelVisual::Type>(type))
101 case Toolkit::Visual::Type::COLOR:
102 case Toolkit::Visual::Type::GRADIENT:
103 case Toolkit::Visual::Type::IMAGE:
104 case Toolkit::Visual::Type::SVG:
105 case Toolkit::Visual::Type::ANIMATED_IMAGE:
106 case Toolkit::DevelVisual::Type::ANIMATED_VECTOR_IMAGE:
119 Visual::Base::Base(VisualFactoryCache& factoryCache, FittingMode fittingMode, Toolkit::Visual::Type type)
120 : mImpl(new Impl(fittingMode, type)),
121 mFactoryCache(factoryCache)
125 Visual::Base::~Base()
130 void Visual::Base::Initialize()
132 // The Renderer should be created inside derived class here.
139 if(IsRoundedCornerRequired())
141 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
142 mImpl->mRenderer.RegisterUniqueProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
144 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
146 if(IsBorderlineRequired())
148 mImpl->mBorderlineWidthIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
149 mImpl->mBorderlineColorIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
150 mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_OFFSET, BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
152 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
157 void Visual::Base::SetCustomShader(const Property::Map& shaderMap)
159 if(mImpl->mCustomShader)
161 mImpl->mCustomShader->SetPropertyMap(shaderMap);
165 mImpl->mCustomShader = new Impl::CustomShader(shaderMap);
168 // Let derived class know
172 void Visual::Base::SetProperties(const Property::Map& propertyMap)
174 bool needUpdateShader = false;
175 for(size_t i = 0; i < propertyMap.Count(); ++i)
177 const KeyValuePair& pair = propertyMap.GetKeyValue(i);
178 const Property::Key& key = pair.first;
179 const Property::Value& value = pair.second;
181 Property::Key matchKey = key;
182 if(matchKey.type == Property::Key::STRING)
184 if(matchKey == CUSTOM_SHADER)
186 matchKey = Property::Key(Toolkit::Visual::Property::SHADER);
188 else if(matchKey == TRANSFORM)
190 matchKey = Property::Key(Toolkit::Visual::Property::TRANSFORM);
192 else if(matchKey == PREMULTIPLIED_ALPHA)
194 matchKey = Property::Key(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA);
196 else if(matchKey == MIX_COLOR)
198 matchKey = Property::Key(Toolkit::Visual::Property::MIX_COLOR);
200 else if(matchKey == OPACITY)
202 matchKey = Property::Key(Toolkit::Visual::Property::OPACITY);
204 else if(matchKey == VISUAL_FITTING_MODE)
206 matchKey = Property::Key(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE);
208 else if(matchKey == BORDERLINE_WIDTH)
210 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH);
212 else if(matchKey == BORDERLINE_COLOR)
214 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_COLOR);
216 else if(matchKey == BORDERLINE_OFFSET)
218 matchKey = Property::Key(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET);
220 else if(matchKey == CORNER_RADIUS)
222 matchKey = Property::Key(Toolkit::DevelVisual::Property::CORNER_RADIUS);
224 else if(matchKey == CORNER_RADIUS_POLICY)
226 matchKey = Property::Key(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY);
230 switch(matchKey.indexKey)
232 case Toolkit::Visual::Property::SHADER:
234 Property::Map shaderMap;
235 if(value.Get(shaderMap))
237 SetCustomShader(shaderMap);
242 case Toolkit::Visual::Property::TRANSFORM:
247 mImpl->mTransform.SetPropertyMap(map);
252 case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
254 bool premultipliedAlpha = false;
255 if(value.Get(premultipliedAlpha))
257 EnablePreMultipliedAlpha(premultipliedAlpha);
262 case Toolkit::Visual::Property::MIX_COLOR:
265 if(value.Get(mixColor))
267 if(value.GetType() == Property::VECTOR4)
269 SetMixColor(mixColor);
273 Vector3 mixColor3(mixColor);
274 SetMixColor(mixColor3);
279 case Toolkit::Visual::Property::OPACITY:
282 if(value.Get(opacity))
284 mImpl->mMixColor.a = opacity;
285 SetMixColor(mImpl->mMixColor);
289 case Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE:
291 Scripting::GetEnumerationProperty<Visual::FittingMode>(
292 value, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT, mImpl->mFittingMode);
295 case Toolkit::DevelVisual::Property::BORDERLINE_WIDTH:
300 mImpl->mBorderlineWidth = width;
303 if(mImpl->mBorderlineWidthIndex != Property::INVALID_INDEX)
305 mImpl->mRenderer.SetProperty(mImpl->mBorderlineWidthIndex, mImpl->mBorderlineWidth);
307 else if(DALI_UNLIKELY(mImpl->mRenderer && IsBorderlineRequired()))
309 // Unusual case. SetProperty called after OnInitialize().
310 // Assume that DoAction call UPDATE_PROPERTY.
311 // We must regist properies into renderer, and update shader.
313 // Borderline added by this action. Register property to renderer.
314 mImpl->mBorderlineWidthIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
315 mImpl->mBorderlineColorIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
316 mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_OFFSET, BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
318 // Make Blend mode ON_WITHOUT_CULL for transparent mix color.
319 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
321 // Change the shader must not be occured many times. we always have to use borderline feature.
322 mImpl->mAlwaysUsingBorderline = true;
325 needUpdateShader = true;
329 case Toolkit::DevelVisual::Property::BORDERLINE_COLOR:
334 mImpl->mBorderlineColor = color;
337 if(mImpl->mBorderlineColorIndex != Property::INVALID_INDEX)
339 mImpl->mRenderer.SetProperty(mImpl->mBorderlineColorIndex, mImpl->mBorderlineColor);
343 case Toolkit::DevelVisual::Property::BORDERLINE_OFFSET:
346 if(value.Get(offset))
348 mImpl->mBorderlineOffset = offset;
351 if(mImpl->mBorderlineOffsetIndex != Property::INVALID_INDEX)
353 mImpl->mRenderer.SetProperty(mImpl->mBorderlineOffsetIndex, mImpl->mBorderlineOffset);
357 case Toolkit::DevelVisual::Property::CORNER_RADIUS:
359 if(value.GetType() == Property::VECTOR4)
361 // If CORNER_RADIUS Property is Vector4,
362 // Each values mean the radius of
363 // (top-left, top-right, bottom-right, bottom-left)
365 if(value.Get(radius))
367 mImpl->mCornerRadius = radius;
372 // If CORNER_RADIUS Property is float,
373 // Every corner radius have same value
375 if(value.Get(radius))
377 mImpl->mCornerRadius = Vector4(radius, radius, radius, radius);
381 if(mImpl->mCornerRadiusIndex != Property::INVALID_INDEX)
383 mImpl->mRenderer.SetProperty(mImpl->mCornerRadiusIndex, mImpl->mCornerRadius);
385 else if(DALI_UNLIKELY(mImpl->mRenderer && IsRoundedCornerRequired()))
387 // Unusual case. SetProperty called after OnInitialize().
388 // Assume that DoAction call UPDATE_PROPERTY.
389 // We must regist properies into renderer, and update shader.
391 // CornerRadius added by this action. Regist property to renderer.
392 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
393 mImpl->mRenderer.RegisterUniqueProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
395 // Change the shader must not be occured many times. we always have to use corner radius feature.
396 mImpl->mAlwaysUsingCornerRadius = true;
398 if(!IsBorderlineRequired())
400 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
401 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
405 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 && mImpl->mCornerRadiusIndex != Property::INVALID_INDEX))
422 // Unusual case. SetProperty called after OnInitialize().
423 // Assume that DoAction call UPDATE_PROPERTY.
424 // We must update properies result into renderer
425 // Note : mImpl->mCornerRadiusIndex is not INVALID_INDEX.
426 // So CornerRadiusPolicy property is already registed.
427 mImpl->mRenderer.SetProperty(mImpl->mRenderer.GetPropertyIndex(CORNER_RADIUS_POLICY), mImpl->mCornerRadiusPolicy);
433 DALI_LOG_ERROR("Unsupported policy: %d\n", policy);
443 DoSetProperties(propertyMap);
445 if(DALI_UNLIKELY(needUpdateShader))
451 void Visual::Base::SetTransformAndSize(const Property::Map& transform, Size controlSize)
453 mImpl->mControlSize = controlSize;
454 mImpl->mTransform.UpdatePropertyMap(transform);
456 #if defined(DEBUG_ENABLED)
457 std::ostringstream oss;
459 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);
465 void Visual::Base::SetName(const std::string& name)
470 const std::string& Visual::Base::GetName() const
475 float Visual::Base::GetHeightForWidth(float width)
477 float aspectCorrectedHeight = 0.f;
479 GetNaturalSize(naturalSize);
480 if(naturalSize.width)
482 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
484 return aspectCorrectedHeight;
487 float Visual::Base::GetWidthForHeight(float height)
489 float aspectCorrectedWidth = 0.f;
491 GetNaturalSize(naturalSize);
492 if(naturalSize.height > 0.0f)
494 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
496 return aspectCorrectedWidth;
499 void Visual::Base::GetNaturalSize(Vector2& naturalSize)
501 naturalSize = Vector2::ZERO;
504 void Visual::Base::DoAction(const Property::Index actionId, const Property::Value attributes)
506 OnDoAction(actionId, attributes);
508 // Check if action is valid for this visual type and perform action if possible
511 case DevelVisual::Action::UPDATE_PROPERTY:
513 const Property::Map* map = attributes.GetMap();
523 void Visual::Base::SetDepthIndex(int index)
525 mImpl->mDepthIndex = index;
528 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
532 int Visual::Base::GetDepthIndex() const
534 return mImpl->mDepthIndex;
537 void Visual::Base::SetOnScene(Actor& actor)
541 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
542 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
547 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
548 mImpl->mRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex);
551 mImpl->mFlags |= Impl::IS_ON_SCENE;
555 void Visual::Base::SetOffScene(Actor& actor)
559 DoSetOffScene(actor);
560 mImpl->mFlags &= ~Impl::IS_ON_SCENE;
564 void Visual::Base::CreatePropertyMap(Property::Map& map) const
568 // Update values from Renderer
569 mImpl->mMixColor = mImpl->mRenderer.GetProperty<Vector3>(VisualRenderer::Property::VISUAL_MIX_COLOR);
570 mImpl->mMixColor.a = mImpl->mRenderer.GetProperty<float>(DevelRenderer::Property::OPACITY);
572 mImpl->mTransform.mOffset = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_OFFSET);
573 mImpl->mTransform.mSize = mImpl->mRenderer.GetProperty<Vector2>(VisualRenderer::Property::TRANSFORM_SIZE);
575 if(mImpl->mCornerRadiusIndex != Property::INVALID_INDEX)
577 mImpl->mCornerRadius = mImpl->mRenderer.GetProperty<Vector4>(mImpl->mCornerRadiusIndex);
579 if(mImpl->mBorderlineWidthIndex != Property::INVALID_INDEX)
581 mImpl->mBorderlineWidth = mImpl->mRenderer.GetProperty<float>(mImpl->mBorderlineWidthIndex);
583 if(mImpl->mBorderlineColorIndex != Property::INVALID_INDEX)
585 mImpl->mBorderlineColor = mImpl->mRenderer.GetProperty<Vector4>(mImpl->mBorderlineColorIndex);
587 if(mImpl->mBorderlineOffsetIndex != Property::INVALID_INDEX)
589 mImpl->mBorderlineOffset = mImpl->mRenderer.GetProperty<float>(mImpl->mBorderlineOffsetIndex);
593 DoCreatePropertyMap(map);
595 if(mImpl->mCustomShader)
597 mImpl->mCustomShader->CreatePropertyMap(map);
600 Property::Map transform;
601 mImpl->mTransform.GetPropertyMap(transform);
602 map.Insert(Toolkit::Visual::Property::TRANSFORM, transform);
604 bool premultipliedAlpha(IsPreMultipliedAlphaEnabled());
605 map.Insert(Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha);
607 // Note, Color and Primitive will also insert their own mix color into the map
608 // which is ok, because they have a different key value range, but uses same cached value anyway.
609 map.Insert(Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor); // vec4
610 map.Insert(Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a);
612 auto fittingModeString = Scripting::GetLinearEnumerationName<FittingMode>(
613 mImpl->mFittingMode, VISUAL_FITTING_MODE_TABLE, VISUAL_FITTING_MODE_TABLE_COUNT);
614 map.Insert(Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString);
616 if(IsTypeAvailableForBorderline(mImpl->mType))
618 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
619 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
620 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
623 if(IsTypeAvailableForCornerRadius(mImpl->mType))
625 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius);
626 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, static_cast<int>(mImpl->mCornerRadiusPolicy));
630 void Visual::Base::CreateInstancePropertyMap(Property::Map& map) const
632 DoCreateInstancePropertyMap(map);
634 if(mImpl->mCustomShader)
636 mImpl->mCustomShader->CreatePropertyMap(map);
640 void Visual::Base::EnablePreMultipliedAlpha(bool preMultiplied)
644 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
648 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
653 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
654 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultiplied);
658 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
660 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
663 void Visual::Base::DoSetOffScene(Actor& actor)
665 actor.RemoveRenderer(mImpl->mRenderer);
668 bool Visual::Base::IsOnScene() const
670 return mImpl->mFlags & Impl::IS_ON_SCENE;
673 bool Visual::Base::IsRoundedCornerRequired() const
675 // If VisualType doesn't support rounded corner, always return false.
676 if(IsTypeAvailableForCornerRadius(mImpl->mType))
678 if(mImpl->mRenderer && mImpl->mCornerRadiusIndex != Property::INVALID_INDEX)
680 // Update values from Renderer
681 mImpl->mCornerRadius = mImpl->mRenderer.GetProperty<Vector4>(mImpl->mCornerRadiusIndex);
683 return !(mImpl->mCornerRadius == Vector4::ZERO) || mImpl->mAlwaysUsingCornerRadius;
688 bool Visual::Base::IsBorderlineRequired() const
690 // If VisualType doesn't support borderline, always return false.
691 if(IsTypeAvailableForBorderline(mImpl->mType))
693 if(mImpl->mRenderer && mImpl->mBorderlineWidthIndex != Property::INVALID_INDEX)
695 // Update values from Renderer
696 mImpl->mBorderlineWidth = mImpl->mRenderer.GetProperty<float>(mImpl->mBorderlineWidthIndex);
698 return !EqualsZero(mImpl->mBorderlineWidth) || mImpl->mAlwaysUsingBorderline;
703 void Visual::Base::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
705 // May be overriden by derived class
708 void Visual::Base::RegisterMixColor()
712 // All visual renderers now use same mix color / opacity properties.
713 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
714 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, mImpl->mMixColor.a);
716 float preMultipliedAlpha = 0.0f;
717 if(IsPreMultipliedAlphaEnabled())
719 preMultipliedAlpha = 1.0f;
721 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultipliedAlpha);
725 void Visual::Base::SetMixColor(const Vector4& color)
727 mImpl->mMixColor = color;
731 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, 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(VisualRenderer::Property::VISUAL_MIX_COLOR, 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 ||
775 mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::FAILED);
778 bool Visual::Base::IsSynchronousLoadingRequired() const
780 return (mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING);
783 Toolkit::Visual::Type Visual::Base::GetType() const
788 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
790 return mImpl->mResourceStatus;
793 Visual::FittingMode Visual::Base::GetFittingMode() const
795 return mImpl->mFittingMode;
798 Visual::Base& Visual::Base::GetVisualObject()
803 Renderer Visual::Base::GetRenderer()
805 return mImpl->mRenderer;
808 Property::Index Visual::Base::GetIntKey(Property::Key key)
810 if(key.type == Property::Key::INDEX)
815 if(key.stringKey == ANCHOR_POINT)
817 return Toolkit::Visual::Transform::Property::ANCHOR_POINT;
819 else if(key.stringKey == EXTRA_SIZE)
821 return Toolkit::DevelVisual::Transform::Property::EXTRA_SIZE;
823 else if(key.stringKey == MIX_COLOR)
825 return Toolkit::Visual::Property::MIX_COLOR;
827 else if(key.stringKey == OPACITY)
829 return Toolkit::Visual::Property::OPACITY;
831 else if(key.stringKey == OFFSET)
833 return Toolkit::Visual::Transform::Property::OFFSET;
835 else if(key.stringKey == OFFSET_POLICY)
837 return Toolkit::Visual::Transform::Property::OFFSET_POLICY;
839 else if(key.stringKey == ORIGIN)
841 return Toolkit::Visual::Transform::Property::ORIGIN;
843 else if(key.stringKey == PREMULTIPLIED_ALPHA)
845 return Toolkit::Visual::Property::PREMULTIPLIED_ALPHA;
847 else if(key.stringKey == CUSTOM_SHADER)
849 return Toolkit::Visual::Property::SHADER;
851 else if(key.stringKey == SIZE)
853 return Toolkit::Visual::Transform::Property::SIZE;
855 else if(key.stringKey == SIZE_POLICY)
857 return Toolkit::Visual::Transform::Property::SIZE_POLICY;
859 else if(key.stringKey == TRANSFORM)
861 return Toolkit::Visual::Property::TRANSFORM;
863 else if(key.stringKey == VISUAL_FITTING_MODE)
865 return Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE;
867 else if(key.stringKey == CORNER_RADIUS)
869 return Toolkit::DevelVisual::Property::CORNER_RADIUS;
871 else if(key.stringKey == CORNER_RADIUS_POLICY)
873 return Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY;
875 else if(key.stringKey == BORDERLINE_WIDTH)
877 return Toolkit::DevelVisual::Property::BORDERLINE_WIDTH;
879 else if(key.stringKey == BORDERLINE_COLOR)
881 return Toolkit::DevelVisual::Property::BORDERLINE_COLOR;
883 else if(key.stringKey == BORDERLINE_OFFSET)
885 return Toolkit::DevelVisual::Property::BORDERLINE_OFFSET;
888 return Property::INVALID_INDEX;
891 Property::Index Visual::Base::GetPropertyIndex(Property::Key key)
893 switch(GetIntKey(key))
895 case Dali::Toolkit::Visual::Transform::Property::OFFSET:
897 return VisualRenderer::Property::TRANSFORM_OFFSET;
899 case Dali::Toolkit::Visual::Transform::Property::SIZE:
901 return VisualRenderer::Property::TRANSFORM_SIZE;
903 case Dali::Toolkit::Visual::Transform::Property::ORIGIN:
905 return VisualRenderer::Property::TRANSFORM_ORIGIN;
907 case Dali::Toolkit::Visual::Transform::Property::ANCHOR_POINT:
909 return VisualRenderer::Property::TRANSFORM_ANCHOR_POINT;
911 case Dali::Toolkit::Visual::Property::MIX_COLOR:
913 return VisualRenderer::Property::VISUAL_MIX_COLOR;
915 case Dali::Toolkit::Visual::Property::OPACITY:
917 return DevelRenderer::Property::OPACITY;
919 case Dali::Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
921 return VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA;
925 Property::Index index = mImpl->mRenderer.GetPropertyIndex(key);
927 if(index == Property::INVALID_INDEX)
929 // Is it a shader property?
930 Shader shader = mImpl->mRenderer.GetShader();
931 index = shader.GetPropertyIndex(key);
932 if(index != Property::INVALID_INDEX)
934 // Yes - we should register it in the Renderer so it can be set / animated
935 // independently, as shaders are shared across multiple renderers.
937 Property::Index keyIndex(Property::INVALID_KEY);
938 if(key.type == Property::Key::INDEX)
940 keyName = shader.GetPropertyName(index);
941 keyIndex = key.indexKey;
945 keyName = key.stringKey;
946 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
948 Property::Value value = shader.GetProperty(index);
950 // We already know that mRenderer didn't have property. So we can assume that it is unique.
951 index = mImpl->mRenderer.RegisterUniqueProperty(keyIndex, keyName, value);
957 void Visual::Base::SetupTransition(
958 Dali::Animation& transition,
959 Internal::TransitionData::Animator& animator,
960 Property::Index index,
961 Property::Value& initialValue,
962 Property::Value& targetValue)
964 if(index != Property::INVALID_INDEX)
968 if(animator.animate == false)
970 mImpl->mRenderer.SetProperty(index, targetValue);
974 if(animator.initialValue.GetType() != Property::NONE)
976 mImpl->mRenderer.SetProperty(index, initialValue);
981 transition = Dali::Animation::New(0.1f);
984 transition.AnimateTo(Property(mImpl->mRenderer, index),
986 animator.alphaFunction,
987 TimePeriod(animator.timePeriodDelay,
988 animator.timePeriodDuration));
994 void Visual::Base::AnimateProperty(
995 Dali::Animation& transition,
996 Internal::TransitionData::Animator& animator)
998 #if defined(DEBUG_ENABLED)
1000 std::ostringstream oss;
1001 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
1002 DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, oss.str().c_str());
1006 if(animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
1007 animator.propertyKey == MIX_COLOR ||
1008 (mImpl->mType == Toolkit::Visual::COLOR &&
1009 animator.propertyKey == ColorVisual::Property::MIX_COLOR) ||
1010 (mImpl->mType == Toolkit::Visual::PRIMITIVE &&
1011 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR))
1013 AnimateMixColorProperty(transition, animator);
1015 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
1016 animator.propertyKey == OPACITY)
1018 AnimateOpacityProperty(transition, animator);
1020 else if(mImpl->mRenderer)
1022 AnimateRendererProperty(transition, animator);
1026 void Visual::Base::AnimateOpacityProperty(
1027 Dali::Animation& transition,
1028 Internal::TransitionData::Animator& animator)
1030 float targetOpacity;
1031 if(animator.targetValue.Get(targetOpacity))
1033 mImpl->mMixColor.a = targetOpacity;
1036 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue);
1039 void Visual::Base::AnimateRendererProperty(
1040 Dali::Animation& transition,
1041 Internal::TransitionData::Animator& animator)
1043 // Get actual renderer index (will convert transform keys into visualproperty indices)
1044 Property::Index index = GetPropertyIndex(animator.propertyKey);
1046 if(index != Property::INVALID_INDEX)
1048 if(animator.targetValue.GetType() != Property::NONE)
1050 // Try writing target value into transform property map
1051 // if it's not a valid key, then it won't alter mTransform
1053 if(animator.propertyKey.type == Property::Key::INDEX)
1055 map.Add(animator.propertyKey.indexKey, animator.targetValue);
1059 map.Add(animator.propertyKey.stringKey, animator.targetValue);
1062 mImpl->mTransform.UpdatePropertyMap(map);
1064 SetupTransition(transition, animator, index, animator.initialValue, animator.targetValue);
1068 void Visual::Base::AnimateMixColorProperty(
1069 Dali::Animation& transition,
1070 Internal::TransitionData::Animator& animator)
1072 bool animateOpacity = false;
1074 Property::Value initialOpacity;
1075 Property::Value targetOpacity;
1076 Property::Value initialMixColor;
1077 Property::Value targetMixColor;
1079 Vector4 initialColor;
1080 if(animator.initialValue.Get(initialColor))
1082 if(animator.initialValue.GetType() == Property::VECTOR4)
1084 // if there is an initial color specifying alpha, test it
1085 initialOpacity = initialColor.a;
1087 initialMixColor = Vector3(initialColor);
1090 // Set target value into data store
1091 if(animator.targetValue.GetType() != Property::NONE)
1094 animator.targetValue.Get(mixColor);
1095 if(animator.targetValue.GetType() == Property::VECTOR4)
1097 mImpl->mMixColor.a = mixColor.a;
1098 targetOpacity = mixColor.a;
1099 animateOpacity = true;
1102 mImpl->mMixColor.r = mixColor.r;
1103 mImpl->mMixColor.g = mixColor.g;
1104 mImpl->mMixColor.b = mixColor.b;
1105 targetMixColor = Vector3(mixColor);
1108 SetupTransition(transition, animator, VisualRenderer::Property::VISUAL_MIX_COLOR, initialMixColor, targetMixColor);
1112 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity);
1116 Dali::Property Visual::Base::GetPropertyObject(Dali::Property::Key key)
1118 if(!mImpl->mRenderer)
1121 return Dali::Property(handle, Property::INVALID_INDEX);
1124 // Default animatable properties from VisualRenderer
1125 switch(GetIntKey(key))
1127 case Toolkit::Visual::Property::MIX_COLOR:
1129 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1131 case Toolkit::Visual::Property::OPACITY:
1133 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1135 case Toolkit::Visual::Transform::Property::OFFSET:
1137 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_OFFSET);
1139 case Toolkit::Visual::Transform::Property::SIZE:
1141 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_SIZE);
1145 if(key.type == Property::Key::INDEX &&
1146 ((mImpl->mType == Toolkit::Visual::COLOR && key.indexKey == ColorVisual::Property::MIX_COLOR) ||
1147 (mImpl->mType == Toolkit::Visual::PRIMITIVE && key.indexKey == PrimitiveVisual::Property::MIX_COLOR)))
1149 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1155 Property::Index index = GetPropertyIndex(key);
1156 if(index == Property::INVALID_INDEX)
1158 if(IsTypeAvailableForBorderline(mImpl->mType) &&
1159 ((key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_WIDTH) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_WIDTH) ||
1160 (key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_COLOR) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_COLOR) ||
1161 (key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_OFFSET) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_OFFSET)))
1163 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
1165 // Register borderline properties
1166 mImpl->mBorderlineWidthIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
1167 mImpl->mBorderlineColorIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
1168 mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_OFFSET, BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
1170 // Borderline is animated now. we always have to use borderline feature.
1171 mImpl->mAlwaysUsingBorderline = true;
1173 index = mImpl->mRenderer.GetPropertyIndex(key);
1178 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)))
1180 // Register CORNER_RADIUS property
1181 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
1182 mImpl->mRenderer.RegisterUniqueProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
1184 // CornerRadius is animated now. we always have to use corner radius feature.
1185 mImpl->mAlwaysUsingCornerRadius = true;
1187 if(!IsBorderlineRequired())
1189 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
1190 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
1193 index = mImpl->mCornerRadiusIndex;
1200 // We can't find the property in the base class.
1201 // Request to child class
1202 return OnGetPropertyObject(key);
1207 if(index == mImpl->mBorderlineWidthIndex ||
1208 index == mImpl->mBorderlineColorIndex ||
1209 index == mImpl->mBorderlineOffsetIndex)
1211 // Borderline is animated now. we always have to use borderline feature.
1212 mImpl->mAlwaysUsingBorderline = true;
1214 if(index == mImpl->mCornerRadiusIndex)
1216 // CornerRadius is animated now. we always have to use corner radius feature.
1217 mImpl->mAlwaysUsingCornerRadius = true;
1221 return Dali::Property(mImpl->mRenderer, index);
1224 } // namespace Internal
1226 } // namespace Toolkit