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/devel-api/visuals/visual-properties-devel.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 ) ),
68 mRelayoutRequired(true)
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 Toolkit::DevelControl::ResourceReadySignal( handle ).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 );
111 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
114 void ImageView::SetImage( const Property::Map& map )
116 // Comparing a property map is too expensive so just creating a new visual
120 mRelayoutRequired = true;
122 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
123 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
129 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
132 void ImageView::SetImage( const std::string& url, ImageDimensions size )
134 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
137 mPropertyMap.Clear();
138 mRelayoutRequired = true;
140 // Don't set mVisual until it is ready and shown. Getters will still use current visual.
141 Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, size );
147 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
150 Image ImageView::GetImage() const
155 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
159 Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
163 bool ImageView::IsPreMultipliedAlphaEnabled() const
167 return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
172 void ImageView::SetDepthIndex( int depthIndex )
176 mVisual.SetDepthIndex( depthIndex );
180 Vector3 ImageView::GetNaturalSize()
184 Vector2 rendererNaturalSize;
185 mVisual.GetNaturalSize( rendererNaturalSize );
186 return Vector3( rendererNaturalSize );
189 // if no visual then use Control's natural size
190 return Control::GetNaturalSize();
193 float ImageView::GetHeightForWidth( float width )
197 return mVisual.GetHeightForWidth( width );
201 return Control::GetHeightForWidth( width );
205 float ImageView::GetWidthForHeight( float height )
209 return mVisual.GetWidthForHeight( height );
213 return Control::GetWidthForHeight( height );
217 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
219 Control::OnRelayout( size, container );
221 // If visual is being replaced then mVisual will be the replacement visual even if not ready.
222 mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
226 // Pass in an empty map which uses default transform values meaning our visual fills the control
227 // Should provide a transform that handles aspect ratio according to image size
228 mVisual.SetTransformAndSize( Property::Map(), size );
232 void ImageView::OnResourceReady( Toolkit::Control control )
234 // Visual ready so update visual attached to this ImageView, following call to RelayoutRequest will use this visual.
235 mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
237 if( mRelayoutRequired)
239 mRelayoutRequired = false;
244 ///////////////////////////////////////////////////////////
249 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
251 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
255 ImageView& impl = GetImpl( imageView );
258 case Toolkit::ImageView::Property::RESOURCE_URL:
260 std::string imageUrl;
261 if( value.Get( imageUrl ) )
263 impl.SetImage( imageUrl, ImageDimensions() );
268 case Toolkit::ImageView::Property::IMAGE:
270 std::string imageUrl;
272 if( value.Get( imageUrl ) )
274 impl.SetImage( imageUrl, ImageDimensions() );
276 // if its not a string then get a Property::Map from the property if possible.
279 map = value.GetMap();
282 Property::Value* shaderValue = map->Find( Toolkit::DevelVisual::Property::SHADER, CUSTOM_SHADER );
283 // set image only if property map contains image information other than custom shader
284 if( map->Count() > 1u || !shaderValue )
286 impl.SetImage( *map );
288 // the property map contains only the custom shader
289 else if( ( impl.mVisual )&&( map->Count() == 1u )&&( shaderValue ) )
291 Property::Map* shaderMap = shaderValue->GetMap();
294 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
295 visual.SetCustomShader( *shaderMap );
296 if( imageView.OnStage() )
298 // force to create new core renderer to use the newly set shader
299 visual.SetOffStage( imageView );
300 visual.SetOnStage( imageView );
309 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
312 if( value.Get( isPre ) )
314 impl.EnablePreMultipliedAlpha( isPre );
322 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
324 Property::Value value;
326 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
330 ImageView& impl = GetImpl( imageview );
331 switch ( propertyIndex )
333 case Toolkit::ImageView::Property::RESOURCE_URL:
335 if ( !impl.mUrl.empty() )
342 case Toolkit::ImageView::Property::IMAGE:
344 if ( !impl.mUrl.empty() )
348 else if( impl.mImage )
351 Scripting::CreatePropertyMap( impl.mImage, map );
354 else if( !impl.mPropertyMap.Empty() )
356 value = impl.mPropertyMap;
361 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
363 value = impl.IsPreMultipliedAlphaEnabled();
372 } // namespace Internal
373 } // namespace Toolkit