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>
18 #include <dali-toolkit/devel-api/controls/control-devel.h>
34 return Toolkit::ImageView::New();
37 // Setup properties, signals and actions using the type-registry.
38 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ImageView, Toolkit::Control, Create );
39 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "resourceUrl", STRING, RESOURCE_URL )
40 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "image", MAP, IMAGE )
41 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "preMultipliedAlpha", BOOLEAN, PRE_MULTIPLIED_ALPHA )
43 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT( Toolkit, ImageView, "pixelArea", Vector4(0.f, 0.f, 1.f, 1.f), PIXEL_AREA )
44 DALI_TYPE_REGISTRATION_END()
46 } // anonymous namespace
50 ImageView::ImageView()
51 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) )
55 ImageView::~ImageView()
59 Toolkit::ImageView ImageView::New()
61 ImageView* impl = new ImageView();
63 Toolkit::ImageView handle = Toolkit::ImageView( *impl );
65 // Second-phase init of the implementation
66 // This can only be done after the CustomActor connection has been made...
72 /////////////////////////////////////////////////////////////
74 void ImageView::SetImage( Image image )
76 if( ( mImage != image ) ||
77 ! mUrl.empty() || // If we're changing from a URL type to an Image type
78 ! mPropertyMap.Empty() ) // If we're changing from a property map type to an Image type
86 InitializeVisual( self, mVisual, image );
87 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, mVisual );
88 mImageSize = image ? ImageDimensions( image.GetWidth(), image.GetHeight() ) : ImageDimensions( 0, 0 );
94 void ImageView::SetImage( Property::Map map )
100 Actor self( Self() );
101 InitializeVisual( self, mVisual, mPropertyMap );
102 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, mVisual );
104 Property::Value* widthValue = mPropertyMap.Find( "width" );
108 if( widthValue->Get( width ) )
110 mImageSize = ImageDimensions( width, mImageSize.GetHeight() );
114 Property::Value* heightValue = mPropertyMap.Find( "height" );
118 if( heightValue->Get( height ) )
120 mImageSize = ImageDimensions( mImageSize.GetWidth(), height );
127 void ImageView::SetImage( const std::string& url, ImageDimensions size )
129 if( ( mUrl != url ) ||
130 mImage || // If we're changing from an Image type to a URL type
131 ! mPropertyMap.Empty() ) // If we're changing from a property map type to a URL type
134 mPropertyMap.Clear();
138 if( size.GetWidth() != 0u && size.GetHeight() != 0u )
143 Actor self( Self() );
144 InitializeVisual( self, mVisual, url, size );
145 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, mVisual );
147 mVisual.SetSize( mSizeSet );
153 Image ImageView::GetImage() const
158 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
162 Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
166 bool ImageView::IsPreMultipliedAlphaEnabled() const
170 return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
175 void ImageView::SetDepthIndex( int depthIndex )
179 mVisual.SetDepthIndex( depthIndex );
183 Vector3 ImageView::GetNaturalSize()
187 Vector2 rendererNaturalSize;
188 mVisual.GetNaturalSize( rendererNaturalSize );
189 return Vector3( rendererNaturalSize );
193 size.x = mImageSize.GetWidth();
194 size.y = mImageSize.GetHeight();
196 if( size.x > 0 && size.y > 0 )
198 size.z = std::min(size.x, size.y);
203 // if no image then use Control's natural size
204 return Control::GetNaturalSize();
208 float ImageView::GetHeightForWidth( float width )
210 if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
212 return GetHeightForWidthBase( width );
216 return Control::GetHeightForWidth( width );
220 float ImageView::GetWidthForHeight( float height )
222 if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
224 return GetWidthForHeightBase( height );
228 return Control::GetWidthForHeight( height );
233 ///////////////////////////////////////////////////////////
238 void ImageView::OnStageConnection( int depth )
240 Control::OnStageConnection( depth );
244 CustomActor self = Self();
245 mVisual.SetOnStage( self );
249 void ImageView::OnStageDisconnection()
253 CustomActor self = Self();
254 mVisual.SetOffStage( self );
257 Control::OnStageDisconnection();
260 void ImageView::OnSizeSet( const Vector3& targetSize )
262 Control::OnSizeSet( targetSize );
263 mSizeSet = targetSize;
267 Vector2 size( targetSize );
268 mVisual.SetSize( size );
272 ///////////////////////////////////////////////////////////
277 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
279 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
283 ImageView& impl = GetImpl( imageView );
286 case Toolkit::ImageView::Property::RESOURCE_URL:
288 std::string imageUrl;
289 if( value.Get( imageUrl ) )
291 impl.SetImage( imageUrl, ImageDimensions() );
296 case Toolkit::ImageView::Property::IMAGE:
298 std::string imageUrl;
300 if( value.Get( imageUrl ) )
302 impl.SetImage( imageUrl, ImageDimensions() );
304 // if its not a string then get a Property::Map from the property if possible.
305 else if( value.Get( map ) )
307 Property::Value* shaderValue = map.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
308 // set image only if property map contains image information other than custom shader
309 if( map.Count() > 1u || !shaderValue )
311 impl.SetImage( map );
313 // the property map contains only the custom shader
314 else if( impl.mVisual && map.Count() == 1u && shaderValue )
316 Property::Map shaderMap;
317 if( shaderValue->Get( shaderMap ) )
319 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
320 visual.SetCustomShader( shaderMap );
321 if( imageView.OnStage() )
323 // force to create new core renderer to use the newly set shader
324 visual.SetOffStage( imageView );
325 visual.SetOnStage( imageView );
333 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
336 if( value.Get( isPre ) )
338 impl.EnablePreMultipliedAlpha( isPre );
346 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
348 Property::Value value;
350 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
354 ImageView& impl = GetImpl( imageview );
355 switch ( propertyIndex )
357 case Toolkit::ImageView::Property::RESOURCE_URL:
359 if ( !impl.mUrl.empty() )
366 case Toolkit::ImageView::Property::IMAGE:
368 if ( !impl.mUrl.empty() )
372 else if( impl.mImage )
375 Scripting::CreatePropertyMap( impl.mImage, map );
378 else if( !impl.mPropertyMap.Empty() )
380 value = impl.mPropertyMap;
385 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
387 value = impl.IsPreMultipliedAlphaEnabled();
396 } // namespace Internal
397 } // namespace Toolkit