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 DemoHelper::RequestThemeChange();
127 Stage stage = Dali::Stage::GetCurrent();
129 // The Init signal is received once (only) during the Application lifetime
130 stage.KeyEventSignal().Connect(this, &RadialMenuExample::OnKeyEvent);
132 // Create toolbar & view
133 Toolkit::ToolBar toolBar;
134 mContents = DemoHelper::CreateView( mApplication,
141 mIconPlay = ResourceImage::New( PLAY_ICON );
142 mIconStop = ResourceImage::New( STOP_ICON );
143 mPlayStopButton = Toolkit::PushButton::New();
144 mPlayStopButton.SetBackgroundImage( mIconStop );
146 mPlayStopButton.ClickedSignal().Connect( this, &RadialMenuExample::OnButtonClicked );
148 toolBar.AddControl( mPlayStopButton,
149 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
150 Toolkit::Alignment::HorizontalRight,
151 DemoHelper::DEFAULT_PLAY_PADDING );
154 const Uint16Pair intImgSize = ResourceImage::GetImageSize(TEST_OUTER_RING_FILENAME);
155 Vector2 imgSize = Vector2( intImgSize.GetWidth(), intImgSize.GetHeight() );
156 Vector2 stageSize = stage.GetSize();
157 float minStageDimension = std::min(stageSize.width, stageSize.height);
159 if(stageSize.height <= stageSize.width)
161 minStageDimension -= DemoHelper::DEFAULT_VIEW_STYLE.mToolBarHeight * 2.0f;
163 float scale = minStageDimension / imgSize.width;
165 mRadialSweepView1 = CreateSweepView( TEST_OUTER_RING_FILENAME, Degree(-90.0f), Degree(-90.0f));
166 mRadialSweepView2 = CreateSweepView( TEST_INNER_RING_FILENAME, Degree(90.0f), Degree(0.0f));
167 mRadialSweepView3 = CreateSweepView( TEST_MENU_FILENAME, Degree(100.0f), Degree(0.0f));
168 mRadialSweepView3.SetInitialActorAngle(Degree(-110));
169 mRadialSweepView3.SetFinalActorAngle(Degree(0));
171 Image dial = ResourceImage::New( TEST_DIAL_FILENAME );
172 mDialActor = ImageActor::New( dial );
173 mDialActor.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
174 mDialActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
175 mDialActor.SetScale(scale);
176 Layer dialLayer = Layer::New();
178 dialLayer.Add(mDialActor);
179 dialLayer.SetPositionInheritanceMode(USE_PARENT_POSITION);
180 dialLayer.SetSize(stage.GetSize());
181 mContents.Add(dialLayer);
183 mRadialSweepView1.SetScale(scale);
184 mRadialSweepView2.SetScale(scale);
185 mRadialSweepView3.SetScale(scale);
190 void RadialMenuExample::StartAnimation()
192 mDialActor.SetOpacity(0.0f);
193 mRadialSweepView1.SetOpacity(0.0f);
194 mAnimation = Animation::New(6.0f);
195 mRadialSweepView1.Activate(mAnimation, 0.0f, 3.0f);
196 mRadialSweepView2.Activate(mAnimation, 1.5f, 3.0f);
197 mRadialSweepView3.Activate(mAnimation, 3.0f, 3.0f);
198 mAnimation.AnimateTo( Property( mDialActor, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.8f ) );
199 mAnimation.AnimateTo( Property( mRadialSweepView1, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN, TimePeriod( 0.0f, 0.5f ) );
200 mAnimation.FinishedSignal().Connect( this, &RadialMenuExample::OnAnimationFinished );
202 mAnimationState = PLAYING;
206 bool RadialMenuExample::OnButtonClicked( Toolkit::Button button )
208 switch( mAnimationState )
213 mPlayStopButton.SetBackgroundImage( mIconPlay );
220 mPlayStopButton.SetBackgroundImage( mIconStop );
226 mPlayStopButton.SetBackgroundImage( mIconStop );
227 mRadialSweepView1.Deactivate();
228 mRadialSweepView2.Deactivate();
229 mRadialSweepView3.Deactivate();
236 void RadialMenuExample::OnAnimationFinished( Animation& source )
238 mAnimationState = STOPPED;
239 mPlayStopButton.SetBackgroundImage( mIconPlay );
242 RadialSweepView RadialMenuExample::CreateSweepView( std::string imageName,
247 Image image = ResourceImage::New(imageName);
248 mImageActor = ImageActor::New(image);
249 mImageActor.SetParentOrigin(ParentOrigin::CENTER);
250 mImageActor.SetAnchorPoint(AnchorPoint::CENTER);
251 mImageActor.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
253 // Create the stencil
254 const Uint16Pair imageSize = ResourceImage::GetImageSize(imageName);
255 float diameter = std::max(imageSize.GetWidth(), imageSize.GetHeight());
256 RadialSweepView radialSweepView = RadialSweepView::New();
257 radialSweepView.SetDiameter( diameter );
258 radialSweepView.SetInitialAngle( initialAngle );
259 radialSweepView.SetFinalAngle( finalAngle );
260 radialSweepView.SetInitialSector( Degree(0.0f) );
261 radialSweepView.SetFinalSector( Degree(359.999f) );
262 radialSweepView.SetSize( Stage::GetCurrent().GetSize());
263 radialSweepView.SetEasingFunction( Dali::AlphaFunction::EASE_IN_OUT );
264 radialSweepView.SetPositionInheritanceMode(USE_PARENT_POSITION);
265 mContents.Add(radialSweepView);
266 radialSweepView.Add( mImageActor );
267 mImageActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
269 return radialSweepView;
273 void RadialMenuExample::OnKeyEvent(const KeyEvent& event)
275 if(event.state == KeyEvent::Down)
277 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
284 void RunTest(Application app)
286 RadialMenuExample test(app);
291 // Entry point for Linux & Tizen applications
292 int main(int argc, char **argv)
294 Application app = Application::New(&argc, &argv);