Changed some demo examples to use ImageView.
[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 PLAY_ICON_SELECTED( DALI_IMAGE_DIR "icon-play-selected.png" );
37 const char * const STOP_ICON( DALI_IMAGE_DIR "icon-stop.png" );
38 const char * const STOP_ICON_SELECTED( DALI_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   Image               mIconPlay;
112   Image               mIconPlaySelected;
113   Image               mIconStop;
114   Image               mIconStopSelected;
115   Toolkit::PushButton mPlayStopButton;
116   ImageView       mDialView;
117   RadialSweepView mRadialSweepView1;
118   RadialSweepView mRadialSweepView2;
119   RadialSweepView mRadialSweepView3;
120 };
121
122 RadialMenuExample::RadialMenuExample(Application app)
123 : mApplication( app ),
124   mAnimationState(STOPPED)
125 {
126   // Connect to the Application's Init signal
127   app.InitSignal().Connect(this, &RadialMenuExample::OnInit);
128 }
129
130 RadialMenuExample::~RadialMenuExample()
131 {
132   // Nothing to do here; actor handles will clean up themselves.
133 }
134
135 void RadialMenuExample::OnInit(Application& app)
136 {
137   Stage stage = Dali::Stage::GetCurrent();
138
139   // The Init signal is received once (only) during the Application lifetime
140   stage.KeyEventSignal().Connect(this, &RadialMenuExample::OnKeyEvent);
141
142   // Create toolbar & view
143   Toolkit::ToolBar toolBar;
144   mContents = DemoHelper::CreateView( mApplication,
145                                       mView,
146                                       toolBar,
147                                       "",
148                                       TOOLBAR_IMAGE,
149                                       APPLICATION_TITLE );
150
151   mIconPlay = ResourceImage::New( PLAY_ICON );
152   mIconPlaySelected = ResourceImage::New( PLAY_ICON_SELECTED );
153   mIconStop = ResourceImage::New( STOP_ICON );
154   mIconStopSelected = ResourceImage::New( STOP_ICON_SELECTED );
155   mPlayStopButton = Toolkit::PushButton::New();
156   mPlayStopButton.SetButtonImage( mIconStop );
157   mPlayStopButton.SetSelectedImage( mIconStopSelected );
158
159   mPlayStopButton.ClickedSignal().Connect( this, &RadialMenuExample::OnButtonClicked );
160
161   toolBar.AddControl( mPlayStopButton,
162                       DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
163                       Toolkit::Alignment::HorizontalRight,
164                       DemoHelper::DEFAULT_PLAY_PADDING );
165
166
167   const ImageDimensions intImgSize = ResourceImage::GetImageSize(TEST_OUTER_RING_FILENAME);
168   Vector2 imgSize = Vector2( intImgSize.GetWidth(), intImgSize.GetHeight() );
169   Vector2 stageSize = stage.GetSize();
170   float scale = stageSize.width / imgSize.width;
171   float availableHeight = stageSize.height - DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight * 2.0f;
172   if(availableHeight <= stageSize.width)
173   {
174     scale = availableHeight / imgSize.width;
175   }
176
177   mRadialSweepView1 = CreateSweepView( TEST_OUTER_RING_FILENAME, Degree(-90.0f), Degree(-90.0f));
178   mRadialSweepView2 = CreateSweepView( TEST_INNER_RING_FILENAME, Degree(90.0f),  Degree(0.0f));
179   mRadialSweepView3 = CreateSweepView( TEST_MENU_FILENAME, Degree(100.0f), Degree(0.0f));
180   mRadialSweepView3.SetInitialActorAngle(Degree(-110));
181   mRadialSweepView3.SetFinalActorAngle(Degree(0));
182
183   Image dial = ResourceImage::New( TEST_DIAL_FILENAME );
184   mDialView = ImageView::New( dial );
185   mDialView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
186   mDialView.SetPositionInheritanceMode(USE_PARENT_POSITION);
187   mDialView.SetScale(scale);
188   Layer dialLayer = Layer::New();
189
190   dialLayer.Add( mDialView );
191   dialLayer.SetPositionInheritanceMode(USE_PARENT_POSITION);
192   dialLayer.SetSize(stage.GetSize());
193   mContents.Add(dialLayer);
194
195   mRadialSweepView1.SetScale(scale);
196   mRadialSweepView2.SetScale(scale);
197   mRadialSweepView3.SetScale(scale);
198
199   StartAnimation();
200 }
201
202 void RadialMenuExample::StartAnimation()
203 {
204   mDialView.SetOpacity(0.0f);
205   mRadialSweepView1.SetOpacity(0.0f);
206   mAnimation = Animation::New(6.0f);
207   mRadialSweepView1.Activate(mAnimation, 0.0f, 3.0f);
208   mRadialSweepView2.Activate(mAnimation, 1.5f, 3.0f);
209   mRadialSweepView3.Activate(mAnimation, 3.0f, 3.0f);
210   mAnimation.AnimateTo( Property( mDialView, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.8f ) );
211   mAnimation.AnimateTo( Property( mRadialSweepView1, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.5f ) );
212   mAnimation.FinishedSignal().Connect( this, &RadialMenuExample::OnAnimationFinished );
213
214   mAnimationState = PLAYING;
215   mAnimation.Play();
216 }
217
218 bool RadialMenuExample::OnButtonClicked( Toolkit::Button button )
219 {
220   switch( mAnimationState )
221   {
222     case PLAYING:
223     {
224       mAnimation.Pause();
225       mAnimationState = PAUSED;
226       mPlayStopButton.SetButtonImage( mIconPlay );
227       mPlayStopButton.SetSelectedImage( mIconPlaySelected );
228     }
229     break;
230
231     case PAUSED:
232     {
233       mAnimation.Play();
234       mAnimationState = PLAYING;
235       mPlayStopButton.SetButtonImage( mIconStop );
236       mPlayStopButton.SetSelectedImage( mIconStopSelected );
237     }
238     break;
239
240     case STOPPED:
241     {
242       mPlayStopButton.SetButtonImage( mIconStop );
243       mPlayStopButton.SetSelectedImage( mIconStopSelected );
244       mRadialSweepView1.Deactivate();
245       mRadialSweepView2.Deactivate();
246       mRadialSweepView3.Deactivate();
247       StartAnimation();
248     }
249   }
250   return false;
251 }
252
253 void RadialMenuExample::OnAnimationFinished( Animation& source )
254 {
255   mAnimationState = STOPPED;
256   mPlayStopButton.SetButtonImage( mIconPlay );
257   mPlayStopButton.SetSelectedImage( mIconPlaySelected );
258 }
259
260 RadialSweepView RadialMenuExample::CreateSweepView( std::string imageName,
261                                                     Degree initialAngle,
262                                                     Degree finalAngle)
263 {
264   // Create the image
265   Image image = ResourceImage::New(imageName);
266   mImageView = ImageView::New(image);
267   mImageView.SetParentOrigin(ParentOrigin::CENTER);
268   mImageView.SetAnchorPoint(AnchorPoint::CENTER);
269   mImageView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
270
271   // Create the stencil
272   const ImageDimensions imageSize = ResourceImage::GetImageSize(imageName);
273   float diameter = std::max(imageSize.GetWidth(), imageSize.GetHeight());
274   RadialSweepView radialSweepView = RadialSweepView::New();
275   radialSweepView.SetDiameter( diameter );
276   radialSweepView.SetInitialAngle( initialAngle );
277   radialSweepView.SetFinalAngle( finalAngle );
278   radialSweepView.SetInitialSector( Degree(0.0f) );
279   radialSweepView.SetFinalSector( Degree(359.999f) );
280   radialSweepView.SetSize( Stage::GetCurrent().GetSize());
281   radialSweepView.SetEasingFunction( Dali::AlphaFunction::EASE_IN_OUT );
282   radialSweepView.SetPositionInheritanceMode(USE_PARENT_POSITION);
283   mContents.Add(radialSweepView);
284   radialSweepView.Add( mImageView );
285   mImageView.SetPositionInheritanceMode(USE_PARENT_POSITION);
286
287   return radialSweepView;
288 }
289
290
291 void RadialMenuExample::OnKeyEvent(const KeyEvent& event)
292 {
293   if(event.state == KeyEvent::Down)
294   {
295     if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
296     {
297       mApplication.Quit();
298     }
299   }
300 }
301
302 void RunTest(Application app)
303 {
304   RadialMenuExample test(app);
305
306   app.MainLoop();
307 }
308
309 // Entry point for Linux & Tizen applications
310 int main(int argc, char **argv)
311 {
312   Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);
313
314   RunTest(app);
315
316   return 0;
317 }