Revert "Revert "Changes after TouchData renamed to TouchEvent""
[platform/core/uifw/dali-demo.git] / examples / renderer-stencil / renderer-stencil-example.cpp
index 994f551..e9004db 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.
@@ -17,7 +17,6 @@
 
 // EXTERNAL INCLUDES
 #include <dali-toolkit/dali-toolkit.h>
-#include <dali/devel-api/adaptor-framework/bitmap-loader.h>
 
 // INTERNAL INCLUDES
 #include "renderer-stencil-shaders.h"
@@ -33,14 +32,13 @@ namespace
 
 // Application constants:
 const char * const APPLICATION_TITLE( "Renderer Stencil API Demo" );
-const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
 const char * const BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
 
 // Texture filenames:
 const char * const CUBE_TEXTURE( DEMO_IMAGE_DIR "people-medium-1.jpg" );
 const char * const FLOOR_TEXTURE( DEMO_IMAGE_DIR "wood.png" );
 
-// Scale dimensions: These values are relative to the stage size. EG. width = 0.32f * stageSize.
+// Scale dimensions: These values are relative to the window size. EG. width = 0.32f * windowSize.
 const float   CUBE_WIDTH_SCALE( 0.32f );                   ///< The width (and height + depth) of the main and reflection cubes.
 const Vector2 FLOOR_DIMENSION_SCALE( 0.67f, 0.017f );      ///< The width and height of the floor object.
 
@@ -52,6 +50,7 @@ const float ANIMATION_BOUNCE_DEFORMATION_PERCENT( 20.0f ); ///< Percentage (of t
 const float ANIMATION_BOUNCE_HEIGHT_PERCENT( 40.0f );      ///< Percentage (of the cube's size) to bounce up in to the air by.
 
 // Base colors for the objects:
+const Vector4 TEXT_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );        ///< White.
 const Vector4 CUBE_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );        ///< White.
 const Vector4 FLOOR_COLOR( 1.0f, 1.0f, 1.0f, 1.0f );       ///< White.
 const Vector4 REFLECTION_COLOR( 0.6f, 0.6f, 0.6f, 0.6f );  ///< Note that alpha is not 1.0f, to make the blend more photo-realistic.
@@ -114,27 +113,57 @@ private:
    */
   void Create( Application& application )
   {
-    Stage stage = Stage::GetCurrent();
-
-    // Creates a default view with a default tool-bar.
-    // The view is added to the stage.
-    Toolkit::ToolBar toolBar;
-    Layer toolBarLayer = DemoHelper::CreateView( application, mView, toolBar, BACKGROUND_IMAGE, TOOLBAR_IMAGE, APPLICATION_TITLE );
-    stage.Add( toolBarLayer );
+    Window window = application.GetWindow();
+
+    // Use a gradient visual to render the background gradient.
+    Toolkit::Control background = Dali::Toolkit::Control::New();
+    background.SetProperty( Actor::Property::ANCHOR_POINT, Dali::AnchorPoint::CENTER );
+    background.SetProperty( Actor::Property::PARENT_ORIGIN, Dali::ParentOrigin::CENTER );
+    background.SetResizePolicy( Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::ALL_DIMENSIONS );
+
+    // Set up the background gradient.
+    Property::Array stopOffsets;
+    stopOffsets.PushBack( 0.0f );
+    stopOffsets.PushBack( 1.0f );
+    Property::Array stopColors;
+    stopColors.PushBack( Vector4( 0.17f, 0.24f, 0.35f, 1.0f ) ); // Dark, medium saturated blue  ( top   of screen)
+    stopColors.PushBack( Vector4( 0.45f, 0.70f, 0.80f, 1.0f ) ); // Medium bright, pastel blue   (bottom of screen)
+    const float percentageWindowHeight = window.GetSize().GetHeight() * 0.7f;
+
+    background.SetProperty( Toolkit::Control::Property::BACKGROUND, Dali::Property::Map()
+      .Add( Toolkit::Visual::Property::TYPE, Dali::Toolkit::Visual::GRADIENT )
+      .Add( Toolkit::GradientVisual::Property::STOP_OFFSET, stopOffsets )
+      .Add( Toolkit::GradientVisual::Property::STOP_COLOR, stopColors )
+      .Add( Toolkit::GradientVisual::Property::START_POSITION, Vector2( 0.0f, -percentageWindowHeight ) )
+      .Add( Toolkit::GradientVisual::Property::END_POSITION, Vector2( 0.0f, percentageWindowHeight ) )
+      .Add( Toolkit::GradientVisual::Property::UNITS, Toolkit::GradientVisual::Units::USER_SPACE ) );
+
+    window.Add( background );
+
+    // Create a TextLabel for the application title.
+    Toolkit::TextLabel label = Toolkit::TextLabel::New( APPLICATION_TITLE );
+    label.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
+    // Set the parent origin to a small percentage below the top (so the demo will scale for different resolutions).
+    label.SetProperty( Actor::Property::PARENT_ORIGIN, Vector3( 0.5f, 0.03f, 0.5f ) );
+    label.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
+    label.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
+    label.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, TEXT_COLOR );
+    window.Add( label );
 
     // Layer to hold the 3D scene.
     Layer layer = Layer::New();
-    layer.SetAnchorPoint( AnchorPoint::CENTER );
+    layer.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
     // Set the parent origin to a small percentage below the center (so the demo will scale for different resolutions).
-    layer.SetParentOrigin( Vector3( 0.5f, 0.58f, 0.5f ) );
-    layer.SetBehavior( Layer::LAYER_2D );
-    layer.SetDepthTestDisabled( false );
-    stage.Add( layer );
+    layer.SetProperty( Actor::Property::PARENT_ORIGIN, Vector3( 0.5f, 0.58f, 0.5f ) );
+    layer.SetProperty( Layer::Property::BEHAVIOR, Layer::LAYER_UI );
+    layer.SetProperty( Layer::Property::DEPTH_TEST, true );
+    window.Add( layer );
 
     // Main cube:
     // Make the demo scalable with different resolutions by basing
-    // the cube size on a percentage of the stage size.
-    float scaleSize( std::min( stage.GetSize().width, stage.GetSize().height ) );
+    // the cube size on a percentage of the window size.
+    Vector2 windowSize = window.GetSize();
+    float scaleSize( std::min( windowSize.width, windowSize.height ) );
     float cubeWidth( scaleSize * CUBE_WIDTH_SCALE );
     Vector3 cubeSize( cubeWidth, cubeWidth, cubeWidth );
     // Create the geometry for the cube, and the texture.
@@ -225,10 +254,10 @@ private:
     mRotationAnimation.Play();
     mBounceAnimation.Play();
 
-    // Respond to a click anywhere on the stage
-    stage.GetRootLayer().TouchSignal().Connect( this, &RendererStencilExample::OnTouch );
+    // Respond to a click anywhere on the window
+    window.GetRootLayer().TouchSignal().Connect( this, &RendererStencilExample::OnTouch );
     // Connect signals to allow Back and Escape to exit.
-    stage.KeyEventSignal().Connect( this, &RendererStencilExample::OnKeyEvent );
+    window.KeyEventSignal().Connect( this, &RendererStencilExample::OnKeyEvent );
   }
 
 private:
@@ -247,9 +276,9 @@ private:
   Actor CreateMainCubeObject( Geometry& geometry, Vector3 size, TextureSet& textureSet )
   {
     Toolkit::Control container = Toolkit::Control::New();
-    container.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
-    container.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
-    container.SetSize( size );
+    container.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER );
+    container.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER );
+    container.SetProperty( Actor::Property::SIZE, Vector2( size ) );
     container.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
 
     // Create a renderer from the geometry and add the texture.
@@ -257,13 +286,10 @@ private:
     renderer.SetTextures( textureSet );
 
     // Setup the renderer properties:
-    // We are writing to the color buffer & culling back faces.
-    renderer.SetProperty( Renderer::Property::WRITE_TO_COLOR_BUFFER, true );
+    // We are writing to the color buffer & culling back faces (no stencil is used for the main cube).
+    renderer.SetProperty( Renderer::Property::RENDER_MODE, RenderMode::COLOR );
     renderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
 
-    // No stencil is used for the main cube.
-    renderer.SetProperty( Renderer::Property::STENCIL_MODE, StencilMode::OFF );
-
     // We do need to write to the depth buffer as other objects need to appear underneath this cube.
     renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
     // We do not need to test the depth buffer as we are culling the back faces.
@@ -287,9 +313,9 @@ private:
   Actor CreateFloorObject( Geometry& geometry, Vector3 size )
   {
     Toolkit::Control container = Toolkit::Control::New();
-    container.SetAnchorPoint( AnchorPoint::TOP_CENTER );
-    container.SetParentOrigin( ParentOrigin::TOP_CENTER );
-    container.SetSize( size );
+    container.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
+    container.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
+    container.SetProperty( Actor::Property::SIZE, Vector2( size ) );
     container.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
 
     // Create a renderer from the geometry and add the texture.
@@ -298,13 +324,10 @@ private:
     renderer.SetTextures( planeTextureSet );
 
     // Setup the renderer properties:
-    // We are writing to the color buffer & culling back faces (as we are NOT doing depth write).
-    renderer.SetProperty( Renderer::Property::WRITE_TO_COLOR_BUFFER, true );
+    // We are writing to the color buffer & culling back faces as we are NOT doing depth write (no stencil is used for the floor).
+    renderer.SetProperty( Renderer::Property::RENDER_MODE, RenderMode::COLOR );
     renderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
 
-    // No stencil is used for the floor.
-    renderer.SetProperty( Renderer::Property::STENCIL_MODE, StencilMode::OFF );
-
     // We do not write to the depth buffer as its not needed.
     renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::OFF );
     // We do need to test the depth buffer as we need the floor to be underneath the cube.
@@ -328,9 +351,9 @@ private:
   Actor CreateStencilPlaneObject( Vector3 size )
   {
     Toolkit::Control container = Toolkit::Control::New();
-    container.SetAnchorPoint( AnchorPoint::CENTER );
-    container.SetParentOrigin( ParentOrigin::CENTER );
-    container.SetSize( size );
+    container.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
+    container.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
+    container.SetProperty( Actor::Property::SIZE, Vector2( size ) );
     container.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
 
     // We rotate the plane as the geometry is created flat in X & Y. We want it to span X & Z axis.
@@ -342,11 +365,9 @@ private:
     Renderer renderer = CreateRenderer( planeGeometry, size, false, Vector4::ONE );
 
     // Setup the renderer properties:
-    // The stencil plane is only for stencilling, so disable writing to color buffer.
-    renderer.SetProperty( Renderer::Property::WRITE_TO_COLOR_BUFFER, false );
+    // The stencil plane is only for stencilling.
+    renderer.SetProperty( Renderer::Property::RENDER_MODE, RenderMode::STENCIL );
 
-    // Enable stencil. Draw to the stencil buffer (only).
-    renderer.SetProperty( Renderer::Property::STENCIL_MODE, StencilMode::ON );
     renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION, StencilFunction::ALWAYS );
     renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION_REFERENCE, 1 );
     renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION_MASK, 0xFF );
@@ -379,9 +400,9 @@ private:
   Actor CreateReflectionCubeObject( Vector3 size, TextureSet& textureSet )
   {
     Toolkit::Control container = Toolkit::Control::New();
-    container.SetAnchorPoint( AnchorPoint::TOP_CENTER );
-    container.SetParentOrigin( ParentOrigin::TOP_CENTER );
-    container.SetSize( size );
+    container.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER );
+    container.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER );
+    container.SetProperty( Actor::Property::SIZE, Vector2( size ) );
     container.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
 
     // Create the cube geometry of unity size.
@@ -392,8 +413,9 @@ private:
     renderer.SetTextures( textureSet );
 
     // Setup the renderer properties:
-    // Write to color buffer so reflection is visible
-    renderer.SetProperty( Renderer::Property::WRITE_TO_COLOR_BUFFER, true );
+    // Write to color buffer so reflection is visible.
+    // Also enable the stencil buffer, as we will be testing against it to only draw to areas within the stencil.
+    renderer.SetProperty( Renderer::Property::RENDER_MODE, RenderMode::COLOR_STENCIL );
     // We cull to skip drawing the back faces.
     renderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
 
@@ -404,7 +426,6 @@ private:
     renderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ONE );
 
     // Enable stencil. Here we only draw to areas within the stencil.
-    renderer.SetProperty( Renderer::Property::STENCIL_MODE, StencilMode::ON );
     renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION, StencilFunction::EQUAL );
     renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION_REFERENCE, 1 );
     renderer.SetProperty( Renderer::Property::STENCIL_FUNCTION_MASK, 0xff );
@@ -460,7 +481,8 @@ private:
    */
   Renderer CreateRenderer( Geometry geometry, Vector3 dimensions, bool textured, Vector4 color )
   {
-    Stage stage = Stage::GetCurrent();
+    Window window = mApplication.GetWindow();
+    Vector2 windowSize = window.GetSize();
     Shader shader;
 
     if( textured )
@@ -472,9 +494,9 @@ private:
       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
     }
 
-    // Here we modify the light position based on half the stage size as a pre-calculation step.
+    // Here we modify the light position based on half the window size as a pre-calculation step.
     // This avoids the work having to be done in the shader.
-    shader.RegisterProperty( LIGHT_POSITION_UNIFORM_NAME, Vector3( -stage.GetSize().width / 2.0f, -stage.GetSize().width / 2.0f, 1000.0f ) );
+    shader.RegisterProperty( LIGHT_POSITION_UNIFORM_NAME, Vector3( -windowSize.width / 2.0f, -windowSize.width / 2.0f, 1000.0f ) );
     shader.RegisterProperty( COLOR_UNIFORM_NAME, color );
     shader.RegisterProperty( OBJECT_DIMENSIONS_UNIFORM_NAME, dimensions );
 
@@ -696,7 +718,7 @@ private:
    * @param[in] touch The touch information
    * @return True if the event has been handled
    */
-  bool OnTouch( Actor actor, const TouchData& touch )
+  bool OnTouch( Actor actor, const TouchEvent& touch )
   {
     // Quit the application.
     mApplication.Quit();
@@ -730,29 +752,10 @@ private:
   Actor            mCubes[2];          ///< The cube object containers
 };
 
-
-/**
- * @brief Creates an instance of the example object and runs it.
- * @param[in] application The DALi application object
- */
-void RunExample( Application& application )
-{
-  RendererStencilExample example( application );
-
-  application.MainLoop();
-}
-
-/**
- * @brief Entry point for Linux & Tizen applications
- * @param[in] argc The executables argument count
- * @param[in] argv The executables argument vector
- * @return The executables exit code (0)
- */
 int DALI_EXPORT_API main( int argc, char **argv )
 {
   Application application = Application::New( &argc, &argv );
-
-  RunExample( application );
-
+  RendererStencilExample example( application );
+  application.MainLoop();
   return 0;
 }