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