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 PLAY_ICON_SELECTED( DALI_IMAGE_DIR "icon-play-selected.png" );
37 const char * const STOP_ICON( DALI_IMAGE_DIR "icon-stop.png" );
38 const char * const STOP_ICON_SELECTED( DALI_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;
112 Image mIconPlaySelected;
114 Image mIconStopSelected;
115 Toolkit::PushButton mPlayStopButton;
117 RadialSweepView mRadialSweepView1;
118 RadialSweepView mRadialSweepView2;
119 RadialSweepView mRadialSweepView3;
122 RadialMenuExample::RadialMenuExample(Application app)
123 : mApplication( app ),
124 mAnimationState(STOPPED)
126 // Connect to the Application's Init signal
127 app.InitSignal().Connect(this, &RadialMenuExample::OnInit);
130 RadialMenuExample::~RadialMenuExample()
132 // Nothing to do here; actor handles will clean up themselves.
135 void RadialMenuExample::OnInit(Application& app)
137 Stage stage = Dali::Stage::GetCurrent();
139 // The Init signal is received once (only) during the Application lifetime
140 stage.KeyEventSignal().Connect(this, &RadialMenuExample::OnKeyEvent);
142 // Create toolbar & view
143 Toolkit::ToolBar toolBar;
144 mContents = DemoHelper::CreateView( mApplication,
151 mIconPlay = ResourceImage::New( PLAY_ICON );
152 mIconPlaySelected = ResourceImage::New( PLAY_ICON_SELECTED );
153 mIconStop = ResourceImage::New( STOP_ICON );
154 mIconStopSelected = ResourceImage::New( STOP_ICON_SELECTED );
155 mPlayStopButton = Toolkit::PushButton::New();
156 mPlayStopButton.SetButtonImage( mIconStop );
157 mPlayStopButton.SetSelectedImage( mIconStopSelected );
159 mPlayStopButton.ClickedSignal().Connect( this, &RadialMenuExample::OnButtonClicked );
161 toolBar.AddControl( mPlayStopButton,
162 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
163 Toolkit::Alignment::HorizontalRight,
164 DemoHelper::DEFAULT_PLAY_PADDING );
167 const ImageDimensions intImgSize = ResourceImage::GetImageSize(TEST_OUTER_RING_FILENAME);
168 Vector2 imgSize = Vector2( intImgSize.GetWidth(), intImgSize.GetHeight() );
169 Vector2 stageSize = stage.GetSize();
170 float scale = stageSize.width / imgSize.width;
171 float availableHeight = stageSize.height - DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight * 2.0f;
172 if(availableHeight <= stageSize.width)
174 scale = availableHeight / imgSize.width;
177 mRadialSweepView1 = CreateSweepView( TEST_OUTER_RING_FILENAME, Degree(-90.0f), Degree(-90.0f));
178 mRadialSweepView2 = CreateSweepView( TEST_INNER_RING_FILENAME, Degree(90.0f), Degree(0.0f));
179 mRadialSweepView3 = CreateSweepView( TEST_MENU_FILENAME, Degree(100.0f), Degree(0.0f));
180 mRadialSweepView3.SetInitialActorAngle(Degree(-110));
181 mRadialSweepView3.SetFinalActorAngle(Degree(0));
183 Image dial = ResourceImage::New( TEST_DIAL_FILENAME );
184 mDialView = ImageView::New( dial );
185 mDialView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
186 mDialView.SetPositionInheritanceMode(USE_PARENT_POSITION);
187 mDialView.SetScale(scale);
188 Layer dialLayer = Layer::New();
190 dialLayer.Add( mDialView );
191 dialLayer.SetPositionInheritanceMode(USE_PARENT_POSITION);
192 dialLayer.SetSize(stage.GetSize());
193 mContents.Add(dialLayer);
195 mRadialSweepView1.SetScale(scale);
196 mRadialSweepView2.SetScale(scale);
197 mRadialSweepView3.SetScale(scale);
202 void RadialMenuExample::StartAnimation()
204 mDialView.SetOpacity(0.0f);
205 mRadialSweepView1.SetOpacity(0.0f);
206 mAnimation = Animation::New(6.0f);
207 mRadialSweepView1.Activate(mAnimation, 0.0f, 3.0f);
208 mRadialSweepView2.Activate(mAnimation, 1.5f, 3.0f);
209 mRadialSweepView3.Activate(mAnimation, 3.0f, 3.0f);
210 mAnimation.AnimateTo( Property( mDialView, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.8f ) );
211 mAnimation.AnimateTo( Property( mRadialSweepView1, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.5f ) );
212 mAnimation.FinishedSignal().Connect( this, &RadialMenuExample::OnAnimationFinished );
214 mAnimationState = PLAYING;
218 bool RadialMenuExample::OnButtonClicked( Toolkit::Button button )
220 switch( mAnimationState )
225 mAnimationState = PAUSED;
226 mPlayStopButton.SetButtonImage( mIconPlay );
227 mPlayStopButton.SetSelectedImage( mIconPlaySelected );
234 mAnimationState = PLAYING;
235 mPlayStopButton.SetButtonImage( mIconStop );
236 mPlayStopButton.SetSelectedImage( mIconStopSelected );
242 mPlayStopButton.SetButtonImage( mIconStop );
243 mPlayStopButton.SetSelectedImage( mIconStopSelected );
244 mRadialSweepView1.Deactivate();
245 mRadialSweepView2.Deactivate();
246 mRadialSweepView3.Deactivate();
253 void RadialMenuExample::OnAnimationFinished( Animation& source )
255 mAnimationState = STOPPED;
256 mPlayStopButton.SetButtonImage( mIconPlay );
257 mPlayStopButton.SetSelectedImage( mIconPlaySelected );
260 RadialSweepView RadialMenuExample::CreateSweepView( std::string imageName,
265 Image image = ResourceImage::New(imageName);
266 mImageView = ImageView::New(image);
267 mImageView.SetParentOrigin(ParentOrigin::CENTER);
268 mImageView.SetAnchorPoint(AnchorPoint::CENTER);
269 mImageView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
271 // Create the stencil
272 const ImageDimensions imageSize = ResourceImage::GetImageSize(imageName);
273 float diameter = std::max(imageSize.GetWidth(), imageSize.GetHeight());
274 RadialSweepView radialSweepView = RadialSweepView::New();
275 radialSweepView.SetDiameter( diameter );
276 radialSweepView.SetInitialAngle( initialAngle );
277 radialSweepView.SetFinalAngle( finalAngle );
278 radialSweepView.SetInitialSector( Degree(0.0f) );
279 radialSweepView.SetFinalSector( Degree(359.999f) );
280 radialSweepView.SetSize( Stage::GetCurrent().GetSize());
281 radialSweepView.SetEasingFunction( Dali::AlphaFunction::EASE_IN_OUT );
282 radialSweepView.SetPositionInheritanceMode(USE_PARENT_POSITION);
283 mContents.Add(radialSweepView);
284 radialSweepView.Add( mImageView );
285 mImageView.SetPositionInheritanceMode(USE_PARENT_POSITION);
287 return radialSweepView;
291 void RadialMenuExample::OnKeyEvent(const KeyEvent& event)
293 if(event.state == KeyEvent::Down)
295 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
302 void RunTest(Application app)
304 RadialMenuExample test(app);
309 // Entry point for Linux & Tizen applications
310 int main(int argc, char **argv)
312 Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);