Updated all files to new format
[platform/core/uifw/dali-demo.git] / examples / arc-visual / arc-visual-example.cpp
1 /*
2  * Copyright (c) 2021 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 } // namespace
51
52 // This example shows the properties of the arc visual - thickness, startAngle and sweepAngle and animates them.
53 //
54 class ArcVisualExample : public ConnectionTracker
55 {
56 public:
57   ArcVisualExample(Application& application)
58   : mApplication(application),
59     mSelectedPoperty(DevelArcVisual::Property::START_ANGLE)
60   {
61     // Connect to the Application's Init signal
62     mApplication.InitSignal().Connect(this, &ArcVisualExample::Create);
63   }
64
65   ~ArcVisualExample() = default;
66
67 private:
68   // The Init signal is received once (only) during the Application lifetime
69   void Create(Application& application)
70   {
71     // Get a handle to the window
72     Window window = application.GetWindow();
73     window.SetBackgroundColor(Color::WHITE);
74
75     mControl = Control::New();
76     mControl.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
77     mControl.SetProperty(Actor::Property::SIZE, Vector2(300.0f, 300.0f));
78     mControl.SetProperty(Control::Property::BACKGROUND, BACKGROUND);
79     window.Add(mControl);
80
81     mStartAngleLabel = TextLabel::New("1");
82     mStartAngleLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
83     mStartAngleLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_RIGHT);
84     mStartAngleLabel.SetProperty(Actor::Property::POSITION, Vector2(-30.0f, -10.0f));
85     mStartAngleLabel.SetProperty(Control::Property::BACKGROUND, TEXT_BACKGROUND);
86     mStartAngleLabel.SetProperty(Actor::Property::WIDTH_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
87     mStartAngleLabel.SetProperty(Actor::Property::HEIGHT_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
88     mStartAngleLabel.SetProperty(Control::Property::PADDING, Extents(20.0f, 20.0f, 10.0f, 10.0f));
89     mStartAngleLabel.TouchedSignal().Connect(this, &ArcVisualExample::OnButtonTouch);
90     window.Add(mStartAngleLabel);
91
92     mSweepAngleLabel = TextLabel::New("2");
93     mSweepAngleLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
94     mSweepAngleLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER);
95     mSweepAngleLabel.SetProperty(Actor::Property::POSITION, Vector2(0.0f, -10.0f));
96     mSweepAngleLabel.SetProperty(Control::Property::BACKGROUND, TEXT_BACKGROUND);
97     mSweepAngleLabel.SetProperty(Actor::Property::WIDTH_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
98     mSweepAngleLabel.SetProperty(Actor::Property::HEIGHT_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
99     mSweepAngleLabel.SetProperty(Control::Property::PADDING, Extents(20.0f, 20.0f, 10.0f, 10.0f));
100     mSweepAngleLabel.TouchedSignal().Connect(this, &ArcVisualExample::OnButtonTouch);
101     window.Add(mSweepAngleLabel);
102
103     mThicknessLabel = TextLabel::New("3");
104     mThicknessLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
105     mThicknessLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_LEFT);
106     mThicknessLabel.SetProperty(Actor::Property::POSITION, Vector2(30.0f, -10.0f));
107     mThicknessLabel.SetProperty(Control::Property::BACKGROUND, TEXT_BACKGROUND);
108     mThicknessLabel.SetProperty(Actor::Property::WIDTH_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
109     mThicknessLabel.SetProperty(Actor::Property::HEIGHT_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
110     mThicknessLabel.SetProperty(Control::Property::PADDING, Extents(20.0f, 20.0f, 10.0f, 10.0f));
111     mThicknessLabel.TouchedSignal().Connect(this, &ArcVisualExample::OnButtonTouch);
112     window.Add(mThicknessLabel);
113
114     mPlusTextLabel = TextLabel::New("+");
115     mPlusTextLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
116     mPlusTextLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
117     mPlusTextLabel.SetProperty(Actor::Property::POSITION, Vector2(20.0f, 10.0f));
118     mPlusTextLabel.SetProperty(Control::Property::BACKGROUND, TEXT_BACKGROUND);
119     mPlusTextLabel.SetProperty(Actor::Property::WIDTH_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
120     mPlusTextLabel.SetProperty(Actor::Property::HEIGHT_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
121     mPlusTextLabel.SetProperty(Control::Property::PADDING, Extents(20.0f, 20.0f, 10.0f, 10.0f));
122     mPlusTextLabel.TouchedSignal().Connect(this, &ArcVisualExample::OnButtonTouch);
123     window.Add(mPlusTextLabel);
124
125     mMinusTextLabel = TextLabel::New("-");
126     mMinusTextLabel.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
127     mMinusTextLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_RIGHT);
128     mMinusTextLabel.SetProperty(Actor::Property::POSITION, Vector2(-20.0f, 10.0f));
129     mMinusTextLabel.SetProperty(Control::Property::BACKGROUND, TEXT_BACKGROUND);
130     mMinusTextLabel.SetProperty(Actor::Property::WIDTH_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
131     mMinusTextLabel.SetProperty(Actor::Property::HEIGHT_RESIZE_POLICY, ResizePolicy::USE_NATURAL_SIZE);
132     mMinusTextLabel.SetProperty(Control::Property::PADDING, Extents(25.0f, 25.0f, 10.0f, 10.0f));
133     mMinusTextLabel.TouchedSignal().Connect(this, &ArcVisualExample::OnButtonTouch);
134     window.Add(mMinusTextLabel);
135
136     // Respond to a click anywhere on the window
137     window.GetRootLayer().TouchedSignal().Connect(this, &ArcVisualExample::OnTouch);
138
139     // Respond to key events
140     window.KeyEventSignal().Connect(this, &ArcVisualExample::OnKeyEvent);
141   }
142
143   bool OnButtonTouch(Actor actor, const TouchEvent& touch)
144   {
145     if(touch.GetState(0) == PointState::UP)
146     {
147       Control control = Control::DownCast(actor);
148       if(control == mStartAngleLabel)
149       {
150         mSelectedPoperty = DevelArcVisual::Property::START_ANGLE;
151       }
152       else if(control == mSweepAngleLabel)
153       {
154         mSelectedPoperty = DevelArcVisual::Property::SWEEP_ANGLE;
155       }
156       else if(control == mThicknessLabel)
157       {
158         mSelectedPoperty = DevelArcVisual::Property::THICKNESS;
159       }
160       else if(control == mPlusTextLabel)
161       {
162         Property::Map    map   = mControl.GetProperty<Property::Map>(Control::Property::BACKGROUND);
163         Property::Value* value = map.Find(mSelectedPoperty);
164         if(value)
165         {
166           DevelControl::DoAction(mControl, Control::Property::BACKGROUND, DevelArcVisual::Action::UPDATE_PROPERTY, Property::Map().Add(mSelectedPoperty, value->Get<float>() + 5.0f));
167         }
168       }
169       else
170       {
171         Property::Map    map   = mControl.GetProperty<Property::Map>(Control::Property::BACKGROUND);
172         Property::Value* value = map.Find(mSelectedPoperty);
173         if(value)
174         {
175           DevelControl::DoAction(mControl, Control::Property::BACKGROUND, DevelArcVisual::Action::UPDATE_PROPERTY, Property::Map().Add(mSelectedPoperty, value->Get<float>() - 5.0f));
176         }
177       }
178     }
179     return true;
180   }
181
182   bool OnTouch(Actor actor, const TouchEvent& touch)
183   {
184     if(touch.GetState(0) == PointState::UP)
185     {
186       DevelControl::DoAction(mControl,
187                              Control::Property::BACKGROUND,
188                              DevelArcVisual::Action::UPDATE_PROPERTY,
189                              Property::Map()
190                                .Add(DevelArcVisual::Property::START_ANGLE, START_ANGLE_INITIAL_VALUE)
191                                .Add(DevelArcVisual::Property::SWEEP_ANGLE, SWEEP_ANGLE_INITIAL_VALUE));
192
193       Animation animation = Animation::New(ANIMATION_DURATION);
194       animation.AnimateTo(DevelControl::GetVisualProperty(mControl, Control::Property::BACKGROUND, DevelArcVisual::Property::START_ANGLE), START_ANGLE_TARGET_VALUE);
195       animation.AnimateTo(DevelControl::GetVisualProperty(mControl, Control::Property::BACKGROUND, DevelArcVisual::Property::SWEEP_ANGLE), SWEEP_ANGLE_TARGET_VALUE);
196       animation.Play();
197     }
198     return true;
199   }
200
201   void OnKeyEvent(const KeyEvent& event)
202   {
203     if(event.GetState() == KeyEvent::UP)
204     {
205       if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
206       {
207         mApplication.Quit();
208       }
209     }
210   }
211
212 private:
213   Application&    mApplication;
214   Control         mControl;
215   TextLabel       mStartAngleLabel;
216   TextLabel       mSweepAngleLabel;
217   TextLabel       mThicknessLabel;
218   TextLabel       mPlusTextLabel;
219   TextLabel       mMinusTextLabel;
220   Property::Index mSelectedPoperty;
221 };
222
223 int DALI_EXPORT_API main(int argc, char** argv)
224 {
225   Application      application = Application::New(&argc, &argv);
226   ArcVisualExample test(application);
227   application.MainLoop();
228   return 0;
229 }