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