Merge "Added Tooltip functionality to Control" into devel/master
[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/batch-image-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 const char * const BATCHING_ENABLED( "batchingEnabled" );
61 BaseHandle Create()
62 {
63   BaseHandle handle = Toolkit::VisualFactory::Get();
64
65   return handle;
66 }
67
68 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::VisualFactory, Dali::BaseHandle, Create, true )
69 DALI_TYPE_REGISTRATION_END()
70
71 } // namespace
72
73 VisualFactory::VisualFactory( bool debugEnabled )
74 :mDebugEnabled( debugEnabled )
75 {
76 }
77
78 VisualFactory::~VisualFactory()
79 {
80 }
81
82 Toolkit::Visual::Base VisualFactory::CreateVisual( const Property::Map& propertyMap )
83 {
84   // Create factory cache if it hasn't already been
85   if( !mFactoryCache )
86   {
87     mFactoryCache = new VisualFactoryCache();
88   }
89
90   Visual::BasePtr visualPtr;
91
92   if( mDebugEnabled )
93   {
94     //Create a WireframeVisual if we have debug enabled
95     visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ) );
96   }
97   else
98   {
99     Property::Value* typeValue = propertyMap.Find( Toolkit::DevelVisual::Property::TYPE, VISUAL_TYPE );
100     Toolkit::DevelVisual::Type visualType = Toolkit::DevelVisual::IMAGE; // Default to IMAGE type.
101     if( typeValue )
102     {
103       Scripting::GetEnumerationProperty( *typeValue, VISUAL_TYPE_TABLE, VISUAL_TYPE_TABLE_COUNT, visualType );
104     }
105
106     switch( visualType )
107     {
108       case Toolkit::Visual::BORDER:
109       {
110         visualPtr = BorderVisual::New( *( mFactoryCache.Get() ), propertyMap );
111         break;
112       }
113
114       case Toolkit::Visual::COLOR:
115       {
116         visualPtr = ColorVisual::New( *( mFactoryCache.Get() ), propertyMap );
117         break;
118       }
119
120       case Toolkit::Visual::GRADIENT:
121       {
122         visualPtr = GradientVisual::New( *( mFactoryCache.Get() ), propertyMap );
123         break;
124       }
125
126       case Toolkit::Visual::IMAGE:
127       {
128         Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
129         std::string imageUrl;
130         if( imageURLValue && imageURLValue->Get( imageUrl ) )
131         {
132           // first resolve url type to know which visual to create
133           UrlType::Type type = ResolveUrlType( imageUrl );
134           if( UrlType::N_PATCH == type )
135           {
136             visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
137           }
138           else if( UrlType::SVG == type )
139           {
140             visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
141           }
142           else if( UrlType::GIF == type )
143           {
144             visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
145           }
146           else // Regular image
147           {
148             bool batchingEnabled( false );
149             Property::Value* batchingEnabledValue = propertyMap.Find( Toolkit::ImageVisual::Property::BATCHING_ENABLED, BATCHING_ENABLED );
150             if( batchingEnabledValue  )
151             {
152               batchingEnabledValue->Get( batchingEnabled );
153             }
154
155             if( batchingEnabled )
156             {
157               visualPtr = BatchImageVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
158             }
159             else
160             {
161               visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
162             }
163           }
164         }
165
166         break;
167       }
168
169       case Toolkit::Visual::MESH:
170       {
171         visualPtr = MeshVisual::New( *( mFactoryCache.Get() ), propertyMap );
172         break;
173       }
174
175       case Toolkit::Visual::PRIMITIVE:
176       {
177         visualPtr = PrimitiveVisual::New( *( mFactoryCache.Get() ), propertyMap );
178         break;
179       }
180
181       case Toolkit::Visual::WIREFRAME:
182       {
183         visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ) );
184         break;
185       }
186
187       case Toolkit::DevelVisual::TEXT:
188       {
189         visualPtr = TextVisual::New( *( mFactoryCache.Get() ), propertyMap );
190         break;
191       }
192     }
193   }
194
195   if( !visualPtr )
196   {
197     DALI_LOG_ERROR( "Renderer type unknown\n" );
198   }
199
200   return Toolkit::Visual::Base( visualPtr.Get() );
201 }
202
203 Toolkit::Visual::Base VisualFactory::CreateVisual( const Image& image )
204 {
205   if( !mFactoryCache )
206   {
207     mFactoryCache = new VisualFactoryCache();
208   }
209
210   if( mDebugEnabled )
211   {
212     return Toolkit::Visual::Base( WireframeVisual::New( *( mFactoryCache.Get() ) ).Get() );
213   }
214
215   Visual::BasePtr visualPtr;
216
217   NinePatchImage npatchImage = NinePatchImage::DownCast( image );
218   if( npatchImage )
219   {
220     visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), npatchImage );
221   }
222   else
223   {
224     visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), image );
225   }
226
227   return Toolkit::Visual::Base( visualPtr.Get() );
228 }
229
230 Toolkit::Visual::Base VisualFactory::CreateVisual( const std::string& url, ImageDimensions size )
231 {
232   if( !mFactoryCache )
233   {
234     mFactoryCache = new VisualFactoryCache();
235   }
236
237   if( mDebugEnabled )
238   {
239     return Toolkit::Visual::Base( WireframeVisual::New( *( mFactoryCache.Get() ) ).Get() );
240   }
241
242   Visual::BasePtr visualPtr;
243
244   // first resolve url type to know which visual to create
245   UrlType::Type type = ResolveUrlType( url );
246   if( UrlType::N_PATCH == type )
247   {
248     visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), url );
249   }
250   else if( UrlType::SVG == type )
251   {
252     visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), url );
253   }
254   else if( UrlType::GIF == type )
255   {
256     visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), url );
257   }
258   else // Regular image
259   {
260     visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), url, size );
261   }
262
263   return Toolkit::Visual::Base( visualPtr.Get() );
264 }
265
266 } // namespace Internal
267
268 } // namespace Toolkit
269
270 } // namespace Dali