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/public-api/object/type-registry.h>
22 #include <dali/internal/event/common/property-index-ranges.h>
23 #include <dali/internal/event/images/image-connector.h>
24 #include <dali/public-api/scripting/scripting.h>
29 const Property::Index ImageActor::PIXEL_AREA = RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT;
30 const Property::Index ImageActor::FADE_IN = RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT + 1;
31 const Property::Index ImageActor::FADE_IN_DURATION = RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT + 2;
32 const Property::Index ImageActor::STYLE = RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT + 3;
33 const Property::Index ImageActor::BORDER = RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT + 4;
34 const Property::Index ImageActor::IMAGE = RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT + 5;
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[] =
60 const int DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT = sizeof( DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES ) / sizeof( std::string );
62 const Property::Type DEFAULT_IMAGE_ACTOR_PROPERTY_TYPES[DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT] =
64 Property::RECTANGLE, // "pixel-area",
65 Property::BOOLEAN, // "fade-in",
66 Property::FLOAT, // "fade-in-duration",
67 Property::STRING, // "style",
68 Property::VECTOR4, // "border",
69 Property::MAP, // "image",
72 ImageActor::Style StyleEnum(const std::string &s)
76 return Dali::ImageActor::STYLE_NINE_PATCH;
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)
90 else // if(s == "QUAD")
97 ImageActorPtr ImageActor::New( Image* image )
99 ImageActorPtr actor( new ImageActor() );
101 // Second-phase construction
104 // Create the attachment
105 actor->mImageAttachment = ImageAttachment::New( *actor->mNode, image );
106 actor->Attach( *actor->mImageAttachment );
108 // Adjust the actor's size
111 actor->mImageNext.Set( image, false );
112 actor->OnImageSet( *image );
113 actor->SetNaturalSize( *image );
119 ImageActorPtr ImageActor::New( Image* image, const PixelArea& pixelArea )
122 ImageActorPtr actor = New( image );
123 // then set the pixel area
124 actor->mImageAttachment->SetPixelArea( pixelArea );
128 void ImageActor::OnInitialize()
130 if(ImageActor::mFirstInstance)
132 mDefaultImageActorPropertyLookup = new DefaultPropertyLookup();
133 const int start = RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT;
134 for ( int i = 0; i < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT; ++i )
136 (*mDefaultImageActorPropertyLookup)[DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[i]] = i + start;
138 ImageActor::mFirstInstance = false;
142 void ImageActor::SetImage( Image* image )
144 Image* currentImage = static_cast<Image*>(mImageAttachment->GetImage().GetObjectPtr());
145 // early exit if it's the same image
146 if ( currentImage == image || mImageNext.Get() == image )
151 mLoadedConnection.DisconnectAll();
153 mImageNext.Set( image, OnStage() );
157 mImageAttachment->SetImage( NULL );
161 // don't disconnect currently shown image until we made sure that the new one is loaded
162 OnImageSet( *image );
166 Dali::Image ImageActor::GetImage()
168 return mImageAttachment->GetImage();
171 void ImageActor::SetToNaturalSize()
173 mUsingNaturalSize = true;
174 Dali::Image image = mImageAttachment->GetImage();
178 SetNaturalSize( GetImplementation(image) );
182 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
184 mImageAttachment->SetPixelArea( pixelArea );
186 if( mUsingNaturalSize )
188 mInternalSetSize = true;
189 SetSize(pixelArea.width, pixelArea.height);
190 mInternalSetSize = false;
194 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
196 return mImageAttachment->GetPixelArea();
199 bool ImageActor::IsPixelAreaSet() const
201 return mImageAttachment->IsPixelAreaSet();
204 void ImageActor::ClearPixelArea()
206 mImageAttachment->ClearPixelArea();
208 if( mUsingNaturalSize )
210 Dali::Image image = mImageAttachment->GetImage();
213 mInternalSetSize = true;
214 SetSize(GetImplementation(image).GetNaturalSize());
215 mInternalSetSize = false;
220 void ImageActor::SetStyle( Style style )
222 mImageAttachment->SetStyle( style );
225 ImageActor::Style ImageActor::GetStyle() const
227 return mImageAttachment->GetStyle();
230 void ImageActor::SetNinePatchBorder( const Vector4& border, bool inPixels )
232 mImageAttachment->SetNinePatchBorder( border, inPixels );
235 Vector4 ImageActor::GetNinePatchBorder() const
237 return mImageAttachment->GetNinePatchBorder();
240 void ImageActor::SetFadeIn( bool enableFade )
242 mFadeIn = enableFade;
245 bool ImageActor::GetFadeIn() const
250 void ImageActor::SetFadeInDuration( float durationSeconds )
252 mFadeInDuration = durationSeconds;
255 float ImageActor::GetFadeInDuration() const
257 return mFadeInDuration;
260 ImageAttachment& ImageActor::GetImageAttachment()
262 return *mImageAttachment;
265 Vector2 ImageActor::GetCurrentImageSize() const
270 // get the texture size / pixel area if only a subset of it is displayed
271 if( IsPixelAreaSet() )
273 PixelArea area(GetPixelArea());
274 return Vector2(area.width, area.height );
278 return Vector2( GetCurrentSize() );
282 RenderableAttachment& ImageActor::GetRenderableAttachment() const
284 DALI_ASSERT_DEBUG( mImageAttachment && "ImageAttachment missing from ImageActor" );
285 return *mImageAttachment;
288 ImageActor::ImageActor()
290 mUsingNaturalSize(true),
291 mInternalSetSize(false),
293 mFadeInitial( true ),
294 mLoadedConnection( this ),
295 mFadeInDuration( 1.0f )
299 ImageActor::~ImageActor()
301 // ScopedConnection disconnects automatically
304 void ImageActor::OnImageSet( Image& image )
306 // observe image loaded
307 if( Dali::ResourceLoading == image.GetLoadingState() && ! image.GetFilename().empty() )
309 image.LoadingFinishedSignal().Connect( mLoadedConnection, &ImageActor::ImageLoaded );
313 // image already loaded, or generated
314 ImageLoaded( Dali::Image(&image) );
318 void ImageActor::SetNaturalSize( Image& image )
320 if( mUsingNaturalSize )
323 if( IsPixelAreaSet() )
325 PixelArea area(GetPixelArea());
326 size.width = area.width;
327 size.height = area.height;
331 size = image.GetNaturalSize();
334 mInternalSetSize = true;
336 mInternalSetSize = false;
340 void ImageActor::OnSizeSet( const Vector3& targetSize )
342 if( !mInternalSetSize )
344 mUsingNaturalSize = false;
348 void ImageActor::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
350 mUsingNaturalSize = false;
353 void ImageActor::OnStageConnectionInternal()
357 mImageNext.OnStageConnect();
360 void ImageActor::OnStageDisconnectionInternal()
362 mImageNext.OnStageDisconnect();
365 void ImageActor::ImageLoaded( Dali::Image image )
367 DALI_ASSERT_DEBUG (image && "Image handle empty!");
369 // TODO: Handle case where image loading failed
371 // Need to keep mUploadedConnection connected as image may change later through Reload
372 // Note: Reloaded images may have changed size.
374 // Set the attachment's image once we know the image has loaded to prevent
375 // blank frames during load / reload.
376 mImageAttachment->SetImage( &GetImplementation( image ) );
378 // If size has never been set by application
379 if( mUsingNaturalSize )
381 // If a pixel area has been set, use this size
382 if( IsPixelAreaSet() )
384 const PixelArea& area = GetPixelArea();
385 mInternalSetSize = true;
386 SetSize(area.width, area.height);
387 mInternalSetSize = false;
391 mInternalSetSize = true;
392 SetSize( GetImplementation(image).GetNaturalSize() );
393 mInternalSetSize = false;
397 // fade in if required
401 void ImageActor::FadeIn()
403 // only fade in if enabled and newly displayed on screen
404 if( mFadeIn && mFadeInitial && ( mFadeInDuration > 0.0f ) )
406 // need to set opacity immediately to 0 otherwise child actors might get rendered
409 Dali::Image image = mImageAttachment->GetImage();
411 // Fade-in when on-stage & the image is loaded
414 image.GetLoadingState() == Dali::ResourceLoadingSucceeded)
416 // fire and forget animation; will clean up after it's finished
417 Dali::Animation animation = Dali::Animation::New( mFadeInDuration );
418 animation.OpacityTo( Dali::Actor( this ), 1.0f, AlphaFunctions::EaseOut );
420 mFadeInitial = false;
425 unsigned int ImageActor::GetDefaultPropertyCount() const
427 return RenderableActor::GetDefaultPropertyCount() + DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT;
430 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
432 if(static_cast<unsigned int>(index) < RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT)
434 return RenderableActor::IsDefaultPropertyWritable(index);
442 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
444 if(static_cast<unsigned int>(index) < RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT)
446 return RenderableActor::IsDefaultPropertyAnimatable(index);
454 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
456 if(static_cast<unsigned int>(index) < RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT)
458 return RenderableActor::GetDefaultPropertyType(index);
462 // ProxyObject guarantees that index is within range
463 return DEFAULT_IMAGE_ACTOR_PROPERTY_TYPES[index - RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT];
467 const std::string& ImageActor::GetDefaultPropertyName( Property::Index index ) const
469 if(static_cast<unsigned int>(index) < RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT)
471 return RenderableActor::GetDefaultPropertyName(index);
475 // ProxyObject guarantees that index is within range
476 return DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[index - RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT];
480 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
482 Property::Index index = Property::INVALID_INDEX;
484 DALI_ASSERT_DEBUG( NULL != mDefaultImageActorPropertyLookup );
486 // Look for name in current class' default properties
487 DefaultPropertyLookup::const_iterator result = mDefaultImageActorPropertyLookup->find( name );
488 if ( mDefaultImageActorPropertyLookup->end() != result )
490 index = result->second;
494 // If not found, check in base class
495 index = RenderableActor::GetDefaultPropertyIndex( name );
501 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
503 // ProxyObject guarantees the property is writable and index is in range
505 if(index < RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT)
507 RenderableActor::SetDefaultProperty(index, propertyValue);
513 case Dali::ImageActor::PIXEL_AREA:
515 SetPixelArea(propertyValue.Get<Rect<int> >());
518 case Dali::ImageActor::FADE_IN:
520 SetFadeIn(propertyValue.Get<bool>());
523 case Dali::ImageActor::FADE_IN_DURATION:
525 SetFadeInDuration(propertyValue.Get<float>());
528 case Dali::ImageActor::STYLE:
530 SetStyle(StyleEnum(propertyValue.Get<std::string>()));
533 case Dali::ImageActor::BORDER:
535 SetNinePatchBorder(propertyValue.Get<Vector4>());
538 case Dali::ImageActor::IMAGE:
540 Dali::Image img = Scripting::NewImage( propertyValue );
543 SetImage( &GetImplementation(img) );
547 DALI_LOG_WARNING("Cannot create image from property value\n");
553 DALI_LOG_WARNING("Unknown property (%d)\n", index);
561 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
564 if(index < RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT)
566 ret = RenderableActor::GetDefaultProperty(index);
572 case Dali::ImageActor::PIXEL_AREA:
574 Rect<int> r = GetPixelArea();
578 case Dali::ImageActor::FADE_IN:
583 case Dali::ImageActor::FADE_IN_DURATION:
585 ret = GetFadeInDuration();
588 case Dali::ImageActor::STYLE:
590 ret = StyleString(GetStyle());
593 case Dali::ImageActor::BORDER:
595 ret = GetNinePatchBorder();
598 case Dali::ImageActor::IMAGE:
600 ret = Property::Value(Property::MAP);
605 DALI_LOG_WARNING("Unknown property (%d)\n", index);
615 } // namespace Internal