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/public-api/object/type-registry.h>
23 #include <dali/public-api/scripting/scripting.h>
24 #include <dali/internal/event/common/property-helper.h>
25 #include <dali/internal/event/effects/shader-effect-impl.h>
26 #include <dali/internal/event/images/image-connector.h>
27 #include <dali/internal/event/images/nine-patch-image-impl.h>
40 // Name Type writable animatable constraint-input enum for index-checking
41 DALI_PROPERTY_TABLE_BEGIN
42 DALI_PROPERTY( "pixel-area", RECTANGLE, true, false, true, Dali::ImageActor::Property::PixelArea )
43 DALI_PROPERTY( "style", STRING, true, false, true, Dali::ImageActor::Property::Style )
44 DALI_PROPERTY( "border", VECTOR4, true, false, true, Dali::ImageActor::Property::Border )
45 DALI_PROPERTY( "image", MAP, true, false, false, Dali::ImageActor::Property::Image )
46 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX )
50 return Dali::ImageActor::New();
53 TypeRegistration mType( typeid( Dali::ImageActor ), typeid( Dali::RenderableActor ), Create );
55 ImageActor::Style StyleEnum(const std::string &s)
57 if(s == "STYLE_NINE_PATCH")
59 return Dali::ImageActor::STYLE_NINE_PATCH;
61 else if(s == "STYLE_NINE_PATCH_NO_CENTER")
63 return Dali::ImageActor::STYLE_NINE_PATCH_NO_CENTER;
65 else // if(s == "QUAD")
67 return Dali::ImageActor::STYLE_QUAD;
71 std::string StyleString(const ImageActor::Style style)
73 if(style == Dali::ImageActor::STYLE_NINE_PATCH)
75 return "STYLE_NINE_PATCH";
77 else if(style == Dali::ImageActor::STYLE_NINE_PATCH_NO_CENTER)
79 return "STYLE_NINE_PATCH_NO_CENTER";
81 else // if(s == "QUAD")
88 ImageActorPtr ImageActor::New()
90 ImageActorPtr actor( new ImageActor );
92 // Second-phase construction of base class
95 // Create the attachment
96 actor->mImageAttachment = ImageAttachment::New( *actor->mStage, *actor->mNode );
97 actor->Attach( *actor->mImageAttachment );
102 void ImageActor::OnInitialize()
106 void ImageActor::SetImage( ImagePtr& image )
108 ImagePtr currentImage = mImageAttachment->GetImage();
109 // early exit if it's the same image as we already have
110 if ( currentImage == image )
115 // NOTE! image might be pointing to NULL, which is fine as in that case app wants to just remove the image
116 ImagePtr newImage( image );
117 // if image is not NULL, check for 9 patch
120 // Automatically convert nine-patch images to cropped bitmap
121 NinePatchImage* ninePatchImage = NinePatchImage::DownCast( image.Get() );
124 newImage = ninePatchImage->CreateCroppedBufferImage();
125 SetStyle( Dali::ImageActor::STYLE_NINE_PATCH );
126 SetNinePatchBorder( ninePatchImage->GetStretchBorders(), true );
129 // set the actual image (normal or 9 patch) and natural size based on that
130 mImageAttachment->SetImage( newImage );
134 ImagePtr ImageActor::GetImage()
136 return mImageAttachment->GetImage();
139 void ImageActor::SetToNaturalSize()
141 mUsingNaturalSize = true;
146 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
148 mImageAttachment->SetPixelArea( pixelArea );
153 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
155 return mImageAttachment->GetPixelArea();
158 bool ImageActor::IsPixelAreaSet() const
160 return mImageAttachment->IsPixelAreaSet();
163 void ImageActor::ClearPixelArea()
165 mImageAttachment->ClearPixelArea();
167 if( mUsingNaturalSize )
169 ImagePtr image = mImageAttachment->GetImage();
172 mInternalSetSize = true;
173 SetSize( image->GetNaturalSize() );
174 mInternalSetSize = false;
179 void ImageActor::SetStyle( Style style )
181 mImageAttachment->SetStyle( style );
184 ImageActor::Style ImageActor::GetStyle() const
186 return mImageAttachment->GetStyle();
189 void ImageActor::SetNinePatchBorder( const Vector4& border, bool inPixels )
191 mImageAttachment->SetNinePatchBorder( border, inPixels );
194 Vector4 ImageActor::GetNinePatchBorder() const
196 return mImageAttachment->GetNinePatchBorder();
199 ImageAttachment& ImageActor::GetImageAttachment()
201 return *mImageAttachment;
204 RenderableAttachment& ImageActor::GetRenderableAttachment() const
206 DALI_ASSERT_DEBUG( mImageAttachment && "ImageAttachment missing from ImageActor" );
207 return *mImageAttachment;
210 ImageActor::ImageActor()
212 mUsingNaturalSize(true),
213 mInternalSetSize(false)
217 ImageActor::~ImageActor()
221 void ImageActor::SetNaturalSize()
223 if( mUsingNaturalSize )
225 mInternalSetSize = true;
226 SetSize( CalculateNaturalSize() );
227 mInternalSetSize = false;
231 Vector3 ImageActor::GetNaturalSize() const
233 Vector2 naturalSize( CalculateNaturalSize() );
234 return Vector3( naturalSize.width, naturalSize.height, CalculateSizeZ( naturalSize ) );
237 Vector2 ImageActor::CalculateNaturalSize() const
239 // if no image then natural size is 0
240 Vector2 size( 0.0f, 0.0f );
242 ImagePtr image = mImageAttachment->GetImage();
245 if( IsPixelAreaSet() )
247 PixelArea area(GetPixelArea());
248 size.width = area.width;
249 size.height = area.height;
253 size = image->GetNaturalSize();
260 void ImageActor::OnSizeSet( const Vector3& targetSize )
262 if( !mInternalSetSize )
264 mUsingNaturalSize = false;
268 void ImageActor::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
270 mUsingNaturalSize = false;
273 void ImageActor::OnStageConnectionInternal()
277 void ImageActor::OnStageDisconnectionInternal()
281 unsigned int ImageActor::GetDefaultPropertyCount() const
283 return RenderableActor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
286 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
288 RenderableActor::GetDefaultPropertyIndices( indices ); // RenderableActor class properties
290 indices.reserve( indices.size() + DEFAULT_PROPERTY_COUNT );
292 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
293 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
295 indices.push_back( index );
299 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
301 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
303 return RenderableActor::IsDefaultPropertyWritable(index);
306 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
307 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
309 return DEFAULT_PROPERTY_DETAILS[ index ].writable;
315 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
317 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
319 return RenderableActor::IsDefaultPropertyAnimatable( index );
322 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
323 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
325 return DEFAULT_PROPERTY_DETAILS[ index ].animatable;
331 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
333 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
335 return RenderableActor::IsDefaultPropertyAConstraintInput( index );
338 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
339 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
341 return DEFAULT_PROPERTY_DETAILS[ index ].constraintInput;
347 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
349 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
351 return RenderableActor::GetDefaultPropertyType( index );
354 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
355 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
357 return DEFAULT_PROPERTY_DETAILS[index].type;
360 // index out-of-bounds
361 return Property::NONE;
364 const char* ImageActor::GetDefaultPropertyName( Property::Index index ) const
366 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
368 return RenderableActor::GetDefaultPropertyName(index);
371 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
372 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
374 return DEFAULT_PROPERTY_DETAILS[index].name;
377 // index out-of-bounds
381 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
383 Property::Index index = Property::INVALID_INDEX;
385 // Look for name in default properties
386 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
388 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
389 if( 0 == strcmp( name.c_str(), property->name ) ) // Don't want to convert rhs to string
391 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
396 // If not found, check in base class
397 if( Property::INVALID_INDEX == index )
399 index = RenderableActor::GetDefaultPropertyIndex( name );
404 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
406 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
408 RenderableActor::SetDefaultProperty( index, propertyValue );
414 case Dali::ImageActor::Property::PixelArea:
416 SetPixelArea(propertyValue.Get<Rect<int> >());
419 case Dali::ImageActor::Property::Style:
421 SetStyle( StyleEnum( propertyValue.Get<std::string>() ) );
424 case Dali::ImageActor::Property::Border:
426 SetNinePatchBorder( propertyValue.Get<Vector4>(), true /*in pixels*/ );
429 case Dali::ImageActor::Property::Image:
431 Dali::Image img = Scripting::NewImage( propertyValue );
434 ImagePtr image( &GetImplementation(img) );
439 DALI_LOG_WARNING("Cannot create image from property value\n");
445 DALI_LOG_WARNING("Unknown property (%d)\n", index);
453 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
456 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
458 ret = RenderableActor::GetDefaultProperty( index );
464 case Dali::ImageActor::Property::PixelArea:
466 Rect<int> r = GetPixelArea();
470 case Dali::ImageActor::Property::Style:
472 ret = StyleString( GetStyle() );
475 case Dali::ImageActor::Property::Border:
477 ret = GetNinePatchBorder();
480 case Dali::ImageActor::Property::Image:
483 Scripting::CreatePropertyMap( Dali::Image( mImageAttachment->GetImage().Get() ), map );
484 ret = Property::Value( map );
489 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
499 void ImageActor::SetSortModifier(float modifier)
501 mImageAttachment->SetSortModifier(modifier);
504 float ImageActor::GetSortModifier() const
506 return mImageAttachment->GetSortModifier();
509 void ImageActor::SetCullFace(CullFaceMode mode)
511 mImageAttachment->SetCullFace( mode );
514 CullFaceMode ImageActor::GetCullFace() const
516 return mImageAttachment->GetCullFace();
519 void ImageActor::SetBlendMode( BlendingMode::Type mode )
521 mImageAttachment->SetBlendMode( mode );
524 BlendingMode::Type ImageActor::GetBlendMode() const
526 return mImageAttachment->GetBlendMode();
529 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgba, BlendingFactor::Type destFactorRgba )
531 mImageAttachment->SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
534 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgb, BlendingFactor::Type destFactorRgb,
535 BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
537 mImageAttachment->SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
540 void ImageActor::GetBlendFunc( BlendingFactor::Type& srcFactorRgb, BlendingFactor::Type& destFactorRgb,
541 BlendingFactor::Type& srcFactorAlpha, BlendingFactor::Type& destFactorAlpha ) const
543 mImageAttachment->GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
546 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgba )
548 mImageAttachment->SetBlendEquation( equationRgba, equationRgba );
551 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
553 mImageAttachment->SetBlendEquation( equationRgb, equationAlpha );
556 void ImageActor::GetBlendEquation( BlendingEquation::Type& equationRgb, BlendingEquation::Type& equationAlpha ) const
558 mImageAttachment->GetBlendEquation( equationRgb, equationAlpha );
561 void ImageActor::SetBlendColor( const Vector4& color )
563 mImageAttachment->SetBlendColor( color );
566 const Vector4& ImageActor::GetBlendColor() const
568 return mImageAttachment->GetBlendColor();
571 void ImageActor::SetFilterMode( FilterMode::Type minFilter, FilterMode::Type magFilter )
573 mImageAttachment->SetFilterMode( minFilter, magFilter );
576 void ImageActor::GetFilterMode( FilterMode::Type& minFilter, FilterMode::Type& magFilter ) const
578 return mImageAttachment->GetFilterMode( minFilter, magFilter );
581 void ImageActor::SetShaderEffect(ShaderEffect& effect)
583 mImageAttachment->SetShaderEffect( effect );
586 ShaderEffectPtr ImageActor::GetShaderEffect() const
588 return mImageAttachment->GetShaderEffect();
591 void ImageActor::RemoveShaderEffect()
593 return mImageAttachment->RemoveShaderEffect();
597 } // namespace Internal