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>
39 Visual::Base::Base( VisualFactoryCache& factoryCache )
40 : mImpl( new Impl() ),
41 mFactoryCache( factoryCache )
50 void Visual::Base::SetCustomShader( const Property::Map& shaderMap )
52 if( mImpl->mCustomShader )
54 mImpl->mCustomShader->SetPropertyMap( shaderMap );
58 mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
62 void Visual::Base::SetProperties( const Property::Map& propertyMap )
64 for( size_t i = 0; i < propertyMap.Count(); ++i )
66 const KeyValuePair& pair = propertyMap.GetKeyValue( i );
67 const Property::Key& key = pair.first;
68 const Property::Value& value = pair.second;
69 switch( key.indexKey )
71 case DevelVisual::Property::SHADER:
73 Property::Map shaderMap;
74 if( value.Get( shaderMap ) )
76 SetCustomShader( shaderMap );
81 case DevelVisual::Property::TRANSFORM:
84 if( value.Get( map ) )
86 mImpl->mTransform.SetPropertyMap( map );
91 case DevelVisual::Property::PREMULTIPLIED_ALPHA:
93 bool premultipliedAlpha( premultipliedAlpha );
94 if( value.Get( premultipliedAlpha ) )
96 EnablePreMultipliedAlpha( premultipliedAlpha );
103 DoSetProperties( propertyMap );
106 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
108 mImpl->mControlSize = controlSize;
109 mImpl->mTransform.SetPropertyMap( transform );
113 void Visual::Base::SetName( const std::string& name )
118 const std::string& Visual::Base::GetName()
123 float Visual::Base::GetHeightForWidth( float width ) const
128 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
130 naturalSize = Vector2::ZERO;
133 void Visual::Base::SetDepthIndex( float index )
135 mImpl->mDepthIndex = index;
136 if( mImpl->mRenderer )
138 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
142 float Visual::Base::GetDepthIndex() const
144 return mImpl->mDepthIndex;
147 void Visual::Base::SetOnStage( Actor& actor )
151 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
152 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
153 DoSetOnStage( actor );
155 if( mImpl->mRenderer )
157 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
158 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
159 mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
164 void Visual::Base::SetOffStage( Actor& actor )
168 DoSetOffStage( actor );
170 mImpl->mFlags &= ~Impl::IS_ON_STAGE;
174 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
176 DoCreatePropertyMap( map );
178 if( mImpl->mCustomShader )
180 mImpl->mCustomShader->CreatePropertyMap( map );
183 Property::Map transform;
184 mImpl->mTransform.GetPropertyMap( transform );
185 map.Insert( DevelVisual::Property::TRANSFORM, transform );
187 bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
188 map.Insert( DevelVisual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
191 void Visual::Base::EnablePreMultipliedAlpha( bool preMultipled )
195 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
199 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
202 if( mImpl->mRenderer )
204 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
208 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
210 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
213 void Visual::Base::DoSetOffStage( Actor& actor )
215 actor.RemoveRenderer( mImpl->mRenderer );
216 mImpl->mRenderer.Reset();
219 bool Visual::Base::IsOnStage() const
221 return mImpl->mFlags & Impl::IS_ON_STAGE;
224 bool Visual::Base::IsFromCache() const
226 return mImpl->mFlags & Impl::IS_FROM_CACHE;
229 } // namespace Internal
231 } // namespace Toolkit