Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / arc-visual / arc-visual-example.cpp
1 /*
2  * Copyright (c) 2020 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 <dali-toolkit/devel-api/controls/control-devel.h>
20 #include <dali-toolkit/devel-api/visual-factory/transition-data.h>
21 #include <dali-toolkit/devel-api/visuals/arc-visual-actions-devel.h>
22 #include <dali-toolkit/devel-api/visuals/arc-visual-properties-devel.h>
23 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
24
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27
28 namespace
29 {
30 const float START_ANGLE_INITIAL_VALUE(0.0f);
31 const float START_ANGLE_TARGET_VALUE(360.0f);
32 const float SWEEP_ANGLE_INITIAL_VALUE(90.0f);
33 const float SWEEP_ANGLE_TARGET_VALUE(360.0f);
34 const float ANIMATION_DURATION(3.0f);
35
36 const Property::Value BACKGROUND{
37   {Visual::Property::TYPE, DevelVisual::ARC},
38   {Visual::Property::MIX_COLOR, Color::RED},
39   {DevelArcVisual::Property::START_ANGLE, 0.0f},
40   {DevelArcVisual::Property::SWEEP_ANGLE, 90.0f},
41   {DevelArcVisual::Property::CAP, DevelArcVisual::Cap::ROUND},
42   {DevelArcVisual::Property::THICKNESS, 20.0f}};
43
44 const Property::Value TEXT_BACKGROUND{
45   {Visual::Property::TYPE, Visual::COLOR},
46   {ColorVisual::Property::MIX_COLOR, Vector4(0.8f, 0.8f, 0.8f, 1.0f)},
47   {DevelVisual::Property::CORNER_RADIUS, 0.5f},
48   {DevelVisual::Property::CORNER_RADIUS_POLICY, Toolkit::Visual::Transform::Policy::RELATIVE}};
49
50 const Property::Value TRANSITION_ANIMATOR{
51   {"timePeriod", Property::Map().Add("duration", ANIMATION_DURATION)}};
52
53 const Property::Value TRANSITION_START_ANGLE{
54   {"target", "background"},
55   {"property", "startAngle"},
56   {"initialValue", START_ANGLE_INITIAL_VALUE},
57   {"targetValue", START_ANGLE_TARGET_VALUE},
58   {"animator", TRANSITION_ANIMATOR}};
59
60 const Property::Value TRANSITION_SWEEP_ANGLE{
61   {"target", "background"},
62   {"property", "sweepAngle"},
63   {"initialValue", SWEEP_ANGLE_INITIAL_VALUE},
64   {"targetValue", SWEEP_ANGLE_TARGET_VALUE},
65   {"animator", TRANSITION_ANIMATOR}};
66
67 } // namespace
68
69 // This example shows the properties of the arc visual - thickness, startAngle and sweepAngle and animates them.
70 //
71 class ArcVisualExample : public ConnectionTracker
72 {
73 public:
74   ArcVisualExample(Application& application)
75   : mApplication(application),
76     mSelectedPoperty(DevelArcVisual::Property::START_ANGLE)
77   {
78     // Connect to the Application's Init signal
79     mApplication.InitSignal().Connect(this, &ArcVisualExample::Create);
80   }
81
82   ~ArcVisualExample() = default;
83
84 private:
85   // The Init signal is received once (only) during the Application lifetime
86   void Create(Application& application)
87   {
88     // Get a handle to the window
89     Window window = application.GetWindow();
90     window.SetBackgroundColor(Color::WHITE);
91
92     mControl = Control::New();
93     mControl.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
94     mControl.SetProperty(Actor::Property::SIZE, Vector2(300.0f, 300.0f));
95     mControl.SetProperty(Control::Property::BACKGROUND, BACKGROUND);
96     window.Add(mControl);
97
98     mStartAngleLabel = TextLabel::New("1");
99     mStartAngleLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
100     mStartAngleLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_RIGHT);
101     mStartAngleLabel.SetProperty(Actor::Property::POSITION, Vector2(-30.0f, -10.0f));
102     mStartAngleLabel.SetProperty(Control::Property::BACKGROUND, TEXT_BACKGROUND);
103     mStartAngleLabel.SetProperty(Actor::Property::WIDTH_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
104     mStartAngleLabel.SetProperty(Actor::Property::HEIGHT_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
105     mStartAngleLabel.SetProperty(Control::Property::PADDING, Extents(20.0f, 20.0f, 10.0f, 10.0f));
106     mStartAngleLabel.TouchedSignal().Connect(this, &ArcVisualExample::OnButtonTouch);
107     window.Add(mStartAngleLabel);
108
109     mSweepAngleLabel = TextLabel::New("2");
110     mSweepAngleLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
111     mSweepAngleLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
112     mSweepAngleLabel.SetProperty(Actor::Property::POSITION, Vector2(0.0f, -10.0f));
113     mSweepAngleLabel.SetProperty(Control::Property::BACKGROUND, TEXT_BACKGROUND);
114     mSweepAngleLabel.SetProperty(Actor::Property::WIDTH_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
115     mSweepAngleLabel.SetProperty(Actor::Property::HEIGHT_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
116     mSweepAngleLabel.SetProperty(Control::Property::PADDING, Extents(20.0f, 20.0f, 10.0f, 10.0f));
117     mSweepAngleLabel.TouchedSignal().Connect(this, &ArcVisualExample::OnButtonTouch);
118     window.Add(mSweepAngleLabel);
119
120     mThicknessLabel = TextLabel::New("3");
121     mThicknessLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
122     mThicknessLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_LEFT);
123     mThicknessLabel.SetProperty(Actor::Property::POSITION, Vector2(30.0f, -10.0f));
124     mThicknessLabel.SetProperty(Control::Property::BACKGROUND, TEXT_BACKGROUND);
125     mThicknessLabel.SetProperty(Actor::Property::WIDTH_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
126     mThicknessLabel.SetProperty(Actor::Property::HEIGHT_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
127     mThicknessLabel.SetProperty(Control::Property::PADDING, Extents(20.0f, 20.0f, 10.0f, 10.0f));
128     mThicknessLabel.TouchedSignal().Connect(this, &ArcVisualExample::OnButtonTouch);
129     window.Add(mThicknessLabel);
130
131     mPlusTextLabel = TextLabel::New("+");
132     mPlusTextLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
133     mPlusTextLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
134     mPlusTextLabel.SetProperty(Actor::Property::POSITION, Vector2(20.0f, 10.0f));
135     mPlusTextLabel.SetProperty(Control::Property::BACKGROUND, TEXT_BACKGROUND);
136     mPlusTextLabel.SetProperty(Actor::Property::WIDTH_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
137     mPlusTextLabel.SetProperty(Actor::Property::HEIGHT_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
138     mPlusTextLabel.SetProperty(Control::Property::PADDING, Extents(20.0f, 20.0f, 10.0f, 10.0f));
139     mPlusTextLabel.TouchedSignal().Connect(this, &ArcVisualExample::OnButtonTouch);
140     window.Add(mPlusTextLabel);
141
142     mMinusTextLabel = TextLabel::New("-");
143     mMinusTextLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
144     mMinusTextLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_RIGHT);
145     mMinusTextLabel.SetProperty(Actor::Property::POSITION, Vector2(-20.0f, 10.0f));
146     mMinusTextLabel.SetProperty(Control::Property::BACKGROUND, TEXT_BACKGROUND);
147     mMinusTextLabel.SetProperty(Actor::Property::WIDTH_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
148     mMinusTextLabel.SetProperty(Actor::Property::HEIGHT_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
149     mMinusTextLabel.SetProperty(Control::Property::PADDING, Extents(25.0f, 25.0f, 10.0f, 10.0f));
150     mMinusTextLabel.TouchedSignal().Connect(this, &ArcVisualExample::OnButtonTouch);
151     window.Add(mMinusTextLabel);
152
153     // Respond to a click anywhere on the window
154     window.GetRootLayer().TouchedSignal().Connect(this, &ArcVisualExample::OnTouch);
155
156     // Respond to key events
157     window.KeyEventSignal().Connect(this, &ArcVisualExample::OnKeyEvent);
158   }
159
160   bool OnButtonTouch(Actor actor, const TouchEvent& touch)
161   {
162     if(touch.GetState(0) == PointState::UP)
163     {
164       Control control = Control::DownCast(actor);
165       if(control == mStartAngleLabel)
166       {
167         mSelectedPoperty = DevelArcVisual::Property::START_ANGLE;
168       }
169       else if(control == mSweepAngleLabel)
170       {
171         mSelectedPoperty = DevelArcVisual::Property::SWEEP_ANGLE;
172       }
173       else if(control == mThicknessLabel)
174       {
175         mSelectedPoperty = DevelArcVisual::Property::THICKNESS;
176       }
177       else if(control == mPlusTextLabel)
178       {
179         Property::Map    map   = mControl.GetProperty<Property::Map>(Control::Property::BACKGROUND);
180         Property::Value* value = map.Find(mSelectedPoperty);
181         if(value)
182         {
183           DevelControl::DoAction(mControl, Control::Property::BACKGROUND, DevelArcVisual::Action::UPDATE_PROPERTY, Property::Map().Add(mSelectedPoperty, value->Get<float>() + 5.0f));
184         }
185       }
186       else
187       {
188         Property::Map    map   = mControl.GetProperty<Property::Map>(Control::Property::BACKGROUND);
189         Property::Value* value = map.Find(mSelectedPoperty);
190         if(value)
191         {
192           DevelControl::DoAction(mControl, Control::Property::BACKGROUND, DevelArcVisual::Action::UPDATE_PROPERTY, Property::Map().Add(mSelectedPoperty, value->Get<float>() - 5.0f));
193         }
194       }
195     }
196     return true;
197   }
198
199   bool OnTouch(Actor actor, const TouchEvent& touch)
200   {
201     if(touch.GetState(0) == PointState::UP)
202     {
203       Property::Array array;
204       array.PushBack(TRANSITION_START_ANGLE);
205       array.PushBack(TRANSITION_SWEEP_ANGLE);
206
207       TransitionData transitionData = TransitionData::New(array);
208       Animation      animation      = DevelControl::CreateTransition(Toolkit::Internal::GetImplementation(mControl), transitionData);
209       animation.Play();
210     }
211     return true;
212   }
213
214   void OnKeyEvent(const KeyEvent& event)
215   {
216     if(event.GetState() == KeyEvent::UP)
217     {
218       if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
219       {
220         mApplication.Quit();
221       }
222     }
223   }
224
225 private:
226   Application&    mApplication;
227   Control         mControl;
228   TextLabel       mStartAngleLabel;
229   TextLabel       mSweepAngleLabel;
230   TextLabel       mThicknessLabel;
231   TextLabel       mPlusTextLabel;
232   TextLabel       mMinusTextLabel;
233   Property::Index mSelectedPoperty;
234 };
235
236 int DALI_EXPORT_API main(int argc, char** argv)
237 {
238   Application      application = Application::New(&argc, &argv);
239   ArcVisualExample test(application);
240   application.MainLoop();
241   return 0;
242 }