e8ed4a32f950f85a3637813717db1193fd70d75c
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-base-impl.cpp
1 /*
2  * Copyright (c) 2016 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/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>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 Visual::Base::Base( VisualFactoryCache& factoryCache )
40 : mImpl( new Impl() ),
41   mFactoryCache( factoryCache )
42 {
43 }
44
45 Visual::Base::~Base()
46 {
47   delete mImpl;
48 }
49
50 void Visual::Base::SetCustomShader( const Property::Map& shaderMap )
51 {
52   if( mImpl->mCustomShader )
53   {
54     mImpl->mCustomShader->SetPropertyMap( shaderMap );
55   }
56   else
57   {
58     mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
59   }
60 }
61
62 void Visual::Base::SetProperties( const Property::Map& propertyMap )
63 {
64   Property::Value* customShaderValue = propertyMap.Find( VisualProperty::SHADER, CUSTOM_SHADER );
65   if( customShaderValue )
66   {
67     Property::Map shaderMap;
68     if( customShaderValue->Get( shaderMap ) )
69     {
70       SetCustomShader( shaderMap );
71     }
72   }
73
74   Property::Value* transform = propertyMap.Find( Toolkit::Visual::DevelProperty::TRANSFORM, TRANSFORM );
75   if( transform )
76   {
77     Property::Map map;
78     if( transform->Get( map ) )
79     {
80       mImpl->mTransform.SetPropertyMap( map );
81     }
82   }
83
84   DoSetProperties( propertyMap );
85 }
86
87 void Visual::Base::SetName( const std::string& name )
88 {
89   mImpl->mName = name;
90 }
91
92 const std::string& Visual::Base::GetName()
93 {
94   return mImpl->mName;
95 }
96
97 void Visual::Base::SetSize( const Vector2& size )
98 {
99   mImpl->mSize = size;
100 }
101
102 const Vector2& Visual::Base::GetSize() const
103 {
104   return mImpl->mSize;
105 }
106
107 float Visual::Base::GetHeightForWidth( float width ) const
108 {
109   return 0.f;
110 }
111
112 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
113 {
114   naturalSize = Vector2::ZERO;
115 }
116
117 void Visual::Base::SetDepthIndex( float index )
118 {
119   mImpl->mDepthIndex = index;
120   if( mImpl->mRenderer )
121   {
122     mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
123   }
124 }
125
126 float Visual::Base::GetDepthIndex() const
127 {
128   return mImpl->mDepthIndex;
129 }
130
131 void Visual::Base::SetOnStage( Actor& actor )
132 {
133   if( !IsOnStage() )
134   {
135     // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
136     // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
137     DoSetOnStage( actor );
138
139     if( mImpl->mRenderer )
140     {
141       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
142       mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
143       mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
144     }
145   }
146 }
147
148 void Visual::Base::SetOffStage( Actor& actor )
149 {
150   if( IsOnStage() )
151   {
152     DoSetOffStage( actor );
153
154     mImpl->mFlags &= ~Impl::IS_ON_STAGE;
155   }
156 }
157
158 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
159 {
160   DoCreatePropertyMap( map );
161
162   if( mImpl->mCustomShader )
163   {
164     mImpl->mCustomShader->CreatePropertyMap( map );
165   }
166
167   Property::Map transform;
168   mImpl->mTransform.GetPropertyMap( transform );
169   map.Insert( Toolkit::Visual::DevelProperty::TRANSFORM, transform );
170 }
171
172 void Visual::Base::EnablePreMultipliedAlpha( bool preMultipled )
173 {
174   if(preMultipled)
175   {
176     mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
177   }
178   else
179   {
180     mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
181   }
182
183   if( mImpl->mRenderer )
184   {
185     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
186   }
187 }
188
189 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
190 {
191   return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
192 }
193
194 void Visual::Base::DoSetOffStage( Actor& actor )
195 {
196   actor.RemoveRenderer( mImpl->mRenderer );
197   mImpl->mRenderer.Reset();
198 }
199
200 bool Visual::Base::IsOnStage() const
201 {
202   return mImpl->mFlags & Impl::IS_ON_STAGE;
203 }
204
205 bool Visual::Base::IsFromCache() const
206 {
207   return mImpl->mFlags & Impl::IS_FROM_CACHE;
208 }
209
210 void Visual::Base::SetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
211 {
212   DALI_ASSERT_ALWAYS( ( index > Property::INVALID_INDEX ) &&
213                       ( index > VISUAL_PROPERTY_BASE_START_INDEX ) && // Change the type of visual is not allowed.
214                       "Property index is out of bounds" );
215
216   if( index < VISUAL_PROPERTY_START_INDEX )
217   {
218     if( index == Dali::Toolkit::Visual::DevelProperty::TRANSFORM )
219     {
220       Property::Map* map = propertyValue.GetMap();
221       if( map )
222       {
223         mImpl->mTransform.SetPropertyMap( *map );
224         OnSetTransform();
225       }
226     }
227
228     // TODO set the properties of the visual base.
229   }
230   else
231   {
232     DoSetProperty( index, propertyValue );
233   }
234 }
235
236 Dali::Property::Value Visual::Base::GetProperty( Dali::Property::Index index )
237 {
238   DALI_ASSERT_ALWAYS( ( index > Property::INVALID_INDEX ) &&
239                       ( index >= VISUAL_PROPERTY_BASE_START_INDEX ) &&
240                       "Property index is out of bounds" );
241
242   Dali::Property::Value value;
243
244   if( index < VISUAL_PROPERTY_START_INDEX )
245   {
246     if( index == Dali::Toolkit::Visual::DevelProperty::TRANSFORM )
247     {
248       Property::Map map;
249       mImpl->mTransform.GetPropertyMap( map );
250       return map;
251     }
252     // TODO retrieve the properties of the visual base.
253   }
254   else
255   {
256     value = DoGetProperty( index );
257   }
258
259   return value;
260 }
261
262 } // namespace Internal
263
264 } // namespace Toolkit
265
266 } // namespace Dali