2 * Copyright (c) 2018 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, "resourceUrl", STRING, RESOURCE_URL )
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 ) ),
72 ImageView::~ImageView()
76 Toolkit::ImageView ImageView::New()
78 ImageView* impl = new ImageView();
80 Toolkit::ImageView handle = Toolkit::ImageView( *impl );
82 // Second-phase init of the implementation
83 // This can only be done after the CustomActor connection has been made...
89 /////////////////////////////////////////////////////////////
91 void ImageView::OnInitialize()
93 // ImageView can relayout in the OnImageReady, alternative to a signal would be to have a upcall from the Control to ImageView
94 Dali::Toolkit::Control handle( GetOwner() );
95 handle.ResourceReadySignal().Connect( this, &ImageView::OnResourceReady );
98 void ImageView::SetImage( Image image )
100 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
103 mPropertyMap.Clear();
105 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
113 if( !mShaderMap.Empty() )
115 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
116 visualImpl.SetCustomShader( mShaderMap );
119 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
123 // Unregister the exsiting visual
124 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
126 // Trigger a size negotiation request that may be needed when unregistering a visual.
130 Toolkit::DevelControl::RequestLayout( *this );
133 void ImageView::SetImage( const Property::Map& map )
135 // Comparing a property map is too expensive so just creating a new visual
140 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
143 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
149 if( !mShaderMap.Empty() )
151 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
152 visualImpl.SetCustomShader( mShaderMap );
155 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
159 // Unregister the exsiting visual
160 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
162 // Trigger a size negotiation request that may be needed when unregistering a visual.
166 Toolkit::DevelControl::RequestLayout( *this );
169 void ImageView::SetImage( const std::string& url, ImageDimensions size )
171 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
175 mPropertyMap.Clear();
177 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
178 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, size );
186 if( !mShaderMap.Empty() )
188 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
189 visualImpl.SetCustomShader( mShaderMap );
192 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
196 // Unregister the exsiting visual
197 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
199 // Trigger a size negotiation request that may be needed when unregistering a visual.
203 Toolkit::DevelControl::RequestLayout( *this );
206 Image ImageView::GetImage() const
211 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
215 Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
219 bool ImageView::IsPreMultipliedAlphaEnabled() const
223 return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
228 void ImageView::SetDepthIndex( int depthIndex )
232 mVisual.SetDepthIndex( depthIndex );
236 Vector3 ImageView::GetNaturalSize()
240 Vector2 rendererNaturalSize;
241 mVisual.GetNaturalSize( rendererNaturalSize );
244 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
246 rendererNaturalSize.width += ( padding.start + padding.end );
247 rendererNaturalSize.height += ( padding.top + padding.bottom );
248 return Vector3( rendererNaturalSize );
251 // if no visual then use Control's natural size
252 return Control::GetNaturalSize();
255 float ImageView::GetHeightForWidth( float width )
258 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
262 return mVisual.GetHeightForWidth( width ) + padding.top + padding.bottom;
266 return Control::GetHeightForWidth( width ) + padding.top + padding.bottom;
270 float ImageView::GetWidthForHeight( float height )
273 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
277 return mVisual.GetWidthForHeight( height ) + padding.start + padding.end;
281 return Control::GetWidthForHeight( height ) + padding.start + padding.end;
285 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
287 Control::OnRelayout( size, container );
291 Property::Map transformMap = Property::Map();
293 Extents padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
294 const Visual::FittingMode fittingMode = Toolkit::GetImplementation(mVisual).GetFittingMode();
296 if( ( padding != Extents() ) || // If padding is not zero
297 ( fittingMode == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ) )
299 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(
300 Self().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
302 if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
304 std::swap( padding.start, padding.end );
307 auto finalOffset = Vector2( padding.start, padding.top );
309 // remove padding from the size to know how much is left for the visual
310 auto finalSize = size - Vector2( padding.start + padding.end, padding.top + padding.bottom );
312 // Should provide a transform that handles aspect ratio according to image size
313 if( fittingMode == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO )
315 auto availableVisualSize = finalSize;
318 mVisual.GetNaturalSize( naturalSize );
320 // scale to fit the padded area
321 finalSize = naturalSize * std::min( ( naturalSize.width ? ( availableVisualSize.width / naturalSize.width ) : 0 ),
322 ( naturalSize.height ? ( availableVisualSize.height / naturalSize.height ) : 0 ) );
324 // calculate final offset within the padded area
325 finalOffset += ( availableVisualSize - finalSize ) * .5f;
328 // populate the transform map
329 transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, finalOffset )
330 .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY,
331 Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
332 .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
333 .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN )
334 .Add( Toolkit::Visual::Transform::Property::SIZE, finalSize )
335 .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY,
336 Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) );
339 mVisual.SetTransformAndSize( transformMap, size );
343 void ImageView::OnResourceReady( Toolkit::Control control )
345 // Visual ready so update visual attached to this ImageView, following call to RelayoutRequest will use this visual.
346 mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
347 Toolkit::DevelControl::RequestLayout( *this );
350 ///////////////////////////////////////////////////////////
355 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
357 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
361 ImageView& impl = GetImpl( imageView );
364 case Toolkit::ImageView::Property::RESOURCE_URL:
366 std::string imageUrl;
367 if( value.Get( imageUrl ) )
369 impl.SetImage( imageUrl, ImageDimensions() );
374 case Toolkit::ImageView::Property::IMAGE:
376 std::string imageUrl;
378 if( value.Get( imageUrl ) )
380 impl.SetImage( imageUrl, ImageDimensions() );
382 // if its not a string then get a Property::Map from the property if possible.
385 map = value.GetMap();
388 Property::Value* shaderValue = map->Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
389 // set image only if property map contains image information other than custom shader
390 if( map->Count() > 1u || !shaderValue )
392 impl.SetImage( *map );
394 // the property map contains only the custom shader
395 else if( ( map->Count() == 1u )&&( shaderValue ) )
397 Property::Map* shaderMap = shaderValue->GetMap();
400 impl.mShaderMap = *shaderMap;
402 if( !impl.mUrl.empty() )
404 impl.SetImage( impl.mUrl, impl.mImageSize );
406 else if( impl.mImage )
408 impl.SetImage( impl.mImage );
410 else if( !impl.mPropertyMap.Empty() )
412 impl.SetImage( impl.mPropertyMap );
421 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
424 if( value.Get( isPre ) )
426 impl.EnablePreMultipliedAlpha( isPre );
434 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
436 Property::Value value;
438 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
442 ImageView& impl = GetImpl( imageview );
443 switch ( propertyIndex )
445 case Toolkit::ImageView::Property::RESOURCE_URL:
447 if ( !impl.mUrl.empty() )
454 case Toolkit::ImageView::Property::IMAGE:
456 if ( !impl.mUrl.empty() )
460 else if( impl.mImage )
463 Scripting::CreatePropertyMap( impl.mImage, map );
466 else if( !impl.mPropertyMap.Empty() )
468 value = impl.mPropertyMap;
473 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
475 value = impl.IsPreMultipliedAlphaEnabled();
484 } // namespace Internal
485 } // namespace Toolkit