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 = 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" );
40 /********************************************************************************
41 * Application controller class
44 // This example shows how to create a mesh actor for use as a stencil buffer
45 class RadialMenuExample : public ConnectionTracker
50 * @param[in] app The application handle
52 RadialMenuExample(Application app);
62 * Initialization signal handler - all actor initialization should happen here
63 * @param[in] app The application handle
65 void OnInit(Application& app);
68 * Create a sweep view with the given image and parameters
70 RadialSweepView CreateSweepView( std::string imageName, Degree initial, Degree final );
73 * Start the sweep animation on the menu
75 void StartAnimation();
78 * Play or pause the animation when the button is clicked
80 bool OnButtonClicked( Toolkit::Button button );
83 * Update the state flag and change the button icon when the animation is finished
85 void OnAnimationFinished( Animation& source );
88 * Main key event handler
90 * @param[in] event The key event to respond to
92 void OnKeyEvent(const KeyEvent& event);
94 private: // Member variables
102 Application mApplication; ///< The application handle
103 Toolkit::Control mView; ///< The toolbar view
104 Layer mContents; ///< The toolbar contents pane
105 ImageActor mImageActor; ///< Image actor shown by stencil mask
106 Animation mAnimation;
107 AnimState mAnimationState;
111 Toolkit::PushButton mPlayStopButton;
112 ImageActor mDialActor;
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 DemoHelper::RequestThemeChange();
135 Stage stage = Dali::Stage::GetCurrent();
137 // The Init signal is received once (only) during the Application lifetime
138 stage.KeyEventSignal().Connect(this, &RadialMenuExample::OnKeyEvent);
140 // Create toolbar & view
141 Toolkit::ToolBar toolBar;
142 mContents = DemoHelper::CreateView( mApplication,
149 mIconPlay = ResourceImage::New( PLAY_ICON );
150 mIconStop = ResourceImage::New( STOP_ICON );
151 mPlayStopButton = Toolkit::PushButton::New();
152 mPlayStopButton.SetBackgroundImage( mIconStop );
154 mPlayStopButton.ClickedSignal().Connect( this, &RadialMenuExample::OnButtonClicked );
156 toolBar.AddControl( mPlayStopButton,
157 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
158 Toolkit::Alignment::HorizontalRight,
159 DemoHelper::DEFAULT_PLAY_PADDING );
162 const ImageDimensions intImgSize = ResourceImage::GetImageSize(TEST_OUTER_RING_FILENAME);
163 Vector2 imgSize = Vector2( intImgSize.GetWidth(), intImgSize.GetHeight() );
164 Vector2 stageSize = stage.GetSize();
165 float scale = stageSize.width / imgSize.width;
166 float availableHeight = stageSize.height - DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight * 2.0f;
167 if(availableHeight <= stageSize.width)
169 scale = availableHeight / imgSize.width;
172 mRadialSweepView1 = CreateSweepView( TEST_OUTER_RING_FILENAME, Degree(-90.0f), Degree(-90.0f));
173 mRadialSweepView2 = CreateSweepView( TEST_INNER_RING_FILENAME, Degree(90.0f), Degree(0.0f));
174 mRadialSweepView3 = CreateSweepView( TEST_MENU_FILENAME, Degree(100.0f), Degree(0.0f));
175 mRadialSweepView3.SetInitialActorAngle(Degree(-110));
176 mRadialSweepView3.SetFinalActorAngle(Degree(0));
178 Image dial = ResourceImage::New( TEST_DIAL_FILENAME );
179 mDialActor = ImageActor::New( dial );
180 mDialActor.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
181 mDialActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
182 mDialActor.SetScale(scale);
183 Layer dialLayer = Layer::New();
185 dialLayer.Add(mDialActor);
186 dialLayer.SetPositionInheritanceMode(USE_PARENT_POSITION);
187 dialLayer.SetSize(stage.GetSize());
188 mContents.Add(dialLayer);
190 mRadialSweepView1.SetScale(scale);
191 mRadialSweepView2.SetScale(scale);
192 mRadialSweepView3.SetScale(scale);
197 void RadialMenuExample::StartAnimation()
199 mDialActor.SetOpacity(0.0f);
200 mRadialSweepView1.SetOpacity(0.0f);
201 mAnimation = Animation::New(6.0f);
202 mRadialSweepView1.Activate(mAnimation, 0.0f, 3.0f);
203 mRadialSweepView2.Activate(mAnimation, 1.5f, 3.0f);
204 mRadialSweepView3.Activate(mAnimation, 3.0f, 3.0f);
205 mAnimation.AnimateTo( Property( mDialActor, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.8f ) );
206 mAnimation.AnimateTo( Property( mRadialSweepView1, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.5f ) );
207 mAnimation.FinishedSignal().Connect( this, &RadialMenuExample::OnAnimationFinished );
209 mAnimationState = PLAYING;
213 bool RadialMenuExample::OnButtonClicked( Toolkit::Button button )
215 switch( mAnimationState )
220 mAnimationState = PAUSED;
221 mPlayStopButton.SetBackgroundImage( mIconPlay );
228 mAnimationState = PLAYING;
229 mPlayStopButton.SetBackgroundImage( mIconStop );
235 mPlayStopButton.SetBackgroundImage( mIconStop );
236 mRadialSweepView1.Deactivate();
237 mRadialSweepView2.Deactivate();
238 mRadialSweepView3.Deactivate();
245 void RadialMenuExample::OnAnimationFinished( Animation& source )
247 mAnimationState = STOPPED;
248 mPlayStopButton.SetBackgroundImage( mIconPlay );
251 RadialSweepView RadialMenuExample::CreateSweepView( std::string imageName,
256 Image image = ResourceImage::New(imageName);
257 mImageActor = ImageActor::New(image);
258 mImageActor.SetParentOrigin(ParentOrigin::CENTER);
259 mImageActor.SetAnchorPoint(AnchorPoint::CENTER);
260 mImageActor.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
262 // Create the stencil
263 const ImageDimensions imageSize = ResourceImage::GetImageSize(imageName);
264 float diameter = std::max(imageSize.GetWidth(), imageSize.GetHeight());
265 RadialSweepView radialSweepView = RadialSweepView::New();
266 radialSweepView.SetDiameter( diameter );
267 radialSweepView.SetInitialAngle( initialAngle );
268 radialSweepView.SetFinalAngle( finalAngle );
269 radialSweepView.SetInitialSector( Degree(0.0f) );
270 radialSweepView.SetFinalSector( Degree(359.999f) );
271 radialSweepView.SetSize( Stage::GetCurrent().GetSize());
272 radialSweepView.SetEasingFunction( Dali::AlphaFunction::EASE_IN_OUT );
273 radialSweepView.SetPositionInheritanceMode(USE_PARENT_POSITION);
274 mContents.Add(radialSweepView);
275 radialSweepView.Add( mImageActor );
276 mImageActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
278 return radialSweepView;
282 void RadialMenuExample::OnKeyEvent(const KeyEvent& event)
284 if(event.state == KeyEvent::Down)
286 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
293 void RunTest(Application app)
295 RadialMenuExample test(app);
300 // Entry point for Linux & Tizen applications
301 int main(int argc, char **argv)
303 Application app = Application::New(&argc, &argv);