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/public-api/visuals/image-visual-properties.h>
30 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
31 #include <dali-toolkit/public-api/visuals/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/image-visual.h>
36 #include <dali-toolkit/internal/visuals/mesh/mesh-visual.h>
37 #include <dali-toolkit/internal/visuals/npatch/npatch-visual.h>
38 #include <dali-toolkit/internal/visuals/primitive/primitive-visual.h>
39 #include <dali-toolkit/internal/visuals/svg/svg-visual.h>
40 #include <dali-toolkit/internal/visuals/text/text-visual.h>
41 #include <dali-toolkit/internal/visuals/animated-image/animated-image-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-url.h>
45 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
61 BaseHandle handle = Toolkit::VisualFactory::Get();
66 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::VisualFactory, Dali::BaseHandle, Create, true )
67 DALI_TYPE_REGISTRATION_END()
71 VisualFactory::VisualFactory( bool debugEnabled )
72 :mDebugEnabled( debugEnabled )
76 VisualFactory::~VisualFactory()
80 Toolkit::Visual::Base VisualFactory::CreateVisual( const Property::Map& propertyMap )
82 // Create factory cache if it hasn't already been
85 mFactoryCache = new VisualFactoryCache();
88 Visual::BasePtr visualPtr;
90 Property::Value* typeValue = propertyMap.Find( Toolkit::Visual::Property::TYPE, VISUAL_TYPE );
91 Toolkit::Visual::Type visualType = Toolkit::Visual::IMAGE; // Default to IMAGE type.
94 Scripting::GetEnumerationProperty( *typeValue, VISUAL_TYPE_TABLE, VISUAL_TYPE_TABLE_COUNT, visualType );
99 case Toolkit::Visual::BORDER:
101 visualPtr = BorderVisual::New( *( mFactoryCache.Get() ), propertyMap );
105 case Toolkit::Visual::COLOR:
107 visualPtr = ColorVisual::New( *( mFactoryCache.Get() ), propertyMap );
111 case Toolkit::Visual::GRADIENT:
113 visualPtr = GradientVisual::New( *( mFactoryCache.Get() ), propertyMap );
117 case Toolkit::Visual::IMAGE:
119 Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
120 std::string imageUrl;
123 if( imageURLValue->Get( imageUrl ) )
125 VisualUrl visualUrl( imageUrl );
127 switch( visualUrl.GetType() )
129 case VisualUrl::N_PATCH:
131 visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
136 visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
141 visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
144 case VisualUrl::REGULAR_IMAGE:
146 visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), visualUrl, propertyMap );
153 Property::Array* array = imageURLValue->GetArray();
156 visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), *array, propertyMap );
163 case Toolkit::Visual::MESH:
165 visualPtr = MeshVisual::New( *( mFactoryCache.Get() ), propertyMap );
169 case Toolkit::Visual::PRIMITIVE:
171 visualPtr = PrimitiveVisual::New( *( mFactoryCache.Get() ), propertyMap );
175 case Toolkit::Visual::WIREFRAME:
177 visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), propertyMap );
181 case Toolkit::Visual::TEXT:
183 visualPtr = TextVisual::New( *( mFactoryCache.Get() ), propertyMap );
187 case Toolkit::Visual::N_PATCH:
189 Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
190 std::string imageUrl;
191 if( imageURLValue && imageURLValue->Get( imageUrl ) )
193 visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
198 case Toolkit::Visual::SVG:
200 Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
201 std::string imageUrl;
202 if( imageURLValue && imageURLValue->Get( imageUrl ) )
204 visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
209 case Toolkit::Visual::ANIMATED_IMAGE:
211 Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
212 std::string imageUrl;
215 if( imageURLValue->Get( imageUrl ) )
217 visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), imageUrl, propertyMap );
221 Property::Array* array = imageURLValue->GetArray();
224 visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), *array, propertyMap );
234 DALI_LOG_ERROR( "Renderer type unknown\n" );
237 if( mDebugEnabled && visualType != Toolkit::Visual::WIREFRAME )
239 //Create a WireframeVisual if we have debug enabled
240 visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr, propertyMap );
243 return Toolkit::Visual::Base( visualPtr.Get() );
246 Toolkit::Visual::Base VisualFactory::CreateVisual( const Image& image )
250 mFactoryCache = new VisualFactoryCache();
253 Visual::BasePtr visualPtr;
255 NinePatchImage npatchImage = NinePatchImage::DownCast( image );
258 visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), npatchImage );
262 visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), image );
267 //Create a WireframeVisual if we have debug enabled
268 visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr );
271 return Toolkit::Visual::Base( visualPtr.Get() );
274 Toolkit::Visual::Base VisualFactory::CreateVisual( const std::string& url, ImageDimensions size )
278 mFactoryCache = new VisualFactoryCache();
281 Visual::BasePtr visualPtr;
283 // first resolve url type to know which visual to create
284 VisualUrl visualUrl( url );
285 switch( visualUrl.GetType() )
287 case VisualUrl::N_PATCH:
289 visualPtr = NPatchVisual::New( *( mFactoryCache.Get() ), visualUrl );
294 visualPtr = SvgVisual::New( *( mFactoryCache.Get() ), visualUrl );
299 visualPtr = AnimatedImageVisual::New( *( mFactoryCache.Get() ), visualUrl );
302 case VisualUrl::REGULAR_IMAGE:
304 visualPtr = ImageVisual::New( *( mFactoryCache.Get() ), visualUrl, size );
311 //Create a WireframeVisual if we have debug enabled
312 visualPtr = WireframeVisual::New( *( mFactoryCache.Get() ), visualPtr );
315 return Toolkit::Visual::Base( visualPtr.Get() );
318 Internal::TextureManager& VisualFactory::GetTextureManager()
322 mFactoryCache = new VisualFactoryCache();
324 return mFactoryCache->GetTextureManager();
327 } // namespace Internal
329 } // namespace Toolkit