2 * Copyright (c) 2014 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 <dali/internal/event/actors/image-actor-impl.h>
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/public-api/scripting/scripting.h>
24 #include <dali/internal/event/common/property-helper.h>
25 #include <dali/internal/event/images/image-connector.h>
26 #include <dali/internal/event/images/nine-patch-image-impl.h>
39 // Name Type writable animatable constraint-input enum for index-checking
40 DALI_PROPERTY_TABLE_BEGIN
41 DALI_PROPERTY( "pixel-area", RECTANGLE, true, false, true, Dali::ImageActor::Property::PIXEL_AREA )
42 DALI_PROPERTY( "style", STRING, true, false, true, Dali::ImageActor::Property::STYLE )
43 DALI_PROPERTY( "border", VECTOR4, true, false, true, Dali::ImageActor::Property::BORDER )
44 DALI_PROPERTY( "image", MAP, true, false, false, Dali::ImageActor::Property::IMAGE )
45 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX )
49 return Dali::ImageActor::New();
52 TypeRegistration mType( typeid( Dali::ImageActor ), typeid( Dali::RenderableActor ), Create );
54 ImageActor::Style StyleEnum(const std::string &s)
56 if(s == "STYLE_NINE_PATCH")
58 return Dali::ImageActor::STYLE_NINE_PATCH;
60 else if(s == "STYLE_NINE_PATCH_NO_CENTER")
62 return Dali::ImageActor::STYLE_NINE_PATCH_NO_CENTER;
64 else // if(s == "QUAD")
66 return Dali::ImageActor::STYLE_QUAD;
70 std::string StyleString(const ImageActor::Style style)
72 if(style == Dali::ImageActor::STYLE_NINE_PATCH)
74 return "STYLE_NINE_PATCH";
76 else if(style == Dali::ImageActor::STYLE_NINE_PATCH_NO_CENTER)
78 return "STYLE_NINE_PATCH_NO_CENTER";
80 else // if(s == "QUAD")
87 ImageActorPtr ImageActor::New()
89 ImageActorPtr actor( new ImageActor );
91 // Second-phase construction of base class
94 // Create the attachment
95 actor->mImageAttachment = ImageAttachment::New( actor->GetEventThreadServices(), *actor->mNode );
96 actor->Attach( *actor->mImageAttachment );
101 void ImageActor::OnInitialize()
103 SetResizePolicy( USE_NATURAL_SIZE, ALL_DIMENSIONS );
106 void ImageActor::SetImage( ImagePtr& image )
108 ImagePtr currentImage = mImageAttachment->GetImage();
109 // early exit if it's the same image as we already have
110 if ( currentImage == image )
115 // NOTE! image might be pointing to NULL, which is fine as in that case app wants to just remove the image
116 ImagePtr newImage( image );
117 // if image is not NULL, check for 9 patch
120 // Automatically convert nine-patch images to cropped bitmap
121 NinePatchImage* ninePatchImage = NinePatchImage::DownCast( image.Get() );
124 newImage = ninePatchImage->CreateCroppedBufferImage();
125 SetStyle( Dali::ImageActor::STYLE_NINE_PATCH );
126 SetNinePatchBorder( ninePatchImage->GetStretchBorders(), true );
129 // set the actual image (normal or 9 patch) and natural size based on that
130 mImageAttachment->SetImage( newImage );
134 ImagePtr ImageActor::GetImage()
136 return mImageAttachment->GetImage();
139 void ImageActor::SetToNaturalSize()
141 mUsingNaturalSize = true;
146 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
148 mImageAttachment->SetPixelArea( pixelArea );
153 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
155 return mImageAttachment->GetPixelArea();
158 bool ImageActor::IsPixelAreaSet() const
160 return mImageAttachment->IsPixelAreaSet();
163 void ImageActor::ClearPixelArea()
165 mImageAttachment->ClearPixelArea();
167 if( mUsingNaturalSize )
169 ImagePtr image = mImageAttachment->GetImage();
172 mInternalSetSize = true;
173 SetSizeInternal( image->GetNaturalSize() );
174 mInternalSetSize = false;
179 void ImageActor::SetStyle( Style style )
181 mImageAttachment->SetStyle( style );
184 ImageActor::Style ImageActor::GetStyle() const
186 return mImageAttachment->GetStyle();
189 void ImageActor::SetNinePatchBorder( const Vector4& border, bool inPixels )
191 mImageAttachment->SetNinePatchBorder( border, inPixels );
194 Vector4 ImageActor::GetNinePatchBorder() const
196 return mImageAttachment->GetNinePatchBorder();
199 ImageAttachment& ImageActor::GetImageAttachment()
201 return *mImageAttachment;
204 RenderableAttachment& ImageActor::GetRenderableAttachment() const
206 DALI_ASSERT_DEBUG( mImageAttachment && "ImageAttachment missing from ImageActor" );
207 return *mImageAttachment;
210 ImageActor::ImageActor()
212 mUsingNaturalSize(true),
213 mInternalSetSize(false)
215 // Size negotiate disabled by default, so turn it on for this actor
216 SetRelayoutEnabled( true );
219 ImageActor::~ImageActor()
223 void ImageActor::SetNaturalSize()
225 if( mUsingNaturalSize )
227 mInternalSetSize = true;
228 SetSizeInternal( CalculateNaturalSize() );
229 mInternalSetSize = false;
233 Vector3 ImageActor::GetNaturalSize() const
235 Vector2 naturalSize( CalculateNaturalSize() );
236 return Vector3( naturalSize.width, naturalSize.height, CalculateSizeZ( naturalSize ) );
239 Vector2 ImageActor::CalculateNaturalSize() const
241 // if no image then natural size is 0
242 Vector2 size( 0.0f, 0.0f );
244 ImagePtr image = mImageAttachment->GetImage();
247 if( IsPixelAreaSet() )
249 PixelArea area(GetPixelArea());
250 size.width = area.width;
251 size.height = area.height;
255 size = image->GetNaturalSize();
262 void ImageActor::OnSizeSet( const Vector3& targetSize )
264 if( !mInternalSetSize )
266 mUsingNaturalSize = false;
270 void ImageActor::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
272 mUsingNaturalSize = false;
275 void ImageActor::OnStageConnectionInternal()
279 void ImageActor::OnStageDisconnectionInternal()
283 unsigned int ImageActor::GetDefaultPropertyCount() const
285 return RenderableActor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
288 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
290 RenderableActor::GetDefaultPropertyIndices( indices ); // RenderableActor class properties
292 indices.reserve( indices.size() + DEFAULT_PROPERTY_COUNT );
294 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
295 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
297 indices.push_back( index );
301 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
303 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
305 return RenderableActor::IsDefaultPropertyWritable(index);
308 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
309 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
311 return DEFAULT_PROPERTY_DETAILS[ index ].writable;
317 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
319 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
321 return RenderableActor::IsDefaultPropertyAnimatable( index );
324 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
325 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
327 return DEFAULT_PROPERTY_DETAILS[ index ].animatable;
333 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
335 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
337 return RenderableActor::IsDefaultPropertyAConstraintInput( index );
340 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
341 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
343 return DEFAULT_PROPERTY_DETAILS[ index ].constraintInput;
349 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
351 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
353 return RenderableActor::GetDefaultPropertyType( index );
356 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
357 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
359 return DEFAULT_PROPERTY_DETAILS[index].type;
362 // index out-of-bounds
363 return Property::NONE;
366 const char* ImageActor::GetDefaultPropertyName( Property::Index index ) const
368 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
370 return RenderableActor::GetDefaultPropertyName(index);
373 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
374 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
376 return DEFAULT_PROPERTY_DETAILS[index].name;
379 // index out-of-bounds
383 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
385 Property::Index index = Property::INVALID_INDEX;
387 // Look for name in default properties
388 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
390 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
391 if( 0 == strcmp( name.c_str(), property->name ) ) // Don't want to convert rhs to string
393 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
398 // If not found, check in base class
399 if( Property::INVALID_INDEX == index )
401 index = RenderableActor::GetDefaultPropertyIndex( name );
406 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
408 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
410 RenderableActor::SetDefaultProperty( index, propertyValue );
416 case Dali::ImageActor::Property::PIXEL_AREA:
418 SetPixelArea(propertyValue.Get<Rect<int> >());
421 case Dali::ImageActor::Property::STYLE:
423 SetStyle( StyleEnum( propertyValue.Get<std::string>() ) );
426 case Dali::ImageActor::Property::BORDER:
428 SetNinePatchBorder( propertyValue.Get<Vector4>(), true /*in pixels*/ );
431 case Dali::ImageActor::Property::IMAGE:
433 Dali::Image img = Scripting::NewImage( propertyValue );
436 ImagePtr image( &GetImplementation(img) );
441 DALI_LOG_WARNING("Cannot create image from property value\n");
447 DALI_LOG_WARNING("Unknown property (%d)\n", index);
455 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
458 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
460 ret = RenderableActor::GetDefaultProperty( index );
466 case Dali::ImageActor::Property::PIXEL_AREA:
468 Rect<int> r = GetPixelArea();
472 case Dali::ImageActor::Property::STYLE:
474 ret = StyleString( GetStyle() );
477 case Dali::ImageActor::Property::BORDER:
479 ret = GetNinePatchBorder();
482 case Dali::ImageActor::Property::IMAGE:
485 Scripting::CreatePropertyMap( Dali::Image( mImageAttachment->GetImage().Get() ), map );
486 ret = Property::Value( map );
491 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
500 } // namespace Internal