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/visual-factory/devel-visual-properties.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 Property::Value* customShaderValue = propertyMap.Find( VisualProperty::SHADER, CUSTOM_SHADER );
65 if( customShaderValue )
67 Property::Map shaderMap;
68 if( customShaderValue->Get( shaderMap ) )
70 SetCustomShader( shaderMap );
74 Property::Value* transform = propertyMap.Find( Toolkit::Visual::DevelProperty::TRANSFORM, TRANSFORM );
78 if( transform->Get( map ) )
80 mImpl->mTransform.SetPropertyMap( map );
84 DoSetProperties( propertyMap );
87 void Visual::Base::SetName( const std::string& name )
92 const std::string& Visual::Base::GetName()
97 void Visual::Base::SetSize( const Vector2& size )
102 const Vector2& Visual::Base::GetSize() const
107 float Visual::Base::GetHeightForWidth( float width ) const
112 void Visual::Base::GetNaturalSize( Vector2& naturalSize ) const
114 naturalSize = Vector2::ZERO;
117 void Visual::Base::SetDepthIndex( float index )
119 mImpl->mDepthIndex = index;
120 if( mImpl->mRenderer )
122 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
126 float Visual::Base::GetDepthIndex() const
128 return mImpl->mDepthIndex;
131 void Visual::Base::SetOnStage( Actor& actor )
133 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
134 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
135 DoSetOnStage( actor );
137 if( mImpl->mRenderer )
139 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
140 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
141 mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
145 void Visual::Base::SetOffStage( Actor& actor )
149 DoSetOffStage( actor );
151 mImpl->mFlags &= ~Impl::IS_ON_STAGE;
155 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
157 DoCreatePropertyMap( map );
159 if( mImpl->mCustomShader )
161 mImpl->mCustomShader->CreatePropertyMap( map );
164 Property::Map transform;
165 mImpl->mTransform.GetPropertyMap( transform );
166 map.Insert( Toolkit::Visual::DevelProperty::TRANSFORM, transform );
169 void Visual::Base::EnablePreMultipliedAlpha( bool preMultipled )
173 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
177 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
180 if( mImpl->mRenderer )
182 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
186 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
188 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
191 void Visual::Base::DoSetOffStage( Actor& actor )
193 actor.RemoveRenderer( mImpl->mRenderer );
194 mImpl->mRenderer.Reset();
197 bool Visual::Base::IsOnStage() const
199 return mImpl->mFlags & Impl::IS_ON_STAGE;
202 bool Visual::Base::IsFromCache() const
204 return mImpl->mFlags & Impl::IS_FROM_CACHE;
207 void Visual::Base::SetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
209 DALI_ASSERT_ALWAYS( ( index > Property::INVALID_INDEX ) &&
210 ( index > VISUAL_PROPERTY_BASE_START_INDEX ) && // Change the type of visual is not allowed.
211 "Property index is out of bounds" );
213 if( index < VISUAL_PROPERTY_START_INDEX )
215 if( index == Dali::Toolkit::Visual::DevelProperty::TRANSFORM )
217 Property::Map* map = propertyValue.GetMap();
220 mImpl->mTransform.SetPropertyMap( *map );
225 // TODO set the properties of the visual base.
229 DoSetProperty( index, propertyValue );
233 Dali::Property::Value Visual::Base::GetProperty( Dali::Property::Index index )
235 DALI_ASSERT_ALWAYS( ( index > Property::INVALID_INDEX ) &&
236 ( index >= VISUAL_PROPERTY_BASE_START_INDEX ) &&
237 "Property index is out of bounds" );
239 Dali::Property::Value value;
241 if( index < VISUAL_PROPERTY_START_INDEX )
243 if( index == Dali::Toolkit::Visual::DevelProperty::TRANSFORM )
246 mImpl->mTransform.GetPropertyMap( map );
249 // TODO retrieve the properties of the visual base.
253 value = DoGetProperty( index );
259 } // namespace Internal
261 } // namespace Toolkit