Merge "Add deprecated tag and missed doxygen" 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 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   for( size_t i = 0; i < propertyMap.Count(); ++i )
65   {
66     const KeyValuePair& pair = propertyMap.GetKeyValue( i );
67     const Property::Key& key = pair.first;
68     const Property::Value& value = pair.second;
69     switch( key.indexKey )
70     {
71       case DevelVisual::Property::SHADER:
72       {
73         Property::Map shaderMap;
74         if( value.Get( shaderMap ) )
75         {
76           SetCustomShader( shaderMap );
77         }
78         break;
79       }
80
81       case DevelVisual::Property::TRANSFORM:
82       {
83         Property::Map map;
84         if( value.Get( map ) )
85         {
86           mImpl->mTransform.SetPropertyMap( map );
87         }
88         break;
89       }
90
91       case DevelVisual::Property::PREMULTIPLIED_ALPHA:
92       {
93         bool premultipliedAlpha( premultipliedAlpha );
94         if( value.Get( premultipliedAlpha ) )
95         {
96           EnablePreMultipliedAlpha( premultipliedAlpha );
97         }
98         break;
99       }
100     }
101   }
102
103   DoSetProperties( propertyMap );
104 }
105
106 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
107 {
108   mImpl->mControlSize = controlSize;
109   mImpl->mTransform.SetPropertyMap( transform );
110   OnSetTransform();
111 }
112
113 void Visual::Base::SetName( const std::string& name )
114 {
115   mImpl->mName = name;
116 }
117
118 const std::string& Visual::Base::GetName()
119 {
120   return mImpl->mName;
121 }
122
123 float Visual::Base::GetHeightForWidth( float width ) const
124 {
125   return 0.f;
126 }
127
128 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
129 {
130   naturalSize = Vector2::ZERO;
131 }
132
133 void Visual::Base::SetDepthIndex( float index )
134 {
135   mImpl->mDepthIndex = index;
136   if( mImpl->mRenderer )
137   {
138     mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
139   }
140 }
141
142 float Visual::Base::GetDepthIndex() const
143 {
144   return mImpl->mDepthIndex;
145 }
146
147 void Visual::Base::SetOnStage( Actor& actor )
148 {
149   if( !IsOnStage() )
150   {
151     // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
152     // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
153     DoSetOnStage( actor );
154
155     if( mImpl->mRenderer )
156     {
157       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
158       mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
159       mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
160     }
161   }
162 }
163
164 void Visual::Base::SetOffStage( Actor& actor )
165 {
166   if( IsOnStage() )
167   {
168     DoSetOffStage( actor );
169
170     mImpl->mFlags &= ~Impl::IS_ON_STAGE;
171   }
172 }
173
174 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
175 {
176   DoCreatePropertyMap( map );
177
178   if( mImpl->mCustomShader )
179   {
180     mImpl->mCustomShader->CreatePropertyMap( map );
181   }
182
183   Property::Map transform;
184   mImpl->mTransform.GetPropertyMap( transform );
185   map.Insert( DevelVisual::Property::TRANSFORM, transform );
186
187   bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
188   map.Insert( DevelVisual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
189 }
190
191 void Visual::Base::EnablePreMultipliedAlpha( bool preMultipled )
192 {
193   if( preMultipled )
194   {
195     mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
196   }
197   else
198   {
199     mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
200   }
201
202   if( mImpl->mRenderer )
203   {
204     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
205   }
206 }
207
208 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
209 {
210   return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
211 }
212
213 void Visual::Base::DoSetOffStage( Actor& actor )
214 {
215   actor.RemoveRenderer( mImpl->mRenderer );
216   mImpl->mRenderer.Reset();
217 }
218
219 bool Visual::Base::IsOnStage() const
220 {
221   return mImpl->mFlags & Impl::IS_ON_STAGE;
222 }
223
224 bool Visual::Base::IsFromCache() const
225 {
226   return mImpl->mFlags & Impl::IS_FROM_CACHE;
227 }
228
229 } // namespace Internal
230
231 } // namespace Toolkit
232
233 } // namespace Dali