1 // Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 #include "image-view-impl.h"
7 #include <dali/public-api/images/resource-image.h>
8 #include <dali/public-api/object/type-registry.h>
9 #include <dali/public-api/object/type-registry-helper.h>
10 #include <dali/devel-api/scripting/scripting.h>
13 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
14 #include <dali-toolkit/public-api/visuals/visual-properties.h>
15 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
16 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
17 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
33 return Toolkit::ImageView::New();
36 // Setup properties, signals and actions using the type-registry.
37 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ImageView, Toolkit::Control, Create );
38 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "resourceUrl", STRING, RESOURCE_URL )
39 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "image", MAP, IMAGE )
40 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "preMultipliedAlpha", BOOLEAN, PRE_MULTIPLIED_ALPHA )
42 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT( Toolkit, ImageView, "pixelArea", Vector4(0.f, 0.f, 1.f, 1.f), PIXEL_AREA )
43 DALI_TYPE_REGISTRATION_END()
45 } // anonymous namespace
49 ImageView::ImageView()
50 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) )
54 ImageView::~ImageView()
58 Toolkit::ImageView ImageView::New()
60 ImageView* impl = new ImageView();
62 Toolkit::ImageView handle = Toolkit::ImageView( *impl );
64 // Second-phase init of the implementation
65 // This can only be done after the CustomActor connection has been made...
71 /////////////////////////////////////////////////////////////
73 void ImageView::SetImage( Image image )
75 if( ( mImage != image ) ||
76 ! mUrl.empty() || // If we're changing from a URL type to an Image type
77 ! mPropertyMap.Empty() ) // If we're changing from a property map type to an Image type
85 mVisual = Toolkit::VisualFactory::Get().CreateVisual( image );
86 RegisterVisual( Toolkit::ImageView::Property::IMAGE, self, mVisual );
87 mImageSize = image ? ImageDimensions( image.GetWidth(), image.GetHeight() ) : ImageDimensions( 0, 0 );
93 void ImageView::SetImage( Property::Map map )
100 mVisual = Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
101 RegisterVisual( Toolkit::ImageView::Property::IMAGE, self, mVisual );
103 Property::Value* widthValue = mPropertyMap.Find( "width" );
107 if( widthValue->Get( width ) )
109 mImageSize = ImageDimensions( width, mImageSize.GetHeight() );
113 Property::Value* heightValue = mPropertyMap.Find( "height" );
117 if( heightValue->Get( height ) )
119 mImageSize = ImageDimensions( mImageSize.GetWidth(), height );
126 void ImageView::SetImage( const std::string& url, ImageDimensions size )
128 if( ( mUrl != url ) ||
129 mImage || // If we're changing from an Image type to a URL type
130 ! mPropertyMap.Empty() ) // If we're changing from a property map type to a URL type
133 mPropertyMap.Clear();
137 if( size.GetWidth() != 0u && size.GetHeight() != 0u )
142 Actor self( Self() );
143 mVisual = Toolkit::VisualFactory::Get().CreateVisual( url, size );
144 RegisterVisual( Toolkit::ImageView::Property::IMAGE, self, mVisual );
146 mVisual.SetSize( mSizeSet );
152 Image ImageView::GetImage() const
157 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
161 Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
165 bool ImageView::IsPreMultipliedAlphaEnabled() const
169 return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
174 void ImageView::SetDepthIndex( int depthIndex )
178 mVisual.SetDepthIndex( depthIndex );
182 Vector3 ImageView::GetNaturalSize()
186 Vector2 rendererNaturalSize;
187 mVisual.GetNaturalSize( rendererNaturalSize );
188 return Vector3( rendererNaturalSize );
192 size.x = mImageSize.GetWidth();
193 size.y = mImageSize.GetHeight();
195 if( size.x > 0 && size.y > 0 )
197 size.z = std::min(size.x, size.y);
202 // if no image then use Control's natural size
203 return Control::GetNaturalSize();
207 float ImageView::GetHeightForWidth( float width )
209 if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
211 return GetHeightForWidthBase( width );
215 return Control::GetHeightForWidth( width );
219 float ImageView::GetWidthForHeight( float height )
221 if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
223 return GetWidthForHeightBase( height );
227 return Control::GetWidthForHeight( height );
232 ///////////////////////////////////////////////////////////
237 void ImageView::OnSizeSet( const Vector3& targetSize )
239 Control::OnSizeSet( targetSize );
240 mSizeSet = targetSize;
244 Vector2 size( targetSize );
245 mVisual.SetSize( size );
249 ///////////////////////////////////////////////////////////
254 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
256 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
260 ImageView& impl = GetImpl( imageView );
263 case Toolkit::ImageView::Property::RESOURCE_URL:
265 std::string imageUrl;
266 if( value.Get( imageUrl ) )
268 impl.SetImage( imageUrl, ImageDimensions() );
273 case Toolkit::ImageView::Property::IMAGE:
275 std::string imageUrl;
277 if( value.Get( imageUrl ) )
279 impl.SetImage( imageUrl, ImageDimensions() );
281 // if its not a string then get a Property::Map from the property if possible.
282 else if( value.Get( map ) )
284 Property::Value* shaderValue = map.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
285 // set image only if property map contains image information other than custom shader
286 if( map.Count() > 1u || !shaderValue )
288 impl.SetImage( map );
290 // the property map contains only the custom shader
291 else if( impl.mVisual && map.Count() == 1u && shaderValue )
293 Property::Map shaderMap;
294 if( shaderValue->Get( shaderMap ) )
296 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
297 visual.SetCustomShader( shaderMap );
298 if( imageView.OnStage() )
300 // force to create new core renderer to use the newly set shader
301 visual.SetOffStage( imageView );
302 visual.SetOnStage( imageView );
310 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
313 if( value.Get( isPre ) )
315 impl.EnablePreMultipliedAlpha( isPre );
323 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
325 Property::Value value;
327 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
331 ImageView& impl = GetImpl( imageview );
332 switch ( propertyIndex )
334 case Toolkit::ImageView::Property::RESOURCE_URL:
336 if ( !impl.mUrl.empty() )
343 case Toolkit::ImageView::Property::IMAGE:
345 if ( !impl.mUrl.empty() )
349 else if( impl.mImage )
352 Scripting::CreatePropertyMap( impl.mImage, map );
355 else if( !impl.mPropertyMap.Empty() )
357 value = impl.mPropertyMap;
362 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
364 value = impl.IsPreMultipliedAlphaEnabled();
373 } // namespace Internal
374 } // namespace Toolkit