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