Merge "Support to reset the image in ImageView" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-factory-impl.cpp
1  /*
2  * Copyright (c) 2017 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/public-api/visuals/text-visual-properties.h>
31 #include <dali-toolkit/public-api/visuals/visual-properties.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-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::Visual::Property::TYPE, VISUAL_TYPE );
91   Toolkit::Visual::Type visualType = Toolkit::Visual::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 )
122       {
123         if( imageURLValue->Get( imageUrl ) )
124         {
125           if( !imageUrl.empty() )
126           {
127             VisualUrl visualUrl( imageUrl );
128
129             switch( visualUrl.GetType() )
130             {
131               case VisualUrl::N_PATCH:
132               {
133                 visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
134                 break;
135               }
136               case VisualUrl::SVG:
137               {
138                 visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
139                 break;
140               }
141               case VisualUrl::GIF:
142               {
143                 visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
144                 break;
145               }
146               case VisualUrl::REGULAR_IMAGE:
147               {
148                 visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
149                 break;
150               }
151             }
152           }
153         }
154         else
155         {
156           Property::Array* array = imageURLValue->GetArray();
157           if( array )
158           {
159             visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), *array, propertyMap );
160           }
161         }
162       }
163       break;
164     }
165
166     case Toolkit::Visual::MESH:
167     {
168       visualPtr = MeshVisual::New( *( mFactoryCache.Get() ), propertyMap );
169       break;
170     }
171
172     case Toolkit::Visual::PRIMITIVE:
173     {
174       visualPtr = PrimitiveVisual::New( *( mFactoryCache.Get() ), propertyMap );
175       break;
176     }
177
178     case Toolkit::Visual::WIREFRAME:
179     {
180       visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), propertyMap );
181       break;
182     }
183
184     case Toolkit::Visual::TEXT:
185     {
186       visualPtr = TextVisual::New( *( mFactoryCache.Get() ), propertyMap );
187       break;
188     }
189
190     case Toolkit::Visual::N_PATCH:
191     {
192       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
193       std::string imageUrl;
194       if( imageURLValue && imageURLValue->Get( imageUrl ) )
195       {
196         visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
197       }
198       break;
199     }
200
201     case Toolkit::Visual::SVG:
202     {
203       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
204       std::string imageUrl;
205       if( imageURLValue && imageURLValue->Get( imageUrl ) )
206       {
207         visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
208       }
209       break;
210     }
211
212     case Toolkit::Visual::ANIMATED_IMAGE:
213     {
214       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
215       std::string imageUrl;
216       if( imageURLValue )
217       {
218         if( imageURLValue->Get( imageUrl ) )
219         {
220           visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
221         }
222         else
223         {
224           Property::Array* array = imageURLValue->GetArray();
225           if( array )
226           {
227             visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), *array, propertyMap );
228           }
229         }
230       }
231       break;
232     }
233   }
234
235   if( !visualPtr )
236   {
237     DALI_LOG_ERROR( "Renderer type unknown\n" );
238   }
239
240   if( mDebugEnabled && visualType !=  Toolkit::Visual::WIREFRAME )
241   {
242     //Create a WireframeVisual if we have debug enabled
243     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr, propertyMap );
244   }
245
246   return Toolkit::Visual::Base( visualPtr.Get() );
247 }
248
249 Toolkit::Visual::Base VisualFactory::CreateVisual( const Image& image )
250 {
251   if( !mFactoryCache )
252   {
253     mFactoryCache = new VisualFactoryCache();
254   }
255
256   Visual::BasePtr visualPtr;
257
258   if( image )
259   {
260     NinePatchImage npatchImage = NinePatchImage::DownCast( image );
261     if( npatchImage )
262     {
263       visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), npatchImage );
264     }
265     else
266     {
267       visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), image );
268     }
269   }
270
271   if( mDebugEnabled )
272   {
273     //Create a WireframeVisual if we have debug enabled
274     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr );
275   }
276
277   return Toolkit::Visual::Base( visualPtr.Get() );
278 }
279
280 Toolkit::Visual::Base VisualFactory::CreateVisual( const std::string& url, ImageDimensions size )
281 {
282   if( !mFactoryCache )
283   {
284     mFactoryCache = new VisualFactoryCache();
285   }
286
287   Visual::BasePtr visualPtr;
288
289   if( !url.empty() )
290   {
291     // first resolve url type to know which visual to create
292     VisualUrl visualUrl( url );
293     switch( visualUrl.GetType() )
294     {
295       case VisualUrl::N_PATCH:
296       {
297         visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), visualUrl );
298         break;
299       }
300       case VisualUrl::SVG:
301       {
302         visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), visualUrl );
303         break;
304       }
305       case VisualUrl::GIF:
306       {
307         visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), visualUrl );
308         break;
309       }
310       case VisualUrl::REGULAR_IMAGE:
311       {
312         visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), visualUrl, size );
313         break;
314       }
315     }
316   }
317
318   if( mDebugEnabled )
319   {
320     //Create a WireframeVisual if we have debug enabled
321     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr );
322   }
323
324   return Toolkit::Visual::Base( visualPtr.Get() );
325 }
326
327 Internal::TextureManager& VisualFactory::GetTextureManager()
328 {
329   if( !mFactoryCache )
330   {
331     mFactoryCache = new VisualFactoryCache();
332   }
333   return mFactoryCache->GetTextureManager();
334 }
335
336 } // namespace Internal
337
338 } // namespace Toolkit
339
340 } // namespace Dali