Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / pivot / pivot-example.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
20 #include <dali/devel-api/actors/actor-devel.h>
21
22 #include <iostream>
23
24 using namespace Dali;
25 using namespace Dali::Toolkit;
26
27 namespace
28 {
29 const int     TABLE_VIEW_ROWS    = 5;
30 const int     TABLE_VIEW_COLUMNS = 3;
31 const Vector3 TABLE_VIEW_SIZE_MODE_FACTOR(0.6f, 0.6f, 1.0f);
32
33 const Quaternion                     ANIMATION_ROTATION(Degree(360.0f), Vector3::ZAXIS);
34 const AlphaFunction::BuiltinFunction ROTATION_ANIMATION_ALPHA_FUNCTION(AlphaFunction::EASE_IN_OUT);
35 const TimePeriod                     ROTATION_ANIMATION_TIME_PERIOD(0.0f, 0.5f);
36
37 const Vector3                        ANIMATION_SCALE(2.0f, 2.0f, 1.0f);
38 const AlphaFunction::BuiltinFunction SCALE_ANIMATION_ALPHA_FUNCTION(AlphaFunction::SIN);
39 const TimePeriod                     SCALE_ANIMATION_TIME_PERIOD(0.15f, 0.85f);
40 } // unnamed namespace
41
42 /**
43  * @brief Demonstrates how to animate a control in a layout-container using the anchor point as the pivot.
44  *
45  * Positions of the controls in the layout-container will be set using the top-left of the control.
46  */
47 class PivotController : public ConnectionTracker
48 {
49 public:
50   /**
51    * @brief Constructor.
52    * @param[in] application Reference to the application instance.
53    */
54   PivotController(Application& application)
55   : mApplication(application)
56   {
57     mApplication.InitSignal().Connect(this, &PivotController::Create);
58   }
59
60 private:
61   /**
62    * @brief The Init signal is received once (only) during the Application lifetime.
63    */
64   void Create(Application& application)
65   {
66     // Get a handle to the window
67     Window window = application.GetWindow();
68     window.SetBackgroundColor(Color::WHITE);
69     window.KeyEventSignal().Connect(this, &PivotController::OnKeyEvent);
70
71     // Create a table view.
72     TableView tableView = TableView::New(TABLE_VIEW_ROWS, TABLE_VIEW_COLUMNS);
73     tableView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
74     tableView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
75     tableView.SetResizePolicy(ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS);
76     tableView.SetProperty(Actor::Property::SIZE_MODE_FACTOR, TABLE_VIEW_SIZE_MODE_FACTOR);
77     window.Add(tableView);
78
79     // Create a tap detector - we are going to rotate an actor round our anchor-point (pivot) when one of our controls is tapped.
80     mTapDetector = TapGestureDetector::New();
81     mTapDetector.DetectedSignal().Connect(this, &PivotController::OnTap);
82
83     Vector3 anchorPoint(AnchorPoint::CENTER); // This will be changed depending on the row and column.
84
85     // Add controls to the table-view - each control will have a random color.
86     for(int row = 0; row < TABLE_VIEW_ROWS; ++row)
87     {
88       anchorPoint.y = (row % TABLE_VIEW_ROWS) * (1.0f / (TABLE_VIEW_ROWS - 1)); // Calculate this row's y anchor-point.
89
90       for(int column = 0; column < TABLE_VIEW_COLUMNS; ++column)
91       {
92         anchorPoint.x = (column % TABLE_VIEW_COLUMNS) * (1.0f / (TABLE_VIEW_COLUMNS - 1)); // Calculate this column's x anchor-point.
93
94         // Create a control with a randomly chosen background color.
95         Control control = Control::New();
96         control.SetBackgroundColor(Vector4(Random::Range(0.0f, 1.0f), Random::Range(0.0f, 1.0f), Random::Range(0.0f, 1.0f), 1.0f));
97         control.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
98         control.SetProperty(Actor::Property::POSITION_USES_ANCHOR_POINT, false); // Ensures the position always uses top-left for its calculations.
99         control.SetProperty(Actor::Property::ANCHOR_POINT, anchorPoint);         // This anchor-point is used for the rotation and the scale.
100
101         // Add to the table-view
102         tableView.AddChild(control, TableView::CellPosition(row, column));
103
104         // Attach each control to the tap-detector so we can start the animation around our anchor point.
105         mTapDetector.Attach(control);
106       }
107     }
108   }
109
110   /**
111    * @brief Called when a control is tapped.
112    *
113    * Here we will start an animation around our pivot.
114    */
115   void OnTap(Actor actor, const TapGesture& /* tap */)
116   {
117     // Raise the actor to the top.
118     actor.RaiseToTop();
119
120     // Create the animation to rotate and scale our actor.
121     Animation animation = Animation::New(1.0f);
122     animation.AnimateBy(Property(actor, Actor::Property::ORIENTATION), ANIMATION_ROTATION, ROTATION_ANIMATION_ALPHA_FUNCTION, ROTATION_ANIMATION_TIME_PERIOD);
123     animation.AnimateTo(Property(actor, Actor::Property::SCALE), ANIMATION_SCALE, SCALE_ANIMATION_ALPHA_FUNCTION, SCALE_ANIMATION_TIME_PERIOD);
124
125     // Play the animation.
126     animation.Play();
127   }
128
129   /**
130    * @brief Called when any key event is received
131    *
132    * Will use this to quit the application if Back or the Escape key is received
133    * @param[in] event The key event information
134    */
135   void OnKeyEvent(const KeyEvent& event)
136   {
137     if(event.GetState() == KeyEvent::DOWN)
138     {
139       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
140       {
141         mApplication.Quit();
142       }
143     }
144   }
145
146 private:
147   Application&       mApplication; ///< Reference to the Application instance.
148   TapGestureDetector mTapDetector; ///< Used for animating the tapped control.
149 };
150
151 int DALI_EXPORT_API main(int argc, char** argv)
152 {
153   Application     application = Application::New(&argc, &argv);
154   PivotController test(application);
155   application.MainLoop();
156   return 0;
157 }