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/public-api/visuals/visual-properties.h>
28 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
29 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
43 Base::Base( VisualFactoryCache& factoryCache )
44 : mImpl( new Impl() ),
45 mFactoryCache( factoryCache )
54 void Base::SetCustomShader( const Property::Map& shaderMap )
56 if( mImpl->mCustomShader )
58 mImpl->mCustomShader->SetPropertyMap( shaderMap );
62 mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
66 void Base::Initialize( Actor& actor, const Property::Map& propertyMap )
69 Property::Value* customShaderValue = propertyMap.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
70 if( customShaderValue )
72 Property::Map shaderMap;
73 if( customShaderValue->Get( shaderMap ) )
75 SetCustomShader( shaderMap );
79 Property::Value* premultipliedAlphaValue = propertyMap.Find( Toolkit::DevelVisual::Property::PREMULTIPLIED_ALPHA, PREMULTIPLIED_ALPHA );
80 if( premultipliedAlphaValue )
82 bool premultipliedAlpha( false );
83 if( premultipliedAlphaValue->Get( premultipliedAlpha ) )
85 EnablePreMultipliedAlpha( premultipliedAlpha );
89 DoInitialize( actor, propertyMap );
92 void Base::SetSize( const Vector2& size )
97 const Vector2& Base::GetSize() const
102 void Base::SetName( const std::string& name )
107 const std::string& Base::GetName() const
112 void Base::GetNaturalSize( Vector2& naturalSize ) const
114 naturalSize = Vector2::ZERO;
117 void Base::SetClipRect( const Rect<int>& clipRect )
121 void Base::SetOffset( const Vector2& offset )
123 mImpl->mOffset = offset;
126 void Base::SetDepthIndex( float index )
128 mImpl->mDepthIndex = index;
129 if( mImpl->mRenderer )
131 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
135 float Base::GetDepthIndex() const
137 return mImpl->mDepthIndex;
140 void Base::SetOnStage( Actor& actor )
142 // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
143 // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
144 DoSetOnStage( actor );
146 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
147 mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
148 mImpl->mFlags |= Impl::IS_ON_STAGE;
151 void Base::SetOffStage( Actor& actor )
155 DoSetOffStage( actor );
157 mImpl->mFlags &= ~Impl::IS_ON_STAGE;
161 void Base::EnablePreMultipliedAlpha( bool preMultipled )
165 mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
169 mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
172 if( mImpl->mRenderer )
174 mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
178 bool Base::IsPreMultipliedAlphaEnabled() const
180 return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
183 void Base::DoSetOffStage( Actor& actor )
185 actor.RemoveRenderer( mImpl->mRenderer );
186 mImpl->mRenderer.Reset();
190 void Visual::Base::AddResourceObserver( Visual::ResourceObserver& observer)
192 mImpl->mResourceObserver = &observer;
195 void Visual::Base::RemoveResourceObserver( Visual::ResourceObserver& observer )
197 mImpl->mResourceObserver = NULL;
200 void Visual::Base::ResourceReady()
203 if( mImpl->mResourceReady )
205 // only inform the observer the first time the resource is ready
210 if( mImpl->mResourceObserver )
212 mImpl->mResourceReady = true;
214 // observer is currently a control impl
215 mImpl->mResourceObserver->ResourceReady( *this );
219 bool Visual::Base::IsResourceReady() const
221 return mImpl->mResourceReady;
224 void Base::CreatePropertyMap( Property::Map& map ) const
226 DoCreatePropertyMap( map );
228 if( mImpl->mCustomShader )
230 mImpl->mCustomShader->CreatePropertyMap( map );
233 bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
234 map.Insert( DevelVisual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
237 bool Base::GetIsOnStage() const
239 return mImpl->mFlags & Impl::IS_ON_STAGE;
242 bool Base::GetIsFromCache() const
244 return mImpl->mFlags & Impl::IS_FROM_CACHE;
247 } // namespace Visual
249 } // namespace Internal
251 } // namespace Toolkit