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 unsigned int degrees = 0;
197 Rotate( static_cast< DeviceOrientation >( degrees ) );
200 ///////////////////////////////////////////////////////
202 // Motion blurred actor
205 // Scale down actor to fit on very low resolution screens with space to interact:
206 Size stageSize = Stage::GetCurrent().GetSize();
207 mMotionBlurActorSize = Size( std::min( stageSize.x * 0.3f, MOTION_BLUR_ACTOR_WIDTH ), std::min( stageSize.y * 0.3f, MOTION_BLUR_ACTOR_HEIGHT ) );
208 mMotionBlurActorSize = Size( std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ), std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ) );
210 Image image = LoadImageFittedInBox( MOTION_BLUR_ACTOR_IMAGE1, mMotionBlurActorSize.x, mMotionBlurActorSize.y );
211 mMotionBlurImageView = ImageView::New(image);
212 mMotionBlurImageView.SetParentOrigin( ParentOrigin::CENTER );
213 mMotionBlurImageView.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
215 mContentLayer.Add( mMotionBlurImageView );
217 // Create shader used for doing motion blur
218 mMotionBlurEffect = CreateMotionBlurEffect();
220 // set actor shader to the blur one
221 Toolkit::SetMotionBlurProperties( mMotionBlurImageView, MOTION_BLUR_NUM_SAMPLES );
222 mMotionBlurImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
225 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
227 ///////////////////////////////////////////////////////
229 // Motion blurred actor 2
232 mMotionBlurImageView2 = ImageView::New(image);
233 mMotionBlurImageView2.SetParentOrigin( ParentOrigin::CENTER );
234 mMotionBlurImageView2.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
235 mMotionBlurImageView2.SetPosition(mMotionBlurActorSize.x * 1.1f, 0.0f);
236 mMotionBlurImageView.Add( mMotionBlurImageView2 );
238 // set actor shader to the blur one
239 Toolkit::SetMotionBlurProperties( mMotionBlurImageView2, MOTION_BLUR_NUM_SAMPLES );
240 mMotionBlurImageView2.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
243 ///////////////////////////////////////////////////////
245 // Motion blurred actor 3
248 mMotionBlurImageView3 = ImageView::New(image);
249 mMotionBlurImageView3.SetParentOrigin( ParentOrigin::CENTER );
250 mMotionBlurImageView3.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
251 mMotionBlurImageView3.SetPosition(-mMotionBlurActorSize.x * 1.1f, 0.0f);
252 mMotionBlurImageView.Add( mMotionBlurImageView3 );
254 // set actor shader to the blur one
255 Toolkit::SetMotionBlurProperties( mMotionBlurImageView3, MOTION_BLUR_NUM_SAMPLES );
256 mMotionBlurImageView3.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
259 ///////////////////////////////////////////////////////
261 // Motion blurred actor 4
264 mMotionBlurImageView4 = ImageView::New(image);
265 mMotionBlurImageView4.SetParentOrigin( ParentOrigin::CENTER );
266 mMotionBlurImageView4.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
267 mMotionBlurImageView4.SetPosition(0.0f, mMotionBlurActorSize.y * 1.1f);
268 mMotionBlurImageView.Add( mMotionBlurImageView4 );
270 // set actor shader to the blur one
271 Toolkit::SetMotionBlurProperties( mMotionBlurImageView4, MOTION_BLUR_NUM_SAMPLES );
272 mMotionBlurImageView4.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
274 ///////////////////////////////////////////////////////
276 // Motion blurred actor 5
279 mMotionBlurImageView5 = ImageView::New(image);
280 mMotionBlurImageView5.SetParentOrigin( ParentOrigin::CENTER );
281 mMotionBlurImageView5.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
282 mMotionBlurImageView5.SetPosition(0.0f, -mMotionBlurActorSize.y * 1.1f);
283 mMotionBlurImageView.Add( mMotionBlurImageView5 );
285 // set actor shader to the blur one
286 Toolkit::SetMotionBlurProperties( mMotionBlurImageView5, MOTION_BLUR_NUM_SAMPLES );
287 mMotionBlurImageView5.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
288 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
291 void Rotate( DeviceOrientation orientation )
293 // Resize the root actor
294 Vector2 stageSize = Stage::GetCurrent().GetSize();
295 Vector2 targetSize = stageSize;
296 if( orientation == LANDSCAPE ||
297 orientation == LANDSCAPE_INVERSE )
299 targetSize = Vector2( stageSize.y, stageSize.x );
302 if( mOrientation != orientation )
304 mOrientation = orientation;
306 // check if actor is on stage
307 if( mView.GetParent() )
309 // has parent so we expect it to be on stage, start animation
310 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
311 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -orientation ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
312 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
313 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
314 mRotateAnimation.Play();
318 // set the rotation to match the orientation
319 mView.SetOrientation( Degree( -orientation ), Vector3::ZAXIS );
320 mView.SetSize( targetSize );
325 // for first time just set size
326 mView.SetSize( targetSize );
331 //////////////////////////////////////////////////////////////
337 // move to point on screen that was tapped
338 void OnTap( Actor actor, const TapGesture& tapGesture )
341 float originOffsetX, originOffsetY;
343 // rotate offset (from top left origin to centre) into actor space
344 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
345 actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
347 // get dest point in local actor space
348 destPos.x = tapGesture.localPoint.x - originOffsetX;
349 destPos.y = tapGesture.localPoint.y - originOffsetY;
352 float animDuration = 0.5f;
353 mActorTapMovementAnimation = Animation::New( animDuration );
354 if ( mMotionBlurImageView )
356 mActorTapMovementAnimation.AnimateTo( Property(mMotionBlurImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
358 mActorTapMovementAnimation.SetEndAction( Animation::Bake );
359 mActorTapMovementAnimation.Play();
362 // perform some spinning etc
363 if(mActorEffectsEnabled)
365 switch(mCurrentActorAnimation)
370 float animDuration = 1.0f;
371 mActorAnimation = Animation::New(animDuration);
372 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
373 mActorAnimation.SetEndAction( Animation::Bake );
374 mActorAnimation.Play();
381 float animDuration = 1.0f;
382 mActorAnimation = Animation::New(animDuration);
383 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
384 mActorAnimation.SetEndAction( Animation::Bake );
385 mActorAnimation.Play();
389 // spin around y and z
392 float animDuration = 1.0f;
393 mActorAnimation = Animation::New(animDuration);
394 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
395 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
396 mActorAnimation.SetEndAction( Animation::Bake );
397 mActorAnimation.Play();
404 float animDuration = 1.0f;
405 mActorAnimation = Animation::New(animDuration);
406 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
407 mActorAnimation.SetEndAction( Animation::Bake );
408 mActorAnimation.Play();
416 mCurrentActorAnimation++;
417 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
419 mCurrentActorAnimation = 0;
424 void ToggleActorEffects()
426 if(!mActorEffectsEnabled)
428 mActorEffectsEnabled = true;
429 mActorEffectsButton.SetUnselectedImage( EFFECTS_ON_ICON );
430 mActorEffectsButton.SetSelectedImage( EFFECTS_ON_ICON_SELECTED );
434 mActorEffectsEnabled = false;
435 mActorEffectsButton.SetUnselectedImage( EFFECTS_OFF_ICON );
436 mActorEffectsButton.SetSelectedImage( EFFECTS_OFF_ICON_SELECTED );
440 //////////////////////////////////////////////////////////////
446 bool OnLayoutButtonClicked( Toolkit::Button button )
452 bool OnEffectButtonClicked( Toolkit::Button button )
454 ToggleActorEffects();
459 * Main key event handler
461 void OnKeyEvent(const KeyEvent& event)
463 if(event.state == KeyEvent::Down)
465 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
472 //////////////////////////////////////////////////////////////
482 if(MOTION_BLUR_NUM_ACTOR_IMAGES == mCurrentImage)
487 Image blurImage = LoadImageFittedInBox( MOTION_BLUR_ACTOR_IMAGES[mCurrentImage], mMotionBlurActorSize.x, mMotionBlurActorSize.y );
489 mMotionBlurImageView.SetImage(blurImage);
490 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
491 mMotionBlurImageView2.SetImage(blurImage);
492 mMotionBlurImageView3.SetImage(blurImage);
493 mMotionBlurImageView4.SetImage(blurImage);
494 mMotionBlurImageView5.SetImage(blurImage);
500 Application& mApplication; ///< Application instance
501 Toolkit::Control mView;
502 Toolkit::ToolBar mToolBar;
504 Layer mContentLayer; ///< Content layer (contains actor for this blur demo)
506 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
509 Property::Map mMotionBlurEffect;
510 ImageView mMotionBlurImageView;
511 Size mMotionBlurActorSize;
513 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
514 ImageView mMotionBlurImageView2;
515 ImageView mMotionBlurImageView3;
516 ImageView mMotionBlurImageView4;
517 ImageView mMotionBlurImageView5;
518 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
520 // animate actor to position where user taps screen
521 Animation mActorTapMovementAnimation;
523 // show different animations to demonstrate blur effect working on an object only movement basis
524 bool mActorEffectsEnabled;
525 Animation mActorAnimation;
526 int mCurrentActorAnimation;
528 // offer a selection of images that user can cycle between
531 TapGestureDetector mTapGestureDetector;
533 DeviceOrientation mOrientation; ///< Current Device orientation
534 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
536 Popup mInstructionsPopup; ///< Info Popup
539 void RunTest(Application& app)
541 MotionBlurExampleApp test(app);
546 // Entry point for Linux & Tizen applications
548 int DALI_EXPORT_API main(int argc, char **argv)
550 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);