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 == "STYLE_NINE_PATCH_NO_CENTER")
76 return Dali::ImageActor::STYLE_NINE_PATCH_NO_CENTER;
78 else // if(s == "QUAD")
80 return Dali::ImageActor::STYLE_QUAD;
84 std::string StyleString(const ImageActor::Style style)
86 if(style == Dali::ImageActor::STYLE_NINE_PATCH)
88 return "STYLE_NINE_PATCH";
90 else if(style == Dali::ImageActor::STYLE_NINE_PATCH_NO_CENTER)
92 return "STYLE_NINE_PATCH_NO_CENTER";
94 else // if(s == "QUAD")
101 ImageActorPtr ImageActor::New()
103 ImageActorPtr actor( new ImageActor );
105 // Second-phase construction of base class
108 // Create the attachment
109 actor->mImageAttachment = ImageAttachment::New( *actor->mNode );
110 actor->Attach( *actor->mImageAttachment );
115 void ImageActor::OnInitialize()
117 if(ImageActor::mFirstInstance)
119 mDefaultImageActorPropertyLookup = new DefaultPropertyLookup();
120 const int start = DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
121 for ( int i = 0; i < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT; ++i )
123 (*mDefaultImageActorPropertyLookup)[DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[i]] = i + start;
125 ImageActor::mFirstInstance = false;
129 void ImageActor::SetImage( ImagePtr& image )
131 ImagePtr currentImage = mImageAttachment->GetImage();
132 // early exit if it's the same image as we already have
133 if ( currentImage == image )
138 // NOTE! image might be pointing to NULL, which is fine as in that case app wants to just remove the image
139 ImagePtr newImage( image );
140 // if image is not NULL, check for 9 patch
143 // Automatically convert nine-patch images to cropped bitmap
144 NinePatchImage* ninePatchImage = NinePatchImage::DownCast( image.Get() );
147 newImage = ninePatchImage->CreateCroppedBitmapImage();
148 SetStyle( Dali::ImageActor::STYLE_NINE_PATCH );
149 SetNinePatchBorder( ninePatchImage->GetStretchBorders(), true );
152 // set the actual image (normal or 9 patch) and natural size based on that
153 mImageAttachment->SetImage( newImage );
157 ImagePtr ImageActor::GetImage()
159 return mImageAttachment->GetImage();
162 void ImageActor::SetToNaturalSize()
164 mUsingNaturalSize = true;
169 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
171 mImageAttachment->SetPixelArea( pixelArea );
176 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
178 return mImageAttachment->GetPixelArea();
181 bool ImageActor::IsPixelAreaSet() const
183 return mImageAttachment->IsPixelAreaSet();
186 void ImageActor::ClearPixelArea()
188 mImageAttachment->ClearPixelArea();
190 if( mUsingNaturalSize )
192 ImagePtr image = mImageAttachment->GetImage();
195 mInternalSetSize = true;
196 SetSize( image->GetNaturalSize() );
197 mInternalSetSize = false;
202 void ImageActor::SetStyle( Style style )
204 mImageAttachment->SetStyle( style );
207 ImageActor::Style ImageActor::GetStyle() const
209 return mImageAttachment->GetStyle();
212 void ImageActor::SetNinePatchBorder( const Vector4& border, bool inPixels )
214 mImageAttachment->SetNinePatchBorder( border, inPixels );
217 Vector4 ImageActor::GetNinePatchBorder() const
219 return mImageAttachment->GetNinePatchBorder();
222 ImageAttachment& ImageActor::GetImageAttachment()
224 return *mImageAttachment;
227 RenderableAttachment& ImageActor::GetRenderableAttachment() const
229 DALI_ASSERT_DEBUG( mImageAttachment && "ImageAttachment missing from ImageActor" );
230 return *mImageAttachment;
233 ImageActor::ImageActor()
235 mUsingNaturalSize(true),
236 mInternalSetSize(false)
240 ImageActor::~ImageActor()
244 void ImageActor::SetNaturalSize()
246 if( mUsingNaturalSize )
248 mInternalSetSize = true;
249 SetSize( CalculateNaturalSize() );
250 mInternalSetSize = false;
254 Vector3 ImageActor::GetNaturalSize() const
256 Vector2 naturalSize( CalculateNaturalSize() );
257 return Vector3( naturalSize.width, naturalSize.height, CalculateSizeZ( naturalSize ) );
260 Vector2 ImageActor::CalculateNaturalSize() const
262 // if no image then natural size is 0
263 Vector2 size( 0.0f, 0.0f );
265 ImagePtr image = mImageAttachment->GetImage();
268 if( IsPixelAreaSet() )
270 PixelArea area(GetPixelArea());
271 size.width = area.width;
272 size.height = area.height;
276 size = image->GetNaturalSize();
283 void ImageActor::OnSizeSet( const Vector3& targetSize )
285 if( !mInternalSetSize )
287 mUsingNaturalSize = false;
291 void ImageActor::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
293 mUsingNaturalSize = false;
296 void ImageActor::OnStageConnectionInternal()
300 void ImageActor::OnStageDisconnectionInternal()
304 unsigned int ImageActor::GetDefaultPropertyCount() const
306 return RenderableActor::GetDefaultPropertyCount() + DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT;
309 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
311 RenderableActor::GetDefaultPropertyIndices( indices ); // RenderableActor class properties
313 indices.reserve( indices.size() + DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT );
315 int index = DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
316 for ( int i = 0; i < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT; ++i, ++index )
318 indices.push_back( index );
322 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
324 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
326 return RenderableActor::IsDefaultPropertyWritable(index);
334 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
336 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
338 return RenderableActor::IsDefaultPropertyAnimatable(index);
346 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
348 if( index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT )
350 return RenderableActor::IsDefaultPropertyAConstraintInput(index);
352 return true; // Our properties can be used as input to constraints.
355 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
357 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
359 return RenderableActor::GetDefaultPropertyType(index);
363 index -= DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
365 if ( ( index >= 0 ) && ( index < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT ) )
367 return DEFAULT_IMAGE_ACTOR_PROPERTY_TYPES[index];
371 // index out-of-bounds
372 return Property::NONE;
377 const std::string& ImageActor::GetDefaultPropertyName( Property::Index index ) const
379 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
381 return RenderableActor::GetDefaultPropertyName(index);
385 index -= DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
387 if ( ( index >= 0 ) && ( index < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT ) )
389 return DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[index];
393 // index out-of-bounds
394 static const std::string INVALID_PROPERTY_NAME;
395 return INVALID_PROPERTY_NAME;
400 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
402 Property::Index index = Property::INVALID_INDEX;
404 DALI_ASSERT_DEBUG( NULL != mDefaultImageActorPropertyLookup );
406 // Look for name in current class' default properties
407 DefaultPropertyLookup::const_iterator result = mDefaultImageActorPropertyLookup->find( name );
408 if ( mDefaultImageActorPropertyLookup->end() != result )
410 index = result->second;
414 // If not found, check in base class
415 index = RenderableActor::GetDefaultPropertyIndex( name );
421 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
423 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
425 RenderableActor::SetDefaultProperty(index, propertyValue);
431 case Dali::ImageActor::PIXEL_AREA:
433 SetPixelArea(propertyValue.Get<Rect<int> >());
436 case Dali::ImageActor::STYLE:
438 SetStyle(StyleEnum(propertyValue.Get<std::string>()));
441 case Dali::ImageActor::BORDER:
443 SetNinePatchBorder( propertyValue.Get<Vector4>(), true /*in pixels*/ );
446 case Dali::ImageActor::IMAGE:
448 Dali::Image img = Scripting::NewImage( propertyValue );
451 ImagePtr image( &GetImplementation(img) );
456 DALI_LOG_WARNING("Cannot create image from property value\n");
462 DALI_LOG_WARNING("Unknown property (%d)\n", index);
470 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
473 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
475 ret = RenderableActor::GetDefaultProperty(index);
481 case Dali::ImageActor::PIXEL_AREA:
483 Rect<int> r = GetPixelArea();
487 case Dali::ImageActor::STYLE:
489 ret = StyleString(GetStyle());
492 case Dali::ImageActor::BORDER:
494 ret = GetNinePatchBorder();
497 case Dali::ImageActor::IMAGE:
500 Scripting::CreatePropertyMap( Dali::Image( mImageAttachment->GetImage().Get() ), map );
501 ret = Property::Value( map );
506 DALI_LOG_WARNING("Unknown property (%d)\n", index);
515 } // namespace Internal