2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "image-view-impl.h"
22 #include <dali/public-api/images/resource-image.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/public-api/object/type-registry-helper.h>
25 #include <dali/devel-api/scripting/scripting.h>
28 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
29 #include <dali-toolkit/devel-api/controls/control-devel.h>
30 #include <dali-toolkit/public-api/visuals/visual-properties.h>
31 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
32 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
33 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
34 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
50 return Toolkit::ImageView::New();
53 // Setup properties, signals and actions using the type-registry.
54 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ImageView, Toolkit::Control, Create );
55 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "reservedProperty01", STRING, RESERVED_PROPERTY_01 )
56 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "image", MAP, IMAGE )
57 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "preMultipliedAlpha", BOOLEAN, PRE_MULTIPLIED_ALPHA )
59 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT( Toolkit, ImageView, "pixelArea", Vector4(0.f, 0.f, 1.f, 1.f), PIXEL_AREA )
60 DALI_TYPE_REGISTRATION_END()
62 } // anonymous namespace
66 ImageView::ImageView()
67 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
69 mImageVisualPaddingSetByTransform( false )
73 ImageView::~ImageView()
77 Toolkit::ImageView ImageView::New()
79 ImageView* impl = new ImageView();
81 Toolkit::ImageView handle = Toolkit::ImageView( *impl );
83 // Second-phase init of the implementation
84 // This can only be done after the CustomActor connection has been made...
90 /////////////////////////////////////////////////////////////
92 void ImageView::OnInitialize()
94 // ImageView can relayout in the OnImageReady, alternative to a signal would be to have a upcall from the Control to ImageView
95 Dali::Toolkit::Control handle( GetOwner() );
96 handle.ResourceReadySignal().Connect( this, &ImageView::OnResourceReady );
99 void ImageView::SetImage( Image image )
101 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
104 mPropertyMap.Clear();
106 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
114 if( !mShaderMap.Empty() )
116 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
117 visualImpl.SetCustomShader( mShaderMap );
120 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
124 // Unregister the existing visual
125 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
127 // Trigger a size negotiation request that may be needed when unregistering a visual.
131 // Signal that a Relayout may be needed
134 void ImageView::SetImage( const Property::Map& map )
136 // Comparing a property map is too expensive so just creating a new visual
141 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
144 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
150 if( !mShaderMap.Empty() )
152 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
153 visualImpl.SetCustomShader( mShaderMap );
156 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
160 // Unregister the exsiting visual
161 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
163 // Trigger a size negotiation request that may be needed when unregistering a visual.
167 // Signal that a Relayout may be needed
170 void ImageView::SetImage( const std::string& url, ImageDimensions size )
172 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
176 mPropertyMap.Clear();
178 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
179 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, size );
187 if( !mShaderMap.Empty() )
189 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
190 visualImpl.SetCustomShader( mShaderMap );
193 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
197 // Unregister the exsiting visual
198 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
200 // Trigger a size negotiation request that may be needed when unregistering a visual.
204 // Signal that a Relayout may be needed
207 Image ImageView::GetImage() const
212 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
216 Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
220 bool ImageView::IsPreMultipliedAlphaEnabled() const
224 return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
229 void ImageView::SetDepthIndex( int depthIndex )
233 mVisual.SetDepthIndex( depthIndex );
237 void ImageView::OnStageConnection( int depth )
241 mImage.UploadedSignal().Emit( mImage );
244 Dali::ResourceImage resourceImage = Dali::ResourceImage::DownCast( mImage );
247 resourceImage.LoadingFinishedSignal().Emit( resourceImage );
250 Control::OnStageConnection( depth ); // Enabled visuals will be put on stage
253 Vector3 ImageView::GetNaturalSize()
257 Vector2 rendererNaturalSize;
258 mVisual.GetNaturalSize( rendererNaturalSize );
261 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
263 rendererNaturalSize.width += ( padding.start + padding.end );
264 rendererNaturalSize.height += ( padding.top + padding.bottom );
265 return Vector3( rendererNaturalSize );
268 // if no visual then use Control's natural size
269 return Control::GetNaturalSize();
272 float ImageView::GetHeightForWidth( float width )
275 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
279 return mVisual.GetHeightForWidth( width ) + padding.top + padding.bottom;
283 return Control::GetHeightForWidth( width ) + padding.top + padding.bottom;
287 float ImageView::GetWidthForHeight( float height )
290 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
294 return mVisual.GetWidthForHeight( height ) + padding.start + padding.end;
298 return Control::GetWidthForHeight( height ) + padding.start + padding.end;
302 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
304 Control::OnRelayout( size, container );
308 Property::Map transformMap = Property::Map();
310 Extents padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
311 const Visual::FittingMode fittingMode = Toolkit::GetImplementation(mVisual).GetFittingMode();
313 bool zeroPadding = ( padding == Extents() );
314 if( ( !zeroPadding ) || // If padding is not zero
315 ( fittingMode == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ) )
317 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(
318 Self().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
320 if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
322 std::swap( padding.start, padding.end );
325 auto finalOffset = Vector2( padding.start, padding.top );
326 mImageVisualPaddingSetByTransform = true;
328 // remove padding from the size to know how much is left for the visual
329 auto finalSize = size - Vector2( padding.start + padding.end, padding.top + padding.bottom );
331 // Should provide a transform that handles aspect ratio according to image size
332 if( fittingMode == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO )
334 auto availableVisualSize = finalSize;
337 mVisual.GetNaturalSize( naturalSize );
339 // scale to fit the padded area
340 finalSize = naturalSize * std::min( ( naturalSize.width ? ( availableVisualSize.width / naturalSize.width ) : 0 ),
341 ( naturalSize.height ? ( availableVisualSize.height / naturalSize.height ) : 0 ) );
343 // calculate final offset within the padded area
344 finalOffset += ( availableVisualSize - finalSize ) * .5f;
347 // populate the transform map
348 transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, finalOffset )
349 .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY,
350 Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
351 .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
352 .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN )
353 .Add( Toolkit::Visual::Transform::Property::SIZE, finalSize )
354 .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY,
355 Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) );
357 else if ( mImageVisualPaddingSetByTransform && zeroPadding ) // Reset offset to zero only if padding applied previously
359 mImageVisualPaddingSetByTransform = false;
360 // Reset the transform map
361 transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2::ZERO )
362 .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY,
363 Vector2( Toolkit::Visual::Transform::Policy::RELATIVE, Toolkit::Visual::Transform::Policy::RELATIVE ) );
367 mVisual.SetTransformAndSize( transformMap, size );
369 // mVisual is not updated util the resource is ready in the case of visual replacement.
370 // So apply the transform and size to the new visual.
371 Toolkit::Visual::Base visual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
372 if( visual && visual != mVisual )
374 visual.SetTransformAndSize( transformMap, size );
379 void ImageView::OnResourceReady( Toolkit::Control control )
381 // Visual ready so update visual attached to this ImageView, following call to RelayoutRequest will use this visual.
382 mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
383 // Signal that a Relayout may be needed
386 ///////////////////////////////////////////////////////////
391 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
393 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
397 ImageView& impl = GetImpl( imageView );
400 case Toolkit::ImageView::Property::IMAGE:
402 std::string imageUrl;
404 if( value.Get( imageUrl ) )
406 impl.SetImage( imageUrl, ImageDimensions() );
408 // if its not a string then get a Property::Map from the property if possible.
411 map = value.GetMap();
414 Property::Value* shaderValue = map->Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
415 // set image only if property map contains image information other than custom shader
416 if( map->Count() > 1u || !shaderValue )
418 impl.SetImage( *map );
420 // the property map contains only the custom shader
421 else if( ( map->Count() == 1u )&&( shaderValue ) )
423 Property::Map* shaderMap = shaderValue->GetMap();
426 impl.mShaderMap = *shaderMap;
428 if( !impl.mUrl.empty() )
430 impl.SetImage( impl.mUrl, impl.mImageSize );
432 else if( impl.mImage )
434 impl.SetImage( impl.mImage );
436 else if( !impl.mPropertyMap.Empty() )
438 impl.SetImage( impl.mPropertyMap );
447 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
450 if( value.Get( isPre ) )
452 impl.EnablePreMultipliedAlpha( isPre );
460 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
462 Property::Value value;
464 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
468 ImageView& impl = GetImpl( imageview );
469 switch ( propertyIndex )
471 case Toolkit::ImageView::Property::IMAGE:
473 if ( !impl.mUrl.empty() )
477 else if( impl.mImage )
480 Scripting::CreatePropertyMap( impl.mImage, map );
486 Toolkit::Visual::Base visual = DevelControl::GetVisual( impl, Toolkit::ImageView::Property::IMAGE );
489 visual.CreatePropertyMap( map );
496 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
498 value = impl.IsPreMultipliedAlphaEnabled();
507 } // namespace Internal
508 } // namespace Toolkit