2 * Copyright (c) 2020 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>
48 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
52 return Toolkit::ImageView::New();
55 // Setup properties, signals and actions using the type-registry.
56 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ImageView, Toolkit::Control, Create );
57 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "reservedProperty01", STRING, RESERVED_PROPERTY_01 )
58 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "image", MAP, IMAGE )
59 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "preMultipliedAlpha", BOOLEAN, PRE_MULTIPLIED_ALPHA )
61 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT( Toolkit, ImageView, "pixelArea", Vector4(0.f, 0.f, 1.f, 1.f), PIXEL_AREA )
62 DALI_TYPE_REGISTRATION_END()
64 } // anonymous namespace
68 ImageView::ImageView()
69 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
71 mImageVisualPaddingSetByTransform( false ),
72 mImageViewPixelAreaSetByFittingMode( false )
76 ImageView::~ImageView()
80 Toolkit::ImageView ImageView::New()
82 ImageView* impl = new ImageView();
84 Toolkit::ImageView handle = Toolkit::ImageView( *impl );
86 // Second-phase init of the implementation
87 // This can only be done after the CustomActor connection has been made...
93 /////////////////////////////////////////////////////////////
95 void ImageView::OnInitialize()
97 // ImageView can relayout in the OnImageReady, alternative to a signal would be to have a upcall from the Control to ImageView
98 Dali::Toolkit::Control handle( GetOwner() );
99 handle.ResourceReadySignal().Connect( this, &ImageView::OnResourceReady );
102 void ImageView::SetImage( Image image )
104 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
107 mPropertyMap.Clear();
109 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
117 if( !mShaderMap.Empty() )
119 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
120 visualImpl.SetCustomShader( mShaderMap );
123 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
127 // Unregister the existing visual
128 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
130 // Trigger a size negotiation request that may be needed when unregistering a visual.
134 // Signal that a Relayout may be needed
137 void ImageView::SetImage( const Property::Map& map )
139 // Comparing a property map is too expensive so just creating a new visual
144 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
147 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
153 if( !mShaderMap.Empty() )
155 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
156 visualImpl.SetCustomShader( mShaderMap );
159 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
163 // Unregister the exsiting visual
164 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
166 // Trigger a size negotiation request that may be needed when unregistering a visual.
170 // Signal that a Relayout may be needed
173 void ImageView::SetImage( const std::string& url, ImageDimensions size )
175 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
179 mPropertyMap.Clear();
181 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
182 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, size );
190 if( !mShaderMap.Empty() )
192 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
193 visualImpl.SetCustomShader( mShaderMap );
196 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
200 // Unregister the exsiting visual
201 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
203 // Trigger a size negotiation request that may be needed when unregistering a visual.
207 // Signal that a Relayout may be needed
210 Image ImageView::GetImage() const
215 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
219 Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
223 bool ImageView::IsPreMultipliedAlphaEnabled() const
227 return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
232 void ImageView::SetDepthIndex( int depthIndex )
236 mVisual.SetDepthIndex( depthIndex );
240 Vector3 ImageView::GetNaturalSize()
244 Vector2 rendererNaturalSize;
245 mVisual.GetNaturalSize( rendererNaturalSize );
248 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
250 rendererNaturalSize.width += ( padding.start + padding.end );
251 rendererNaturalSize.height += ( padding.top + padding.bottom );
252 return Vector3( rendererNaturalSize );
255 // if no visual then use Control's natural size
256 return Control::GetNaturalSize();
259 float ImageView::GetHeightForWidth( float width )
262 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
266 return mVisual.GetHeightForWidth( width ) + padding.top + padding.bottom;
270 return Control::GetHeightForWidth( width ) + padding.top + padding.bottom;
274 float ImageView::GetWidthForHeight( float height )
277 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
281 return mVisual.GetWidthForHeight( height ) + padding.start + padding.end;
285 return Control::GetWidthForHeight( height ) + padding.start + padding.end;
289 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
291 Control::OnRelayout( size, container );
294 Property::Map transformMap = Property::Map();
296 Extents padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
298 bool zeroPadding = ( padding == Extents() );
301 mVisual.GetNaturalSize( naturalSize );
303 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(
304 Self().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
305 if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
307 std::swap( padding.start, padding.end );
310 // remove padding from the size to know how much is left for the visual
311 Vector2 finalSize = size - Vector2( padding.start + padding.end, padding.top + padding.bottom );
312 Vector2 finalOffset = Vector2( padding.start, padding.top );
314 ApplyFittingMode( finalSize, naturalSize, finalOffset, zeroPadding , transformMap );
316 mVisual.SetTransformAndSize( transformMap, size );
318 // mVisual is not updated util the resource is ready in the case of visual replacement.
319 // So apply the transform and size to the new visual.
320 Toolkit::Visual::Base visual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
321 if( visual && visual != mVisual )
323 visual.SetTransformAndSize( transformMap, size );
328 void ImageView::OnResourceReady( Toolkit::Control control )
330 // Visual ready so update visual attached to this ImageView, following call to RelayoutRequest will use this visual.
331 mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
332 // Signal that a Relayout may be needed
335 void ImageView::SetTransformMapForFittingMode( Vector2 finalSize, Vector2 naturalSize, Vector2 finalOffset, Visual::FittingMode fittingMode, Property::Map& transformMap )
339 case Visual::FittingMode::FIT_KEEP_ASPECT_RATIO:
341 auto availableVisualSize = finalSize;
343 // scale to fit the padded area
344 finalSize = naturalSize * std::min( ( naturalSize.width ? ( availableVisualSize.width / naturalSize.width ) : 0 ),
345 ( naturalSize.height ? ( availableVisualSize.height / naturalSize.height ) : 0 ) );
347 // calculate final offset within the padded area
348 finalOffset += ( availableVisualSize - finalSize ) * .5f;
350 // populate the transform map
351 transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, finalOffset )
352 .Add( Toolkit::Visual::Transform::Property::SIZE, finalSize );
355 case Visual::FittingMode::OVER_FIT_KEEP_ASPECT_RATIO:
357 mImageViewPixelAreaSetByFittingMode = true;
358 auto availableVisualSize = finalSize;
359 finalSize = naturalSize * std::max( ( naturalSize.width ? ( availableVisualSize.width / naturalSize.width ) : 0 ),
360 ( naturalSize.height ? ( availableVisualSize.height / naturalSize.height ) : 0 ) );
362 auto originalOffset = finalOffset;
363 finalOffset += ( availableVisualSize - finalSize ) * .5f;
365 float x = abs( (availableVisualSize.width - finalSize.width ) / finalSize.width )* .5f;
366 float y = abs( (availableVisualSize.height - finalSize.height ) / finalSize.height ) * .5f;
367 float widthRatio = 1.f - abs( (availableVisualSize.width - finalSize.width ) / finalSize.width );
368 float heightRatio = 1.f - abs( (availableVisualSize.height - finalSize.height ) / finalSize.height );
369 Vector4 pixelArea = Vector4( x, y, widthRatio, heightRatio);
370 Self().SetProperty( Toolkit::ImageView::Property::PIXEL_AREA, pixelArea );
372 // populate the transform map
373 transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, originalOffset )
374 .Add( Toolkit::Visual::Transform::Property::SIZE, availableVisualSize );
377 case Visual::FittingMode::CENTER:
379 auto availableVisualSize = finalSize;
380 if( availableVisualSize.width > naturalSize.width && availableVisualSize.height > naturalSize.height )
382 finalSize = naturalSize;
386 finalSize = naturalSize * std::min( ( naturalSize.width ? ( availableVisualSize.width / naturalSize.width ) : 0 ),
387 ( naturalSize.height ? ( availableVisualSize.height / naturalSize.height ) : 0 ) );
390 finalOffset += ( availableVisualSize - finalSize ) * .5f;
392 // populate the transform map
393 transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, finalOffset )
394 .Add( Toolkit::Visual::Transform::Property::SIZE, finalSize );
397 case Visual::FittingMode::FILL:
399 transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, finalOffset )
400 .Add( Toolkit::Visual::Transform::Property::SIZE, finalSize );
403 case Visual::FittingMode::FIT_WIDTH:
404 case Visual::FittingMode::FIT_HEIGHT:
406 // This FittingMode already converted
412 void ImageView::ApplyFittingMode( Vector2 finalSize, Vector2 naturalSize, Vector2 finalOffset, bool zeroPadding , Property::Map& transformMap )
414 Visual::FittingMode fittingMode = Toolkit::GetImplementation(mVisual).GetFittingMode();
416 // Reset PIXEL_AREA after using OVER_FIT_KEEP_ASPECT_RATIO
417 if( mImageViewPixelAreaSetByFittingMode )
419 Self().SetProperty( Toolkit::ImageView::Property::PIXEL_AREA, FULL_TEXTURE_RECT );
420 mImageViewPixelAreaSetByFittingMode = false;
423 if( ( !zeroPadding ) || // If padding is not zero
424 ( fittingMode != Visual::FittingMode::FILL ) )
426 mImageVisualPaddingSetByTransform = true;
428 // If FittingMode use FIT_WIDTH or FIT_HEIGTH, it need to change proper fittingMode
429 if( fittingMode == Visual::FittingMode::FIT_WIDTH )
431 fittingMode = ( finalSize.height / naturalSize.height ) < ( finalSize.width / naturalSize.width ) ? Visual::FittingMode::OVER_FIT_KEEP_ASPECT_RATIO : Visual::FittingMode::FIT_KEEP_ASPECT_RATIO;
433 else if( fittingMode == Visual::FittingMode::FIT_HEIGHT )
435 fittingMode = ( finalSize.height / naturalSize.height ) < ( finalSize.width / naturalSize.width ) ? Visual::FittingMode::FIT_KEEP_ASPECT_RATIO : Visual::FittingMode::OVER_FIT_KEEP_ASPECT_RATIO;
438 SetTransformMapForFittingMode( finalSize, naturalSize, finalOffset, fittingMode, transformMap );
440 // Set extra value for applying transformMap
441 transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY,
442 Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
443 .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
444 .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN )
445 .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY,
446 Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) );
448 else if ( mImageVisualPaddingSetByTransform && zeroPadding ) // Reset offset to zero only if padding applied previously
450 mImageVisualPaddingSetByTransform = false;
451 // Reset the transform map
452 transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2::ZERO )
453 .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY,
454 Vector2( Toolkit::Visual::Transform::Policy::RELATIVE, Toolkit::Visual::Transform::Policy::RELATIVE ) )
455 .Add( Toolkit::Visual::Transform::Property::SIZE, Vector2::ONE )
456 .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY,
457 Vector2( Toolkit::Visual::Transform::Policy::RELATIVE, Toolkit::Visual::Transform::Policy::RELATIVE ) );
461 ///////////////////////////////////////////////////////////
466 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
468 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
472 ImageView& impl = GetImpl( imageView );
475 case Toolkit::ImageView::Property::IMAGE:
477 std::string imageUrl;
479 if( value.Get( imageUrl ) )
481 impl.SetImage( imageUrl, ImageDimensions() );
483 // if its not a string then get a Property::Map from the property if possible.
486 map = value.GetMap();
489 Property::Value* shaderValue = map->Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
490 // set image only if property map contains image information other than custom shader
491 if( map->Count() > 1u || !shaderValue )
493 impl.SetImage( *map );
495 // the property map contains only the custom shader
496 else if( ( map->Count() == 1u )&&( shaderValue ) )
498 Property::Map* shaderMap = shaderValue->GetMap();
501 impl.mShaderMap = *shaderMap;
503 if( !impl.mUrl.empty() )
505 impl.SetImage( impl.mUrl, impl.mImageSize );
507 else if( impl.mImage )
509 impl.SetImage( impl.mImage );
511 else if( !impl.mPropertyMap.Empty() )
513 impl.SetImage( impl.mPropertyMap );
522 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
525 if( value.Get( isPre ) )
527 impl.EnablePreMultipliedAlpha( isPre );
535 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
537 Property::Value value;
539 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
543 ImageView& impl = GetImpl( imageview );
544 switch ( propertyIndex )
546 case Toolkit::ImageView::Property::IMAGE:
548 if ( !impl.mUrl.empty() )
552 else if( impl.mImage )
555 Scripting::CreatePropertyMap( impl.mImage, map );
561 Toolkit::Visual::Base visual = DevelControl::GetVisual( impl, Toolkit::ImageView::Property::IMAGE );
564 visual.CreatePropertyMap( map );
571 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
573 value = impl.IsPreMultipliedAlphaEnabled();
582 } // namespace Internal
583 } // namespace Toolkit