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