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/internal/event/images/nine-patch-image-impl.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/internal/event/common/property-index-ranges.h>
25 #include <dali/internal/event/images/image-connector.h>
26 #include <dali/public-api/scripting/scripting.h>
31 const Property::Index ImageActor::PIXEL_AREA = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
32 const Property::Index ImageActor::STYLE = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 1;
33 const Property::Index ImageActor::BORDER = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 2;
34 const Property::Index ImageActor::IMAGE = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 3;
38 bool ImageActor::mFirstInstance = true;
39 Actor::DefaultPropertyLookup* ImageActor::mDefaultImageActorPropertyLookup = NULL;
46 return Dali::ImageActor::New();
49 TypeRegistration mType( typeid(Dali::ImageActor), typeid(Dali::RenderableActor), Create );
51 const std::string DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[] =
58 const int DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT = sizeof( DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES ) / sizeof( std::string );
60 const Property::Type DEFAULT_IMAGE_ACTOR_PROPERTY_TYPES[DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT] =
62 Property::RECTANGLE, // "pixel-area",
63 Property::STRING, // "style",
64 Property::VECTOR4, // "border",
65 Property::MAP, // "image",
68 ImageActor::Style StyleEnum(const std::string &s)
70 if(s == "STYLE_NINE_PATCH")
72 return Dali::ImageActor::STYLE_NINE_PATCH;
74 else // if(s == "QUAD")
76 return Dali::ImageActor::STYLE_QUAD;
80 std::string StyleString(const ImageActor::Style style)
82 if(style == Dali::ImageActor::STYLE_NINE_PATCH)
84 return "STYLE_NINE_PATCH";
86 else // if(s == "QUAD")
93 ImageActorPtr ImageActor::New()
95 ImageActorPtr actor( new ImageActor );
97 // Second-phase construction of base class
100 // Create the attachment
101 actor->mImageAttachment = ImageAttachment::New( *actor->mNode );
102 actor->Attach( *actor->mImageAttachment );
107 void ImageActor::OnInitialize()
109 if(ImageActor::mFirstInstance)
111 mDefaultImageActorPropertyLookup = new DefaultPropertyLookup();
112 const int start = DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
113 for ( int i = 0; i < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT; ++i )
115 (*mDefaultImageActorPropertyLookup)[DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[i]] = i + start;
117 ImageActor::mFirstInstance = false;
121 void ImageActor::SetImage( ImagePtr& image )
123 ImagePtr currentImage = mImageAttachment->GetImage();
124 // early exit if it's the same image as we already have
125 if ( currentImage == image )
130 // NOTE! image might be pointing to NULL, which is fine as in that case app wants to just remove the image
131 ImagePtr newImage( image );
132 // if image is not NULL, check for 9 patch
135 // Automatically convert nine-patch images to cropped bitmap
136 NinePatchImage* ninePatchImage = NinePatchImage::DownCast( image.Get() );
139 newImage = ninePatchImage->CreateCroppedBitmapImage();
140 SetStyle( Dali::ImageActor::STYLE_NINE_PATCH );
141 SetNinePatchBorder( ninePatchImage->GetStretchBorders(), true );
144 // set the actual image (normal or 9 patch) and natural size based on that
145 mImageAttachment->SetImage( newImage );
149 ImagePtr ImageActor::GetImage()
151 return mImageAttachment->GetImage();
154 void ImageActor::SetToNaturalSize()
156 mUsingNaturalSize = true;
161 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
163 mImageAttachment->SetPixelArea( pixelArea );
168 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
170 return mImageAttachment->GetPixelArea();
173 bool ImageActor::IsPixelAreaSet() const
175 return mImageAttachment->IsPixelAreaSet();
178 void ImageActor::ClearPixelArea()
180 mImageAttachment->ClearPixelArea();
182 if( mUsingNaturalSize )
184 ImagePtr image = mImageAttachment->GetImage();
187 mInternalSetSize = true;
188 SetSize( image->GetNaturalSize() );
189 mInternalSetSize = false;
194 void ImageActor::SetStyle( Style style )
196 mImageAttachment->SetStyle( style );
199 ImageActor::Style ImageActor::GetStyle() const
201 return mImageAttachment->GetStyle();
204 void ImageActor::SetNinePatchBorder( const Vector4& border, bool inPixels )
206 mImageAttachment->SetNinePatchBorder( border, inPixels );
209 Vector4 ImageActor::GetNinePatchBorder() const
211 return mImageAttachment->GetNinePatchBorder();
214 ImageAttachment& ImageActor::GetImageAttachment()
216 return *mImageAttachment;
219 RenderableAttachment& ImageActor::GetRenderableAttachment() const
221 DALI_ASSERT_DEBUG( mImageAttachment && "ImageAttachment missing from ImageActor" );
222 return *mImageAttachment;
225 ImageActor::ImageActor()
227 mUsingNaturalSize(true),
228 mInternalSetSize(false)
232 ImageActor::~ImageActor()
236 void ImageActor::SetNaturalSize()
238 if( mUsingNaturalSize )
240 // if no image then natural size is 0
242 ImagePtr image = mImageAttachment->GetImage();
245 if( IsPixelAreaSet() )
247 PixelArea area(GetPixelArea());
248 size.width = area.width;
249 size.height = area.height;
253 size = image->GetNaturalSize();
256 mInternalSetSize = true;
258 mInternalSetSize = false;
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_IMAGE_ACTOR_PROPERTY_COUNT;
288 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
290 RenderableActor::GetDefaultPropertyIndices( indices ); // RenderableActor class properties
292 indices.reserve( indices.size() + DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT );
294 int index = DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
295 for ( int i = 0; i < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT; ++i, ++index )
297 indices.push_back( index );
301 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
303 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
305 return RenderableActor::IsDefaultPropertyWritable(index);
313 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
315 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
317 return RenderableActor::IsDefaultPropertyAnimatable(index);
325 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
327 if( index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT )
329 return RenderableActor::IsDefaultPropertyAConstraintInput(index);
331 return true; // Our properties can be used as input to constraints.
334 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
336 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
338 return RenderableActor::GetDefaultPropertyType(index);
342 index -= DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
344 if ( ( index >= 0 ) && ( index < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT ) )
346 return DEFAULT_IMAGE_ACTOR_PROPERTY_TYPES[index];
350 // index out-of-bounds
351 return Property::NONE;
356 const std::string& ImageActor::GetDefaultPropertyName( Property::Index index ) const
358 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
360 return RenderableActor::GetDefaultPropertyName(index);
364 index -= DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
366 if ( ( index >= 0 ) && ( index < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT ) )
368 return DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[index];
372 // index out-of-bounds
373 static const std::string INVALID_PROPERTY_NAME;
374 return INVALID_PROPERTY_NAME;
379 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
381 Property::Index index = Property::INVALID_INDEX;
383 DALI_ASSERT_DEBUG( NULL != mDefaultImageActorPropertyLookup );
385 // Look for name in current class' default properties
386 DefaultPropertyLookup::const_iterator result = mDefaultImageActorPropertyLookup->find( name );
387 if ( mDefaultImageActorPropertyLookup->end() != result )
389 index = result->second;
393 // If not found, check in base class
394 index = RenderableActor::GetDefaultPropertyIndex( name );
400 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
402 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
404 RenderableActor::SetDefaultProperty(index, propertyValue);
410 case Dali::ImageActor::PIXEL_AREA:
412 SetPixelArea(propertyValue.Get<Rect<int> >());
415 case Dali::ImageActor::STYLE:
417 SetStyle(StyleEnum(propertyValue.Get<std::string>()));
420 case Dali::ImageActor::BORDER:
422 SetNinePatchBorder( propertyValue.Get<Vector4>(), true /*in pixels*/ );
425 case Dali::ImageActor::IMAGE:
427 Dali::Image img = Scripting::NewImage( propertyValue );
430 ImagePtr image( &GetImplementation(img) );
435 DALI_LOG_WARNING("Cannot create image from property value\n");
441 DALI_LOG_WARNING("Unknown property (%d)\n", index);
449 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
452 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
454 ret = RenderableActor::GetDefaultProperty(index);
460 case Dali::ImageActor::PIXEL_AREA:
462 Rect<int> r = GetPixelArea();
466 case Dali::ImageActor::STYLE:
468 ret = StyleString(GetStyle());
471 case Dali::ImageActor::BORDER:
473 ret = GetNinePatchBorder();
476 case Dali::ImageActor::IMAGE:
479 Scripting::CreatePropertyMap( Dali::Image( mImageAttachment->GetImage().Get() ), map );
480 ret = Property::Value( map );
485 DALI_LOG_WARNING("Unknown property (%d)\n", index);
494 } // namespace Internal