Allow ImageView padding to be set to Zero
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / image-view / image-view-impl.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 37b2dd5..edff0e3
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -64,7 +64,9 @@ DALI_TYPE_REGISTRATION_END()
 using namespace Dali;
 
 ImageView::ImageView()
-: Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) )
+: Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
+  mImageSize(),
+  mImageVisualPaddingSetByTransform( false )
 {
 }
 
@@ -102,12 +104,31 @@ void ImageView::SetImage( Image image )
   mPropertyMap.Clear();
 
   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( image );
-  if (!mVisual)
+  if( visual )
   {
-    mVisual = visual;
+    if( !mVisual )
+    {
+      mVisual = visual;
+    }
+
+    if( !mShaderMap.Empty() )
+    {
+      Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
+      visualImpl.SetCustomShader( mShaderMap );
+    }
+
+    DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
+  }
+  else
+  {
+    // Unregister the existing visual
+    DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
+
+    // Trigger a size negotiation request that may be needed when unregistering a visual.
+    RelayoutRequest();
   }
 
-  DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual  );
+  // Signal that a Relayout may be needed
 }
 
 void ImageView::SetImage( const Property::Map& map )
@@ -118,30 +139,69 @@ void ImageView::SetImage( const Property::Map& map )
   mImage.Reset();
 
   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
-  // Don't set mVisual until it is ready and shown. Getters will still use current visual.
-  if (!mVisual)
+  if( visual )
   {
-    mVisual = visual;
+    // Don't set mVisual until it is ready and shown. Getters will still use current visual.
+    if( !mVisual )
+    {
+      mVisual = visual;
+    }
+
+    if( !mShaderMap.Empty() )
+    {
+      Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
+      visualImpl.SetCustomShader( mShaderMap );
+    }
+
+    DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual  );
+  }
+  else
+  {
+    // Unregister the exsiting visual
+    DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
+
+    // Trigger a size negotiation request that may be needed when unregistering a visual.
+    RelayoutRequest();
   }
 
-  DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual  );
+  // Signal that a Relayout may be needed
 }
 
 void ImageView::SetImage( const std::string& url, ImageDimensions size )
 {
   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
   mUrl = url;
+  mImageSize = size;
   mImage.Reset();
   mPropertyMap.Clear();
 
   // Don't set mVisual until it is ready and shown. Getters will still use current visual.
   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( url, size );
-  if (!mVisual)
+  if( visual )
   {
-    mVisual = visual;
+    if( !mVisual )
+    {
+      mVisual = visual;
+    }
+
+    if( !mShaderMap.Empty() )
+    {
+      Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
+      visualImpl.SetCustomShader( mShaderMap );
+    }
+
+    DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
+  }
+  else
+  {
+    // Unregister the exsiting visual
+    DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
+
+    // Trigger a size negotiation request that may be needed when unregistering a visual.
+    RelayoutRequest();
   }
 
-  DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
+  // Signal that a Relayout may be needed
 }
 
 Image ImageView::GetImage() const
@@ -229,24 +289,65 @@ void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
 
   if( mVisual )
   {
-    Extents margin;
-    margin = Self().GetProperty<Extents>( Toolkit::Control::Property::MARGIN );
-
-    Extents padding;
-    padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
-
     Property::Map transformMap = Property::Map();
 
-    if( ( padding.start != 0 ) || ( padding.end != 0 ) || ( padding.top != 0 ) || ( padding.bottom != 0 ) ||
-        ( margin.start != 0 ) || ( margin.end != 0 ) || ( margin.top != 0 ) || ( margin.bottom != 0 ) )
+    Extents padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
+    const Visual::FittingMode fittingMode = Toolkit::GetImplementation(mVisual).GetFittingMode();
+
+    bool zeroPadding = ( padding == Extents() );
+    if( ( !zeroPadding ) || // If padding is not zero
+        ( fittingMode == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO ) )
     {
-      transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2( margin.start + padding.start, margin.top + padding.top ) )
-                  .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
+      Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(
+            Self().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
+
+      if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
+      {
+        std::swap( padding.start, padding.end );
+      }
+
+      auto finalOffset = Vector2( padding.start, padding.top );
+      mImageVisualPaddingSetByTransform = true;
+
+      // remove padding from the size to know how much is left for the visual
+      auto finalSize = size - Vector2( padding.start + padding.end, padding.top + padding.bottom );
+
+      // Should provide a transform that handles aspect ratio according to image size
+      if( fittingMode == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO )
+      {
+        auto availableVisualSize = finalSize;
+
+        Vector2 naturalSize;
+        mVisual.GetNaturalSize( naturalSize );
+
+        // scale to fit the padded area
+        finalSize = naturalSize * std::min( ( naturalSize.width  ? ( availableVisualSize.width  / naturalSize.width  ) : 0 ),
+                                            ( naturalSize.height ? ( availableVisualSize.height / naturalSize.height ) : 0 ) );
+
+        // calculate final offset within the padded area
+        finalOffset += ( availableVisualSize - finalSize ) * .5f;
+      }
+
+      // populate the transform map
+      transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, finalOffset )
+                  .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY,
+                      Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
                   .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
-                  .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN );
+                  .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN )
+                  .Add( Toolkit::Visual::Transform::Property::SIZE, finalSize )
+                  .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY,
+                      Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) );
+    }
+    else if ( mImageVisualPaddingSetByTransform && zeroPadding )  // Reset offset to zero only if padding applied previously
+    {
+      mImageVisualPaddingSetByTransform = false;
+      // Reset the transform map
+      transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2::ZERO )
+                  .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY,
+                        Vector2( Toolkit::Visual::Transform::Policy::RELATIVE, Toolkit::Visual::Transform::Policy::RELATIVE ) );
     }
 
-    // Should provide a transform that handles aspect ratio according to image size
+
     mVisual.SetTransformAndSize( transformMap, size );
   }
 }
@@ -255,6 +356,7 @@ void ImageView::OnResourceReady( Toolkit::Control control )
 {
   // Visual ready so update visual attached to this ImageView, following call to RelayoutRequest will use this visual.
   mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
+  // Signal that a Relayout may be needed
 }
 
 ///////////////////////////////////////////////////////////
@@ -302,18 +404,24 @@ void ImageView::SetProperty( BaseObject* object, Property::Index index, const Pr
               impl.SetImage( *map );
             }
             // the property map contains only the custom shader
-            else if( ( impl.mVisual )&&( map->Count() == 1u )&&( shaderValue ) )
+            else if( ( map->Count() == 1u )&&( shaderValue ) )
             {
               Property::Map* shaderMap = shaderValue->GetMap();
               if( shaderMap )
               {
-                Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
-                visual.SetCustomShader( *shaderMap );
-                if( imageView.OnStage() )
+                impl.mShaderMap = *shaderMap;
+
+                if( !impl.mUrl.empty() )
+                {
+                  impl.SetImage( impl.mUrl, impl.mImageSize );
+                }
+                else if( impl.mImage )
                 {
-                  // force to create new core renderer to use the newly set shader
-                  visual.SetOffStage( imageView );
-                  visual.SetOnStage( imageView );
+                  impl.SetImage( impl.mImage );
+                }
+                else if( !impl.mPropertyMap.Empty() )
+                {
+                  impl.SetImage( impl.mPropertyMap );
                 }
               }
             }
@@ -367,9 +475,15 @@ Property::Value ImageView::GetProperty( BaseObject* object, Property::Index prop
           Scripting::CreatePropertyMap( impl.mImage, map );
           value = map;
         }
-        else if( !impl.mPropertyMap.Empty() )
+        else
         {
-          value = impl.mPropertyMap;
+          Property::Map map;
+          Toolkit::Visual::Base visual = DevelControl::GetVisual( impl, Toolkit::ImageView::Property::IMAGE );
+          if( visual )
+          {
+            visual.CreatePropertyMap( map );
+          }
+          value = map;
         }
         break;
       }