Update refraction-effect & radial-menu to use 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   DemoHelper::RequestThemeChange();
134
135   Stage stage = Dali::Stage::GetCurrent();
136
137   // The Init signal is received once (only) during the Application lifetime
138   stage.KeyEventSignal().Connect(this, &RadialMenuExample::OnKeyEvent);
139
140   // Create toolbar & view
141   Toolkit::ToolBar toolBar;
142   mContents = DemoHelper::CreateView( mApplication,
143                                       mView,
144                                       toolBar,
145                                       "",
146                                       TOOLBAR_IMAGE,
147                                       APPLICATION_TITLE );
148
149   mIconPlay = ResourceImage::New( PLAY_ICON );
150   mIconStop = ResourceImage::New( STOP_ICON );
151   mPlayStopButton = Toolkit::PushButton::New();
152   mPlayStopButton.SetBackgroundImage( mIconStop );
153
154   mPlayStopButton.ClickedSignal().Connect( this, &RadialMenuExample::OnButtonClicked );
155
156   toolBar.AddControl( mPlayStopButton,
157                       DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
158                       Toolkit::Alignment::HorizontalRight,
159                       DemoHelper::DEFAULT_PLAY_PADDING );
160
161
162   const ImageDimensions intImgSize = ResourceImage::GetImageSize(TEST_OUTER_RING_FILENAME);
163   Vector2 imgSize = Vector2( intImgSize.GetWidth(), intImgSize.GetHeight() );
164   Vector2 stageSize = stage.GetSize();
165   float scale = stageSize.width / imgSize.width;
166   float availableHeight = stageSize.height - DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight * 2.0f;
167   if(availableHeight <= stageSize.width)
168   {
169     scale = availableHeight / imgSize.width;
170   }
171
172   mRadialSweepView1 = CreateSweepView( TEST_OUTER_RING_FILENAME, Degree(-90.0f), Degree(-90.0f));
173   mRadialSweepView2 = CreateSweepView( TEST_INNER_RING_FILENAME, Degree(90.0f),  Degree(0.0f));
174   mRadialSweepView3 = CreateSweepView( TEST_MENU_FILENAME, Degree(100.0f), Degree(0.0f));
175   mRadialSweepView3.SetInitialActorAngle(Degree(-110));
176   mRadialSweepView3.SetFinalActorAngle(Degree(0));
177
178   Image dial = ResourceImage::New( TEST_DIAL_FILENAME );
179   mDialActor = ImageActor::New( dial );
180   mDialActor.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
181   mDialActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
182   mDialActor.SetScale(scale);
183   Layer dialLayer = Layer::New();
184
185   dialLayer.Add(mDialActor);
186   dialLayer.SetPositionInheritanceMode(USE_PARENT_POSITION);
187   dialLayer.SetSize(stage.GetSize());
188   mContents.Add(dialLayer);
189
190   mRadialSweepView1.SetScale(scale);
191   mRadialSweepView2.SetScale(scale);
192   mRadialSweepView3.SetScale(scale);
193
194   StartAnimation();
195 }
196
197 void RadialMenuExample::StartAnimation()
198 {
199   mDialActor.SetOpacity(0.0f);
200   mRadialSweepView1.SetOpacity(0.0f);
201   mAnimation = Animation::New(6.0f);
202   mRadialSweepView1.Activate(mAnimation, 0.0f, 3.0f);
203   mRadialSweepView2.Activate(mAnimation, 1.5f, 3.0f);
204   mRadialSweepView3.Activate(mAnimation, 3.0f, 3.0f);
205   mAnimation.AnimateTo( Property( mDialActor, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.8f ) );
206   mAnimation.AnimateTo( Property( mRadialSweepView1, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.5f ) );
207   mAnimation.FinishedSignal().Connect( this, &RadialMenuExample::OnAnimationFinished );
208
209   mAnimationState = PLAYING;
210   mAnimation.Play();
211 }
212
213 bool RadialMenuExample::OnButtonClicked( Toolkit::Button button )
214 {
215   switch( mAnimationState )
216   {
217     case PLAYING:
218     {
219       mAnimation.Pause();
220       mAnimationState = PAUSED;
221       mPlayStopButton.SetBackgroundImage( mIconPlay );
222     }
223     break;
224
225     case PAUSED:
226     {
227       mAnimation.Play();
228       mAnimationState = PLAYING;
229       mPlayStopButton.SetBackgroundImage( mIconStop );
230     }
231     break;
232
233     case STOPPED:
234     {
235       mPlayStopButton.SetBackgroundImage( mIconStop );
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.SetBackgroundImage( mIconPlay );
249 }
250
251 RadialSweepView RadialMenuExample::CreateSweepView( std::string imageName,
252                                                     Degree initialAngle,
253                                                     Degree finalAngle)
254 {
255   // Create the image
256   Image image = ResourceImage::New(imageName);
257   mImageActor = ImageActor::New(image);
258   mImageActor.SetParentOrigin(ParentOrigin::CENTER);
259   mImageActor.SetAnchorPoint(AnchorPoint::CENTER);
260   mImageActor.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
261
262   // Create the stencil
263   const ImageDimensions imageSize = ResourceImage::GetImageSize(imageName);
264   float diameter = std::max(imageSize.GetWidth(), imageSize.GetHeight());
265   RadialSweepView radialSweepView = RadialSweepView::New();
266   radialSweepView.SetDiameter( diameter );
267   radialSweepView.SetInitialAngle( initialAngle );
268   radialSweepView.SetFinalAngle( finalAngle );
269   radialSweepView.SetInitialSector( Degree(0.0f) );
270   radialSweepView.SetFinalSector( Degree(359.999f) );
271   radialSweepView.SetSize( Stage::GetCurrent().GetSize());
272   radialSweepView.SetEasingFunction( Dali::AlphaFunction::EASE_IN_OUT );
273   radialSweepView.SetPositionInheritanceMode(USE_PARENT_POSITION);
274   mContents.Add(radialSweepView);
275   radialSweepView.Add( mImageActor );
276   mImageActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
277
278   return radialSweepView;
279 }
280
281
282 void RadialMenuExample::OnKeyEvent(const KeyEvent& event)
283 {
284   if(event.state == KeyEvent::Down)
285   {
286     if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
287     {
288       mApplication.Quit();
289     }
290   }
291 }
292
293 void RunTest(Application app)
294 {
295   RadialMenuExample test(app);
296
297   app.MainLoop();
298 }
299
300 // Entry point for Linux & Tizen applications
301 int main(int argc, char **argv)
302 {
303   Application app = Application::New(&argc, &argv);
304
305   RunTest(app);
306
307   return 0;
308 }