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 * @brief Load an image, scaled-down to no more than the stage dimensions.
92 * Uses image scaling mode ImageAttributes::ScaleToFill to resize the image at
93 * load time to cover the entire stage with pixels with no borders,
94 * and filter mode ImageAttributes::BoxThenLinear to sample the image with
97 ResourceImage LoadStageFillingImage( const char * const imagePath )
99 Size stageSize = Stage::GetCurrent().GetSize();
100 ImageAttributes attributes;
101 attributes.SetSize( stageSize.x, stageSize.y );
102 attributes.SetFilterMode( ImageAttributes::BoxThenLinear );
103 attributes.SetScalingMode( ImageAttributes::ScaleToFill );
104 return ResourceImage::New( imagePath, attributes );
109 class CubeTransitionApp : public ConnectionTracker
115 * @param application class, stored as reference
117 CubeTransitionApp( Application& application );
119 ~CubeTransitionApp();
124 * This method gets called once the main loop of application is up and running
126 void OnInit( Application& application );
128 * PanGesture callback. This method gets called when the pan gesture is detected.
129 * @param[in] actor The actor receiving the pan gesture.
130 * @param[in] gesture The detected pan gesture.
132 void OnPanGesture( Actor actor, const PanGesture& gesture );
134 * Load the next image and start the transition;
136 void GoToNextImage();
138 * Callback function of image resource loading succeed
139 * Start the transition
140 * @param[in] image The image content of the imageActor for transition
142 void OnImageLoaded(ResourceImage image);
144 * Main key event handler
146 void OnKeyEvent(const KeyEvent& event);
148 * Callback function of effect-switch button
149 * Change the effect when the effect button is clicked
150 * @param[in] button The handle of the clicked button
152 bool OnEffectButtonClicked( Toolkit::Button button );
154 * Callback function of slideshow button
155 * Start or stop the automatical image display when the slideshow button is clicked
156 * @param[in] button The handle of the clicked button
158 bool OnSildeshowButtonClicked( Toolkit::Button button );
160 * Callback function of cube transition completed signal
161 * @param[in] effect The cube effect used for the transition
162 * @param[in] imageActor The target imageActor of the completed transition
164 void OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, ImageActor imageActor);
166 * Callback function of timer tick
167 * The timer is used to count the image display duration in slideshow,
172 Application& mApplication;
174 Toolkit::ToolBar mToolBar;
180 ImageActor mCurrentImage;
181 ImageActor mNextImage;
183 bool mIsImageLoading;
184 Constraint mImageConstraint;
186 PanGestureDetector mPanGestureDetector;
188 Toolkit::CubeTransitionEffect mCubeWaveEffect;
189 Toolkit::CubeTransitionEffect mCubeCrossEffect;
190 Toolkit::CubeTransitionEffect mCubeFoldEffect;
191 Toolkit::CubeTransitionEffect mCurrentEffect;
195 Toolkit::PushButton mSlideshowButton;
196 Image mIconSlideshowStart;
197 Image mIconSlideshowStop;
199 Vector2 mPanPosition;
200 Vector2 mPanDisplacement;
205 Toolkit::PushButton mEffectChangeButton;
208 CubeTransitionApp::CubeTransitionApp( Application& application )
209 : mApplication( application ),
211 mIsImageLoading( false ),
214 mApplication.InitSignal().Connect( this, &CubeTransitionApp::OnInit );
217 CubeTransitionApp::~CubeTransitionApp()
222 void CubeTransitionApp::OnInit( Application& application )
224 Stage::GetCurrent().KeyEventSignal().Connect(this, &CubeTransitionApp::OnKeyEvent);
226 // Creates a default view with a default tool bar, the view is added to the stage.
227 mContent = DemoHelper::CreateView( application, mView, mToolBar, "", TOOLBAR_IMAGE, "" );
229 // Add an effect-changing button on the right of the tool bar.
230 mImageWave = ResourceImage::New( EFFECT_WAVE_IMAGE );
231 mImageCross = ResourceImage::New( EFFECT_CROSS_IMAGE );
232 mImageFold = ResourceImage::New( EFFECT_FOLD_IMAGE );
233 mEffectChangeButton = Toolkit::PushButton::New();
234 mEffectChangeButton.SetBackgroundImage(mImageWave);
235 mEffectChangeButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnEffectButtonClicked );
236 mToolBar.AddControl( mEffectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
238 //Add an slideshow icon on the right of the title
239 mIconSlideshowStart = ResourceImage::New( SLIDE_SHOW_START_ICON );
240 mIconSlideshowStop = ResourceImage::New( SLIDE_SHOW_STOP_ICON );
241 mSlideshowButton = Toolkit::PushButton::New();
242 mSlideshowButton.SetBackgroundImage( mIconSlideshowStart );
243 mSlideshowButton.ClickedSignal().Connect( this, &CubeTransitionApp::OnSildeshowButtonClicked );
244 mToolBar.AddControl( mSlideshowButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
246 // Set size to stage size to avoid seeing a black border on transition
247 mViewSize = Stage::GetCurrent().GetSize();
249 mParent = Actor::New();
250 mParent.SetSize( mViewSize );
251 mParent.SetPositionInheritanceMode( USE_PARENT_POSITION );
252 mContent.Add( mParent );
254 // use pan gesture to detect the cursor or finger movement
255 mPanGestureDetector = PanGestureDetector::New();
256 mPanGestureDetector.DetectedSignal().Connect( this, &CubeTransitionApp::OnPanGesture );
257 mPanGestureDetector.Attach( mParent );
260 mCubeWaveEffect = Toolkit::CubeTransitionWaveEffect::New(NUM_ROWS_WAVE, NUM_COLUMNS_WAVE, mViewSize);
261 mCubeWaveEffect.SetTransitionDuration( ANIMATION_DURATION_WAVE );
262 mCubeWaveEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_WAVE );
263 mCubeWaveEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
264 mParent.Add(mCubeWaveEffect.GetRoot());
266 mCubeCrossEffect = Toolkit::CubeTransitionCrossEffect::New(NUM_ROWS_CROSS, NUM_COLUMNS_CROSS, mViewSize);
267 mCubeCrossEffect.SetTransitionDuration( ANIMATION_DURATION_CROSS);
268 mCubeCrossEffect.SetCubeDisplacement( CUBE_DISPLACEMENT_CROSS );
269 mCubeCrossEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
270 mParent.Add(mCubeCrossEffect.GetRoot());
272 mCubeFoldEffect = Toolkit::CubeTransitionFoldEffect::New(NUM_ROWS_FOLD, NUM_COLUMNS_FOLD, mViewSize);
273 mCubeFoldEffect.SetTransitionDuration( ANIMATION_DURATION_FOLD);
274 mCubeFoldEffect.TransitionCompletedSignal().Connect(this, &CubeTransitionApp::OnTransitionCompleted);
275 mParent.Add(mCubeFoldEffect.GetRoot());
277 mViewTimer = Timer::New( VIEWINGTIME );
278 mViewTimer.TickSignal().Connect( this, &CubeTransitionApp::OnTimerTick );
280 // show the first image
281 mImageConstraint = Constraint::New<Vector3>( Actor::Property::SCALE, LocalSource( Actor::Property::SIZE ), ParentSource( Actor::Property::SIZE ), ScaleToFitKeepAspectRatioConstraint() );
283 mCurrentImage = ImageActor::New( LoadStageFillingImage( IMAGES[mIndex] ) );
284 mCurrentImage.SetPositionInheritanceMode( USE_PARENT_POSITION );
285 mCurrentImage.ApplyConstraint( mImageConstraint );
286 mCurrentImage.SetRelayoutEnabled( false );
287 mParent.Add( mCurrentImage );
289 mCurrentEffect = mCubeWaveEffect;
290 mCurrentEffect.SetCurrentImage( mCurrentImage );
293 // signal handler, called when the pan gesture is detected
294 void CubeTransitionApp::OnPanGesture( Actor actor, const PanGesture& gesture )
296 // does not response when the transition has not finished
297 if( mIsImageLoading || mCubeWaveEffect.IsTransiting() || mCubeCrossEffect.IsTransiting() || mCubeFoldEffect.IsTransiting() || mSlideshow )
302 if( gesture.state == Gesture::Continuing )
304 if( gesture.displacement.x < 0)
306 mIndex = (mIndex + 1)%NUM_IMAGES;
310 mIndex = (mIndex + NUM_IMAGES -1)%NUM_IMAGES;
313 mPanPosition = gesture.position;
314 mPanDisplacement = gesture.displacement;
319 void CubeTransitionApp::GoToNextImage()
321 ResourceImage image = LoadStageFillingImage( IMAGES[ mIndex ] );
322 mNextImage = ImageActor::New( image );
324 mNextImage.SetPositionInheritanceMode(USE_PARENT_POSITION);
325 mNextImage.ApplyConstraint( mImageConstraint );
326 mNextImage.SetRelayoutEnabled( false );
327 mCurrentEffect.SetTargetImage(mNextImage);
328 if( image.GetLoadingState() == ResourceLoadingSucceeded )
330 mIsImageLoading = false;
331 OnImageLoaded( image );
335 mIsImageLoading = true;
336 image.LoadingFinishedSignal().Connect( this, &CubeTransitionApp::OnImageLoaded );
340 void CubeTransitionApp::OnImageLoaded(ResourceImage image)
342 mIsImageLoading = false;
343 mCurrentEffect.StartTransition( mPanPosition, mPanDisplacement );
344 mParent.Remove(mCurrentImage);
345 mParent.Add(mNextImage);
346 mCurrentImage = mNextImage;
349 bool CubeTransitionApp::OnEffectButtonClicked( Toolkit::Button button )
351 if(mCurrentEffect == mCubeWaveEffect)
353 mCurrentEffect = mCubeCrossEffect;
354 mEffectChangeButton.SetBackgroundImage(mImageCross);
357 else if(mCurrentEffect == mCubeCrossEffect)
359 mCurrentEffect = mCubeFoldEffect;
360 mEffectChangeButton.SetBackgroundImage(mImageFold);
364 mCurrentEffect = mCubeWaveEffect;
365 mEffectChangeButton.SetBackgroundImage(mImageWave);
368 // Set the current image to cube transition effect
369 // only need to set at beginning or change from another effect
370 mCurrentEffect.SetCurrentImage(mCurrentImage);
374 bool CubeTransitionApp::OnSildeshowButtonClicked( Toolkit::Button button )
376 mSlideshow = !mSlideshow;
379 mPanGestureDetector.Detach( mParent );
380 mSlideshowButton.SetBackgroundImage( mIconSlideshowStop );
381 mPanPosition = Vector2( mViewSize.width, mViewSize.height*0.5f );
382 mPanDisplacement = Vector2( -10.f, 0.f );
387 mPanGestureDetector.Attach( mParent );
388 mSlideshowButton.SetBackgroundImage( mIconSlideshowStart );
394 void CubeTransitionApp::OnTransitionCompleted(Toolkit::CubeTransitionEffect effect, ImageActor imageActor)
402 bool CubeTransitionApp::OnTimerTick()
406 mIndex = (mIndex + 1)%NUM_IMAGES;
410 //return false to stop the timer
414 void CubeTransitionApp::OnKeyEvent(const KeyEvent& event)
416 if(event.state == KeyEvent::Down)
418 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
425 // Entry point for Linux & Tizen applications
426 int main( int argc, char **argv )
428 Application application = Application::New( &argc, &argv );
429 CubeTransitionApp test( application );
430 application.MainLoop();