2 * Copyright (c) 2017 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/public-api/common/dali-common.h>
23 #include <dali/devel-api/object/handle-devel.h>
24 #include <dali/integration-api/debug.h>
27 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
28 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
29 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
33 #if defined(DEBUG_ENABLED)
34 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VISUAL_BASE" );
47 Visual::Base::Base( VisualFactoryCache& factoryCache )
48 : mImpl( new Impl() ),
49 mFactoryCache( factoryCache )
58 void Visual::Base::SetCustomShader( const Property::Map& shaderMap )
60 if( mImpl->mCustomShader )
62 mImpl->mCustomShader->SetPropertyMap( shaderMap );
66 mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
70 void Visual::Base::SetProperties( const Property::Map& propertyMap )
72 for( size_t i = 0; i < propertyMap.Count(); ++i )
74 const KeyValuePair& pair = propertyMap.GetKeyValue( i );
75 const Property::Key& key = pair.first;
76 const Property::Value& value = pair.second;
78 Property::Key matchKey = key;
79 if( matchKey.type == Property::Key::STRING )
81 if( matchKey == CUSTOM_SHADER )
83 matchKey = Property::Key( DevelVisual::Property::SHADER );
85 else if( matchKey == TRANSFORM )
87 matchKey = Property::Key( DevelVisual::Property::TRANSFORM );
89 else if( matchKey == PREMULTIPLIED_ALPHA )
91 matchKey = Property::Key( DevelVisual::Property::PREMULTIPLIED_ALPHA );
93 else if( matchKey == MIX_COLOR )
95 matchKey = Property::Key( DevelVisual::Property::MIX_COLOR );
99 switch( matchKey.indexKey )
101 case DevelVisual::Property::SHADER:
103 Property::Map shaderMap;
104 if( value.Get( shaderMap ) )
106 SetCustomShader( shaderMap );
111 case DevelVisual::Property::TRANSFORM:
114 if( value.Get( map ) )
116 mImpl->mTransform.SetPropertyMap( map );
121 case DevelVisual::Property::PREMULTIPLIED_ALPHA:
123 bool premultipliedAlpha = false;
124 if( value.Get( premultipliedAlpha ) )
126 EnablePreMultipliedAlpha( premultipliedAlpha );
131 case DevelVisual::Property::MIX_COLOR:
134 if( value.Get( mixColor ) )
136 SetMixColor( mixColor );
143 DoSetProperties( propertyMap );
146 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
148 mImpl->mControlSize = controlSize;
149 mImpl->mTransform.SetPropertyMap( transform );
151 #if defined(DEBUG_ENABLED)
152 std::ostringstream oss;
154 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::SetTransformAndSize(%s) - [\e[1;32mtransform: %s controlSize: (%3.1f, %3.1f)]\e[0m\n",
155 GetName().c_str(), oss.str().c_str(), controlSize.x, controlSize.y );
161 void Visual::Base::SetName( const std::string& name )
166 const std::string& Visual::Base::GetName()
171 float Visual::Base::GetHeightForWidth( float width )
173 float aspectCorrectedHeight = 0.f;
175 GetNaturalSize( naturalSize );
176 if( naturalSize.width )
178 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
180 return aspectCorrectedHeight;
183 float Visual::Base::GetWidthForHeight( float height )
185 float aspectCorrectedWidth = 0.f;
187 GetNaturalSize( naturalSize );
188 if( naturalSize.height > 0.0f )
190 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
192 return aspectCorrectedWidth;
195 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
197 naturalSize = Vector2::ZERO;
200 void Visual::Base::SetDepthIndex( float index )
202 mImpl->mDepthIndex = index;
203 if( mImpl->mRenderer )
205 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
209 float Visual::Base::GetDepthIndex() const
211 return mImpl->mDepthIndex;
214 void Visual::Base::SetOnStage( Actor& actor )
218 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
219 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
220 DoSetOnStage( actor );
222 if( mImpl->mRenderer )
226 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
227 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
228 mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
233 void Visual::Base::SetOffStage( Actor& actor )
237 DoSetOffStage( actor );
238 mImpl->mMixColorIndex = Property::INVALID_INDEX;
239 mImpl->mFlags &= ~Impl::IS_ON_STAGE;
243 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
245 DoCreatePropertyMap( map );
247 if( mImpl->mCustomShader )
249 mImpl->mCustomShader->CreatePropertyMap( map );
252 Property::Map transform;
253 mImpl->mTransform.GetPropertyMap( transform );
254 map.Insert( DevelVisual::Property::TRANSFORM, transform );
256 bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
257 map.Insert( DevelVisual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
259 // Note, Color and Primitive will also insert their own mix color into the map
260 // which is ok, because they have a different key value range.
261 map.Insert( DevelVisual::Property::MIX_COLOR, GetMixColor() );
264 void Visual::Base::EnablePreMultipliedAlpha( bool preMultipled )
268 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
272 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
275 if( mImpl->mRenderer )
277 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
281 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
283 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
286 void Visual::Base::DoSetOffStage( Actor& actor )
288 actor.RemoveRenderer( mImpl->mRenderer );
289 mImpl->mRenderer.Reset();
292 bool Visual::Base::IsOnStage() const
294 return mImpl->mFlags & Impl::IS_ON_STAGE;
297 bool Visual::Base::IsFromCache() const
299 return mImpl->mFlags & Impl::IS_FROM_CACHE;
302 void Visual::Base::RegisterMixColor()
304 // Only register if not already registered.
305 // (Color and Primitive visuals will register their own and save to this index)
306 if( mImpl->mMixColorIndex == Property::INVALID_INDEX )
308 mImpl->mMixColorIndex = DevelHandle::RegisterProperty(
310 Toolkit::DevelVisual::Property::MIX_COLOR,
314 if( mImpl->mMixColor.a < 1.f )
316 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
319 float preMultipliedAlpha = 0.0f;
320 if( IsPreMultipliedAlphaEnabled() )
322 preMultipliedAlpha = 1.0f;
324 mImpl->mRenderer.RegisterProperty( "preMultipliedAlpha", preMultipliedAlpha );
328 void Visual::Base::SetMixColor( const Vector4& color )
330 mImpl->mMixColor = color;
332 if( mImpl->mRenderer )
334 mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
337 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
342 const Vector4& Visual::Base::GetMixColor() const
344 return mImpl->mMixColor;
347 Renderer Visual::Base::GetRenderer()
349 return mImpl->mRenderer;
352 void Visual::Base::AnimateProperty( Dali::Animation& transition, Internal::TransitionData::Animator& animator )
354 #if defined(DEBUG_ENABLED)
356 std::ostringstream oss;
357 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
358 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
362 Property::Index index = Property::INVALID_INDEX;
364 // Get the property index
365 bool isMixColor = false;
366 if( animator.propertyKey == Toolkit::DevelVisual::Property::MIX_COLOR ||
367 animator.propertyKey == MIX_COLOR )
370 index = mImpl->mMixColorIndex;
372 else if( mImpl->mRenderer )
374 index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, animator.propertyKey );
377 Vector4 currentMixColor( mImpl->mMixColor );
379 // Set target value into data store
380 if( animator.targetValue.GetType() != Property::NONE )
384 animator.targetValue.Get( mImpl->mMixColor );
388 // Note: there may be several of these calls if more than one transform property is animated.
390 if( animator.propertyKey.type == Property::Key::INDEX )
392 map.Add( animator.propertyKey.indexKey, animator.targetValue );
396 map.Add( animator.propertyKey.stringKey, animator.targetValue );
399 mImpl->mTransform.UpdatePropertyMap( map );
403 if( index != Property::INVALID_INDEX )
405 if( mImpl->mRenderer )
407 if( animator.animate == false )
409 mImpl->mRenderer.SetProperty( index, animator.targetValue );
412 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, mImpl->mMixColor.a < 1.0 ? BlendMode::ON : BlendMode::AUTO );
417 if( animator.initialValue.GetType() != Property::NONE )
421 animator.initialValue.Get( currentMixColor );
424 #if defined(DEBUG_ENABLED)
426 std::ostringstream oss;
427 oss << animator.initialValue;
428 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, " Setting Initial Value - %s\n", oss.str().c_str() );
431 mImpl->mRenderer.SetProperty( index, animator.initialValue );
436 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
437 ( currentMixColor.a < 1.0 || mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
442 transition = Dali::Animation::New( 0.1f );
445 #if defined(DEBUG_ENABLED)
447 std::ostringstream oss;
448 oss << animator.targetValue;
449 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, " Animating to Value - %s\n", oss.str().c_str() );
453 transition.AnimateTo( Property( mImpl->mRenderer, index ),
454 animator.targetValue,
455 animator.alphaFunction,
456 TimePeriod( animator.timePeriodDelay,
457 animator.timePeriodDuration ) );
459 if( isMixColor && currentMixColor.a < 1.0f && mImpl->mMixColor.a >= 1.0f )
461 // When it becomes opaque, set the blend mode back to automatically
462 if( ! mImpl->mBlendSlotDelegate )
464 mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
466 transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate), &Visual::Base::OnMixColorFinished );
473 void Visual::Base::OnMixColorFinished( Animation& animation )
475 if( mImpl->mRenderer )
477 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
478 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
479 ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
481 delete mImpl->mBlendSlotDelegate;
482 mImpl->mBlendSlotDelegate = NULL;
485 } // namespace Internal
487 } // namespace Toolkit