2 * Copyright (c) 2016 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/integration-api/debug.h>
26 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
27 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
28 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
32 #if defined(DEBUG_ENABLED)
33 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VISUAL_BASE" );
46 Visual::Base::Base( VisualFactoryCache& factoryCache )
47 : mImpl( new Impl() ),
48 mFactoryCache( factoryCache )
57 void Visual::Base::SetCustomShader( const Property::Map& shaderMap )
59 if( mImpl->mCustomShader )
61 mImpl->mCustomShader->SetPropertyMap( shaderMap );
65 mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
69 void Visual::Base::SetProperties( const Property::Map& propertyMap )
71 for( size_t i = 0; i < propertyMap.Count(); ++i )
73 const KeyValuePair& pair = propertyMap.GetKeyValue( i );
74 const Property::Key& key = pair.first;
75 const Property::Value& value = pair.second;
76 switch( key.indexKey )
78 case DevelVisual::Property::SHADER:
80 Property::Map shaderMap;
81 if( value.Get( shaderMap ) )
83 SetCustomShader( shaderMap );
88 case DevelVisual::Property::TRANSFORM:
91 if( value.Get( map ) )
93 mImpl->mTransform.SetPropertyMap( map );
98 case DevelVisual::Property::PREMULTIPLIED_ALPHA:
100 bool premultipliedAlpha = false;
101 if( value.Get( premultipliedAlpha ) )
103 EnablePreMultipliedAlpha( premultipliedAlpha );
110 DoSetProperties( propertyMap );
113 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
115 mImpl->mControlSize = controlSize;
116 mImpl->mTransform.SetPropertyMap( transform );
118 #if defined(DEBUG_ENABLED)
119 std::ostringstream oss;
121 DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::SetTransformAndSize(%s) - [\e[1;32mtransform: %s controlSize: (%3.1f, %3.1f)]\e[0m\n",
122 GetName().c_str(), oss.str().c_str(), controlSize.x, controlSize.y );
128 void Visual::Base::SetName( const std::string& name )
133 const std::string& Visual::Base::GetName()
138 float Visual::Base::GetHeightForWidth( float width )
140 float aspectCorrectedHeight = 0.f;
142 GetNaturalSize( naturalSize );
143 if( naturalSize.width )
145 aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
147 return aspectCorrectedHeight;
150 float Visual::Base::GetWidthForHeight( float height )
152 float aspectCorrectedWidth = 0.f;
154 GetNaturalSize( naturalSize );
155 if( naturalSize.height > 0.0f )
157 aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
159 return aspectCorrectedWidth;
162 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
164 naturalSize = Vector2::ZERO;
167 void Visual::Base::SetDepthIndex( float index )
169 mImpl->mDepthIndex = index;
170 if( mImpl->mRenderer )
172 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
176 float Visual::Base::GetDepthIndex() const
178 return mImpl->mDepthIndex;
181 void Visual::Base::SetOnStage( Actor& actor )
185 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
186 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
187 DoSetOnStage( actor );
189 if( mImpl->mRenderer )
191 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
192 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
193 mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
198 void Visual::Base::SetOffStage( Actor& actor )
202 DoSetOffStage( actor );
204 mImpl->mFlags &= ~Impl::IS_ON_STAGE;
208 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
210 DoCreatePropertyMap( map );
212 if( mImpl->mCustomShader )
214 mImpl->mCustomShader->CreatePropertyMap( map );
217 Property::Map transform;
218 mImpl->mTransform.GetPropertyMap( transform );
219 map.Insert( DevelVisual::Property::TRANSFORM, transform );
221 bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
222 map.Insert( DevelVisual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
225 void Visual::Base::EnablePreMultipliedAlpha( bool preMultipled )
229 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
233 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
236 if( mImpl->mRenderer )
238 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
242 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
244 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
247 void Visual::Base::DoSetOffStage( Actor& actor )
249 actor.RemoveRenderer( mImpl->mRenderer );
250 mImpl->mRenderer.Reset();
253 bool Visual::Base::IsOnStage() const
255 return mImpl->mFlags & Impl::IS_ON_STAGE;
258 bool Visual::Base::IsFromCache() const
260 return mImpl->mFlags & Impl::IS_FROM_CACHE;
263 Renderer Visual::Base::GetRenderer()
265 return mImpl->mRenderer;
268 } // namespace Internal
270 } // namespace Toolkit