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/visuals/visual-properties-devel.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()
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 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
81 mVisual = Toolkit::VisualFactory::Get().CreateVisual( image );
82 RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual );
87 void ImageView::SetImage( const Property::Map& map )
89 // Comparing a property map is too expensive so just creating a new visual
94 mVisual = Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
95 RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual );
100 void ImageView::SetImage( const std::string& url, ImageDimensions size )
102 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
105 mPropertyMap.Clear();
107 mVisual = Toolkit::VisualFactory::Get().CreateVisual( url, size );
108 RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual );
113 Image ImageView::GetImage() const
118 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
122 Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
126 bool ImageView::IsPreMultipliedAlphaEnabled() const
130 return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
135 void ImageView::SetDepthIndex( int depthIndex )
139 mVisual.SetDepthIndex( depthIndex );
143 Vector3 ImageView::GetNaturalSize()
147 Vector2 rendererNaturalSize;
148 mVisual.GetNaturalSize( rendererNaturalSize );
149 return Vector3( rendererNaturalSize );
152 // if no visual then use Control's natural size
153 return Control::GetNaturalSize();
156 float ImageView::GetHeightForWidth( float width )
160 return mVisual.GetHeightForWidth( width );
164 return Control::GetHeightForWidth( width );
168 float ImageView::GetWidthForHeight( float height )
172 return mVisual.GetWidthForHeight( height );
176 return Control::GetWidthForHeight( height );
180 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
182 Control::OnRelayout( size, container );
186 // Pass in an empty map which uses default transform values meaning our visual fills the control
187 // Should provide a transform that handles aspect ratio according to image size
188 mVisual.SetTransformAndSize( Property::Map(), size );
192 ///////////////////////////////////////////////////////////
197 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
199 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
203 ImageView& impl = GetImpl( imageView );
206 case Toolkit::ImageView::Property::RESOURCE_URL:
208 std::string imageUrl;
209 if( value.Get( imageUrl ) )
211 impl.SetImage( imageUrl, ImageDimensions() );
216 case Toolkit::ImageView::Property::IMAGE:
218 std::string imageUrl;
220 if( value.Get( imageUrl ) )
222 impl.SetImage( imageUrl, ImageDimensions() );
224 // if its not a string then get a Property::Map from the property if possible.
227 map = value.GetMap();
230 Property::Value* shaderValue = map->Find( Toolkit::DevelVisual::Property::SHADER, CUSTOM_SHADER );
231 // set image only if property map contains image information other than custom shader
232 if( map->Count() > 1u || !shaderValue )
234 impl.SetImage( *map );
236 // the property map contains only the custom shader
237 else if( ( impl.mVisual )&&( map->Count() == 1u )&&( shaderValue ) )
239 Property::Map* shaderMap = shaderValue->GetMap();
242 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
243 visual.SetCustomShader( *shaderMap );
244 if( imageView.OnStage() )
246 // force to create new core renderer to use the newly set shader
247 visual.SetOffStage( imageView );
248 visual.SetOnStage( imageView );
257 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
260 if( value.Get( isPre ) )
262 impl.EnablePreMultipliedAlpha( isPre );
270 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
272 Property::Value value;
274 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
278 ImageView& impl = GetImpl( imageview );
279 switch ( propertyIndex )
281 case Toolkit::ImageView::Property::RESOURCE_URL:
283 if ( !impl.mUrl.empty() )
290 case Toolkit::ImageView::Property::IMAGE:
292 if ( !impl.mUrl.empty() )
296 else if( impl.mImage )
299 Scripting::CreatePropertyMap( impl.mImage, map );
302 else if( !impl.mPropertyMap.Empty() )
304 value = impl.mPropertyMap;
309 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
311 value = impl.IsPreMultipliedAlphaEnabled();
320 } // namespace Internal
321 } // namespace Toolkit