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