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()
105 void ImageActor::SetImage( ImagePtr& image )
107 ImagePtr currentImage = mImageAttachment->GetImage();
108 // early exit if it's the same image as we already have
109 if ( currentImage == image )
114 // NOTE! image might be pointing to NULL, which is fine as in that case app wants to just remove the image
115 ImagePtr newImage( image );
116 // if image is not NULL, check for 9 patch
119 // Automatically convert nine-patch images to cropped bitmap
120 NinePatchImage* ninePatchImage = NinePatchImage::DownCast( image.Get() );
123 newImage = ninePatchImage->CreateCroppedBufferImage();
124 SetStyle( Dali::ImageActor::STYLE_NINE_PATCH );
125 SetNinePatchBorder( ninePatchImage->GetStretchBorders(), true );
128 // set the actual image (normal or 9 patch) and natural size based on that
129 mImageAttachment->SetImage( newImage );
133 ImagePtr ImageActor::GetImage()
135 return mImageAttachment->GetImage();
138 void ImageActor::SetToNaturalSize()
140 mUsingNaturalSize = true;
145 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
147 mImageAttachment->SetPixelArea( pixelArea );
152 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
154 return mImageAttachment->GetPixelArea();
157 bool ImageActor::IsPixelAreaSet() const
159 return mImageAttachment->IsPixelAreaSet();
162 void ImageActor::ClearPixelArea()
164 mImageAttachment->ClearPixelArea();
166 if( mUsingNaturalSize )
168 ImagePtr image = mImageAttachment->GetImage();
171 mInternalSetSize = true;
172 SetSize( image->GetNaturalSize() );
173 mInternalSetSize = false;
178 void ImageActor::SetStyle( Style style )
180 mImageAttachment->SetStyle( style );
183 ImageActor::Style ImageActor::GetStyle() const
185 return mImageAttachment->GetStyle();
188 void ImageActor::SetNinePatchBorder( const Vector4& border, bool inPixels )
190 mImageAttachment->SetNinePatchBorder( border, inPixels );
193 Vector4 ImageActor::GetNinePatchBorder() const
195 return mImageAttachment->GetNinePatchBorder();
198 ImageAttachment& ImageActor::GetImageAttachment()
200 return *mImageAttachment;
203 RenderableAttachment& ImageActor::GetRenderableAttachment() const
205 DALI_ASSERT_DEBUG( mImageAttachment && "ImageAttachment missing from ImageActor" );
206 return *mImageAttachment;
209 ImageActor::ImageActor()
211 mUsingNaturalSize(true),
212 mInternalSetSize(false)
216 ImageActor::~ImageActor()
220 void ImageActor::SetNaturalSize()
222 if( mUsingNaturalSize )
224 mInternalSetSize = true;
225 SetSize( CalculateNaturalSize() );
226 mInternalSetSize = false;
230 Vector3 ImageActor::GetNaturalSize() const
232 Vector2 naturalSize( CalculateNaturalSize() );
233 return Vector3( naturalSize.width, naturalSize.height, CalculateSizeZ( naturalSize ) );
236 Vector2 ImageActor::CalculateNaturalSize() const
238 // if no image then natural size is 0
239 Vector2 size( 0.0f, 0.0f );
241 ImagePtr image = mImageAttachment->GetImage();
244 if( IsPixelAreaSet() )
246 PixelArea area(GetPixelArea());
247 size.width = area.width;
248 size.height = area.height;
252 size = image->GetNaturalSize();
259 void ImageActor::OnSizeSet( const Vector3& targetSize )
261 if( !mInternalSetSize )
263 mUsingNaturalSize = false;
267 void ImageActor::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
269 mUsingNaturalSize = false;
272 void ImageActor::OnStageConnectionInternal()
276 void ImageActor::OnStageDisconnectionInternal()
280 unsigned int ImageActor::GetDefaultPropertyCount() const
282 return RenderableActor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
285 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
287 RenderableActor::GetDefaultPropertyIndices( indices ); // RenderableActor class properties
289 indices.reserve( indices.size() + DEFAULT_PROPERTY_COUNT );
291 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
292 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
294 indices.push_back( index );
298 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
300 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
302 return RenderableActor::IsDefaultPropertyWritable(index);
305 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
306 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
308 return DEFAULT_PROPERTY_DETAILS[ index ].writable;
314 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
316 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
318 return RenderableActor::IsDefaultPropertyAnimatable( index );
321 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
322 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
324 return DEFAULT_PROPERTY_DETAILS[ index ].animatable;
330 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
332 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
334 return RenderableActor::IsDefaultPropertyAConstraintInput( index );
337 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
338 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
340 return DEFAULT_PROPERTY_DETAILS[ index ].constraintInput;
346 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
348 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
350 return RenderableActor::GetDefaultPropertyType( index );
353 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
354 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
356 return DEFAULT_PROPERTY_DETAILS[index].type;
359 // index out-of-bounds
360 return Property::NONE;
363 const char* ImageActor::GetDefaultPropertyName( Property::Index index ) const
365 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
367 return RenderableActor::GetDefaultPropertyName(index);
370 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
371 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
373 return DEFAULT_PROPERTY_DETAILS[index].name;
376 // index out-of-bounds
380 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
382 Property::Index index = Property::INVALID_INDEX;
384 // Look for name in default properties
385 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
387 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
388 if( 0 == strcmp( name.c_str(), property->name ) ) // Don't want to convert rhs to string
390 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
395 // If not found, check in base class
396 if( Property::INVALID_INDEX == index )
398 index = RenderableActor::GetDefaultPropertyIndex( name );
403 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
405 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
407 RenderableActor::SetDefaultProperty( index, propertyValue );
413 case Dali::ImageActor::Property::PIXEL_AREA:
415 SetPixelArea(propertyValue.Get<Rect<int> >());
418 case Dali::ImageActor::Property::STYLE:
420 SetStyle( StyleEnum( propertyValue.Get<std::string>() ) );
423 case Dali::ImageActor::Property::BORDER:
425 SetNinePatchBorder( propertyValue.Get<Vector4>(), true /*in pixels*/ );
428 case Dali::ImageActor::Property::IMAGE:
430 Dali::Image img = Scripting::NewImage( propertyValue );
433 ImagePtr image( &GetImplementation(img) );
438 DALI_LOG_WARNING("Cannot create image from property value\n");
444 DALI_LOG_WARNING("Unknown property (%d)\n", index);
452 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
455 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
457 ret = RenderableActor::GetDefaultProperty( index );
463 case Dali::ImageActor::Property::PIXEL_AREA:
465 Rect<int> r = GetPixelArea();
469 case Dali::ImageActor::Property::STYLE:
471 ret = StyleString( GetStyle() );
474 case Dali::ImageActor::Property::BORDER:
476 ret = GetNinePatchBorder();
479 case Dali::ImageActor::Property::IMAGE:
482 Scripting::CreatePropertyMap( Dali::Image( mImageAttachment->GetImage().Get() ), map );
483 ret = Property::Value( map );
488 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
497 } // namespace Internal