DirectRendering demo
[platform/core/uifw/dali-demo.git] / examples / bezier-curve / bezier-curve-example.cpp
index b8765c3..d2c285d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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.
@@ -18,6 +18,8 @@
 #include <dali-toolkit/dali-toolkit.h>
 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
 #include <dali/dali.h>
+#include "generated/bezier-curve-frag.h"
+#include "generated/bezier-curve-vert.h"
 #include "shared/view.h"
 
 #include <sstream>
@@ -42,28 +44,6 @@ const float       ANIM_RIGHT_FACTOR(0.8f);
 const int         AXIS_LABEL_POINT_SIZE(7);
 const float       AXIS_LINE_SIZE(1.0f);
 
-// clang-format off
-const char* CURVE_VERTEX_SHADER = DALI_COMPOSE_SHADER
-  (
-    attribute mediump vec2 aPosition;
-    uniform mediump mat4 uMvpMatrix;
-    uniform vec3 uSize;
-    void main()
-    {
-      gl_Position = uMvpMatrix * vec4(aPosition*uSize.xy, 0.0, 1.0);
-    }
-   );
-
-const char* CURVE_FRAGMENT_SHADER = DALI_COMPOSE_SHADER
-  (
-    uniform lowp vec4 uColor;
-    void main()
-    {
-      gl_FragColor = vec4(0.0,0.0,0.0,1.0);
-    }
-   );
-// clang-format on
-
 inline float Clamp(float v, float min, float max)
 {
   if(v < min) return min;
@@ -73,14 +53,6 @@ inline float Clamp(float v, float min, float max)
 
 struct HandlePositionConstraint
 {
-  HandlePositionConstraint(float minRelX, float maxRelX, float minRelY, float maxRelY)
-  : minRelX(minRelX),
-    maxRelX(maxRelX),
-    minRelY(minRelY),
-    maxRelY(maxRelY)
-  {
-  }
-
   void operator()(Vector3& current, const PropertyInputContainer& inputs)
   {
     Vector3 size(inputs[0]->GetVector3());
@@ -88,10 +60,10 @@ struct HandlePositionConstraint
     current.y = Clamp(current.y, minRelY * size.y, maxRelY * size.y);
   }
 
-  float minRelX;
-  float maxRelX;
-  float minRelY;
-  float maxRelY;
+  float minRelX{0.0f};
+  float maxRelX{0.0f};
+  float minRelY{0.0f};
+  float maxRelY{0.0f};
 };
 
 void AnimatingPositionConstraint(Vector3& current, const PropertyInputContainer& inputs)
@@ -151,6 +123,22 @@ public:
   {
     Window window = mApplication.GetWindow();
     window.KeyEventSignal().Connect(this, &BezierCurveExample::OnKeyEvent);
+    const Vector2 windowSize = window.GetSize();
+    const bool orientationPortrait = windowSize.width < windowSize.height;
+
+    unsigned int tableViewRows = 5;
+    unsigned int tableViewColumns = 1;
+    unsigned int rowPositionAdder = 1;
+    TableView::CellPosition gridPosition{1,0};
+
+    // Change layout if we're in landscape mode
+    if(!orientationPortrait)
+    {
+      tableViewRows = 4;
+      tableViewColumns = 2;
+      rowPositionAdder = 0;
+      gridPosition = {0,1,4,1};
+    }
 
     CreateBackground(window);
 
@@ -163,8 +151,7 @@ public:
     mContentLayer.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
     window.Add(mContentLayer);
 
-    // 6 rows: title, grid, coords, play, anim1, anim2
-    TableView contentLayout = TableView::New(5, 1);
+    TableView contentLayout = TableView::New(tableViewRows, tableViewColumns);
     contentLayout.SetProperty(Dali::Actor::Property::NAME, "contentLayout");
     contentLayout.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
     contentLayout.SetCellPadding(Size(30, 30));
@@ -189,7 +176,7 @@ public:
     mGrid.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
     mGrid.SetBackgroundColor(GRID_BACKGROUND_COLOR);
 
-    contentLayout.Add(mGrid);
+    contentLayout.AddChild(mGrid, gridPosition);
     contentLayout.SetCellAlignment(1, HorizontalAlignment::CENTER, VerticalAlignment::CENTER);
     CreateCubic(mGrid);
     CreateControlPoints(mGrid); // Control points constrained to double height of grid
@@ -203,7 +190,7 @@ public:
 
     contentLayout.Add(mCoefficientLabel);
     SetLabel(Vector2(0, 0), Vector2(1, 1));
-    contentLayout.SetCellAlignment(2, HorizontalAlignment::CENTER, VerticalAlignment::CENTER);
+    contentLayout.SetCellAlignment(1 + rowPositionAdder, HorizontalAlignment::CENTER, VerticalAlignment::CENTER);
     contentLayout.SetFitHeight(2);
 
     // Setup Play button and 2 icons to show off current anim and linear anim
@@ -215,8 +202,8 @@ public:
     play.ClickedSignal().Connect(this, &BezierCurveExample::OnPlayClicked);
 
     contentLayout.Add(play);
-    contentLayout.SetCellAlignment(3, HorizontalAlignment::CENTER, VerticalAlignment::CENTER);
-    contentLayout.SetFitHeight(3);
+    contentLayout.SetCellAlignment(2 + rowPositionAdder, HorizontalAlignment::CENTER, VerticalAlignment::CENTER);
+    contentLayout.SetFitHeight(2 + rowPositionAdder);
 
     auto animContainer = Control::New();
     animContainer.SetProperty(Dali::Actor::Property::NAME, "AnimationContainer");
@@ -231,7 +218,7 @@ public:
     animContainer.Add(animRail);
 
     contentLayout.Add(animContainer);
-    contentLayout.SetFixedHeight(4, 150);
+    contentLayout.SetFixedHeight(3 + rowPositionAdder, 150);
 
     mAnimIcon1 = ImageView::New(CIRCLE1_IMAGE);
     mAnimIcon1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
@@ -294,7 +281,7 @@ public:
     mCurve.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
     mCurve.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
 
-    Shader shader = Shader::New(CURVE_VERTEX_SHADER, CURVE_FRAGMENT_SHADER);
+    Shader shader = Shader::New(SHADER_BEZIER_CURVE_VERT, SHADER_BEZIER_CURVE_FRAG);
 
     Property::Map curveVertexFormat;
     curveVertexFormat["aPosition"] = Property::VECTOR2;
@@ -324,7 +311,7 @@ public:
     positionAnimation.FinishedSignal().Connect(this, &BezierCurveExample::OnAnimationFinished);
 
     // Set up constraints for drag/drop
-    Constraint constraint = Constraint::New<Vector3>(actor, Actor::Property::POSITION, HandlePositionConstraint(-0.5, 0.5, -1, 1));
+    Constraint constraint = Constraint::New<Vector3>(actor, Actor::Property::POSITION, HandlePositionConstraint{-0.5, 0.5, -1, 1});
     constraint.AddSource(Source(parent, Actor::Property::SIZE));
     constraint.Apply();
 
@@ -338,7 +325,7 @@ public:
     line.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
     line.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
 
-    Shader   shader   = Shader::New(CURVE_VERTEX_SHADER, CURVE_FRAGMENT_SHADER);
+    Shader   shader   = Shader::New(SHADER_BEZIER_CURVE_VERT, SHADER_BEZIER_CURVE_FRAG);
     Geometry geometry = Geometry::New();
     geometry.AddVertexBuffer(vertexBuffer);
     geometry.SetType(Geometry::LINE_STRIP);