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")
98 ImageActorPtr ImageActor::New( Image* image )
100 ImageActorPtr actor( new ImageActor() );
102 // Second-phase construction
105 // Create the attachment
106 actor->mImageAttachment = ImageAttachment::New( *actor->mNode, image );
107 actor->Attach( *actor->mImageAttachment );
109 // Adjust the actor's size
112 actor->mImageNext.Set( image, false );
113 actor->OnImageSet( *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::SetPixelArea( const PixelArea& pixelArea )
173 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();
191 void ImageActor::SetStyle( Style style )
193 mImageAttachment->SetStyle( style );
196 ImageActor::Style ImageActor::GetStyle() const
198 return mImageAttachment->GetStyle();
201 void ImageActor::SetNinePatchBorder( const Vector4& border, bool inPixels )
203 mImageAttachment->SetNinePatchBorder( border, inPixels );
206 Vector4 ImageActor::GetNinePatchBorder() const
208 return mImageAttachment->GetNinePatchBorder();
211 void ImageActor::SetFadeIn( bool enableFade )
213 mFadeIn = enableFade;
216 bool ImageActor::GetFadeIn() const
221 void ImageActor::SetFadeInDuration( float durationSeconds )
223 mFadeInDuration = durationSeconds;
226 float ImageActor::GetFadeInDuration() const
228 return mFadeInDuration;
231 ImageAttachment& ImageActor::GetImageAttachment()
233 return *mImageAttachment;
236 Vector2 ImageActor::GetCurrentImageSize() const
241 // get the texture size / pixel area if only a subset of it is displayed
242 if (IsPixelAreaSet())
244 PixelArea area(GetPixelArea());
245 return Vector2(area.width, area.height );
249 return Vector2( GetCurrentSize() );
253 RenderableAttachment& ImageActor::GetRenderableAttachment() const
255 DALI_ASSERT_DEBUG( mImageAttachment && "ImageAttachment missing from ImageActor" );
256 return *mImageAttachment;
259 ImageActor::ImageActor()
261 mCustomSizeSet( false ),
263 mFadeInitial( true ),
264 mLoadedConnection( this ),
265 mFadeInDuration( 1.0f )
269 ImageActor::~ImageActor()
271 // ScopedConnection disconnects automatically
274 void ImageActor::OnImageSet( Image& image )
276 // observe image loaded
277 if( Dali::ResourceLoadingSucceeded == image.GetLoadingState() )
279 //image already loaded
280 ImageLoaded( Dali::Image(&image) );
284 image.LoadingFinishedSignal().Connect( mLoadedConnection, &ImageActor::ImageLoaded );
288 void ImageActor::OnSizeSet( const Vector3& targetSize )
290 // True if SizeSet() has ever been called
291 mCustomSizeSet = true;
294 void ImageActor::OnStageConnectionInternal()
298 mImageNext.OnStageConnect();
301 void ImageActor::OnStageDisconnectionInternal()
303 mImageNext.OnStageDisconnect();
306 void ImageActor::ImageLoaded( Dali::Image image )
308 DALI_ASSERT_DEBUG (image && "Image handle empty!");
310 // TODO: Handle case where image loading failed
312 // Need to keep mUploadedConnection connected as image may change later through Reload
313 // Note: Reloaded images may have changed size.
315 // Set the attachment's image once we know the image has loaded to prevent
316 // blank frames during load / reload.
317 mImageAttachment->SetImage( &GetImplementation( image ) );
319 // If size has never been set
320 if( !mCustomSizeSet )
322 // If a pixel area has been set, use this size
323 if (mImageAttachment->IsPixelAreaSet())
325 const PixelArea& area = mImageAttachment->GetPixelArea();
326 SetSize(area.width, area.height);
330 SetSize(image.GetWidth(), image.GetHeight());
334 // fade in if required
338 void ImageActor::FadeIn()
340 // only fade in if enabled and newly displayed on screen
341 if( mFadeIn && mFadeInitial && ( mFadeInDuration > 0.0f ) )
343 // need to set opacity immediately to 0 otherwise child actors might get rendered
346 Dali::Image image = mImageAttachment->GetImage();
348 // Fade-in when on-stage & the image is loaded
351 image.GetLoadingState() == Dali::ResourceLoadingSucceeded)
353 // fire and forget animation; will clean up after it's finished
354 Dali::Animation animation = Dali::Animation::New( mFadeInDuration );
355 animation.OpacityTo( Dali::Actor( this ), 1.0f, AlphaFunctions::EaseOut );
357 mFadeInitial = false;
362 unsigned int ImageActor::GetDefaultPropertyCount() const
364 return RenderableActor::GetDefaultPropertyCount() + DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT;
367 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
369 if(static_cast<unsigned int>(index) < RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT)
371 return RenderableActor::IsDefaultPropertyWritable(index);
379 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
381 if(static_cast<unsigned int>(index) < RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT)
383 return RenderableActor::IsDefaultPropertyAnimatable(index);
391 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
393 if(static_cast<unsigned int>(index) < RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT)
395 return RenderableActor::GetDefaultPropertyType(index);
399 // ProxyObject guarantees that index is within range
400 return DEFAULT_IMAGE_ACTOR_PROPERTY_TYPES[index - RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT];
404 const std::string& ImageActor::GetDefaultPropertyName( Property::Index index ) const
406 if(static_cast<unsigned int>(index) < RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT)
408 return RenderableActor::GetDefaultPropertyName(index);
412 // ProxyObject guarantees that index is within range
413 return DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[index - RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT];
417 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
419 Property::Index index = Property::INVALID_INDEX;
421 DALI_ASSERT_DEBUG( NULL != mDefaultImageActorPropertyLookup );
423 // Look for name in current class' default properties
424 DefaultPropertyLookup::const_iterator result = mDefaultImageActorPropertyLookup->find( name );
425 if ( mDefaultImageActorPropertyLookup->end() != result )
427 index = result->second;
431 // If not found, check in base class
432 index = RenderableActor::GetDefaultPropertyIndex( name );
438 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
440 // ProxyObject guarantees the property is writable and index is in range
442 if(index < RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT)
444 RenderableActor::SetDefaultProperty(index, propertyValue);
450 case Dali::ImageActor::PIXEL_AREA:
452 SetPixelArea(propertyValue.Get<Rect<int> >());
455 case Dali::ImageActor::FADE_IN:
457 SetFadeIn(propertyValue.Get<bool>());
460 case Dali::ImageActor::FADE_IN_DURATION:
462 SetFadeInDuration(propertyValue.Get<float>());
465 case Dali::ImageActor::STYLE:
467 SetStyle(StyleEnum(propertyValue.Get<std::string>()));
470 case Dali::ImageActor::BORDER:
472 SetNinePatchBorder(propertyValue.Get<Vector4>());
475 case Dali::ImageActor::IMAGE:
477 Dali::Image img = Scripting::NewImage( propertyValue );
480 SetImage( &GetImplementation(img) );
484 DALI_LOG_WARNING("Cannot create image from property value\n");
490 DALI_LOG_WARNING("Unknown property (%d)\n", index);
498 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
501 if(index < RENDERABLE_ACTOR_DEFAULT_PROPERTY_MAX_COUNT)
503 ret = RenderableActor::GetDefaultProperty(index);
509 case Dali::ImageActor::PIXEL_AREA:
511 Rect<int> r = GetPixelArea();
515 case Dali::ImageActor::FADE_IN:
520 case Dali::ImageActor::FADE_IN_DURATION:
522 ret = GetFadeInDuration();
525 case Dali::ImageActor::STYLE:
527 ret = StyleString(GetStyle());
530 case Dali::ImageActor::BORDER:
532 ret = GetNinePatchBorder();
535 case Dali::ImageActor::IMAGE:
537 ret = Property::Value(Property::MAP);
542 DALI_LOG_WARNING("Unknown property (%d)\n", index);
552 } // namespace Internal