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