Adding Visual namespace and Visual becomes Visual::Base
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-base-impl.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "visual-base-impl.h"
20
21 // EXTERNAL HEADER
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/integration-api/debug.h>
24
25 //INTERNAL HEARDER
26 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
27
28 namespace
29 {
30 //custom shader
31 const char * const CUSTOM_SHADER( "shader" );
32 const char * const CUSTOM_VERTEX_SHADER( "vertexShader" );
33 const char * const CUSTOM_FRAGMENT_SHADER( "fragmentShader" );
34 const char * const CUSTOM_SUBDIVIDE_GRID_X( "subdivideGridX" );
35 const char * const CUSTOM_SUBDIVIDE_GRID_Y( "subdivideGridY" );
36 const char * const CUSTOM_SHADER_HINTS( "hints" ); ///< type INTEGER; (bitfield) values from enum Shader::Hint
37 }
38
39 namespace Dali
40 {
41
42 namespace Toolkit
43 {
44
45 namespace Internal
46 {
47
48 namespace Visual
49 {
50
51 Base::Base( VisualFactoryCache& factoryCache )
52 : mImpl( new Impl() ),
53   mFactoryCache( factoryCache )
54 {
55 }
56
57 Base::~Base()
58 {
59   delete mImpl;
60 }
61
62 void Base::SetCustomShader( const Property::Map& shaderMap )
63 {
64   if( mImpl->mCustomShader )
65   {
66     mImpl->mCustomShader->SetPropertyMap( shaderMap );
67   }
68   else
69   {
70    mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
71   }
72 }
73
74 void Base::Initialize( Actor& actor, const Property::Map& propertyMap )
75 {
76   Property::Value* customShaderValue = propertyMap.Find( CUSTOM_SHADER );
77   if( customShaderValue )
78   {
79     Property::Map shaderMap;
80     if( customShaderValue->Get( shaderMap ) )
81     {
82       SetCustomShader( shaderMap );
83     }
84   }
85
86   DoInitialize( actor, propertyMap );
87 }
88
89 void Base::SetSize( const Vector2& size )
90 {
91   mImpl->mSize = size;
92 }
93
94 const Vector2& Base::GetSize() const
95 {
96   return mImpl->mSize;
97 }
98
99 void Base::GetNaturalSize( Vector2& naturalSize ) const
100 {
101   naturalSize = Vector2::ZERO;
102 }
103
104 void Base::SetClipRect( const Rect<int>& clipRect )
105 {
106 }
107
108 void Base::SetOffset( const Vector2& offset )
109 {
110   mImpl->mOffset = offset;
111 }
112
113 void Base::SetDepthIndex( float index )
114 {
115   mImpl->mDepthIndex = index;
116   if( mImpl->mRenderer )
117   {
118     mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
119   }
120 }
121
122 float Base::GetDepthIndex() const
123 {
124   return mImpl->mDepthIndex;
125 }
126
127 void Base::SetOnStage( Actor& actor )
128 {
129   DoSetOnStage( actor );
130
131   mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
132   mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
133   actor.AddRenderer( mImpl->mRenderer );
134   mImpl->mFlags |= Impl::IS_ON_STAGE;
135 }
136
137 void Base::SetOffStage( Actor& actor )
138 {
139   if( GetIsOnStage() )
140   {
141     DoSetOffStage( actor );
142
143     mImpl->mFlags &= ~Impl::IS_ON_STAGE;
144   }
145 }
146
147 void Base::EnablePreMultipliedAlpha( bool preMultipled )
148 {
149   if(preMultipled)
150   {
151     mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
152   }
153   else
154   {
155     mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
156   }
157
158   if( mImpl->mRenderer )
159   {
160     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
161   }
162 }
163
164 bool Base::IsPreMultipliedAlphaEnabled() const
165 {
166   return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
167 }
168
169 void Base::DoSetOnStage( Actor& actor )
170 {
171 }
172
173 void Base::DoSetOffStage( Actor& actor )
174 {
175   actor.RemoveRenderer( mImpl->mRenderer );
176   mImpl->mRenderer.Reset();
177 }
178
179 void Base::CreatePropertyMap( Property::Map& map ) const
180 {
181   DoCreatePropertyMap( map );
182
183   if( mImpl->mCustomShader )
184   {
185     mImpl->mCustomShader->CreatePropertyMap( map );
186   }
187 }
188
189 bool Base::GetIsOnStage() const
190 {
191   return mImpl->mFlags & Impl::IS_ON_STAGE;
192 }
193
194 bool Base::GetIsFromCache() const
195 {
196   return mImpl->mFlags & Impl::IS_FROM_CACHE;
197 }
198
199 } // namespace Visual
200
201 } // namespace Internal
202
203 } // namespace Toolkit
204
205 } // namespace Dali