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.
21 #include "shared/view.h"
22 #include <dali/dali.h>
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit/devel-api/shader-effects/motion-blur-effect.h>
25 #include <dali-toolkit/devel-api/controls/popup/popup.h>
28 using namespace Dali::Toolkit;
32 namespace // unnamed namespace
35 ////////////////////////////////////////////////////
37 // Demo setup parameters
40 //#define MULTIPLE_MOTION_BLURRED_ACTORS
41 #ifndef MULTIPLE_MOTION_BLURRED_ACTORS
43 const float MOTION_BLUR_ACTOR_WIDTH = 256; // actor size on screen
44 const float MOTION_BLUR_ACTOR_HEIGHT = 256; // ""
46 #else //#ifndef MULTIPLE_MOTION_BLURRED_ACTORS
48 const float MOTION_BLUR_ACTOR_WIDTH = 150; // actor size on screen
49 const float MOTION_BLUR_ACTOR_HEIGHT = 112; // ""
51 #endif //#ifndef MULTIPLE_MOTION_BLURRED_ACTORS
54 const unsigned int MOTION_BLUR_NUM_SAMPLES = 8;
56 const int MOTION_BLUR_NUM_ACTOR_IMAGES = 5;
57 const char* MOTION_BLUR_ACTOR_IMAGE1( DEMO_IMAGE_DIR "image-with-border-1.jpg" );
58 const char* MOTION_BLUR_ACTOR_IMAGE2( DEMO_IMAGE_DIR "image-with-border-2.jpg" );
59 const char* MOTION_BLUR_ACTOR_IMAGE3( DEMO_IMAGE_DIR "image-with-border-3.jpg" );
60 const char* MOTION_BLUR_ACTOR_IMAGE4( DEMO_IMAGE_DIR "image-with-border-4.jpg" );
61 const char* MOTION_BLUR_ACTOR_IMAGE5( DEMO_IMAGE_DIR "image-with-border-1.jpg" );
63 const char* MOTION_BLUR_ACTOR_IMAGES[] = {
64 MOTION_BLUR_ACTOR_IMAGE1,
65 MOTION_BLUR_ACTOR_IMAGE2,
66 MOTION_BLUR_ACTOR_IMAGE3,
67 MOTION_BLUR_ACTOR_IMAGE4,
68 MOTION_BLUR_ACTOR_IMAGE5,
71 const int NUM_ACTOR_ANIMATIONS = 4;
72 const int NUM_CAMERA_ANIMATIONS = 2;
75 const char* BACKGROUND_IMAGE_PATH = DEMO_IMAGE_DIR "background-default.png";
77 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
78 const char* LAYOUT_IMAGE( DEMO_IMAGE_DIR "icon-change.png" );
79 const char* LAYOUT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
80 const char* APPLICATION_TITLE( "Motion Blur" );
81 const char* EFFECTS_OFF_ICON( DEMO_IMAGE_DIR "icon-effects-off.png" );
82 const char* EFFECTS_OFF_ICON_SELECTED( DEMO_IMAGE_DIR "icon-effects-off-selected.png" );
83 const char* EFFECTS_ON_ICON( DEMO_IMAGE_DIR "icon-effects-on.png" );
84 const char* EFFECTS_ON_ICON_SELECTED( DEMO_IMAGE_DIR "icon-effects-on-selected.png" );
86 const float UI_MARGIN = 4.0f; ///< Screen Margin for placement of UI buttons
88 const Vector3 BUTTON_SIZE_CONSTRAINT( 0.24f, 0.09f, 1.0f );
89 const Vector3 BUTTON_TITLE_LABEL_TAP_HERE_SIZE_CONSTRAINT( 0.55f, 0.06f, 1.0f );
90 const Vector3 BUTTON_TITLE_LABEL_INSTRUCTIONS_POPUP_SIZE_CONSTRAINT( 1.0f, 1.0f, 1.0f );
92 // move this button down a bit so it is visible on target and not covered up by toolbar
93 const float BUTTON_TITLE_LABEL_Y_OFFSET = 0.05f;
95 const float ORIENTATION_DURATION = 0.5f; ///< Time to rotate to new orientation.
98 * @brief Load an image, scaled-down to no more than the dimensions passed in.
100 * Uses SHRINK_TO_FIT which ensures the resulting image is
101 * smaller than or equal to the specified dimensions while preserving its
102 * original aspect ratio.
104 ResourceImage LoadImageFittedInBox( const char * const imagePath, uint32_t maxWidth, uint32_t maxHeight )
106 // Load the image nicely scaled-down to fit within the specified max width and height:
107 return ResourceImage::New( imagePath, ImageDimensions( maxWidth, maxHeight ), FittingMode::SHRINK_TO_FIT, Dali::SamplingMode::BOX_THEN_LINEAR );
110 } // unnamed namespace
114 class MotionBlurExampleApp : public ConnectionTracker
119 * DeviceOrientation describes the four different
120 * orientations the device can be in based on accelerometer reports.
122 enum DeviceOrientation
126 PORTRAIT_INVERSE = 180,
127 LANDSCAPE_INVERSE = 270
132 * @param application class, stored as reference
134 MotionBlurExampleApp(Application &app)
136 mActorEffectsEnabled(false),
137 mCurrentActorAnimation(0),
140 // Connect to the Application's Init signal
141 app.InitSignal().Connect(this, &MotionBlurExampleApp::OnInit);
144 ~MotionBlurExampleApp()
146 // Nothing to do here; everything gets deleted automatically
150 * This method gets called once the main loop of application is up and running
152 void OnInit(Application& app)
154 // The Init signal is received once (only) during the Application lifetime
156 Stage::GetCurrent().KeyEventSignal().Connect(this, &MotionBlurExampleApp::OnKeyEvent);
159 // Creates a default view with a default tool bar.
160 // The view is added to the stage.
161 mContentLayer = DemoHelper::CreateView( mApplication,
164 BACKGROUND_IMAGE_PATH,
168 //Add an effects icon on the right of the title
169 mActorEffectsButton = Toolkit::PushButton::New();
170 mActorEffectsButton.SetUnselectedImage( EFFECTS_OFF_ICON );
171 mActorEffectsButton.SetSelectedImage( EFFECTS_OFF_ICON_SELECTED );
172 mActorEffectsButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnEffectButtonClicked );
173 mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
175 // Creates a mode button.
176 // Create a effect toggle button. (right of toolbar)
177 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
178 layoutButton.SetUnselectedImage( LAYOUT_IMAGE );
179 layoutButton.SetSelectedImage( LAYOUT_IMAGE_SELECTED );
180 layoutButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnLayoutButtonClicked);
181 layoutButton.SetLeaveRequired( true );
182 mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
185 mTapGestureDetector = TapGestureDetector::New();
186 mTapGestureDetector.Attach( mContentLayer );
187 mTapGestureDetector.DetectedSignal().Connect( this, &MotionBlurExampleApp::OnTap );
189 Dali::Window winHandle = app.GetWindow();
190 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
191 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
192 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
193 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
195 // set initial orientation
196 // winHandle.GetOrientation().ChangedSignal().Connect( this, &MotionBlurExampleApp::OnOrientationChanged );
197 unsigned int degrees = 0;
198 Rotate( static_cast< DeviceOrientation >( degrees ) );
201 ///////////////////////////////////////////////////////
203 // Motion blurred actor
206 // Scale down actor to fit on very low resolution screens with space to interact:
207 Size stageSize = Stage::GetCurrent().GetSize();
208 mMotionBlurActorSize = Size( std::min( stageSize.x * 0.3f, MOTION_BLUR_ACTOR_WIDTH ), std::min( stageSize.y * 0.3f, MOTION_BLUR_ACTOR_HEIGHT ) );
209 mMotionBlurActorSize = Size( std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ), std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ) );
211 Image image = LoadImageFittedInBox( MOTION_BLUR_ACTOR_IMAGE1, mMotionBlurActorSize.x, mMotionBlurActorSize.y );
212 mMotionBlurImageView = ImageView::New(image);
213 mMotionBlurImageView.SetParentOrigin( ParentOrigin::CENTER );
214 mMotionBlurImageView.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
216 mContentLayer.Add( mMotionBlurImageView );
218 // Create shader used for doing motion blur
219 mMotionBlurEffect = CreateMotionBlurEffect();
221 // set actor shader to the blur one
222 Toolkit::SetMotionBlurProperties( mMotionBlurImageView, MOTION_BLUR_NUM_SAMPLES );
223 mMotionBlurImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
226 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
228 ///////////////////////////////////////////////////////
230 // Motion blurred actor 2
233 mMotionBlurImageView2 = ImageView::New(image);
234 mMotionBlurImageView2.SetParentOrigin( ParentOrigin::CENTER );
235 mMotionBlurImageView2.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
236 mMotionBlurImageView2.SetPosition(mMotionBlurActorSize.x * 1.1f, 0.0f);
237 mMotionBlurImageView.Add( mMotionBlurImageView2 );
239 // set actor shader to the blur one
240 Toolkit::SetMotionBlurProperties( mMotionBlurImageView2, MOTION_BLUR_NUM_SAMPLES );
241 mMotionBlurImageView2.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
244 ///////////////////////////////////////////////////////
246 // Motion blurred actor 3
249 mMotionBlurImageView3 = ImageView::New(image);
250 mMotionBlurImageView3.SetParentOrigin( ParentOrigin::CENTER );
251 mMotionBlurImageView3.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
252 mMotionBlurImageView3.SetPosition(-mMotionBlurActorSize.x * 1.1f, 0.0f);
253 mMotionBlurImageView.Add( mMotionBlurImageView3 );
255 // set actor shader to the blur one
256 Toolkit::SetMotionBlurProperties( mMotionBlurImageView3, MOTION_BLUR_NUM_SAMPLES );
257 mMotionBlurImageView3.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
260 ///////////////////////////////////////////////////////
262 // Motion blurred actor 4
265 mMotionBlurImageView4 = ImageView::New(image);
266 mMotionBlurImageView4.SetParentOrigin( ParentOrigin::CENTER );
267 mMotionBlurImageView4.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
268 mMotionBlurImageView4.SetPosition(0.0f, mMotionBlurActorSize.y * 1.1f);
269 mMotionBlurImageView.Add( mMotionBlurImageView4 );
271 // set actor shader to the blur one
272 Toolkit::SetMotionBlurProperties( mMotionBlurImageView4, MOTION_BLUR_NUM_SAMPLES );
273 mMotionBlurImageView4.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
275 ///////////////////////////////////////////////////////
277 // Motion blurred actor 5
280 mMotionBlurImageView5 = ImageView::New(image);
281 mMotionBlurImageView5.SetParentOrigin( ParentOrigin::CENTER );
282 mMotionBlurImageView5.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
283 mMotionBlurImageView5.SetPosition(0.0f, -mMotionBlurActorSize.y * 1.1f);
284 mMotionBlurImageView.Add( mMotionBlurImageView5 );
286 // set actor shader to the blur one
287 Toolkit::SetMotionBlurProperties( mMotionBlurImageView5, MOTION_BLUR_NUM_SAMPLES );
288 mMotionBlurImageView5.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
289 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
292 //////////////////////////////////////////////////////////////
294 // Device Orientation Support
298 void OnOrientationChanged( Orientation orientation )
300 unsigned int degrees = orientation.GetDegrees();
301 Rotate( static_cast< DeviceOrientation >( degrees ) );
304 void Rotate( DeviceOrientation orientation )
306 // Resize the root actor
307 Vector2 stageSize = Stage::GetCurrent().GetSize();
308 Vector2 targetSize = stageSize;
309 if( orientation == LANDSCAPE ||
310 orientation == LANDSCAPE_INVERSE )
312 targetSize = Vector2( stageSize.y, stageSize.x );
315 if( mOrientation != orientation )
317 mOrientation = orientation;
319 // check if actor is on stage
320 if( mView.GetParent() )
322 // has parent so we expect it to be on stage, start animation
323 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
324 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -orientation ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
325 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
326 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
327 mRotateAnimation.Play();
331 // set the rotation to match the orientation
332 mView.SetOrientation( Degree( -orientation ), Vector3::ZAXIS );
333 mView.SetSize( targetSize );
338 // for first time just set size
339 mView.SetSize( targetSize );
344 //////////////////////////////////////////////////////////////
350 // move to point on screen that was tapped
351 void OnTap( Actor actor, const TapGesture& tapGesture )
354 float originOffsetX, originOffsetY;
356 // rotate offset (from top left origin to centre) into actor space
357 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
358 actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
360 // get dest point in local actor space
361 destPos.x = tapGesture.localPoint.x - originOffsetX;
362 destPos.y = tapGesture.localPoint.y - originOffsetY;
365 float animDuration = 0.5f;
366 mActorTapMovementAnimation = Animation::New( animDuration );
367 if ( mMotionBlurImageView )
369 mActorTapMovementAnimation.AnimateTo( Property(mMotionBlurImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
371 mActorTapMovementAnimation.SetEndAction( Animation::Bake );
372 mActorTapMovementAnimation.Play();
375 // perform some spinning etc
376 if(mActorEffectsEnabled)
378 switch(mCurrentActorAnimation)
383 float animDuration = 1.0f;
384 mActorAnimation = Animation::New(animDuration);
385 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
386 mActorAnimation.SetEndAction( Animation::Bake );
387 mActorAnimation.Play();
394 float animDuration = 1.0f;
395 mActorAnimation = Animation::New(animDuration);
396 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
397 mActorAnimation.SetEndAction( Animation::Bake );
398 mActorAnimation.Play();
402 // spin around y and z
405 float animDuration = 1.0f;
406 mActorAnimation = Animation::New(animDuration);
407 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
408 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
409 mActorAnimation.SetEndAction( Animation::Bake );
410 mActorAnimation.Play();
417 float animDuration = 1.0f;
418 mActorAnimation = Animation::New(animDuration);
419 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
420 mActorAnimation.SetEndAction( Animation::Bake );
421 mActorAnimation.Play();
429 mCurrentActorAnimation++;
430 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
432 mCurrentActorAnimation = 0;
437 void ToggleActorEffects()
439 if(!mActorEffectsEnabled)
441 mActorEffectsEnabled = true;
442 mActorEffectsButton.SetUnselectedImage( EFFECTS_ON_ICON );
443 mActorEffectsButton.SetSelectedImage( EFFECTS_ON_ICON_SELECTED );
447 mActorEffectsEnabled = false;
448 mActorEffectsButton.SetUnselectedImage( EFFECTS_OFF_ICON );
449 mActorEffectsButton.SetSelectedImage( EFFECTS_OFF_ICON_SELECTED );
453 //////////////////////////////////////////////////////////////
459 bool OnLayoutButtonClicked( Toolkit::Button button )
465 bool OnEffectButtonClicked( Toolkit::Button button )
467 ToggleActorEffects();
472 * Main key event handler
474 void OnKeyEvent(const KeyEvent& event)
476 if(event.state == KeyEvent::Down)
478 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
485 //////////////////////////////////////////////////////////////
495 if(MOTION_BLUR_NUM_ACTOR_IMAGES == mCurrentImage)
500 Image blurImage = LoadImageFittedInBox( MOTION_BLUR_ACTOR_IMAGES[mCurrentImage], mMotionBlurActorSize.x, mMotionBlurActorSize.y );
502 mMotionBlurImageView.SetImage(blurImage);
503 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
504 mMotionBlurImageView2.SetImage(blurImage);
505 mMotionBlurImageView3.SetImage(blurImage);
506 mMotionBlurImageView4.SetImage(blurImage);
507 mMotionBlurImageView5.SetImage(blurImage);
513 Application& mApplication; ///< Application instance
514 Toolkit::Control mView;
515 Toolkit::ToolBar mToolBar;
517 Layer mContentLayer; ///< Content layer (contains actor for this blur demo)
519 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
522 Property::Map mMotionBlurEffect;
523 ImageView mMotionBlurImageView;
524 Size mMotionBlurActorSize;
526 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
527 ImageView mMotionBlurImageView2;
528 ImageView mMotionBlurImageView3;
529 ImageView mMotionBlurImageView4;
530 ImageView mMotionBlurImageView5;
531 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
533 // animate actor to position where user taps screen
534 Animation mActorTapMovementAnimation;
536 // show different animations to demonstrate blur effect working on an object only movement basis
537 bool mActorEffectsEnabled;
538 Animation mActorAnimation;
539 int mCurrentActorAnimation;
541 // offer a selection of images that user can cycle between
544 TapGestureDetector mTapGestureDetector;
546 DeviceOrientation mOrientation; ///< Current Device orientation
547 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
549 Popup mInstructionsPopup; ///< Info Popup
552 void RunTest(Application& app)
554 MotionBlurExampleApp test(app);
559 // Entry point for Linux & Tizen applications
561 int DALI_EXPORT_API main(int argc, char **argv)
563 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);