Merge "Remove unsafe new-delete pair from program and change unoptimal resizes to...
[platform/core/uifw/dali-core.git] / dali / internal / event / actors / image-actor-impl.cpp
index ebdce3e..e1450d7 100644 (file)
 #include <dali/internal/event/actors/image-actor-impl.h>
 
 // INTERNAL INCLUDES
-#include <dali/internal/event/images/nine-patch-image-impl.h>
 #include <dali/public-api/object/type-registry.h>
-#include <dali/internal/event/common/property-index-ranges.h>
-#include <dali/internal/event/images/image-connector.h>
 #include <dali/public-api/scripting/scripting.h>
+#include <dali/internal/event/common/property-helper.h>
+#include <dali/internal/event/images/image-connector.h>
+#include <dali/internal/event/images/nine-patch-image-impl.h>
 
 namespace Dali
 {
 
-const Property::Index ImageActor::PIXEL_AREA           = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
-const Property::Index ImageActor::STYLE                = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 1;
-const Property::Index ImageActor::BORDER               = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 2;
-const Property::Index ImageActor::IMAGE                = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 3;
-
 namespace Internal
 {
-bool ImageActor::mFirstInstance = true;
-Actor::DefaultPropertyLookup* ImageActor::mDefaultImageActorPropertyLookup = NULL;
 
 namespace
 {
 
+// Properties
+
+//              Name           Type   writable animatable constraint-input  enum for index-checking
+DALI_PROPERTY_TABLE_BEGIN
+DALI_PROPERTY( "pixel-area",   RECTANGLE, true,    false,   true,    Dali::ImageActor::Property::PIXEL_AREA )
+DALI_PROPERTY( "style",        STRING,    true,    false,   true,    Dali::ImageActor::Property::STYLE      )
+DALI_PROPERTY( "border",       VECTOR4,   true,    false,   true,    Dali::ImageActor::Property::BORDER     )
+DALI_PROPERTY( "image",        MAP,       true,    false,   false,   Dali::ImageActor::Property::IMAGE      )
+DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX )
+
 BaseHandle Create()
 {
   return Dali::ImageActor::New();
 }
 
-TypeRegistration mType( typeid(Dali::ImageActor), typeid(Dali::RenderableActor), Create );
-
-const std::string DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[] =
-{
-  "pixel-area",
-  "style",
-  "border",
-  "image"
-};
-const int DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT = sizeof( DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES ) / sizeof( std::string );
-
-const Property::Type DEFAULT_IMAGE_ACTOR_PROPERTY_TYPES[DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT] =
-{
-  Property::RECTANGLE,  // "pixel-area",
-  Property::STRING,     // "style",
-  Property::VECTOR4,    // "border",
-  Property::MAP,        // "image",
-};
+TypeRegistration mType( typeid( Dali::ImageActor ), typeid( Dali::RenderableActor ), Create );
 
 ImageActor::Style StyleEnum(const std::string &s)
 {
@@ -106,7 +92,7 @@ ImageActorPtr ImageActor::New()
   actor->Initialize();
 
   // Create the attachment
-  actor->mImageAttachment = ImageAttachment::New( *actor->mNode );
+  actor->mImageAttachment = ImageAttachment::New( *actor->mStage, *actor->mNode );
   actor->Attach( *actor->mImageAttachment );
 
   return actor;
@@ -114,16 +100,6 @@ ImageActorPtr ImageActor::New()
 
 void ImageActor::OnInitialize()
 {
-  if(ImageActor::mFirstInstance)
-  {
-    mDefaultImageActorPropertyLookup = new DefaultPropertyLookup();
-    const int start = DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
-    for ( int i = 0; i < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT; ++i )
-    {
-      (*mDefaultImageActorPropertyLookup)[DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[i]] = i + start;
-    }
-    ImageActor::mFirstInstance = false;
-  }
 }
 
 void ImageActor::SetImage( ImagePtr& image )
@@ -144,7 +120,7 @@ void ImageActor::SetImage( ImagePtr& image )
     NinePatchImage* ninePatchImage = NinePatchImage::DownCast( image.Get() );
     if( ninePatchImage )
     {
-      newImage = ninePatchImage->CreateCroppedBitmapImage();
+      newImage = ninePatchImage->CreateCroppedBufferImage();
       SetStyle( Dali::ImageActor::STYLE_NINE_PATCH );
       SetNinePatchBorder( ninePatchImage->GetStretchBorders(), true );
     }
@@ -303,17 +279,17 @@ void ImageActor::OnStageDisconnectionInternal()
 
 unsigned int ImageActor::GetDefaultPropertyCount() const
 {
-  return RenderableActor::GetDefaultPropertyCount() + DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT;
+  return RenderableActor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
 }
 
 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
 {
   RenderableActor::GetDefaultPropertyIndices( indices ); // RenderableActor class properties
 
-  indices.reserve( indices.size() + DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT );
+  indices.reserve( indices.size() + DEFAULT_PROPERTY_COUNT );
 
-  int index = DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
-  for ( int i = 0; i < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT; ++i, ++index )
+  int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
+  for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
   {
     indices.push_back( index );
   }
@@ -321,129 +297,135 @@ void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices )
 
 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
 {
-  if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
+  if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
   {
     return RenderableActor::IsDefaultPropertyWritable(index);
   }
-  else
+
+  index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
+  if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
   {
-    return true;
+    return DEFAULT_PROPERTY_DETAILS[ index ].writable;
   }
+
+  return false;
 }
 
 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
 {
-  if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
+  if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
   {
-    return RenderableActor::IsDefaultPropertyAnimatable(index);
+    return RenderableActor::IsDefaultPropertyAnimatable( index );
   }
-  else
+
+  index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
+  if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
   {
-    return false;
+    return DEFAULT_PROPERTY_DETAILS[ index ].animatable;
   }
+
+  return false;
 }
 
 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
 {
-  if( index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT )
+  if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
+  {
+    return RenderableActor::IsDefaultPropertyAConstraintInput( index );
+  }
+
+  index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
+  if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
   {
-    return RenderableActor::IsDefaultPropertyAConstraintInput(index);
+    return DEFAULT_PROPERTY_DETAILS[ index ].constraintInput;
   }
-  return true; // Our properties can be used as input to constraints.
+
+  return false;
 }
 
 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
 {
-  if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
+  if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
   {
-    return RenderableActor::GetDefaultPropertyType(index);
+    return RenderableActor::GetDefaultPropertyType( index );
   }
-  else
-  {
-    index -= DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
 
-    if ( ( index >= 0 ) && ( index < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT ) )
-    {
-      return DEFAULT_IMAGE_ACTOR_PROPERTY_TYPES[index];
-    }
-    else
-    {
-      // index out-of-bounds
-      return Property::NONE;
-    }
+  index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
+  if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
+  {
+    return DEFAULT_PROPERTY_DETAILS[index].type;
   }
+
+  // index out-of-bounds
+  return Property::NONE;
 }
 
-const std::string& ImageActor::GetDefaultPropertyName( Property::Index index ) const
+const char* ImageActor::GetDefaultPropertyName( Property::Index index ) const
 {
-  if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
+  if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
   {
     return RenderableActor::GetDefaultPropertyName(index);
   }
-  else
-  {
-    index -= DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
 
-    if ( ( index >= 0 ) && ( index < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT ) )
-    {
-      return DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[index];
-    }
-    else
-    {
-      // index out-of-bounds
-      static const std::string INVALID_PROPERTY_NAME;
-      return INVALID_PROPERTY_NAME;
-    }
+  index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
+  if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
+  {
+    return DEFAULT_PROPERTY_DETAILS[index].name;
   }
+
+  // index out-of-bounds
+  return NULL;
 }
 
 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
 {
   Property::Index index = Property::INVALID_INDEX;
 
-  DALI_ASSERT_DEBUG( NULL != mDefaultImageActorPropertyLookup );
-
-  // Look for name in current class' default properties
-  DefaultPropertyLookup::const_iterator result = mDefaultImageActorPropertyLookup->find( name );
-  if ( mDefaultImageActorPropertyLookup->end() != result )
+  // Look for name in default properties
+  for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
   {
-    index = result->second;
+    const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
+    if( 0 == strcmp( name.c_str(), property->name ) ) // Don't want to convert rhs to string
+    {
+      index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
+      break;
+    }
   }
-  else
+
+  // If not found, check in base class
+  if( Property::INVALID_INDEX == index )
   {
-    // If not found, check in base class
     index = RenderableActor::GetDefaultPropertyIndex( name );
   }
-
   return index;
 }
 
 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
 {
-  if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
+  if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
   {
-    RenderableActor::SetDefaultProperty(index, propertyValue);
+    RenderableActor::SetDefaultProperty( index, propertyValue );
   }
   else
   {
     switch(index)
     {
-      case Dali::ImageActor::PIXEL_AREA:
+      case Dali::ImageActor::Property::PIXEL_AREA:
       {
         SetPixelArea(propertyValue.Get<Rect<int> >());
         break;
       }
-      case Dali::ImageActor::STYLE:
+      case Dali::ImageActor::Property::STYLE:
       {
-        SetStyle(StyleEnum(propertyValue.Get<std::string>()));
+        SetStyle( StyleEnum( propertyValue.Get<std::string>() ) );
         break;
       }
-      case Dali::ImageActor::BORDER:
+      case Dali::ImageActor::Property::BORDER:
       {
         SetNinePatchBorder( propertyValue.Get<Vector4>(), true /*in pixels*/ );
         break;
       }
-      case Dali::ImageActor::IMAGE:
+      case Dali::ImageActor::Property::IMAGE:
       {
         Dali::Image img = Scripting::NewImage( propertyValue );
         if(img)
@@ -470,31 +452,31 @@ void ImageActor::SetDefaultProperty( Property::Index index, const Property::Valu
 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
 {
   Property::Value ret;
-  if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
+  if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
   {
-    ret = RenderableActor::GetDefaultProperty(index);
+    ret = RenderableActor::GetDefaultProperty( index );
   }
   else
   {
-    switch(index)
+    switch( index )
     {
-      case Dali::ImageActor::PIXEL_AREA:
+      case Dali::ImageActor::Property::PIXEL_AREA:
       {
         Rect<int> r = GetPixelArea();
         ret = r;
         break;
       }
-      case Dali::ImageActor::STYLE:
+      case Dali::ImageActor::Property::STYLE:
       {
-        ret = StyleString(GetStyle());
+        ret = StyleString( GetStyle() );
         break;
       }
-      case Dali::ImageActor::BORDER:
+      case Dali::ImageActor::Property::BORDER:
       {
         ret = GetNinePatchBorder();
         break;
       }
-      case Dali::ImageActor::IMAGE:
+      case Dali::ImageActor::Property::IMAGE:
       {
         Property::Map map;
         Scripting::CreatePropertyMap( Dali::Image( mImageAttachment->GetImage().Get() ), map );
@@ -503,7 +485,7 @@ Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
       }
       default:
       {
-        DALI_LOG_WARNING("Unknown property (%d)\n", index);
+        DALI_LOG_WARNING( "Unknown property (%d)\n", index );
         break;
       }
     } // switch(index)