3 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "text-overlap-example.h"
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <dali/devel-api/actors/actor-devel.h>
25 using namespace Dali::Toolkit;
27 static const int NUMBER_OF_LABELS(2);
31 TextOverlapController::TextOverlapController(Application& app)
35 app.InitSignal().Connect(this, &TextOverlapController::Create);
36 app.TerminateSignal().Connect(this, &TextOverlapController::Destroy);
39 void TextOverlapController::Create(Application& app)
41 Window window = app.GetWindow();
42 window.KeyEventSignal().Connect(this, &TextOverlapController::OnKeyEvent);
44 Vector2 windowSize = window.GetSize();
46 mLabels[0] = TextLabel::New("Text Label 1");
47 mLabels[1] = TextLabel::New("Text Label 2");
49 mLabels[0].SetProperty(Dali::Actor::Property::NAME, "Label1");
50 mLabels[1].SetProperty(Dali::Actor::Property::NAME, "Label2");
52 mLabels[0].SetProperty(Dali::DevelActor::Property::SIBLING_ORDER, 1);
53 mLabels[1].SetProperty(Dali::DevelActor::Property::SIBLING_ORDER, 2);
55 mLabels[0].SetProperty(Control::Property::BACKGROUND, Color::RED);
56 mLabels[1].SetProperty(Control::Property::BACKGROUND, Color::YELLOW);
58 for(int i = 0; i < NUMBER_OF_LABELS; ++i)
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));
66 window.Add(mLabels[0]);
67 window.Add(mLabels[1]);
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);
78 Layer rootLayer = window.GetRootLayer();
79 rootLayer.SetProperty(Dali::Actor::Property::NAME, "RootLayer");
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);
88 void TextOverlapController::OnPan(Actor actor, const PanGesture& gesture)
90 const GestureState state = gesture.GetState();
91 if(!mGrabbedActor || state == GestureState::STARTED)
93 const Vector2& gesturePosition = gesture.GetPosition();
94 for(int i = 0; i < NUMBER_OF_LABELS; ++i)
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)
101 mGrabbedActor = mLabels[i];
106 else if(mGrabbedActor && state == GestureState::CONTINUING)
108 Vector2 windowSize = mApplication.GetWindow().GetSize();
109 Vector3 size = mGrabbedActor.GetCurrentProperty<Vector3>(Actor::Property::SIZE);
110 const Vector2& gesturePosition = gesture.GetPosition();
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));
117 mGrabbedActor.Reset();
121 void TextOverlapController::Destroy(Application& app)
123 mPanDetector.DetachAll();
124 UnparentAndReset(mLabels[0]);
125 UnparentAndReset(mLabels[1]);
126 mGrabbedActor.Reset();
129 bool TextOverlapController::OnClicked(Button button)
131 mTopmostLabel = 1 - mTopmostLabel; // toggles between 0 and 1
132 mLabels[mTopmostLabel].RaiseToTop();
136 void TextOverlapController::OnKeyEvent(const KeyEvent& keyEvent)
138 if(keyEvent.GetState() == KeyEvent::DOWN &&
139 (IsKey(keyEvent, DALI_KEY_BACK) ||
140 IsKey(keyEvent, DALI_KEY_ESCAPE)))
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);
154 int DALI_EXPORT_API main(int argc, char** argv)
157 Application app = Application::New(&argc, &argv);
158 Demo::TextOverlapController controller(app);