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 mIconEffectsOff = ResourceImage::New( EFFECTS_OFF_ICON );
170 mIconEffectsOffSelected = ResourceImage::New( EFFECTS_OFF_ICON_SELECTED );
171 mIconEffectsOn = ResourceImage::New( EFFECTS_ON_ICON );
172 mIconEffectsOnSelected = ResourceImage::New( EFFECTS_ON_ICON_SELECTED );
173 mActorEffectsButton = Toolkit::PushButton::New();
174 mActorEffectsButton.SetButtonImage( mIconEffectsOff );
175 mActorEffectsButton.SetSelectedImage( mIconEffectsOffSelected );
176 mActorEffectsButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnEffectButtonClicked );
177 mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
179 // Creates a mode button.
180 // Create a effect toggle button. (right of toolbar)
181 Image imageLayout = ResourceImage::New( LAYOUT_IMAGE );
182 Image imageLayoutSelected = ResourceImage::New( LAYOUT_IMAGE_SELECTED );
183 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
184 layoutButton.SetButtonImage( imageLayout );
185 layoutButton.SetSelectedImage( imageLayoutSelected );
186 layoutButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnLayoutButtonClicked);
187 layoutButton.SetLeaveRequired( true );
188 mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
191 mTapGestureDetector = TapGestureDetector::New();
192 mTapGestureDetector.Attach( mContentLayer );
193 mTapGestureDetector.DetectedSignal().Connect( this, &MotionBlurExampleApp::OnTap );
195 Dali::Window winHandle = app.GetWindow();
196 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
197 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
198 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
199 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
201 // set initial orientation
202 // winHandle.GetOrientation().ChangedSignal().Connect( this, &MotionBlurExampleApp::OnOrientationChanged );
203 unsigned int degrees = 0;
204 Rotate( static_cast< DeviceOrientation >( degrees ) );
207 ///////////////////////////////////////////////////////
209 // Motion blurred actor
212 // Scale down actor to fit on very low resolution screens with space to interact:
213 Size stageSize = Stage::GetCurrent().GetSize();
214 mMotionBlurActorSize = Size( std::min( stageSize.x * 0.3f, MOTION_BLUR_ACTOR_WIDTH ), std::min( stageSize.y * 0.3f, MOTION_BLUR_ACTOR_HEIGHT ) );
215 mMotionBlurActorSize = Size( std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ), std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ) );
217 Image image = LoadImageFittedInBox( MOTION_BLUR_ACTOR_IMAGE1, mMotionBlurActorSize.x, mMotionBlurActorSize.y );
218 mMotionBlurImageActor = ImageActor::New(image);
219 mMotionBlurImageActor.SetParentOrigin( ParentOrigin::CENTER );
220 mMotionBlurImageActor.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
222 mContentLayer.Add( mMotionBlurImageActor );
224 // Create shader used for doing motion blur
225 mMotionBlurEffect = Toolkit::CreateMotionBlurEffect();
226 Dali::Property::Index uModelProperty = mMotionBlurEffect.GetPropertyIndex( "uModelLastFrame" );
227 Constraint constraint = Constraint::New<Matrix>( mMotionBlurEffect, uModelProperty, EqualToConstraint() );
228 constraint.AddSource( Source( mMotionBlurImageActor , Actor::Property::WORLD_MATRIX ) );
230 mMotionBlurImageActor.SetShaderEffect( mMotionBlurEffect );
233 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
235 ///////////////////////////////////////////////////////
237 // Motion blurred actor 2
240 mMotionBlurImageActor2 = ImageActor::New(image);
241 mMotionBlurImageActor2.SetParentOrigin( ParentOrigin::CENTER );
242 mMotionBlurImageActor2.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
243 mMotionBlurImageActor2.SetPosition(mMotionBlurActorSize.x * 1.1f, 0.0f);
244 mMotionBlurImageActor.Add( mMotionBlurImageActor2 );
246 // Create shader used for doing motion blur
247 mMotionBlurEffect2 = CreateMotionBlurEffect(MOTION_BLUR_NUM_SAMPLES);
249 // set actor shader to the blur one
250 mMotionBlurImageActor2.SetShaderEffect( mMotionBlurEffect2 );
253 ///////////////////////////////////////////////////////
255 // Motion blurred actor 3
258 mMotionBlurImageActor3 = ImageActor::New(image);
259 mMotionBlurImageActor3.SetParentOrigin( ParentOrigin::CENTER );
260 mMotionBlurImageActor3.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
261 mMotionBlurImageActor3.SetPosition(-mMotionBlurActorSize.x * 1.1f, 0.0f);
262 mMotionBlurImageActor.Add( mMotionBlurImageActor3 );
264 // Create shader used for doing motion blur
265 mMotionBlurEffect3 = CreateMotionBlurEffect(MOTION_BLUR_NUM_SAMPLES);
267 // set actor shader to the blur one
268 mMotionBlurImageActor3.SetShaderEffect( mMotionBlurEffect3 );
271 ///////////////////////////////////////////////////////
273 // Motion blurred actor 4
276 mMotionBlurImageActor4 = ImageActor::New(image);
277 mMotionBlurImageActor4.SetParentOrigin( ParentOrigin::CENTER );
278 mMotionBlurImageActor4.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
279 mMotionBlurImageActor4.SetPosition(0.0f, mMotionBlurActorSize.y * 1.1f);
280 mMotionBlurImageActor.Add( mMotionBlurImageActor4 );
282 // Create shader used for doing motion blur
283 mMotionBlurEffect4 = CreateMotionBlurEffect(MOTION_BLUR_NUM_SAMPLES);
285 // set actor shader to the blur one
286 mMotionBlurImageActor4.SetShaderEffect( mMotionBlurEffect4 );
289 ///////////////////////////////////////////////////////
291 // Motion blurred actor 5
294 mMotionBlurImageActor5 = ImageActor::New(image);
295 mMotionBlurImageActor5.SetParentOrigin( ParentOrigin::CENTER );
296 mMotionBlurImageActor5.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
297 mMotionBlurImageActor5.SetPosition(0.0f, -mMotionBlurActorSize.y * 1.1f);
298 mMotionBlurImageActor.Add( mMotionBlurImageActor5 );
300 // Create shader used for doing motion blur
301 mMotionBlurEffect5 = CreateMotionBlurEffect(MOTION_BLUR_NUM_SAMPLES);
303 // set actor shader to the blur one
304 mMotionBlurImageActor5.SetShaderEffect( mMotionBlurEffect5 );
305 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
308 //////////////////////////////////////////////////////////////
310 // Device Orientation Support
314 void OnOrientationChanged( Orientation orientation )
316 unsigned int degrees = orientation.GetDegrees();
317 Rotate( static_cast< DeviceOrientation >( degrees ) );
320 void Rotate( DeviceOrientation orientation )
322 // Resize the root actor
323 Vector2 stageSize = Stage::GetCurrent().GetSize();
324 Vector2 targetSize = stageSize;
325 if( orientation == LANDSCAPE ||
326 orientation == LANDSCAPE_INVERSE )
328 targetSize = Vector2( stageSize.y, stageSize.x );
331 if( mOrientation != orientation )
333 mOrientation = orientation;
335 // check if actor is on stage
336 if( mView.GetParent() )
338 // has parent so we expect it to be on stage, start animation
339 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
340 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -orientation ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
341 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
342 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
343 mRotateAnimation.Play();
347 // set the rotation to match the orientation
348 mView.SetOrientation( Degree( -orientation ), Vector3::ZAXIS );
349 mView.SetSize( targetSize );
354 // for first time just set size
355 mView.SetSize( targetSize );
360 //////////////////////////////////////////////////////////////
366 // move to point on screen that was tapped
367 void OnTap( Actor actor, const TapGesture& tapGesture )
370 float originOffsetX, originOffsetY;
372 // rotate offset (from top left origin to centre) into actor space
373 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
374 actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
376 // get dest point in local actor space
377 destPos.x = tapGesture.localPoint.x - originOffsetX;
378 destPos.y = tapGesture.localPoint.y - originOffsetY;
381 float animDuration = 0.5f;
382 mActorTapMovementAnimation = Animation::New( animDuration );
383 if ( mMotionBlurImageActor )
385 mActorTapMovementAnimation.AnimateTo( Property(mMotionBlurImageActor, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
387 mActorTapMovementAnimation.SetEndAction( Animation::Bake );
388 mActorTapMovementAnimation.Play();
391 // perform some spinning etc
392 if(mActorEffectsEnabled)
394 switch(mCurrentActorAnimation)
399 float animDuration = 1.0f;
400 mActorAnimation = Animation::New(animDuration);
401 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
402 mActorAnimation.SetEndAction( Animation::Bake );
403 mActorAnimation.Play();
410 float animDuration = 1.0f;
411 mActorAnimation = Animation::New(animDuration);
412 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
413 mActorAnimation.SetEndAction( Animation::Bake );
414 mActorAnimation.Play();
418 // spin around y and z
421 float animDuration = 1.0f;
422 mActorAnimation = Animation::New(animDuration);
423 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
424 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
425 mActorAnimation.SetEndAction( Animation::Bake );
426 mActorAnimation.Play();
433 float animDuration = 1.0f;
434 mActorAnimation = Animation::New(animDuration);
435 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
436 mActorAnimation.SetEndAction( Animation::Bake );
437 mActorAnimation.Play();
445 mCurrentActorAnimation++;
446 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
448 mCurrentActorAnimation = 0;
453 void ToggleActorEffects()
455 if(!mActorEffectsEnabled)
457 mActorEffectsEnabled = true;
458 mActorEffectsButton.SetButtonImage( mIconEffectsOn );
459 mActorEffectsButton.SetSelectedImage( mIconEffectsOnSelected );
463 mActorEffectsEnabled = false;
464 mActorEffectsButton.SetButtonImage( mIconEffectsOff );
465 mActorEffectsButton.SetSelectedImage( mIconEffectsOffSelected );
469 //////////////////////////////////////////////////////////////
475 bool OnLayoutButtonClicked( Toolkit::Button button )
481 bool OnEffectButtonClicked( Toolkit::Button button )
483 ToggleActorEffects();
488 * Main key event handler
490 void OnKeyEvent(const KeyEvent& event)
492 if(event.state == KeyEvent::Down)
494 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
501 //////////////////////////////////////////////////////////////
511 if(MOTION_BLUR_NUM_ACTOR_IMAGES == mCurrentImage)
516 Image blurImage = LoadImageFittedInBox( MOTION_BLUR_ACTOR_IMAGES[mCurrentImage], mMotionBlurActorSize.x, mMotionBlurActorSize.y );
517 mMotionBlurImageActor.SetImage(blurImage);
522 Application& mApplication; ///< Application instance
523 Toolkit::Control mView;
524 Toolkit::ToolBar mToolBar;
525 Image mIconEffectsOff;
526 Image mIconEffectsOffSelected;
527 Image mIconEffectsOn;
528 Image mIconEffectsOnSelected;
530 Layer mContentLayer; ///< Content layer (contains actor for this blur demo)
532 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
535 ShaderEffect mMotionBlurEffect;
536 ImageActor mMotionBlurImageActor;
537 Size mMotionBlurActorSize;
539 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
540 ShaderEffect mMotionBlurEffect2;
541 ShaderEffect mMotionBlurEffect3;
542 ShaderEffect mMotionBlurEffect4;
543 ShaderEffect mMotionBlurEffect5;
545 ImageActor mMotionBlurImageActor2;
546 ImageActor mMotionBlurImageActor3;
547 ImageActor mMotionBlurImageActor4;
548 ImageActor mMotionBlurImageActor5;
549 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
551 // animate actor to position where user taps screen
552 Animation mActorTapMovementAnimation;
554 // show different animations to demonstrate blur effect working on an object only movement basis
555 bool mActorEffectsEnabled;
556 Animation mActorAnimation;
557 int mCurrentActorAnimation;
559 // offer a selection of images that user can cycle between
562 TapGestureDetector mTapGestureDetector;
564 DeviceOrientation mOrientation; ///< Current Device orientation
565 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
567 Popup mInstructionsPopup; ///< Info Popup
570 void RunTest(Application& app)
572 MotionBlurExampleApp test(app);
577 // Entry point for Linux & Tizen applications
579 int main(int argc, char **argv)
581 Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);