Updated visuals to separate alpha channel from mixColor
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-factory-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 // CLASS HEADER
18 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
19
20 // EXTERNAL INCLUDES
21 #include <dali/integration-api/debug.h>
22 #include <dali/public-api/images/image.h>
23 #include <dali/public-api/object/property-array.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/public-api/object/type-registry-helper.h>
26 #include <dali/devel-api/scripting/scripting.h>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
30 #include <dali-toolkit/devel-api/visuals/text-visual-properties.h>
31 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
32 #include <dali-toolkit/internal/visuals/border/border-visual.h>
33 #include <dali-toolkit/internal/visuals/color/color-visual.h>
34 #include <dali-toolkit/internal/visuals/gradient/gradient-visual.h>
35 #include <dali-toolkit/internal/visuals/image/image-visual.h>
36 #include <dali-toolkit/internal/visuals/mesh/mesh-visual.h>
37 #include <dali-toolkit/internal/visuals/npatch/npatch-visual.h>
38 #include <dali-toolkit/internal/visuals/primitive/primitive-visual.h>
39 #include <dali-toolkit/internal/visuals/svg/svg-visual.h>
40 #include <dali-toolkit/internal/visuals/text/text-visual.h>
41 #include <dali-toolkit/internal/visuals/animated-image/animated-image-visual.h>
42 #include <dali-toolkit/internal/visuals/wireframe/wireframe-visual.h>
43 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
44 #include <dali-toolkit/internal/visuals/visual-factory-resolve-url.h>
45 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
46
47 namespace Dali
48 {
49
50 namespace Toolkit
51 {
52
53 namespace Internal
54 {
55
56 namespace
57 {
58
59 BaseHandle Create()
60 {
61   BaseHandle handle = Toolkit::VisualFactory::Get();
62
63   return handle;
64 }
65
66 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::VisualFactory, Dali::BaseHandle, Create, true )
67 DALI_TYPE_REGISTRATION_END()
68
69 } // namespace
70
71 VisualFactory::VisualFactory( bool debugEnabled )
72 :mDebugEnabled( debugEnabled )
73 {
74 }
75
76 VisualFactory::~VisualFactory()
77 {
78 }
79
80 Toolkit::Visual::Base VisualFactory::CreateVisual( const Property::Map& propertyMap )
81 {
82   // Create factory cache if it hasn't already been
83   if( !mFactoryCache )
84   {
85     mFactoryCache = new VisualFactoryCache();
86   }
87
88   Visual::BasePtr visualPtr;
89
90   Property::Value* typeValue = propertyMap.Find( Toolkit::DevelVisual::Property::TYPE, VISUAL_TYPE );
91   Toolkit::DevelVisual::Type visualType = Toolkit::DevelVisual::IMAGE; // Default to IMAGE type.
92   if( typeValue )
93   {
94     Scripting::GetEnumerationProperty( *typeValue, VISUAL_TYPE_TABLE, VISUAL_TYPE_TABLE_COUNT, visualType );
95   }
96
97   switch( visualType )
98   {
99     case Toolkit::Visual::BORDER:
100     {
101       visualPtr = BorderVisual::New( *( mFactoryCache.Get() ), propertyMap );
102       break;
103     }
104
105     case Toolkit::Visual::COLOR:
106     {
107       visualPtr = ColorVisual::New( *( mFactoryCache.Get() ), propertyMap );
108       break;
109     }
110
111     case Toolkit::Visual::GRADIENT:
112     {
113       visualPtr = GradientVisual::New( *( mFactoryCache.Get() ), propertyMap );
114       break;
115     }
116
117     case Toolkit::Visual::IMAGE:
118     {
119       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
120       std::string imageUrl;
121       if( imageURLValue && imageURLValue->Get( imageUrl ) )
122       {
123         // first resolve url type to know which visual to create
124         UrlType::Type type = ResolveUrlType( imageUrl );
125         if( UrlType::N_PATCH == type )
126         {
127           visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
128         }
129         else if( UrlType::SVG == type )
130         {
131           visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
132         }
133         else if( UrlType::GIF == type )
134         {
135           visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
136         }
137         else // Regular image
138         {
139           visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
140         }
141       }
142
143       break;
144     }
145
146     case Toolkit::Visual::MESH:
147     {
148       visualPtr = MeshVisual::New( *( mFactoryCache.Get() ), propertyMap );
149       break;
150     }
151
152     case Toolkit::Visual::PRIMITIVE:
153     {
154       visualPtr = PrimitiveVisual::New( *( mFactoryCache.Get() ), propertyMap );
155       break;
156     }
157
158     case Toolkit::Visual::WIREFRAME:
159     {
160       visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), propertyMap );
161       break;
162     }
163
164     case Toolkit::DevelVisual::TEXT:
165     {
166       visualPtr = TextVisual::New( *( mFactoryCache.Get() ), propertyMap );
167       break;
168     }
169   }
170
171   if( !visualPtr )
172   {
173     DALI_LOG_ERROR( "Renderer type unknown\n" );
174   }
175
176   if( mDebugEnabled && visualType !=  Toolkit::DevelVisual::WIREFRAME )
177   {
178     //Create a WireframeVisual if we have debug enabled
179     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr, propertyMap );
180   }
181
182   return Toolkit::Visual::Base( visualPtr.Get() );
183 }
184
185 Toolkit::Visual::Base VisualFactory::CreateVisual( const Image& image )
186 {
187   if( !mFactoryCache )
188   {
189     mFactoryCache = new VisualFactoryCache();
190   }
191
192   Visual::BasePtr visualPtr;
193
194   NinePatchImage npatchImage = NinePatchImage::DownCast( image );
195   if( npatchImage )
196   {
197     visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), npatchImage );
198   }
199   else
200   {
201     visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), image );
202   }
203
204   if( mDebugEnabled )
205   {
206     //Create a WireframeVisual if we have debug enabled
207     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr );
208   }
209
210   return Toolkit::Visual::Base( visualPtr.Get() );
211 }
212
213 Toolkit::Visual::Base VisualFactory::CreateVisual( const std::string& url, ImageDimensions size )
214 {
215   if( !mFactoryCache )
216   {
217     mFactoryCache = new VisualFactoryCache();
218   }
219
220   Visual::BasePtr visualPtr;
221
222   // first resolve url type to know which visual to create
223   UrlType::Type type = ResolveUrlType( url );
224   if( UrlType::N_PATCH == type )
225   {
226     visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), url );
227   }
228   else if( UrlType::SVG == type )
229   {
230     visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), url );
231   }
232   else if( UrlType::GIF == type )
233   {
234     visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), url );
235   }
236   else // Regular image
237   {
238     visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), url, size );
239   }
240
241   if( mDebugEnabled )
242   {
243     //Create a WireframeVisual if we have debug enabled
244     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr );
245   }
246
247   return Toolkit::Visual::Base( visualPtr.Get() );
248 }
249
250 } // namespace Internal
251
252 } // namespace Toolkit
253
254 } // namespace Dali