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.
124 void ImageView::SetImage( const Property::Map& map )
126 // Comparing a property map is too expensive so just creating a new visual
131 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
134 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
140 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
144 // Unregister the exsiting visual
145 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
147 // Trigger a size negotiation request that may be needed when unregistering a visual.
152 void ImageView::SetImage( const std::string& url, ImageDimensions size )
154 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
157 mPropertyMap.Clear();
159 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
160 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, size );
168 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
172 // Unregister the exsiting visual
173 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
175 // Trigger a size negotiation request that may be needed when unregistering a visual.
180 Image ImageView::GetImage() const
185 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
189 Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
193 bool ImageView::IsPreMultipliedAlphaEnabled() const
197 return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
202 void ImageView::SetDepthIndex( int depthIndex )
206 mVisual.SetDepthIndex( depthIndex );
210 Vector3 ImageView::GetNaturalSize()
214 Vector2 rendererNaturalSize;
215 mVisual.GetNaturalSize( rendererNaturalSize );
218 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
220 rendererNaturalSize.width += ( padding.start + padding.end );
221 rendererNaturalSize.height += ( padding.top + padding.bottom );
222 return Vector3( rendererNaturalSize );
225 // if no visual then use Control's natural size
226 return Control::GetNaturalSize();
229 float ImageView::GetHeightForWidth( float width )
232 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
236 return mVisual.GetHeightForWidth( width ) + padding.top + padding.bottom;
240 return Control::GetHeightForWidth( width ) + padding.top + padding.bottom;
244 float ImageView::GetWidthForHeight( float height )
247 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
251 return mVisual.GetWidthForHeight( height ) + padding.start + padding.end;
255 return Control::GetWidthForHeight( height ) + padding.start + padding.end;
259 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
261 Control::OnRelayout( size, container );
265 Property::Map transformMap = Property::Map();
267 // Don't transform if fitting mode is FILL
268 if(Toolkit::GetImplementation(mVisual).GetFittingMode() == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO)
271 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
273 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(
274 Self().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
276 if (Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection)
278 std::swap(padding.start, padding.end);
281 // remove padding from the size to know how much is left for the visual
282 auto paddedSize = size - Vector2(padding.start + padding.end, padding.top + padding.bottom);
285 mVisual.GetNaturalSize(naturalSize);
287 // scale to fit the padded area
289 naturalSize * std::min((paddedSize.width / naturalSize.width), (paddedSize.height / naturalSize.height));
291 // calculate final offset within the padded area
292 auto finalOffset = Vector2(padding.start, padding.top) + (paddedSize - finalSize) * .5f;
294 // populate the transform map
295 transformMap.Add(Toolkit::Visual::Transform::Property::OFFSET, finalOffset)
296 .Add(Toolkit::Visual::Transform::Property::OFFSET_POLICY,
297 Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
298 .Add(Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN)
299 .Add(Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN)
300 .Add(Toolkit::Visual::Transform::Property::SIZE, finalSize)
301 .Add(Toolkit::Visual::Transform::Property::SIZE_POLICY,
302 Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE));
305 // Should provide a transform that handles aspect ratio according to image size
306 mVisual.SetTransformAndSize( transformMap, size );
310 void ImageView::OnResourceReady( Toolkit::Control control )
312 // Visual ready so update visual attached to this ImageView, following call to RelayoutRequest will use this visual.
313 mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
316 ///////////////////////////////////////////////////////////
321 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
323 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
327 ImageView& impl = GetImpl( imageView );
330 case Toolkit::ImageView::Property::RESOURCE_URL:
332 std::string imageUrl;
333 if( value.Get( imageUrl ) )
335 impl.SetImage( imageUrl, ImageDimensions() );
340 case Toolkit::ImageView::Property::IMAGE:
342 std::string imageUrl;
344 if( value.Get( imageUrl ) )
346 impl.SetImage( imageUrl, ImageDimensions() );
348 // if its not a string then get a Property::Map from the property if possible.
351 map = value.GetMap();
354 Property::Value* shaderValue = map->Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
355 // set image only if property map contains image information other than custom shader
356 if( map->Count() > 1u || !shaderValue )
358 impl.SetImage( *map );
360 // the property map contains only the custom shader
361 else if( ( impl.mVisual )&&( map->Count() == 1u )&&( shaderValue ) )
363 Property::Map* shaderMap = shaderValue->GetMap();
366 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
367 visual.SetCustomShader( *shaderMap );
368 if( imageView.OnStage() )
370 // force to create new core renderer to use the newly set shader
371 visual.SetOffStage( imageView );
372 visual.SetOnStage( imageView );
381 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
384 if( value.Get( isPre ) )
386 impl.EnablePreMultipliedAlpha( isPre );
394 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
396 Property::Value value;
398 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
402 ImageView& impl = GetImpl( imageview );
403 switch ( propertyIndex )
405 case Toolkit::ImageView::Property::RESOURCE_URL:
407 if ( !impl.mUrl.empty() )
414 case Toolkit::ImageView::Property::IMAGE:
416 if ( !impl.mUrl.empty() )
420 else if( impl.mImage )
423 Scripting::CreatePropertyMap( impl.mImage, map );
426 else if( !impl.mPropertyMap.Empty() )
428 value = impl.mPropertyMap;
433 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
435 value = impl.IsPreMultipliedAlphaEnabled();
444 } // namespace Internal
445 } // namespace Toolkit