Changes after TouchedSignal changes
[platform/core/uifw/dali-demo.git] / examples / image-scaling-irregular-grid / image-scaling-irregular-grid-example.cpp
index 69f5931..cb41669 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
 #include <algorithm>
 #include <map>
 #include <dali-toolkit/dali-toolkit.h>
-#include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
 #include <iostream>
 #include <dali-toolkit/devel-api/controls/control-devel.h>
+#include <dali-toolkit/devel-api/controls/scroll-bar/scroll-bar.h>
+#include <random>       // std::default_random_engine
+#include <chrono>       // std::chrono::system_clock
 
 // INTERNAL INCLUDES
 #include "grid-flags.h"
@@ -190,9 +192,9 @@ ImageView CreateImageView(const std::string& filename, int width, int height, Da
   map[Toolkit::ImageVisual::Property::FITTING_MODE] = fittingMode;
   imageView.SetProperty( Toolkit::ImageView::Property::IMAGE, map );
 
-  imageView.SetName( filename );
-  imageView.SetParentOrigin(ParentOrigin::CENTER);
-  imageView.SetAnchorPoint(AnchorPoint::CENTER);
+  imageView.SetProperty( Dali::Actor::Property::NAME, filename );
+  imageView.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
+  imageView.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER);
 
   return imageView;
 }
@@ -281,11 +283,11 @@ public:
   void ResourceReadySignal( Toolkit::Control control )
   {
     mImagesLoaded++;
-    // To allow fast startup, we only place a small number of ImageViews on stage first
+    // To allow fast startup, we only place a small number of ImageViews on window first
     if ( mImagesLoaded == INITIAL_IMAGES_TO_LOAD )
     {
-      // Adding the ImageViews to the stage will trigger loading of the Images
-      mGridActor.Add( mOffStageImageViews );
+      // Adding the ImageViews to the window will trigger loading of the Images
+      mGridActor.Add( mOffWindowImageViews );
     }
   }
 
@@ -297,14 +299,11 @@ public:
   {
     std::cout << "ImageScalingIrregularGridController::Create" << std::endl;
 
-    // Get a handle to the stage:
-    Stage stage = Stage::GetCurrent();
+    // Get a handle to the window:
+    Window window = application.GetWindow();
 
     // Connect to input event signals:
-    stage.KeyEventSignal().Connect(this, &ImageScalingIrregularGridController::OnKeyEvent);
-
-    // Hide the indicator bar
-    mApplication.GetWindow().ShowIndicator(Dali::Window::INVISIBLE);
+    window.KeyEventSignal().Connect(this, &ImageScalingIrregularGridController::OnKeyEvent);
 
     // Create a default view with a default tool bar:
     mContentLayer = DemoHelper::CreateView( mApplication,
@@ -316,17 +315,17 @@ public:
 
     // Create an image scaling toggle button. (right of toolbar)
     Toolkit::PushButton toggleScalingButton = Toolkit::PushButton::New();
-    toggleScalingButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, TOGGLE_SCALING_IMAGE );
-    toggleScalingButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, TOGGLE_SCALING_IMAGE_SELECTED );
+    toggleScalingButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, TOGGLE_SCALING_IMAGE );
+    toggleScalingButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, TOGGLE_SCALING_IMAGE_SELECTED );
     toggleScalingButton.ClickedSignal().Connect( this, &ImageScalingIrregularGridController::OnToggleScalingTouched );
-    mToolBar.AddControl( toggleScalingButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
+    mToolBar.AddControl( toggleScalingButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_RIGHT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
 
     SetTitle( APPLICATION_TITLE );
 
-    mOffStageImageViews = Actor::New();
-    mOffStageImageViews.SetAnchorPoint( AnchorPoint::CENTER );
-    mOffStageImageViews.SetParentOrigin(ParentOrigin::CENTER);
-    mOffStageImageViews.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+    mOffWindowImageViews = Actor::New();
+    mOffWindowImageViews.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
+    mOffWindowImageViews.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
+    mOffWindowImageViews.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
 
     // Build the main content of the widow:
     PopulateContentLayer( DEFAULT_SCALING_MODE );
@@ -337,19 +336,19 @@ public:
    */
   void PopulateContentLayer( const Dali::FittingMode::Type fittingMode )
   {
-    Stage stage = Stage::GetCurrent();
-    Vector2 stageSize = stage.GetSize();
+    Window window = mApplication.GetWindow();
+    Vector2 windowSize = window.GetSize();
 
     float fieldHeight;
-    Actor imageField = BuildImageField( stageSize.x, GRID_WIDTH, GRID_MAX_HEIGHT, fittingMode, fieldHeight );
+    Actor imageField = BuildImageField( windowSize.x, GRID_WIDTH, GRID_MAX_HEIGHT, fittingMode, fieldHeight );
 
     mScrollView = ScrollView::New();
 
     mScrollView.ScrollStartedSignal().Connect( this, &ImageScalingIrregularGridController::OnScrollStarted );
     mScrollView.ScrollCompletedSignal().Connect( this, &ImageScalingIrregularGridController::OnScrollCompleted );
 
-    mScrollView.SetAnchorPoint(AnchorPoint::CENTER);
-    mScrollView.SetParentOrigin(ParentOrigin::CENTER);
+    mScrollView.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER);
+    mScrollView.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
 
     mScrollView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
 
@@ -358,12 +357,12 @@ public:
 
     // Restrict scrolling to mostly vertical only, but with some horizontal wiggle-room:
 
-    RulerPtr rulerX = new FixedRuler( stageSize.width ); //< Pull the view back to the grid's centre-line when touch is release using a snapping ruler.
-    rulerX->SetDomain( RulerDomain( stageSize.width * -0.125f, stageSize.width * 1.125f ) ); //< Scroll slightly left/right of image field.
+    RulerPtr rulerX = new FixedRuler( windowSize.width ); //< Pull the view back to the grid's centre-line when touch is release using a snapping ruler.
+    rulerX->SetDomain( RulerDomain( windowSize.width * -0.125f, windowSize.width * 1.125f ) ); //< Scroll slightly left/right of image field.
     mScrollView.SetRulerX ( rulerX );
 
-    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 ) );
+    RulerPtr rulerY = new DefaultRuler(); //< Snap in multiples of a screen / window height
+    rulerY->SetDomain( RulerDomain( - fieldHeight * 0.5f + windowSize.height * 0.5f - GRID_CELL_PADDING, fieldHeight * 0.5f + windowSize.height * 0.5f + GRID_CELL_PADDING ) );
     mScrollView.SetRulerY ( rulerY );
 
     mContentLayer.Add( mScrollView );
@@ -371,18 +370,18 @@ public:
     mGridActor = imageField;
 
     // Create the scroll bar
-    mScrollBarVertical = ScrollBar::New(Toolkit::ScrollBar::Vertical);
-    mScrollBarVertical.SetParentOrigin(ParentOrigin::TOP_RIGHT);
-    mScrollBarVertical.SetAnchorPoint(AnchorPoint::TOP_RIGHT);
+    mScrollBarVertical = ScrollBar::New(Toolkit::ScrollBar::VERTICAL);
+    mScrollBarVertical.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_RIGHT);
+    mScrollBarVertical.SetProperty( Actor::Property::ANCHOR_POINT,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 = ScrollBar::New(Toolkit::ScrollBar::HORIZONTAL);
+    mScrollBarHorizontal.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::BOTTOM_LEFT);
+    mScrollBarHorizontal.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
     mScrollBarHorizontal.SetResizePolicy(Dali::ResizePolicy::FIT_TO_CHILDREN, Dali::Dimension::WIDTH);
-    mScrollBarHorizontal.SetOrientation(Quaternion(Radian( 1.5f * Math::PI ), Vector3::ZAXIS));
+    mScrollBarHorizontal.SetProperty( Actor::Property::ORIENTATION, Quaternion( Quaternion( Radian( 1.5f * Math::PI ), Vector3::ZAXIS) ) );
     mScrollView.Add(mScrollBarHorizontal);
 
     mScrollView.OnRelayoutSignal().Connect( this, &ImageScalingIrregularGridController::OnScrollViewRelayout );
@@ -394,7 +393,7 @@ public:
   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) ));
+    mScrollBarHorizontal.SetProperty( Actor::Property::SIZE, Vector2(0.0f, mScrollView.GetRelayoutSize( Dimension::WIDTH) ));
   }
 
   /**
@@ -420,8 +419,10 @@ public:
       }
     }
     // Stir-up the list to get some nice irregularity in the generated field:
-    std::random_shuffle( configurations.begin(), configurations.end() );
-    std::random_shuffle( configurations.begin(), configurations.end() );
+    unsigned int seed = std::chrono::system_clock::now().time_since_epoch().count();
+    std::shuffle( configurations.begin(), configurations.end(), std::default_random_engine(seed) );
+    seed = std::chrono::system_clock::now().time_since_epoch().count();
+    std::shuffle( configurations.begin(), configurations.end(), std::default_random_engine(seed) );
 
     // Place the images in the grid:
 
@@ -454,8 +455,8 @@ public:
 
     Actor gridActor = Actor::New();
     gridActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
-    gridActor.SetParentOrigin( ParentOrigin::CENTER );
-    gridActor.SetAnchorPoint( AnchorPoint::CENTER );
+    gridActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
+    gridActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
 
     // Work out the constants of the grid and cell dimensions and positions:
     const float cellWidth = fieldWidth / gridWidth;
@@ -474,22 +475,22 @@ public:
       const Vector2 imagePosition = imageRegionCorner + Vector2( GRID_CELL_PADDING , GRID_CELL_PADDING ) + imageSize * 0.5f;
 
       ImageView image = CreateImageView( imageSource.configuration.path, imageSize.x, imageSize.y, fittingMode );
-      image.SetPosition( Vector3( imagePosition.x, imagePosition.y, 0 ) );
-      image.SetSize( imageSize );
-      image.TouchSignal().Connect( this, &ImageScalingIrregularGridController::OnTouchImage );
-      Toolkit::DevelControl::ResourceReadySignal( image).Connect( this, &ImageScalingIrregularGridController::ResourceReadySignal);
-      mFittingModes[image.GetId()] = fittingMode;
-      mResourceUrls[image.GetId()] = imageSource.configuration.path;
-      mSizes[image.GetId()] = imageSize;
+      image.SetProperty( Actor::Property::POSITION, Vector3( imagePosition.x, imagePosition.y, 0 ) );
+      image.SetProperty( Actor::Property::SIZE, imageSize );
+      image.TouchedSignal().Connect( this, &ImageScalingIrregularGridController::OnTouchImage );
+      image.ResourceReadySignal().Connect( this, &ImageScalingIrregularGridController::ResourceReadySignal );
+      mFittingModes[image.GetProperty< int >( Actor::Property::ID )] = fittingMode;
+      mResourceUrls[image.GetProperty< int >( Actor::Property::ID )] = imageSource.configuration.path;
+      mSizes[image.GetProperty< int >( Actor::Property::ID )] = imageSize;
       if ( count < INITIAL_IMAGES_TO_LOAD )
       {
         gridActor.Add( image );
       }
       else
       {
-        // Store the ImageView in an offstage actor until the inital batch of ImageViews have finished loading their images
+        // Store the ImageView in an offwindow actor until the inital batch of ImageViews have finished loading their images
         // Required
-        mOffStageImageViews.Add( image );
+        mOffWindowImageViews.Add( image );
       }
     }
 
@@ -501,7 +502,7 @@ public:
   * @param[in] actor The actor touched
   * @param[in] event The Touch information.
   */
-  bool OnTouchImage( Actor actor, const TouchData& event )
+  bool OnTouchImage( Actor actor, const TouchEvent& event )
   {
     if( ( event.GetPointCount() > 0 ) && ( !mScrolling ) )
     {
@@ -513,9 +514,9 @@ public:
         animation.Play();
 
         // Change the scaling mode:
-        const unsigned id = actor.GetId();
+        const unsigned id = actor.GetProperty< int >( Actor::Property::ID );
         Dali::FittingMode::Type newMode = NextMode( mFittingModes[id] );
-        const Vector2 imageSize = mSizes[actor.GetId()];
+        const Vector2 imageSize = mSizes[actor.GetProperty< int >( Actor::Property::ID )];
 
         ImageView imageView = ImageView::DownCast( actor );
         if( imageView)
@@ -541,7 +542,7 @@ public:
   */
   void OnKeyEvent(const KeyEvent& event)
   {
-    if( event.state == KeyEvent::Down )
+    if( event.GetState() == KeyEvent::DOWN )
     {
       if( IsKey( event, Dali::DALI_KEY_ESCAPE )
           || IsKey( event, Dali::DALI_KEY_BACK ) )
@@ -566,7 +567,7 @@ public:
       if( gridImageView )
       {
         // Cycle the scaling mode options:
-        unsigned int id = gridImageView.GetId();
+        unsigned int id = gridImageView.GetProperty< int >( Actor::Property::ID );
 
         const Vector2 imageSize = mSizes[ id ];
         Dali::FittingMode::Type newMode = NextMode( mFittingModes[ id ] );
@@ -599,7 +600,7 @@ public:
     {
       mTitleActor = DemoHelper::CreateToolBarLabel( "" );
       // Add title to the tool bar.
-      mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
+      mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HORIZONTAL_CENTER );
     }
 
     mTitleActor.SetProperty( TextLabel::Property::TEXT, title );
@@ -633,7 +634,7 @@ private:
   Toolkit::ToolBar mToolBar;          ///< The View's Toolbar.
   TextLabel mTitleActor;               ///< The Toolbar's Title.
   Actor mGridActor;                   ///< The container for the grid of images
-  Actor mOffStageImageViews;          ///< ImageViews held off stage until the inital batch have loaded their images
+  Actor mOffWindowImageViews;          ///< ImageViews held off window until the inital batch have loaded their images
   ScrollView mScrollView;             ///< ScrollView UI Component
   ScrollBar mScrollBarVertical;
   ScrollBar mScrollBarHorizontal;
@@ -644,19 +645,10 @@ private:
   unsigned int mImagesLoaded;         ///< How many images have been loaded
 };
 
-void RunTest( Application& application )
-{
-  ImageScalingIrregularGridController test( application );
-
-  application.MainLoop();
-}
-
-/** Entry point for Linux & Tizen applications */
 int DALI_EXPORT_API main( int argc, char **argv )
 {
   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
-
-  RunTest( application );
-
+  ImageScalingIrregularGridController test( application );
+  application.MainLoop();
   return 0;
 }