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.
22 #include "shared/view.h"
24 #include <dali/dali.h>
25 #include <dali-toolkit/dali-toolkit.h>
33 const char * const TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
34 const char * const APPLICATION_TITLE_WAVE( "Cube Transition: Wave" );
35 const char * const APPLICATION_TITLE_CROSS( "Cube Transition: Cross" );
36 const char * const APPLICATION_TITLE_FOLD( "Cube Transition: Fold" );
37 const char * const EFFECT_WAVE_IMAGE( DALI_IMAGE_DIR "icon-effect-wave.png" );
38 const char * const EFFECT_CROSS_IMAGE( DALI_IMAGE_DIR "icon-effect-cross.png" );
39 const char * const EFFECT_FOLD_IMAGE( DALI_IMAGE_DIR "icon-effect-fold.png" );
40 const char * const SLIDE_SHOW_START_ICON( DALI_IMAGE_DIR "icon-play.png" );
41 const char * const SLIDE_SHOW_STOP_ICON( DALI_IMAGE_DIR "icon-stop.png" );
43 const char* IMAGES[] =
45 DALI_IMAGE_DIR "gallery-large-1.jpg",
46 DALI_IMAGE_DIR "gallery-large-2.jpg",
47 DALI_IMAGE_DIR "gallery-large-3.jpg",
48 DALI_IMAGE_DIR "gallery-large-4.jpg",
49 DALI_IMAGE_DIR "gallery-large-5.jpg",
50 DALI_IMAGE_DIR "gallery-large-6.jpg",
51 DALI_IMAGE_DIR "gallery-large-7.jpg",
52 DALI_IMAGE_DIR "gallery-large-8.jpg",
53 DALI_IMAGE_DIR "gallery-large-9.jpg",
54 DALI_IMAGE_DIR "gallery-large-10.jpg",
55 DALI_IMAGE_DIR "gallery-large-11.jpg",
56 DALI_IMAGE_DIR "gallery-large-12.jpg",
57 DALI_IMAGE_DIR "gallery-large-13.jpg",
58 DALI_IMAGE_DIR "gallery-large-14.jpg",
59 DALI_IMAGE_DIR "gallery-large-15.jpg",
60 DALI_IMAGE_DIR "gallery-large-16.jpg",
61 DALI_IMAGE_DIR "gallery-large-17.jpg",
62 DALI_IMAGE_DIR "gallery-large-18.jpg",
63 DALI_IMAGE_DIR "gallery-large-19.jpg",
64 DALI_IMAGE_DIR "gallery-large-20.jpg",
65 DALI_IMAGE_DIR "gallery-large-21.jpg",
67 const int NUM_IMAGES( sizeof(IMAGES) / sizeof(IMAGES[0]) );
69 // the number of cubes: NUM_COLUMNS*NUM_ROWS
70 // better choose the numbers that can divide viewAreaSize.x
71 const int NUM_COLUMNS_WAVE(16);
72 const int NUM_COLUMNS_CROSS(8);
73 const int NUM_COLUMNS_FOLD(8);
74 // better choose the numbers that can divide viewAreaSize.y
75 const int NUM_ROWS_WAVE(20);
76 const int NUM_ROWS_CROSS(10);
77 const int NUM_ROWS_FOLD(10);
78 //transition effect duration
79 const float ANIMATION_DURATION_WAVE(1.5f);
80 const float ANIMATION_DURATION_CROSS(1.f);
81 const float ANIMATION_DURATION_FOLD(1.f);
82 //transition effect displacement
83 const float CUBE_DISPLACEMENT_WAVE(70.f);
84 const float CUBE_DISPLACEMENT_CROSS(30.f);
86 // The duration of the current image staying on screen when slideshow is on
87 const int VIEWINGTIME = 2000; // 2 seconds
90 class CubeTransitionApp : public ConnectionTracker
96 * @param application class, stored as reference
98 CubeTransitionApp( Application& application );
100 ~CubeTransitionApp();
105 * This method gets called once the main loop of application is up and running
107 void OnInit( Application& application );
109 * PanGesture callback. This method gets called when the pan gesture is detected.
110 * @param[in] actor The actor receiving the pan gesture.
111 * @param[in] gesture The detected pan gesture.
113 void OnPanGesture( Actor actor, const PanGesture& gesture );
115 * Load the next image and start the transition;
117 void GoToNextImage();
119 * Callback function of image resource loading succeed
120 * Start the transition
121 * @param[in] image The image content of the imageActor for transition
123 void OnImageLoaded(ResourceImage image);
125 * Main key event handler
127 void OnKeyEvent(const KeyEvent& event);
129 * Callback function of effect-switch button
130 * Change the effect when the effect button is clicked
131 * @param[in] button The handle of the clicked button
133 bool OnEffectButtonClicked( Toolkit::Button button );
135 * Callback function of slideshow button
136 * Start or stop the automatical image display when the slideshow button is clicked
137 * @param[in] button The handle of the clicked button
139 bool OnSildeshowButtonClicked( Toolkit::Button button );
141 * Callback function of cube transition completed signal
142 * @param[in] effect The cube effect used for the transition
143 * @param[in] imageActor The target imageActor of the completed transition
145 void OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, ImageActor imageActor);
147 * Callback function of timer tick
148 * The timer is used to count the image display duration in slideshow,
153 Application& mApplication;
155 Toolkit::ToolBar mToolBar;
157 Toolkit::TextView mTitleActor;
162 ImageActor mCurrentImage;
163 ImageActor mNextImage;
165 bool mIsImageLoading;
166 Constraint mImageConstraint;
168 PanGestureDetector mPanGestureDetector;
170 Toolkit::CubeTransitionEffect mCubeWaveEffect;
171 Toolkit::CubeTransitionEffect mCubeCrossEffect;
172 Toolkit::CubeTransitionEffect mCubeFoldEffect;
173 Toolkit::CubeTransitionEffect mCurrentEffect;
177 Toolkit::PushButton mSlideshowButton;
178 Image mIconSlideshowStart;
179 Image mIconSlideshowStop;
181 Vector2 mPanPosition;
182 Vector2 mPanDisplacement;
187 Toolkit::PushButton mEffectChangeButton;
190 CubeTransitionApp::CubeTransitionApp( Application& application )
191 : mApplication( application ),
193 mIsImageLoading( false ),
196 mApplication.InitSignal().Connect( this, &CubeTransitionApp::OnInit );
199 CubeTransitionApp::~CubeTransitionApp()
204 void CubeTransitionApp::OnInit( Application& application )
206 Stage::GetCurrent().KeyEventSignal().Connect(this, &CubeTransitionApp::OnKeyEvent);
208 // Creates a default view with a default tool bar, the view is added to the stage.
209 mContent = DemoHelper::CreateView( application, mView, mToolBar, "", TOOLBAR_IMAGE, "" );
211 // Add an effect-changing button on the right of the tool bar.
212 mImageWave = ResourceImage::New( EFFECT_WAVE_IMAGE );
213 mImageCross = ResourceImage::New( EFFECT_CROSS_IMAGE );
214 mImageFold = ResourceImage::New( EFFECT_FOLD_IMAGE );
215 mEffectChangeButton = Toolkit::PushButton::New();
216 mEffectChangeButton.SetBackgroundImage(mImageWave);
217 mEffectChangeButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnEffectButtonClicked );
218 mToolBar.AddControl( mEffectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
220 // Add title to the tool bar.
221 mTitleActor = Toolkit::TextView::New();
222 mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
224 //Add an slideshow icon on the right of the title
225 mIconSlideshowStart = ResourceImage::New( SLIDE_SHOW_START_ICON );
226 mIconSlideshowStop = ResourceImage::New( SLIDE_SHOW_STOP_ICON );
227 mSlideshowButton = Toolkit::PushButton::New();
228 mSlideshowButton.SetBackgroundImage( mIconSlideshowStart );
229 mSlideshowButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnSildeshowButtonClicked );
230 mToolBar.AddControl( mSlideshowButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
232 // Set size to stage size to avoid seeing a black border on transition
233 mViewSize = Stage::GetCurrent().GetSize();
235 mParent = Actor::New();
236 mParent.SetSize( mViewSize );
237 mParent.SetPositionInheritanceMode( USE_PARENT_POSITION );
238 mContent.Add( mParent );
240 // use pan gesture to detect the cursor or finger movement
241 mPanGestureDetector = PanGestureDetector::New();
242 mPanGestureDetector.DetectedSignal().Connect( this, &CubeTransitionApp::OnPanGesture );
243 mPanGestureDetector.Attach( mParent );
246 mCubeWaveEffect = Toolkit::CubeTransitionWaveEffect::New(NUM_ROWS_WAVE, NUM_COLUMNS_WAVE, mViewSize);
247 mCubeWaveEffect.SetTransitionDuration( ANIMATION_DURATION_WAVE );
248 mCubeWaveEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_WAVE );
249 mCubeWaveEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
250 mParent.Add(mCubeWaveEffect.GetRoot());
252 mCubeCrossEffect = Toolkit::CubeTransitionCrossEffect::New(NUM_ROWS_CROSS, NUM_COLUMNS_CROSS, mViewSize);
253 mCubeCrossEffect.SetTransitionDuration( ANIMATION_DURATION_CROSS);
254 mCubeCrossEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_CROSS );
255 mCubeCrossEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
256 mParent.Add(mCubeCrossEffect.GetRoot());
258 mCubeFoldEffect = Toolkit::CubeTransitionFoldEffect::New(NUM_ROWS_FOLD, NUM_COLUMNS_FOLD, mViewSize);
259 mCubeFoldEffect.SetTransitionDuration( ANIMATION_DURATION_FOLD);
260 mCubeFoldEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
261 mParent.Add(mCubeFoldEffect.GetRoot());
263 mViewTimer = Timer::New( VIEWINGTIME );
264 mViewTimer.TickSignal().Connect( this, &CubeTransitionApp::OnTimerTick );
266 // show the first image
267 mImageConstraint = Constraint::New<Vector3>( Actor::Property::Scale, LocalSource( Actor::Property::Size ), ParentSource( Actor::Property::Size ), ScaleToFitKeepAspectRatioConstraint() );
269 mCurrentImage = ImageActor::New( ResourceImage::New( IMAGES[mIndex] ) );
270 mCurrentImage.SetPositionInheritanceMode( USE_PARENT_POSITION );
271 mCurrentImage.ApplyConstraint( mImageConstraint );
272 mParent.Add( mCurrentImage );
274 mCurrentEffect = mCubeWaveEffect;
275 mCurrentEffect.SetCurrentImage( mCurrentImage );
278 mTitleActor.SetText( APPLICATION_TITLE_WAVE );
279 mTitleActor.SetSize( Font::New().MeasureText( APPLICATION_TITLE_WAVE ) );
280 mTitleActor.SetStyleToCurrentText( DemoHelper::GetDefaultTextStyle() );
283 // signal handler, called when the pan gesture is detected
284 void CubeTransitionApp::OnPanGesture( Actor actor, const PanGesture& gesture )
286 // does not response when the transition has not finished
287 if( mIsImageLoading || mCubeWaveEffect.IsTransiting() || mCubeCrossEffect.IsTransiting() || mCubeFoldEffect.IsTransiting() || mSlideshow )
292 if( gesture.state == Gesture::Continuing )
294 if( gesture.displacement.x < 0)
296 mIndex = (mIndex + 1)%NUM_IMAGES;
300 mIndex = (mIndex + NUM_IMAGES -1)%NUM_IMAGES;
303 mPanPosition = gesture.position;
304 mPanDisplacement = gesture.displacement;
309 void CubeTransitionApp::GoToNextImage()
311 ResourceImage image = ResourceImage::New( IMAGES[ mIndex ] );
312 mNextImage = ImageActor::New( image );
313 mNextImage.SetPositionInheritanceMode(USE_PARENT_POSITION);
314 mNextImage.ApplyConstraint( mImageConstraint );
315 mCurrentEffect.SetTargetImage(mNextImage);
316 if( image.GetLoadingState() == ResourceLoadingSucceeded )
318 mIsImageLoading = false;
319 OnImageLoaded( image );
323 mIsImageLoading = true;
324 image.LoadingFinishedSignal().Connect( this, &CubeTransitionApp::OnImageLoaded );
328 void CubeTransitionApp::OnImageLoaded(ResourceImage image)
330 mIsImageLoading = false;
331 mCurrentEffect.StartTransition( mPanPosition, mPanDisplacement );
332 mParent.Remove(mCurrentImage);
333 mParent.Add(mNextImage);
334 mCurrentImage = mNextImage;
337 bool CubeTransitionApp::OnEffectButtonClicked( Toolkit::Button button )
339 if(mCurrentEffect == mCubeWaveEffect)
341 mCurrentEffect = mCubeCrossEffect;
342 mTitleActor.SetText( APPLICATION_TITLE_CROSS );
343 mTitleActor.SetSize( Font::New().MeasureText( APPLICATION_TITLE_CROSS ) );
344 mEffectChangeButton.SetBackgroundImage(mImageCross);
347 else if(mCurrentEffect == mCubeCrossEffect)
349 mCurrentEffect = mCubeFoldEffect;
350 mTitleActor.SetText( APPLICATION_TITLE_FOLD );
351 mTitleActor.SetSize( Font::New().MeasureText( APPLICATION_TITLE_FOLD ) );
352 mEffectChangeButton.SetBackgroundImage(mImageFold);
356 mCurrentEffect = mCubeWaveEffect;
357 mTitleActor.SetText( APPLICATION_TITLE_WAVE );
358 mTitleActor.SetSize( Font::New().MeasureText( APPLICATION_TITLE_WAVE ) );
359 mEffectChangeButton.SetBackgroundImage(mImageWave);
361 mTitleActor.SetStyleToCurrentText(DemoHelper::GetDefaultTextStyle());
363 // Set the current image to cube transition effect
364 // only need to set at beginning or change from another effect
365 mCurrentEffect.SetCurrentImage(mCurrentImage);
369 bool CubeTransitionApp::OnSildeshowButtonClicked( Toolkit::Button button )
371 mSlideshow = !mSlideshow;
374 mPanGestureDetector.Detach( mParent );
375 mSlideshowButton.SetBackgroundImage( mIconSlideshowStop );
376 mPanPosition = Vector2( mViewSize.width, mViewSize.height*0.5f );
377 mPanDisplacement = Vector2( -10.f, 0.f );
382 mPanGestureDetector.Attach( mParent );
383 mSlideshowButton.SetBackgroundImage( mIconSlideshowStart );
389 void CubeTransitionApp::OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, ImageActor imageActor)
397 bool CubeTransitionApp::OnTimerTick()
401 mIndex = (mIndex + 1)%NUM_IMAGES;
405 //return false to stop the timer
409 void CubeTransitionApp::OnKeyEvent(const KeyEvent& event)
411 if(event.state == KeyEvent::Down)
413 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
420 // Entry point for Linux & SLP applications
421 int main( int argc, char **argv )
423 Application application = Application::New( &argc, &argv );
424 CubeTransitionApp test( application );
425 application.MainLoop();