Removed background image define as it is not required
[platform/core/uifw/dali-demo.git] / examples / radial-menu / radial-menu-example.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 #include <dali/dali.h>
18 #include <dali-toolkit/dali-toolkit.h>
19 #include "../shared/view.h"
20 #include "radial-sweep-view.h"
21 #include "radial-sweep-view-impl.h"
22
23 using namespace Dali;
24 using namespace Dali::Toolkit;
25
26 namespace
27 {
28 const char* TEST_OUTER_RING_FILENAME = DALI_IMAGE_DIR "layer2.png"; // Image to be masked
29 const char* TEST_INNER_RING_FILENAME = DALI_IMAGE_DIR "layer1.png"; // Image to be masked
30 const char* TEST_MENU_FILENAME = DALI_IMAGE_DIR "layer3.png"; // Image to be masked
31 const char* TEST_DIAL_FILENAME = DALI_IMAGE_DIR "layer4.png"; // Image to be masked
32 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" ); // Background for toolbar
33 const char* APPLICATION_TITLE( "Radial Menu" );
34 const char * const PLAY_ICON( DALI_IMAGE_DIR "icon-play.png" );
35 const char * const STOP_ICON( DALI_IMAGE_DIR "icon-stop.png" );
36 }
37
38
39 /********************************************************************************
40  * Application controller class
41  */
42
43 // This example shows how to create a mesh actor for use as a stencil buffer
44 class RadialMenuExample : public ConnectionTracker
45 {
46 public:
47   /**
48    * Constructor
49    * @param[in] app The application handle
50    */
51   RadialMenuExample(Application app);
52
53   /**
54    * Destructor
55    */
56   ~RadialMenuExample();
57
58 private:
59
60   /**
61    * Initialization signal handler - all actor initialization should happen here
62    * @param[in] app The application handle
63    */
64   void OnInit(Application& app);
65
66   /**
67    * Create a sweep view with the given image and parameters
68    */
69   RadialSweepView CreateSweepView( std::string imageName, Degree initial, Degree final );
70
71   void StartAnimation();
72
73   bool OnButtonClicked( Toolkit::Button button );
74
75   void OnAnimationFinished( Animation& source );
76
77   /**
78    * Main key event handler
79    *
80    * @param[in] event The key event to respond to
81    */
82   void OnKeyEvent(const KeyEvent& event);
83
84 private: // Member variables
85   enum AnimState
86   {
87     STOPPED,
88     PAUSED,
89     PLAYING
90   };
91
92   Application     mApplication; ///< The application handle
93   Toolkit::View   mView;        ///< The toolbar view
94   Layer           mContents;    ///< The toolbar contents pane
95   ImageActor      mImageActor;  ///< Image actor shown by stencil mask
96   Animation       mAnimation;
97   AnimState       mAnimationState;
98
99   Image               mIconPlay;
100   Image               mIconStop;
101   Toolkit::PushButton mPlayStopButton;
102   ImageActor      mDialActor;
103   RadialSweepView mRadialSweepView1;
104   RadialSweepView mRadialSweepView2;
105   RadialSweepView mRadialSweepView3;
106 };
107
108 RadialMenuExample::RadialMenuExample(Application app)
109 : mApplication( app ),
110   mAnimationState(STOPPED)
111 {
112   // Connect to the Application's Init signal
113   app.InitSignal().Connect(this, &RadialMenuExample::OnInit);
114 }
115
116 RadialMenuExample::~RadialMenuExample()
117 {
118   // Nothing to do here; actor handles will clean up themselves.
119 }
120
121 void RadialMenuExample::OnInit(Application& app)
122 {
123   // The Init signal is received once (only) during the Application lifetime
124   Stage::GetCurrent().KeyEventSignal().Connect(this, &RadialMenuExample::OnKeyEvent);
125
126   // Create toolbar & view
127   Toolkit::ToolBar toolBar;
128   mContents = DemoHelper::CreateView( mApplication,
129                                       mView,
130                                       toolBar,
131                                       "",
132                                       TOOLBAR_IMAGE,
133                                       APPLICATION_TITLE );
134
135   mIconPlay = Image::New( PLAY_ICON );
136   mIconStop = Image::New( STOP_ICON );
137   mPlayStopButton = Toolkit::PushButton::New();
138   mPlayStopButton.SetBackgroundImage( mIconStop );
139
140   mPlayStopButton.ClickedSignal().Connect( this, &RadialMenuExample::OnButtonClicked );
141
142   toolBar.AddControl( mPlayStopButton,
143                       DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
144                       Toolkit::Alignment::HorizontalRight,
145                       DemoHelper::DEFAULT_PLAY_PADDING );
146
147   Vector2 imgSize = Image::GetImageSize(TEST_OUTER_RING_FILENAME);
148   float scale = Stage::GetCurrent().GetSize().width / imgSize.width;
149
150   mRadialSweepView1 = CreateSweepView( TEST_OUTER_RING_FILENAME, Degree(-90.0f), Degree(-90.0f));
151   mRadialSweepView2 = CreateSweepView( TEST_INNER_RING_FILENAME, Degree(90.0f),  Degree(0.0f));
152   mRadialSweepView3 = CreateSweepView( TEST_MENU_FILENAME, Degree(100.0f), Degree(0.0f));
153   mRadialSweepView3.SetInitialActorAngle(Degree(-110));
154   mRadialSweepView3.SetFinalActorAngle(Degree(0));
155
156   Image dial = Image::New( TEST_DIAL_FILENAME );
157   mDialActor = ImageActor::New( dial );
158   mDialActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
159   mDialActor.SetScale(scale);
160   Layer dialLayer = Layer::New();
161
162   dialLayer.Add(mDialActor);
163   dialLayer.SetPositionInheritanceMode(USE_PARENT_POSITION);
164   dialLayer.SetSize(Stage::GetCurrent().GetSize());
165   mContents.Add(dialLayer);
166
167   mRadialSweepView1.SetScale(scale);
168   mRadialSweepView2.SetScale(scale);
169   mRadialSweepView3.SetScale(scale);
170
171   StartAnimation();
172 }
173
174 void RadialMenuExample::StartAnimation()
175 {
176   mDialActor.SetOpacity(0.0f);
177   mRadialSweepView1.SetOpacity(0.0f);
178   mAnimation = Animation::New(6.0f);
179   mRadialSweepView1.Activate(mAnimation, 0.0f, 3.0f);
180   mRadialSweepView2.Activate(mAnimation, 1.5f, 3.0f);
181   mRadialSweepView3.Activate(mAnimation, 3.0f, 3.0f);
182   mAnimation.OpacityTo(mDialActor, 1.0f, AlphaFunctions::EaseIn, 0.0f, 0.8f);
183   mAnimation.OpacityTo(mRadialSweepView1, 1.0f, AlphaFunctions::EaseIn, 0.0f, 0.5f);
184   mAnimation.FinishedSignal().Connect( this, &RadialMenuExample::OnAnimationFinished );
185
186   mAnimationState = PLAYING;
187   mAnimation.Play();
188 }
189
190 bool RadialMenuExample::OnButtonClicked( Toolkit::Button button )
191 {
192   switch( mAnimationState )
193   {
194     case PLAYING:
195     {
196       mAnimation.Pause();
197       mPlayStopButton.SetBackgroundImage( mIconPlay );
198     }
199     break;
200
201     case PAUSED:
202     {
203       mAnimation.Play();
204       mPlayStopButton.SetBackgroundImage( mIconStop );
205     }
206     break;
207
208     case STOPPED:
209     {
210       mPlayStopButton.SetBackgroundImage( mIconStop );
211       mRadialSweepView1.Deactivate();
212       mRadialSweepView2.Deactivate();
213       mRadialSweepView3.Deactivate();
214       StartAnimation();
215     }
216   }
217   return false;
218 }
219
220 void RadialMenuExample::OnAnimationFinished( Animation& source )
221 {
222   mAnimationState = STOPPED;
223   mPlayStopButton.SetBackgroundImage( mIconPlay );
224 }
225
226 RadialSweepView RadialMenuExample::CreateSweepView( std::string imageName,
227                                                     Degree initialAngle,
228                                                     Degree finalAngle)
229 {
230   // Create the image
231   Image image = Image::New(imageName);
232   mImageActor = ImageActor::New(image);
233   mImageActor.SetParentOrigin(ParentOrigin::CENTER);
234   mImageActor.SetAnchorPoint(AnchorPoint::CENTER);
235
236   // Create the stencil
237   float diameter = std::max(image.GetWidth(), image.GetHeight());
238   RadialSweepView radialSweepView = RadialSweepView::New();
239   radialSweepView.SetDiameter( diameter );
240   radialSweepView.SetInitialAngle( initialAngle );
241   radialSweepView.SetFinalAngle( finalAngle );
242   radialSweepView.SetInitialSector( Degree(0.0f) );
243   radialSweepView.SetFinalSector( Degree(359.999f) );
244   radialSweepView.SetSize( Stage::GetCurrent().GetSize());
245   radialSweepView.SetEasingFunction( Dali::AlphaFunctions::EaseInOut );
246   radialSweepView.SetPositionInheritanceMode(USE_PARENT_POSITION);
247   mContents.Add(radialSweepView);
248   radialSweepView.Add( mImageActor );
249   mImageActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
250
251   return radialSweepView;
252 }
253
254
255 void RadialMenuExample::OnKeyEvent(const KeyEvent& event)
256 {
257   if(event.state == KeyEvent::Down)
258   {
259     if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
260     {
261       mApplication.Quit();
262     }
263   }
264 }
265
266 void RunTest(Application app)
267 {
268   RadialMenuExample test(app);
269
270   app.MainLoop();
271 }
272
273 // Entry point for Linux & Tizen applications
274 int main(int argc, char **argv)
275 {
276   Application app = Application::New(&argc, &argv);
277
278   RunTest(app);
279
280   return 0;
281 }