Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / text-overlap / text-overlap-example.cpp
1
2 /*
3  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "text-overlap-example.h"
19 #include <dali-toolkit/dali-toolkit.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 static const int NUMBER_OF_LABELS(2);
28
29 namespace Demo
30 {
31 TextOverlapController::TextOverlapController(Application& app)
32 : mApplication(app),
33   mTopmostLabel(1)
34 {
35   app.InitSignal().Connect(this, &TextOverlapController::Create);
36   app.TerminateSignal().Connect(this, &TextOverlapController::Destroy);
37 }
38
39 void TextOverlapController::Create(Application& app)
40 {
41   Window window = app.GetWindow();
42   window.KeyEventSignal().Connect(this, &TextOverlapController::OnKeyEvent);
43
44   Vector2 windowSize = window.GetSize();
45
46   mLabels[0] = TextLabel::New("Text Label 1");
47   mLabels[1] = TextLabel::New("Text Label 2");
48
49   mLabels[0].SetProperty(Dali::Actor::Property::NAME, "Label1");
50   mLabels[1].SetProperty(Dali::Actor::Property::NAME, "Label2");
51
52   mLabels[0].SetProperty(Dali::DevelActor::Property::SIBLING_ORDER, 1);
53   mLabels[1].SetProperty(Dali::DevelActor::Property::SIBLING_ORDER, 2);
54
55   mLabels[0].SetProperty(Control::Property::BACKGROUND, Color::RED);
56   mLabels[1].SetProperty(Control::Property::BACKGROUND, Color::YELLOW);
57
58   for(int i = 0; i < NUMBER_OF_LABELS; ++i)
59   {
60     mLabels[i].SetProperty(TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");
61     mLabels[i].SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
62     mLabels[i].SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
63     mLabels[i].SetProperty(Actor::Property::POSITION, Vector2(0, (i * 2 + 1) * windowSize.height * 0.25f));
64   }
65
66   window.Add(mLabels[0]);
67   window.Add(mLabels[1]);
68
69   mSwapButton = PushButton::New();
70   mSwapButton.SetProperty(Button::Property::LABEL, "Swap depth order");
71   mSwapButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER);
72   mSwapButton.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
73   mSwapButton.ClickedSignal().Connect(this, &TextOverlapController::OnClicked);
74   mSwapButton.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
75   mSwapButton.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
76   window.Add(mSwapButton);
77
78   Layer rootLayer = window.GetRootLayer();
79   rootLayer.SetProperty(Dali::Actor::Property::NAME, "RootLayer");
80
81   mPanDetector = PanGestureDetector::New();
82   mPanDetector.Attach(rootLayer);
83   mPanDetector.AddAngle(Radian(-0.5f * Math::PI));
84   mPanDetector.AddAngle(Radian(0.5f * Math::PI));
85   mPanDetector.DetectedSignal().Connect(this, &TextOverlapController::OnPan);
86 }
87
88 void TextOverlapController::OnPan(Actor actor, const PanGesture& gesture)
89 {
90   const GestureState state = gesture.GetState();
91   if(!mGrabbedActor || state == GestureState::STARTED)
92   {
93     const Vector2& gesturePosition = gesture.GetPosition();
94     for(int i = 0; i < NUMBER_OF_LABELS; ++i)
95     {
96       Vector3 position = mLabels[i].GetCurrentProperty<Vector3>(Actor::Property::POSITION);
97       Vector3 size     = mLabels[i].GetCurrentProperty<Vector3>(Actor::Property::SIZE);
98       if(gesturePosition.y > position.y - size.y * 0.5f &&
99          gesturePosition.y <= position.y + size.y * 0.5f)
100       {
101         mGrabbedActor = mLabels[i];
102         break;
103       }
104     }
105   }
106   else if(mGrabbedActor && state == GestureState::CONTINUING)
107   {
108     Vector2        windowSize      = mApplication.GetWindow().GetSize();
109     Vector3        size            = mGrabbedActor.GetCurrentProperty<Vector3>(Actor::Property::SIZE);
110     const Vector2& gesturePosition = gesture.GetPosition();
111
112     float y = Clamp(gesturePosition.y, size.y * 0.5f, windowSize.y - size.y * 0.5f);
113     mGrabbedActor.SetProperty(Actor::Property::POSITION, Vector2(0, y));
114   }
115   else
116   {
117     mGrabbedActor.Reset();
118   }
119 }
120
121 void TextOverlapController::Destroy(Application& app)
122 {
123   mPanDetector.DetachAll();
124   UnparentAndReset(mLabels[0]);
125   UnparentAndReset(mLabels[1]);
126   mGrabbedActor.Reset();
127 }
128
129 bool TextOverlapController::OnClicked(Button button)
130 {
131   mTopmostLabel = 1 - mTopmostLabel; // toggles between 0 and 1
132   mLabels[mTopmostLabel].RaiseToTop();
133   return false;
134 }
135
136 void TextOverlapController::OnKeyEvent(const KeyEvent& keyEvent)
137 {
138   if(keyEvent.GetState() == KeyEvent::DOWN &&
139      (IsKey(keyEvent, DALI_KEY_BACK) ||
140       IsKey(keyEvent, DALI_KEY_ESCAPE)))
141   {
142     mApplication.Quit();
143   }
144   else
145   {
146     Dali::Layer l  = mApplication.GetWindow().GetRootLayer();
147     int         so = l.GetProperty<int>(Dali::DevelActor::Property::SIBLING_ORDER);
148     l.SetProperty(Dali::DevelActor::Property::SIBLING_ORDER, so + 1);
149   }
150 }
151
152 } // namespace Demo
153
154 int DALI_EXPORT_API main(int argc, char** argv)
155 {
156   {
157     Application                 app = Application::New(&argc, &argv);
158     Demo::TextOverlapController controller(app);
159     app.MainLoop();
160   }
161   exit(0);
162 }