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 ) )
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::SetImage( Image image )
92 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
97 mVisual = Toolkit::VisualFactory::Get().CreateVisual( image );
98 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, mVisual );
103 void ImageView::SetImage( const Property::Map& map )
105 // Comparing a property map is too expensive so just creating a new visual
110 mVisual = Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
111 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, mVisual );
116 void ImageView::SetImage( const std::string& url, ImageDimensions size )
118 // Don't bother comparing if we had a visual previously, just drop old visual and create new one
121 mPropertyMap.Clear();
123 mVisual = Toolkit::VisualFactory::Get().CreateVisual( url, size );
124 DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, mVisual );
129 Image ImageView::GetImage() const
134 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
138 Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
142 bool ImageView::IsPreMultipliedAlphaEnabled() const
146 return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
151 void ImageView::SetDepthIndex( int depthIndex )
155 mVisual.SetDepthIndex( depthIndex );
159 Vector3 ImageView::GetNaturalSize()
163 Vector2 rendererNaturalSize;
164 mVisual.GetNaturalSize( rendererNaturalSize );
165 return Vector3( rendererNaturalSize );
168 // if no visual then use Control's natural size
169 return Control::GetNaturalSize();
172 float ImageView::GetHeightForWidth( float width )
176 return mVisual.GetHeightForWidth( width );
180 return Control::GetHeightForWidth( width );
184 float ImageView::GetWidthForHeight( float height )
188 return mVisual.GetWidthForHeight( height );
192 return Control::GetWidthForHeight( height );
196 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
198 Control::OnRelayout( size, container );
202 // Pass in an empty map which uses default transform values meaning our visual fills the control
203 // Should provide a transform that handles aspect ratio according to image size
204 mVisual.SetTransformAndSize( Property::Map(), size );
208 ///////////////////////////////////////////////////////////
213 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
215 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
219 ImageView& impl = GetImpl( imageView );
222 case Toolkit::ImageView::Property::RESOURCE_URL:
224 std::string imageUrl;
225 if( value.Get( imageUrl ) )
227 impl.SetImage( imageUrl, ImageDimensions() );
232 case Toolkit::ImageView::Property::IMAGE:
234 std::string imageUrl;
236 if( value.Get( imageUrl ) )
238 impl.SetImage( imageUrl, ImageDimensions() );
240 // if its not a string then get a Property::Map from the property if possible.
243 map = value.GetMap();
246 Property::Value* shaderValue = map->Find( Toolkit::DevelVisual::Property::SHADER, CUSTOM_SHADER );
247 // set image only if property map contains image information other than custom shader
248 if( map->Count() > 1u || !shaderValue )
250 impl.SetImage( *map );
252 // the property map contains only the custom shader
253 else if( ( impl.mVisual )&&( map->Count() == 1u )&&( shaderValue ) )
255 Property::Map* shaderMap = shaderValue->GetMap();
258 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
259 visual.SetCustomShader( *shaderMap );
260 if( imageView.OnStage() )
262 // force to create new core renderer to use the newly set shader
263 visual.SetOffStage( imageView );
264 visual.SetOnStage( imageView );
273 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
276 if( value.Get( isPre ) )
278 impl.EnablePreMultipliedAlpha( isPre );
286 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
288 Property::Value value;
290 Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
294 ImageView& impl = GetImpl( imageview );
295 switch ( propertyIndex )
297 case Toolkit::ImageView::Property::RESOURCE_URL:
299 if ( !impl.mUrl.empty() )
306 case Toolkit::ImageView::Property::IMAGE:
308 if ( !impl.mUrl.empty() )
312 else if( impl.mImage )
315 Scripting::CreatePropertyMap( impl.mImage, map );
318 else if( !impl.mPropertyMap.Empty() )
320 value = impl.mPropertyMap;
325 case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
327 value = impl.IsPreMultipliedAlphaEnabled();
336 } // namespace Internal
337 } // namespace Toolkit