Merge branch 'devel/master' into devel/new_mesh
[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 = 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   /**
73    * Start the sweep animation on the menu
74    */
75   void StartAnimation();
76
77   /**
78    * Play or pause the animation when the button is clicked
79    */
80   bool OnButtonClicked( Toolkit::Button button );
81
82   /**
83    * Update the state flag and change the button icon when the animation is finished
84    */
85   void OnAnimationFinished( Animation& source );
86
87   /**
88    * Main key event handler
89    *
90    * @param[in] event The key event to respond to
91    */
92   void OnKeyEvent(const KeyEvent& event);
93
94 private: // Member variables
95   enum AnimState
96   {
97     STOPPED,
98     PAUSED,
99     PLAYING
100   };
101
102   Application     mApplication; ///< The application handle
103   Toolkit::Control mView;        ///< The toolbar view
104   Layer           mContents;    ///< The toolbar contents pane
105   ImageActor      mImageActor;  ///< Image actor shown by stencil mask
106   Animation       mAnimation;
107   AnimState       mAnimationState;
108
109   Image               mIconPlay;
110   Image               mIconStop;
111   Toolkit::PushButton mPlayStopButton;
112   ImageActor      mDialActor;
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   mIconPlay = ResourceImage::New( PLAY_ICON );
148   mIconStop = ResourceImage::New( STOP_ICON );
149   mPlayStopButton = Toolkit::PushButton::New();
150   mPlayStopButton.SetBackgroundImage( mIconStop );
151
152   mPlayStopButton.ClickedSignal().Connect( this, &RadialMenuExample::OnButtonClicked );
153
154   toolBar.AddControl( mPlayStopButton,
155                       DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
156                       Toolkit::Alignment::HorizontalRight,
157                       DemoHelper::DEFAULT_PLAY_PADDING );
158
159
160   const ImageDimensions intImgSize = ResourceImage::GetImageSize(TEST_OUTER_RING_FILENAME);
161   Vector2 imgSize = Vector2( intImgSize.GetWidth(), intImgSize.GetHeight() );
162   Vector2 stageSize = stage.GetSize();
163   float scale = stageSize.width / imgSize.width;
164   float availableHeight = stageSize.height - DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight * 2.0f;
165   if(availableHeight <= stageSize.width)
166   {
167     scale = availableHeight / imgSize.width;
168   }
169
170   mRadialSweepView1 = CreateSweepView( TEST_OUTER_RING_FILENAME, Degree(-90.0f), Degree(-90.0f));
171   mRadialSweepView2 = CreateSweepView( TEST_INNER_RING_FILENAME, Degree(90.0f),  Degree(0.0f));
172   mRadialSweepView3 = CreateSweepView( TEST_MENU_FILENAME, Degree(100.0f), Degree(0.0f));
173   mRadialSweepView3.SetInitialActorAngle(Degree(-110));
174   mRadialSweepView3.SetFinalActorAngle(Degree(0));
175
176   Image dial = ResourceImage::New( TEST_DIAL_FILENAME );
177   mDialActor = ImageActor::New( dial );
178   mDialActor.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
179   mDialActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
180   mDialActor.SetScale(scale);
181   Layer dialLayer = Layer::New();
182
183   dialLayer.Add(mDialActor);
184   dialLayer.SetPositionInheritanceMode(USE_PARENT_POSITION);
185   dialLayer.SetSize(stage.GetSize());
186   mContents.Add(dialLayer);
187
188   mRadialSweepView1.SetScale(scale);
189   mRadialSweepView2.SetScale(scale);
190   mRadialSweepView3.SetScale(scale);
191
192   StartAnimation();
193 }
194
195 void RadialMenuExample::StartAnimation()
196 {
197   mDialActor.SetOpacity(0.0f);
198   mRadialSweepView1.SetOpacity(0.0f);
199   mAnimation = Animation::New(6.0f);
200   mRadialSweepView1.Activate(mAnimation, 0.0f, 3.0f);
201   mRadialSweepView2.Activate(mAnimation, 1.5f, 3.0f);
202   mRadialSweepView3.Activate(mAnimation, 3.0f, 3.0f);
203   mAnimation.AnimateTo( Property( mDialActor, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.8f ) );
204   mAnimation.AnimateTo( Property( mRadialSweepView1, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.5f ) );
205   mAnimation.FinishedSignal().Connect( this, &RadialMenuExample::OnAnimationFinished );
206
207   mAnimationState = PLAYING;
208   mAnimation.Play();
209 }
210
211 bool RadialMenuExample::OnButtonClicked( Toolkit::Button button )
212 {
213   switch( mAnimationState )
214   {
215     case PLAYING:
216     {
217       mAnimation.Pause();
218       mAnimationState = PAUSED;
219       mPlayStopButton.SetBackgroundImage( mIconPlay );
220     }
221     break;
222
223     case PAUSED:
224     {
225       mAnimation.Play();
226       mAnimationState = PLAYING;
227       mPlayStopButton.SetBackgroundImage( mIconStop );
228     }
229     break;
230
231     case STOPPED:
232     {
233       mPlayStopButton.SetBackgroundImage( mIconStop );
234       mRadialSweepView1.Deactivate();
235       mRadialSweepView2.Deactivate();
236       mRadialSweepView3.Deactivate();
237       StartAnimation();
238     }
239   }
240   return false;
241 }
242
243 void RadialMenuExample::OnAnimationFinished( Animation& source )
244 {
245   mAnimationState = STOPPED;
246   mPlayStopButton.SetBackgroundImage( mIconPlay );
247 }
248
249 RadialSweepView RadialMenuExample::CreateSweepView( std::string imageName,
250                                                     Degree initialAngle,
251                                                     Degree finalAngle)
252 {
253   // Create the image
254   Image image = ResourceImage::New(imageName);
255   mImageActor = ImageActor::New(image);
256   mImageActor.SetParentOrigin(ParentOrigin::CENTER);
257   mImageActor.SetAnchorPoint(AnchorPoint::CENTER);
258   mImageActor.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
259
260   // Create the stencil
261   const ImageDimensions imageSize = ResourceImage::GetImageSize(imageName);
262   float diameter = std::max(imageSize.GetWidth(), imageSize.GetHeight());
263   RadialSweepView radialSweepView = RadialSweepView::New();
264   radialSweepView.SetDiameter( diameter );
265   radialSweepView.SetInitialAngle( initialAngle );
266   radialSweepView.SetFinalAngle( finalAngle );
267   radialSweepView.SetInitialSector( Degree(0.0f) );
268   radialSweepView.SetFinalSector( Degree(359.999f) );
269   radialSweepView.SetSize( Stage::GetCurrent().GetSize());
270   radialSweepView.SetEasingFunction( Dali::AlphaFunction::EASE_IN_OUT );
271   radialSweepView.SetPositionInheritanceMode(USE_PARENT_POSITION);
272   mContents.Add(radialSweepView);
273   radialSweepView.Add( mImageActor );
274   mImageActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
275
276   return radialSweepView;
277 }
278
279
280 void RadialMenuExample::OnKeyEvent(const KeyEvent& event)
281 {
282   if(event.state == KeyEvent::Down)
283   {
284     if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
285     {
286       mApplication.Quit();
287     }
288   }
289 }
290
291 void RunTest(Application app)
292 {
293   RadialMenuExample test(app);
294
295   app.MainLoop();
296 }
297
298 // Entry point for Linux & Tizen applications
299 int main(int argc, char **argv)
300 {
301   Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);
302
303   RunTest(app);
304
305   return 0;
306 }