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>
29 using Dali::Toolkit::TextLabel;
35 const char * const TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
36 const char * const APPLICATION_TITLE_HIGHP( "Dissolve Effect(highp)" );
37 const char * const APPLICATION_TITLE_MEDIUMP( "Dissolve Effect(mediump)" );
38 const char * const EFFECT_HIGHP_IMAGE( DALI_IMAGE_DIR "icon-highp.png" );
39 const char * const EFFECT_MEDIUMP_IMAGE( DALI_IMAGE_DIR "icon-mediump.png" );
40 const char * const PLAY_ICON( DALI_IMAGE_DIR "icon-play.png" );
41 const char * const 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",
68 const int NUM_IMAGES( sizeof(IMAGES) / sizeof(IMAGES[0]) );
70 // The duration of the current image staying on screen when slideshow is on
71 const int VIEWINGTIME = 2000; // 2 seconds
73 const float TRANSITION_DURATION = 2.5f; //2.5 second
75 const float INITIAL_DEPTH = -10.0f;
78 * @brief Load an image, scaled-down to no more than the stage dimensions.
80 * Uses image scaling mode ImageAttributes::ScaleToFill to resize the image at
81 * load time to cover the entire stage with pixels with no borders,
82 * and filter mode ImageAttributes::BoxThenLinear to sample the image with
85 ResourceImage LoadStageFillingImage( const char * const imagePath )
87 Size stageSize = Stage::GetCurrent().GetSize();
88 ImageAttributes attributes;
89 attributes.SetSize( stageSize.x, stageSize.y );
90 attributes.SetFilterMode( ImageAttributes::BoxThenLinear );
91 attributes.SetScalingMode( ImageAttributes::ScaleToFill );
92 return ResourceImage::New( imagePath, attributes );
97 class DissolveEffectApp : public ConnectionTracker
103 * @param application class, stored as reference
105 DissolveEffectApp( Application& application );
107 ~DissolveEffectApp();
112 * This method gets called once the main loop of application is up and running
114 void OnInit( Application& application );
116 * PanGesture callback. This method gets called when the pan gesture is detected.
117 * @param[in] actor The actor receiving the pan gesture.
118 * @param[in] gesture The detected pan gesture.
120 void OnPanGesture( Actor actor, const PanGesture& gesture );
123 * Set up the animations for transition
124 * @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
125 * @param[in] displacement The direction of the central line of the dissolve effect
127 void StartTransition(Vector2 position, Vector2 displacement);
129 * Callback function of effect-switch button
130 * Change the precision of the effect shader 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(Animation& source);
147 * Callback function of timer tick
148 * The timer is used to count the image display duration after cube transition in slideshow,
153 * Main key event handler
155 void OnKeyEvent(const KeyEvent& event);
158 Application& mApplication;
160 Toolkit::ToolBar mToolBar;
162 Toolkit::TextLabel mTitleActor;
165 ImageActor mCurrentImage;
166 ImageActor mNextImage;
168 Constraint mSizeConstraint;
170 Toolkit::DissolveEffect mCurrentImageEffect;
171 Toolkit::DissolveEffect mNextImageEffect;
172 bool mUseHighPrecision;
173 Animation mAnimation;
175 PanGestureDetector mPanGestureDetector;
181 unsigned int mCentralLineIndex;
185 Toolkit::PushButton mPlayStopButton;
189 Toolkit::PushButton mEffectChangeButton;
192 DissolveEffectApp::DissolveEffectApp( Application& application )
193 : mApplication( application ),
195 mUseHighPrecision(true),
196 mIsTransiting( false ),
198 mTimerReady( false ),
199 mCentralLineIndex( 0 )
201 mApplication.InitSignal().Connect( this, &DissolveEffectApp::OnInit );
204 DissolveEffectApp::~DissolveEffectApp()
209 void DissolveEffectApp::OnInit( Application& application )
211 DemoHelper::RequestThemeChange();
213 Stage::GetCurrent().KeyEventSignal().Connect(this, &DissolveEffectApp::OnKeyEvent);
215 // Creates a default view with a default tool bar, the view is added to the stage.
216 mContent = DemoHelper::CreateView( application, mView,mToolBar, "", TOOLBAR_IMAGE, "" );
218 // Add an effect-changing button on the right of the tool bar.
219 mIconHighP = ResourceImage::New( EFFECT_HIGHP_IMAGE );
220 mIconMediumP = ResourceImage::New( EFFECT_MEDIUMP_IMAGE );
221 mEffectChangeButton = Toolkit::PushButton::New();
222 mEffectChangeButton.SetBackgroundImage(mIconHighP);
223 mEffectChangeButton.ClickedSignal().Connect( this, &DissolveEffectApp::OnEffectButtonClicked );
224 mToolBar.AddControl( mEffectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
226 // Add title to the tool bar.
227 mTitleActor = DemoHelper::CreateToolBarLabel( APPLICATION_TITLE_HIGHP );
228 mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
230 // Add an slide-show button on the right of the title
231 mIconPlay = ResourceImage::New( PLAY_ICON );
232 mIconStop = ResourceImage::New( STOP_ICON );
233 mPlayStopButton = Toolkit::PushButton::New();
234 mPlayStopButton.SetBackgroundImage( mIconPlay );
235 mPlayStopButton.ClickedSignal().Connect( this, &DissolveEffectApp::OnSildeshowButtonClicked );
236 mToolBar.AddControl( mPlayStopButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
238 // use pan gesture to detect the cursor or finger movement
239 mPanGestureDetector = PanGestureDetector::New();
240 mPanGestureDetector.DetectedSignal().Connect( this, &DissolveEffectApp::OnPanGesture );
242 // create the dissolve effect object
243 mCurrentImageEffect = Toolkit::DissolveEffect::New(mUseHighPrecision);
244 mNextImageEffect = Toolkit::DissolveEffect::New(mUseHighPrecision);
246 mViewTimer = Timer::New( VIEWINGTIME );
247 mViewTimer.TickSignal().Connect( this, &DissolveEffectApp::OnTimerTick );
250 // Set size to stage size to avoid seeing a black border on transition
251 mParent = Actor::New();
252 mParent.SetSize( Stage::GetCurrent().GetSize() );
253 mParent.SetPositionInheritanceMode( USE_PARENT_POSITION );
254 mContent.Add( mParent );
256 mSizeConstraint= Constraint::New<Vector3>( Actor::Property::SCALE, LocalSource( Actor::Property::SIZE ), ParentSource( Actor::Property::SIZE ), ScaleToFitKeepAspectRatioConstraint() );
258 // show the first image
259 mCurrentImage = ImageActor::New( LoadStageFillingImage( IMAGES[mIndex] ) );
260 mCurrentImage.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
261 mCurrentImage.ApplyConstraint( mSizeConstraint );
262 mParent.Add( mCurrentImage );
264 mPanGestureDetector.Attach( mCurrentImage );
267 // signal handler, called when the pan gesture is detected
268 void DissolveEffectApp::OnPanGesture( Actor actor, const PanGesture& gesture )
270 // does not response when the animation has not finished
271 if( mIsTransiting || mSlideshow )
276 if( gesture.state == Gesture::Continuing )
278 if( gesture.displacement.x < 0)
280 mIndex = (mIndex + 1)%NUM_IMAGES;
284 mIndex = (mIndex + NUM_IMAGES -1)%NUM_IMAGES;
287 Image image = LoadStageFillingImage( IMAGES[ mIndex ] );
288 mNextImage = ImageActor::New( image );
289 mNextImage.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
290 mNextImage.ApplyConstraint( mSizeConstraint );
291 mNextImage.SetZ(INITIAL_DEPTH);
292 mParent.Add( mNextImage );
293 Vector2 size = Vector2( mCurrentImage.GetCurrentSize() );
294 StartTransition( gesture.position / size, gesture.displacement * Vector2(1.0, size.x/size.y));
298 void DissolveEffectApp::StartTransition(Vector2 position, Vector2 displacement)
300 mAnimation = Animation::New(TRANSITION_DURATION);
302 mCurrentImageEffect.SetCentralLine(position,displacement);
303 mCurrentImageEffect.SetDistortion(0.0f);
304 mCurrentImage.SetShaderEffect(mCurrentImageEffect);
305 mAnimation.AnimateTo( Property(mCurrentImageEffect, mCurrentImageEffect.GetDistortionPropertyName()), 1.0f, AlphaFunctions::Linear );
307 mNextImage.SetOpacity(0.0f);
308 mAnimation.OpacityTo( mNextImage, 1.0, AlphaFunctions::Linear );
310 if(mUseHighPrecision)
312 mNextImageEffect.SetCentralLine(position,-displacement);
313 mNextImageEffect.SetDistortion(1.0f);
314 mNextImage.SetShaderEffect(mNextImageEffect);
315 mAnimation.AnimateTo( Property(mNextImageEffect, mNextImageEffect.GetDistortionPropertyName()), 0.0f, AlphaFunctions::Linear );
319 mAnimation.MoveTo(mNextImage, Vector3(0.0f, 0.0f, 0.0f), AlphaFunctions::Linear);
322 mAnimation.FinishedSignal().Connect( this, &DissolveEffectApp::OnTransitionCompleted );
324 mIsTransiting = true;
327 void DissolveEffectApp::OnKeyEvent(const KeyEvent& event)
329 if(event.state == KeyEvent::Down)
331 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
338 bool DissolveEffectApp::OnEffectButtonClicked( Toolkit::Button button )
340 mUseHighPrecision = !mUseHighPrecision;
341 mCurrentImageEffect = Toolkit::DissolveEffect::New(mUseHighPrecision);
342 if(mUseHighPrecision)
344 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_HIGHP) );
345 mEffectChangeButton.SetBackgroundImage(mIconHighP);
349 mEffectChangeButton.SetBackgroundImage(mIconMediumP);
355 bool DissolveEffectApp::OnSildeshowButtonClicked( Toolkit::Button button )
357 mSlideshow = !mSlideshow;
360 mPlayStopButton.SetBackgroundImage( mIconStop );
361 mPanGestureDetector.Detach( mParent );
367 mPlayStopButton.SetBackgroundImage( mIconPlay );
369 mPanGestureDetector.Attach( mParent );
374 void DissolveEffectApp::OnTransitionCompleted( Animation& source )
376 mCurrentImage.RemoveShaderEffect();
377 mNextImage.RemoveShaderEffect();
378 mParent.Remove( mCurrentImage );
379 mPanGestureDetector.Detach( mCurrentImage );
380 mCurrentImage = mNextImage;
381 mPanGestureDetector.Attach( mCurrentImage );
382 mIsTransiting = false;
391 bool DissolveEffectApp::OnTimerTick()
396 mIndex = (mIndex + 1)%NUM_IMAGES;
397 Image image = LoadStageFillingImage( IMAGES[ mIndex ] );
398 mNextImage = ImageActor::New( image );
399 mNextImage.SetPositionInheritanceMode(USE_PARENT_POSITION_PLUS_LOCAL_POSITION);
400 mNextImage.ApplyConstraint( mSizeConstraint );
401 mNextImage.SetZ(INITIAL_DEPTH);
402 mParent.Add( mNextImage );
403 switch( mCentralLineIndex%4 )
407 StartTransition(Vector2(1.0f,0.5f), Vector2(-1.0f, 0.0f));
412 StartTransition(Vector2(0.5f,0.0f), Vector2(0.0f, 1.0f));
417 StartTransition(Vector2(0.0f,0.5f), Vector2(1.0f, 0.0f));
422 StartTransition(Vector2(0.5f,1.0f), Vector2(0.0f, -1.0f));
429 return false; //return false to stop the timer
432 // Entry point for Linux & Tizen applications
433 int main( int argc, char **argv )
435 Application application = Application::New( &argc, &argv );
436 DissolveEffectApp test( application );
437 application.MainLoop();