TextVisual implementation.
[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/public-api/visuals/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::Initialize( Actor& actor, const Property::Map& propertyMap )
63 {
64   Property::Value* customShaderValue = propertyMap.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
65   if( customShaderValue )
66   {
67     Property::Map shaderMap;
68     if( customShaderValue->Get( shaderMap ) )
69     {
70       SetCustomShader( shaderMap );
71     }
72   }
73
74   DoInitialize( actor, propertyMap );
75 }
76
77 void Visual::Base::SetSize( const Vector2& size )
78 {
79   mImpl->mSize = size;
80 }
81
82 const Vector2& Visual::Base::GetSize() const
83 {
84   return mImpl->mSize;
85 }
86
87 float Visual::Base::GetHeightForWidth( float width ) const
88 {
89   return 0.f;
90 }
91
92 void Visual::Base::GetNaturalSize( Vector2& naturalSize ) const
93 {
94   naturalSize = Vector2::ZERO;
95 }
96
97 void Visual::Base::SetDepthIndex( float index )
98 {
99   mImpl->mDepthIndex = index;
100   if( mImpl->mRenderer )
101   {
102     mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
103   }
104 }
105
106 float Visual::Base::GetDepthIndex() const
107 {
108   return mImpl->mDepthIndex;
109 }
110
111 void Visual::Base::SetOnStage( Actor& actor )
112 {
113   // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
114   // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
115   DoSetOnStage( actor );
116
117   mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
118   mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
119   mImpl->mFlags |= Impl::IS_ON_STAGE;
120 }
121
122 void Visual::Base::SetOffStage( Actor& actor )
123 {
124   if( GetIsOnStage() )
125   {
126     DoSetOffStage( actor );
127
128     mImpl->mFlags &= ~Impl::IS_ON_STAGE;
129   }
130 }
131
132 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
133 {
134   DoCreatePropertyMap( map );
135
136   if( mImpl->mCustomShader )
137   {
138     mImpl->mCustomShader->CreatePropertyMap( map );
139   }
140 }
141
142 void Visual::Base::EnablePreMultipliedAlpha( bool preMultipled )
143 {
144   if(preMultipled)
145   {
146     mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
147   }
148   else
149   {
150     mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
151   }
152
153   if( mImpl->mRenderer )
154   {
155     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
156   }
157 }
158
159 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
160 {
161   return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
162 }
163
164 void Visual::Base::DoSetOffStage( Actor& actor )
165 {
166   actor.RemoveRenderer( mImpl->mRenderer );
167   mImpl->mRenderer.Reset();
168 }
169
170 bool Visual::Base::GetIsOnStage() const
171 {
172   return mImpl->mFlags & Impl::IS_ON_STAGE;
173 }
174
175 bool Visual::Base::GetIsFromCache() const
176 {
177   return mImpl->mFlags & Impl::IS_FROM_CACHE;
178 }
179
180 void Visual::Base::SetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
181 {
182   DALI_ASSERT_ALWAYS( ( index > Property::INVALID_INDEX ) &&
183                       ( index > VISUAL_PROPERTY_BASE_START_INDEX ) && // Change the type of visual is not allowed.
184                       "Property index is out of bounds" );
185
186   if( index < VISUAL_PROPERTY_START_INDEX )
187   {
188     // TODO set the properties of the visual base.
189   }
190   else
191   {
192     DoSetProperty( index, propertyValue );
193   }
194 }
195
196 Dali::Property::Value Visual::Base::GetProperty( Dali::Property::Index index )
197 {
198   DALI_ASSERT_ALWAYS( ( index > Property::INVALID_INDEX ) &&
199                       ( index >= VISUAL_PROPERTY_BASE_START_INDEX ) &&
200                       "Property index is out of bounds" );
201
202   Dali::Property::Value value;
203
204   if( index < VISUAL_PROPERTY_START_INDEX )
205   {
206     // TODO retrieve the properties of the visual base.
207   }
208   else
209   {
210     value = DoGetProperty( index );
211   }
212
213   return value;
214 }
215
216 } // namespace Internal
217
218 } // namespace Toolkit
219
220 } // namespace Dali