2 * Copyright (c) 2016 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 Set an image to image view, 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 original aspect ratio.
103 void SetImageFittedInBox( ImageView& imageView, Property::Map& shaderEffect, const char * const imagePath, int maxWidth, int maxHeight )
106 map[Visual::Property::TYPE] = Visual::IMAGE;
107 map[ImageVisual::Property::URL] = imagePath;
108 // Load the image nicely scaled-down to fit within the specified max width and height:
109 map[ImageVisual::Property::DESIRED_WIDTH] = maxWidth;
110 map[ImageVisual::Property::DESIRED_HEIGHT] = maxHeight;
111 map[ImageVisual::Property::FITTING_MODE] = FittingMode::SHRINK_TO_FIT;
112 map[ImageVisual::Property::SAMPLING_MODE] = SamplingMode::BOX_THEN_LINEAR;
113 map.Merge( shaderEffect );
115 imageView.SetProperty( ImageView::Property::IMAGE, map );
118 } // unnamed namespace
122 class MotionBlurExampleApp : public ConnectionTracker
127 * DeviceOrientation describes the four different
128 * orientations the device can be in based on accelerometer reports.
130 enum DeviceOrientation
134 PORTRAIT_INVERSE = 180,
135 LANDSCAPE_INVERSE = 270
140 * @param application class, stored as reference
142 MotionBlurExampleApp(Application &app)
144 mActorEffectsEnabled(false),
145 mCurrentActorAnimation(0),
147 mOrientation( PORTRAIT )
149 // Connect to the Application's Init signal
150 app.InitSignal().Connect(this, &MotionBlurExampleApp::OnInit);
153 ~MotionBlurExampleApp()
155 // Nothing to do here; everything gets deleted automatically
159 * This method gets called once the main loop of application is up and running
161 void OnInit(Application& app)
163 // The Init signal is received once (only) during the Application lifetime
165 Stage::GetCurrent().KeyEventSignal().Connect(this, &MotionBlurExampleApp::OnKeyEvent);
168 // Creates a default view with a default tool bar.
169 // The view is added to the stage.
170 mContentLayer = DemoHelper::CreateView( mApplication,
173 BACKGROUND_IMAGE_PATH,
177 //Add an effects icon on the right of the title
178 mActorEffectsButton = Toolkit::PushButton::New();
179 mActorEffectsButton.SetUnselectedImage( EFFECTS_OFF_ICON );
180 mActorEffectsButton.SetSelectedImage( EFFECTS_OFF_ICON_SELECTED );
181 mActorEffectsButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnEffectButtonClicked );
182 mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
184 // Creates a mode button.
185 // Create a effect toggle button. (right of toolbar)
186 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
187 layoutButton.SetUnselectedImage( LAYOUT_IMAGE );
188 layoutButton.SetSelectedImage( LAYOUT_IMAGE_SELECTED );
189 layoutButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnLayoutButtonClicked);
190 layoutButton.SetLeaveRequired( true );
191 mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
194 mTapGestureDetector = TapGestureDetector::New();
195 mTapGestureDetector.Attach( mContentLayer );
196 mTapGestureDetector.DetectedSignal().Connect( this, &MotionBlurExampleApp::OnTap );
198 Dali::Window winHandle = app.GetWindow();
199 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
200 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
201 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
202 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
204 // set initial orientation
205 unsigned int degrees = 0;
206 Rotate( static_cast< DeviceOrientation >( degrees ) );
209 ///////////////////////////////////////////////////////
211 // Motion blurred actor
214 // Scale down actor to fit on very low resolution screens with space to interact:
215 Size stageSize = Stage::GetCurrent().GetSize();
216 mMotionBlurActorSize = Size( std::min( stageSize.x * 0.3f, MOTION_BLUR_ACTOR_WIDTH ), std::min( stageSize.y * 0.3f, MOTION_BLUR_ACTOR_HEIGHT ) );
217 mMotionBlurActorSize = Size( std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ), std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ) );
219 mMotionBlurEffect = CreateMotionBlurEffect();
220 mMotionBlurImageView = ImageView::New();
221 SetImageFittedInBox( mMotionBlurImageView, mMotionBlurEffect, MOTION_BLUR_ACTOR_IMAGE1, mMotionBlurActorSize.x, mMotionBlurActorSize.y );
222 mMotionBlurImageView.SetParentOrigin( ParentOrigin::CENTER );
223 mMotionBlurImageView.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
225 mContentLayer.Add( mMotionBlurImageView );
227 // Create shader used for doing motion blur
228 mMotionBlurEffect = CreateMotionBlurEffect();
230 // set actor shader to the blur one
231 Toolkit::SetMotionBlurProperties( mMotionBlurImageView, MOTION_BLUR_NUM_SAMPLES );
234 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
236 ///////////////////////////////////////////////////////
238 // Motion blurred actor 2
241 mMotionBlurImageView2 = ImageView::New(image);
242 mMotionBlurImageView2.SetParentOrigin( ParentOrigin::CENTER );
243 mMotionBlurImageView2.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
244 mMotionBlurImageView2.SetPosition(mMotionBlurActorSize.x * 1.1f, 0.0f);
245 mMotionBlurImageView.Add( mMotionBlurImageView2 );
247 // set actor shader to the blur one
248 Toolkit::SetMotionBlurProperties( mMotionBlurImageView2, MOTION_BLUR_NUM_SAMPLES );
249 mMotionBlurImageView2.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
252 ///////////////////////////////////////////////////////
254 // Motion blurred actor 3
257 mMotionBlurImageView3 = ImageView::New(image);
258 mMotionBlurImageView3.SetParentOrigin( ParentOrigin::CENTER );
259 mMotionBlurImageView3.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
260 mMotionBlurImageView3.SetPosition(-mMotionBlurActorSize.x * 1.1f, 0.0f);
261 mMotionBlurImageView.Add( mMotionBlurImageView3 );
263 // set actor shader to the blur one
264 Toolkit::SetMotionBlurProperties( mMotionBlurImageView3, MOTION_BLUR_NUM_SAMPLES );
265 mMotionBlurImageView3.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
268 ///////////////////////////////////////////////////////
270 // Motion blurred actor 4
273 mMotionBlurImageView4 = ImageView::New(image);
274 mMotionBlurImageView4.SetParentOrigin( ParentOrigin::CENTER );
275 mMotionBlurImageView4.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
276 mMotionBlurImageView4.SetPosition(0.0f, mMotionBlurActorSize.y * 1.1f);
277 mMotionBlurImageView.Add( mMotionBlurImageView4 );
279 // set actor shader to the blur one
280 Toolkit::SetMotionBlurProperties( mMotionBlurImageView4, MOTION_BLUR_NUM_SAMPLES );
281 mMotionBlurImageView4.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
283 ///////////////////////////////////////////////////////
285 // Motion blurred actor 5
288 mMotionBlurImageView5 = ImageView::New(image);
289 mMotionBlurImageView5.SetParentOrigin( ParentOrigin::CENTER );
290 mMotionBlurImageView5.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
291 mMotionBlurImageView5.SetPosition(0.0f, -mMotionBlurActorSize.y * 1.1f);
292 mMotionBlurImageView.Add( mMotionBlurImageView5 );
294 // set actor shader to the blur one
295 Toolkit::SetMotionBlurProperties( mMotionBlurImageView5, MOTION_BLUR_NUM_SAMPLES );
296 mMotionBlurImageView5.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
297 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
300 void Rotate( DeviceOrientation orientation )
302 // Resize the root actor
303 Vector2 stageSize = Stage::GetCurrent().GetSize();
304 Vector2 targetSize = stageSize;
305 if( orientation == LANDSCAPE ||
306 orientation == LANDSCAPE_INVERSE )
308 targetSize = Vector2( stageSize.y, stageSize.x );
311 if( mOrientation != orientation )
313 mOrientation = orientation;
315 // check if actor is on stage
316 if( mView.GetParent() )
318 // has parent so we expect it to be on stage, start animation
319 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
320 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -orientation ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
321 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
322 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
323 mRotateAnimation.Play();
327 // set the rotation to match the orientation
328 mView.SetOrientation( Degree( -orientation ), Vector3::ZAXIS );
329 mView.SetSize( targetSize );
334 // for first time just set size
335 mView.SetSize( targetSize );
340 //////////////////////////////////////////////////////////////
346 // move to point on screen that was tapped
347 void OnTap( Actor actor, const TapGesture& tapGesture )
350 float originOffsetX, originOffsetY;
352 // rotate offset (from top left origin to centre) into actor space
353 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
354 actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
356 // get dest point in local actor space
357 destPos.x = tapGesture.localPoint.x - originOffsetX;
358 destPos.y = tapGesture.localPoint.y - originOffsetY;
361 float animDuration = 0.5f;
362 mActorTapMovementAnimation = Animation::New( animDuration );
363 if ( mMotionBlurImageView )
365 mActorTapMovementAnimation.AnimateTo( Property(mMotionBlurImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
367 mActorTapMovementAnimation.SetEndAction( Animation::Bake );
368 mActorTapMovementAnimation.Play();
371 // perform some spinning etc
372 if(mActorEffectsEnabled)
374 switch(mCurrentActorAnimation)
379 float animDuration = 1.0f;
380 mActorAnimation = Animation::New(animDuration);
381 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
382 mActorAnimation.SetEndAction( Animation::Bake );
383 mActorAnimation.Play();
390 float animDuration = 1.0f;
391 mActorAnimation = Animation::New(animDuration);
392 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
393 mActorAnimation.SetEndAction( Animation::Bake );
394 mActorAnimation.Play();
398 // spin around y and z
401 float animDuration = 1.0f;
402 mActorAnimation = Animation::New(animDuration);
403 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
404 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
405 mActorAnimation.SetEndAction( Animation::Bake );
406 mActorAnimation.Play();
413 float animDuration = 1.0f;
414 mActorAnimation = Animation::New(animDuration);
415 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
416 mActorAnimation.SetEndAction( Animation::Bake );
417 mActorAnimation.Play();
425 mCurrentActorAnimation++;
426 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
428 mCurrentActorAnimation = 0;
433 void ToggleActorEffects()
435 if(!mActorEffectsEnabled)
437 mActorEffectsEnabled = true;
438 mActorEffectsButton.SetUnselectedImage( EFFECTS_ON_ICON );
439 mActorEffectsButton.SetSelectedImage( EFFECTS_ON_ICON_SELECTED );
443 mActorEffectsEnabled = false;
444 mActorEffectsButton.SetUnselectedImage( EFFECTS_OFF_ICON );
445 mActorEffectsButton.SetSelectedImage( EFFECTS_OFF_ICON_SELECTED );
449 //////////////////////////////////////////////////////////////
455 bool OnLayoutButtonClicked( Toolkit::Button button )
461 bool OnEffectButtonClicked( Toolkit::Button button )
463 ToggleActorEffects();
468 * Main key event handler
470 void OnKeyEvent(const KeyEvent& event)
472 if(event.state == KeyEvent::Down)
474 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
481 //////////////////////////////////////////////////////////////
491 if(MOTION_BLUR_NUM_ACTOR_IMAGES == mCurrentImage)
495 SetImageFittedInBox( mMotionBlurImageView, mMotionBlurEffect, MOTION_BLUR_ACTOR_IMAGES[mCurrentImage], mMotionBlurActorSize.x, mMotionBlurActorSize.y );
497 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
498 mMotionBlurImageView2.SetImage(blurImage);
499 mMotionBlurImageView3.SetImage(blurImage);
500 mMotionBlurImageView4.SetImage(blurImage);
501 mMotionBlurImageView5.SetImage(blurImage);
507 Application& mApplication; ///< Application instance
508 Toolkit::Control mView;
509 Toolkit::ToolBar mToolBar;
511 Layer mContentLayer; ///< Content layer (contains actor for this blur demo)
513 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
516 Property::Map mMotionBlurEffect;
517 ImageView mMotionBlurImageView;
518 Size mMotionBlurActorSize;
520 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
521 ImageView mMotionBlurImageView2;
522 ImageView mMotionBlurImageView3;
523 ImageView mMotionBlurImageView4;
524 ImageView mMotionBlurImageView5;
525 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
527 // animate actor to position where user taps screen
528 Animation mActorTapMovementAnimation;
530 // show different animations to demonstrate blur effect working on an object only movement basis
531 bool mActorEffectsEnabled;
532 Animation mActorAnimation;
533 int mCurrentActorAnimation;
535 // offer a selection of images that user can cycle between
538 TapGestureDetector mTapGestureDetector;
540 DeviceOrientation mOrientation; ///< Current Device orientation
541 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
543 Popup mInstructionsPopup; ///< Info Popup
546 void RunTest(Application& app)
548 MotionBlurExampleApp test(app);
553 // Entry point for Linux & Tizen applications
555 int DALI_EXPORT_API main(int argc, char **argv)
557 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);