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 <cstring> // for strcmp
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/public-api/scripting/scripting.h>
27 #include <dali/internal/event/common/property-helper.h>
28 #include <dali/internal/event/images/image-connector.h>
29 #include <dali/internal/event/images/nine-patch-image-impl.h>
42 // Name Type writable animatable constraint-input enum for index-checking
43 DALI_PROPERTY_TABLE_BEGIN
44 DALI_PROPERTY( "pixel-area", RECTANGLE, true, false, true, Dali::ImageActor::Property::PIXEL_AREA )
45 DALI_PROPERTY( "style", STRING, true, false, true, Dali::ImageActor::Property::STYLE )
46 DALI_PROPERTY( "border", VECTOR4, true, false, true, Dali::ImageActor::Property::BORDER )
47 DALI_PROPERTY( "image", MAP, true, false, false, Dali::ImageActor::Property::IMAGE )
48 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX )
52 return Dali::ImageActor::New();
55 TypeRegistration mType( typeid( Dali::ImageActor ), typeid( Dali::RenderableActor ), Create );
57 ImageActor::Style StyleEnum(const std::string &s)
59 if(s == "STYLE_NINE_PATCH")
61 return Dali::ImageActor::STYLE_NINE_PATCH;
63 else if(s == "STYLE_NINE_PATCH_NO_CENTER")
65 return Dali::ImageActor::STYLE_NINE_PATCH_NO_CENTER;
67 else // if(s == "QUAD")
69 return Dali::ImageActor::STYLE_QUAD;
73 std::string StyleString(const ImageActor::Style style)
75 if(style == Dali::ImageActor::STYLE_NINE_PATCH)
77 return "STYLE_NINE_PATCH";
79 else if(style == Dali::ImageActor::STYLE_NINE_PATCH_NO_CENTER)
81 return "STYLE_NINE_PATCH_NO_CENTER";
83 else // if(s == "QUAD")
90 ImageActorPtr ImageActor::New()
92 ImageActorPtr actor( new ImageActor );
94 // Second-phase construction of base class
97 // Create the attachment
98 actor->mImageAttachment = ImageAttachment::New( actor->GetEventThreadServices(), *actor->mNode );
99 actor->Attach( *actor->mImageAttachment );
104 void ImageActor::OnInitialize()
106 SetResizePolicy( USE_NATURAL_SIZE, ALL_DIMENSIONS );
109 void ImageActor::SetImage( ImagePtr& image )
111 ImagePtr currentImage = mImageAttachment->GetImage();
112 // early exit if it's the same image as we already have
113 if ( currentImage == image )
118 // NOTE! image might be pointing to NULL, which is fine as in that case app wants to just remove the image
119 ImagePtr newImage( image );
120 // if image is not NULL, check for 9 patch
123 // Automatically convert nine-patch images to cropped bitmap
124 NinePatchImage* ninePatchImage = NinePatchImage::DownCast( image.Get() );
127 newImage = ninePatchImage->CreateCroppedBufferImage();
128 SetStyle( Dali::ImageActor::STYLE_NINE_PATCH );
129 SetNinePatchBorder( ninePatchImage->GetStretchBorders(), true );
132 // set the actual image (normal or 9 patch) and natural size based on that
133 mImageAttachment->SetImage( newImage );
137 ImagePtr ImageActor::GetImage()
139 return mImageAttachment->GetImage();
142 void ImageActor::SetToNaturalSize()
144 mUsingNaturalSize = true;
149 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
151 mImageAttachment->SetPixelArea( pixelArea );
156 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
158 return mImageAttachment->GetPixelArea();
161 bool ImageActor::IsPixelAreaSet() const
163 return mImageAttachment->IsPixelAreaSet();
166 void ImageActor::ClearPixelArea()
168 mImageAttachment->ClearPixelArea();
170 if( mUsingNaturalSize )
172 ImagePtr image = mImageAttachment->GetImage();
175 mInternalSetSize = true;
176 SetSizeInternal( image->GetNaturalSize() );
177 mInternalSetSize = false;
182 void ImageActor::SetStyle( Style style )
184 mImageAttachment->SetStyle( style );
187 ImageActor::Style ImageActor::GetStyle() const
189 return mImageAttachment->GetStyle();
192 void ImageActor::SetNinePatchBorder( const Vector4& border, bool inPixels )
194 mImageAttachment->SetNinePatchBorder( border, inPixels );
197 Vector4 ImageActor::GetNinePatchBorder() const
199 return mImageAttachment->GetNinePatchBorder();
202 ImageAttachment& ImageActor::GetImageAttachment()
204 return *mImageAttachment;
207 RenderableAttachment& ImageActor::GetRenderableAttachment() const
209 DALI_ASSERT_DEBUG( mImageAttachment && "ImageAttachment missing from ImageActor" );
210 return *mImageAttachment;
213 ImageActor::ImageActor()
215 mUsingNaturalSize(true),
216 mInternalSetSize(false)
218 // Size negotiate disabled by default, so turn it on for this actor
219 SetRelayoutEnabled( true );
222 ImageActor::~ImageActor()
226 void ImageActor::SetNaturalSize()
228 if( mUsingNaturalSize )
230 mInternalSetSize = true;
231 SetSizeInternal( CalculateNaturalSize() );
232 mInternalSetSize = false;
236 Vector3 ImageActor::GetNaturalSize() const
238 Vector2 naturalSize( CalculateNaturalSize() );
239 return Vector3( naturalSize.width, naturalSize.height, CalculateSizeZ( naturalSize ) );
242 Vector2 ImageActor::CalculateNaturalSize() const
244 // if no image then natural size is 0
245 Vector2 size( 0.0f, 0.0f );
247 ImagePtr image = mImageAttachment->GetImage();
250 if( IsPixelAreaSet() )
252 PixelArea area(GetPixelArea());
253 size.width = area.width;
254 size.height = area.height;
258 size = image->GetNaturalSize();
265 void ImageActor::OnSizeSet( const Vector3& targetSize )
267 if( !mInternalSetSize )
269 mUsingNaturalSize = false;
273 void ImageActor::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
275 mUsingNaturalSize = false;
278 void ImageActor::OnStageConnectionInternal()
282 void ImageActor::OnStageDisconnectionInternal()
286 unsigned int ImageActor::GetDefaultPropertyCount() const
288 return RenderableActor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
291 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
293 RenderableActor::GetDefaultPropertyIndices( indices ); // RenderableActor class properties
295 indices.reserve( indices.size() + DEFAULT_PROPERTY_COUNT );
297 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
298 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
300 indices.push_back( index );
304 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
306 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
308 return RenderableActor::IsDefaultPropertyWritable(index);
311 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
312 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
314 return DEFAULT_PROPERTY_DETAILS[ index ].writable;
320 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
322 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
324 return RenderableActor::IsDefaultPropertyAnimatable( index );
327 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
328 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
330 return DEFAULT_PROPERTY_DETAILS[ index ].animatable;
336 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
338 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
340 return RenderableActor::IsDefaultPropertyAConstraintInput( index );
343 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
344 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
346 return DEFAULT_PROPERTY_DETAILS[ index ].constraintInput;
352 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
354 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
356 return RenderableActor::GetDefaultPropertyType( index );
359 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
360 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
362 return DEFAULT_PROPERTY_DETAILS[index].type;
365 // index out-of-bounds
366 return Property::NONE;
369 const char* ImageActor::GetDefaultPropertyName( Property::Index index ) const
371 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
373 return RenderableActor::GetDefaultPropertyName(index);
376 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
377 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
379 return DEFAULT_PROPERTY_DETAILS[index].name;
382 // index out-of-bounds
386 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
388 Property::Index index = Property::INVALID_INDEX;
390 // Look for name in default properties
391 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
393 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
394 if( 0 == strcmp( name.c_str(), property->name ) ) // Don't want to convert rhs to string
396 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
401 // If not found, check in base class
402 if( Property::INVALID_INDEX == index )
404 index = RenderableActor::GetDefaultPropertyIndex( name );
409 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
411 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
413 RenderableActor::SetDefaultProperty( index, propertyValue );
419 case Dali::ImageActor::Property::PIXEL_AREA:
421 SetPixelArea(propertyValue.Get<Rect<int> >());
424 case Dali::ImageActor::Property::STYLE:
426 SetStyle( StyleEnum( propertyValue.Get<std::string>() ) );
429 case Dali::ImageActor::Property::BORDER:
431 SetNinePatchBorder( propertyValue.Get<Vector4>(), true /*in pixels*/ );
434 case Dali::ImageActor::Property::IMAGE:
436 Dali::Image img = Scripting::NewImage( propertyValue );
439 ImagePtr image( &GetImplementation(img) );
444 DALI_LOG_WARNING("Cannot create image from property value\n");
450 DALI_LOG_WARNING("Unknown property (%d)\n", index);
458 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
461 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
463 ret = RenderableActor::GetDefaultProperty( index );
469 case Dali::ImageActor::Property::PIXEL_AREA:
471 Rect<int> r = GetPixelArea();
475 case Dali::ImageActor::Property::STYLE:
477 ret = StyleString( GetStyle() );
480 case Dali::ImageActor::Property::BORDER:
482 ret = GetNinePatchBorder();
485 case Dali::ImageActor::Property::IMAGE:
488 Scripting::CreatePropertyMap( Dali::Image( mImageAttachment->GetImage().Get() ), map );
489 ret = Property::Value( map );
494 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
503 } // namespace Internal