Merge "[3.0] Removed 3D layer dependency of Model3dView and Mesh Visual." into tizen
[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   mDialView = ImageView::New( TEST_DIAL_FILENAME );
176   mDialView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
177   mDialView.SetParentOrigin( ParentOrigin::CENTER );
178   mDialView.SetScale(scale);
179   Layer dialLayer = Layer::New();
180
181   dialLayer.Add( mDialView );
182   dialLayer.SetParentOrigin( ParentOrigin::CENTER );
183   dialLayer.SetSize(stage.GetSize());
184   mContents.Add(dialLayer);
185
186   mRadialSweepView1.SetScale(scale);
187   mRadialSweepView2.SetScale(scale);
188   mRadialSweepView3.SetScale(scale);
189
190   StartAnimation();
191 }
192
193 void RadialMenuExample::StartAnimation()
194 {
195   mDialView.SetOpacity(0.0f);
196   mRadialSweepView1.SetOpacity(0.0f);
197   mAnimation = Animation::New(6.0f);
198   mRadialSweepView1.Activate(mAnimation, 0.0f, 3.0f);
199   mRadialSweepView2.Activate(mAnimation, 1.5f, 3.0f);
200   mRadialSweepView3.Activate(mAnimation, 3.0f, 3.0f);
201   mAnimation.AnimateTo( Property( mDialView, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.8f ) );
202   mAnimation.AnimateTo( Property( mRadialSweepView1, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.5f ) );
203   mAnimation.FinishedSignal().Connect( this, &RadialMenuExample::OnAnimationFinished );
204
205   mAnimationState = PLAYING;
206   mAnimation.Play();
207 }
208
209 bool RadialMenuExample::OnButtonClicked( Toolkit::Button button )
210 {
211   switch( mAnimationState )
212   {
213     case PLAYING:
214     {
215       mAnimation.Pause();
216       mAnimationState = PAUSED;
217       mPlayStopButton.SetUnselectedImage( PLAY_ICON );
218       mPlayStopButton.SetSelectedImage( PLAY_ICON_SELECTED );
219     }
220     break;
221
222     case PAUSED:
223     {
224       mAnimation.Play();
225       mAnimationState = PLAYING;
226       mPlayStopButton.SetUnselectedImage( STOP_ICON );
227       mPlayStopButton.SetSelectedImage( STOP_ICON_SELECTED );
228     }
229     break;
230
231     case STOPPED:
232     {
233       mPlayStopButton.SetUnselectedImage( STOP_ICON );
234       mPlayStopButton.SetSelectedImage( STOP_ICON_SELECTED );
235       mRadialSweepView1.Deactivate();
236       mRadialSweepView2.Deactivate();
237       mRadialSweepView3.Deactivate();
238       StartAnimation();
239     }
240   }
241   return false;
242 }
243
244 void RadialMenuExample::OnAnimationFinished( Animation& source )
245 {
246   mAnimationState = STOPPED;
247   mPlayStopButton.SetUnselectedImage( PLAY_ICON );
248   mPlayStopButton.SetSelectedImage( PLAY_ICON_SELECTED );
249 }
250
251 RadialSweepView RadialMenuExample::CreateSweepView( std::string imageName,
252                                                     Degree initialAngle,
253                                                     Degree finalAngle)
254 {
255   // Create the image
256   mImageView = ImageView::New(imageName);
257   mImageView.SetParentOrigin(ParentOrigin::CENTER);
258   mImageView.SetAnchorPoint(AnchorPoint::CENTER);
259   mImageView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
260
261   // Create the stencil
262   const ImageDimensions imageSize = ResourceImage::GetImageSize(imageName);
263   float diameter = std::max(imageSize.GetWidth(), imageSize.GetHeight());
264   RadialSweepView radialSweepView = RadialSweepView::New();
265   radialSweepView.SetDiameter( diameter );
266   radialSweepView.SetInitialAngle( initialAngle );
267   radialSweepView.SetFinalAngle( finalAngle );
268   radialSweepView.SetInitialSector( Degree(0.0f) );
269   radialSweepView.SetFinalSector( Degree(359.999f) );
270   radialSweepView.SetSize( Stage::GetCurrent().GetSize());
271   radialSweepView.SetEasingFunction( Dali::AlphaFunction::EASE_IN_OUT );
272   radialSweepView.SetParentOrigin( ParentOrigin::CENTER );
273   mContents.Add(radialSweepView);
274   radialSweepView.Add( mImageView );
275   mImageView.SetParentOrigin( ParentOrigin::CENTER );
276
277   return radialSweepView;
278 }
279
280
281 void RadialMenuExample::OnKeyEvent(const KeyEvent& event)
282 {
283   if(event.state == KeyEvent::Down)
284   {
285     if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
286     {
287       mApplication.Quit();
288     }
289   }
290 }
291
292 void RunTest(Application app)
293 {
294   RadialMenuExample test(app);
295
296   app.MainLoop();
297 }
298
299 // Entry point for Linux & Tizen applications
300 int DALI_EXPORT_API main(int argc, char **argv)
301 {
302   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
303
304   RunTest(app);
305
306   return 0;
307 }