(Visuals) Added visual indices
[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 namespace Visual
40 {
41
42 Base::Base( VisualFactoryCache& factoryCache )
43 : mImpl( new Impl() ),
44   mFactoryCache( factoryCache )
45 {
46 }
47
48 Base::~Base()
49 {
50   delete mImpl;
51 }
52
53 void Base::SetCustomShader( const Property::Map& shaderMap )
54 {
55   if( mImpl->mCustomShader )
56   {
57     mImpl->mCustomShader->SetPropertyMap( shaderMap );
58   }
59   else
60   {
61    mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
62   }
63 }
64
65 void Base::Initialize( Actor& actor, const Property::Map& propertyMap )
66 {
67   Property::Value* customShaderValue = propertyMap.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
68   if( customShaderValue )
69   {
70     Property::Map shaderMap;
71     if( customShaderValue->Get( shaderMap ) )
72     {
73       SetCustomShader( shaderMap );
74     }
75   }
76
77   DoInitialize( actor, propertyMap );
78 }
79
80 void Base::SetSize( const Vector2& size )
81 {
82   mImpl->mSize = size;
83 }
84
85 const Vector2& Base::GetSize() const
86 {
87   return mImpl->mSize;
88 }
89
90 void Base::GetNaturalSize( Vector2& naturalSize ) const
91 {
92   naturalSize = Vector2::ZERO;
93 }
94
95 void Base::SetClipRect( const Rect<int>& clipRect )
96 {
97 }
98
99 void Base::SetOffset( const Vector2& offset )
100 {
101   mImpl->mOffset = offset;
102 }
103
104 void Base::SetDepthIndex( float index )
105 {
106   mImpl->mDepthIndex = index;
107   if( mImpl->mRenderer )
108   {
109     mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
110   }
111 }
112
113 float Base::GetDepthIndex() const
114 {
115   return mImpl->mDepthIndex;
116 }
117
118 void Base::SetOnStage( Actor& actor )
119 {
120   DoSetOnStage( actor );
121
122   mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
123   mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
124   actor.AddRenderer( mImpl->mRenderer );
125   mImpl->mFlags |= Impl::IS_ON_STAGE;
126 }
127
128 void Base::SetOffStage( Actor& actor )
129 {
130   if( GetIsOnStage() )
131   {
132     DoSetOffStage( actor );
133
134     mImpl->mFlags &= ~Impl::IS_ON_STAGE;
135   }
136 }
137
138 void Base::EnablePreMultipliedAlpha( bool preMultipled )
139 {
140   if(preMultipled)
141   {
142     mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
143   }
144   else
145   {
146     mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
147   }
148
149   if( mImpl->mRenderer )
150   {
151     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultipled);
152   }
153 }
154
155 bool Base::IsPreMultipliedAlphaEnabled() const
156 {
157   return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
158 }
159
160 void Base::DoSetOnStage( Actor& actor )
161 {
162 }
163
164 void Base::DoSetOffStage( Actor& actor )
165 {
166   actor.RemoveRenderer( mImpl->mRenderer );
167   mImpl->mRenderer.Reset();
168 }
169
170 void Base::CreatePropertyMap( Property::Map& map ) const
171 {
172   DoCreatePropertyMap( map );
173
174   if( mImpl->mCustomShader )
175   {
176     mImpl->mCustomShader->CreatePropertyMap( map );
177   }
178 }
179
180 bool Base::GetIsOnStage() const
181 {
182   return mImpl->mFlags & Impl::IS_ON_STAGE;
183 }
184
185 bool Base::GetIsFromCache() const
186 {
187   return mImpl->mFlags & Impl::IS_FROM_CACHE;
188 }
189
190 } // namespace Visual
191
192 } // namespace Internal
193
194 } // namespace Toolkit
195
196 } // namespace Dali