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_HIGHP( "Dissolve Effect(highp)" );
35 const char * const APPLICATION_TITLE_MEDIUMP( "Dissolve Effect(mediump)" );
36 const char * const EFFECT_HIGHP_IMAGE( DALI_IMAGE_DIR "icon-highp.png" );
37 const char * const EFFECT_MEDIUMP_IMAGE( DALI_IMAGE_DIR "icon-mediump.png" );
38 const char * const PLAY_ICON( DALI_IMAGE_DIR "icon-play.png" );
39 const char * const STOP_ICON( DALI_IMAGE_DIR "icon-stop.png" );
41 const char* IMAGES[] =
43 DALI_IMAGE_DIR "gallery-large-1.jpg",
44 DALI_IMAGE_DIR "gallery-large-2.jpg",
45 DALI_IMAGE_DIR "gallery-large-3.jpg",
46 DALI_IMAGE_DIR "gallery-large-4.jpg",
47 DALI_IMAGE_DIR "gallery-large-5.jpg",
48 DALI_IMAGE_DIR "gallery-large-6.jpg",
49 DALI_IMAGE_DIR "gallery-large-7.jpg",
50 DALI_IMAGE_DIR "gallery-large-8.jpg",
51 DALI_IMAGE_DIR "gallery-large-9.jpg",
52 DALI_IMAGE_DIR "gallery-large-10.jpg",
53 DALI_IMAGE_DIR "gallery-large-11.jpg",
54 DALI_IMAGE_DIR "gallery-large-12.jpg",
55 DALI_IMAGE_DIR "gallery-large-13.jpg",
56 DALI_IMAGE_DIR "gallery-large-14.jpg",
57 DALI_IMAGE_DIR "gallery-large-15.jpg",
58 DALI_IMAGE_DIR "gallery-large-16.jpg",
59 DALI_IMAGE_DIR "gallery-large-17.jpg",
60 DALI_IMAGE_DIR "gallery-large-18.jpg",
61 DALI_IMAGE_DIR "gallery-large-19.jpg",
62 DALI_IMAGE_DIR "gallery-large-20.jpg",
63 DALI_IMAGE_DIR "gallery-large-21.jpg",
66 const int NUM_IMAGES( sizeof(IMAGES) / sizeof(IMAGES[0]) );
68 // The duration of the current image staying on screen when slideshow is on
69 const int VIEWINGTIME = 2000; // 2 seconds
71 const float TRANSITION_DURATION = 2.5f; //2.5 second
73 const float INITIAL_DEPTH = -10.0f;
76 class DissolveEffectApp : public ConnectionTracker
82 * @param application class, stored as reference
84 DissolveEffectApp( Application& application );
91 * This method gets called once the main loop of application is up and running
93 void OnInit( Application& application );
95 * PanGesture callback. This method gets called when the pan gesture is detected.
96 * @param[in] actor The actor receiving the pan gesture.
97 * @param[in] gesture The detected pan gesture.
99 void OnPanGesture( Actor actor, const PanGesture& gesture );
102 * Set up the animations for transition
103 * @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
104 * @param[in] displacement The direction of the central line of the dissolve effect
106 void StartTransition(Vector2 position, Vector2 displacement);
108 * Callback function of effect-switch button
109 * Change the precision of the effect shader when the effect button is clicked
110 * @param[in] button The handle of the clicked button
112 bool OnEffectButtonClicked( Toolkit::Button button );
114 * Callback function of slideshow button
115 * Start or stop the automatical image display when the slideshow button is clicked
116 * @param[in] button The handle of the clicked button
118 bool OnSildeshowButtonClicked( Toolkit::Button button );
120 * Callback function of cube transition completed signal
121 * @param[in] effect The cube effect used for the transition
122 * @param[in] imageActor The target imageActor of the completed transition
124 void OnTransitionCompleted(Animation& source);
126 * Callback function of timer tick
127 * The timer is used to count the image display duration after cube transition in slideshow,
132 * Main key event handler
134 void OnKeyEvent(const KeyEvent& event);
137 Application& mApplication;
139 Toolkit::ToolBar mToolBar;
143 ImageActor mCurrentImage;
144 ImageActor mNextImage;
146 Constraint mSizeConstraint;
148 Toolkit::DissolveEffect mCurrentImageEffect;
149 Toolkit::DissolveEffect mNextImageEffect;
150 bool mUseHighPrecision;
151 Animation mAnimation;
153 PanGestureDetector mPanGestureDetector;
159 unsigned int mCentralLineIndex;
163 Toolkit::PushButton mPlayStopButton;
167 Toolkit::PushButton mEffectChangeButton;
170 DissolveEffectApp::DissolveEffectApp( Application& application )
171 : mApplication( application ),
173 mUseHighPrecision(true),
174 mIsTransiting( false ),
176 mTimerReady( false ),
177 mCentralLineIndex( 0 )
179 mApplication.InitSignal().Connect( this, &DissolveEffectApp::OnInit );
182 DissolveEffectApp::~DissolveEffectApp()
187 void DissolveEffectApp::OnInit( Application& application )
189 Stage::GetCurrent().KeyEventSignal().Connect(this, &DissolveEffectApp::OnKeyEvent);
191 // Creates a default view with a default tool bar, the view is added to the stage.
192 mContent = DemoHelper::CreateView( application, mView,mToolBar, "", TOOLBAR_IMAGE, "" );
194 // Add an effect-changing button on the right of the tool bar.
195 mIconHighP = ResourceImage::New( EFFECT_HIGHP_IMAGE );
196 mIconMediumP = ResourceImage::New( EFFECT_MEDIUMP_IMAGE );
197 mEffectChangeButton = Toolkit::PushButton::New();
198 mEffectChangeButton.SetBackgroundImage(mIconHighP);
199 mEffectChangeButton.ClickedSignal().Connect( this, &DissolveEffectApp::OnEffectButtonClicked );
200 mToolBar.AddControl( mEffectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
202 // Add title to the tool bar.
205 // Add an slide-show button on the right of the title
206 mIconPlay = ResourceImage::New( PLAY_ICON );
207 mIconStop = ResourceImage::New( STOP_ICON );
208 mPlayStopButton = Toolkit::PushButton::New();
209 mPlayStopButton.SetBackgroundImage( mIconPlay );
210 mPlayStopButton.ClickedSignal().Connect( this, &DissolveEffectApp::OnSildeshowButtonClicked );
211 mToolBar.AddControl( mPlayStopButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
213 // use pan gesture to detect the cursor or finger movement
214 mPanGestureDetector = PanGestureDetector::New();
215 mPanGestureDetector.DetectedSignal().Connect( this, &DissolveEffectApp::OnPanGesture );
217 // create the dissolve effect object
218 mCurrentImageEffect = Toolkit::DissolveEffect::New(mUseHighPrecision);
219 mNextImageEffect = Toolkit::DissolveEffect::New(mUseHighPrecision);
221 mViewTimer = Timer::New( VIEWINGTIME );
222 mViewTimer.TickSignal().Connect( this, &DissolveEffectApp::OnTimerTick );
225 // Set size to stage size to avoid seeing a black border on transition
226 mParent = Actor::New();
227 mParent.SetSize( Stage::GetCurrent().GetSize() );
228 mParent.SetPositionInheritanceMode( USE_PARENT_POSITION );
229 mContent.Add( mParent );
231 mSizeConstraint= Constraint::New<Vector3>( Actor::Property::Scale, LocalSource( Actor::Property::Size ), ParentSource( Actor::Property::Size ), ScaleToFitKeepAspectRatioConstraint() );
233 // show the first image
234 mCurrentImage = ImageActor::New( ResourceImage::New( IMAGES[mIndex] ) );
235 mCurrentImage.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
236 mCurrentImage.ApplyConstraint( mSizeConstraint );
237 mParent.Add( mCurrentImage );
239 mPanGestureDetector.Attach( mCurrentImage );
242 // signal handler, called when the pan gesture is detected
243 void DissolveEffectApp::OnPanGesture( Actor actor, const PanGesture& gesture )
245 // does not response when the animation has not finished
246 if( mIsTransiting || mSlideshow )
251 if( gesture.state == Gesture::Continuing )
253 if( gesture.displacement.x < 0)
255 mIndex = (mIndex + 1)%NUM_IMAGES;
259 mIndex = (mIndex + NUM_IMAGES -1)%NUM_IMAGES;
262 Image image = ResourceImage::New( IMAGES[ mIndex ] );
263 mNextImage = ImageActor::New( image );
264 mNextImage.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
265 mNextImage.ApplyConstraint( mSizeConstraint );
266 mNextImage.SetZ(INITIAL_DEPTH);
267 mParent.Add( mNextImage );
268 Vector2 size = Vector2( mCurrentImage.GetCurrentSize() );
269 StartTransition( gesture.position / size, gesture.displacement * Vector2(1.0, size.x/size.y));
273 void DissolveEffectApp::StartTransition(Vector2 position, Vector2 displacement)
275 mAnimation = Animation::New(TRANSITION_DURATION);
277 mCurrentImageEffect.SetCentralLine(position,displacement);
278 mCurrentImageEffect.SetDistortion(0.0f);
279 mCurrentImage.SetShaderEffect(mCurrentImageEffect);
280 mAnimation.AnimateTo( Property(mCurrentImageEffect, mCurrentImageEffect.GetDistortionPropertyName()), 1.0f, AlphaFunctions::Linear );
282 mNextImage.SetOpacity(0.0f);
283 mAnimation.OpacityTo( mNextImage, 1.0, AlphaFunctions::Linear );
285 if(mUseHighPrecision)
287 mNextImageEffect.SetCentralLine(position,-displacement);
288 mNextImageEffect.SetDistortion(1.0f);
289 mNextImage.SetShaderEffect(mNextImageEffect);
290 mAnimation.AnimateTo( Property(mNextImageEffect, mNextImageEffect.GetDistortionPropertyName()), 0.0f, AlphaFunctions::Linear );
294 mAnimation.MoveTo(mNextImage, Vector3(0.0f, 0.0f, 0.0f), AlphaFunctions::Linear);
297 mAnimation.FinishedSignal().Connect( this, &DissolveEffectApp::OnTransitionCompleted );
299 mIsTransiting = true;
302 void DissolveEffectApp::OnKeyEvent(const KeyEvent& event)
304 if(event.state == KeyEvent::Down)
306 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
313 bool DissolveEffectApp::OnEffectButtonClicked( Toolkit::Button button )
315 mUseHighPrecision = !mUseHighPrecision;
316 mCurrentImageEffect = Toolkit::DissolveEffect::New(mUseHighPrecision);
317 if(mUseHighPrecision)
319 mEffectChangeButton.SetBackgroundImage(mIconHighP);
323 mEffectChangeButton.SetBackgroundImage(mIconMediumP);
329 bool DissolveEffectApp::OnSildeshowButtonClicked( Toolkit::Button button )
331 mSlideshow = !mSlideshow;
334 mPlayStopButton.SetBackgroundImage( mIconStop );
335 mPanGestureDetector.Detach( mParent );
341 mPlayStopButton.SetBackgroundImage( mIconPlay );
343 mPanGestureDetector.Attach( mParent );
348 void DissolveEffectApp::OnTransitionCompleted( Animation& source )
350 mCurrentImage.RemoveShaderEffect();
351 mNextImage.RemoveShaderEffect();
352 mParent.Remove( mCurrentImage );
353 mPanGestureDetector.Detach( mCurrentImage );
354 mCurrentImage = mNextImage;
355 mPanGestureDetector.Attach( mCurrentImage );
356 mIsTransiting = false;
365 bool DissolveEffectApp::OnTimerTick()
370 mIndex = (mIndex + 1)%NUM_IMAGES;
371 Image image = ResourceImage::New( IMAGES[ mIndex ] );
372 mNextImage = ImageActor::New( image );
373 mNextImage.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
374 mNextImage.ApplyConstraint( mSizeConstraint );
375 mNextImage.SetZ(INITIAL_DEPTH);
376 mParent.Add( mNextImage );
377 switch( mCentralLineIndex%4 )
381 StartTransition(Vector2(1.0f,0.5f), Vector2(-1.0f, 0.0f));
386 StartTransition(Vector2(0.5f,0.0f), Vector2(0.0f, 1.0f));
391 StartTransition(Vector2(0.0f,0.5f), Vector2(1.0f, 0.0f));
396 StartTransition(Vector2(0.5f,1.0f), Vector2(0.0f, -1.0f));
403 return false; //return false to stop the timer
406 // Entry point for Linux & SLP applications
407 int main( int argc, char **argv )
409 Application application = Application::New( &argc, &argv );
410 DissolveEffectApp test( application );
411 application.MainLoop();