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>
26 #include <dali-toolkit/devel-api/shader-effects/dissolve-effect.h>
30 using Dali::Toolkit::TextLabel;
36 const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
37 const char * const APPLICATION_TITLE_HIGHP( "Dissolve Effect(highp)" );
38 const char * const APPLICATION_TITLE_MEDIUMP( "Dissolve Effect(mediump)" );
39 const char * const EFFECT_HIGHP_IMAGE( DEMO_IMAGE_DIR "icon-highp.png" );
40 const char * const EFFECT_HIGHP_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-highp-selected.png" );
41 const char * const EFFECT_MEDIUMP_IMAGE( DEMO_IMAGE_DIR "icon-mediump.png" );
42 const char * const EFFECT_MEDIUMP_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-mediump-selected.png" );
43 const char * const PLAY_ICON( DEMO_IMAGE_DIR "icon-play.png" );
44 const char * const PLAY_ICON_SELECTED( DEMO_IMAGE_DIR "icon-play-selected.png" );
45 const char * const STOP_ICON( DEMO_IMAGE_DIR "icon-stop.png" );
46 const char * const STOP_ICON_SELECTED( DEMO_IMAGE_DIR "icon-stop-selected.png" );
48 const char* IMAGES[] =
50 DEMO_IMAGE_DIR "gallery-large-1.jpg",
51 DEMO_IMAGE_DIR "gallery-large-2.jpg",
52 DEMO_IMAGE_DIR "gallery-large-3.jpg",
53 DEMO_IMAGE_DIR "gallery-large-4.jpg",
54 DEMO_IMAGE_DIR "gallery-large-5.jpg",
55 DEMO_IMAGE_DIR "gallery-large-6.jpg",
56 DEMO_IMAGE_DIR "gallery-large-7.jpg",
57 DEMO_IMAGE_DIR "gallery-large-8.jpg",
58 DEMO_IMAGE_DIR "gallery-large-9.jpg",
59 DEMO_IMAGE_DIR "gallery-large-10.jpg",
60 DEMO_IMAGE_DIR "gallery-large-11.jpg",
61 DEMO_IMAGE_DIR "gallery-large-12.jpg",
62 DEMO_IMAGE_DIR "gallery-large-13.jpg",
63 DEMO_IMAGE_DIR "gallery-large-14.jpg",
64 DEMO_IMAGE_DIR "gallery-large-15.jpg",
65 DEMO_IMAGE_DIR "gallery-large-16.jpg",
66 DEMO_IMAGE_DIR "gallery-large-17.jpg",
67 DEMO_IMAGE_DIR "gallery-large-18.jpg",
68 DEMO_IMAGE_DIR "gallery-large-19.jpg",
69 DEMO_IMAGE_DIR "gallery-large-20.jpg",
70 DEMO_IMAGE_DIR "gallery-large-21.jpg",
73 const int NUM_IMAGES( sizeof(IMAGES) / sizeof(IMAGES[0]) );
75 // The duration of the current image staying on screen when slideshow is on
76 const int VIEWINGTIME = 2000; // 2 seconds
78 const float TRANSITION_DURATION = 2.5f; //2.5 second
80 const float INITIAL_DEPTH = 10.0f;
83 * @brief Load an image, scaled-down to no more than the stage dimensions.
85 * Uses image scaling mode SCALE_TO_FILL to resize the image at
86 * load time to cover the entire stage with pixels with no borders,
87 * and filter mode BOX_THEN_LINEAR to sample the image with
90 ResourceImage LoadStageFillingImage( const char * const imagePath )
92 Size stageSize = Stage::GetCurrent().GetSize();
93 return ResourceImage::New( imagePath, ImageDimensions( stageSize.x, stageSize.y ), Dali::FittingMode::SCALE_TO_FILL, Dali::SamplingMode::BOX_THEN_LINEAR );
98 class DissolveEffectApp : public ConnectionTracker
104 * @param application class, stored as reference
106 DissolveEffectApp( Application& application );
108 ~DissolveEffectApp();
113 * This method gets called once the main loop of application is up and running
115 void OnInit( Application& application );
117 * PanGesture callback. This method gets called when the pan gesture is detected.
118 * @param[in] actor The actor receiving the pan gesture.
119 * @param[in] gesture The detected pan gesture.
121 void OnPanGesture( Actor actor, const PanGesture& gesture );
124 * Set up the animations for transition
125 * @param[in] position The point ( locates within rectange {(0,0),(0,1),(1,0),(1,1)} ) passing through the central line of the dissolve effect
126 * @param[in] displacement The direction of the central line of the dissolve effect
128 void StartTransition(Vector2 position, Vector2 displacement);
130 * Callback function of effect-switch button
131 * Change the precision of the effect shader when the effect button is clicked
132 * @param[in] button The handle of the clicked button
134 bool OnEffectButtonClicked( Toolkit::Button button );
136 * Callback function of slideshow button
137 * Start or stop the automatical image display when the slideshow button is clicked
138 * @param[in] button The handle of the clicked button
140 bool OnSildeshowButtonClicked( Toolkit::Button button );
142 * Callback function of cube transition completed signal
143 * @param[in] effect The cube effect used for the transition
144 * @param[in] imageActor The target imageActor of the completed transition
146 void OnTransitionCompleted(Animation& source);
148 * Callback function of timer tick
149 * The timer is used to count the image display duration after cube transition in slideshow,
154 * Main key event handler
156 void OnKeyEvent(const KeyEvent& event);
159 Application& mApplication;
160 Toolkit::Control mView;
161 Toolkit::ToolBar mToolBar;
163 Toolkit::TextLabel mTitleActor;
166 Toolkit::ImageView mCurrentImage;
167 Toolkit::ImageView mNextImage;
170 Property::Map mDissolveEffect;
171 Property::Map mEmptyEffect;
173 bool mUseHighPrecision;
174 Animation mAnimation;
176 PanGestureDetector mPanGestureDetector;
182 unsigned int mCentralLineIndex;
185 Image mIconPlaySelected;
187 Image mIconStopSelected;
188 Toolkit::PushButton mPlayStopButton;
191 Image mIconHighPSelected;
193 Image mIconMediumPSelected;
194 Toolkit::PushButton mEffectChangeButton;
197 DissolveEffectApp::DissolveEffectApp( Application& application )
198 : mApplication( application ),
200 mUseHighPrecision(true),
201 mIsTransiting( false ),
203 mTimerReady( false ),
204 mCentralLineIndex( 0 )
206 mApplication.InitSignal().Connect( this, &DissolveEffectApp::OnInit );
209 DissolveEffectApp::~DissolveEffectApp()
214 void DissolveEffectApp::OnInit( Application& application )
216 Stage::GetCurrent().KeyEventSignal().Connect(this, &DissolveEffectApp::OnKeyEvent);
218 // Creates a default view with a default tool bar, the view is added to the stage.
219 mContent = DemoHelper::CreateView( application, mView,mToolBar, "", TOOLBAR_IMAGE, "" );
221 // Add an effect-changing button on the right of the tool bar.
222 mIconHighP = ResourceImage::New( EFFECT_HIGHP_IMAGE );
223 mIconHighPSelected = ResourceImage::New( EFFECT_HIGHP_IMAGE_SELECTED );
224 mIconMediumP = ResourceImage::New( EFFECT_MEDIUMP_IMAGE );
225 mIconMediumPSelected = ResourceImage::New( EFFECT_MEDIUMP_IMAGE_SELECTED );
226 mEffectChangeButton = Toolkit::PushButton::New();
227 mEffectChangeButton.SetButtonImage( mIconHighP );
228 mEffectChangeButton.SetSelectedImage( mIconHighPSelected );
229 mEffectChangeButton.ClickedSignal().Connect( this, &DissolveEffectApp::OnEffectButtonClicked );
230 mToolBar.AddControl( mEffectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
232 // Add title to the tool bar.
233 mTitleActor = DemoHelper::CreateToolBarLabel( APPLICATION_TITLE_HIGHP );
234 mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
236 // Add an slide-show button on the right of the title
237 mIconPlay = ResourceImage::New( PLAY_ICON );
238 mIconPlaySelected = ResourceImage::New( PLAY_ICON_SELECTED );
239 mIconStop = ResourceImage::New( STOP_ICON );
240 mIconStopSelected = ResourceImage::New( STOP_ICON_SELECTED );
241 mPlayStopButton = Toolkit::PushButton::New();
242 mPlayStopButton.SetButtonImage( mIconPlay );
243 mPlayStopButton.SetSelectedImage( mIconPlaySelected );
244 mPlayStopButton.ClickedSignal().Connect( this, &DissolveEffectApp::OnSildeshowButtonClicked );
245 mToolBar.AddControl( mPlayStopButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
247 // use pan gesture to detect the cursor or finger movement
248 mPanGestureDetector = PanGestureDetector::New();
249 mPanGestureDetector.DetectedSignal().Connect( this, &DissolveEffectApp::OnPanGesture );
251 mViewTimer = Timer::New( VIEWINGTIME );
252 mViewTimer.TickSignal().Connect( this, &DissolveEffectApp::OnTimerTick );
255 // Set size to stage size to avoid seeing a black border on transition
256 mParent = Actor::New();
257 mParent.SetSize( Stage::GetCurrent().GetSize() );
258 mParent.SetParentOrigin( ParentOrigin::CENTER );
259 mContent.Add( mParent );
261 // show the first image
262 mCurrentImage = Toolkit::ImageView::New( LoadStageFillingImage( IMAGES[mIndex] ) );
263 mCurrentImage.SetParentOrigin( ParentOrigin::CENTER );
264 mCurrentImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
265 mCurrentImage.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
266 mParent.Add( mCurrentImage );
268 mPanGestureDetector.Attach( mCurrentImage );
270 mDissolveEffect = Dali::Toolkit::CreateDissolveEffect( mUseHighPrecision );
271 mEmptyEffect.Insert( "shader", Property::Value() );
274 // signal handler, called when the pan gesture is detected
275 void DissolveEffectApp::OnPanGesture( Actor actor, const PanGesture& gesture )
277 // does not response when the animation has not finished
278 if( mIsTransiting || mSlideshow )
283 if( gesture.state == Gesture::Continuing )
285 if( gesture.displacement.x < 0)
287 mIndex = (mIndex + 1)%NUM_IMAGES;
291 mIndex = (mIndex + NUM_IMAGES -1)%NUM_IMAGES;
294 Image image = LoadStageFillingImage( IMAGES[ mIndex ] );
295 mNextImage = Toolkit::ImageView::New( image );
296 mNextImage.SetParentOrigin( ParentOrigin::CENTER );
297 mNextImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
298 mNextImage.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
299 mNextImage.SetZ(INITIAL_DEPTH);
300 mParent.Add( mNextImage );
301 Vector2 size = Vector2( mCurrentImage.GetCurrentSize() );
302 StartTransition( gesture.position / size, gesture.displacement * Vector2(1.0, size.x/size.y));
306 void DissolveEffectApp::StartTransition(Vector2 position, Vector2 displacement)
308 mAnimation = Animation::New(TRANSITION_DURATION);
310 Dali::Toolkit::DissolveEffectSetCentralLine( mCurrentImage, position, displacement, 0.0f );
311 mCurrentImage.SetProperty( Toolkit::ImageView::Property::IMAGE, mDissolveEffect );
312 mAnimation.AnimateTo( Property( mCurrentImage, "uPercentage" ), 1.0f, AlphaFunction::LINEAR );
314 mNextImage.SetOpacity(0.0f);
315 mAnimation.AnimateTo( Property( mNextImage, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::LINEAR );
317 if(mUseHighPrecision)
319 Dali::Toolkit::DissolveEffectSetCentralLine( mNextImage, position, displacement, 1.0f );
320 mNextImage.SetProperty( Toolkit::ImageView::Property::IMAGE, mDissolveEffect );
321 mAnimation.AnimateTo( Property( mNextImage, "uPercentage" ), 0.0f, AlphaFunction::LINEAR );
325 mAnimation.AnimateTo( Property( mNextImage, Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), AlphaFunction::LINEAR );
328 mAnimation.FinishedSignal().Connect( this, &DissolveEffectApp::OnTransitionCompleted );
330 mIsTransiting = true;
333 void DissolveEffectApp::OnKeyEvent(const KeyEvent& event)
335 if(event.state == KeyEvent::Down)
337 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
344 bool DissolveEffectApp::OnEffectButtonClicked( Toolkit::Button button )
346 mUseHighPrecision = !mUseHighPrecision;
347 mDissolveEffect = Dali::Toolkit::CreateDissolveEffect(mUseHighPrecision);
348 if(mUseHighPrecision)
350 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_HIGHP) );
351 mEffectChangeButton.SetButtonImage( mIconHighP );
352 mEffectChangeButton.SetSelectedImage( mIconHighPSelected );
356 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_MEDIUMP) );
357 mEffectChangeButton.SetButtonImage( mIconMediumP );
358 mEffectChangeButton.SetSelectedImage( mIconMediumPSelected );
364 bool DissolveEffectApp::OnSildeshowButtonClicked( Toolkit::Button button )
366 mSlideshow = !mSlideshow;
369 mPlayStopButton.SetButtonImage( mIconStop );
370 mPlayStopButton.SetSelectedImage( mIconStopSelected );
371 mPanGestureDetector.Detach( mParent );
377 mPlayStopButton.SetButtonImage( mIconPlay );
378 mPlayStopButton.SetSelectedImage( mIconPlaySelected );
380 mPanGestureDetector.Attach( mParent );
385 void DissolveEffectApp::OnTransitionCompleted( Animation& source )
387 mNextImage.SetProperty( Toolkit::ImageView::Property::IMAGE, mEmptyEffect );
388 mParent.Remove( mCurrentImage );
389 mPanGestureDetector.Detach( mCurrentImage );
390 mCurrentImage = mNextImage;
391 mPanGestureDetector.Attach( mCurrentImage );
392 mIsTransiting = false;
401 bool DissolveEffectApp::OnTimerTick()
406 mIndex = (mIndex + 1)%NUM_IMAGES;
407 Image image = LoadStageFillingImage( IMAGES[ mIndex ] );
408 mNextImage = Toolkit::ImageView::New( image );
409 mNextImage.SetParentOrigin( ParentOrigin::CENTER );
410 mNextImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
411 mNextImage.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
412 mNextImage.SetZ(INITIAL_DEPTH);
413 mParent.Add( mNextImage );
414 switch( mCentralLineIndex%4 )
418 StartTransition(Vector2(1.0f,0.5f), Vector2(-1.0f, 0.0f));
423 StartTransition(Vector2(0.5f,0.0f), Vector2(0.0f, 1.0f));
428 StartTransition(Vector2(0.0f,0.5f), Vector2(1.0f, 0.0f));
433 StartTransition(Vector2(0.5f,1.0f), Vector2(0.0f, -1.0f));
440 return false; //return false to stop the timer
443 // Entry point for Linux & Tizen applications
444 int DALI_EXPORT_API main( int argc, char **argv )
446 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
447 DissolveEffectApp test( application );
448 application.MainLoop();