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