2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
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>
29 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
30 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
31 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
32 #include <dali-toolkit/public-api/visuals/visual-properties.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/animated-gradient/animated-gradient-visual.h>
37 #include <dali-toolkit/internal/visuals/image/image-visual.h>
38 #include <dali-toolkit/internal/visuals/mesh/mesh-visual.h>
39 #include <dali-toolkit/internal/visuals/npatch/npatch-visual.h>
40 #include <dali-toolkit/internal/visuals/primitive/primitive-visual.h>
41 #include <dali-toolkit/internal/visuals/svg/svg-visual.h>
42 #include <dali-toolkit/internal/visuals/text/text-visual.h>
43 #include <dali-toolkit/internal/visuals/animated-image/animated-image-visual.h>
44 #include <dali-toolkit/internal/visuals/wireframe/wireframe-visual.h>
45 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
46 #include <dali-toolkit/internal/visuals/visual-url.h>
47 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
63 BaseHandle handle = Toolkit::VisualFactory::Get();
68 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::VisualFactory, Dali::BaseHandle, Create, true )
69 DALI_TYPE_REGISTRATION_END()
73 VisualFactory::VisualFactory( bool debugEnabled )
74 :mDebugEnabled( debugEnabled )
78 VisualFactory::~VisualFactory()
82 Toolkit::Visual::Base VisualFactory::CreateVisual( const Property::Map& propertyMap )
84 // Create factory cache if it hasn't already been
87 mFactoryCache = new VisualFactoryCache();
90 Visual::BasePtr visualPtr;
92 Property::Value* typeValue = propertyMap.Find( Toolkit::Visual::Property::TYPE, VISUAL_TYPE );
93 Toolkit::DevelVisual::Type visualType = Toolkit::DevelVisual::IMAGE; // Default to IMAGE type.
96 Scripting::GetEnumerationProperty( *typeValue, VISUAL_TYPE_TABLE, VISUAL_TYPE_TABLE_COUNT, visualType );
101 case Toolkit::Visual::BORDER:
103 visualPtr = BorderVisual::New( *( mFactoryCache.Get() ), propertyMap );
107 case Toolkit::Visual::COLOR:
109 visualPtr = ColorVisual::New( *( mFactoryCache.Get() ), propertyMap );
113 case Toolkit::Visual::GRADIENT:
115 visualPtr = GradientVisual::New( *( mFactoryCache.Get() ), propertyMap );
119 case Toolkit::Visual::IMAGE:
121 Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
122 std::string imageUrl;
125 if( imageURLValue->Get( imageUrl ) )
127 if( !imageUrl.empty() )
129 VisualUrl visualUrl( imageUrl );
131 switch( visualUrl.GetType() )
133 case VisualUrl::N_PATCH:
135 visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
140 visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
145 visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
148 case VisualUrl::REGULAR_IMAGE:
150 visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
158 Property::Array* array = imageURLValue->GetArray();
161 visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), *array, propertyMap );
168 case Toolkit::Visual::MESH:
170 visualPtr = MeshVisual::New( *( mFactoryCache.Get() ), propertyMap );
174 case Toolkit::Visual::PRIMITIVE:
176 visualPtr = PrimitiveVisual::New( *( mFactoryCache.Get() ), propertyMap );
180 case Toolkit::Visual::WIREFRAME:
182 visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), propertyMap );
186 case Toolkit::Visual::TEXT:
188 visualPtr = TextVisual::New( *( mFactoryCache.Get() ), propertyMap );
192 case Toolkit::Visual::N_PATCH:
194 Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
195 std::string imageUrl;
196 if( imageURLValue && imageURLValue->Get( imageUrl ) )
198 visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
203 case Toolkit::Visual::SVG:
205 Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
206 std::string imageUrl;
207 if( imageURLValue && imageURLValue->Get( imageUrl ) )
209 visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
214 case Toolkit::Visual::ANIMATED_IMAGE:
216 Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
217 std::string imageUrl;
220 if( imageURLValue->Get( imageUrl ) )
222 visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
226 Property::Array* array = imageURLValue->GetArray();
229 visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), *array, propertyMap );
236 case Toolkit::DevelVisual::ANIMATED_GRADIENT:
238 visualPtr = AnimatedGradientVisual::New( *( mFactoryCache.Get() ), propertyMap );
245 DALI_LOG_ERROR( "Renderer type unknown\n" );
248 if( mDebugEnabled && visualType != Toolkit::DevelVisual::WIREFRAME )
250 //Create a WireframeVisual if we have debug enabled
251 visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr, propertyMap );
254 return Toolkit::Visual::Base( visualPtr.Get() );
257 Toolkit::Visual::Base VisualFactory::CreateVisual( const Image& image )
261 mFactoryCache = new VisualFactoryCache();
264 Visual::BasePtr visualPtr;
268 NinePatchImage npatchImage = NinePatchImage::DownCast( image );
271 visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), npatchImage );
275 visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), image );
281 //Create a WireframeVisual if we have debug enabled
282 visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr );
285 return Toolkit::Visual::Base( visualPtr.Get() );
288 Toolkit::Visual::Base VisualFactory::CreateVisual( const std::string& url, ImageDimensions size )
292 mFactoryCache = new VisualFactoryCache();
295 Visual::BasePtr visualPtr;
299 // first resolve url type to know which visual to create
300 VisualUrl visualUrl( url );
301 switch( visualUrl.GetType() )
303 case VisualUrl::N_PATCH:
305 visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), visualUrl );
310 visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), visualUrl );
315 visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), visualUrl );
318 case VisualUrl::REGULAR_IMAGE:
320 visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), visualUrl, size );
328 //Create a WireframeVisual if we have debug enabled
329 visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr );
332 return Toolkit::Visual::Base( visualPtr.Get() );
335 Internal::TextureManager& VisualFactory::GetTextureManager()
339 mFactoryCache = new VisualFactoryCache();
341 return mFactoryCache->GetTextureManager();
344 } // namespace Internal
346 } // namespace Toolkit