469ead5a0ee6d107a52ac5ccd0fb6598ba725f13
[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/image-visual-properties-devel.h>
31 #include <dali-toolkit/devel-api/visuals/text-visual-properties.h>
32 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
33 #include <dali-toolkit/internal/visuals/border/border-visual.h>
34 #include <dali-toolkit/internal/visuals/color/color-visual.h>
35 #include <dali-toolkit/internal/visuals/gradient/gradient-visual.h>
36 #include <dali-toolkit/internal/visuals/image/image-visual.h>
37 #include <dali-toolkit/internal/visuals/mesh/mesh-visual.h>
38 #include <dali-toolkit/internal/visuals/npatch/npatch-visual.h>
39 #include <dali-toolkit/internal/visuals/primitive/primitive-visual.h>
40 #include <dali-toolkit/internal/visuals/svg/svg-visual.h>
41 #include <dali-toolkit/internal/visuals/text/text-visual.h>
42 #include <dali-toolkit/internal/visuals/animated-image/animated-image-visual.h>
43 #include <dali-toolkit/internal/visuals/wireframe/wireframe-visual.h>
44 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
45 #include <dali-toolkit/internal/visuals/visual-url.h>
46 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
47
48 namespace Dali
49 {
50
51 namespace Toolkit
52 {
53
54 namespace Internal
55 {
56
57 namespace
58 {
59
60 BaseHandle Create()
61 {
62   BaseHandle handle = Toolkit::VisualFactory::Get();
63
64   return handle;
65 }
66
67 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::VisualFactory, Dali::BaseHandle, Create, true )
68 DALI_TYPE_REGISTRATION_END()
69
70 } // namespace
71
72 VisualFactory::VisualFactory( bool debugEnabled )
73 :mDebugEnabled( debugEnabled )
74 {
75 }
76
77 VisualFactory::~VisualFactory()
78 {
79 }
80
81 Toolkit::Visual::Base VisualFactory::CreateVisual( const Property::Map& propertyMap )
82 {
83   // Create factory cache if it hasn't already been
84   if( !mFactoryCache )
85   {
86     mFactoryCache = new VisualFactoryCache();
87   }
88
89   Visual::BasePtr visualPtr;
90
91   Property::Value* typeValue = propertyMap.Find( Toolkit::DevelVisual::Property::TYPE, VISUAL_TYPE );
92   Toolkit::DevelVisual::Type visualType = Toolkit::DevelVisual::IMAGE; // Default to IMAGE type.
93   if( typeValue )
94   {
95     Scripting::GetEnumerationProperty( *typeValue, VISUAL_TYPE_TABLE, VISUAL_TYPE_TABLE_COUNT, visualType );
96   }
97
98   switch( visualType )
99   {
100     case Toolkit::Visual::BORDER:
101     {
102       visualPtr = BorderVisual::New( *( mFactoryCache.Get() ), propertyMap );
103       break;
104     }
105
106     case Toolkit::Visual::COLOR:
107     {
108       visualPtr = ColorVisual::New( *( mFactoryCache.Get() ), propertyMap );
109       break;
110     }
111
112     case Toolkit::Visual::GRADIENT:
113     {
114       visualPtr = GradientVisual::New( *( mFactoryCache.Get() ), propertyMap );
115       break;
116     }
117
118     case Toolkit::Visual::IMAGE:
119     {
120       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
121       std::string imageUrl;
122       if( imageURLValue )
123       {
124         if( imageURLValue->Get( imageUrl ) )
125         {
126           VisualUrl visualUrl( imageUrl );
127
128           switch( visualUrl.GetType() )
129           {
130             case VisualUrl::N_PATCH:
131             {
132               visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
133               break;
134             }
135             case VisualUrl::SVG:
136             {
137               visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
138               break;
139             }
140             case VisualUrl::GIF:
141             {
142               visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
143               break;
144             }
145             case VisualUrl::REGULAR_IMAGE:
146             {
147               visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
148               break;
149             }
150           }
151         }
152         else
153         {
154           Property::Array* array = imageURLValue->GetArray();
155           if( array )
156           {
157             visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), *array, propertyMap );
158           }
159         }
160       }
161       break;
162     }
163
164     case Toolkit::Visual::MESH:
165     {
166       visualPtr = MeshVisual::New( *( mFactoryCache.Get() ), propertyMap );
167       break;
168     }
169
170     case Toolkit::Visual::PRIMITIVE:
171     {
172       visualPtr = PrimitiveVisual::New( *( mFactoryCache.Get() ), propertyMap );
173       break;
174     }
175
176     case Toolkit::Visual::WIREFRAME:
177     {
178       visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), propertyMap );
179       break;
180     }
181
182     case Toolkit::DevelVisual::TEXT:
183     {
184       visualPtr = TextVisual::New( *( mFactoryCache.Get() ), propertyMap );
185       break;
186     }
187
188     case Toolkit::DevelVisual::N_PATCH:
189     {
190       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
191       std::string imageUrl;
192       if( imageURLValue && imageURLValue->Get( imageUrl ) )
193       {
194         visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
195       }
196       break;
197     }
198
199     case Toolkit::DevelVisual::SVG:
200     {
201       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
202       std::string imageUrl;
203       if( imageURLValue && imageURLValue->Get( imageUrl ) )
204       {
205         visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
206       }
207       break;
208     }
209
210     case Toolkit::DevelVisual::ANIMATED_IMAGE:
211     {
212       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
213       std::string imageUrl;
214       if( imageURLValue )
215       {
216         if( imageURLValue->Get( imageUrl ) )
217         {
218           visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
219         }
220         else
221         {
222           Property::Array* array = imageURLValue->GetArray();
223           if( array )
224           {
225             visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), *array, propertyMap );
226           }
227         }
228       }
229       break;
230     }
231   }
232
233   if( !visualPtr )
234   {
235     DALI_LOG_ERROR( "Renderer type unknown\n" );
236   }
237
238   if( mDebugEnabled && visualType !=  Toolkit::DevelVisual::WIREFRAME )
239   {
240     //Create a WireframeVisual if we have debug enabled
241     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr, propertyMap );
242   }
243
244   return Toolkit::Visual::Base( visualPtr.Get() );
245 }
246
247 Toolkit::Visual::Base VisualFactory::CreateVisual( const Image& image )
248 {
249   if( !mFactoryCache )
250   {
251     mFactoryCache = new VisualFactoryCache();
252   }
253
254   Visual::BasePtr visualPtr;
255
256   NinePatchImage npatchImage = NinePatchImage::DownCast( image );
257   if( npatchImage )
258   {
259     visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), npatchImage );
260   }
261   else
262   {
263     visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), image );
264   }
265
266   if( mDebugEnabled )
267   {
268     //Create a WireframeVisual if we have debug enabled
269     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr );
270   }
271
272   return Toolkit::Visual::Base( visualPtr.Get() );
273 }
274
275 Toolkit::Visual::Base VisualFactory::CreateVisual( const std::string& url, ImageDimensions size )
276 {
277   if( !mFactoryCache )
278   {
279     mFactoryCache = new VisualFactoryCache();
280   }
281
282   Visual::BasePtr visualPtr;
283
284   // first resolve url type to know which visual to create
285   VisualUrl visualUrl( url );
286   switch( visualUrl.GetType() )
287   {
288     case VisualUrl::N_PATCH:
289     {
290       visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), visualUrl );
291       break;
292     }
293     case VisualUrl::SVG:
294     {
295       visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), visualUrl );
296       break;
297     }
298     case VisualUrl::GIF:
299     {
300       visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), visualUrl );
301       break;
302     }
303     case VisualUrl::REGULAR_IMAGE:
304     {
305       visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), visualUrl, size );
306       break;
307     }
308   }
309
310   if( mDebugEnabled )
311   {
312     //Create a WireframeVisual if we have debug enabled
313     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr );
314   }
315
316   return Toolkit::Visual::Base( visualPtr.Get() );
317 }
318
319 } // namespace Internal
320
321 } // namespace Toolkit
322
323 } // namespace Dali