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 <cstring> // for strcmp
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/devel-api/scripting/scripting.h>
27 #include <dali/internal/event/common/property-helper.h>
28 #include <dali/internal/event/effects/shader-effect-impl.h>
29 #include <dali/internal/event/images/image-connector.h>
30 #include <dali/internal/event/images/nine-patch-image-impl.h>
43 // Name Type writable animatable constraint-input enum for index-checking
44 DALI_PROPERTY_TABLE_BEGIN
45 DALI_PROPERTY( "pixel-area", RECTANGLE, true, false, true, Dali::ImageActor::Property::PIXEL_AREA )
46 DALI_PROPERTY( "style", STRING, true, false, true, Dali::ImageActor::Property::STYLE )
47 DALI_PROPERTY( "border", VECTOR4, true, false, true, Dali::ImageActor::Property::BORDER )
48 DALI_PROPERTY( "image", MAP, true, false, false, Dali::ImageActor::Property::IMAGE )
49 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX )
53 return Dali::ImageActor::New();
56 TypeRegistration mType( typeid( Dali::ImageActor ), typeid( Dali::Actor ), Create );
58 ImageActor::Style StyleEnum(const std::string &s)
60 if(s == "STYLE_NINE_PATCH")
62 return Dali::ImageActor::STYLE_NINE_PATCH;
64 else if(s == "STYLE_NINE_PATCH_NO_CENTER")
66 return Dali::ImageActor::STYLE_NINE_PATCH_NO_CENTER;
68 else // if(s == "QUAD")
70 return Dali::ImageActor::STYLE_QUAD;
74 std::string StyleString(const ImageActor::Style style)
76 if(style == Dali::ImageActor::STYLE_NINE_PATCH)
78 return "STYLE_NINE_PATCH";
80 else if(style == Dali::ImageActor::STYLE_NINE_PATCH_NO_CENTER)
82 return "STYLE_NINE_PATCH_NO_CENTER";
84 else // if(s == "QUAD")
91 ImageActorPtr ImageActor::New()
93 ImageActorPtr actor( new ImageActor );
95 // Second-phase construction of base class
98 // Create the attachment
99 actor->mImageAttachment = ImageAttachment::New( actor->GetEventThreadServices(), *actor->mNode );
100 actor->Attach( *actor->mImageAttachment );
105 void ImageActor::OnInitialize()
107 // TODO: Remove this, at the moment its needed for size negotiation to work
108 SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
111 void ImageActor::SetImage( ImagePtr& image )
113 ImagePtr currentImage = mImageAttachment->GetImage();
114 // early exit if it's the same image as we already have
115 if ( currentImage == image )
120 // NOTE! image might be pointing to NULL, which is fine as in that case app wants to just remove the image
121 ImagePtr newImage( image );
122 // if image is not NULL, check for 9 patch
125 // Automatically convert nine-patch images to cropped bitmap
126 NinePatchImage* ninePatchImage = NinePatchImage::DownCast( image.Get() );
129 newImage = ninePatchImage->CreateCroppedBufferImage();
130 SetStyle( Dali::ImageActor::STYLE_NINE_PATCH );
131 SetNinePatchBorder( ninePatchImage->GetStretchBorders(), true );
134 // set the actual image (normal or 9 patch) and natural size based on that
135 mImageAttachment->SetImage( newImage );
140 ImagePtr ImageActor::GetImage()
142 return mImageAttachment->GetImage();
145 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
147 mImageAttachment->SetPixelArea( pixelArea );
152 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
154 return mImageAttachment->GetPixelArea();
157 bool ImageActor::IsPixelAreaSet() const
159 return mImageAttachment->IsPixelAreaSet();
162 void ImageActor::ClearPixelArea()
164 mImageAttachment->ClearPixelArea();
169 void ImageActor::SetStyle( Style style )
171 mImageAttachment->SetStyle( style );
174 ImageActor::Style ImageActor::GetStyle() const
176 return mImageAttachment->GetStyle();
179 void ImageActor::SetNinePatchBorder( const Vector4& border, bool inPixels )
181 mImageAttachment->SetNinePatchBorder( border, inPixels );
184 Vector4 ImageActor::GetNinePatchBorder() const
186 return mImageAttachment->GetNinePatchBorder();
189 ImageAttachment& ImageActor::GetImageAttachment()
191 return *mImageAttachment;
194 RenderableAttachment& ImageActor::GetRenderableAttachment() const
196 DALI_ASSERT_DEBUG( mImageAttachment && "ImageAttachment missing from ImageActor" );
197 return *mImageAttachment;
200 ImageActor::ImageActor()
201 : Actor( Actor::RENDERABLE )
205 ImageActor::~ImageActor()
209 Vector3 ImageActor::GetNaturalSize() const
211 Vector2 naturalSize( CalculateNaturalSize() );
212 return Vector3( naturalSize.width, naturalSize.height, 0.f );
215 Vector2 ImageActor::CalculateNaturalSize() const
217 // if no image then natural size is 0
218 Vector2 size( 0.0f, 0.0f );
220 ImagePtr image = mImageAttachment->GetImage();
223 if( IsPixelAreaSet() )
225 PixelArea area(GetPixelArea());
226 size.width = area.width;
227 size.height = area.height;
231 size = image->GetNaturalSize();
238 void ImageActor::OnStageConnectionInternal()
242 void ImageActor::OnStageDisconnectionInternal()
246 unsigned int ImageActor::GetDefaultPropertyCount() const
248 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
251 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
253 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
255 indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
257 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
258 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
260 indices.PushBack( index );
264 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
266 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
268 return Actor::IsDefaultPropertyWritable(index);
271 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
272 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
274 return DEFAULT_PROPERTY_DETAILS[ index ].writable;
280 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
282 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
284 return Actor::IsDefaultPropertyAnimatable( index );
287 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
288 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
290 return DEFAULT_PROPERTY_DETAILS[ index ].animatable;
296 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
298 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
300 return Actor::IsDefaultPropertyAConstraintInput( index );
303 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
304 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
306 return DEFAULT_PROPERTY_DETAILS[ index ].constraintInput;
312 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
314 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
316 return Actor::GetDefaultPropertyType( index );
319 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
320 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
322 return DEFAULT_PROPERTY_DETAILS[index].type;
325 // index out-of-bounds
326 return Property::NONE;
329 const char* ImageActor::GetDefaultPropertyName( Property::Index index ) const
331 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
333 return Actor::GetDefaultPropertyName(index);
336 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
337 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
339 return DEFAULT_PROPERTY_DETAILS[index].name;
342 // index out-of-bounds
346 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
348 Property::Index index = Property::INVALID_INDEX;
350 // Look for name in default properties
351 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
353 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
354 if( 0 == strcmp( name.c_str(), property->name ) ) // Don't want to convert rhs to string
356 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
361 // If not found, check in base class
362 if( Property::INVALID_INDEX == index )
364 index = Actor::GetDefaultPropertyIndex( name );
369 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
371 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
373 Actor::SetDefaultProperty( index, propertyValue );
379 case Dali::ImageActor::Property::PIXEL_AREA:
381 SetPixelArea(propertyValue.Get<Rect<int> >());
384 case Dali::ImageActor::Property::STYLE:
386 SetStyle( StyleEnum( propertyValue.Get<std::string>() ) );
389 case Dali::ImageActor::Property::BORDER:
391 SetNinePatchBorder( propertyValue.Get<Vector4>(), true /*in pixels*/ );
394 case Dali::ImageActor::Property::IMAGE:
396 Dali::Image img = Scripting::NewImage( propertyValue );
399 ImagePtr image( &GetImplementation(img) );
404 DALI_LOG_WARNING("Cannot create image from property value\n");
410 DALI_LOG_WARNING("Unknown property (%d)\n", index);
418 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
421 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
423 ret = Actor::GetDefaultProperty( index );
429 case Dali::ImageActor::Property::PIXEL_AREA:
431 Rect<int> r = GetPixelArea();
435 case Dali::ImageActor::Property::STYLE:
437 ret = StyleString( GetStyle() );
440 case Dali::ImageActor::Property::BORDER:
442 ret = GetNinePatchBorder();
445 case Dali::ImageActor::Property::IMAGE:
448 Scripting::CreatePropertyMap( Dali::Image( mImageAttachment->GetImage().Get() ), map );
449 ret = Property::Value( map );
454 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
464 void ImageActor::SetSortModifier(float modifier)
466 mImageAttachment->SetSortModifier( modifier );
469 float ImageActor::GetSortModifier() const
471 return mImageAttachment->GetSortModifier();
474 void ImageActor::SetDepthIndex( int depthIndex )
476 mImageAttachment->SetSortModifier( depthIndex );
479 int ImageActor::GetDepthIndex() const
481 return static_cast< int >( mImageAttachment->GetSortModifier() );
484 void ImageActor::SetCullFace(CullFaceMode mode)
486 mImageAttachment->SetCullFace( mode );
489 CullFaceMode ImageActor::GetCullFace() const
491 return mImageAttachment->GetCullFace();
494 void ImageActor::SetBlendMode( BlendingMode::Type mode )
496 mImageAttachment->SetBlendMode( mode );
499 BlendingMode::Type ImageActor::GetBlendMode() const
501 return mImageAttachment->GetBlendMode();
504 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgba, BlendingFactor::Type destFactorRgba )
506 mImageAttachment->SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
509 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgb, BlendingFactor::Type destFactorRgb,
510 BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
512 mImageAttachment->SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
515 void ImageActor::GetBlendFunc( BlendingFactor::Type& srcFactorRgb, BlendingFactor::Type& destFactorRgb,
516 BlendingFactor::Type& srcFactorAlpha, BlendingFactor::Type& destFactorAlpha ) const
518 mImageAttachment->GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
521 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgba )
523 mImageAttachment->SetBlendEquation( equationRgba, equationRgba );
526 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
528 mImageAttachment->SetBlendEquation( equationRgb, equationAlpha );
531 void ImageActor::GetBlendEquation( BlendingEquation::Type& equationRgb, BlendingEquation::Type& equationAlpha ) const
533 mImageAttachment->GetBlendEquation( equationRgb, equationAlpha );
536 void ImageActor::SetBlendColor( const Vector4& color )
538 mImageAttachment->SetBlendColor( color );
541 const Vector4& ImageActor::GetBlendColor() const
543 return mImageAttachment->GetBlendColor();
546 void ImageActor::SetFilterMode( FilterMode::Type minFilter, FilterMode::Type magFilter )
548 mImageAttachment->SetFilterMode( minFilter, magFilter );
551 void ImageActor::GetFilterMode( FilterMode::Type& minFilter, FilterMode::Type& magFilter ) const
553 return mImageAttachment->GetFilterMode( minFilter, magFilter );
556 void ImageActor::SetShaderEffect(ShaderEffect& effect)
558 mImageAttachment->SetShaderEffect( effect );
561 ShaderEffectPtr ImageActor::GetShaderEffect() const
563 return mImageAttachment->GetShaderEffect();
566 void ImageActor::RemoveShaderEffect()
568 return mImageAttachment->RemoveShaderEffect();
572 } // namespace Internal