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/devel-api/visual-factory/devel-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/internal/visuals/visual-base-data-impl.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()
47 void SetDefaultTransformMap( Property::Map& transformMap )
51 .Add( Toolkit::Visual::DevelProperty::Transform::Property::OFFSET, Vector2(0.0f, 0.0f) )
52 .Add( Toolkit::Visual::DevelProperty::Transform::Property::SIZE, Vector2(1.0f, 1.0f) )
53 .Add( Toolkit::Visual::DevelProperty::Transform::Property::ORIGIN, Toolkit::Align::CENTER )
54 .Add( Toolkit::Visual::DevelProperty::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER )
55 .Add( Toolkit::Visual::DevelProperty::Transform::Property::OFFSET_SIZE_MODE, Vector4::ZERO );
59 } // anonymous namespace
63 ImageView::ImageView()
64 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) )
68 ImageView::~ImageView()
72 Toolkit::ImageView ImageView::New()
74 ImageView* impl = new ImageView();
76 Toolkit::ImageView handle = Toolkit::ImageView( *impl );
78 // Second-phase init of the implementation
79 // This can only be done after the CustomActor connection has been made...
85 /////////////////////////////////////////////////////////////
87 void ImageView::SetImage( Image image )
89 if( ( mImage != image ) ||
90 ! mUrl.empty() || // If we're changing from a URL type to an Image type
91 ! mPropertyMap.Empty() ) // If we're changing from a property map type to an Image type
98 mVisual = Toolkit::VisualFactory::Get().CreateVisual( image );
99 RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual );
100 mImageSize = image ? ImageDimensions( image.GetWidth(), image.GetHeight() ) : ImageDimensions( 0, 0 );
106 void ImageView::SetImage( Property::Map map )
112 mVisual = Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
113 RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual );
115 Property::Value* widthValue = mPropertyMap.Find( "width" );
119 if( widthValue->Get( width ) )
121 mImageSize = ImageDimensions( width, mImageSize.GetHeight() );
125 Property::Value* heightValue = mPropertyMap.Find( "height" );
129 if( heightValue->Get( height ) )
131 mImageSize = ImageDimensions( mImageSize.GetWidth(), height );
138 void ImageView::SetImage( const std::string& url, ImageDimensions size )
140 if( ( mUrl != url ) ||
141 mImage || // If we're changing from an Image type to a URL type
142 ! mPropertyMap.Empty() ) // If we're changing from a property map type to a URL type
145 mPropertyMap.Clear();
149 if( size.GetWidth() != 0u && size.GetHeight() != 0u )
154 mVisual = Toolkit::VisualFactory::Get().CreateVisual( url, size );
155 RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual );
157 // This transform fills the control
158 // Should provide a transform that handles aspect ratio according to image size
159 Property::Map transformMap;
160 SetDefaultTransformMap( transformMap );
161 mVisual.SetTransformAndSize( transformMap, mSizeSet );
167 Image ImageView::GetImage() const
172 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
176 Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
180 bool ImageView::IsPreMultipliedAlphaEnabled() const
184 return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
189 void ImageView::SetDepthIndex( int depthIndex )
193 mVisual.SetDepthIndex( depthIndex );
197 Vector3 ImageView::GetNaturalSize()
201 Vector2 rendererNaturalSize;
202 mVisual.GetNaturalSize( rendererNaturalSize );
203 return Vector3( rendererNaturalSize );
207 size.x = mImageSize.GetWidth();
208 size.y = mImageSize.GetHeight();
210 if( size.x > 0 && size.y > 0 )
212 size.z = std::min(size.x, size.y);
217 // if no image then use Control's natural size
218 return Control::GetNaturalSize();
222 float ImageView::GetHeightForWidth( float width )
224 if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
226 return GetHeightForWidthBase( width );
230 return Control::GetHeightForWidth( width );
234 float ImageView::GetWidthForHeight( float height )
236 if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
238 return GetWidthForHeightBase( height );
242 return Control::GetWidthForHeight( height );
247 ///////////////////////////////////////////////////////////
252 void ImageView::OnSizeSet( const Vector3& targetSize )
254 Control::OnSizeSet( targetSize );
255 mSizeSet = targetSize;
259 Vector2 size( targetSize );
261 // This transform fills the control
262 // Should provide a transform that handles aspect ratio according to image size
263 Property::Map transformMap;
264 SetDefaultTransformMap( transformMap );
265 mVisual.SetTransformAndSize( transformMap, size );
269 ///////////////////////////////////////////////////////////
274 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
276 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
280 ImageView& impl = GetImpl( imageView );
283 case Toolkit::ImageView::Property::RESOURCE_URL:
285 std::string imageUrl;
286 if( value.Get( imageUrl ) )
288 impl.SetImage( imageUrl, ImageDimensions() );
293 case Toolkit::ImageView::Property::IMAGE:
295 std::string imageUrl;
297 if( value.Get( imageUrl ) )
299 impl.SetImage( imageUrl, ImageDimensions() );
301 // if its not a string then get a Property::Map from the property if possible.
302 else if( value.Get( map ) )
304 Property::Value* shaderValue = map.Find( Toolkit::VisualProperty::SHADER, CUSTOM_SHADER );
305 // set image only if property map contains image information other than custom shader
306 if( map.Count() > 1u || !shaderValue )
308 impl.SetImage( map );
310 // the property map contains only the custom shader
311 else if( impl.mVisual && map.Count() == 1u && shaderValue )
313 Property::Map shaderMap;
314 if( shaderValue->Get( shaderMap ) )
316 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
317 visual.SetCustomShader( shaderMap );
318 if( imageView.OnStage() )
320 // force to create new core renderer to use the newly set shader
321 visual.SetOffStage( imageView );
322 visual.SetOnStage( imageView );
330 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
333 if( value.Get( isPre ) )
335 impl.EnablePreMultipliedAlpha( isPre );
343 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
345 Property::Value value;
347 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
351 ImageView& impl = GetImpl( imageview );
352 switch ( propertyIndex )
354 case Toolkit::ImageView::Property::RESOURCE_URL:
356 if ( !impl.mUrl.empty() )
363 case Toolkit::ImageView::Property::IMAGE:
365 if ( !impl.mUrl.empty() )
369 else if( impl.mImage )
372 Scripting::CreatePropertyMap( impl.mImage, map );
375 else if( !impl.mPropertyMap.Empty() )
377 value = impl.mPropertyMap;
382 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
384 value = impl.IsPreMultipliedAlphaEnabled();
393 } // namespace Internal
394 } // namespace Toolkit