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::FADE_IN = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 1;
33 const Property::Index ImageActor::FADE_IN_DURATION = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 2;
34 const Property::Index ImageActor::STYLE = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 3;
35 const Property::Index ImageActor::BORDER = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 4;
36 const Property::Index ImageActor::IMAGE = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 5;
40 bool ImageActor::mFirstInstance = true;
41 Actor::DefaultPropertyLookup* ImageActor::mDefaultImageActorPropertyLookup = NULL;
48 return Dali::ImageActor::New();
51 TypeRegistration mType( typeid(Dali::ImageActor), typeid(Dali::RenderableActor), Create );
53 const std::string DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[] =
62 const int DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT = sizeof( DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES ) / sizeof( std::string );
64 const Property::Type DEFAULT_IMAGE_ACTOR_PROPERTY_TYPES[DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT] =
66 Property::RECTANGLE, // "pixel-area",
67 Property::BOOLEAN, // "fade-in",
68 Property::FLOAT, // "fade-in-duration",
69 Property::STRING, // "style",
70 Property::VECTOR4, // "border",
71 Property::MAP, // "image",
74 ImageActor::Style StyleEnum(const std::string &s)
76 if(s == "STYLE_NINE_PATCH")
78 return Dali::ImageActor::STYLE_NINE_PATCH;
80 else // if(s == "QUAD")
82 return Dali::ImageActor::STYLE_QUAD;
86 std::string StyleString(const ImageActor::Style style)
88 if(style == Dali::ImageActor::STYLE_NINE_PATCH)
90 return "STYLE_NINE_PATCH";
92 else // if(s == "QUAD")
99 ImageActorPtr ImageActor::New( Image* anImage )
101 ImageActorPtr actor( new ImageActor() );
102 ImagePtr theImage( anImage );
104 // Second-phase construction of base class
107 // Create the attachment
108 actor->mImageAttachment = ImageAttachment::New( *actor->mNode, theImage.Get() );
109 actor->Attach( *actor->mImageAttachment );
111 // don't call the external version as attachment already has the image
112 actor->SetImageInternal( NULL, anImage );
118 ImageActorPtr ImageActor::New( Image* image, const PixelArea& pixelArea )
121 ImageActorPtr actor = New( image );
122 // then set the pixel area
123 actor->mImageAttachment->SetPixelArea( pixelArea );
127 void ImageActor::OnInitialize()
129 if(ImageActor::mFirstInstance)
131 mDefaultImageActorPropertyLookup = new DefaultPropertyLookup();
132 const int start = DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
133 for ( int i = 0; i < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT; ++i )
135 (*mDefaultImageActorPropertyLookup)[DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[i]] = i + start;
137 ImageActor::mFirstInstance = false;
141 void ImageActor::SetImage( Image* image )
143 Image* currentImage = static_cast<Image*>(mImageAttachment->GetImage().GetObjectPtr());
144 // early exit if it's the same image
145 if ( currentImage == image || mImageNext.Get() == image )
150 SetImageInternal( currentImage, image );
153 Dali::Image ImageActor::GetImage()
155 return mImageAttachment->GetImage();
158 void ImageActor::SetToNaturalSize()
160 mUsingNaturalSize = true;
161 Dali::Image image = mImageAttachment->GetImage();
165 SetNaturalSize( GetImplementation(image) );
169 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
171 mImageAttachment->SetPixelArea( pixelArea );
173 if( mUsingNaturalSize )
175 mInternalSetSize = true;
176 SetSize(pixelArea.width, pixelArea.height);
177 mInternalSetSize = false;
181 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
183 return mImageAttachment->GetPixelArea();
186 bool ImageActor::IsPixelAreaSet() const
188 return mImageAttachment->IsPixelAreaSet();
191 void ImageActor::ClearPixelArea()
193 mImageAttachment->ClearPixelArea();
195 if( mUsingNaturalSize )
197 Dali::Image image = mImageAttachment->GetImage();
200 mInternalSetSize = true;
201 SetSize(GetImplementation(image).GetNaturalSize());
202 mInternalSetSize = false;
207 void ImageActor::SetStyle( Style style )
209 mImageAttachment->SetStyle( style );
212 ImageActor::Style ImageActor::GetStyle() const
214 return mImageAttachment->GetStyle();
217 void ImageActor::SetNinePatchBorder( const Vector4& border, bool inPixels )
219 mImageAttachment->SetNinePatchBorder( border, inPixels );
222 Vector4 ImageActor::GetNinePatchBorder() const
224 return mImageAttachment->GetNinePatchBorder();
227 void ImageActor::SetFadeIn( bool enableFade )
229 mFadeIn = enableFade;
232 bool ImageActor::GetFadeIn() const
237 void ImageActor::SetFadeInDuration( float durationSeconds )
239 mFadeInDuration = durationSeconds;
242 float ImageActor::GetFadeInDuration() const
244 return mFadeInDuration;
247 ImageAttachment& ImageActor::GetImageAttachment()
249 return *mImageAttachment;
252 Vector2 ImageActor::GetCurrentImageSize() const
257 // get the texture size / pixel area if only a subset of it is displayed
258 if( IsPixelAreaSet() )
260 PixelArea area(GetPixelArea());
261 return Vector2(area.width, area.height );
265 return Vector2( GetCurrentSize() );
269 RenderableAttachment& ImageActor::GetRenderableAttachment() const
271 DALI_ASSERT_DEBUG( mImageAttachment && "ImageAttachment missing from ImageActor" );
272 return *mImageAttachment;
275 void ImageActor::SignalConnected( SlotObserver*, CallbackBase* )
277 // nothing to do as we only ever connect to one signal, which we disconnect from in the destructor
280 void ImageActor::SignalDisconnected( SlotObserver*, CallbackBase* )
282 // nothing to do as we only ever connect to one signal, which we disconnect from in the destructor
283 // also worth noting that we own the image whose signal we connect to so in practice this method is never called.
286 ImageActor::ImageActor()
288 mFadeInDuration( 1.0f ),
289 mUsingNaturalSize(true),
290 mInternalSetSize(false),
296 ImageActor::~ImageActor()
298 if( mImageAttachment )
300 Dali::Image image = mImageAttachment->GetImage();
303 // just call Disconnect as if not connected it is a no-op
304 image.LoadingFinishedSignal().Disconnect( this, &ImageActor::ImageLoaded );
309 void ImageActor::SetNaturalSize( Image& image )
311 if( mUsingNaturalSize )
314 if( IsPixelAreaSet() )
316 PixelArea area(GetPixelArea());
317 size.width = area.width;
318 size.height = area.height;
322 size = image.GetNaturalSize();
325 mInternalSetSize = true;
327 mInternalSetSize = false;
331 void ImageActor::OnSizeSet( const Vector3& targetSize )
333 if( !mInternalSetSize )
335 mUsingNaturalSize = false;
339 void ImageActor::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
341 mUsingNaturalSize = false;
344 void ImageActor::OnStageConnectionInternal()
348 mImageNext.OnStageConnect();
351 void ImageActor::OnStageDisconnectionInternal()
353 mImageNext.OnStageDisconnect();
356 void ImageActor::ImageLoaded( Dali::Image image )
358 DALI_ASSERT_DEBUG (image && "Image handle empty!");
360 // TODO: Handle case where image loading failed
362 // Need to keep mUploadedConnection connected as image may change later through Reload
363 // Note: Reloaded images may have changed size.
365 // Set the attachment's image once we know the image has loaded to prevent
366 // blank frames during load / reload.
367 mImageAttachment->SetImage( &GetImplementation( image ) );
369 // If size has never been set by application
370 if( mUsingNaturalSize )
372 // If a pixel area has been set, use this size
373 if( IsPixelAreaSet() )
375 const PixelArea& area = GetPixelArea();
376 mInternalSetSize = true;
377 SetSize(area.width, area.height);
378 mInternalSetSize = false;
382 mInternalSetSize = true;
383 SetSize( GetImplementation(image).GetNaturalSize() );
384 mInternalSetSize = false;
388 // fade in if required
392 void ImageActor::FadeIn()
394 // only fade in if enabled and newly displayed on screen
395 if( mFadeIn && mFadeInitial && ( mFadeInDuration > 0.0f ) )
397 // need to set opacity immediately to 0 otherwise child actors might get rendered
400 Dali::Image image = mImageAttachment->GetImage();
402 // Fade-in when on-stage & the image is loaded
405 image.GetLoadingState() == Dali::ResourceLoadingSucceeded)
407 // fire and forget animation; will clean up after it's finished
408 Dali::Animation animation = Dali::Animation::New( mFadeInDuration );
409 animation.OpacityTo( Dali::Actor( this ), 1.0f, AlphaFunctions::EaseOut );
411 mFadeInitial = false;
416 unsigned int ImageActor::GetDefaultPropertyCount() const
418 return RenderableActor::GetDefaultPropertyCount() + DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT;
421 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
423 RenderableActor::GetDefaultPropertyIndices( indices ); // RenderableActor class properties
425 indices.reserve( indices.size() + DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT );
427 int index = DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
428 for ( int i = 0; i < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT; ++i, ++index )
430 indices.push_back( index );
434 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
436 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
438 return RenderableActor::IsDefaultPropertyWritable(index);
446 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
448 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
450 return RenderableActor::IsDefaultPropertyAnimatable(index);
458 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
460 if( index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT )
462 return RenderableActor::IsDefaultPropertyAConstraintInput(index);
464 return true; // Our properties can be used as input to constraints.
467 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
469 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
471 return RenderableActor::GetDefaultPropertyType(index);
475 index -= DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
477 if ( ( index >= 0 ) && ( index < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT ) )
479 return DEFAULT_IMAGE_ACTOR_PROPERTY_TYPES[index];
483 // index out-of-bounds
484 return Property::NONE;
489 const std::string& ImageActor::GetDefaultPropertyName( Property::Index index ) const
491 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
493 return RenderableActor::GetDefaultPropertyName(index);
497 index -= DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
499 if ( ( index >= 0 ) && ( index < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT ) )
501 return DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[index];
505 // index out-of-bounds
506 static const std::string INVALID_PROPERTY_NAME;
507 return INVALID_PROPERTY_NAME;
512 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
514 Property::Index index = Property::INVALID_INDEX;
516 DALI_ASSERT_DEBUG( NULL != mDefaultImageActorPropertyLookup );
518 // Look for name in current class' default properties
519 DefaultPropertyLookup::const_iterator result = mDefaultImageActorPropertyLookup->find( name );
520 if ( mDefaultImageActorPropertyLookup->end() != result )
522 index = result->second;
526 // If not found, check in base class
527 index = RenderableActor::GetDefaultPropertyIndex( name );
533 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
535 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
537 RenderableActor::SetDefaultProperty(index, propertyValue);
543 case Dali::ImageActor::PIXEL_AREA:
545 SetPixelArea(propertyValue.Get<Rect<int> >());
548 case Dali::ImageActor::FADE_IN:
550 SetFadeIn(propertyValue.Get<bool>());
553 case Dali::ImageActor::FADE_IN_DURATION:
555 SetFadeInDuration(propertyValue.Get<float>());
558 case Dali::ImageActor::STYLE:
560 SetStyle(StyleEnum(propertyValue.Get<std::string>()));
563 case Dali::ImageActor::BORDER:
565 SetNinePatchBorder( propertyValue.Get<Vector4>(), true /*in pixels*/ );
568 case Dali::ImageActor::IMAGE:
570 Dali::Image img = Scripting::NewImage( propertyValue );
573 SetImage( &GetImplementation(img) );
577 DALI_LOG_WARNING("Cannot create image from property value\n");
583 DALI_LOG_WARNING("Unknown property (%d)\n", index);
591 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
594 if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
596 ret = RenderableActor::GetDefaultProperty(index);
602 case Dali::ImageActor::PIXEL_AREA:
604 Rect<int> r = GetPixelArea();
608 case Dali::ImageActor::FADE_IN:
613 case Dali::ImageActor::FADE_IN_DURATION:
615 ret = GetFadeInDuration();
618 case Dali::ImageActor::STYLE:
620 ret = StyleString(GetStyle());
623 case Dali::ImageActor::BORDER:
625 ret = GetNinePatchBorder();
628 case Dali::ImageActor::IMAGE:
631 Scripting::CreatePropertyMap( mImageAttachment->GetImage(), map );
632 ret = Property::Value( map );
637 DALI_LOG_WARNING("Unknown property (%d)\n", index);
646 void ImageActor::SetImageInternal( Image* currentImage, Image* image )
650 // just call Disconnect as if not connected it is a no-op
651 currentImage->LoadingFinishedSignal().Disconnect( this, &ImageActor::ImageLoaded );
654 ImagePtr imagePtr( image );
655 // Automatically convert nine-patch images to cropped bitmap
656 NinePatchImage* ninePatchImage = NinePatchImage::GetNinePatchImage( image );
659 imagePtr = ninePatchImage->CreateCroppedBitmapImage();
661 mImageNext.Set( imagePtr.Get(), OnStage() );
664 SetStyle( Dali::ImageActor::STYLE_NINE_PATCH );
665 SetNinePatchBorder( ninePatchImage->GetStretchBorders(), true );
669 mImageAttachment->SetImage( NULL );
673 // don't disconnect currently shown image until we made sure that the new one is loaded
674 if( Dali::ResourceLoading == image->GetLoadingState() && !image->GetFilename().empty() )
676 // observe image loading, @todo stop using signals internally!
677 image->LoadingFinishedSignal().Connect( this, &ImageActor::ImageLoaded );
681 // image already loaded, generated or 9 patch
682 ImageLoaded( Dali::Image( image ) );
687 } // namespace Internal