Revert "Revert "Renamed KeyEvent enum values to comply with coding standards.""
[platform/core/uifw/dali-demo.git] / examples / image-view-svg / image-view-svg-example.cpp
index 7c34a92..bfbe038 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 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-toolkit/dali-toolkit.h>
-#include <dali-toolkit/devel-api/controls/renderer-factory/renderer-factory.h>
-#include <dali-toolkit/devel-api/controls/renderer-factory/control-renderer.h>
+#include <dali/devel-api/actors/actor-devel.h>
 #include <string.h>
 
 using namespace Dali;
 
 namespace
 {
+const float MIN_SCALE = 0.6f;
 const float MAX_SCALE = 6.f;
 
 const char* SVG_IMAGES[] =
@@ -38,9 +38,10 @@ const char* SVG_IMAGES[] =
     DEMO_IMAGE_DIR "Kid1.svg"
 };
 const unsigned int NUM_SVG_IMAGES( sizeof( SVG_IMAGES ) / sizeof( SVG_IMAGES[0] ) );
-}
+const unsigned int NUM_IMAGES_DISPLAYED = 4u;
+} // unnamed namespace
 
-// This example shows how to display svg images with ImageView
+// This example shows how to display svg images with ImageView.
 //
 class ImageSvgController : public ConnectionTracker
 {
@@ -62,69 +63,73 @@ public:
   // The Init signal is received once (only) during the Application lifetime
   void Create( Application& application )
   {
-    // Get a handle to the stage
-    Stage stage = Stage::GetCurrent();
-    stage.SetBackgroundColor( Color::WHITE );
-    Vector2 stageSize = stage.GetSize();
-    mActorSize = stageSize/2.f;
+    // Get a handle to the window
+    Window window = application.GetWindow();
+    window.SetBackgroundColor( Color::WHITE );
+    Vector2 windowSize = window.GetSize();
+    mActorSize = windowSize/2.f;
 
-    stage.KeyEventSignal().Connect(this, &ImageSvgController::OnKeyEvent);
+    window.KeyEventSignal().Connect(this, &ImageSvgController::OnKeyEvent);
 
     // Background, for receiving gestures
-    mStageBackground = Actor::New();
-    mStageBackground.SetAnchorPoint( AnchorPoint::TOP_CENTER );
-    mStageBackground.SetParentOrigin( ParentOrigin::TOP_CENTER );
-    mStageBackground.SetSize( stageSize.x, stageSize.y );
-    stage.Add(mStageBackground);
+    mWindowBackground = Actor::New();
+    mWindowBackground.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
+    mWindowBackground.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
+    mWindowBackground.SetProperty( Actor::Property::SIZE, Vector2( windowSize.x, windowSize.y ) );
+    window.Add(mWindowBackground);
 
     // Push button,  for changing the image set for displaying
     Toolkit::PushButton changeButton = Toolkit::PushButton::New();
-    changeButton.SetLabelText( "Next" );
-    changeButton.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
-    changeButton.SetParentOrigin( ParentOrigin::TOP_RIGHT );
-    stage.Add( changeButton );
+    changeButton.SetProperty( Toolkit::Button::Property::LABEL, "Next" );
+    changeButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_RIGHT );
+    changeButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_RIGHT );
+    window.Add( changeButton );
     changeButton.ClickedSignal().Connect( this, &ImageSvgController::OnChangeButtonClicked );
 
     // Push button, for resetting the actor size and position
     Toolkit::PushButton resetButton = Toolkit::PushButton::New();
-    resetButton.SetLabelText( "Reset" );
-    resetButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
-    resetButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
-    stage.Add( resetButton );
+    resetButton.SetProperty( Toolkit::Button::Property::LABEL, "Reset" );
+    resetButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
+    resetButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
+    window.Add( resetButton );
     resetButton.ClickedSignal().Connect( this, &ImageSvgController::OnResetButtonClicked );
 
-    // Create and put imageViews to stage
-    for( unsigned int i=0; i<4u; i++)
+    // Create and put imageViews to window
+    for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
     {
       mSvgActor[i] = Toolkit::ImageView::New(SVG_IMAGES[mIndex+i]);
-      mSvgActor[i].SetSize( mActorSize );
-      stage.Add( mSvgActor[i] );
+      mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize );
+      mSvgActor[i].TranslateBy( Vector3( 0.0, windowSize.height * 0.05, 0.0f ) );
+      window.Add( mSvgActor[i] );
     }
-    mSvgActor[0].SetParentOrigin( ParentOrigin::CENTER );
-    mSvgActor[0].SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
-    mSvgActor[1].SetParentOrigin( ParentOrigin::CENTER );
-    mSvgActor[1].SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
-    mSvgActor[2].SetParentOrigin( ParentOrigin::CENTER );
-    mSvgActor[2].SetAnchorPoint( AnchorPoint::TOP_RIGHT );
-    mSvgActor[3].SetParentOrigin( ParentOrigin::CENTER );
-    mSvgActor[3].SetAnchorPoint( AnchorPoint::TOP_LEFT );
+    mSvgActor[0].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
+    mSvgActor[0].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_RIGHT );
+    mSvgActor[1].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
+    mSvgActor[1].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_LEFT );
+    mSvgActor[2].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
+    mSvgActor[2].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_RIGHT );
+    mSvgActor[3].SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
+    mSvgActor[3].SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
 
     // Connect pan gesture for moving the actors
     mPanGestureDetector = PanGestureDetector::New();
     mPanGestureDetector.DetectedSignal().Connect( this, &ImageSvgController::OnPanGesture );
-    mPanGestureDetector.Attach( mStageBackground );
+    mPanGestureDetector.Attach( mWindowBackground );
 
     // Connect pinch gesture for resizing the actors
     mPinchGestureDetector = PinchGestureDetector::New();
-    mPinchGestureDetector.Attach( mStageBackground);
+    mPinchGestureDetector.Attach( mWindowBackground);
     mPinchGestureDetector.DetectedSignal().Connect(this, &ImageSvgController::OnPinch);
+
+    changeButton.RaiseToTop();
+    resetButton.RaiseToTop();
   }
 
   // Callback of push button, for changing image set
   bool OnChangeButtonClicked( Toolkit::Button button )
   {
-    mIndex = (mIndex+4) % NUM_SVG_IMAGES;
-    for( unsigned int i=0; i<4u; i++)
+    mIndex = ( mIndex + NUM_IMAGES_DISPLAYED ) % NUM_SVG_IMAGES;
+    for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
     {
       mSvgActor[i].SetImage(SVG_IMAGES[mIndex+i]);
     }
@@ -135,10 +140,10 @@ public:
   // Callback of push button, for resetting image size and position
   bool OnResetButtonClicked( Toolkit::Button button )
   {
-    for( unsigned int i=0; i<4u; i++)
+    for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED ; i++ )
     {
-      mSvgActor[i].SetSize(mActorSize);
-      mSvgActor[i].SetPosition( Vector3::ZERO );
+      mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize);
+      mSvgActor[i].SetProperty( Actor::Property::POSITION, Vector3::ZERO );
       mScale = 1.f;
     }
 
@@ -150,7 +155,7 @@ public:
   {
     if( gesture.state == Gesture::Continuing )
     {
-      for( unsigned int i=0; i<4u; i++)
+      for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
       {
         mSvgActor[i].TranslateBy(Vector3(gesture.displacement));
       }
@@ -160,18 +165,42 @@ public:
   // Callback of pinch gesture, for resizing the actors
   void OnPinch(Actor actor, const PinchGesture& gesture)
   {
-    if (gesture.state == Gesture::Started)
-    {
-      mScaleAtPinchStart = mScale;
-    }
-    if( gesture.state == Gesture::Finished )
+    switch( gesture.state )
     {
-      mScale = mScaleAtPinchStart * gesture.scale;
-      mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
-      for( unsigned int i=0; i<4u; i++)
+      // Only scale the image when we start or continue pinching
+
+      case Gesture::Started:
+      case Gesture::Continuing:
+      {
+        float scale = std::max( gesture.scale, MIN_SCALE / mScale );
+        scale = std::min( MAX_SCALE / mScale, scale );
+
+        for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
+        {
+          mSvgActor[i].SetProperty( Actor::Property::SCALE, scale );
+        }
+        break;
+      }
+
+      case Gesture::Finished:
       {
-        mSvgActor[i].SetSize( mActorSize * mScale);
+        // Resize the image when pinching is complete, this will rasterize the SVG to the new size
+
+        mScale = mScale * gesture.scale;
+        mScale = mScale > MAX_SCALE ? MAX_SCALE : mScale;
+        mScale = mScale < MIN_SCALE ? MIN_SCALE : mScale;
+        for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
+        {
+          mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize * mScale );
+          mSvgActor[i].SetProperty( Actor::Property::SCALE, 1.0f );
+        }
+        break;
       }
+
+      case Gesture::Cancelled:
+      case Gesture::Clear:
+      case Gesture::Possible:
+        break;
     }
   }
 
@@ -180,7 +209,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) )
        {
@@ -188,13 +217,16 @@ public:
        }
        else
        {
-         const char* keyName = event.keyPressedName.c_str();
+         const char* keyName = event.GetKeyName().c_str();
          if( strcmp(keyName, "Left") == 0 )
          {
-           mScale /= 1.1f;
-           for( unsigned int i=0; i<4u; i++)
+           if( mScale > MIN_SCALE )
+           {
+             mScale /= 1.1f;
+           }
+           for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
            {
-             mSvgActor[i].SetSize( mActorSize * mScale);
+             mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize * mScale );
            }
          }
          else if( strcmp(keyName, "Right") == 0 )
@@ -203,9 +235,9 @@ public:
            {
              mScale *= 1.1f;
            }
-           for( unsigned int i=0; i<4u; i++)
+           for( unsigned int i = 0; i < NUM_IMAGES_DISPLAYED; i++ )
            {
-             mSvgActor[i].SetSize( mActorSize * mScale);
+             mSvgActor[i].SetProperty( Actor::Property::SIZE, mActorSize * mScale );
            }
          }
        }
@@ -214,31 +246,20 @@ public:
 
 private:
   Application&         mApplication;
-  Actor                mStageBackground;
+  Actor                mWindowBackground;
   PanGestureDetector   mPanGestureDetector;
   PinchGestureDetector mPinchGestureDetector;
 
   Toolkit::ImageView  mSvgActor[4];
   Vector2             mActorSize;
   float               mScale;
-  float               mScaleAtPinchStart;
   unsigned int        mIndex;
 };
 
-void RunTest( Application& application )
-{
-  ImageSvgController 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 );
-
-  RunTest( application );
-
+  ImageSvgController test( application );
+  application.MainLoop();
   return 0;
 }