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( ACTOR_BEHAVIOUR_NONE ) )
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 InitializeVisual( self, mVisual, image );
86 mImageSize = image ? ImageDimensions( image.GetWidth(), image.GetHeight() ) : ImageDimensions( 0, 0 );
92 void ImageView::SetImage( Property::Map map )
99 InitializeVisual( self, mVisual, mPropertyMap );
101 Property::Value* widthValue = mPropertyMap.Find( "width" );
105 if( widthValue->Get( width ) )
107 mImageSize = ImageDimensions( width, mImageSize.GetHeight() );
111 Property::Value* heightValue = mPropertyMap.Find( "height" );
115 if( heightValue->Get( height ) )
117 mImageSize = ImageDimensions( mImageSize.GetWidth(), height );
124 void ImageView::SetImage( const std::string& url, ImageDimensions size )
126 if( ( mUrl != url ) ||
127 mImage || // If we're changing from an Image type to a URL type
128 ! mPropertyMap.Empty() ) // If we're changing from a property map type to a URL type
131 mPropertyMap.Clear();
135 if( size.GetWidth() != 0u && size.GetHeight() != 0u )
140 Actor self( Self() );
141 InitializeVisual( self, mVisual, url, size );
143 mVisual.SetSize( mSizeSet );
149 Image ImageView::GetImage() const
154 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
158 Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
162 bool ImageView::IsPreMultipliedAlphaEnabled() const
166 return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
171 void ImageView::SetDepthIndex( int depthIndex )
175 mVisual.SetDepthIndex( depthIndex );
179 Vector3 ImageView::GetNaturalSize()
183 Vector2 rendererNaturalSize;
184 mVisual.GetNaturalSize( rendererNaturalSize );
185 return Vector3( rendererNaturalSize );
189 size.x = mImageSize.GetWidth();
190 size.y = mImageSize.GetHeight();
192 if( size.x > 0 && size.y > 0 )
194 size.z = std::min(size.x, size.y);
199 // if no image then use Control's natural size
200 return Control::GetNaturalSize();
204 float ImageView::GetHeightForWidth( float width )
206 if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
208 return GetHeightForWidthBase( width );
212 return Control::GetHeightForWidth( width );
216 float ImageView::GetWidthForHeight( float height )
218 if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
220 return GetWidthForHeightBase( height );
224 return Control::GetWidthForHeight( height );
229 ///////////////////////////////////////////////////////////
234 void ImageView::OnStageConnection( int depth )
236 Control::OnStageConnection( depth );
240 CustomActor self = Self();
241 mVisual.SetOnStage( self );
245 void ImageView::OnStageDisconnection()
249 CustomActor self = Self();
250 mVisual.SetOffStage( self );
253 Control::OnStageDisconnection();
256 void ImageView::OnSizeSet( const Vector3& targetSize )
258 Control::OnSizeSet( targetSize );
259 mSizeSet = targetSize;
263 Vector2 size( targetSize );
264 mVisual.SetSize( size );
268 ///////////////////////////////////////////////////////////
273 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
275 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
279 ImageView& impl = GetImpl( imageView );
282 case Toolkit::ImageView::Property::RESOURCE_URL:
284 std::string imageUrl;
285 if( value.Get( imageUrl ) )
287 impl.SetImage( imageUrl, ImageDimensions() );
292 case Toolkit::ImageView::Property::IMAGE:
294 std::string imageUrl;
296 if( value.Get( imageUrl ) )
298 impl.SetImage( imageUrl, ImageDimensions() );
300 // if its not a string then get a Property::Map from the property if possible.
301 else if( value.Get( map ) )
303 Property::Value* shaderValue = map.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
304 // set image only if property map contains image information other than custom shader
305 if( map.Count() > 1u || !shaderValue )
307 impl.SetImage( map );
309 // the property map contains only the custom shader
310 else if( impl.mVisual && map.Count() == 1u && shaderValue )
312 Property::Map shaderMap;
313 if( shaderValue->Get( shaderMap ) )
315 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
316 visual.SetCustomShader( shaderMap );
317 if( imageView.OnStage() )
319 // force to create new core renderer to use the newly set shader
320 visual.SetOffStage( imageView );
321 visual.SetOnStage( imageView );
329 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
332 if( value.Get( isPre ) )
334 impl.EnablePreMultipliedAlpha( isPre );
342 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
344 Property::Value value;
346 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
350 ImageView& impl = GetImpl( imageview );
351 switch ( propertyIndex )
353 case Toolkit::ImageView::Property::RESOURCE_URL:
355 if ( !impl.mUrl.empty() )
362 case Toolkit::ImageView::Property::IMAGE:
364 if ( !impl.mUrl.empty() )
368 else if( impl.mImage )
371 Scripting::CreatePropertyMap( impl.mImage, map );
374 else if( !impl.mPropertyMap.Empty() )
376 value = impl.mPropertyMap;
381 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
383 value = impl.IsPreMultipliedAlphaEnabled();
392 } // namespace Internal
393 } // namespace Toolkit