inherit-test-example.cpp
11.7 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright (c) 2023 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 <sstream>
using namespace Dali;
using namespace Dali::Toolkit;
namespace
{
constexpr Vector2 PARENT_VIEW_SIZE_LIST[] = {
Vector2(100.0f, 150.0f),
Vector2(200.0f, 100.0f),
Vector2(100.0f, 200.0f),
Vector2(100.0f, 100.0f),
};
constexpr int PARENT_VIEW_SIZE_LIST_COUNT = sizeof(PARENT_VIEW_SIZE_LIST) / sizeof(PARENT_VIEW_SIZE_LIST[0]);
constexpr Vector2 PARENT_VIEW_SCALE_LIST[] = {
Vector2(2.0f, 0.5f),
Vector2(1.0f, 1.0f),
Vector2(0.5f, 2.0f),
};
constexpr int PARENT_VIEW_SCALE_LIST_COUNT = sizeof(PARENT_VIEW_SCALE_LIST) / sizeof(PARENT_VIEW_SCALE_LIST[0]);
constexpr Vector2 CURRENT_VIEW_SIZE_LIST[] = {
Vector2(200.0f, 250.0f),
Vector2(200.0f, 100.0f),
Vector2(100.0f, 200.0f),
Vector2(10.0f, 10.0f),
};
constexpr int CURRENT_VIEW_SIZE_LIST_COUNT = sizeof(CURRENT_VIEW_SIZE_LIST) / sizeof(CURRENT_VIEW_SIZE_LIST[0]);
constexpr Vector2 CURRENT_VIEW_POSITION_LIST[] = {
Vector2(100.0f, 120.0f),
Vector2(0.0f, 0.0f),
Vector2(100.0f, 100.0f),
Vector2(-100.0f, -200.0f),
};
constexpr int CURRENT_VIEW_POSITION_LIST_COUNT = sizeof(CURRENT_VIEW_POSITION_LIST) / sizeof(CURRENT_VIEW_POSITION_LIST[0]);
constexpr Vector2 CURRENT_VIEW_SCALE_LIST[] = {
Vector2(0.5f, 2.0f),
Vector2(1.0f, 1.0f),
Vector2(2.0f, 0.5f),
};
constexpr int CURRENT_VIEW_SCALE_LIST_COUNT = sizeof(CURRENT_VIEW_SCALE_LIST) / sizeof(CURRENT_VIEW_SCALE_LIST[0]);
constexpr Vector3 PARENT_ORIGIN_LIST[] = {
ParentOrigin::TOP_LEFT,
ParentOrigin::TOP_CENTER,
ParentOrigin::TOP_RIGHT,
ParentOrigin::CENTER_LEFT,
ParentOrigin::CENTER,
ParentOrigin::CENTER_RIGHT,
ParentOrigin::BOTTOM_LEFT,
ParentOrigin::BOTTOM_CENTER,
ParentOrigin::BOTTOM_RIGHT,
};
constexpr int PARENT_ORIGIN_LIST_COUNT = sizeof(PARENT_ORIGIN_LIST) / sizeof(PARENT_ORIGIN_LIST[0]);
constexpr Vector3 ANCHOR_POINT_LIST[] = {
AnchorPoint::TOP_LEFT,
AnchorPoint::TOP_CENTER,
AnchorPoint::TOP_RIGHT,
AnchorPoint::CENTER_LEFT,
AnchorPoint::CENTER,
AnchorPoint::CENTER_RIGHT,
AnchorPoint::BOTTOM_LEFT,
AnchorPoint::BOTTOM_CENTER,
AnchorPoint::BOTTOM_RIGHT,
};
constexpr int ANCHOR_POINT_LIST_COUNT = sizeof(ANCHOR_POINT_LIST) / sizeof(ANCHOR_POINT_LIST[0]);
} // namespace
// This example shows how to create and display Hello World! using a simple TextActor
//
class InheritTestController : public ConnectionTracker
{
public:
InheritTestController(Application& application)
: mApplication(application)
{
// Connect to the Application's Init signal
mApplication.InitSignal().Connect(this, &InheritTestController::Create);
}
~InheritTestController() = default; // Nothing to do in destructor
// The Init signal is received once (only) during the Application lifetime
void Create(Application& application)
{
// Get a handle to the window
mWindow = application.GetWindow();
mWindow.SetBackgroundColor(Color::WHITE);
Vector2 windowSize(1280, 800);
if(mWindow.GetSize().GetWidth() < 1280)
{
mWindow.SetSize(Uint16Pair(windowSize.x, windowSize.y));
}
else
{
windowSize.x = mWindow.GetSize().GetWidth();
windowSize.y = mWindow.GetSize().GetHeight();
}
mTitle = TextLabel::New();
mTitle.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
mTitle.SetProperty(TextLabel::Property::MULTI_LINE, true);
mWindow.Add(mTitle);
SetupView();
// Respond to a touch anywhere on the window
mWindow.GetRootLayer().TouchedSignal().Connect(this, &InheritTestController::OnTouch);
// Respond to key events
mWindow.KeyEventSignal().Connect(this, &InheritTestController::OnKeyEvent);
}
bool OnTouch(Actor actor, const TouchEvent& touch)
{
// quit the application
mApplication.Quit();
return true;
}
void OnKeyEvent(const KeyEvent& event)
{
if(event.GetState() == KeyEvent::DOWN)
{
bool needUpdate = false;
if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
{
mApplication.Quit();
}
if(event.GetKeyName() == "1") // Inherit flag
{
mInheritFlag = (mInheritFlag + 1) % 8;
needUpdate = true;
}
else if(event.GetKeyName() == "2") // ParentView Size
{
mParentSizeIndex = (mParentSizeIndex + 1) % PARENT_VIEW_SIZE_LIST_COUNT;
needUpdate = true;
}
else if(event.GetKeyName() == "3") // ParentView Scale
{
mParentScaleIndex = (mParentScaleIndex + 1) % PARENT_VIEW_SCALE_LIST_COUNT;
needUpdate = true;
}
else if(event.GetKeyName() == "4") // ParentOrigin
{
mParentOriginIndex = (mParentOriginIndex + 1) % PARENT_ORIGIN_LIST_COUNT;
needUpdate = true;
}
else if(event.GetKeyName() == "5") // AnchorPoint
{
mAnchorPointIndex = (mAnchorPointIndex + 1) % ANCHOR_POINT_LIST_COUNT;
needUpdate = true;
}
else if(event.GetKeyName() == "6") // Size
{
mCurrentSizeIndex = (mCurrentSizeIndex + 1) % CURRENT_VIEW_SIZE_LIST_COUNT;
needUpdate = true;
}
else if(event.GetKeyName() == "7") // Position
{
mCurrentPositionIndex = (mCurrentPositionIndex + 1) % CURRENT_VIEW_POSITION_LIST_COUNT;
needUpdate = true;
}
else if(event.GetKeyName() == "8") // Scale
{
mCurrentScaleIndex = (mCurrentScaleIndex + 1) % CURRENT_VIEW_SCALE_LIST_COUNT;
needUpdate = true;
}
if(needUpdate)
{
SetupView();
}
}
}
void SetupView()
{
if(mParent)
{
mParent.Unparent();
}
if(mCurrent)
{
mCurrent.Unparent();
}
Vector2 parentViewSize = PARENT_VIEW_SIZE_LIST[mParentSizeIndex];
Vector2 parentViewScale = PARENT_VIEW_SCALE_LIST[mParentScaleIndex];
mParent = Control::New();
mParent.SetBackgroundColor(Color::BLUE);
mParent.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
mParent.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
mParent.SetProperty(Actor::Property::SIZE, Vector3(parentViewSize));
mParent.SetProperty(Actor::Property::POSITION, Vector3::ZERO);
mParent.SetProperty(Actor::Property::SCALE, Vector3(parentViewScale.x, parentViewScale.y, 1.0f));
Vector3 parentOrigin = PARENT_ORIGIN_LIST[mParentOriginIndex];
Vector3 anchorPoint = ANCHOR_POINT_LIST[mAnchorPointIndex];
Vector2 currentViewSize = CURRENT_VIEW_SIZE_LIST[mCurrentSizeIndex];
Vector2 currentViewPosition = CURRENT_VIEW_POSITION_LIST[mCurrentPositionIndex];
Vector2 currentViewScale = CURRENT_VIEW_SCALE_LIST[mCurrentScaleIndex];
bool inheritPosition = mInheritFlag & 1;
bool inheritScale = mInheritFlag & 2;
bool inheritOrientation = mInheritFlag & 4;
mCurrent = Control::New();
mCurrent.SetBackgroundColor(Color::RED);
mCurrent.SetProperty(Actor::Property::PARENT_ORIGIN, parentOrigin);
mCurrent.SetProperty(Actor::Property::ANCHOR_POINT, anchorPoint);
mCurrent.SetProperty(Actor::Property::SIZE, Vector3(currentViewSize));
mCurrent.SetProperty(Actor::Property::POSITION, Vector3(currentViewPosition));
mCurrent.SetProperty(Actor::Property::SCALE, Vector3(currentViewScale.x, currentViewScale.y, 1.0f));
mCurrent.SetProperty(Actor::Property::INHERIT_POSITION, inheritPosition);
mCurrent.SetProperty(Actor::Property::INHERIT_SCALE, inheritScale);
mCurrent.SetProperty(Actor::Property::INHERIT_ORIENTATION, inheritOrientation);
mParent.Add(mCurrent);
mWindow.Add(mParent);
mAnimation = Animation::New(5.0f);
mAnimation.AnimateBy(Property(mParent, Actor::Property::ORIENTATION), Quaternion(Radian(Degree(360.0f)), Vector3::ZAXIS));
mAnimation.AnimateBy(Property(mCurrent, Actor::Property::ORIENTATION), Quaternion(Radian(Degree(360.0f)), Vector3::ZAXIS));
mAnimation.SetLooping(true);
mAnimation.PlayAfter(1.0f); // Play animation after world value printed
mTimer = Timer::New(32);
mTimer.TickSignal().Connect(this, &InheritTestController::OnTickLableUpdate);
mTimer.Start();
}
bool OnTickLableUpdate()
{
UpdateLabelInfo();
return false;
}
void UpdateLabelInfo()
{
std::ostringstream oss;
Vector2 parentViewSize = PARENT_VIEW_SIZE_LIST[mParentSizeIndex];
Vector2 parentViewScale = PARENT_VIEW_SCALE_LIST[mParentScaleIndex];
Vector3 parentOrigin = PARENT_ORIGIN_LIST[mParentOriginIndex];
Vector3 anchorPoint = ANCHOR_POINT_LIST[mAnchorPointIndex];
Vector2 currentViewSize = CURRENT_VIEW_SIZE_LIST[mCurrentSizeIndex];
Vector2 currentViewPosition = CURRENT_VIEW_POSITION_LIST[mCurrentPositionIndex];
Vector2 currentViewScale = CURRENT_VIEW_SCALE_LIST[mCurrentScaleIndex];
Vector2 screenPositon = mCurrent.GetProperty<Vector2>(Actor::Property::SCREEN_POSITION);
Vector3 worldPosition = mCurrent.GetProperty<Vector3>(Actor::Property::WORLD_POSITION);
Vector3 worldScale = mCurrent.GetProperty<Vector3>(Actor::Property::WORLD_SCALE);
Quaternion worldOrientation = mCurrent.GetProperty<Quaternion>(Actor::Property::WORLD_ORIENTATION);
bool inheritPosition = mInheritFlag & 1;
bool inheritScale = mInheritFlag & 2;
bool inheritOrientation = mInheritFlag & 4;
oss << "Inherit P S O : " << inheritPosition << " " << inheritScale << " " << inheritOrientation << "\n";
oss << "screenPosition : " << screenPositon << "\n";
oss << "\n";
oss << "parentViewSize : " << parentViewSize << "\n";
oss << "parentViewScale : " << parentViewScale << "\n";
oss << "\n";
oss << "parentOrigin : " << parentOrigin << "\n";
oss << "anchorPoint : " << anchorPoint << "\n";
oss << "currentViewSize : " << currentViewSize << "\n";
oss << "currentViewPosition : " << currentViewPosition << "\n";
oss << "currentViewScale : " << currentViewScale << "\n";
oss << "\n";
oss << "worldPosition : " << worldPosition << "\n";
oss << "worldScale : " << worldScale << "\n";
oss << "worldOrientation : " << worldOrientation << "\n";
oss << "\n";
oss << "Key 1 : Change flag\n";
oss << "Key 2 : Change ParentView's Size\n";
oss << "Key 3 : Change ParentView's Scale\n";
oss << "Key 4 : Change CurrentView's ParentOrigin\n";
oss << "Key 5 : Change CurrentView's AnchorPoint\n";
oss << "Key 6 : Change CurrentView's Size\n";
oss << "Key 7 : Change CurrentView's Position\n";
oss << "Key 8 : Change CurrentView's Scale\n";
mTitle.SetProperty(TextLabel::Property::TEXT, oss.str());
}
private:
Application& mApplication;
Window mWindow;
TextLabel mTitle;
Control mParent;
Control mCurrent;
Timer mTimer;
Animation mAnimation;
int mInheritFlag = 0;
int mParentSizeIndex = 0;
int mParentScaleIndex = 0;
int mCurrentSizeIndex = 0;
int mCurrentPositionIndex = 0;
int mCurrentScaleIndex = 0;
int mParentOriginIndex = 0;
int mAnchorPointIndex = 0;
};
int DALI_EXPORT_API main(int argc, char** argv)
{
Application application = Application::New(&argc, &argv);
InheritTestController test(application);
application.MainLoop();
return 0;
}