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( DALI_IMAGE_DIR "image-with-border-1.jpg" );
58 const char* MOTION_BLUR_ACTOR_IMAGE2( DALI_IMAGE_DIR "image-with-border-2.jpg" );
59 const char* MOTION_BLUR_ACTOR_IMAGE3( DALI_IMAGE_DIR "image-with-border-3.jpg" );
60 const char* MOTION_BLUR_ACTOR_IMAGE4( DALI_IMAGE_DIR "image-with-border-4.jpg" );
61 const char* MOTION_BLUR_ACTOR_IMAGE5( DALI_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 = DALI_IMAGE_DIR "background-default.png";
77 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
78 const char* LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-change.png" );
79 const char* LAYOUT_IMAGE_SELECTED( DALI_IMAGE_DIR "icon-change-selected.png" );
80 const char* APPLICATION_TITLE( "Motion Blur" );
81 const char* EFFECTS_OFF_ICON( DALI_IMAGE_DIR "icon-effects-off.png" );
82 const char* EFFECTS_OFF_ICON_SELECTED( DALI_IMAGE_DIR "icon-effects-off-selected.png" );
83 const char* EFFECTS_ON_ICON( DALI_IMAGE_DIR "icon-effects-on.png" );
84 const char* EFFECTS_ON_ICON_SELECTED( DALI_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 mMotionBlurImageActor = ImageActor::New(image);
213 mMotionBlurImageActor.SetParentOrigin( ParentOrigin::CENTER );
214 mMotionBlurImageActor.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
216 mContentLayer.Add( mMotionBlurImageActor );
218 // Create shader used for doing motion blur
219 mMotionBlurEffect = Toolkit::CreateMotionBlurEffect();
220 Dali::Property::Index uModelProperty = mMotionBlurEffect.GetPropertyIndex( "uModelLastFrame" );
221 Constraint constraint = Constraint::New<Matrix>( mMotionBlurEffect, uModelProperty, EqualToConstraint() );
222 constraint.AddSource( Source( mMotionBlurImageActor , Actor::Property::WORLD_MATRIX ) );
224 mMotionBlurImageActor.SetShaderEffect( mMotionBlurEffect );
227 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
229 ///////////////////////////////////////////////////////
231 // Motion blurred actor 2
234 mMotionBlurImageActor2 = ImageActor::New(image);
235 mMotionBlurImageActor2.SetParentOrigin( ParentOrigin::CENTER );
236 mMotionBlurImageActor2.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
237 mMotionBlurImageActor2.SetPosition(mMotionBlurActorSize.x * 1.1f, 0.0f);
238 mMotionBlurImageActor.Add( mMotionBlurImageActor2 );
240 // Create shader used for doing motion blur
241 mMotionBlurEffect2 = CreateMotionBlurEffect(MOTION_BLUR_NUM_SAMPLES);
243 // set actor shader to the blur one
244 mMotionBlurImageActor2.SetShaderEffect( mMotionBlurEffect2 );
247 ///////////////////////////////////////////////////////
249 // Motion blurred actor 3
252 mMotionBlurImageActor3 = ImageActor::New(image);
253 mMotionBlurImageActor3.SetParentOrigin( ParentOrigin::CENTER );
254 mMotionBlurImageActor3.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
255 mMotionBlurImageActor3.SetPosition(-mMotionBlurActorSize.x * 1.1f, 0.0f);
256 mMotionBlurImageActor.Add( mMotionBlurImageActor3 );
258 // Create shader used for doing motion blur
259 mMotionBlurEffect3 = CreateMotionBlurEffect(MOTION_BLUR_NUM_SAMPLES);
261 // set actor shader to the blur one
262 mMotionBlurImageActor3.SetShaderEffect( mMotionBlurEffect3 );
265 ///////////////////////////////////////////////////////
267 // Motion blurred actor 4
270 mMotionBlurImageActor4 = ImageActor::New(image);
271 mMotionBlurImageActor4.SetParentOrigin( ParentOrigin::CENTER );
272 mMotionBlurImageActor4.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
273 mMotionBlurImageActor4.SetPosition(0.0f, mMotionBlurActorSize.y * 1.1f);
274 mMotionBlurImageActor.Add( mMotionBlurImageActor4 );
276 // Create shader used for doing motion blur
277 mMotionBlurEffect4 = CreateMotionBlurEffect(MOTION_BLUR_NUM_SAMPLES);
279 // set actor shader to the blur one
280 mMotionBlurImageActor4.SetShaderEffect( mMotionBlurEffect4 );
283 ///////////////////////////////////////////////////////
285 // Motion blurred actor 5
288 mMotionBlurImageActor5 = ImageActor::New(image);
289 mMotionBlurImageActor5.SetParentOrigin( ParentOrigin::CENTER );
290 mMotionBlurImageActor5.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
291 mMotionBlurImageActor5.SetPosition(0.0f, -mMotionBlurActorSize.y * 1.1f);
292 mMotionBlurImageActor.Add( mMotionBlurImageActor5 );
294 // Create shader used for doing motion blur
295 mMotionBlurEffect5 = CreateMotionBlurEffect(MOTION_BLUR_NUM_SAMPLES);
297 // set actor shader to the blur one
298 mMotionBlurImageActor5.SetShaderEffect( mMotionBlurEffect5 );
299 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
302 //////////////////////////////////////////////////////////////
304 // Device Orientation Support
308 void OnOrientationChanged( Orientation orientation )
310 unsigned int degrees = orientation.GetDegrees();
311 Rotate( static_cast< DeviceOrientation >( degrees ) );
314 void Rotate( DeviceOrientation orientation )
316 // Resize the root actor
317 Vector2 stageSize = Stage::GetCurrent().GetSize();
318 Vector2 targetSize = stageSize;
319 if( orientation == LANDSCAPE ||
320 orientation == LANDSCAPE_INVERSE )
322 targetSize = Vector2( stageSize.y, stageSize.x );
325 if( mOrientation != orientation )
327 mOrientation = orientation;
329 // check if actor is on stage
330 if( mView.GetParent() )
332 // has parent so we expect it to be on stage, start animation
333 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
334 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -orientation ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
335 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
336 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
337 mRotateAnimation.Play();
341 // set the rotation to match the orientation
342 mView.SetOrientation( Degree( -orientation ), Vector3::ZAXIS );
343 mView.SetSize( targetSize );
348 // for first time just set size
349 mView.SetSize( targetSize );
354 //////////////////////////////////////////////////////////////
360 // move to point on screen that was tapped
361 void OnTap( Actor actor, const TapGesture& tapGesture )
364 float originOffsetX, originOffsetY;
366 // rotate offset (from top left origin to centre) into actor space
367 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
368 actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
370 // get dest point in local actor space
371 destPos.x = tapGesture.localPoint.x - originOffsetX;
372 destPos.y = tapGesture.localPoint.y - originOffsetY;
375 float animDuration = 0.5f;
376 mActorTapMovementAnimation = Animation::New( animDuration );
377 if ( mMotionBlurImageActor )
379 mActorTapMovementAnimation.AnimateTo( Property(mMotionBlurImageActor, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
381 mActorTapMovementAnimation.SetEndAction( Animation::Bake );
382 mActorTapMovementAnimation.Play();
385 // perform some spinning etc
386 if(mActorEffectsEnabled)
388 switch(mCurrentActorAnimation)
393 float animDuration = 1.0f;
394 mActorAnimation = Animation::New(animDuration);
395 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), 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( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
407 mActorAnimation.SetEndAction( Animation::Bake );
408 mActorAnimation.Play();
412 // spin around y and z
415 float animDuration = 1.0f;
416 mActorAnimation = Animation::New(animDuration);
417 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
418 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
419 mActorAnimation.SetEndAction( Animation::Bake );
420 mActorAnimation.Play();
427 float animDuration = 1.0f;
428 mActorAnimation = Animation::New(animDuration);
429 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
430 mActorAnimation.SetEndAction( Animation::Bake );
431 mActorAnimation.Play();
439 mCurrentActorAnimation++;
440 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
442 mCurrentActorAnimation = 0;
447 void ToggleActorEffects()
449 if(!mActorEffectsEnabled)
451 mActorEffectsEnabled = true;
452 mActorEffectsButton.SetUnselectedImage( EFFECTS_ON_ICON );
453 mActorEffectsButton.SetSelectedImage( EFFECTS_ON_ICON_SELECTED );
457 mActorEffectsEnabled = false;
458 mActorEffectsButton.SetUnselectedImage( EFFECTS_OFF_ICON );
459 mActorEffectsButton.SetSelectedImage( EFFECTS_OFF_ICON_SELECTED );
463 //////////////////////////////////////////////////////////////
469 bool OnLayoutButtonClicked( Toolkit::Button button )
475 bool OnEffectButtonClicked( Toolkit::Button button )
477 ToggleActorEffects();
482 * Main key event handler
484 void OnKeyEvent(const KeyEvent& event)
486 if(event.state == KeyEvent::Down)
488 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
495 //////////////////////////////////////////////////////////////
505 if(MOTION_BLUR_NUM_ACTOR_IMAGES == mCurrentImage)
510 Image blurImage = LoadImageFittedInBox( MOTION_BLUR_ACTOR_IMAGES[mCurrentImage], mMotionBlurActorSize.x, mMotionBlurActorSize.y );
511 mMotionBlurImageActor.SetImage(blurImage);
516 Application& mApplication; ///< Application instance
517 Toolkit::Control mView;
518 Toolkit::ToolBar mToolBar;
520 Layer mContentLayer; ///< Content layer (contains actor for this blur demo)
522 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
525 ShaderEffect mMotionBlurEffect;
526 ImageActor mMotionBlurImageActor;
527 Size mMotionBlurActorSize;
529 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
530 ShaderEffect mMotionBlurEffect2;
531 ShaderEffect mMotionBlurEffect3;
532 ShaderEffect mMotionBlurEffect4;
533 ShaderEffect mMotionBlurEffect5;
535 ImageActor mMotionBlurImageActor2;
536 ImageActor mMotionBlurImageActor3;
537 ImageActor mMotionBlurImageActor4;
538 ImageActor mMotionBlurImageActor5;
539 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
541 // animate actor to position where user taps screen
542 Animation mActorTapMovementAnimation;
544 // show different animations to demonstrate blur effect working on an object only movement basis
545 bool mActorEffectsEnabled;
546 Animation mActorAnimation;
547 int mCurrentActorAnimation;
549 // offer a selection of images that user can cycle between
552 TapGestureDetector mTapGestureDetector;
554 DeviceOrientation mOrientation; ///< Current Device orientation
555 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
557 Popup mInstructionsPopup; ///< Info Popup
560 void RunTest(Application& app)
562 MotionBlurExampleApp test(app);
567 // Entry point for Linux & Tizen applications
569 int main(int argc, char **argv)
571 Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);