Changes after TouchedSignal changes
[platform/core/uifw/dali-demo.git] / examples / blocks / blocks-example.cpp
index ed7195f..9018283 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 <dali/dali.h>
 #include <dali-toolkit/dali-toolkit.h>
+#include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
 #include "shared/view.h"
 
-#include <dali/devel-api/object/handle-devel.h>
-
 using namespace Dali;
 using namespace Dali::Toolkit;
 using namespace DemoHelper;
@@ -61,12 +60,12 @@ const Vector3 INITIAL_BALL_DIRECTION(1.0f, 1.0f, 0.0f);                     ///<
 const std::string WOBBLE_PROPERTY_NAME("wobbleProperty");                  ///< Wobble property name.
 const std::string COLLISION_PROPERTY_NAME("collisionProperty");            ///< Collision property name.
 
-const Vector2 BRICK_SIZE(0.1f, 0.05f );                                     ///< Brick size relative to width of stage.
-const Vector2 BALL_SIZE( 0.05f, 0.05f );                                    ///< Ball size relative to width of stage.
-const Vector2 PADDLE_SIZE( 0.2f, 0.05f );                                   ///< Paddle size relative to width of stage.
-const Vector2 PADDLE_HANDLE_SIZE( 0.3f, 0.3f );                             ///< Paddle handle size relative to width of stage.
-const Vector2 BALL_START_POSITION(0.5f, 0.8f);                              ///< Ball start position relative to stage size.
-const Vector2 PADDLE_START_POSITION(0.5f, 0.9f);                            ///< Paddler start position relative to stage size.
+const Vector2 BRICK_SIZE(0.1f, 0.05f );                                     ///< Brick size relative to width of window.
+const Vector2 BALL_SIZE( 0.05f, 0.05f );                                    ///< Ball size relative to width of window.
+const Vector2 PADDLE_SIZE( 0.2f, 0.05f );                                   ///< Paddle size relative to width of window.
+const Vector2 PADDLE_HANDLE_SIZE( 0.3f, 0.3f );                             ///< Paddle handle size relative to width of window.
+const Vector2 BALL_START_POSITION(0.5f, 0.8f);                              ///< Ball start position relative to window size.
+const Vector2 PADDLE_START_POSITION(0.5f, 0.9f);                            ///< Paddler start position relative to window size.
 const Vector2 PADDLE_HIT_MARGIN( 0.1, 0.15f );                              ///< Extra hit Area for Paddle when touching.
 
 const int TOTAL_LIVES(3);                                                   ///< Total lives in game before it's game over!
@@ -250,13 +249,10 @@ public:
    */
   void Create(Application& application)
   {
-    Stage::GetCurrent().KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
-
-    // Hide the indicator bar
-    application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
+    application.GetWindow().KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
 
     // Creates a default view with a default tool bar.
-    // The view is added to the stage.
+    // The view is added to the window.
     Toolkit::ToolBar toolBar;
     mContentLayer = DemoHelper::CreateView( application,
                                             mView,
@@ -266,7 +262,7 @@ public:
                                             APPLICATION_TITLE );
 
     // Add an extra space on the right to center the title text.
-    toolBar.AddControl( Actor::New(), DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight );
+    toolBar.AddControl( Actor::New(), DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_RIGHT );
 
     // Create the content layer, which is where game actors appear.
     AddContentLayer();
@@ -275,68 +271,68 @@ public:
 private:
 
   /**
-   * Adds a new layer to the stage, containing game actors.
+   * Adds a new layer to the window, containing game actors.
    */
   void AddContentLayer()
   {
-    Stage stage = Stage::GetCurrent();
-    const Vector3 stageSize(stage.GetSize());
+    Window window = mApplication.GetWindow();
+    const Vector3 windowSize(window.GetSize());
 
     // Ball setup
-    mBallStartPosition = stageSize * Vector3( BALL_START_POSITION );
+    mBallStartPosition = windowSize * Vector3( BALL_START_POSITION );
     mBall = CreateImage(BALL_IMAGE);
-    mBall.SetPosition( mBallStartPosition );
-    mBall.SetSize( BALL_SIZE * stageSize.width );
+    mBall.SetProperty( Actor::Property::POSITION, mBallStartPosition );
+    mBall.SetProperty( Actor::Property::SIZE, BALL_SIZE * windowSize.width );
     mContentLayer.Add(mBall);
     mBallVelocity = Vector3::ZERO;
 
     // Paddle setup
-    mPaddleHitMargin = Vector2(stageSize) * PADDLE_HIT_MARGIN;
+    mPaddleHitMargin = Vector2(windowSize) * PADDLE_HIT_MARGIN;
     mPaddle = Actor::New();
     mPaddleHandle = CreateImage(PADDLE_HANDLE_IMAGE);
     mPaddleImage = CreateImage(PADDLE_IMAGE);
     mPaddle.Add( mPaddleHandle );
     mPaddle.Add( mPaddleImage );
-    mPaddleHandle.SetParentOrigin( ParentOrigin::TOP_CENTER );
-    mPaddleHandle.SetAnchorPoint( AnchorPoint::TOP_CENTER );
-    mPaddleHandle.SetPosition( 0.0f, stageSize.width * 0.0125f );
-    mPaddleImage.SetParentOrigin( ParentOrigin::TOP_CENTER );
-    mPaddleImage.SetAnchorPoint( AnchorPoint::TOP_CENTER );
-    mPaddle.SetParentOrigin( ParentOrigin::TOP_LEFT );
-    mPaddle.SetAnchorPoint( AnchorPoint::CENTER );
-    mPaddleFullSize = PADDLE_SIZE * stageSize.width;
-    mPaddle.SetSize( mPaddleFullSize + mPaddleHitMargin );
-    mPaddleHandle.SetSize( PADDLE_HANDLE_SIZE * stageSize.width );
-    mPaddleImage.SetSize( mPaddleFullSize );
+    mPaddleHandle.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
+    mPaddleHandle.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
+    mPaddleHandle.SetProperty( Actor::Property::POSITION, Vector2( 0.0f, windowSize.width * 0.0125f ));
+    mPaddleImage.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
+    mPaddleImage.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
+    mPaddle.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
+    mPaddle.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
+    mPaddleFullSize = PADDLE_SIZE * windowSize.width;
+    mPaddle.SetProperty( Actor::Property::SIZE, mPaddleFullSize + mPaddleHitMargin );
+    mPaddleHandle.SetProperty( Actor::Property::SIZE, PADDLE_HANDLE_SIZE * windowSize.width );
+    mPaddleImage.SetProperty( Actor::Property::SIZE, mPaddleFullSize );
 
     mWobbleProperty = mPaddle.RegisterProperty(WOBBLE_PROPERTY_NAME, 0.0f);
     Constraint wobbleConstraint = Constraint::New<Quaternion>( mPaddle, Actor::Property::ORIENTATION, WobbleConstraint(Degree( 10.0f )));
     wobbleConstraint.AddSource( LocalSource(mWobbleProperty) );
     wobbleConstraint.Apply();
 
-    mPaddle.SetPosition( stageSize * Vector3( PADDLE_START_POSITION ) );
+    mPaddle.SetProperty( Actor::Property::POSITION, windowSize * Vector3( PADDLE_START_POSITION ) );
     mContentLayer.Add(mPaddle);
-    mPaddle.TouchSignal().Connect(this, &ExampleController::OnTouchPaddle);
-    mContentLayer.TouchSignal().Connect(this, &ExampleController::OnTouchLayer);
+    mPaddle.TouchedSignal().Connect(this, &ExampleController::OnTouchPaddle);
+    mContentLayer.TouchedSignal().Connect(this, &ExampleController::OnTouchLayer);
 
-    const float margin(BALL_SIZE.width * stageSize.width * 0.5f);
+    const float margin(BALL_SIZE.width * windowSize.width * 0.5f);
 
     // Set up notifications for ball's collisions against walls.
     PropertyNotification leftNotification = mBall.AddPropertyNotification( Actor::Property::POSITION_X, LessThanCondition(margin) );
     leftNotification.NotifySignal().Connect( this, &ExampleController::OnHitLeftWall );
 
-    PropertyNotification rightNotification = mBall.AddPropertyNotification( Actor::Property::POSITION_X, GreaterThanCondition(stageSize.width - margin) );
+    PropertyNotification rightNotification = mBall.AddPropertyNotification( Actor::Property::POSITION_X, GreaterThanCondition(windowSize.width - margin) );
     rightNotification.NotifySignal().Connect( this, &ExampleController::OnHitRightWall );
 
     PropertyNotification topNotification = mBall.AddPropertyNotification( Actor::Property::POSITION_Y, LessThanCondition(margin) );
     topNotification.NotifySignal().Connect( this, &ExampleController::OnHitTopWall );
 
-    PropertyNotification bottomNotification = mBall.AddPropertyNotification( Actor::Property::POSITION_Y, GreaterThanCondition(stageSize.height + margin) );
+    PropertyNotification bottomNotification = mBall.AddPropertyNotification( Actor::Property::POSITION_Y, GreaterThanCondition(windowSize.height + margin) );
     bottomNotification.NotifySignal().Connect( this, &ExampleController::OnHitBottomWall );
 
     // Set up notification for ball colliding against paddle.
     Actor delegate = Actor::New();
-    stage.Add(delegate);
+    window.Add(delegate);
     Property::Index property = delegate.RegisterProperty(COLLISION_PROPERTY_NAME, Vector3::ZERO);
     Constraint constraint = Constraint::New<Vector3>( delegate, property, CollisionCircleRectangleConstraint( -Vector3(0.0f, mPaddleHitMargin.height * 0.575f, 0.0f),-Vector3(mPaddleHitMargin) ) );
     constraint.AddSource( Source(mBall, Actor::Property::POSITION) );
@@ -359,10 +355,10 @@ private:
   {
     mLives = TOTAL_LIVES;
     mLevel = 0;
-    mBall.SetPosition( mBallStartPosition );
+    mBall.SetProperty( Actor::Property::POSITION, mBallStartPosition );
     mBallVelocity = Vector3::ZERO;
-    mPaddle.SetSize( mPaddleFullSize + mPaddleHitMargin );
-    mPaddleImage.SetSize( mPaddleFullSize );
+    mPaddle.SetProperty( Actor::Property::SIZE, mPaddleFullSize + mPaddleHitMargin );
+    mPaddleImage.SetProperty( Actor::Property::SIZE, mPaddleFullSize );
 
     LoadLevel(mLevel);
   }
@@ -381,8 +377,8 @@ private:
     }
 
     mLevelContainer = Actor::New();
-    mLevelContainer.SetAnchorPoint( AnchorPoint::CENTER );
-    mLevelContainer.SetParentOrigin( ParentOrigin::CENTER );
+    mLevelContainer.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
+    mLevelContainer.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
     mLevelContainer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
 
     mContentLayer.Add( mLevelContainer );
@@ -391,8 +387,8 @@ private:
 
     if( mBrickImageMap.Empty() )
     {
-      Vector2 stageSize(Stage::GetCurrent().GetSize());
-      const Vector2 brickSize(BRICK_SIZE * Vector2(stageSize.x, stageSize.x));
+      Vector2 windowSize(mApplication.GetWindow().GetSize());
+      const Vector2 brickSize(BRICK_SIZE * Vector2(windowSize.x, windowSize.x));
 
       mBrickImageMap["desiredWidth"] = static_cast<int>( brickSize.width );
       mBrickImageMap["desiredHeight"] = static_cast<int>( brickSize.height );
@@ -429,13 +425,13 @@ private:
    */
   void GenerateLevel0()
   {
-    Vector2 stageSize(Stage::GetCurrent().GetSize());
-    const Vector2 brickSize(BRICK_SIZE * stageSize.width);
+    Vector2 windowSize(mApplication.GetWindow().GetSize());
+    const Vector2 brickSize(BRICK_SIZE * windowSize.width);
 
-    const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
-    const int rows = (0.3f * stageSize.height) / brickSize.height;   // 30 percent of the height of the screen covered with bricks.
-    const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
-                           stageSize.y * 0.125f );
+    const int columns = (0.85f * windowSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
+    const int rows = (0.3f * windowSize.height) / brickSize.height;   // 30 percent of the height of the screen covered with bricks.
+    const Vector2 offset( (windowSize.x - (columns * brickSize.width)) * 0.5f,
+                           windowSize.y * 0.125f );
 
     for(int j = 0; j < rows; j++)
     {
@@ -453,13 +449,13 @@ private:
    */
   void GenerateLevel1()
   {
-    Vector2 stageSize(Stage::GetCurrent().GetSize());
-    const Vector2 brickSize(BRICK_SIZE * stageSize.width);
+    Vector2 windowSize(mApplication.GetWindow().GetSize());
+    const Vector2 brickSize(BRICK_SIZE * windowSize.width);
 
-    const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
-    const int rows = (0.3f * stageSize.height) / brickSize.height;   // 30 percent of the height of the screen covered with bricks.
-    const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
-                           stageSize.y * 0.125f );
+    const int columns = (0.85f * windowSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
+    const int rows = (0.3f * windowSize.height) / brickSize.height;   // 30 percent of the height of the screen covered with bricks.
+    const Vector2 offset( (windowSize.x - (columns * brickSize.width)) * 0.5f,
+                           windowSize.y * 0.125f );
 
     for(int j = 0; j < rows; j++)
     {
@@ -482,13 +478,13 @@ private:
    */
   void GenerateLevel2()
   {
-    Vector2 stageSize(Stage::GetCurrent().GetSize());
-    const Vector2 brickSize(BRICK_SIZE * stageSize.width);
+    Vector2 windowSize(mApplication.GetWindow().GetSize());
+    const Vector2 brickSize(BRICK_SIZE * windowSize.width);
 
-    const int columns = (0.85f * stageSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
-    const int rows = (0.3f * stageSize.height) / brickSize.height;   // 30 percent of the height of the screen covered with bricks.
-    const Vector2 offset( (stageSize.x - (columns * brickSize.width)) * 0.5f,
-                           stageSize.y * 0.125f );
+    const int columns = (0.85f * windowSize.width) / brickSize.width; // 85 percent of the width of the screen covered with bricks.
+    const int rows = (0.3f * windowSize.height) / brickSize.height;   // 30 percent of the height of the screen covered with bricks.
+    const Vector2 offset( (windowSize.x - (columns * brickSize.width)) * 0.5f,
+                           windowSize.y * 0.125f );
 
     // lays down bricks in a spiral formation starting at i,j = (0,0) top left corner
     // travelling right di,dj = (1,0) initially
@@ -550,7 +546,7 @@ private:
 
 
   /**
-   * Creates a brick at a specified position on the stage
+   * Creates a brick at a specified position on the window
    * @param[in] position the position for the brick
    * @param[in] type the type of brick
    * @return The Brick Actor is returned.
@@ -560,9 +556,9 @@ private:
     mBrickImageMap["url"] = BRICK_IMAGE_PATH[type];
     ImageView brick = ImageView::New();
     brick.SetProperty( ImageView::Property::IMAGE, mBrickImageMap );
-    brick.SetParentOrigin(ParentOrigin::TOP_LEFT);
-    brick.SetAnchorPoint(AnchorPoint::CENTER);
-    brick.SetPosition( Vector3( position ) );
+    brick.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
+    brick.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER);
+    brick.SetProperty( Actor::Property::POSITION, position );
 
     // Add a constraint on the brick between it and the ball generating a collision-property
     Property::Index property = brick.RegisterProperty(COLLISION_PROPERTY_NAME, Vector3::ZERO);
@@ -588,9 +584,14 @@ private:
    */
   ImageView CreateImage(const std::string& filename)
   {
-    ImageView actor = ImageView::New(filename);
-    actor.SetParentOrigin(ParentOrigin::TOP_LEFT);
-    actor.SetAnchorPoint(AnchorPoint::CENTER);
+    Property::Map propertyMap;
+    propertyMap.Insert(Visual::Property::TYPE, Visual::IMAGE);
+    propertyMap.Insert(ImageVisual::Property::URL, filename);
+    propertyMap.Insert(DevelVisual::Property::VISUAL_FITTING_MODE, DevelVisual::FILL);
+    ImageView actor = ImageView::New();
+    actor.SetProperty(Toolkit::ImageView::Property::IMAGE, propertyMap);
+    actor.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
+    actor.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER);
     return actor;
   }
 
@@ -614,7 +615,7 @@ private:
    * @param[in] actor The actor touched
    * @param[in] event The touch event
    */
-  bool OnTouchPaddle(Actor actor, const TouchData& event)
+  bool OnTouchPaddle(Actor actor, const TouchEvent& event)
   {
     if(event.GetPointCount()>0)
     {
@@ -623,7 +624,7 @@ private:
         // Get point where user touched paddle (relative to paddle's center)
         Vector2 screenPoint = event.GetScreenPosition( 0 );
         mRelativeDragPoint = screenPoint;
-        mRelativeDragPoint -= actor.GetCurrentPosition();
+        mRelativeDragPoint -= actor.GetCurrentProperty< Vector3 >( Actor::Property::POSITION );
 
         mDragActor = actor;
         mDragAnimation = Animation::New(0.25f);
@@ -640,14 +641,14 @@ private:
    * @param[in] actor The actor touched
    * @param[in] event The touch event
    */
-  bool OnTouchLayer(Actor actor, const TouchData& event)
+  bool OnTouchLayer(Actor actor, const TouchEvent& event)
   {
     if(event.GetPointCount()>0)
     {
       if(mDragActor)
       {
         Vector3 position( event.GetScreenPosition( 0 ) );
-        mPaddle.SetPosition( position - mRelativeDragPoint );
+        mPaddle.SetProperty( Actor::Property::POSITION, position - mRelativeDragPoint );
 
         if( event.GetState( 0 ) == PointState::UP ) // Stop dragging
         {
@@ -725,8 +726,8 @@ private:
   void OnPaddleShrunk( Animation &source )
   {
     // Reposition Ball in start position, and make ball appear.
-    mBall.SetPosition( mBallStartPosition );
-    mBall.SetColor( Vector4(1.0f, 1.0f, 1.0f, 0.1f) );
+    mBall.SetProperty( Actor::Property::POSITION, mBallStartPosition );
+    mBall.SetProperty( Actor::Property::COLOR, Vector4(1.0f, 1.0f, 1.0f, 0.1f) );
     Animation appear = Animation::New(0.5f);
     appear.AnimateTo( Property(mBall, Actor::Property::COLOR), Vector4(1.0f, 1.0f, 1.0f, 1.0f) );
     appear.Play();
@@ -744,8 +745,8 @@ private:
   void OnHitPaddle(PropertyNotification& source)
   {
     Actor delegate = Actor::DownCast(source.GetTarget());
-    Vector3 collisionVector = DevelHandle::GetCurrentProperty< Vector3 >( delegate, source.GetTargetProperty() );
-    Vector3 ballRelativePosition(mBall.GetCurrentPosition() - mPaddle.GetCurrentPosition());
+    Vector3 collisionVector = delegate.GetCurrentProperty< Vector3 >( source.GetTargetProperty() );
+    Vector3 ballRelativePosition(mBall.GetCurrentProperty< Vector3 >( Actor::Property::POSITION ) - mPaddle.GetCurrentProperty< Vector3 >( Actor::Property::POSITION ));
     ballRelativePosition.Normalize();
 
     collisionVector.x += ballRelativePosition.x * 0.5f;
@@ -779,7 +780,7 @@ private:
   void OnHitBrick(PropertyNotification& source)
   {
     Actor brick = Actor::DownCast(source.GetTarget());
-    Vector3 collisionVector = DevelHandle::GetCurrentProperty< Vector3 >( brick, source.GetTargetProperty() );
+    Vector3 collisionVector = brick.GetCurrentProperty< Vector3 >( source.GetTargetProperty() );
 
     const float normalVelocity = fabsf(mBallVelocity.Dot(collisionVector));
     mBallVelocity += collisionVector * normalVelocity * 2.0f;
@@ -807,7 +808,7 @@ private:
    */
   void OnBrickDestroyed( Animation& source )
   {
-    // Remove brick from stage, it's constraint and property notification should also remove themselves.
+    // Remove brick from window, it's constraint and property notification should also remove themselves.
     Actor brick = mDestroyAnimationMap[source];
     mDestroyAnimationMap.erase(source);
     brick.GetParent().Remove(brick);
@@ -825,7 +826,7 @@ private:
    */
   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) )
       {
@@ -864,18 +865,10 @@ private:
   int mBrickCount;                                      ///< Total bricks on screen.
 };
 
-void RunTest(Application& app)
-{
-  ExampleController test(app);
-
-  app.MainLoop();
-}
-
 int DALI_EXPORT_API main(int argc, char **argv)
 {
   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
-
-  RunTest(app);
-
+  ExampleController test(app);
+  app.MainLoop();
   return 0;
 }