Merge "C# control dashboard demo" into devel/master
[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/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>
29
30 namespace
31 {
32 #if defined(DEBUG_ENABLED)
33 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VISUAL_BASE" );
34 #endif
35 }
36
37 namespace Dali
38 {
39
40 namespace Toolkit
41 {
42
43 namespace Internal
44 {
45
46 Visual::Base::Base( VisualFactoryCache& factoryCache )
47 : mImpl( new Impl() ),
48   mFactoryCache( factoryCache )
49 {
50 }
51
52 Visual::Base::~Base()
53 {
54   delete mImpl;
55 }
56
57 void Visual::Base::SetCustomShader( const Property::Map& shaderMap )
58 {
59   if( mImpl->mCustomShader )
60   {
61     mImpl->mCustomShader->SetPropertyMap( shaderMap );
62   }
63   else
64   {
65     mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
66   }
67 }
68
69 void Visual::Base::SetProperties( const Property::Map& propertyMap )
70 {
71   for( size_t i = 0; i < propertyMap.Count(); ++i )
72   {
73     const KeyValuePair& pair = propertyMap.GetKeyValue( i );
74     const Property::Key& key = pair.first;
75     const Property::Value& value = pair.second;
76     switch( key.indexKey )
77     {
78       case DevelVisual::Property::SHADER:
79       {
80         Property::Map shaderMap;
81         if( value.Get( shaderMap ) )
82         {
83           SetCustomShader( shaderMap );
84         }
85         break;
86       }
87
88       case DevelVisual::Property::TRANSFORM:
89       {
90         Property::Map map;
91         if( value.Get( map ) )
92         {
93           mImpl->mTransform.SetPropertyMap( map );
94         }
95         break;
96       }
97
98       case DevelVisual::Property::PREMULTIPLIED_ALPHA:
99       {
100         bool premultipliedAlpha = false;
101         if( value.Get( premultipliedAlpha ) )
102         {
103           EnablePreMultipliedAlpha( premultipliedAlpha );
104         }
105         break;
106       }
107     }
108   }
109
110   DoSetProperties( propertyMap );
111 }
112
113 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
114 {
115   mImpl->mControlSize = controlSize;
116   mImpl->mTransform.SetPropertyMap( transform );
117
118 #if defined(DEBUG_ENABLED)
119   std::ostringstream oss;
120   oss << transform;
121   DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::SetTransformAndSize(%s) - [\e[1;32mtransform: %s  controlSize: (%3.1f, %3.1f)]\e[0m\n",
122                  GetName().c_str(), oss.str().c_str(), controlSize.x, controlSize.y );
123 #endif
124
125   OnSetTransform();
126 }
127
128 void Visual::Base::SetName( const std::string& name )
129 {
130   mImpl->mName = name;
131 }
132
133 const std::string& Visual::Base::GetName()
134 {
135   return mImpl->mName;
136 }
137
138 float Visual::Base::GetHeightForWidth( float width )
139 {
140   float aspectCorrectedHeight = 0.f;
141   Vector2 naturalSize;
142   GetNaturalSize( naturalSize );
143   if( naturalSize.width )
144   {
145     aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
146   }
147   return aspectCorrectedHeight;
148 }
149
150 float Visual::Base::GetWidthForHeight( float height )
151 {
152   float aspectCorrectedWidth = 0.f;
153   Vector2 naturalSize;
154   GetNaturalSize( naturalSize );
155   if( naturalSize.height > 0.0f )
156   {
157     aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
158   }
159   return aspectCorrectedWidth;
160 }
161
162 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
163 {
164   naturalSize = Vector2::ZERO;
165 }
166
167 void Visual::Base::SetDepthIndex( float index )
168 {
169   mImpl->mDepthIndex = index;
170   if( mImpl->mRenderer )
171   {
172     mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
173   }
174 }
175
176 float Visual::Base::GetDepthIndex() const
177 {
178   return mImpl->mDepthIndex;
179 }
180
181 void Visual::Base::SetOnStage( Actor& actor )
182 {
183   if( !IsOnStage() )
184   {
185     // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
186     // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
187     DoSetOnStage( actor );
188
189     if( mImpl->mRenderer )
190     {
191       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
192       mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
193       mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
194     }
195   }
196 }
197
198 void Visual::Base::SetOffStage( Actor& actor )
199 {
200   if( IsOnStage() )
201   {
202     DoSetOffStage( actor );
203
204     mImpl->mFlags &= ~Impl::IS_ON_STAGE;
205   }
206 }
207
208 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
209 {
210   DoCreatePropertyMap( map );
211
212   if( mImpl->mCustomShader )
213   {
214     mImpl->mCustomShader->CreatePropertyMap( map );
215   }
216
217   Property::Map transform;
218   mImpl->mTransform.GetPropertyMap( transform );
219   map.Insert( DevelVisual::Property::TRANSFORM, transform );
220
221   bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
222   map.Insert( DevelVisual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
223 }
224
225 void Visual::Base::EnablePreMultipliedAlpha( bool preMultipled )
226 {
227   if( preMultipled )
228   {
229     mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
230   }
231   else
232   {
233     mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
234   }
235
236   if( mImpl->mRenderer )
237   {
238     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
239   }
240 }
241
242 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
243 {
244   return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
245 }
246
247 void Visual::Base::DoSetOffStage( Actor& actor )
248 {
249   actor.RemoveRenderer( mImpl->mRenderer );
250   mImpl->mRenderer.Reset();
251 }
252
253 bool Visual::Base::IsOnStage() const
254 {
255   return mImpl->mFlags & Impl::IS_ON_STAGE;
256 }
257
258 bool Visual::Base::IsFromCache() const
259 {
260   return mImpl->mFlags & Impl::IS_FROM_CACHE;
261 }
262
263 Renderer Visual::Base::GetRenderer()
264 {
265   return mImpl->mRenderer;
266 }
267
268 } // namespace Internal
269
270 } // namespace Toolkit
271
272 } // namespace Dali