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 Create an image view with an image which would be 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 maximum quality.
89 Toolkit::ImageView CreateStageFillingImageView( const char * const imagePath )
91 Size stageSize = Stage::GetCurrent().GetSize();
92 Toolkit::ImageView imageView = Toolkit::ImageView::New();
94 map[Toolkit::Visual::Property::TYPE] = Toolkit::Visual::IMAGE;
95 map[Toolkit::ImageVisual::Property::URL] = imagePath;
96 map[Toolkit::ImageVisual::Property::DESIRED_WIDTH] = stageSize.x;
97 map[Toolkit::ImageVisual::Property::DESIRED_HEIGHT] = stageSize.y;
98 map[Toolkit::ImageVisual::Property::FITTING_MODE] = FittingMode::SCALE_TO_FILL;
99 map[Toolkit::ImageVisual::Property::SAMPLING_MODE] = SamplingMode::BOX_THEN_LINEAR;
100 map[Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING] = true;
101 imageView.SetProperty( Toolkit::ImageView::Property::IMAGE, map );
108 class DissolveEffectApp : public ConnectionTracker
114 * @param application class, stored as reference
116 DissolveEffectApp( Application& application );
118 ~DissolveEffectApp();
123 * This method gets called once the main loop of application is up and running
125 void OnInit( Application& application );
127 * PanGesture callback. This method gets called when the pan gesture is detected.
128 * @param[in] actor The actor receiving the pan gesture.
129 * @param[in] gesture The detected pan gesture.
131 void OnPanGesture( Actor actor, const PanGesture& gesture );
134 * Set up the animations for transition
135 * @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
136 * @param[in] displacement The direction of the central line of the dissolve effect
138 void StartTransition(Vector2 position, Vector2 displacement);
140 * Callback function of effect-switch button
141 * Change the precision of the effect shader when the effect button is clicked
142 * @param[in] button The handle of the clicked button
144 bool OnEffectButtonClicked( Toolkit::Button button );
146 * Callback function of slideshow button
147 * Start or stop the automatical image display when the slideshow button is clicked
148 * @param[in] button The handle of the clicked button
150 bool OnSildeshowButtonClicked( Toolkit::Button button );
152 * Callback function of cube transition completed signal
153 * @param[in] effect The cube effect used for the transition
154 * @param[in] imageActor The target imageActor of the completed transition
156 void OnTransitionCompleted(Animation& source);
158 * Callback function of timer tick
159 * The timer is used to count the image display duration after cube transition in slideshow,
164 * Main key event handler
166 void OnKeyEvent(const KeyEvent& event);
169 Application& mApplication;
170 Toolkit::Control mView;
171 Toolkit::ToolBar mToolBar;
173 Toolkit::TextLabel mTitleActor;
176 Toolkit::ImageView mCurrentImage;
177 Toolkit::ImageView mNextImage;
180 Property::Map mDissolveEffect;
181 Property::Map mEmptyEffect;
183 bool mUseHighPrecision;
184 Animation mAnimation;
186 PanGestureDetector mPanGestureDetector;
192 unsigned int mCentralLineIndex;
194 Toolkit::PushButton mPlayStopButton;
195 Toolkit::PushButton mEffectChangeButton;
198 DissolveEffectApp::DissolveEffectApp( Application& application )
199 : mApplication( application ),
201 mUseHighPrecision(true),
202 mIsTransiting( false ),
204 mTimerReady( false ),
205 mCentralLineIndex( 0 )
207 mApplication.InitSignal().Connect( this, &DissolveEffectApp::OnInit );
210 DissolveEffectApp::~DissolveEffectApp()
215 void DissolveEffectApp::OnInit( Application& application )
217 Stage::GetCurrent().KeyEventSignal().Connect(this, &DissolveEffectApp::OnKeyEvent);
219 // Creates a default view with a default tool bar, the view is added to the stage.
220 mContent = DemoHelper::CreateView( application, mView,mToolBar, "", TOOLBAR_IMAGE, "" );
222 // Add an effect-changing button on the right of the tool bar.
223 mEffectChangeButton = Toolkit::PushButton::New();
224 mEffectChangeButton.SetProperty( Toolkit::Button::Property::UNSELECTED_STATE_IMAGE, EFFECT_HIGHP_IMAGE );
225 mEffectChangeButton.SetProperty( Toolkit::Button::Property::SELECTED_STATE_IMAGE, EFFECT_HIGHP_IMAGE_SELECTED );
226 mEffectChangeButton.ClickedSignal().Connect( this, &DissolveEffectApp::OnEffectButtonClicked );
227 mToolBar.AddControl( mEffectChangeButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
229 // Add title to the tool bar.
230 mTitleActor = DemoHelper::CreateToolBarLabel( APPLICATION_TITLE_HIGHP );
231 mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Toolkit::Alignment::HorizontalCenter );
233 // Add an slide-show button on the right of the title
234 mPlayStopButton = Toolkit::PushButton::New();
235 mPlayStopButton.SetProperty( Toolkit::Button::Property::UNSELECTED_STATE_IMAGE, PLAY_ICON );
236 mPlayStopButton.SetProperty( Toolkit::Button::Property::SELECTED_STATE_IMAGE, PLAY_ICON_SELECTED );
237 mPlayStopButton.ClickedSignal().Connect( this, &DissolveEffectApp::OnSildeshowButtonClicked );
238 mToolBar.AddControl( mPlayStopButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
240 // use pan gesture to detect the cursor or finger movement
241 mPanGestureDetector = PanGestureDetector::New();
242 mPanGestureDetector.DetectedSignal().Connect( this, &DissolveEffectApp::OnPanGesture );
244 mViewTimer = Timer::New( VIEWINGTIME );
245 mViewTimer.TickSignal().Connect( this, &DissolveEffectApp::OnTimerTick );
248 // Set size to stage size to avoid seeing a black border on transition
249 mParent = Actor::New();
250 mParent.SetSize( Stage::GetCurrent().GetSize() );
251 mParent.SetParentOrigin( ParentOrigin::CENTER );
252 mContent.Add( mParent );
254 // show the first image
255 mCurrentImage = CreateStageFillingImageView( IMAGES[mIndex] );
256 mCurrentImage.SetParentOrigin( ParentOrigin::CENTER );
257 mCurrentImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
258 mCurrentImage.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
259 mParent.Add( mCurrentImage );
261 mPanGestureDetector.Attach( mCurrentImage );
263 mDissolveEffect = Dali::Toolkit::CreateDissolveEffect( mUseHighPrecision );
264 Property::Map emptyShaderMap;
265 mEmptyEffect.Insert( "shader", emptyShaderMap );
268 // signal handler, called when the pan gesture is detected
269 void DissolveEffectApp::OnPanGesture( Actor actor, const PanGesture& gesture )
271 // does not response when the animation has not finished
272 if( mIsTransiting || mSlideshow )
277 if( gesture.state == Gesture::Continuing )
279 if( gesture.displacement.x < 0)
281 mIndex = (mIndex + 1)%NUM_IMAGES;
285 mIndex = (mIndex + NUM_IMAGES -1)%NUM_IMAGES;
288 mNextImage = CreateStageFillingImageView( IMAGES[ mIndex ] );
289 mNextImage.SetParentOrigin( ParentOrigin::CENTER );
290 mNextImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
291 mNextImage.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
292 mNextImage.SetZ(INITIAL_DEPTH);
293 mParent.Add( mNextImage );
294 Vector2 size = Vector2( mCurrentImage.GetCurrentSize() );
295 StartTransition( gesture.position / size, gesture.displacement * Vector2(1.0, size.x/size.y));
299 void DissolveEffectApp::StartTransition(Vector2 position, Vector2 displacement)
301 mAnimation = Animation::New(TRANSITION_DURATION);
303 Dali::Toolkit::DissolveEffectSetCentralLine( mCurrentImage, position, displacement, 0.0f );
304 mCurrentImage.SetProperty( Toolkit::ImageView::Property::IMAGE, mDissolveEffect );
305 mAnimation.AnimateTo( Property( mCurrentImage, "uPercentage" ), 1.0f, AlphaFunction::LINEAR );
307 mNextImage.SetOpacity(0.0f);
308 mAnimation.AnimateTo( Property( mNextImage, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::LINEAR );
310 if(mUseHighPrecision)
312 Dali::Toolkit::DissolveEffectSetCentralLine( mNextImage, position, displacement, 1.0f );
313 mNextImage.SetProperty( Toolkit::ImageView::Property::IMAGE, mDissolveEffect );
314 mAnimation.AnimateTo( Property( mNextImage, "uPercentage" ), 0.0f, AlphaFunction::LINEAR );
318 mAnimation.AnimateTo( Property( mNextImage, Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), AlphaFunction::LINEAR );
321 mAnimation.FinishedSignal().Connect( this, &DissolveEffectApp::OnTransitionCompleted );
323 mIsTransiting = true;
326 void DissolveEffectApp::OnKeyEvent(const KeyEvent& event)
328 if(event.state == KeyEvent::Down)
330 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
337 bool DissolveEffectApp::OnEffectButtonClicked( Toolkit::Button button )
339 mUseHighPrecision = !mUseHighPrecision;
340 mDissolveEffect = Dali::Toolkit::CreateDissolveEffect(mUseHighPrecision);
341 if(mUseHighPrecision)
343 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_HIGHP) );
344 mEffectChangeButton.SetProperty( Toolkit::Button::Property::UNSELECTED_STATE_IMAGE, EFFECT_HIGHP_IMAGE );
345 mEffectChangeButton.SetProperty( Toolkit::Button::Property::SELECTED_STATE_IMAGE, EFFECT_HIGHP_IMAGE_SELECTED );
349 mTitleActor.SetProperty( TextLabel::Property::TEXT, std::string(APPLICATION_TITLE_MEDIUMP) );
350 mEffectChangeButton.SetProperty( Toolkit::Button::Property::UNSELECTED_STATE_IMAGE, EFFECT_MEDIUMP_IMAGE );
351 mEffectChangeButton.SetProperty( Toolkit::Button::Property::SELECTED_STATE_IMAGE, EFFECT_MEDIUMP_IMAGE_SELECTED );
357 bool DissolveEffectApp::OnSildeshowButtonClicked( Toolkit::Button button )
359 mSlideshow = !mSlideshow;
362 mPlayStopButton.SetProperty( Toolkit::Button::Property::UNSELECTED_STATE_IMAGE, STOP_ICON );
363 mPlayStopButton.SetProperty( Toolkit::Button::Property::SELECTED_STATE_IMAGE, STOP_ICON_SELECTED );
364 mPanGestureDetector.Detach( mParent );
370 mPlayStopButton.SetProperty( Toolkit::Button::Property::UNSELECTED_STATE_IMAGE, PLAY_ICON );
371 mPlayStopButton.SetProperty( Toolkit::Button::Property::SELECTED_STATE_IMAGE, PLAY_ICON_SELECTED );
373 mPanGestureDetector.Attach( mParent );
378 void DissolveEffectApp::OnTransitionCompleted( Animation& source )
380 if(mUseHighPrecision)
382 mNextImage.SetProperty( Toolkit::ImageView::Property::IMAGE, mEmptyEffect );
384 mParent.Remove( mCurrentImage );
385 mPanGestureDetector.Detach( mCurrentImage );
386 mCurrentImage = mNextImage;
387 mPanGestureDetector.Attach( mCurrentImage );
388 mIsTransiting = false;
397 bool DissolveEffectApp::OnTimerTick()
402 mIndex = (mIndex + 1)%NUM_IMAGES;
403 mNextImage = CreateStageFillingImageView( IMAGES[ mIndex ] );
404 mNextImage.SetParentOrigin( ParentOrigin::CENTER );
405 mNextImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
406 mNextImage.SetSizeScalePolicy( SizeScalePolicy::FIT_WITH_ASPECT_RATIO );
407 mNextImage.SetZ(INITIAL_DEPTH);
408 mParent.Add( mNextImage );
409 switch( mCentralLineIndex%4 )
413 StartTransition(Vector2(1.0f,0.5f), Vector2(-1.0f, 0.0f));
418 StartTransition(Vector2(0.5f,0.0f), Vector2(0.0f, 1.0f));
423 StartTransition(Vector2(0.0f,0.5f), Vector2(1.0f, 0.0f));
428 StartTransition(Vector2(0.5f,1.0f), Vector2(0.0f, -1.0f));
435 return false; //return false to stop the timer
438 // Entry point for Linux & Tizen applications
439 int DALI_EXPORT_API main( int argc, char **argv )
441 Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
442 DissolveEffectApp test( application );
443 application.MainLoop();