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