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 ) )
71 ImageView::~ImageView()
75 Toolkit::ImageView ImageView::New()
77 ImageView* impl = new ImageView();
79 Toolkit::ImageView handle = Toolkit::ImageView( *impl );
81 // Second-phase init of the implementation
82 // This can only be done after the CustomActor connection has been made...
88 /////////////////////////////////////////////////////////////
90 void ImageView::OnInitialize()
92 // ImageView can relayout in the OnImageReady, alternative to a signal would be to have a upcall from the Control to ImageView
93 Dali::Toolkit::Control handle( GetOwner() );
94 handle.ResourceReadySignal().Connect( this, &ImageView::OnResourceReady );
97 void ImageView::SetImage( Image image )
99 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
102 mPropertyMap.Clear();
104 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
112 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
116 // Unregister the exsiting visual
117 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
119 // Trigger a size negotiation request that may be needed when unregistering a visual.
123 Toolkit::DevelControl::RequestLayout( *this );
126 void ImageView::SetImage( const Property::Map& map )
128 // Comparing a property map is too expensive so just creating a new visual
133 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
136 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
142 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
146 // Unregister the exsiting visual
147 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
149 // Trigger a size negotiation request that may be needed when unregistering a visual.
153 Toolkit::DevelControl::RequestLayout( *this );
156 void ImageView::SetImage( const std::string& url, ImageDimensions size )
158 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
161 mPropertyMap.Clear();
163 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
164 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, size );
172 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
176 // Unregister the exsiting visual
177 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
179 // Trigger a size negotiation request that may be needed when unregistering a visual.
183 Toolkit::DevelControl::RequestLayout( *this );
186 Image ImageView::GetImage() const
191 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
195 Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
199 bool ImageView::IsPreMultipliedAlphaEnabled() const
203 return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
208 void ImageView::SetDepthIndex( int depthIndex )
212 mVisual.SetDepthIndex( depthIndex );
216 Vector3 ImageView::GetNaturalSize()
220 Vector2 rendererNaturalSize;
221 mVisual.GetNaturalSize( rendererNaturalSize );
224 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
226 rendererNaturalSize.width += ( padding.start + padding.end );
227 rendererNaturalSize.height += ( padding.top + padding.bottom );
228 return Vector3( rendererNaturalSize );
231 // if no visual then use Control's natural size
232 return Control::GetNaturalSize();
235 float ImageView::GetHeightForWidth( float width )
238 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
242 return mVisual.GetHeightForWidth( width ) + padding.top + padding.bottom;
246 return Control::GetHeightForWidth( width ) + padding.top + padding.bottom;
250 float ImageView::GetWidthForHeight( float height )
253 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
257 return mVisual.GetWidthForHeight( height ) + padding.start + padding.end;
261 return Control::GetWidthForHeight( height ) + padding.start + padding.end;
265 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
267 Control::OnRelayout( size, container );
271 Property::Map transformMap = Property::Map();
273 Extents padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
274 const Visual::FittingMode fittingMode = Toolkit::GetImplementation(mVisual).GetFittingMode();
276 if( ( padding != Extents() ) || // If padding is not zero
277 ( fittingMode == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ) )
279 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(
280 Self().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
282 if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
284 std::swap( padding.start, padding.end );
287 auto finalOffset = Vector2( padding.start, padding.top );
289 // remove padding from the size to know how much is left for the visual
290 auto finalSize = size - Vector2( padding.start + padding.end, padding.top + padding.bottom );
292 // Should provide a transform that handles aspect ratio according to image size
293 if( fittingMode == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO )
295 auto availableVisualSize = finalSize;
298 mVisual.GetNaturalSize( naturalSize );
300 // scale to fit the padded area
301 finalSize = naturalSize * std::min( ( naturalSize.width ? ( availableVisualSize.width / naturalSize.width ) : 0 ),
302 ( naturalSize.height ? ( availableVisualSize.height / naturalSize.height ) : 0 ) );
304 // calculate final offset within the padded area
305 finalOffset += ( availableVisualSize - finalSize ) * .5f;
308 // populate the transform map
309 transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, finalOffset )
310 .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY,
311 Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
312 .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
313 .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN )
314 .Add( Toolkit::Visual::Transform::Property::SIZE, finalSize )
315 .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY,
316 Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) );
319 mVisual.SetTransformAndSize( transformMap, size );
323 void ImageView::OnResourceReady( Toolkit::Control control )
325 // Visual ready so update visual attached to this ImageView, following call to RelayoutRequest will use this visual.
326 mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
329 ///////////////////////////////////////////////////////////
334 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
336 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
340 ImageView& impl = GetImpl( imageView );
343 case Toolkit::ImageView::Property::RESOURCE_URL:
345 std::string imageUrl;
346 if( value.Get( imageUrl ) )
348 impl.SetImage( imageUrl, ImageDimensions() );
353 case Toolkit::ImageView::Property::IMAGE:
355 std::string imageUrl;
357 if( value.Get( imageUrl ) )
359 impl.SetImage( imageUrl, ImageDimensions() );
361 // if its not a string then get a Property::Map from the property if possible.
364 map = value.GetMap();
367 Property::Value* shaderValue = map->Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
368 // set image only if property map contains image information other than custom shader
369 if( map->Count() > 1u || !shaderValue )
371 impl.SetImage( *map );
373 // the property map contains only the custom shader
374 else if( ( impl.mVisual )&&( map->Count() == 1u )&&( shaderValue ) )
376 Property::Map* shaderMap = shaderValue->GetMap();
379 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
380 visual.SetCustomShader( *shaderMap );
381 if( imageView.OnStage() )
383 // force to create new core renderer to use the newly set shader
384 visual.SetOffStage( imageView );
385 visual.SetOnStage( imageView );
394 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
397 if( value.Get( isPre ) )
399 impl.EnablePreMultipliedAlpha( isPre );
407 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
409 Property::Value value;
411 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
415 ImageView& impl = GetImpl( imageview );
416 switch ( propertyIndex )
418 case Toolkit::ImageView::Property::RESOURCE_URL:
420 if ( !impl.mUrl.empty() )
427 case Toolkit::ImageView::Property::IMAGE:
429 if ( !impl.mUrl.empty() )
433 else if( impl.mImage )
436 Scripting::CreatePropertyMap( impl.mImage, map );
439 else if( !impl.mPropertyMap.Empty() )
441 value = impl.mPropertyMap;
446 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
448 value = impl.IsPreMultipliedAlphaEnabled();
457 } // namespace Internal
458 } // namespace Toolkit