2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
25 using namespace Dali::Toolkit;
29 const char* TEST_OUTER_RING_FILENAME = DEMO_IMAGE_DIR "layer2.png"; // Image to be masked
30 const char* TEST_INNER_RING_FILENAME = DEMO_IMAGE_DIR "layer1.png"; // Image to be masked
31 const char* TEST_MENU_FILENAME = DEMO_IMAGE_DIR "layer3.png"; // Image to be masked
32 const char* TEST_DIAL_FILENAME = DEMO_IMAGE_DIR "layer4.png"; // Image to be masked
33 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" ); // Background for toolbar
34 const char* APPLICATION_TITLE( "Radial Menu" );
35 const char * const PLAY_ICON( DEMO_IMAGE_DIR "icon-play.png" );
36 const char * const PLAY_ICON_SELECTED( DEMO_IMAGE_DIR "icon-play-selected.png" );
37 const char * const STOP_ICON( DEMO_IMAGE_DIR "icon-stop.png" );
38 const char * const STOP_ICON_SELECTED( DEMO_IMAGE_DIR "icon-stop-selected.png" );
42 /********************************************************************************
43 * Application controller class
46 // This example shows how to create a mesh actor for use as a stencil buffer
47 class RadialMenuExample : public ConnectionTracker
52 * @param[in] app The application handle
54 RadialMenuExample(Application app);
64 * Initialization signal handler - all actor initialization should happen here
65 * @param[in] app The application handle
67 void OnInit(Application& app);
70 * Create a sweep view with the given image and parameters
72 RadialSweepView CreateSweepView( std::string imageName, Degree initial, Degree final );
75 * Start the sweep animation on the menu
77 void StartAnimation();
80 * Play or pause the animation when the button is clicked
82 bool OnButtonClicked( Toolkit::Button button );
85 * Update the state flag and change the button icon when the animation is finished
87 void OnAnimationFinished( Animation& source );
90 * Main key event handler
92 * @param[in] event The key event to respond to
94 void OnKeyEvent(const KeyEvent& event);
96 private: // Member variables
104 Application mApplication; ///< The application handle
105 Toolkit::Control mView; ///< The toolbar view
106 Layer mContents; ///< The toolbar contents pane
107 ImageView mImageView; ///< Image view shown by stencil mask
108 Animation mAnimation;
109 AnimState mAnimationState;
111 Toolkit::PushButton mPlayStopButton;
113 RadialSweepView mRadialSweepView1;
114 RadialSweepView mRadialSweepView2;
115 RadialSweepView mRadialSweepView3;
118 RadialMenuExample::RadialMenuExample(Application app)
119 : mApplication( app ),
120 mAnimationState(STOPPED)
122 // Connect to the Application's Init signal
123 app.InitSignal().Connect(this, &RadialMenuExample::OnInit);
126 RadialMenuExample::~RadialMenuExample()
128 // Nothing to do here; actor handles will clean up themselves.
131 void RadialMenuExample::OnInit(Application& app)
133 Stage stage = Dali::Stage::GetCurrent();
135 // The Init signal is received once (only) during the Application lifetime
136 stage.KeyEventSignal().Connect(this, &RadialMenuExample::OnKeyEvent);
138 // Create toolbar & view
139 Toolkit::ToolBar toolBar;
140 mContents = DemoHelper::CreateView( mApplication,
147 mPlayStopButton = Toolkit::PushButton::New();
148 mPlayStopButton.SetUnselectedImage( STOP_ICON );
149 mPlayStopButton.SetSelectedImage( STOP_ICON_SELECTED );
151 mPlayStopButton.ClickedSignal().Connect( this, &RadialMenuExample::OnButtonClicked );
153 toolBar.AddControl( mPlayStopButton,
154 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
155 Toolkit::Alignment::HorizontalRight,
156 DemoHelper::DEFAULT_PLAY_PADDING );
159 const ImageDimensions intImgSize = ResourceImage::GetImageSize(TEST_OUTER_RING_FILENAME);
160 Vector2 imgSize = Vector2( intImgSize.GetWidth(), intImgSize.GetHeight() );
161 Vector2 stageSize = stage.GetSize();
162 float scale = stageSize.width / imgSize.width;
163 float availableHeight = stageSize.height - DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight * 2.0f;
164 if(availableHeight <= stageSize.width)
166 scale = availableHeight / imgSize.width;
169 mRadialSweepView1 = CreateSweepView( TEST_OUTER_RING_FILENAME, Degree(-90.0f), Degree(-90.0f));
170 mRadialSweepView2 = CreateSweepView( TEST_INNER_RING_FILENAME, Degree(90.0f), Degree(0.0f));
171 mRadialSweepView3 = CreateSweepView( TEST_MENU_FILENAME, Degree(100.0f), Degree(0.0f));
172 mRadialSweepView3.SetInitialActorAngle(Degree(-110));
173 mRadialSweepView3.SetFinalActorAngle(Degree(0));
175 mDialView = ImageView::New( TEST_DIAL_FILENAME );
176 mDialView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
177 mDialView.SetParentOrigin( ParentOrigin::CENTER );
178 mDialView.SetScale(scale);
179 Layer dialLayer = Layer::New();
181 dialLayer.Add( mDialView );
182 dialLayer.SetParentOrigin( ParentOrigin::CENTER );
183 dialLayer.SetSize(stage.GetSize());
184 mContents.Add(dialLayer);
186 mRadialSweepView1.SetScale(scale);
187 mRadialSweepView2.SetScale(scale);
188 mRadialSweepView3.SetScale(scale);
193 void RadialMenuExample::StartAnimation()
195 mDialView.SetOpacity(0.0f);
196 mRadialSweepView1.SetOpacity(0.0f);
197 mAnimation = Animation::New(6.0f);
198 mRadialSweepView1.Activate(mAnimation, 0.0f, 3.0f);
199 mRadialSweepView2.Activate(mAnimation, 1.5f, 3.0f);
200 mRadialSweepView3.Activate(mAnimation, 3.0f, 3.0f);
201 mAnimation.AnimateTo( Property( mDialView, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.8f ) );
202 mAnimation.AnimateTo( Property( mRadialSweepView1, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.5f ) );
203 mAnimation.FinishedSignal().Connect( this, &RadialMenuExample::OnAnimationFinished );
205 mAnimationState = PLAYING;
209 bool RadialMenuExample::OnButtonClicked( Toolkit::Button button )
211 switch( mAnimationState )
216 mAnimationState = PAUSED;
217 mPlayStopButton.SetUnselectedImage( PLAY_ICON );
218 mPlayStopButton.SetSelectedImage( PLAY_ICON_SELECTED );
225 mAnimationState = PLAYING;
226 mPlayStopButton.SetUnselectedImage( STOP_ICON );
227 mPlayStopButton.SetSelectedImage( STOP_ICON_SELECTED );
233 mPlayStopButton.SetUnselectedImage( STOP_ICON );
234 mPlayStopButton.SetSelectedImage( STOP_ICON_SELECTED );
235 mRadialSweepView1.Deactivate();
236 mRadialSweepView2.Deactivate();
237 mRadialSweepView3.Deactivate();
244 void RadialMenuExample::OnAnimationFinished( Animation& source )
246 mAnimationState = STOPPED;
247 mPlayStopButton.SetUnselectedImage( PLAY_ICON );
248 mPlayStopButton.SetSelectedImage( PLAY_ICON_SELECTED );
251 RadialSweepView RadialMenuExample::CreateSweepView( std::string imageName,
256 mImageView = ImageView::New(imageName);
257 mImageView.SetParentOrigin(ParentOrigin::CENTER);
258 mImageView.SetAnchorPoint(AnchorPoint::CENTER);
259 mImageView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
261 // Create the stencil
262 const ImageDimensions imageSize = ResourceImage::GetImageSize(imageName);
263 float diameter = std::max(imageSize.GetWidth(), imageSize.GetHeight());
264 RadialSweepView radialSweepView = RadialSweepView::New();
265 radialSweepView.SetDiameter( diameter );
266 radialSweepView.SetInitialAngle( initialAngle );
267 radialSweepView.SetFinalAngle( finalAngle );
268 radialSweepView.SetInitialSector( Degree(0.0f) );
269 radialSweepView.SetFinalSector( Degree(359.999f) );
270 radialSweepView.SetSize( Stage::GetCurrent().GetSize());
271 radialSweepView.SetEasingFunction( Dali::AlphaFunction::EASE_IN_OUT );
272 radialSweepView.SetParentOrigin( ParentOrigin::CENTER );
273 mContents.Add(radialSweepView);
274 radialSweepView.Add( mImageView );
275 mImageView.SetParentOrigin( ParentOrigin::CENTER );
277 return radialSweepView;
281 void RadialMenuExample::OnKeyEvent(const KeyEvent& event)
283 if(event.state == KeyEvent::Down)
285 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
292 void RunTest(Application app)
294 RadialMenuExample test(app);
299 // Entry point for Linux & Tizen applications
300 int DALI_EXPORT_API main(int argc, char **argv)
302 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);