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.UpdatePropertyMap( 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;
353 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
355 Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
357 if( index == Property::INVALID_INDEX )
359 // Is it a shader property?
360 Shader shader = mImpl->mRenderer.GetShader();
361 index = DevelHandle::GetPropertyIndex( shader, key );
362 if( index != Property::INVALID_INDEX )
364 // Yes - we should register it in the Renderer so it can be set / animated
365 // independently, as shaders are shared across multiple renderers.
367 Property::Index keyIndex( Property::INVALID_KEY );
368 if( key.type == Property::Key::INDEX )
370 keyName = shader.GetPropertyName( index );
371 keyIndex = key.indexKey;
375 keyName = key.stringKey;
376 // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
378 Property::Value value = shader.GetProperty( index );
379 index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
385 void Visual::Base::SetupTransition(
386 Dali::Animation& transition,
387 Internal::TransitionData::Animator& animator,
388 Property::Index index )
390 if( index != Property::INVALID_INDEX )
392 if( mImpl->mRenderer )
394 if( animator.animate == false )
396 mImpl->mRenderer.SetProperty( index, animator.targetValue );
400 if( animator.initialValue.GetType() != Property::NONE )
402 mImpl->mRenderer.SetProperty( index, animator.initialValue );
407 transition = Dali::Animation::New( 0.1f );
410 transition.AnimateTo( Property( mImpl->mRenderer, index ),
411 animator.targetValue,
412 animator.alphaFunction,
413 TimePeriod( animator.timePeriodDelay,
414 animator.timePeriodDuration ) );
420 void Visual::Base::AnimateProperty(
421 Dali::Animation& transition,
422 Internal::TransitionData::Animator& animator )
424 #if defined(DEBUG_ENABLED)
426 std::ostringstream oss;
427 oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
428 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
432 Property::Index index = Property::INVALID_INDEX;
434 bool isMixColor = false;
435 bool isMixColorOpaque = true;
437 // Get the property index
438 if( animator.propertyKey == Toolkit::DevelVisual::Property::MIX_COLOR ||
439 animator.propertyKey == MIX_COLOR )
442 index = mImpl->mMixColorIndex;
444 Vector4 initialColor;
445 if( animator.initialValue.Get(initialColor) ) // if there is an initial color, test it
447 isMixColorOpaque = initialColor.a >= 1.0f;
451 isMixColorOpaque = mImpl->mMixColor.a >= 1.0f; // otherwise, test the current color
454 else if( mImpl->mRenderer )
456 index = GetPropertyIndex( animator.propertyKey );
459 // Set target value into data store
460 if( animator.targetValue.GetType() != Property::NONE )
464 animator.targetValue.Get( mImpl->mMixColor );
468 // Note: there may be several of these calls if more than one
469 // transform property is animated.
471 if( animator.propertyKey.type == Property::Key::INDEX )
473 map.Add( animator.propertyKey.indexKey, animator.targetValue );
477 map.Add( animator.propertyKey.stringKey, animator.targetValue );
480 mImpl->mTransform.UpdatePropertyMap( map );
484 if( index != Property::INVALID_INDEX )
486 SetupTransition( transition, animator, index );
488 // For mix color, ensure the blend mode is on if the initial or final values are not opaque,
489 // and that it is turned off after the animation ends if the final value is opaque
490 if( isMixColor && (!isMixColorOpaque || mImpl->mMixColor.a < 1.0f) )
492 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
494 if( animator.animate == true && mImpl->mMixColor.a >= 1.0f )
496 // When it becomes opaque, set the blend mode back to automatically
497 if( ! mImpl->mBlendSlotDelegate )
499 mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
501 transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
502 &Visual::Base::OnMixColorFinished );
508 void Visual::Base::OnMixColorFinished( Animation& animation )
510 if( mImpl->mRenderer )
512 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
513 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
514 ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
516 delete mImpl->mBlendSlotDelegate;
517 mImpl->mBlendSlotDelegate = NULL;
520 } // namespace Internal
522 } // namespace Toolkit