(ArcVisual) Fix wrong transtion animator value
[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/visual-factory/transition-data.h>
23
24 using namespace Dali;
25 using namespace Dali::Toolkit;
26
27 namespace
28 {
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 {
38   { Visual::Property::TYPE, DevelVisual::ARC },
39   { Visual::Property::MIX_COLOR, Color::RED },
40   { DevelArcVisual::Property::START_ANGLE, 0.0f },
41   { DevelArcVisual::Property::SWEEP_ANGLE, 90.0f },
42   { DevelArcVisual::Property::CAP, DevelArcVisual::Cap::ROUND },
43   { DevelArcVisual::Property::THICKNESS, 20.0f }
44 };
45
46 const Property::Value TRANSITION_ANIMATOR
47 {
48   { "timePeriod", Property::Map().Add( "duration", ANIMATION_DURATION ) }
49 };
50
51 const Property::Value TRANSITION_START_ANGLE
52 {
53   { "target", "background" },
54   { "property", "startAngle" },
55   { "initialValue", START_ANGLE_INITIAL_VALUE },
56   { "targetValue", START_ANGLE_TARGET_VALUE },
57   { "animator", TRANSITION_ANIMATOR }
58 };
59
60 const Property::Value TRANSITION_SWEEP_ANGLE
61 {
62   { "target", "background" },
63   { "property", "sweepAngle" },
64   { "initialValue", SWEEP_ANGLE_INITIAL_VALUE },
65   { "targetValue", SWEEP_ANGLE_TARGET_VALUE },
66   { "animator", TRANSITION_ANIMATOR }
67 };
68
69 } // namespace
70
71 // This example shows the properties of the arc visual - thickness, startAngle and sweepAngle and animates them.
72 //
73 class ArcVisualExample : public ConnectionTracker
74 {
75 public:
76
77   ArcVisualExample( Application& application )
78   : mApplication( application ),
79     mStartAngle( 0.0f ),
80     mSweepAngle( 0.0f )
81   {
82     // Connect to the Application's Init signal
83     mApplication.InitSignal().Connect( this, &ArcVisualExample::Create );
84   }
85
86   ~ArcVisualExample() = default;
87
88 private:
89
90   // The Init signal is received once (only) during the Application lifetime
91   void Create( Application& application )
92   {
93     // Get a handle to the stage
94     Stage stage = Stage::GetCurrent();
95     stage.SetBackgroundColor( Color::WHITE );
96
97     mControl = Control::New();
98     mControl.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
99     mControl.SetProperty( Actor::Property::SIZE, Vector2( 200.0f, 200.0f ) );
100     mControl.SetProperty( Control::Property::BACKGROUND, BACKGROUND );
101
102     stage.Add( mControl );
103
104     // Respond to a click anywhere on the stage
105     stage.GetRootLayer().TouchSignal().Connect( this, &ArcVisualExample::OnTouch );
106
107     // Respond to key events
108     stage.KeyEventSignal().Connect( this, &ArcVisualExample::OnKeyEvent );
109   }
110
111   bool OnTouch( Actor actor, const TouchData& touch )
112   {
113     if( touch.GetState( 0 ) == PointState::UP )
114     {
115       Property::Array array;
116       array.PushBack( TRANSITION_START_ANGLE );
117       array.PushBack( TRANSITION_SWEEP_ANGLE );
118
119       TransitionData transitionData = TransitionData::New( array );
120       Animation animation = DevelControl::CreateTransition( Toolkit::Internal::GetImplementation( mControl ), transitionData );
121       animation.Play();
122     }
123     return true;
124   }
125
126   void OnKeyEvent( const KeyEvent& event )
127   {
128     if( event.state == KeyEvent::Down )
129     {
130       if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
131       {
132         mApplication.Quit();
133       }
134     }
135   }
136
137 private:
138   Application&  mApplication;
139   Control mControl;
140   float mStartAngle;
141   float mSweepAngle;
142 };
143
144 int DALI_EXPORT_API main( int argc, char **argv )
145 {
146   Application application = Application::New( &argc, &argv );
147   ArcVisualExample test( application );
148   application.MainLoop();
149   return 0;
150 }