2 * Copyright (c) 2014 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 <dali/devel-api/actors/mesh-actor.h>
21 #include "shared/view.h"
22 #include "radial-sweep-view.h"
23 #include "radial-sweep-view-impl.h"
26 using namespace Dali::Toolkit;
30 const char* TEST_OUTER_RING_FILENAME = DALI_IMAGE_DIR "layer2.png"; // Image to be masked
31 const char* TEST_INNER_RING_FILENAME = DALI_IMAGE_DIR "layer1.png"; // Image to be masked
32 const char* TEST_MENU_FILENAME = DALI_IMAGE_DIR "layer3.png"; // Image to be masked
33 const char* TEST_DIAL_FILENAME = DALI_IMAGE_DIR "layer4.png"; // Image to be masked
34 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" ); // Background for toolbar
35 const char* APPLICATION_TITLE( "Radial Menu" );
36 const char * const PLAY_ICON( DALI_IMAGE_DIR "icon-play.png" );
37 const char * const STOP_ICON( DALI_IMAGE_DIR "icon-stop.png" );
41 /********************************************************************************
42 * Application controller class
45 // This example shows how to create a mesh actor for use as a stencil buffer
46 class RadialMenuExample : public ConnectionTracker
51 * @param[in] app The application handle
53 RadialMenuExample(Application app);
63 * Initialization signal handler - all actor initialization should happen here
64 * @param[in] app The application handle
66 void OnInit(Application& app);
69 * Create a sweep view with the given image and parameters
71 RadialSweepView CreateSweepView( std::string imageName, Degree initial, Degree final );
73 void StartAnimation();
75 bool OnButtonClicked( Toolkit::Button button );
77 void OnAnimationFinished( Animation& source );
80 * Main key event handler
82 * @param[in] event The key event to respond to
84 void OnKeyEvent(const KeyEvent& event);
86 private: // Member variables
94 Application mApplication; ///< The application handle
95 Toolkit::Control mView; ///< The toolbar view
96 Layer mContents; ///< The toolbar contents pane
97 ImageActor mImageActor; ///< Image actor shown by stencil mask
99 AnimState mAnimationState;
103 Toolkit::PushButton mPlayStopButton;
104 ImageActor mDialActor;
105 RadialSweepView mRadialSweepView1;
106 RadialSweepView mRadialSweepView2;
107 RadialSweepView mRadialSweepView3;
110 RadialMenuExample::RadialMenuExample(Application app)
111 : mApplication( app ),
112 mAnimationState(STOPPED)
114 // Connect to the Application's Init signal
115 app.InitSignal().Connect(this, &RadialMenuExample::OnInit);
118 RadialMenuExample::~RadialMenuExample()
120 // Nothing to do here; actor handles will clean up themselves.
123 void RadialMenuExample::OnInit(Application& app)
125 Stage stage = Dali::Stage::GetCurrent();
127 // The Init signal is received once (only) during the Application lifetime
128 stage.KeyEventSignal().Connect(this, &RadialMenuExample::OnKeyEvent);
130 // Create toolbar & view
131 Toolkit::ToolBar toolBar;
132 mContents = DemoHelper::CreateView( mApplication,
139 mIconPlay = ResourceImage::New( PLAY_ICON );
140 mIconStop = ResourceImage::New( STOP_ICON );
141 mPlayStopButton = Toolkit::PushButton::New();
142 mPlayStopButton.SetBackgroundImage( mIconStop );
144 mPlayStopButton.ClickedSignal().Connect( this, &RadialMenuExample::OnButtonClicked );
146 toolBar.AddControl( mPlayStopButton,
147 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
148 Toolkit::Alignment::HorizontalRight,
149 DemoHelper::DEFAULT_PLAY_PADDING );
152 const Uint16Pair intImgSize = ResourceImage::GetImageSize(TEST_OUTER_RING_FILENAME);
153 Vector2 imgSize = Vector2( intImgSize.GetWidth(), intImgSize.GetHeight() );
154 Vector2 stageSize = stage.GetSize();
155 float minStageDimension = std::min(stageSize.width, stageSize.height);
157 if(stageSize.height <= stageSize.width)
159 minStageDimension -= DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight * 2.0f;
161 float scale = minStageDimension / imgSize.width;
163 mRadialSweepView1 = CreateSweepView( TEST_OUTER_RING_FILENAME, Degree(-90.0f), Degree(-90.0f));
164 mRadialSweepView2 = CreateSweepView( TEST_INNER_RING_FILENAME, Degree(90.0f), Degree(0.0f));
165 mRadialSweepView3 = CreateSweepView( TEST_MENU_FILENAME, Degree(100.0f), Degree(0.0f));
166 mRadialSweepView3.SetInitialActorAngle(Degree(-110));
167 mRadialSweepView3.SetFinalActorAngle(Degree(0));
169 Image dial = ResourceImage::New( TEST_DIAL_FILENAME );
170 mDialActor = ImageActor::New( dial );
171 mDialActor.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
172 mDialActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
173 mDialActor.SetScale(scale);
174 Layer dialLayer = Layer::New();
176 dialLayer.Add(mDialActor);
177 dialLayer.SetPositionInheritanceMode(USE_PARENT_POSITION);
178 dialLayer.SetSize(stage.GetSize());
179 mContents.Add(dialLayer);
181 mRadialSweepView1.SetScale(scale);
182 mRadialSweepView2.SetScale(scale);
183 mRadialSweepView3.SetScale(scale);
188 void RadialMenuExample::StartAnimation()
190 mDialActor.SetOpacity(0.0f);
191 mRadialSweepView1.SetOpacity(0.0f);
192 mAnimation = Animation::New(6.0f);
193 mRadialSweepView1.Activate(mAnimation, 0.0f, 3.0f);
194 mRadialSweepView2.Activate(mAnimation, 1.5f, 3.0f);
195 mRadialSweepView3.Activate(mAnimation, 3.0f, 3.0f);
196 mAnimation.AnimateTo( Property( mDialActor, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.8f ) );
197 mAnimation.AnimateTo( Property( mRadialSweepView1, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.5f ) );
198 mAnimation.FinishedSignal().Connect( this, &RadialMenuExample::OnAnimationFinished );
200 mAnimationState = PLAYING;
204 bool RadialMenuExample::OnButtonClicked( Toolkit::Button button )
206 switch( mAnimationState )
211 mPlayStopButton.SetBackgroundImage( mIconPlay );
218 mPlayStopButton.SetBackgroundImage( mIconStop );
224 mPlayStopButton.SetBackgroundImage( mIconStop );
225 mRadialSweepView1.Deactivate();
226 mRadialSweepView2.Deactivate();
227 mRadialSweepView3.Deactivate();
234 void RadialMenuExample::OnAnimationFinished( Animation& source )
236 mAnimationState = STOPPED;
237 mPlayStopButton.SetBackgroundImage( mIconPlay );
240 RadialSweepView RadialMenuExample::CreateSweepView( std::string imageName,
245 Image image = ResourceImage::New(imageName);
246 mImageActor = ImageActor::New(image);
247 mImageActor.SetParentOrigin(ParentOrigin::CENTER);
248 mImageActor.SetAnchorPoint(AnchorPoint::CENTER);
249 mImageActor.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
251 // Create the stencil
252 const Uint16Pair imageSize = ResourceImage::GetImageSize(imageName);
253 float diameter = std::max(imageSize.GetWidth(), imageSize.GetHeight());
254 RadialSweepView radialSweepView = RadialSweepView::New();
255 radialSweepView.SetDiameter( diameter );
256 radialSweepView.SetInitialAngle( initialAngle );
257 radialSweepView.SetFinalAngle( finalAngle );
258 radialSweepView.SetInitialSector( Degree(0.0f) );
259 radialSweepView.SetFinalSector( Degree(359.999f) );
260 radialSweepView.SetSize( Stage::GetCurrent().GetSize());
261 radialSweepView.SetEasingFunction( Dali::AlphaFunction::EASE_IN_OUT );
262 radialSweepView.SetPositionInheritanceMode(USE_PARENT_POSITION);
263 mContents.Add(radialSweepView);
264 radialSweepView.Add( mImageActor );
265 mImageActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
267 return radialSweepView;
271 void RadialMenuExample::OnKeyEvent(const KeyEvent& event)
273 if(event.state == KeyEvent::Down)
275 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
282 void RunTest(Application app)
284 RadialMenuExample test(app);
289 // Entry point for Linux & Tizen applications
290 int main(int argc, char **argv)
292 Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);