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* APPLICATION_TITLE( "Motion Blur" );
80 const char* EFFECTS_OFF_ICON( DALI_IMAGE_DIR "icon-effects-off.png" );
81 const char* EFFECTS_ON_ICON( DALI_IMAGE_DIR "icon-effects-on.png" );
83 const float UI_MARGIN = 4.0f; ///< Screen Margin for placement of UI buttons
85 const Vector3 BUTTON_SIZE_CONSTRAINT( 0.24f, 0.09f, 1.0f );
86 const Vector3 BUTTON_TITLE_LABEL_TAP_HERE_SIZE_CONSTRAINT( 0.55f, 0.06f, 1.0f );
87 const Vector3 BUTTON_TITLE_LABEL_INSTRUCTIONS_POPUP_SIZE_CONSTRAINT( 1.0f, 1.0f, 1.0f );
89 // move this button down a bit so it is visible on target and not covered up by toolbar
90 const float BUTTON_TITLE_LABEL_Y_OFFSET = 0.05f;
92 const float ORIENTATION_DURATION = 0.5f; ///< Time to rotate to new orientation.
95 * @brief Load an image, scaled-down to no more than the dimensions passed in.
97 * Uses SHRINK_TO_FIT which ensures the resulting image is
98 * smaller than or equal to the specified dimensions while preserving its
99 * original aspect ratio.
101 ResourceImage LoadImageFittedInBox( const char * const imagePath, uint32_t maxWidth, uint32_t maxHeight )
103 // Load the image nicely scaled-down to fit within the specified max width and height:
104 return ResourceImage::New( imagePath, ImageDimensions( maxWidth, maxHeight ), FittingMode::SHRINK_TO_FIT, Dali::SamplingMode::BOX_THEN_LINEAR );
107 } // unnamed namespace
111 class MotionBlurExampleApp : public ConnectionTracker
116 * DeviceOrientation describes the four different
117 * orientations the device can be in based on accelerometer reports.
119 enum DeviceOrientation
123 PORTRAIT_INVERSE = 180,
124 LANDSCAPE_INVERSE = 270
129 * @param application class, stored as reference
131 MotionBlurExampleApp(Application &app)
133 mActorEffectsEnabled(false),
134 mCurrentActorAnimation(0),
137 // Connect to the Application's Init signal
138 app.InitSignal().Connect(this, &MotionBlurExampleApp::OnInit);
141 ~MotionBlurExampleApp()
143 // Nothing to do here; everything gets deleted automatically
147 * This method gets called once the main loop of application is up and running
149 void OnInit(Application& app)
151 // The Init signal is received once (only) during the Application lifetime
153 Stage::GetCurrent().KeyEventSignal().Connect(this, &MotionBlurExampleApp::OnKeyEvent);
156 // Creates a default view with a default tool bar.
157 // The view is added to the stage.
158 mContentLayer = DemoHelper::CreateView( mApplication,
161 BACKGROUND_IMAGE_PATH,
165 //Add an effects icon on the right of the title
166 mIconEffectsOff = ResourceImage::New( EFFECTS_OFF_ICON );
167 mIconEffectsOn = ResourceImage::New( EFFECTS_ON_ICON );
168 mActorEffectsButton = Toolkit::PushButton::New();
169 mActorEffectsButton.SetBackgroundImage( mIconEffectsOff );
170 mActorEffectsButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnEffectButtonClicked );
171 mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
173 // Creates a mode button.
174 // Create a effect toggle button. (right of toolbar)
175 Image imageLayout = ResourceImage::New( LAYOUT_IMAGE );
176 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
177 layoutButton.SetBackgroundImage(imageLayout);
178 layoutButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnLayoutButtonClicked);
179 layoutButton.SetLeaveRequired( true );
180 mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
183 mTapGestureDetector = TapGestureDetector::New();
184 mTapGestureDetector.Attach( mContentLayer );
185 mTapGestureDetector.DetectedSignal().Connect( this, &MotionBlurExampleApp::OnTap );
187 Dali::Window winHandle = app.GetWindow();
188 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
189 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
190 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
191 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
193 // set initial orientation
194 // winHandle.GetOrientation().ChangedSignal().Connect( this, &MotionBlurExampleApp::OnOrientationChanged );
195 unsigned int degrees = 0;
196 Rotate( static_cast< DeviceOrientation >( degrees ) );
199 ///////////////////////////////////////////////////////
201 // Motion blurred actor
204 // Scale down actor to fit on very low resolution screens with space to interact:
205 Size stageSize = Stage::GetCurrent().GetSize();
206 mMotionBlurActorSize = Size( std::min( stageSize.x * 0.3f, MOTION_BLUR_ACTOR_WIDTH ), std::min( stageSize.y * 0.3f, MOTION_BLUR_ACTOR_HEIGHT ) );
207 mMotionBlurActorSize = Size( std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ), std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ) );
209 Image image = LoadImageFittedInBox( MOTION_BLUR_ACTOR_IMAGE1, mMotionBlurActorSize.x, mMotionBlurActorSize.y );
210 mMotionBlurImageActor = ImageActor::New(image);
211 mMotionBlurImageActor.SetParentOrigin( ParentOrigin::CENTER );
212 mMotionBlurImageActor.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
214 mContentLayer.Add( mMotionBlurImageActor );
216 // Create shader used for doing motion blur
217 mMotionBlurEffect = Toolkit::CreateMotionBlurEffect();
218 Dali::Property::Index uModelProperty = mMotionBlurEffect.GetPropertyIndex( "uModelLastFrame" );
219 Constraint constraint = Constraint::New<Matrix>( mMotionBlurEffect, uModelProperty, EqualToConstraint() );
220 constraint.AddSource( Source( mMotionBlurImageActor , Actor::Property::WORLD_MATRIX ) );
222 mMotionBlurImageActor.SetShaderEffect( mMotionBlurEffect );
225 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
227 ///////////////////////////////////////////////////////
229 // Motion blurred actor 2
232 mMotionBlurImageActor2 = ImageActor::New(image);
233 mMotionBlurImageActor2.SetParentOrigin( ParentOrigin::CENTER );
234 mMotionBlurImageActor2.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
235 mMotionBlurImageActor2.SetPosition(mMotionBlurActorSize.x * 1.1f, 0.0f);
236 mMotionBlurImageActor.Add( mMotionBlurImageActor2 );
238 // Create shader used for doing motion blur
239 mMotionBlurEffect2 = CreateMotionBlurEffect(MOTION_BLUR_NUM_SAMPLES);
241 // set actor shader to the blur one
242 mMotionBlurImageActor2.SetShaderEffect( mMotionBlurEffect2 );
245 ///////////////////////////////////////////////////////
247 // Motion blurred actor 3
250 mMotionBlurImageActor3 = ImageActor::New(image);
251 mMotionBlurImageActor3.SetParentOrigin( ParentOrigin::CENTER );
252 mMotionBlurImageActor3.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
253 mMotionBlurImageActor3.SetPosition(-mMotionBlurActorSize.x * 1.1f, 0.0f);
254 mMotionBlurImageActor.Add( mMotionBlurImageActor3 );
256 // Create shader used for doing motion blur
257 mMotionBlurEffect3 = CreateMotionBlurEffect(MOTION_BLUR_NUM_SAMPLES);
259 // set actor shader to the blur one
260 mMotionBlurImageActor3.SetShaderEffect( mMotionBlurEffect3 );
263 ///////////////////////////////////////////////////////
265 // Motion blurred actor 4
268 mMotionBlurImageActor4 = ImageActor::New(image);
269 mMotionBlurImageActor4.SetParentOrigin( ParentOrigin::CENTER );
270 mMotionBlurImageActor4.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
271 mMotionBlurImageActor4.SetPosition(0.0f, mMotionBlurActorSize.y * 1.1f);
272 mMotionBlurImageActor.Add( mMotionBlurImageActor4 );
274 // Create shader used for doing motion blur
275 mMotionBlurEffect4 = CreateMotionBlurEffect(MOTION_BLUR_NUM_SAMPLES);
277 // set actor shader to the blur one
278 mMotionBlurImageActor4.SetShaderEffect( mMotionBlurEffect4 );
281 ///////////////////////////////////////////////////////
283 // Motion blurred actor 5
286 mMotionBlurImageActor5 = ImageActor::New(image);
287 mMotionBlurImageActor5.SetParentOrigin( ParentOrigin::CENTER );
288 mMotionBlurImageActor5.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
289 mMotionBlurImageActor5.SetPosition(0.0f, -mMotionBlurActorSize.y * 1.1f);
290 mMotionBlurImageActor.Add( mMotionBlurImageActor5 );
292 // Create shader used for doing motion blur
293 mMotionBlurEffect5 = CreateMotionBlurEffect(MOTION_BLUR_NUM_SAMPLES);
295 // set actor shader to the blur one
296 mMotionBlurImageActor5.SetShaderEffect( mMotionBlurEffect5 );
297 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
300 //////////////////////////////////////////////////////////////
302 // Device Orientation Support
306 void OnOrientationChanged( Orientation orientation )
308 unsigned int degrees = orientation.GetDegrees();
309 Rotate( static_cast< DeviceOrientation >( degrees ) );
312 void Rotate( DeviceOrientation orientation )
314 // Resize the root actor
315 Vector2 stageSize = Stage::GetCurrent().GetSize();
316 Vector2 targetSize = stageSize;
317 if( orientation == LANDSCAPE ||
318 orientation == LANDSCAPE_INVERSE )
320 targetSize = Vector2( stageSize.y, stageSize.x );
323 if( mOrientation != orientation )
325 mOrientation = orientation;
327 // check if actor is on stage
328 if( mView.GetParent() )
330 // has parent so we expect it to be on stage, start animation
331 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
332 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -orientation ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
333 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
334 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
335 mRotateAnimation.Play();
339 // set the rotation to match the orientation
340 mView.SetOrientation( Degree( -orientation ), Vector3::ZAXIS );
341 mView.SetSize( targetSize );
346 // for first time just set size
347 mView.SetSize( targetSize );
352 //////////////////////////////////////////////////////////////
358 // move to point on screen that was tapped
359 void OnTap( Actor actor, const TapGesture& tapGesture )
362 float originOffsetX, originOffsetY;
364 // rotate offset (from top left origin to centre) into actor space
365 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
366 actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
368 // get dest point in local actor space
369 destPos.x = tapGesture.localPoint.x - originOffsetX;
370 destPos.y = tapGesture.localPoint.y - originOffsetY;
373 float animDuration = 0.5f;
374 mActorTapMovementAnimation = Animation::New( animDuration );
375 if ( mMotionBlurImageActor )
377 mActorTapMovementAnimation.AnimateTo( Property(mMotionBlurImageActor, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
379 mActorTapMovementAnimation.SetEndAction( Animation::Bake );
380 mActorTapMovementAnimation.Play();
383 // perform some spinning etc
384 if(mActorEffectsEnabled)
386 switch(mCurrentActorAnimation)
391 float animDuration = 1.0f;
392 mActorAnimation = Animation::New(animDuration);
393 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
394 mActorAnimation.SetEndAction( Animation::Bake );
395 mActorAnimation.Play();
402 float animDuration = 1.0f;
403 mActorAnimation = Animation::New(animDuration);
404 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
405 mActorAnimation.SetEndAction( Animation::Bake );
406 mActorAnimation.Play();
410 // spin around y and z
413 float animDuration = 1.0f;
414 mActorAnimation = Animation::New(animDuration);
415 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
416 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
417 mActorAnimation.SetEndAction( Animation::Bake );
418 mActorAnimation.Play();
425 float animDuration = 1.0f;
426 mActorAnimation = Animation::New(animDuration);
427 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
428 mActorAnimation.SetEndAction( Animation::Bake );
429 mActorAnimation.Play();
437 mCurrentActorAnimation++;
438 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
440 mCurrentActorAnimation = 0;
445 void ToggleActorEffects()
447 if(!mActorEffectsEnabled)
449 mActorEffectsEnabled = true;
450 mActorEffectsButton.SetBackgroundImage( mIconEffectsOn );
454 mActorEffectsEnabled = false;
455 mActorEffectsButton.SetBackgroundImage( mIconEffectsOff );
459 //////////////////////////////////////////////////////////////
465 bool OnLayoutButtonClicked( Toolkit::Button button )
471 bool OnEffectButtonClicked( Toolkit::Button button )
473 ToggleActorEffects();
478 * Main key event handler
480 void OnKeyEvent(const KeyEvent& event)
482 if(event.state == KeyEvent::Down)
484 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
491 //////////////////////////////////////////////////////////////
501 if(MOTION_BLUR_NUM_ACTOR_IMAGES == mCurrentImage)
506 Image blurImage = LoadImageFittedInBox( MOTION_BLUR_ACTOR_IMAGES[mCurrentImage], mMotionBlurActorSize.x, mMotionBlurActorSize.y );
507 mMotionBlurImageActor.SetImage(blurImage);
512 Application& mApplication; ///< Application instance
513 Toolkit::Control mView;
514 Toolkit::ToolBar mToolBar;
515 Image mIconEffectsOff;
516 Image mIconEffectsOn;
518 Layer mContentLayer; ///< Content layer (contains actor for this blur demo)
520 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
523 ShaderEffect mMotionBlurEffect;
524 ImageActor mMotionBlurImageActor;
525 Size mMotionBlurActorSize;
527 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
528 ShaderEffect mMotionBlurEffect2;
529 ShaderEffect mMotionBlurEffect3;
530 ShaderEffect mMotionBlurEffect4;
531 ShaderEffect mMotionBlurEffect5;
533 ImageActor mMotionBlurImageActor2;
534 ImageActor mMotionBlurImageActor3;
535 ImageActor mMotionBlurImageActor4;
536 ImageActor mMotionBlurImageActor5;
537 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
539 // animate actor to position where user taps screen
540 Animation mActorTapMovementAnimation;
542 // show different animations to demonstrate blur effect working on an object only movement basis
543 bool mActorEffectsEnabled;
544 Animation mActorAnimation;
545 int mCurrentActorAnimation;
547 // offer a selection of images that user can cycle between
550 TapGestureDetector mTapGestureDetector;
552 DeviceOrientation mOrientation; ///< Current Device orientation
553 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
555 Popup mInstructionsPopup; ///< Info Popup
558 void RunTest(Application& app)
560 MotionBlurExampleApp test(app);
565 // Entry point for Linux & Tizen applications
567 int main(int argc, char **argv)
569 Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);