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