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 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
617 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_COLOR, mImpl->mBorderlineColor);
618 map.Insert(Toolkit::DevelVisual::Property::BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
620 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius);
621 map.Insert(Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, static_cast<int>(mImpl->mCornerRadiusPolicy));
624 void Visual::Base::CreateInstancePropertyMap(Property::Map& map) const
626 DoCreateInstancePropertyMap(map);
628 if(mImpl->mCustomShader)
630 mImpl->mCustomShader->CreatePropertyMap(map);
634 void Visual::Base::EnablePreMultipliedAlpha(bool preMultiplied)
638 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
642 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
647 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
648 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultiplied);
652 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
654 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
657 void Visual::Base::DoSetOffScene(Actor& actor)
659 actor.RemoveRenderer(mImpl->mRenderer);
662 bool Visual::Base::IsOnScene() const
664 return mImpl->mFlags & Impl::IS_ON_SCENE;
667 bool Visual::Base::IsRoundedCornerRequired() const
669 // If VisualType doesn't support rounded corner, always return false.
670 if(IsTypeAvailableForCornerRadius(mImpl->mType))
672 if(mImpl->mRenderer && mImpl->mCornerRadiusIndex != Property::INVALID_INDEX)
674 // Update values from Renderer
675 mImpl->mCornerRadius = mImpl->mRenderer.GetProperty<Vector4>(mImpl->mCornerRadiusIndex);
677 return !(mImpl->mCornerRadius == Vector4::ZERO) || mImpl->mAlwaysUsingCornerRadius;
682 bool Visual::Base::IsBorderlineRequired() const
684 // If VisualType doesn't support borderline, always return false.
685 if(IsTypeAvailableForBorderline(mImpl->mType))
687 if(mImpl->mRenderer && mImpl->mBorderlineWidthIndex != Property::INVALID_INDEX)
689 // Update values from Renderer
690 mImpl->mBorderlineWidth = mImpl->mRenderer.GetProperty<float>(mImpl->mBorderlineWidthIndex);
692 return !EqualsZero(mImpl->mBorderlineWidth) || mImpl->mAlwaysUsingBorderline;
697 void Visual::Base::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
699 // May be overriden by derived class
702 void Visual::Base::RegisterMixColor()
706 // All visual renderers now use same mix color / opacity properties.
707 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(mImpl->mMixColor));
708 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, mImpl->mMixColor.a);
710 float preMultipliedAlpha = 0.0f;
711 if(IsPreMultipliedAlphaEnabled())
713 preMultipliedAlpha = 1.0f;
715 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA, preMultipliedAlpha);
719 void Visual::Base::SetMixColor(const Vector4& color)
721 mImpl->mMixColor = color;
725 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, Vector3(color));
726 mImpl->mRenderer.SetProperty(DevelRenderer::Property::OPACITY, color.a);
730 void Visual::Base::SetMixColor(const Vector3& color)
732 mImpl->mMixColor.r = color.r;
733 mImpl->mMixColor.g = color.g;
734 mImpl->mMixColor.b = color.b;
738 mImpl->mRenderer.SetProperty(VisualRenderer::Property::VISUAL_MIX_COLOR, color);
742 void Visual::Base::AddEventObserver(Visual::EventObserver& observer)
744 mImpl->mEventObserver = &observer;
747 void Visual::Base::RemoveEventObserver(Visual::EventObserver& observer)
749 mImpl->mEventObserver = NULL;
752 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
754 if(mImpl->mResourceStatus != resourceStatus)
756 mImpl->mResourceStatus = resourceStatus;
758 if(mImpl->mEventObserver)
760 // observer is currently a control impl
761 mImpl->mEventObserver->ResourceReady(*this);
766 bool Visual::Base::IsResourceReady() const
768 return (mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY);
771 bool Visual::Base::IsSynchronousLoadingRequired() const
773 return (mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING);
776 Toolkit::Visual::Type Visual::Base::GetType() const
781 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
783 return mImpl->mResourceStatus;
786 Visual::FittingMode Visual::Base::GetFittingMode() const
788 return mImpl->mFittingMode;
791 Visual::Base& Visual::Base::GetVisualObject()
796 Renderer Visual::Base::GetRenderer()
798 return mImpl->mRenderer;
801 Property::Index Visual::Base::GetIntKey(Property::Key key)
803 if(key.type == Property::Key::INDEX)
808 if(key.stringKey == ANCHOR_POINT)
810 return Toolkit::Visual::Transform::Property::ANCHOR_POINT;
812 else if(key.stringKey == EXTRA_SIZE)
814 return Toolkit::DevelVisual::Transform::Property::EXTRA_SIZE;
816 else if(key.stringKey == MIX_COLOR)
818 return Toolkit::Visual::Property::MIX_COLOR;
820 else if(key.stringKey == OPACITY)
822 return Toolkit::Visual::Property::OPACITY;
824 else if(key.stringKey == OFFSET)
826 return Toolkit::Visual::Transform::Property::OFFSET;
828 else if(key.stringKey == OFFSET_POLICY)
830 return Toolkit::Visual::Transform::Property::OFFSET_POLICY;
832 else if(key.stringKey == ORIGIN)
834 return Toolkit::Visual::Transform::Property::ORIGIN;
836 else if(key.stringKey == PREMULTIPLIED_ALPHA)
838 return Toolkit::Visual::Property::PREMULTIPLIED_ALPHA;
840 else if(key.stringKey == CUSTOM_SHADER)
842 return Toolkit::Visual::Property::SHADER;
844 else if(key.stringKey == SIZE)
846 return Toolkit::Visual::Transform::Property::SIZE;
848 else if(key.stringKey == SIZE_POLICY)
850 return Toolkit::Visual::Transform::Property::SIZE_POLICY;
852 else if(key.stringKey == TRANSFORM)
854 return Toolkit::Visual::Property::TRANSFORM;
856 else if(key.stringKey == VISUAL_FITTING_MODE)
858 return Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE;
860 else if(key.stringKey == CORNER_RADIUS)
862 return Toolkit::DevelVisual::Property::CORNER_RADIUS;
864 else if(key.stringKey == CORNER_RADIUS_POLICY)
866 return Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY;
868 else if(key.stringKey == BORDERLINE_WIDTH)
870 return Toolkit::DevelVisual::Property::BORDERLINE_WIDTH;
872 else if(key.stringKey == BORDERLINE_COLOR)
874 return Toolkit::DevelVisual::Property::BORDERLINE_COLOR;
876 else if(key.stringKey == BORDERLINE_OFFSET)
878 return Toolkit::DevelVisual::Property::BORDERLINE_OFFSET;
881 return Property::INVALID_INDEX;
884 Property::Index Visual::Base::GetPropertyIndex(Property::Key key)
886 switch(GetIntKey(key))
888 case Dali::Toolkit::Visual::Transform::Property::OFFSET:
890 return VisualRenderer::Property::TRANSFORM_OFFSET;
892 case Dali::Toolkit::Visual::Transform::Property::SIZE:
894 return VisualRenderer::Property::TRANSFORM_SIZE;
896 case Dali::Toolkit::Visual::Transform::Property::ORIGIN:
898 return VisualRenderer::Property::TRANSFORM_ORIGIN;
900 case Dali::Toolkit::Visual::Transform::Property::ANCHOR_POINT:
902 return VisualRenderer::Property::TRANSFORM_ANCHOR_POINT;
904 case Dali::Toolkit::Visual::Property::MIX_COLOR:
906 return VisualRenderer::Property::VISUAL_MIX_COLOR;
908 case Dali::Toolkit::Visual::Property::OPACITY:
910 return DevelRenderer::Property::OPACITY;
912 case Dali::Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
914 return VisualRenderer::Property::VISUAL_PRE_MULTIPLIED_ALPHA;
918 Property::Index index = mImpl->mRenderer.GetPropertyIndex(key);
920 if(index == Property::INVALID_INDEX)
922 // Is it a shader property?
923 Shader shader = mImpl->mRenderer.GetShader();
924 index = shader.GetPropertyIndex(key);
925 if(index != Property::INVALID_INDEX)
927 // Yes - we should register it in the Renderer so it can be set / animated
928 // independently, as shaders are shared across multiple renderers.
930 Property::Index keyIndex(Property::INVALID_KEY);
931 if(key.type == Property::Key::INDEX)
933 keyName = shader.GetPropertyName(index);
934 keyIndex = key.indexKey;
938 keyName = key.stringKey;
939 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
941 Property::Value value = shader.GetProperty(index);
943 // We already know that mRenderer didn't have property. So we can assume that it is unique.
944 index = mImpl->mRenderer.RegisterUniqueProperty(keyIndex, keyName, value);
950 void Visual::Base::SetupTransition(
951 Dali::Animation& transition,
952 Internal::TransitionData::Animator& animator,
953 Property::Index index,
954 Property::Value& initialValue,
955 Property::Value& targetValue)
957 if(index != Property::INVALID_INDEX)
961 if(animator.animate == false)
963 mImpl->mRenderer.SetProperty(index, targetValue);
967 if(animator.initialValue.GetType() != Property::NONE)
969 mImpl->mRenderer.SetProperty(index, initialValue);
974 transition = Dali::Animation::New(0.1f);
977 transition.AnimateTo(Property(mImpl->mRenderer, index),
979 animator.alphaFunction,
980 TimePeriod(animator.timePeriodDelay,
981 animator.timePeriodDuration));
987 void Visual::Base::AnimateProperty(
988 Dali::Animation& transition,
989 Internal::TransitionData::Animator& animator)
991 #if defined(DEBUG_ENABLED)
993 std::ostringstream oss;
994 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
995 DALI_LOG_INFO(gVisualBaseLogFilter, Debug::General, oss.str().c_str());
999 if(animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
1000 animator.propertyKey == MIX_COLOR ||
1001 (mImpl->mType == Toolkit::Visual::COLOR &&
1002 animator.propertyKey == ColorVisual::Property::MIX_COLOR) ||
1003 (mImpl->mType == Toolkit::Visual::PRIMITIVE &&
1004 animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR))
1006 AnimateMixColorProperty(transition, animator);
1008 else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
1009 animator.propertyKey == OPACITY)
1011 AnimateOpacityProperty(transition, animator);
1013 else if(mImpl->mRenderer)
1015 AnimateRendererProperty(transition, animator);
1019 void Visual::Base::AnimateOpacityProperty(
1020 Dali::Animation& transition,
1021 Internal::TransitionData::Animator& animator)
1023 float targetOpacity;
1024 if(animator.targetValue.Get(targetOpacity))
1026 mImpl->mMixColor.a = targetOpacity;
1029 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, animator.initialValue, animator.targetValue);
1032 void Visual::Base::AnimateRendererProperty(
1033 Dali::Animation& transition,
1034 Internal::TransitionData::Animator& animator)
1036 // Get actual renderer index (will convert transform keys into visualproperty indices)
1037 Property::Index index = GetPropertyIndex(animator.propertyKey);
1039 if(index != Property::INVALID_INDEX)
1041 if(animator.targetValue.GetType() != Property::NONE)
1043 // Try writing target value into transform property map
1044 // if it's not a valid key, then it won't alter mTransform
1046 if(animator.propertyKey.type == Property::Key::INDEX)
1048 map.Add(animator.propertyKey.indexKey, animator.targetValue);
1052 map.Add(animator.propertyKey.stringKey, animator.targetValue);
1055 mImpl->mTransform.UpdatePropertyMap(map);
1057 SetupTransition(transition, animator, index, animator.initialValue, animator.targetValue);
1061 void Visual::Base::AnimateMixColorProperty(
1062 Dali::Animation& transition,
1063 Internal::TransitionData::Animator& animator)
1065 bool animateOpacity = false;
1067 Property::Value initialOpacity;
1068 Property::Value targetOpacity;
1069 Property::Value initialMixColor;
1070 Property::Value targetMixColor;
1072 Vector4 initialColor;
1073 if(animator.initialValue.Get(initialColor))
1075 if(animator.initialValue.GetType() == Property::VECTOR4)
1077 // if there is an initial color specifying alpha, test it
1078 initialOpacity = initialColor.a;
1080 initialMixColor = Vector3(initialColor);
1083 // Set target value into data store
1084 if(animator.targetValue.GetType() != Property::NONE)
1087 animator.targetValue.Get(mixColor);
1088 if(animator.targetValue.GetType() == Property::VECTOR4)
1090 mImpl->mMixColor.a = mixColor.a;
1091 targetOpacity = mixColor.a;
1092 animateOpacity = true;
1095 mImpl->mMixColor.r = mixColor.r;
1096 mImpl->mMixColor.g = mixColor.g;
1097 mImpl->mMixColor.b = mixColor.b;
1098 targetMixColor = Vector3(mixColor);
1101 SetupTransition(transition, animator, VisualRenderer::Property::VISUAL_MIX_COLOR, initialMixColor, targetMixColor);
1105 SetupTransition(transition, animator, DevelRenderer::Property::OPACITY, initialOpacity, targetOpacity);
1109 Dali::Property Visual::Base::GetPropertyObject(Dali::Property::Key key)
1111 if(!mImpl->mRenderer)
1114 return Dali::Property(handle, Property::INVALID_INDEX);
1117 // Default animatable properties from VisualRenderer
1118 switch(GetIntKey(key))
1120 case Toolkit::Visual::Property::MIX_COLOR:
1122 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1124 case Toolkit::Visual::Property::OPACITY:
1126 return Dali::Property(mImpl->mRenderer, DevelRenderer::Property::OPACITY);
1128 case Toolkit::Visual::Transform::Property::OFFSET:
1130 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_OFFSET);
1132 case Toolkit::Visual::Transform::Property::SIZE:
1134 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::TRANSFORM_SIZE);
1138 if(key.type == Property::Key::INDEX &&
1139 ((mImpl->mType == Toolkit::Visual::COLOR && key.indexKey == ColorVisual::Property::MIX_COLOR) ||
1140 (mImpl->mType == Toolkit::Visual::PRIMITIVE && key.indexKey == PrimitiveVisual::Property::MIX_COLOR)))
1142 return Dali::Property(mImpl->mRenderer, VisualRenderer::Property::VISUAL_MIX_COLOR);
1148 Property::Index index = GetPropertyIndex(key);
1149 if(index == Property::INVALID_INDEX)
1151 if(IsTypeAvailableForBorderline(mImpl->mType) &&
1152 ((key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_WIDTH) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_WIDTH) ||
1153 (key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_COLOR) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_COLOR) ||
1154 (key.type == Property::Key::INDEX && key.indexKey == DevelVisual::Property::BORDERLINE_OFFSET) || (key.type == Property::Key::STRING && key.stringKey == BORDERLINE_OFFSET)))
1156 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON_WITHOUT_CULL);
1158 // Register borderline properties
1159 mImpl->mBorderlineWidthIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_WIDTH, BORDERLINE_WIDTH, mImpl->mBorderlineWidth);
1160 mImpl->mBorderlineColorIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_COLOR, BORDERLINE_COLOR, mImpl->mBorderlineColor);
1161 mImpl->mBorderlineOffsetIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::BORDERLINE_OFFSET, BORDERLINE_OFFSET, mImpl->mBorderlineOffset);
1163 // Borderline is animated now. we always have to use borderline feature.
1164 mImpl->mAlwaysUsingBorderline = true;
1166 index = mImpl->mRenderer.GetPropertyIndex(key);
1171 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)))
1173 // Register CORNER_RADIUS property
1174 mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelVisual::Property::CORNER_RADIUS, CORNER_RADIUS, mImpl->mCornerRadius);
1175 mImpl->mRenderer.RegisterUniqueProperty(CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy);
1177 // CornerRadius is animated now. we always have to use corner radius feature.
1178 mImpl->mAlwaysUsingCornerRadius = true;
1180 if(!IsBorderlineRequired())
1182 // If IsBorderlineRequired is true, BLEND_MODE is already BlendMode::ON_WITHOUT_CULL. So we don't overwrite it.
1183 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
1186 index = mImpl->mCornerRadiusIndex;
1193 // We can't find the property in the base class.
1194 // Request to child class
1195 return OnGetPropertyObject(key);
1200 if(index == mImpl->mBorderlineWidthIndex ||
1201 index == mImpl->mBorderlineColorIndex ||
1202 index == mImpl->mBorderlineOffsetIndex)
1204 // Borderline is animated now. we always have to use borderline feature.
1205 mImpl->mAlwaysUsingBorderline = true;
1207 if(index == mImpl->mCornerRadiusIndex)
1209 // CornerRadius is animated now. we always have to use corner radius feature.
1210 mImpl->mAlwaysUsingCornerRadius = true;
1214 return Dali::Property(mImpl->mRenderer, index);
1217 } // namespace Internal
1219 } // namespace Toolkit