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 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 mIconPlay = ResourceImage::New( PLAY_ICON );
148 mIconStop = ResourceImage::New( STOP_ICON );
149 mPlayStopButton = Toolkit::PushButton::New();
150 mPlayStopButton.SetBackgroundImage( mIconStop );
152 mPlayStopButton.ClickedSignal().Connect( this, &RadialMenuExample::OnButtonClicked );
154 toolBar.AddControl( mPlayStopButton,
155 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
156 Toolkit::Alignment::HorizontalRight,
157 DemoHelper::DEFAULT_PLAY_PADDING );
160 const ImageDimensions intImgSize = ResourceImage::GetImageSize(TEST_OUTER_RING_FILENAME);
161 Vector2 imgSize = Vector2( intImgSize.GetWidth(), intImgSize.GetHeight() );
162 Vector2 stageSize = stage.GetSize();
163 float scale = stageSize.width / imgSize.width;
164 float availableHeight = stageSize.height - DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight * 2.0f;
165 if(availableHeight <= stageSize.width)
167 scale = availableHeight / imgSize.width;
170 mRadialSweepView1 = CreateSweepView( TEST_OUTER_RING_FILENAME, Degree(-90.0f), Degree(-90.0f));
171 mRadialSweepView2 = CreateSweepView( TEST_INNER_RING_FILENAME, Degree(90.0f), Degree(0.0f));
172 mRadialSweepView3 = CreateSweepView( TEST_MENU_FILENAME, Degree(100.0f), Degree(0.0f));
173 mRadialSweepView3.SetInitialActorAngle(Degree(-110));
174 mRadialSweepView3.SetFinalActorAngle(Degree(0));
176 Image dial = ResourceImage::New( TEST_DIAL_FILENAME );
177 mDialActor = ImageActor::New( dial );
178 mDialActor.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
179 mDialActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
180 mDialActor.SetScale(scale);
181 Layer dialLayer = Layer::New();
183 dialLayer.Add(mDialActor);
184 dialLayer.SetPositionInheritanceMode(USE_PARENT_POSITION);
185 dialLayer.SetSize(stage.GetSize());
186 mContents.Add(dialLayer);
188 mRadialSweepView1.SetScale(scale);
189 mRadialSweepView2.SetScale(scale);
190 mRadialSweepView3.SetScale(scale);
195 void RadialMenuExample::StartAnimation()
197 mDialActor.SetOpacity(0.0f);
198 mRadialSweepView1.SetOpacity(0.0f);
199 mAnimation = Animation::New(6.0f);
200 mRadialSweepView1.Activate(mAnimation, 0.0f, 3.0f);
201 mRadialSweepView2.Activate(mAnimation, 1.5f, 3.0f);
202 mRadialSweepView3.Activate(mAnimation, 3.0f, 3.0f);
203 mAnimation.AnimateTo( Property( mDialActor, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.8f ) );
204 mAnimation.AnimateTo( Property( mRadialSweepView1, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.5f ) );
205 mAnimation.FinishedSignal().Connect( this, &RadialMenuExample::OnAnimationFinished );
207 mAnimationState = PLAYING;
211 bool RadialMenuExample::OnButtonClicked( Toolkit::Button button )
213 switch( mAnimationState )
218 mAnimationState = PAUSED;
219 mPlayStopButton.SetBackgroundImage( mIconPlay );
226 mAnimationState = PLAYING;
227 mPlayStopButton.SetBackgroundImage( mIconStop );
233 mPlayStopButton.SetBackgroundImage( mIconStop );
234 mRadialSweepView1.Deactivate();
235 mRadialSweepView2.Deactivate();
236 mRadialSweepView3.Deactivate();
243 void RadialMenuExample::OnAnimationFinished( Animation& source )
245 mAnimationState = STOPPED;
246 mPlayStopButton.SetBackgroundImage( mIconPlay );
249 RadialSweepView RadialMenuExample::CreateSweepView( std::string imageName,
254 Image image = ResourceImage::New(imageName);
255 mImageActor = ImageActor::New(image);
256 mImageActor.SetParentOrigin(ParentOrigin::CENTER);
257 mImageActor.SetAnchorPoint(AnchorPoint::CENTER);
258 mImageActor.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
260 // Create the stencil
261 const ImageDimensions imageSize = ResourceImage::GetImageSize(imageName);
262 float diameter = std::max(imageSize.GetWidth(), imageSize.GetHeight());
263 RadialSweepView radialSweepView = RadialSweepView::New();
264 radialSweepView.SetDiameter( diameter );
265 radialSweepView.SetInitialAngle( initialAngle );
266 radialSweepView.SetFinalAngle( finalAngle );
267 radialSweepView.SetInitialSector( Degree(0.0f) );
268 radialSweepView.SetFinalSector( Degree(359.999f) );
269 radialSweepView.SetSize( Stage::GetCurrent().GetSize());
270 radialSweepView.SetEasingFunction( Dali::AlphaFunction::EASE_IN_OUT );
271 radialSweepView.SetPositionInheritanceMode(USE_PARENT_POSITION);
272 mContents.Add(radialSweepView);
273 radialSweepView.Add( mImageActor );
274 mImageActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
276 return radialSweepView;
280 void RadialMenuExample::OnKeyEvent(const KeyEvent& event)
282 if(event.state == KeyEvent::Down)
284 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
291 void RunTest(Application app)
293 RadialMenuExample test(app);
298 // Entry point for Linux & Tizen applications
299 int main(int argc, char **argv)
301 Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);