update demos with ResourceImage/Image split
[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 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   void StartAnimation();
73
74   bool OnButtonClicked( Toolkit::Button button );
75
76   void OnAnimationFinished( Animation& source );
77
78   /**
79    * Main key event handler
80    *
81    * @param[in] event The key event to respond to
82    */
83   void OnKeyEvent(const KeyEvent& event);
84
85 private: // Member variables
86   enum AnimState
87   {
88     STOPPED,
89     PAUSED,
90     PLAYING
91   };
92
93   Application     mApplication; ///< The application handle
94   Toolkit::View   mView;        ///< The toolbar view
95   Layer           mContents;    ///< The toolbar contents pane
96   ImageActor      mImageActor;  ///< Image actor shown by stencil mask
97   Animation       mAnimation;
98   AnimState       mAnimationState;
99
100   Image               mIconPlay;
101   Image               mIconStop;
102   Toolkit::PushButton mPlayStopButton;
103   ImageActor      mDialActor;
104   RadialSweepView mRadialSweepView1;
105   RadialSweepView mRadialSweepView2;
106   RadialSweepView mRadialSweepView3;
107 };
108
109 RadialMenuExample::RadialMenuExample(Application app)
110 : mApplication( app ),
111   mAnimationState(STOPPED)
112 {
113   // Connect to the Application's Init signal
114   app.InitSignal().Connect(this, &RadialMenuExample::OnInit);
115 }
116
117 RadialMenuExample::~RadialMenuExample()
118 {
119   // Nothing to do here; actor handles will clean up themselves.
120 }
121
122 void RadialMenuExample::OnInit(Application& app)
123 {
124   Stage stage = Dali::Stage::GetCurrent();
125
126   // The Init signal is received once (only) during the Application lifetime
127   stage.KeyEventSignal().Connect(this, &RadialMenuExample::OnKeyEvent);
128
129   // Create toolbar & view
130   Toolkit::ToolBar toolBar;
131   mContents = DemoHelper::CreateView( mApplication,
132                                       mView,
133                                       toolBar,
134                                       "",
135                                       TOOLBAR_IMAGE,
136                                       APPLICATION_TITLE );
137
138   mIconPlay = ResourceImage::New( PLAY_ICON );
139   mIconStop = ResourceImage::New( STOP_ICON );
140   mPlayStopButton = Toolkit::PushButton::New();
141   mPlayStopButton.SetBackgroundImage( mIconStop );
142
143   mPlayStopButton.ClickedSignal().Connect( this, &RadialMenuExample::OnButtonClicked );
144
145   toolBar.AddControl( mPlayStopButton,
146                       DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
147                       Toolkit::Alignment::HorizontalRight,
148                       DemoHelper::DEFAULT_PLAY_PADDING );
149
150   Vector2 imgSize = ResourceImage::GetImageSize(TEST_OUTER_RING_FILENAME);
151   Vector2 stageSize = stage.GetSize();
152   float minStageDimension = std::min(stageSize.width, stageSize.height);
153
154   if(stageSize.height <= stageSize.width)
155   {
156     minStageDimension -= DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight * 2.0f;
157   }
158   float scale = minStageDimension / imgSize.width;
159
160   mRadialSweepView1 = CreateSweepView( TEST_OUTER_RING_FILENAME, Degree(-90.0f), Degree(-90.0f));
161   mRadialSweepView2 = CreateSweepView( TEST_INNER_RING_FILENAME, Degree(90.0f),  Degree(0.0f));
162   mRadialSweepView3 = CreateSweepView( TEST_MENU_FILENAME, Degree(100.0f), Degree(0.0f));
163   mRadialSweepView3.SetInitialActorAngle(Degree(-110));
164   mRadialSweepView3.SetFinalActorAngle(Degree(0));
165
166   Image dial = ResourceImage::New( TEST_DIAL_FILENAME );
167   mDialActor = ImageActor::New( dial );
168   mDialActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
169   mDialActor.SetScale(scale);
170   Layer dialLayer = Layer::New();
171
172   dialLayer.Add(mDialActor);
173   dialLayer.SetPositionInheritanceMode(USE_PARENT_POSITION);
174   dialLayer.SetSize(stage.GetSize());
175   mContents.Add(dialLayer);
176
177   mRadialSweepView1.SetScale(scale);
178   mRadialSweepView2.SetScale(scale);
179   mRadialSweepView3.SetScale(scale);
180
181   StartAnimation();
182 }
183
184 void RadialMenuExample::StartAnimation()
185 {
186   mDialActor.SetOpacity(0.0f);
187   mRadialSweepView1.SetOpacity(0.0f);
188   mAnimation = Animation::New(6.0f);
189   mRadialSweepView1.Activate(mAnimation, 0.0f, 3.0f);
190   mRadialSweepView2.Activate(mAnimation, 1.5f, 3.0f);
191   mRadialSweepView3.Activate(mAnimation, 3.0f, 3.0f);
192   mAnimation.OpacityTo(mDialActor, 1.0f, AlphaFunctions::EaseIn, 0.0f, 0.8f);
193   mAnimation.OpacityTo(mRadialSweepView1, 1.0f, AlphaFunctions::EaseIn, 0.0f, 0.5f);
194   mAnimation.FinishedSignal().Connect( this, &RadialMenuExample::OnAnimationFinished );
195
196   mAnimationState = PLAYING;
197   mAnimation.Play();
198 }
199
200 bool RadialMenuExample::OnButtonClicked( Toolkit::Button button )
201 {
202   switch( mAnimationState )
203   {
204     case PLAYING:
205     {
206       mAnimation.Pause();
207       mPlayStopButton.SetBackgroundImage( mIconPlay );
208     }
209     break;
210
211     case PAUSED:
212     {
213       mAnimation.Play();
214       mPlayStopButton.SetBackgroundImage( mIconStop );
215     }
216     break;
217
218     case STOPPED:
219     {
220       mPlayStopButton.SetBackgroundImage( mIconStop );
221       mRadialSweepView1.Deactivate();
222       mRadialSweepView2.Deactivate();
223       mRadialSweepView3.Deactivate();
224       StartAnimation();
225     }
226   }
227   return false;
228 }
229
230 void RadialMenuExample::OnAnimationFinished( Animation& source )
231 {
232   mAnimationState = STOPPED;
233   mPlayStopButton.SetBackgroundImage( mIconPlay );
234 }
235
236 RadialSweepView RadialMenuExample::CreateSweepView( std::string imageName,
237                                                     Degree initialAngle,
238                                                     Degree finalAngle)
239 {
240   // Create the image
241   Image image = ResourceImage::New(imageName);
242   mImageActor = ImageActor::New(image);
243   mImageActor.SetParentOrigin(ParentOrigin::CENTER);
244   mImageActor.SetAnchorPoint(AnchorPoint::CENTER);
245
246   // Create the stencil
247   Vector2 imageSize = ResourceImage::GetImageSize(imageName);
248   float diameter = std::max(imageSize.width, imageSize.height);
249   RadialSweepView radialSweepView = RadialSweepView::New();
250   radialSweepView.SetDiameter( diameter );
251   radialSweepView.SetInitialAngle( initialAngle );
252   radialSweepView.SetFinalAngle( finalAngle );
253   radialSweepView.SetInitialSector( Degree(0.0f) );
254   radialSweepView.SetFinalSector( Degree(359.999f) );
255   radialSweepView.SetSize( Stage::GetCurrent().GetSize());
256   radialSweepView.SetEasingFunction( Dali::AlphaFunctions::EaseInOut );
257   radialSweepView.SetPositionInheritanceMode(USE_PARENT_POSITION);
258   mContents.Add(radialSweepView);
259   radialSweepView.Add( mImageActor );
260   mImageActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
261
262   return radialSweepView;
263 }
264
265
266 void RadialMenuExample::OnKeyEvent(const KeyEvent& event)
267 {
268   if(event.state == KeyEvent::Down)
269   {
270     if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
271     {
272       mApplication.Quit();
273     }
274   }
275 }
276
277 void RunTest(Application app)
278 {
279   RadialMenuExample test(app);
280
281   app.MainLoop();
282 }
283
284 // Entry point for Linux & Tizen applications
285 int main(int argc, char **argv)
286 {
287   Application app = Application::New(&argc, &argv);
288
289   RunTest(app);
290
291   return 0;
292 }