Merge "Change the double tap and long press events on top of no text actions to highl...
[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-factory-resolve-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 && imageURLValue->Get( imageUrl ) )
123       {
124         // first resolve url type to know which visual to create
125         UrlType::Type type = ResolveUrlType( imageUrl );
126         if( UrlType::N_PATCH == type )
127         {
128           visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
129         }
130         else if( UrlType::SVG == type )
131         {
132           visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
133         }
134         else if( UrlType::GIF == type )
135         {
136           visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
137         }
138         else // Regular image
139         {
140           visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
141         }
142       }
143
144       break;
145     }
146
147     case Toolkit::Visual::MESH:
148     {
149       visualPtr = MeshVisual::New( *( mFactoryCache.Get() ), propertyMap );
150       break;
151     }
152
153     case Toolkit::Visual::PRIMITIVE:
154     {
155       visualPtr = PrimitiveVisual::New( *( mFactoryCache.Get() ), propertyMap );
156       break;
157     }
158
159     case Toolkit::Visual::WIREFRAME:
160     {
161       visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), propertyMap );
162       break;
163     }
164
165     case Toolkit::DevelVisual::TEXT:
166     {
167       visualPtr = TextVisual::New( *( mFactoryCache.Get() ), propertyMap );
168       break;
169     }
170
171     case Toolkit::DevelVisual::N_PATCH:
172     {
173       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
174       std::string imageUrl;
175       if( imageURLValue && imageURLValue->Get( imageUrl ) )
176       {
177         visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
178       }
179       break;
180     }
181
182     case Toolkit::DevelVisual::SVG:
183     {
184       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
185       std::string imageUrl;
186       if( imageURLValue && imageURLValue->Get( imageUrl ) )
187       {
188         visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
189       }
190       break;
191     }
192
193     case Toolkit::DevelVisual::ANIMATED_IMAGE:
194     {
195       Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
196       std::string imageUrl;
197       if( imageURLValue && imageURLValue->Get( imageUrl ) )
198       {
199         visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
200       }
201       break;
202     }
203   }
204
205   if( !visualPtr )
206   {
207     DALI_LOG_ERROR( "Renderer type unknown\n" );
208   }
209
210   if( mDebugEnabled && visualType !=  Toolkit::DevelVisual::WIREFRAME )
211   {
212     //Create a WireframeVisual if we have debug enabled
213     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr, propertyMap );
214   }
215
216   return Toolkit::Visual::Base( visualPtr.Get() );
217 }
218
219 Toolkit::Visual::Base VisualFactory::CreateVisual( const Image& image )
220 {
221   if( !mFactoryCache )
222   {
223     mFactoryCache = new VisualFactoryCache();
224   }
225
226   Visual::BasePtr visualPtr;
227
228   NinePatchImage npatchImage = NinePatchImage::DownCast( image );
229   if( npatchImage )
230   {
231     visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), npatchImage );
232   }
233   else
234   {
235     visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), image );
236   }
237
238   if( mDebugEnabled )
239   {
240     //Create a WireframeVisual if we have debug enabled
241     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr );
242   }
243
244   return Toolkit::Visual::Base( visualPtr.Get() );
245 }
246
247 Toolkit::Visual::Base VisualFactory::CreateVisual( const std::string& url, ImageDimensions size )
248 {
249   if( !mFactoryCache )
250   {
251     mFactoryCache = new VisualFactoryCache();
252   }
253
254   Visual::BasePtr visualPtr;
255
256   // first resolve url type to know which visual to create
257   UrlType::Type type = ResolveUrlType( url );
258   if( UrlType::N_PATCH == type )
259   {
260     visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), url );
261   }
262   else if( UrlType::SVG == type )
263   {
264     visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), url );
265   }
266   else if( UrlType::GIF == type )
267   {
268     visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), url );
269   }
270   else // Regular image
271   {
272     visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), url, size );
273   }
274
275   if( mDebugEnabled )
276   {
277     //Create a WireframeVisual if we have debug enabled
278     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr );
279   }
280
281   return Toolkit::Visual::Base( visualPtr.Get() );
282 }
283
284 } // namespace Internal
285
286 } // namespace Toolkit
287
288 } // namespace Dali