Fix demo app s.t. don't use z=0 scale
[platform/core/uifw/dali-demo.git] / examples / inherit-test / inherit-test-example.cpp
1 /*
2  * Copyright (c) 2023 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 <sstream>
20
21 using namespace Dali;
22 using namespace Dali::Toolkit;
23
24 namespace
25 {
26 constexpr Vector2 PARENT_VIEW_SIZE_LIST[] = {
27   Vector2(100.0f, 150.0f),
28   Vector2(200.0f, 100.0f),
29   Vector2(100.0f, 200.0f),
30   Vector2(100.0f, 100.0f),
31 };
32 constexpr int PARENT_VIEW_SIZE_LIST_COUNT = sizeof(PARENT_VIEW_SIZE_LIST) / sizeof(PARENT_VIEW_SIZE_LIST[0]);
33
34 constexpr Vector2 PARENT_VIEW_SCALE_LIST[] = {
35   Vector2(2.0f, 0.5f),
36   Vector2(1.0f, 1.0f),
37   Vector2(0.5f, 2.0f),
38 };
39 constexpr int PARENT_VIEW_SCALE_LIST_COUNT = sizeof(PARENT_VIEW_SCALE_LIST) / sizeof(PARENT_VIEW_SCALE_LIST[0]);
40
41 constexpr Vector2 CURRENT_VIEW_SIZE_LIST[] = {
42   Vector2(200.0f, 250.0f),
43   Vector2(200.0f, 100.0f),
44   Vector2(100.0f, 200.0f),
45   Vector2(10.0f, 10.0f),
46 };
47 constexpr int CURRENT_VIEW_SIZE_LIST_COUNT = sizeof(CURRENT_VIEW_SIZE_LIST) / sizeof(CURRENT_VIEW_SIZE_LIST[0]);
48
49 constexpr Vector2 CURRENT_VIEW_POSITION_LIST[] = {
50   Vector2(100.0f, 120.0f),
51   Vector2(0.0f, 0.0f),
52   Vector2(100.0f, 100.0f),
53   Vector2(-100.0f, -200.0f),
54 };
55 constexpr int CURRENT_VIEW_POSITION_LIST_COUNT = sizeof(CURRENT_VIEW_POSITION_LIST) / sizeof(CURRENT_VIEW_POSITION_LIST[0]);
56
57 constexpr Vector2 CURRENT_VIEW_SCALE_LIST[] = {
58   Vector2(0.5f, 2.0f),
59   Vector2(1.0f, 1.0f),
60   Vector2(2.0f, 0.5f),
61 };
62 constexpr int CURRENT_VIEW_SCALE_LIST_COUNT = sizeof(CURRENT_VIEW_SCALE_LIST) / sizeof(CURRENT_VIEW_SCALE_LIST[0]);
63
64 constexpr Vector3 PARENT_ORIGIN_LIST[] = {
65   ParentOrigin::TOP_LEFT,
66   ParentOrigin::TOP_CENTER,
67   ParentOrigin::TOP_RIGHT,
68   ParentOrigin::CENTER_LEFT,
69   ParentOrigin::CENTER,
70   ParentOrigin::CENTER_RIGHT,
71   ParentOrigin::BOTTOM_LEFT,
72   ParentOrigin::BOTTOM_CENTER,
73   ParentOrigin::BOTTOM_RIGHT,
74 };
75 constexpr int PARENT_ORIGIN_LIST_COUNT = sizeof(PARENT_ORIGIN_LIST) / sizeof(PARENT_ORIGIN_LIST[0]);
76
77 constexpr Vector3 ANCHOR_POINT_LIST[] = {
78   AnchorPoint::TOP_LEFT,
79   AnchorPoint::TOP_CENTER,
80   AnchorPoint::TOP_RIGHT,
81   AnchorPoint::CENTER_LEFT,
82   AnchorPoint::CENTER,
83   AnchorPoint::CENTER_RIGHT,
84   AnchorPoint::BOTTOM_LEFT,
85   AnchorPoint::BOTTOM_CENTER,
86   AnchorPoint::BOTTOM_RIGHT,
87 };
88 constexpr int ANCHOR_POINT_LIST_COUNT = sizeof(ANCHOR_POINT_LIST) / sizeof(ANCHOR_POINT_LIST[0]);
89 } // namespace
90
91 // This example shows how to create and display Hello World! using a simple TextActor
92 //
93 class InheritTestController : public ConnectionTracker
94 {
95 public:
96   InheritTestController(Application& application)
97   : mApplication(application)
98   {
99     // Connect to the Application's Init signal
100     mApplication.InitSignal().Connect(this, &InheritTestController::Create);
101   }
102
103   ~InheritTestController() = default; // Nothing to do in destructor
104
105   // The Init signal is received once (only) during the Application lifetime
106   void Create(Application& application)
107   {
108     // Get a handle to the window
109     mWindow = application.GetWindow();
110     mWindow.SetBackgroundColor(Color::WHITE);
111
112     Vector2 windowSize(1280, 800);
113     if(mWindow.GetSize().GetWidth() < 1280)
114     {
115       mWindow.SetSize(Uint16Pair(windowSize.x, windowSize.y));
116     }
117     else
118     {
119       windowSize.x = mWindow.GetSize().GetWidth();
120       windowSize.y = mWindow.GetSize().GetHeight();
121     }
122
123     mTitle = TextLabel::New();
124     mTitle.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
125     mTitle.SetProperty(TextLabel::Property::MULTI_LINE, true);
126     mWindow.Add(mTitle);
127
128     SetupView();
129
130     // Respond to a touch anywhere on the window
131     mWindow.GetRootLayer().TouchedSignal().Connect(this, &InheritTestController::OnTouch);
132
133     // Respond to key events
134     mWindow.KeyEventSignal().Connect(this, &InheritTestController::OnKeyEvent);
135   }
136
137   bool OnTouch(Actor actor, const TouchEvent& touch)
138   {
139     // quit the application
140     mApplication.Quit();
141     return true;
142   }
143
144   void OnKeyEvent(const KeyEvent& event)
145   {
146     if(event.GetState() == KeyEvent::DOWN)
147     {
148       bool needUpdate = false;
149       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
150       {
151         mApplication.Quit();
152       }
153       if(event.GetKeyName() == "1") // Inherit flag
154       {
155         mInheritFlag = (mInheritFlag + 1) % 8;
156         needUpdate   = true;
157       }
158       else if(event.GetKeyName() == "2") // ParentView Size
159       {
160         mParentSizeIndex = (mParentSizeIndex + 1) % PARENT_VIEW_SIZE_LIST_COUNT;
161         needUpdate       = true;
162       }
163       else if(event.GetKeyName() == "3") // ParentView Scale
164       {
165         mParentScaleIndex = (mParentScaleIndex + 1) % PARENT_VIEW_SCALE_LIST_COUNT;
166         needUpdate        = true;
167       }
168       else if(event.GetKeyName() == "4") // ParentOrigin
169       {
170         mParentOriginIndex = (mParentOriginIndex + 1) % PARENT_ORIGIN_LIST_COUNT;
171         needUpdate         = true;
172       }
173       else if(event.GetKeyName() == "5") // AnchorPoint
174       {
175         mAnchorPointIndex = (mAnchorPointIndex + 1) % ANCHOR_POINT_LIST_COUNT;
176         needUpdate        = true;
177       }
178       else if(event.GetKeyName() == "6") // Size
179       {
180         mCurrentSizeIndex = (mCurrentSizeIndex + 1) % CURRENT_VIEW_SIZE_LIST_COUNT;
181         needUpdate        = true;
182       }
183       else if(event.GetKeyName() == "7") // Position
184       {
185         mCurrentPositionIndex = (mCurrentPositionIndex + 1) % CURRENT_VIEW_POSITION_LIST_COUNT;
186         needUpdate            = true;
187       }
188       else if(event.GetKeyName() == "8") // Scale
189       {
190         mCurrentScaleIndex = (mCurrentScaleIndex + 1) % CURRENT_VIEW_SCALE_LIST_COUNT;
191         needUpdate         = true;
192       }
193
194       if(needUpdate)
195       {
196         SetupView();
197       }
198     }
199   }
200
201   void SetupView()
202   {
203     if(mParent)
204     {
205       mParent.Unparent();
206     }
207     if(mCurrent)
208     {
209       mCurrent.Unparent();
210     }
211
212     Vector2 parentViewSize  = PARENT_VIEW_SIZE_LIST[mParentSizeIndex];
213     Vector2 parentViewScale = PARENT_VIEW_SCALE_LIST[mParentScaleIndex];
214
215     mParent = Control::New();
216     mParent.SetBackgroundColor(Color::BLUE);
217     mParent.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
218     mParent.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
219     mParent.SetProperty(Actor::Property::SIZE, Vector3(parentViewSize));
220     mParent.SetProperty(Actor::Property::POSITION, Vector3::ZERO);
221     mParent.SetProperty(Actor::Property::SCALE, Vector3(parentViewScale.x, parentViewScale.y, 1.0f));
222
223     Vector3 parentOrigin        = PARENT_ORIGIN_LIST[mParentOriginIndex];
224     Vector3 anchorPoint         = ANCHOR_POINT_LIST[mAnchorPointIndex];
225     Vector2 currentViewSize     = CURRENT_VIEW_SIZE_LIST[mCurrentSizeIndex];
226     Vector2 currentViewPosition = CURRENT_VIEW_POSITION_LIST[mCurrentPositionIndex];
227     Vector2 currentViewScale    = CURRENT_VIEW_SCALE_LIST[mCurrentScaleIndex];
228
229     bool inheritPosition    = mInheritFlag & 1;
230     bool inheritScale       = mInheritFlag & 2;
231     bool inheritOrientation = mInheritFlag & 4;
232
233     mCurrent = Control::New();
234     mCurrent.SetBackgroundColor(Color::RED);
235     mCurrent.SetProperty(Actor::Property::PARENT_ORIGIN, parentOrigin);
236     mCurrent.SetProperty(Actor::Property::ANCHOR_POINT, anchorPoint);
237     mCurrent.SetProperty(Actor::Property::SIZE, Vector3(currentViewSize));
238     mCurrent.SetProperty(Actor::Property::POSITION, Vector3(currentViewPosition));
239     mCurrent.SetProperty(Actor::Property::SCALE, Vector3(currentViewScale.x, currentViewScale.y, 1.0f));
240
241     mCurrent.SetProperty(Actor::Property::INHERIT_POSITION, inheritPosition);
242     mCurrent.SetProperty(Actor::Property::INHERIT_SCALE, inheritScale);
243     mCurrent.SetProperty(Actor::Property::INHERIT_ORIENTATION, inheritOrientation);
244
245     mParent.Add(mCurrent);
246     mWindow.Add(mParent);
247
248     mAnimation = Animation::New(5.0f);
249     mAnimation.AnimateBy(Property(mParent, Actor::Property::ORIENTATION), Quaternion(Radian(Degree(360.0f)), Vector3::ZAXIS));
250     mAnimation.AnimateBy(Property(mCurrent, Actor::Property::ORIENTATION), Quaternion(Radian(Degree(360.0f)), Vector3::ZAXIS));
251     mAnimation.SetLooping(true);
252     mAnimation.PlayAfter(1.0f); // Play animation after world value printed
253
254     mTimer = Timer::New(32);
255     mTimer.TickSignal().Connect(this, &InheritTestController::OnTickLableUpdate);
256     mTimer.Start();
257   }
258
259   bool OnTickLableUpdate()
260   {
261     UpdateLabelInfo();
262     return false;
263   }
264
265   void UpdateLabelInfo()
266   {
267     std::ostringstream oss;
268
269     Vector2 parentViewSize  = PARENT_VIEW_SIZE_LIST[mParentSizeIndex];
270     Vector2 parentViewScale = PARENT_VIEW_SCALE_LIST[mParentScaleIndex];
271
272     Vector3 parentOrigin        = PARENT_ORIGIN_LIST[mParentOriginIndex];
273     Vector3 anchorPoint         = ANCHOR_POINT_LIST[mAnchorPointIndex];
274     Vector2 currentViewSize     = CURRENT_VIEW_SIZE_LIST[mCurrentSizeIndex];
275     Vector2 currentViewPosition = CURRENT_VIEW_POSITION_LIST[mCurrentPositionIndex];
276     Vector2 currentViewScale    = CURRENT_VIEW_SCALE_LIST[mCurrentScaleIndex];
277
278     Vector2    screenPositon    = mCurrent.GetProperty<Vector2>(Actor::Property::SCREEN_POSITION);
279     Vector3    worldPosition    = mCurrent.GetProperty<Vector3>(Actor::Property::WORLD_POSITION);
280     Vector3    worldScale       = mCurrent.GetProperty<Vector3>(Actor::Property::WORLD_SCALE);
281     Quaternion worldOrientation = mCurrent.GetProperty<Quaternion>(Actor::Property::WORLD_ORIENTATION);
282
283     bool inheritPosition    = mInheritFlag & 1;
284     bool inheritScale       = mInheritFlag & 2;
285     bool inheritOrientation = mInheritFlag & 4;
286
287     oss << "Inherit P S O : " << inheritPosition << " " << inheritScale << " " << inheritOrientation << "\n";
288     oss << "screenPosition : " << screenPositon << "\n";
289     oss << "\n";
290     oss << "parentViewSize : " << parentViewSize << "\n";
291     oss << "parentViewScale : " << parentViewScale << "\n";
292     oss << "\n";
293     oss << "parentOrigin : " << parentOrigin << "\n";
294     oss << "anchorPoint : " << anchorPoint << "\n";
295     oss << "currentViewSize : " << currentViewSize << "\n";
296     oss << "currentViewPosition : " << currentViewPosition << "\n";
297     oss << "currentViewScale : " << currentViewScale << "\n";
298     oss << "\n";
299     oss << "worldPosition : " << worldPosition << "\n";
300     oss << "worldScale : " << worldScale << "\n";
301     oss << "worldOrientation : " << worldOrientation << "\n";
302     oss << "\n";
303     oss << "Key 1 : Change flag\n";
304     oss << "Key 2 : Change ParentView's Size\n";
305     oss << "Key 3 : Change ParentView's Scale\n";
306     oss << "Key 4 : Change CurrentView's ParentOrigin\n";
307     oss << "Key 5 : Change CurrentView's AnchorPoint\n";
308     oss << "Key 6 : Change CurrentView's Size\n";
309     oss << "Key 7 : Change CurrentView's Position\n";
310     oss << "Key 8 : Change CurrentView's Scale\n";
311
312     mTitle.SetProperty(TextLabel::Property::TEXT, oss.str());
313   }
314
315 private:
316   Application& mApplication;
317   Window       mWindow;
318   TextLabel    mTitle;
319
320   Control mParent;
321   Control mCurrent;
322
323   Timer     mTimer;
324   Animation mAnimation;
325
326   int mInheritFlag = 0;
327
328   int mParentSizeIndex      = 0;
329   int mParentScaleIndex     = 0;
330   int mCurrentSizeIndex     = 0;
331   int mCurrentPositionIndex = 0;
332   int mCurrentScaleIndex    = 0;
333   int mParentOriginIndex    = 0;
334   int mAnchorPointIndex     = 0;
335 };
336
337 int DALI_EXPORT_API main(int argc, char** argv)
338 {
339   Application           application = Application::New(&argc, &argv);
340   InheritTestController test(application);
341   application.MainLoop();
342   return 0;
343 }