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 ) )
69 SetAccessibilityConstructor( []( Dali::Actor actor ) {
70 return std::unique_ptr< Dali::Accessibility::Accessible >(
71 new AccessibleImpl( actor, Dali::Accessibility::Role::Image ) );
75 ImageView::~ImageView()
79 Toolkit::ImageView ImageView::New()
81 ImageView* impl = new ImageView();
83 Toolkit::ImageView handle = Toolkit::ImageView( *impl );
85 // Second-phase init of the implementation
86 // This can only be done after the CustomActor connection has been made...
92 /////////////////////////////////////////////////////////////
94 void ImageView::OnInitialize()
96 // ImageView can relayout in the OnImageReady, alternative to a signal would be to have a upcall from the Control to ImageView
97 Dali::Toolkit::Control handle( GetOwner() );
98 handle.ResourceReadySignal().Connect( this, &ImageView::OnResourceReady );
101 void ImageView::SetImage( Image image )
103 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
106 mPropertyMap.Clear();
108 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
116 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
120 // Unregister the exsiting visual
121 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
123 // Trigger a size negotiation request that may be needed when unregistering a visual.
128 void ImageView::SetImage( const Property::Map& map )
130 // Comparing a property map is too expensive so just creating a new visual
135 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
138 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
144 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
148 // Unregister the exsiting visual
149 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
151 // Trigger a size negotiation request that may be needed when unregistering a visual.
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.
184 Image ImageView::GetImage() const
189 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
193 Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
197 bool ImageView::IsPreMultipliedAlphaEnabled() const
201 return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
206 void ImageView::SetDepthIndex( int depthIndex )
210 mVisual.SetDepthIndex( depthIndex );
214 void ImageView::OnStageConnection( int depth )
218 mImage.UploadedSignal().Emit( mImage );
221 Dali::ResourceImage resourceImage = Dali::ResourceImage::DownCast( mImage );
224 resourceImage.LoadingFinishedSignal().Emit( resourceImage );
227 Control::OnStageConnection( depth ); // Enabled visuals will be put on stage
230 Vector3 ImageView::GetNaturalSize()
234 Vector2 rendererNaturalSize;
235 mVisual.GetNaturalSize( rendererNaturalSize );
238 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
240 rendererNaturalSize.width += ( padding.start + padding.end );
241 rendererNaturalSize.height += ( padding.top + padding.bottom );
242 return Vector3( rendererNaturalSize );
245 // if no visual then use Control's natural size
246 return Control::GetNaturalSize();
249 float ImageView::GetHeightForWidth( float width )
252 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
256 return mVisual.GetHeightForWidth( width ) + padding.top + padding.bottom;
260 return Control::GetHeightForWidth( width ) + padding.top + padding.bottom;
264 float ImageView::GetWidthForHeight( float height )
267 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
271 return mVisual.GetWidthForHeight( height ) + padding.start + padding.end;
275 return Control::GetWidthForHeight( height ) + padding.start + padding.end;
279 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
281 Control::OnRelayout( size, container );
285 Property::Map transformMap = Property::Map();
287 // Don't transform if fitting mode is FILL
288 if(Toolkit::GetImplementation(mVisual).GetFittingMode() == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO)
291 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
293 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(
294 Self().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
296 if (Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection)
298 std::swap(padding.start, padding.end);
301 // remove padding from the size to know how much is left for the visual
302 auto paddedSize = size - Vector2(padding.start + padding.end, padding.top + padding.bottom);
305 mVisual.GetNaturalSize(naturalSize);
307 // scale to fit the padded area
309 naturalSize * std::min((paddedSize.width / naturalSize.width), (paddedSize.height / naturalSize.height));
311 // calculate final offset within the padded area
312 auto finalOffset = Vector2(padding.start, padding.top) + (paddedSize - finalSize) * .5f;
314 // populate the transform map
315 transformMap.Add(Toolkit::Visual::Transform::Property::OFFSET, finalOffset)
316 .Add(Toolkit::Visual::Transform::Property::OFFSET_POLICY,
317 Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
318 .Add(Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN)
319 .Add(Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN)
320 .Add(Toolkit::Visual::Transform::Property::SIZE, finalSize)
321 .Add(Toolkit::Visual::Transform::Property::SIZE_POLICY,
322 Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE));
325 // Should provide a transform that handles aspect ratio according to image size
326 mVisual.SetTransformAndSize( transformMap, size );
330 void ImageView::OnResourceReady( Toolkit::Control control )
332 // Visual ready so update visual attached to this ImageView, following call to RelayoutRequest will use this visual.
333 mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
336 ///////////////////////////////////////////////////////////
341 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
343 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
347 ImageView& impl = GetImpl( imageView );
350 case Toolkit::ImageView::Property::RESOURCE_URL:
352 std::string imageUrl;
353 if( value.Get( imageUrl ) )
355 impl.SetImage( imageUrl, ImageDimensions() );
360 case Toolkit::ImageView::Property::IMAGE:
362 std::string imageUrl;
364 if( value.Get( imageUrl ) )
366 impl.SetImage( imageUrl, ImageDimensions() );
368 // if its not a string then get a Property::Map from the property if possible.
371 map = value.GetMap();
374 Property::Value* shaderValue = map->Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
375 // set image only if property map contains image information other than custom shader
376 if( map->Count() > 1u || !shaderValue )
378 impl.SetImage( *map );
380 // the property map contains only the custom shader
381 else if( ( impl.mVisual )&&( map->Count() == 1u )&&( shaderValue ) )
383 Property::Map* shaderMap = shaderValue->GetMap();
386 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
387 visual.SetCustomShader( *shaderMap );
388 if( imageView.OnStage() )
390 // force to create new core renderer to use the newly set shader
391 visual.SetOffStage( imageView );
392 visual.SetOnStage( imageView );
401 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
404 if( value.Get( isPre ) )
406 impl.EnablePreMultipliedAlpha( isPre );
414 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
416 Property::Value value;
418 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
422 ImageView& impl = GetImpl( imageview );
423 switch ( propertyIndex )
425 case Toolkit::ImageView::Property::RESOURCE_URL:
427 if ( !impl.mUrl.empty() )
434 case Toolkit::ImageView::Property::IMAGE:
436 if ( !impl.mUrl.empty() )
440 else if( impl.mImage )
443 Scripting::CreatePropertyMap( impl.mImage, map );
446 else if( !impl.mPropertyMap.Empty() )
448 value = impl.mPropertyMap;
453 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
455 value = impl.IsPreMultipliedAlphaEnabled();
464 } // namespace Internal
465 } // namespace Toolkit