2 * Copyright (c) 2017 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.
131 void ImageView::SetImage( const Property::Map& map )
133 // Comparing a property map is too expensive so just creating a new visual
138 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
141 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
147 if( !mShaderMap.Empty() )
149 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
150 visualImpl.SetCustomShader( mShaderMap );
153 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
157 // Unregister the exsiting visual
158 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
160 // Trigger a size negotiation request that may be needed when unregistering a visual.
165 void ImageView::SetImage( const std::string& url, ImageDimensions size )
167 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
171 mPropertyMap.Clear();
173 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
174 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, size );
182 if( !mShaderMap.Empty() )
184 Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
185 visualImpl.SetCustomShader( mShaderMap );
188 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
192 // Unregister the exsiting visual
193 DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
195 // Trigger a size negotiation request that may be needed when unregistering a visual.
200 Image ImageView::GetImage() const
205 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
209 Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
213 bool ImageView::IsPreMultipliedAlphaEnabled() const
217 return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
222 void ImageView::SetDepthIndex( int depthIndex )
226 mVisual.SetDepthIndex( depthIndex );
230 void ImageView::OnStageConnection( int depth )
234 mImage.UploadedSignal().Emit( mImage );
237 Dali::ResourceImage resourceImage = Dali::ResourceImage::DownCast( mImage );
240 resourceImage.LoadingFinishedSignal().Emit( resourceImage );
243 Control::OnStageConnection( depth ); // Enabled visuals will be put on stage
246 Vector3 ImageView::GetNaturalSize()
250 Vector2 rendererNaturalSize;
251 mVisual.GetNaturalSize( rendererNaturalSize );
254 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
256 rendererNaturalSize.width += ( padding.start + padding.end );
257 rendererNaturalSize.height += ( padding.top + padding.bottom );
258 return Vector3( rendererNaturalSize );
261 // if no visual then use Control's natural size
262 return Control::GetNaturalSize();
265 float ImageView::GetHeightForWidth( float width )
268 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
272 return mVisual.GetHeightForWidth( width ) + padding.top + padding.bottom;
276 return Control::GetHeightForWidth( width ) + padding.top + padding.bottom;
280 float ImageView::GetWidthForHeight( float height )
283 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
287 return mVisual.GetWidthForHeight( height ) + padding.start + padding.end;
291 return Control::GetWidthForHeight( height ) + padding.start + padding.end;
295 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
297 Control::OnRelayout( size, container );
302 padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
304 Property::Map transformMap = Property::Map();
306 if( ( padding.start != 0 ) || ( padding.end != 0 ) || ( padding.top != 0 ) || ( padding.bottom != 0 ) )
308 Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( Self().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>() );
310 if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
312 std::swap(padding.start, padding.end);
315 transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2( padding.start, padding.top ) )
316 .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
317 .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
318 .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN );
321 // Should provide a transform that handles aspect ratio according to image size
322 mVisual.SetTransformAndSize( transformMap, size );
324 // mVisual is not updated util the resource is ready in the case of visual replacement.
325 // So apply the transform and size to the new visual.
326 Toolkit::Visual::Base visual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
327 if( visual && visual != mVisual )
329 visual.SetTransformAndSize( transformMap, size );
334 void ImageView::OnResourceReady( Toolkit::Control control )
336 // Visual ready so update visual attached to this ImageView, following call to RelayoutRequest will use this visual.
337 mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
340 ///////////////////////////////////////////////////////////
345 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
347 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
351 ImageView& impl = GetImpl( imageView );
354 case Toolkit::ImageView::Property::RESOURCE_URL:
356 std::string imageUrl;
357 if( value.Get( imageUrl ) )
359 impl.SetImage( imageUrl, ImageDimensions() );
364 case Toolkit::ImageView::Property::IMAGE:
366 std::string imageUrl;
368 if( value.Get( imageUrl ) )
370 impl.SetImage( imageUrl, ImageDimensions() );
372 // if its not a string then get a Property::Map from the property if possible.
375 map = value.GetMap();
378 Property::Value* shaderValue = map->Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
379 // set image only if property map contains image information other than custom shader
380 if( map->Count() > 1u || !shaderValue )
382 impl.SetImage( *map );
384 // the property map contains only the custom shader
385 else if( ( map->Count() == 1u )&&( shaderValue ) )
387 impl.mShaderMap = *( shaderValue->GetMap() );
389 if( !impl.mUrl.empty() )
391 impl.SetImage( impl.mUrl, impl.mImageSize );
393 else if( impl.mImage )
395 impl.SetImage( impl.mImage );
397 else if( !impl.mPropertyMap.Empty() )
399 impl.SetImage( impl.mPropertyMap );
407 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
410 if( value.Get( isPre ) )
412 impl.EnablePreMultipliedAlpha( isPre );
420 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
422 Property::Value value;
424 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
428 ImageView& impl = GetImpl( imageview );
429 switch ( propertyIndex )
431 case Toolkit::ImageView::Property::RESOURCE_URL:
433 if ( !impl.mUrl.empty() )
440 case Toolkit::ImageView::Property::IMAGE:
442 if ( !impl.mUrl.empty() )
446 else if( impl.mImage )
449 Scripting::CreatePropertyMap( impl.mImage, map );
455 Toolkit::Visual::Base visual = DevelControl::GetVisual( impl, Toolkit::ImageView::Property::IMAGE );
458 visual.CreatePropertyMap( map );
465 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
467 value = impl.IsPreMultipliedAlphaEnabled();
476 } // namespace Internal
477 } // namespace Toolkit