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 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),
148 // Connect to the Application's Init signal
149 app.InitSignal().Connect(this, &MotionBlurExampleApp::OnInit);
152 ~MotionBlurExampleApp()
154 // Nothing to do here; everything gets deleted automatically
158 * This method gets called once the main loop of application is up and running
160 void OnInit(Application& app)
162 // The Init signal is received once (only) during the Application lifetime
164 Stage::GetCurrent().KeyEventSignal().Connect(this, &MotionBlurExampleApp::OnKeyEvent);
167 // Creates a default view with a default tool bar.
168 // The view is added to the stage.
169 mContentLayer = DemoHelper::CreateView( mApplication,
172 BACKGROUND_IMAGE_PATH,
176 //Add an effects icon on the right of the title
177 mActorEffectsButton = Toolkit::PushButton::New();
178 mActorEffectsButton.SetUnselectedImage( EFFECTS_OFF_ICON );
179 mActorEffectsButton.SetSelectedImage( EFFECTS_OFF_ICON_SELECTED );
180 mActorEffectsButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnEffectButtonClicked );
181 mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
183 // Creates a mode button.
184 // Create a effect toggle button. (right of toolbar)
185 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
186 layoutButton.SetUnselectedImage( LAYOUT_IMAGE );
187 layoutButton.SetSelectedImage( LAYOUT_IMAGE_SELECTED );
188 layoutButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnLayoutButtonClicked);
189 layoutButton.SetLeaveRequired( true );
190 mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
193 mTapGestureDetector = TapGestureDetector::New();
194 mTapGestureDetector.Attach( mContentLayer );
195 mTapGestureDetector.DetectedSignal().Connect( this, &MotionBlurExampleApp::OnTap );
197 Dali::Window winHandle = app.GetWindow();
198 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
199 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
200 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
201 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
203 // set initial orientation
204 unsigned int degrees = 0;
205 Rotate( static_cast< DeviceOrientation >( degrees ) );
208 ///////////////////////////////////////////////////////
210 // Motion blurred actor
213 // Scale down actor to fit on very low resolution screens with space to interact:
214 Size stageSize = Stage::GetCurrent().GetSize();
215 mMotionBlurActorSize = Size( std::min( stageSize.x * 0.3f, MOTION_BLUR_ACTOR_WIDTH ), std::min( stageSize.y * 0.3f, MOTION_BLUR_ACTOR_HEIGHT ) );
216 mMotionBlurActorSize = Size( std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ), std::min( mMotionBlurActorSize.x, mMotionBlurActorSize.y ) );
218 mMotionBlurEffect = CreateMotionBlurEffect();
219 mMotionBlurImageView = ImageView::New();
220 SetImageFittedInBox( mMotionBlurImageView, mMotionBlurEffect, MOTION_BLUR_ACTOR_IMAGE1, mMotionBlurActorSize.x, mMotionBlurActorSize.y );
221 mMotionBlurImageView.SetParentOrigin( ParentOrigin::CENTER );
222 mMotionBlurImageView.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
224 mContentLayer.Add( mMotionBlurImageView );
226 // Create shader used for doing motion blur
227 mMotionBlurEffect = CreateMotionBlurEffect();
229 // set actor shader to the blur one
230 Toolkit::SetMotionBlurProperties( mMotionBlurImageView, MOTION_BLUR_NUM_SAMPLES );
233 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
235 ///////////////////////////////////////////////////////
237 // Motion blurred actor 2
240 mMotionBlurImageView2 = ImageView::New(image);
241 mMotionBlurImageView2.SetParentOrigin( ParentOrigin::CENTER );
242 mMotionBlurImageView2.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
243 mMotionBlurImageView2.SetPosition(mMotionBlurActorSize.x * 1.1f, 0.0f);
244 mMotionBlurImageView.Add( mMotionBlurImageView2 );
246 // set actor shader to the blur one
247 Toolkit::SetMotionBlurProperties( mMotionBlurImageView2, MOTION_BLUR_NUM_SAMPLES );
248 mMotionBlurImageView2.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
251 ///////////////////////////////////////////////////////
253 // Motion blurred actor 3
256 mMotionBlurImageView3 = ImageView::New(image);
257 mMotionBlurImageView3.SetParentOrigin( ParentOrigin::CENTER );
258 mMotionBlurImageView3.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
259 mMotionBlurImageView3.SetPosition(-mMotionBlurActorSize.x * 1.1f, 0.0f);
260 mMotionBlurImageView.Add( mMotionBlurImageView3 );
262 // set actor shader to the blur one
263 Toolkit::SetMotionBlurProperties( mMotionBlurImageView3, MOTION_BLUR_NUM_SAMPLES );
264 mMotionBlurImageView3.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
267 ///////////////////////////////////////////////////////
269 // Motion blurred actor 4
272 mMotionBlurImageView4 = ImageView::New(image);
273 mMotionBlurImageView4.SetParentOrigin( ParentOrigin::CENTER );
274 mMotionBlurImageView4.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
275 mMotionBlurImageView4.SetPosition(0.0f, mMotionBlurActorSize.y * 1.1f);
276 mMotionBlurImageView.Add( mMotionBlurImageView4 );
278 // set actor shader to the blur one
279 Toolkit::SetMotionBlurProperties( mMotionBlurImageView4, MOTION_BLUR_NUM_SAMPLES );
280 mMotionBlurImageView4.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
282 ///////////////////////////////////////////////////////
284 // Motion blurred actor 5
287 mMotionBlurImageView5 = ImageView::New(image);
288 mMotionBlurImageView5.SetParentOrigin( ParentOrigin::CENTER );
289 mMotionBlurImageView5.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
290 mMotionBlurImageView5.SetPosition(0.0f, -mMotionBlurActorSize.y * 1.1f);
291 mMotionBlurImageView.Add( mMotionBlurImageView5 );
293 // set actor shader to the blur one
294 Toolkit::SetMotionBlurProperties( mMotionBlurImageView5, MOTION_BLUR_NUM_SAMPLES );
295 mMotionBlurImageView5.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionBlurEffect );
296 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
299 void Rotate( DeviceOrientation orientation )
301 // Resize the root actor
302 Vector2 stageSize = Stage::GetCurrent().GetSize();
303 Vector2 targetSize = stageSize;
304 if( orientation == LANDSCAPE ||
305 orientation == LANDSCAPE_INVERSE )
307 targetSize = Vector2( stageSize.y, stageSize.x );
310 if( mOrientation != orientation )
312 mOrientation = orientation;
314 // check if actor is on stage
315 if( mView.GetParent() )
317 // has parent so we expect it to be on stage, start animation
318 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
319 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -orientation ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
320 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
321 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
322 mRotateAnimation.Play();
326 // set the rotation to match the orientation
327 mView.SetOrientation( Degree( -orientation ), Vector3::ZAXIS );
328 mView.SetSize( targetSize );
333 // for first time just set size
334 mView.SetSize( targetSize );
339 //////////////////////////////////////////////////////////////
345 // move to point on screen that was tapped
346 void OnTap( Actor actor, const TapGesture& tapGesture )
349 float originOffsetX, originOffsetY;
351 // rotate offset (from top left origin to centre) into actor space
352 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
353 actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
355 // get dest point in local actor space
356 destPos.x = tapGesture.localPoint.x - originOffsetX;
357 destPos.y = tapGesture.localPoint.y - originOffsetY;
360 float animDuration = 0.5f;
361 mActorTapMovementAnimation = Animation::New( animDuration );
362 if ( mMotionBlurImageView )
364 mActorTapMovementAnimation.AnimateTo( Property(mMotionBlurImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
366 mActorTapMovementAnimation.SetEndAction( Animation::Bake );
367 mActorTapMovementAnimation.Play();
370 // perform some spinning etc
371 if(mActorEffectsEnabled)
373 switch(mCurrentActorAnimation)
378 float animDuration = 1.0f;
379 mActorAnimation = Animation::New(animDuration);
380 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
381 mActorAnimation.SetEndAction( Animation::Bake );
382 mActorAnimation.Play();
389 float animDuration = 1.0f;
390 mActorAnimation = Animation::New(animDuration);
391 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
392 mActorAnimation.SetEndAction( Animation::Bake );
393 mActorAnimation.Play();
397 // spin around y and z
400 float animDuration = 1.0f;
401 mActorAnimation = Animation::New(animDuration);
402 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
403 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
404 mActorAnimation.SetEndAction( Animation::Bake );
405 mActorAnimation.Play();
412 float animDuration = 1.0f;
413 mActorAnimation = Animation::New(animDuration);
414 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
415 mActorAnimation.SetEndAction( Animation::Bake );
416 mActorAnimation.Play();
424 mCurrentActorAnimation++;
425 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
427 mCurrentActorAnimation = 0;
432 void ToggleActorEffects()
434 if(!mActorEffectsEnabled)
436 mActorEffectsEnabled = true;
437 mActorEffectsButton.SetUnselectedImage( EFFECTS_ON_ICON );
438 mActorEffectsButton.SetSelectedImage( EFFECTS_ON_ICON_SELECTED );
442 mActorEffectsEnabled = false;
443 mActorEffectsButton.SetUnselectedImage( EFFECTS_OFF_ICON );
444 mActorEffectsButton.SetSelectedImage( EFFECTS_OFF_ICON_SELECTED );
448 //////////////////////////////////////////////////////////////
454 bool OnLayoutButtonClicked( Toolkit::Button button )
460 bool OnEffectButtonClicked( Toolkit::Button button )
462 ToggleActorEffects();
467 * Main key event handler
469 void OnKeyEvent(const KeyEvent& event)
471 if(event.state == KeyEvent::Down)
473 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
480 //////////////////////////////////////////////////////////////
490 if(MOTION_BLUR_NUM_ACTOR_IMAGES == mCurrentImage)
494 SetImageFittedInBox( mMotionBlurImageView, mMotionBlurEffect, MOTION_BLUR_ACTOR_IMAGES[mCurrentImage], mMotionBlurActorSize.x, mMotionBlurActorSize.y );
496 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
497 mMotionBlurImageView2.SetImage(blurImage);
498 mMotionBlurImageView3.SetImage(blurImage);
499 mMotionBlurImageView4.SetImage(blurImage);
500 mMotionBlurImageView5.SetImage(blurImage);
506 Application& mApplication; ///< Application instance
507 Toolkit::Control mView;
508 Toolkit::ToolBar mToolBar;
510 Layer mContentLayer; ///< Content layer (contains actor for this blur demo)
512 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
515 Property::Map mMotionBlurEffect;
516 ImageView mMotionBlurImageView;
517 Size mMotionBlurActorSize;
519 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
520 ImageView mMotionBlurImageView2;
521 ImageView mMotionBlurImageView3;
522 ImageView mMotionBlurImageView4;
523 ImageView mMotionBlurImageView5;
524 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
526 // animate actor to position where user taps screen
527 Animation mActorTapMovementAnimation;
529 // show different animations to demonstrate blur effect working on an object only movement basis
530 bool mActorEffectsEnabled;
531 Animation mActorAnimation;
532 int mCurrentActorAnimation;
534 // offer a selection of images that user can cycle between
537 TapGestureDetector mTapGestureDetector;
539 DeviceOrientation mOrientation; ///< Current Device orientation
540 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
542 Popup mInstructionsPopup; ///< Info Popup
545 void RunTest(Application& app)
547 MotionBlurExampleApp test(app);
552 // Entry point for Linux & Tizen applications
554 int DALI_EXPORT_API main(int argc, char **argv)
556 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);