Changed to Apache 2.0 License
[platform/core/uifw/dali-demo.git] / examples / radial-menu / radial-menu-example.cpp
1 /*
2  * Copyright (c) 2014 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/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20 #include "../shared/view.h"
21 #include "radial-sweep-view.h"
22 #include "radial-sweep-view-impl.h"
23
24 using namespace Dali;
25 using namespace Dali::Toolkit;
26
27 namespace
28 {
29 const char* TEST_OUTER_RING_FILENAME = DALI_IMAGE_DIR "layer2.png"; // Image to be masked
30 const char* TEST_INNER_RING_FILENAME = DALI_IMAGE_DIR "layer1.png"; // Image to be masked
31 const char* TEST_MENU_FILENAME = DALI_IMAGE_DIR "layer3.png"; // Image to be masked
32 const char* TEST_DIAL_FILENAME = DALI_IMAGE_DIR "layer4.png"; // Image to be masked
33 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" ); // Background for toolbar
34 const char* APPLICATION_TITLE( "Radial Menu" );
35 const char * const PLAY_ICON( DALI_IMAGE_DIR "icon-play.png" );
36 const char * const STOP_ICON( DALI_IMAGE_DIR "icon-stop.png" );
37 }
38
39
40 /********************************************************************************
41  * Application controller class
42  */
43
44 // This example shows how to create a mesh actor for use as a stencil buffer
45 class RadialMenuExample : public ConnectionTracker
46 {
47 public:
48   /**
49    * Constructor
50    * @param[in] app The application handle
51    */
52   RadialMenuExample(Application app);
53
54   /**
55    * Destructor
56    */
57   ~RadialMenuExample();
58
59 private:
60
61   /**
62    * Initialization signal handler - all actor initialization should happen here
63    * @param[in] app The application handle
64    */
65   void OnInit(Application& app);
66
67   /**
68    * Create a sweep view with the given image and parameters
69    */
70   RadialSweepView CreateSweepView( std::string imageName, Degree initial, Degree final );
71
72   void StartAnimation();
73
74   bool OnButtonClicked( Toolkit::Button button );
75
76   void OnAnimationFinished( Animation& source );
77
78   /**
79    * Main key event handler
80    *
81    * @param[in] event The key event to respond to
82    */
83   void OnKeyEvent(const KeyEvent& event);
84
85 private: // Member variables
86   enum AnimState
87   {
88     STOPPED,
89     PAUSED,
90     PLAYING
91   };
92
93   Application     mApplication; ///< The application handle
94   Toolkit::View   mView;        ///< The toolbar view
95   Layer           mContents;    ///< The toolbar contents pane
96   ImageActor      mImageActor;  ///< Image actor shown by stencil mask
97   Animation       mAnimation;
98   AnimState       mAnimationState;
99
100   Image               mIconPlay;
101   Image               mIconStop;
102   Toolkit::PushButton mPlayStopButton;
103   ImageActor      mDialActor;
104   RadialSweepView mRadialSweepView1;
105   RadialSweepView mRadialSweepView2;
106   RadialSweepView mRadialSweepView3;
107 };
108
109 RadialMenuExample::RadialMenuExample(Application app)
110 : mApplication( app ),
111   mAnimationState(STOPPED)
112 {
113   // Connect to the Application's Init signal
114   app.InitSignal().Connect(this, &RadialMenuExample::OnInit);
115 }
116
117 RadialMenuExample::~RadialMenuExample()
118 {
119   // Nothing to do here; actor handles will clean up themselves.
120 }
121
122 void RadialMenuExample::OnInit(Application& app)
123 {
124   // The Init signal is received once (only) during the Application lifetime
125   Stage::GetCurrent().KeyEventSignal().Connect(this, &RadialMenuExample::OnKeyEvent);
126
127   // Create toolbar & view
128   Toolkit::ToolBar toolBar;
129   mContents = DemoHelper::CreateView( mApplication,
130                                       mView,
131                                       toolBar,
132                                       "",
133                                       TOOLBAR_IMAGE,
134                                       APPLICATION_TITLE );
135
136   mIconPlay = Image::New( PLAY_ICON );
137   mIconStop = Image::New( STOP_ICON );
138   mPlayStopButton = Toolkit::PushButton::New();
139   mPlayStopButton.SetBackgroundImage( mIconStop );
140
141   mPlayStopButton.ClickedSignal().Connect( this, &RadialMenuExample::OnButtonClicked );
142
143   toolBar.AddControl( mPlayStopButton,
144                       DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
145                       Toolkit::Alignment::HorizontalRight,
146                       DemoHelper::DEFAULT_PLAY_PADDING );
147
148   Vector2 imgSize = Image::GetImageSize(TEST_OUTER_RING_FILENAME);
149   float scale = Stage::GetCurrent().GetSize().width / imgSize.width;
150
151   mRadialSweepView1 = CreateSweepView( TEST_OUTER_RING_FILENAME, Degree(-90.0f), Degree(-90.0f));
152   mRadialSweepView2 = CreateSweepView( TEST_INNER_RING_FILENAME, Degree(90.0f),  Degree(0.0f));
153   mRadialSweepView3 = CreateSweepView( TEST_MENU_FILENAME, Degree(100.0f), Degree(0.0f));
154   mRadialSweepView3.SetInitialActorAngle(Degree(-110));
155   mRadialSweepView3.SetFinalActorAngle(Degree(0));
156
157   Image dial = Image::New( TEST_DIAL_FILENAME );
158   mDialActor = ImageActor::New( dial );
159   mDialActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
160   mDialActor.SetScale(scale);
161   Layer dialLayer = Layer::New();
162
163   dialLayer.Add(mDialActor);
164   dialLayer.SetPositionInheritanceMode(USE_PARENT_POSITION);
165   dialLayer.SetSize(Stage::GetCurrent().GetSize());
166   mContents.Add(dialLayer);
167
168   mRadialSweepView1.SetScale(scale);
169   mRadialSweepView2.SetScale(scale);
170   mRadialSweepView3.SetScale(scale);
171
172   StartAnimation();
173 }
174
175 void RadialMenuExample::StartAnimation()
176 {
177   mDialActor.SetOpacity(0.0f);
178   mRadialSweepView1.SetOpacity(0.0f);
179   mAnimation = Animation::New(6.0f);
180   mRadialSweepView1.Activate(mAnimation, 0.0f, 3.0f);
181   mRadialSweepView2.Activate(mAnimation, 1.5f, 3.0f);
182   mRadialSweepView3.Activate(mAnimation, 3.0f, 3.0f);
183   mAnimation.OpacityTo(mDialActor, 1.0f, AlphaFunctions::EaseIn, 0.0f, 0.8f);
184   mAnimation.OpacityTo(mRadialSweepView1, 1.0f, AlphaFunctions::EaseIn, 0.0f, 0.5f);
185   mAnimation.FinishedSignal().Connect( this, &RadialMenuExample::OnAnimationFinished );
186
187   mAnimationState = PLAYING;
188   mAnimation.Play();
189 }
190
191 bool RadialMenuExample::OnButtonClicked( Toolkit::Button button )
192 {
193   switch( mAnimationState )
194   {
195     case PLAYING:
196     {
197       mAnimation.Pause();
198       mPlayStopButton.SetBackgroundImage( mIconPlay );
199     }
200     break;
201
202     case PAUSED:
203     {
204       mAnimation.Play();
205       mPlayStopButton.SetBackgroundImage( mIconStop );
206     }
207     break;
208
209     case STOPPED:
210     {
211       mPlayStopButton.SetBackgroundImage( mIconStop );
212       mRadialSweepView1.Deactivate();
213       mRadialSweepView2.Deactivate();
214       mRadialSweepView3.Deactivate();
215       StartAnimation();
216     }
217   }
218   return false;
219 }
220
221 void RadialMenuExample::OnAnimationFinished( Animation& source )
222 {
223   mAnimationState = STOPPED;
224   mPlayStopButton.SetBackgroundImage( mIconPlay );
225 }
226
227 RadialSweepView RadialMenuExample::CreateSweepView( std::string imageName,
228                                                     Degree initialAngle,
229                                                     Degree finalAngle)
230 {
231   // Create the image
232   Image image = Image::New(imageName);
233   mImageActor = ImageActor::New(image);
234   mImageActor.SetParentOrigin(ParentOrigin::CENTER);
235   mImageActor.SetAnchorPoint(AnchorPoint::CENTER);
236
237   // Create the stencil
238   float diameter = std::max(image.GetWidth(), image.GetHeight());
239   RadialSweepView radialSweepView = RadialSweepView::New();
240   radialSweepView.SetDiameter( diameter );
241   radialSweepView.SetInitialAngle( initialAngle );
242   radialSweepView.SetFinalAngle( finalAngle );
243   radialSweepView.SetInitialSector( Degree(0.0f) );
244   radialSweepView.SetFinalSector( Degree(359.999f) );
245   radialSweepView.SetSize( Stage::GetCurrent().GetSize());
246   radialSweepView.SetEasingFunction( Dali::AlphaFunctions::EaseInOut );
247   radialSweepView.SetPositionInheritanceMode(USE_PARENT_POSITION);
248   mContents.Add(radialSweepView);
249   radialSweepView.Add( mImageActor );
250   mImageActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
251
252   return radialSweepView;
253 }
254
255
256 void RadialMenuExample::OnKeyEvent(const KeyEvent& event)
257 {
258   if(event.state == KeyEvent::Down)
259   {
260     if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
261     {
262       mApplication.Quit();
263     }
264   }
265 }
266
267 void RunTest(Application app)
268 {
269   RadialMenuExample test(app);
270
271   app.MainLoop();
272 }
273
274 // Entry point for Linux & Tizen applications
275 int main(int argc, char **argv)
276 {
277   Application app = Application::New(&argc, &argv);
278
279   RunTest(app);
280
281   return 0;
282 }