Changed some demo examples to use ImageView.
[platform/core/uifw/dali-demo.git] / examples / image-scaling-irregular-grid / image-scaling-irregular-grid-example.cpp
index 1d82b00..0caf323 100644 (file)
  * reduce the image to save memory, improve performance, and potentially display
  * a better small version of the image than if the default size were loaded.
  *
- * The functions CreateImage and CreateImageActor below show how to build an
+ * The functions CreateImage and CreateImageView below show how to build an
  * image using a scaling mode to have %Dali resize it during loading.
  *
- * This demo defaults to the ScaleToFill mode of ImageAttributes which makes
+ * This demo defaults to the SCALE_TO_FILL mode of ImageAttributes which makes
  * sure that every pixel in the loaded image is filled with a source colour
  * from the image's central region while losing the minimum number of pixels
  * from its periphery.
@@ -38,7 +38,7 @@
  * grid  using the button in the top-right of the toolbar.
  * A single image can be cycled by clicking the image directly.
  *
- * @see CreateImage CreateImageActor
+ * @see CreateImage CreateImageView
  */
 
 // EXTERNAL INCLUDES
@@ -65,6 +65,7 @@ const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-gradient.jpg" );
 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
 const char* APPLICATION_TITLE( "Image Scaling Modes" );
 const char* TOGGLE_SCALING_IMAGE( DALI_IMAGE_DIR "icon-change.png" );
+const char* TOGGLE_SCALING_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-change-selected.png" );
 
 /** The width of the grid in whole grid cells. */
 const unsigned GRID_WIDTH = 9;
@@ -77,7 +78,7 @@ const unsigned GRID_CELL_PADDING = 4;
 /** The aspect ratio of cells in the image grid. */
 const float CELL_ASPECT_RATIO = 1.33333333333333333333f;
 
-const ImageAttributes::ScalingMode DEFAULT_SCALING_MODE = ImageAttributes::ScaleToFill;
+const Dali::FittingMode::Type DEFAULT_SCALING_MODE = Dali::FittingMode::SCALE_TO_FILL;
 
 /** The number of times to spin an image on touching, each spin taking a second.*/
 const float SPIN_DURATION = 1.0f;
@@ -172,33 +173,30 @@ const unsigned NUM_IMAGE_PATHS = sizeof(IMAGE_PATHS) / sizeof(IMAGE_PATHS[0]) -
  * @param[in] filename The path of the image.
  * @param[in] width The width of the image in pixels.
  * @param[in] height The height of the image in pixels.
- * @param[in] scalingMode The mode to use when scaling the image to fit the desired dimensions.
+ * @param[in] fittingMode The mode to use when scaling the image to fit the desired dimensions.
  */
-Image CreateImage(const std::string& filename, unsigned int width, unsigned int height, ImageAttributes::ScalingMode scalingMode )
+Image CreateImage(const std::string& filename, unsigned int width, unsigned int height, Dali::FittingMode::Type fittingMode )
 {
 #ifdef DEBUG_PRINT_DIAGNOSTICS
-    fprintf( stderr, "CreateImage(%s, %u, %u, scalingMode=%u)\n", filename.c_str(), width, height, unsigned( scalingMode ) );
+    fprintf( stderr, "CreateImage(%s, %u, %u, fittingMode=%u)\n", filename.c_str(), width, height, unsigned( fittingMode ) );
 #endif
-  ImageAttributes attributes;
+  Image image = ResourceImage::New( filename, ImageDimensions( width, height ), fittingMode, Dali::SamplingMode::BOX_THEN_LINEAR );
 
-  attributes.SetSize( width, height );
-  attributes.SetScalingMode( scalingMode );
-  Image image = ResourceImage::New( filename, attributes );
   return image;
 }
 
 /**
- * Creates an ImageActor
+ * Creates an ImageView
  *
  * @param[in] filename The path of the image.
  * @param[in] width The width of the image in pixels.
  * @param[in] height The height of the image in pixels.
- * @param[in] scalingMode The mode to use when scaling the image to fit the desired dimensions.
+ * @param[in] fittingMode The mode to use when scaling the image to fit the desired dimensions.
  */
-ImageActor CreateImageActor(const std::string& filename, unsigned int width, unsigned int height, ImageAttributes::ScalingMode scalingMode )
+ImageView CreateImageView(const std::string& filename, unsigned int width, unsigned int height, Dali::FittingMode::Type fittingMode )
 {
-  Image img = CreateImage( filename, width, height, scalingMode );
-  ImageActor actor = ImageActor::New( img );
+  Image img = CreateImage( filename, width, height, fittingMode );
+  ImageView actor = ImageView::New( img );
   actor.SetName( filename );
   actor.SetParentOrigin(ParentOrigin::CENTER);
   actor.SetAnchorPoint(AnchorPoint::CENTER);
@@ -207,22 +205,22 @@ ImageActor CreateImageActor(const std::string& filename, unsigned int width, uns
 }
 
 /** Cycle the scaling mode options. */
-ImageAttributes::ScalingMode NextMode( const ImageAttributes::ScalingMode oldMode )
+Dali::FittingMode::Type NextMode( const Dali::FittingMode::Type oldMode )
 {
-  ImageAttributes::ScalingMode newMode = ImageAttributes::ShrinkToFit;
+  Dali::FittingMode::Type newMode = FittingMode::SHRINK_TO_FIT;
   switch ( oldMode )
   {
-    case ImageAttributes::ShrinkToFit:
-      newMode = ImageAttributes::ScaleToFill;
+    case FittingMode::SHRINK_TO_FIT:
+      newMode = FittingMode::SCALE_TO_FILL;
       break;
-    case ImageAttributes::ScaleToFill:
-      newMode = ImageAttributes::FitWidth;
+    case FittingMode::SCALE_TO_FILL:
+      newMode = FittingMode::FIT_WIDTH;
       break;
-    case ImageAttributes::FitWidth:
-      newMode = ImageAttributes::FitHeight;
+    case FittingMode::FIT_WIDTH:
+      newMode = FittingMode::FIT_HEIGHT;
       break;
-    case ImageAttributes::FitHeight:
-      newMode = ImageAttributes::ShrinkToFit;
+    case FittingMode::FIT_HEIGHT:
+      newMode = FittingMode::SHRINK_TO_FIT;
       break;
   }
   return newMode;
@@ -272,7 +270,7 @@ public:
   : mApplication( application ),
     mScrolling( false )
   {
-    std::cout << "ImageScalingScaleToFillController::ImageScalingScaleToFillController" << std::endl;
+    std::cout << "ImageScalingIrregularGridController::ImageScalingIrregularGridController" << std::endl;
 
     // Connect to the Application's Init signal
     mApplication.InitSignal().Connect( this, &ImageScalingIrregularGridController::Create );
@@ -288,7 +286,7 @@ public:
    */
   void Create( Application& application )
   {
-    std::cout << "ImageScalingScaleToFillController::Create" << std::endl;
+    std::cout << "ImageScalingIrregularGridController::Create" << std::endl;
 
     // Get a handle to the stage:
     Stage stage = Stage::GetCurrent();
@@ -309,8 +307,10 @@ public:
 
     // Create an image scaling toggle button. (right of toolbar)
     Image toggleScalingImage = ResourceImage::New( TOGGLE_SCALING_IMAGE );
+    Image toggleScalingImageSelected = ResourceImage::New( TOGGLE_SCALING_IMAGE_SELECTED );
     Toolkit::PushButton toggleScalingButton = Toolkit::PushButton::New();
-    toggleScalingButton.SetBackgroundImage( toggleScalingImage );
+    toggleScalingButton.SetButtonImage( toggleScalingImage );
+    toggleScalingButton.SetSelectedImage( toggleScalingImageSelected );
     toggleScalingButton.ClickedSignal().Connect( this, &ImageScalingIrregularGridController::OnToggleScalingTouched );
     mToolBar.AddControl( toggleScalingButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
 
@@ -323,26 +323,24 @@ public:
   /**
    * Build the main part of the application's view.
    */
-  void PopulateContentLayer( const ImageAttributes::ScalingMode scalingMode )
+  void PopulateContentLayer( const Dali::FittingMode::Type fittingMode )
   {
     Stage stage = Stage::GetCurrent();
     Vector2 stageSize = stage.GetSize();
 
     float fieldHeight;
-    Actor imageField = BuildImageField( stageSize.x, GRID_WIDTH, GRID_MAX_HEIGHT, scalingMode, fieldHeight );
+    Actor imageField = BuildImageField( stageSize.x, GRID_WIDTH, GRID_MAX_HEIGHT, fittingMode, fieldHeight );
 
     mScrollView = ScrollView::New();
 
     mScrollView.ScrollStartedSignal().Connect( this, &ImageScalingIrregularGridController::OnScrollStarted );
     mScrollView.ScrollCompletedSignal().Connect( this, &ImageScalingIrregularGridController::OnScrollCompleted );
 
-    mScrollView.EnableScrollComponent( Scrollable::VerticalScrollBar );
-    mScrollView.EnableScrollComponent( Scrollable::HorizontalScrollBar );
-
     mScrollView.SetAnchorPoint(AnchorPoint::CENTER);
     mScrollView.SetParentOrigin(ParentOrigin::CENTER);
 
-    mScrollView.SetSize( stageSize );//Vector2( stageSize.width, fieldHeight ) );//stageSize );
+    mScrollView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+
     mScrollView.SetAxisAutoLock( true );
     mScrollView.SetAxisAutoLockGradient( 1.0f );
 
@@ -352,13 +350,39 @@ public:
     rulerX->SetDomain( RulerDomain( stageSize.width * -0.125f, stageSize.width * 1.125f ) ); //< Scroll slightly left/right of image field.
     mScrollView.SetRulerX ( rulerX );
 
-    RulerPtr rulerY = new DefaultRuler(); //stageSize.height ); //< Snap in multiples of a screen / stage height
+    RulerPtr rulerY = new DefaultRuler(); //< Snap in multiples of a screen / stage height
     rulerY->SetDomain( RulerDomain( - fieldHeight * 0.5f + stageSize.height * 0.5f - GRID_CELL_PADDING, fieldHeight * 0.5f + stageSize.height * 0.5f + GRID_CELL_PADDING ) );
     mScrollView.SetRulerY ( rulerY );
 
     mContentLayer.Add( mScrollView );
     mScrollView.Add( imageField );
     mGridActor = imageField;
+
+    // Create the scroll bar
+    mScrollBarVertical = ScrollBar::New(Toolkit::ScrollBar::Vertical);
+    mScrollBarVertical.SetParentOrigin(ParentOrigin::TOP_RIGHT);
+    mScrollBarVertical.SetAnchorPoint(AnchorPoint::TOP_RIGHT);
+    mScrollBarVertical.SetResizePolicy(Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::HEIGHT);
+    mScrollBarVertical.SetResizePolicy(Dali::ResizePolicy::FIT_TO_CHILDREN, Dali::Dimension::WIDTH);
+    mScrollView.Add(mScrollBarVertical);
+
+    mScrollBarHorizontal = ScrollBar::New(Toolkit::ScrollBar::Horizontal);
+    mScrollBarHorizontal.SetParentOrigin(ParentOrigin::BOTTOM_LEFT);
+    mScrollBarHorizontal.SetAnchorPoint(AnchorPoint::TOP_LEFT);
+    mScrollBarHorizontal.SetResizePolicy(Dali::ResizePolicy::FIT_TO_CHILDREN, Dali::Dimension::WIDTH);
+    mScrollBarHorizontal.SetOrientation(Quaternion(Radian( 1.5f * Math::PI ), Vector3::ZAXIS));
+    mScrollView.Add(mScrollBarHorizontal);
+
+    mScrollView.OnRelayoutSignal().Connect( this, &ImageScalingIrregularGridController::OnScrollViewRelayout );
+
+    // Scroll to top of grid so first images loaded are on-screen:
+    mScrollView.ScrollTo( Vector2( 0, -1000000 ) );
+  }
+
+  void OnScrollViewRelayout(Actor actor)
+  {
+    // Make the height of the horizontal scroll bar to be the same as the width of scroll view.
+    mScrollBarHorizontal.SetSize(Vector2(0.0f, mScrollView.GetRelayoutSize( Dimension::WIDTH) ));
   }
 
   /**
@@ -369,7 +393,7 @@ public:
   Actor BuildImageField( const float fieldWidth,
                            const unsigned gridWidth,
                            const unsigned maxGridHeight,
-                           ImageAttributes::ScalingMode scalingMode,
+                           Dali::FittingMode::Type fittingMode,
                            float & outFieldHeight )
   {
     // Generate the list of image configurations to be fitted into the field:
@@ -417,7 +441,7 @@ public:
     // coordinates in a frame defined by a parent actor:
 
     Actor gridActor = Actor::New();
-    gridActor.SetSizeMode( SIZE_EQUAL_TO_PARENT );
+    gridActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
     gridActor.SetParentOrigin( ParentOrigin::CENTER );
     gridActor.SetAnchorPoint( AnchorPoint::CENTER );
 
@@ -436,11 +460,11 @@ public:
       const Vector2 imageRegionCorner = gridOrigin + cellSize * Vector2( imageSource.cellX, imageSource.cellY );
       const Vector2 imagePosition = imageRegionCorner + Vector2( GRID_CELL_PADDING , GRID_CELL_PADDING ) + imageSize * 0.5f;
 
-      ImageActor image = CreateImageActor( imageSource.configuration.path, imageSize.x, imageSize.y, scalingMode );
+      ImageView image = CreateImageView( imageSource.configuration.path, imageSize.x, imageSize.y, fittingMode );
       image.SetPosition( Vector3( imagePosition.x, imagePosition.y, 0 ) );
       image.SetSize( imageSize );
       image.TouchedSignal().Connect( this, &ImageScalingIrregularGridController::OnTouchImage );
-      mScalingModes[image.GetId()] = scalingMode;
+      mFittingModes[image.GetId()] = fittingMode;
       mSizes[image.GetId()] = imageSize;
 
       gridActor.Add( image );
@@ -463,19 +487,19 @@ public:
       {
         // Spin the image a few times:
         Animation animation = Animation::New(SPIN_DURATION);
-        animation.RotateBy( actor, Degree(360.0f * SPIN_DURATION), Vector3::XAXIS, AlphaFunctions::EaseOut);
+        animation.AnimateBy( Property( actor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f * SPIN_DURATION) ), Vector3::XAXIS ), AlphaFunction::EASE_OUT );
         animation.Play();
 
         // Change the scaling mode:
         const unsigned id = actor.GetId();
-        ImageAttributes::ScalingMode newMode = NextMode( mScalingModes[id] );
+        Dali::FittingMode::Type newMode = NextMode( mFittingModes[id] );
         const Vector2 imageSize = mSizes[actor.GetId()];
 
-        ImageActor imageActor = ImageActor::DownCast( actor );
-        Image oldImage = imageActor.GetImage();
+        ImageView imageView = ImageView::DownCast( actor );
+        Image oldImage = imageView.GetImage();
         Image newImage = CreateImage( ResourceImage::DownCast(oldImage).GetUrl(), imageSize.width + 0.5f, imageSize.height + 0.5f, newMode );
-        imageActor.SetImage( newImage );
-        mScalingModes[id] = newMode;
+        imageView.SetImage( newImage );
+        mFittingModes[id] = newMode;
       }
     }
     return false;
@@ -508,19 +532,19 @@ public:
 
     for( unsigned i = 0; i < numChildren; ++i )
     {
-      ImageActor gridImageActor = ImageActor::DownCast( mGridActor.GetChildAt( i ) );
-      if( gridImageActor )
+      ImageView gridImageView = ImageView::DownCast( mGridActor.GetChildAt( i ) );
+      if( gridImageView )
       {
         // Cycle the scaling mode options:
-        const Vector2 imageSize = mSizes[gridImageActor.GetId()];
-        ImageAttributes::ScalingMode newMode = NextMode( mScalingModes[gridImageActor.GetId()] );
-        Image oldImage = gridImageActor.GetImage();
+        const Vector2 imageSize = mSizes[gridImageView.GetId()];
+        Dali::FittingMode::Type newMode = NextMode( mFittingModes[gridImageView.GetId()] );
+        Image oldImage = gridImageView.GetImage();
         Image newImage = CreateImage(ResourceImage::DownCast(oldImage).GetUrl(), imageSize.width, imageSize.height, newMode );
-        gridImageActor.SetImage( newImage );
+        gridImageView.SetImage( newImage );
 
-        mScalingModes[gridImageActor.GetId()] = newMode;
+        mFittingModes[gridImageView.GetId()] = newMode;
 
-        SetTitle( std::string( newMode == ImageAttributes::ShrinkToFit ? "ShrinkToFit" : newMode == ImageAttributes::ScaleToFill ?  "ScaleToFill" : newMode == ImageAttributes::FitWidth ? "FitWidth" : "FitHeight" ) );
+        SetTitle( std::string( newMode == FittingMode::SHRINK_TO_FIT ? "SHRINK_TO_FIT" : newMode == FittingMode::SCALE_TO_FILL ?  "SCALE_TO_FILL" : newMode == FittingMode::FIT_WIDTH ? "FIT_WIDTH" : "FIT_HEIGHT" ) );
       }
     }
     return true;
@@ -532,7 +556,14 @@ public:
    */
   void SetTitle(const std::string& title)
   {
-    // TODO
+    if(!mTitleActor)
+    {
+      mTitleActor = DemoHelper::CreateToolBarLabel( "" );
+      // Add title to the tool bar.
+      mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
+    }
+
+    mTitleActor.SetProperty( TextLabel::Property::TEXT, title );
   }
 
   /**
@@ -540,7 +571,7 @@ public:
    * note this state (mScrolling = true)
    * @param[in] position Current Scroll Position
    */
-  void OnScrollStarted( const Vector3& position )
+  void OnScrollStarted( const Vector2& position )
   {
     mScrolling = true;
   }
@@ -550,7 +581,7 @@ public:
    * note this state (mScrolling = false).
    * @param[in] position Current Scroll Position
    */
-  void OnScrollCompleted( const Vector3& position )
+  void OnScrollCompleted( const Vector2& position )
   {
     mScrolling = false;
   }
@@ -559,12 +590,15 @@ private:
   Application&  mApplication;
 
   Layer mContentLayer;                ///< The content layer (contains non gui chrome actors)
-  Toolkit::View mView;                ///< The View instance.
+  Toolkit::Control mView;             ///< The View instance.
   Toolkit::ToolBar mToolBar;          ///< The View's Toolbar.
+  TextLabel mTitleActor;               ///< The Toolbar's Title.
   Actor mGridActor;                   ///< The container for the grid of images
   ScrollView mScrollView;             ///< ScrollView UI Component
+  ScrollBar mScrollBarVertical;
+  ScrollBar mScrollBarHorizontal;
   bool mScrolling;                    ///< ScrollView scrolling state (true = scrolling, false = stationary)
-  std::map<unsigned, ImageAttributes::ScalingMode> mScalingModes; ///< Stores the current scaling mode of each image, keyed by image actor id.
+  std::map<unsigned, Dali::FittingMode::Type> mFittingModes; ///< Stores the current scaling mode of each image, keyed by image actor id.
   std::map<unsigned, Vector2> mSizes; ///< Stores the current size of each image, keyed by image actor id.
 };
 
@@ -578,7 +612,7 @@ void RunTest( Application& application )
 /** Entry point for Linux & Tizen applications */
 int main( int argc, char **argv )
 {
-  Application application = Application::New( &argc, &argv );
+  Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
 
   RunTest( application );