2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include <dali/internal/event/actors/image-actor-impl.h>
21 #include <dali/internal/event/images/nine-patch-image-impl.h>
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/internal/event/common/property-index-ranges.h>
24 #include <dali/internal/event/images/image-connector.h>
25 #include <dali/public-api/scripting/scripting.h>
30 const Property::Index ImageActor::PIXEL_AREA = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
31 const Property::Index ImageActor::FADE_IN = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 1;
32 const Property::Index ImageActor::FADE_IN_DURATION = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 2;
33 const Property::Index ImageActor::STYLE = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 3;
34 const Property::Index ImageActor::BORDER = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 4;
35 const Property::Index ImageActor::IMAGE = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 5;
39 bool ImageActor::mFirstInstance = true;
40 Actor::DefaultPropertyLookup* ImageActor::mDefaultImageActorPropertyLookup = NULL;
47 return Dali::ImageActor::New();
50 TypeRegistration mType( typeid(Dali::ImageActor), typeid(Dali::RenderableActor), Create );
52 const std::string DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[] =
61 const int DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT = sizeof( DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES ) / sizeof( std::string );
63 const Property::Type DEFAULT_IMAGE_ACTOR_PROPERTY_TYPES[DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT] =
65 Property::RECTANGLE, // "pixel-area",
66 Property::BOOLEAN, // "fade-in",
67 Property::FLOAT, // "fade-in-duration",
68 Property::STRING, // "style",
69 Property::VECTOR4, // "border",
70 Property::MAP, // "image",
73 ImageActor::Style StyleEnum(const std::string &s)
75 if(s == "STYLE_NINE_PATCH")
77 return Dali::ImageActor::STYLE_NINE_PATCH;
79 else // if(s == "QUAD")
81 return Dali::ImageActor::STYLE_QUAD;
85 std::string StyleString(const ImageActor::Style style)
87 if(style == Dali::ImageActor::STYLE_NINE_PATCH)
89 return "STYLE_NINE_PATCH";
91 else // if(s == "QUAD")
98 ImageActorPtr ImageActor::New( Image* anImage )
100 ImageActorPtr actor( new ImageActor() );
101 ImagePtr theImage( anImage );
103 // Second-phase construction
105 NinePatchImage* ninePatchImage = NULL;
107 // Automatically convert upcasted nine-patch images to cropped bitmap
108 ninePatchImage = NinePatchImage::GetNinePatchImage( anImage );
110 if( ninePatchImage != NULL )
112 theImage = ninePatchImage->CreateCroppedBitmapImage();
115 // Create the attachment
116 actor->mImageAttachment = ImageAttachment::New( *actor->mNode, theImage.Get() );
117 actor->Attach( *actor->mImageAttachment );
119 // Adjust the actor's size
122 actor->mImageNext.Set( theImage.Get(), false );
123 actor->OnImageSet( *theImage );
124 actor->SetNaturalSize( *theImage );
127 if( ninePatchImage != NULL )
129 actor->SetStyle( Dali::ImageActor::STYLE_NINE_PATCH );
130 Vector4 border = ninePatchImage->GetStretchBorders();
131 actor->SetNinePatchBorder( border, true );
138 ImageActorPtr ImageActor::New( Image* image, const PixelArea& pixelArea )
141 ImageActorPtr actor = New( image );
142 // then set the pixel area
143 actor->mImageAttachment->SetPixelArea( pixelArea );
147 void ImageActor::OnInitialize()
149 if(ImageActor::mFirstInstance)
151 mDefaultImageActorPropertyLookup = new DefaultPropertyLookup();
152 const int start = DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
153 for ( int i = 0; i < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT; ++i )
155 (*mDefaultImageActorPropertyLookup)[DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[i]] = i + start;
157 ImageActor::mFirstInstance = false;
161 void ImageActor::SetImage( Image* image )
163 Image* currentImage = static_cast<Image*>(mImageAttachment->GetImage().GetObjectPtr());
164 // early exit if it's the same image
165 if ( currentImage == image || mImageNext.Get() == image )
170 mLoadedConnection.DisconnectAll();
172 mImageNext.Set( image, OnStage() );
176 mImageAttachment->SetImage( NULL );
180 // don't disconnect currently shown image until we made sure that the new one is loaded
181 OnImageSet( *image );
185 Dali::Image ImageActor::GetImage()
187 return mImageAttachment->GetImage();
190 void ImageActor::SetToNaturalSize()
192 mUsingNaturalSize = true;
193 Dali::Image image = mImageAttachment->GetImage();
197 SetNaturalSize( GetImplementation(image) );
201 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
203 mImageAttachment->SetPixelArea( pixelArea );
205 if( mUsingNaturalSize )
207 mInternalSetSize = true;
208 SetSize(pixelArea.width, pixelArea.height);
209 mInternalSetSize = false;
213 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
215 return mImageAttachment->GetPixelArea();
218 bool ImageActor::IsPixelAreaSet() const
220 return mImageAttachment->IsPixelAreaSet();
223 void ImageActor::ClearPixelArea()
225 mImageAttachment->ClearPixelArea();
227 if( mUsingNaturalSize )
229 Dali::Image image = mImageAttachment->GetImage();
232 mInternalSetSize = true;
233 SetSize(GetImplementation(image).GetNaturalSize());
234 mInternalSetSize = false;
239 void ImageActor::SetStyle( Style style )
241 mImageAttachment->SetStyle( style );
244 ImageActor::Style ImageActor::GetStyle() const
246 return mImageAttachment->GetStyle();
249 void ImageActor::SetNinePatchBorder( const Vector4& border, bool inPixels )
251 mImageAttachment->SetNinePatchBorder( border, inPixels );
254 Vector4 ImageActor::GetNinePatchBorder() const
256 return mImageAttachment->GetNinePatchBorder();
259 void ImageActor::SetFadeIn( bool enableFade )
261 mFadeIn = enableFade;
264 bool ImageActor::GetFadeIn() const
269 void ImageActor::SetFadeInDuration( float durationSeconds )
271 mFadeInDuration = durationSeconds;
274 float ImageActor::GetFadeInDuration() const
276 return mFadeInDuration;
279 ImageAttachment& ImageActor::GetImageAttachment()
281 return *mImageAttachment;
284 Vector2 ImageActor::GetCurrentImageSize() const
289 // get the texture size / pixel area if only a subset of it is displayed
290 if( IsPixelAreaSet() )
292 PixelArea area(GetPixelArea());
293 return Vector2(area.width, area.height );
297 return Vector2( GetCurrentSize() );
301 RenderableAttachment& ImageActor::GetRenderableAttachment() const
303 DALI_ASSERT_DEBUG( mImageAttachment && "ImageAttachment missing from ImageActor" );
304 return *mImageAttachment;
307 ImageActor::ImageActor()
309 mUsingNaturalSize(true),
310 mInternalSetSize(false),
312 mFadeInitial( true ),
313 mLoadedConnection( this ),
314 mFadeInDuration( 1.0f )
318 ImageActor::~ImageActor()
320 // ScopedConnection disconnects automatically
323 void ImageActor::OnImageSet( Image& image )
325 // observe image loaded
326 if( Dali::ResourceLoading == image.GetLoadingState() && ! image.GetFilename().empty() )
328 image.LoadingFinishedSignal().Connect( mLoadedConnection, &ImageActor::ImageLoaded );
332 // image already loaded, or generated
333 ImageLoaded( Dali::Image(&image) );
337 void ImageActor::SetNaturalSize( Image& image )
339 if( mUsingNaturalSize )
342 if( IsPixelAreaSet() )
344 PixelArea area(GetPixelArea());
345 size.width = area.width;
346 size.height = area.height;
350 size = image.GetNaturalSize();
353 mInternalSetSize = true;
355 mInternalSetSize = false;
359 void ImageActor::OnSizeSet( const Vector3& targetSize )
361 if( !mInternalSetSize )
363 mUsingNaturalSize = false;
367 void ImageActor::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
369 mUsingNaturalSize = false;
372 void ImageActor::OnStageConnectionInternal()
376 mImageNext.OnStageConnect();
379 void ImageActor::OnStageDisconnectionInternal()
381 mImageNext.OnStageDisconnect();
384 void ImageActor::ImageLoaded( Dali::Image image )
386 DALI_ASSERT_DEBUG (image && "Image handle empty!");
388 // TODO: Handle case where image loading failed
390 // Need to keep mUploadedConnection connected as image may change later through Reload
391 // Note: Reloaded images may have changed size.
393 // Set the attachment's image once we know the image has loaded to prevent
394 // blank frames during load / reload.
395 mImageAttachment->SetImage( &GetImplementation( image ) );
397 // If size has never been set by application
398 if( mUsingNaturalSize )
400 // If a pixel area has been set, use this size
401 if( IsPixelAreaSet() )
403 const PixelArea& area = GetPixelArea();
404 mInternalSetSize = true;
405 SetSize(area.width, area.height);
406 mInternalSetSize = false;
410 mInternalSetSize = true;
411 SetSize( GetImplementation(image).GetNaturalSize() );
412 mInternalSetSize = false;
416 // fade in if required
420 void ImageActor::FadeIn()
422 // only fade in if enabled and newly displayed on screen
423 if( mFadeIn && mFadeInitial && ( mFadeInDuration > 0.0f ) )
425 // need to set opacity immediately to 0 otherwise child actors might get rendered
428 Dali::Image image = mImageAttachment->GetImage();
430 // Fade-in when on-stage & the image is loaded
433 image.GetLoadingState() == Dali::ResourceLoadingSucceeded)
435 // fire and forget animation; will clean up after it's finished
436 Dali::Animation animation = Dali::Animation::New( mFadeInDuration );
437 animation.OpacityTo( Dali::Actor( this ), 1.0f, AlphaFunctions::EaseOut );
439 mFadeInitial = false;
444 unsigned int ImageActor::GetDefaultPropertyCount() const
446 return RenderableActor::GetDefaultPropertyCount() + DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT;
449 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
451 RenderableActor::GetDefaultPropertyIndices( indices ); // RenderableActor class properties
453 indices.reserve( indices.size() + DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT );
455 int index = DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
456 for ( int i = 0; i < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT; ++i, ++index )
458 indices.push_back( index );
462 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
464 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
466 return RenderableActor::IsDefaultPropertyWritable(index);
474 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
476 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
478 return RenderableActor::IsDefaultPropertyAnimatable(index);
486 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
488 if( index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT )
490 return RenderableActor::IsDefaultPropertyAConstraintInput(index);
492 return true; // Our properties can be used as input to constraints.
495 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
497 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
499 return RenderableActor::GetDefaultPropertyType(index);
503 index -= DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
505 if ( ( index >= 0 ) && ( index < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT ) )
507 return DEFAULT_IMAGE_ACTOR_PROPERTY_TYPES[index];
511 // index out-of-bounds
512 return Property::NONE;
517 const std::string& ImageActor::GetDefaultPropertyName( Property::Index index ) const
519 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
521 return RenderableActor::GetDefaultPropertyName(index);
525 index -= DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
527 if ( ( index >= 0 ) && ( index < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT ) )
529 return DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[index];
533 // index out-of-bounds
534 static const std::string INVALID_PROPERTY_NAME;
535 return INVALID_PROPERTY_NAME;
540 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
542 Property::Index index = Property::INVALID_INDEX;
544 DALI_ASSERT_DEBUG( NULL != mDefaultImageActorPropertyLookup );
546 // Look for name in current class' default properties
547 DefaultPropertyLookup::const_iterator result = mDefaultImageActorPropertyLookup->find( name );
548 if ( mDefaultImageActorPropertyLookup->end() != result )
550 index = result->second;
554 // If not found, check in base class
555 index = RenderableActor::GetDefaultPropertyIndex( name );
561 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
563 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
565 RenderableActor::SetDefaultProperty(index, propertyValue);
571 case Dali::ImageActor::PIXEL_AREA:
573 SetPixelArea(propertyValue.Get<Rect<int> >());
576 case Dali::ImageActor::FADE_IN:
578 SetFadeIn(propertyValue.Get<bool>());
581 case Dali::ImageActor::FADE_IN_DURATION:
583 SetFadeInDuration(propertyValue.Get<float>());
586 case Dali::ImageActor::STYLE:
588 SetStyle(StyleEnum(propertyValue.Get<std::string>()));
591 case Dali::ImageActor::BORDER:
593 SetNinePatchBorder( propertyValue.Get<Vector4>(), true /*in pixels*/ );
596 case Dali::ImageActor::IMAGE:
598 Dali::Image img = Scripting::NewImage( propertyValue );
601 SetImage( &GetImplementation(img) );
605 DALI_LOG_WARNING("Cannot create image from property value\n");
611 DALI_LOG_WARNING("Unknown property (%d)\n", index);
619 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
622 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
624 ret = RenderableActor::GetDefaultProperty(index);
630 case Dali::ImageActor::PIXEL_AREA:
632 Rect<int> r = GetPixelArea();
636 case Dali::ImageActor::FADE_IN:
641 case Dali::ImageActor::FADE_IN_DURATION:
643 ret = GetFadeInDuration();
646 case Dali::ImageActor::STYLE:
648 ret = StyleString(GetStyle());
651 case Dali::ImageActor::BORDER:
653 ret = GetNinePatchBorder();
656 case Dali::ImageActor::IMAGE:
659 Scripting::CreatePropertyMap( mImageAttachment->GetImage(), map );
660 ret = Property::Value( map );
665 DALI_LOG_WARNING("Unknown property (%d)\n", index);
675 } // namespace Internal