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