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 = winHandle.GetOrientation().GetDegrees();
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 = MotionBlurEffect::Apply(mMotionBlurImageActor);
220 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
222 ///////////////////////////////////////////////////////
224 // Motion blurred actor 2
227 mMotionBlurImageActor2 = ImageActor::New(image);
228 mMotionBlurImageActor2.SetParentOrigin( ParentOrigin::CENTER );
229 mMotionBlurImageActor2.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
230 mMotionBlurImageActor2.SetPosition(mMotionBlurActorSize.x * 1.1f, 0.0f);
231 mMotionBlurImageActor.Add( mMotionBlurImageActor2 );
233 // Create shader used for doing motion blur
234 mMotionBlurEffect2 = MotionBlurEffect::New(MOTION_BLUR_NUM_SAMPLES);
236 // set actor shader to the blur one
237 mMotionBlurImageActor2.SetShaderEffect( mMotionBlurEffect2 );
240 ///////////////////////////////////////////////////////
242 // Motion blurred actor 3
245 mMotionBlurImageActor3 = ImageActor::New(image);
246 mMotionBlurImageActor3.SetParentOrigin( ParentOrigin::CENTER );
247 mMotionBlurImageActor3.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
248 mMotionBlurImageActor3.SetPosition(-mMotionBlurActorSize.x * 1.1f, 0.0f);
249 mMotionBlurImageActor.Add( mMotionBlurImageActor3 );
251 // Create shader used for doing motion blur
252 mMotionBlurEffect3 = MotionBlurEffect::New(MOTION_BLUR_NUM_SAMPLES);
254 // set actor shader to the blur one
255 mMotionBlurImageActor3.SetShaderEffect( mMotionBlurEffect3 );
258 ///////////////////////////////////////////////////////
260 // Motion blurred actor 4
263 mMotionBlurImageActor4 = ImageActor::New(image);
264 mMotionBlurImageActor4.SetParentOrigin( ParentOrigin::CENTER );
265 mMotionBlurImageActor4.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
266 mMotionBlurImageActor4.SetPosition(0.0f, mMotionBlurActorSize.y * 1.1f);
267 mMotionBlurImageActor.Add( mMotionBlurImageActor4 );
269 // Create shader used for doing motion blur
270 mMotionBlurEffect4 = MotionBlurEffect::New(MOTION_BLUR_NUM_SAMPLES);
272 // set actor shader to the blur one
273 mMotionBlurImageActor4.SetShaderEffect( mMotionBlurEffect4 );
276 ///////////////////////////////////////////////////////
278 // Motion blurred actor 5
281 mMotionBlurImageActor5 = ImageActor::New(image);
282 mMotionBlurImageActor5.SetParentOrigin( ParentOrigin::CENTER );
283 mMotionBlurImageActor5.SetSize(mMotionBlurActorSize.x, mMotionBlurActorSize.y);
284 mMotionBlurImageActor5.SetPosition(0.0f, -mMotionBlurActorSize.y * 1.1f);
285 mMotionBlurImageActor.Add( mMotionBlurImageActor5 );
287 // Create shader used for doing motion blur
288 mMotionBlurEffect5 = MotionBlurEffect::New(MOTION_BLUR_NUM_SAMPLES);
290 // set actor shader to the blur one
291 mMotionBlurImageActor5.SetShaderEffect( mMotionBlurEffect5 );
292 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
295 //////////////////////////////////////////////////////////////
297 // Device Orientation Support
301 void OnOrientationChanged( Orientation orientation )
303 unsigned int degrees = orientation.GetDegrees();
304 Rotate( static_cast< DeviceOrientation >( degrees ) );
307 void Rotate( DeviceOrientation orientation )
309 // Resize the root actor
310 Vector2 stageSize = Stage::GetCurrent().GetSize();
311 Vector2 targetSize = stageSize;
312 if( orientation == LANDSCAPE ||
313 orientation == LANDSCAPE_INVERSE )
315 targetSize = Vector2( stageSize.y, stageSize.x );
318 if( mOrientation != orientation )
320 mOrientation = orientation;
322 // check if actor is on stage
323 if( mView.GetParent() )
325 // has parent so we expect it to be on stage, start animation
326 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
327 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -orientation ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
328 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
329 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
330 mRotateAnimation.Play();
334 // set the rotation to match the orientation
335 mView.SetOrientation( Degree( -orientation ), Vector3::ZAXIS );
336 mView.SetSize( targetSize );
341 // for first time just set size
342 mView.SetSize( targetSize );
347 //////////////////////////////////////////////////////////////
353 // move to point on screen that was tapped
354 void OnTap( Actor actor, const TapGesture& tapGesture )
357 float originOffsetX, originOffsetY;
359 // rotate offset (from top left origin to centre) into actor space
360 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
361 actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
363 // get dest point in local actor space
364 destPos.x = tapGesture.localPoint.x - originOffsetX;
365 destPos.y = tapGesture.localPoint.y - originOffsetY;
368 float animDuration = 0.5f;
369 mActorTapMovementAnimation = Animation::New( animDuration );
370 if ( mMotionBlurImageActor )
372 mActorTapMovementAnimation.AnimateTo( Property(mMotionBlurImageActor, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
374 mActorTapMovementAnimation.SetEndAction( Animation::Bake );
375 mActorTapMovementAnimation.Play();
378 // perform some spinning etc
379 if(mActorEffectsEnabled)
381 switch(mCurrentActorAnimation)
386 float animDuration = 1.0f;
387 mActorAnimation = Animation::New(animDuration);
388 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
389 mActorAnimation.SetEndAction( Animation::Bake );
390 mActorAnimation.Play();
397 float animDuration = 1.0f;
398 mActorAnimation = Animation::New(animDuration);
399 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
400 mActorAnimation.SetEndAction( Animation::Bake );
401 mActorAnimation.Play();
405 // spin around y and z
408 float animDuration = 1.0f;
409 mActorAnimation = Animation::New(animDuration);
410 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
411 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
412 mActorAnimation.SetEndAction( Animation::Bake );
413 mActorAnimation.Play();
420 float animDuration = 1.0f;
421 mActorAnimation = Animation::New(animDuration);
422 mActorAnimation.AnimateBy( Property( mMotionBlurImageActor, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
423 mActorAnimation.SetEndAction( Animation::Bake );
424 mActorAnimation.Play();
432 mCurrentActorAnimation++;
433 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
435 mCurrentActorAnimation = 0;
440 void ToggleActorEffects()
442 if(!mActorEffectsEnabled)
444 mActorEffectsEnabled = true;
445 mActorEffectsButton.SetBackgroundImage( mIconEffectsOn );
449 mActorEffectsEnabled = false;
450 mActorEffectsButton.SetBackgroundImage( mIconEffectsOff );
454 //////////////////////////////////////////////////////////////
460 bool OnLayoutButtonClicked( Toolkit::Button button )
466 bool OnEffectButtonClicked( Toolkit::Button button )
468 ToggleActorEffects();
473 * Main key event handler
475 void OnKeyEvent(const KeyEvent& event)
477 if(event.state == KeyEvent::Down)
479 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
486 //////////////////////////////////////////////////////////////
496 if(MOTION_BLUR_NUM_ACTOR_IMAGES == mCurrentImage)
501 Image blurImage = LoadImageFittedInBox( MOTION_BLUR_ACTOR_IMAGES[mCurrentImage], mMotionBlurActorSize.x, mMotionBlurActorSize.y );
502 mMotionBlurImageActor.SetImage(blurImage);
507 Application& mApplication; ///< Application instance
508 Toolkit::Control mView;
509 Toolkit::ToolBar mToolBar;
510 Image mIconEffectsOff;
511 Image mIconEffectsOn;
513 Layer mContentLayer; ///< Content layer (contains actor for this blur demo)
515 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
518 MotionBlurEffect mMotionBlurEffect;
519 ImageActor mMotionBlurImageActor;
520 Size mMotionBlurActorSize;
522 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
523 MotionBlurEffect mMotionBlurEffect2;
524 MotionBlurEffect mMotionBlurEffect3;
525 MotionBlurEffect mMotionBlurEffect4;
526 MotionBlurEffect mMotionBlurEffect5;
528 ImageActor mMotionBlurImageActor2;
529 ImageActor mMotionBlurImageActor3;
530 ImageActor mMotionBlurImageActor4;
531 ImageActor mMotionBlurImageActor5;
532 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
534 // animate actor to position where user taps screen
535 Animation mActorTapMovementAnimation;
537 // show different animations to demonstrate blur effect working on an object only movement basis
538 bool mActorEffectsEnabled;
539 Animation mActorAnimation;
540 int mCurrentActorAnimation;
542 // offer a selection of images that user can cycle between
545 TapGestureDetector mTapGestureDetector;
547 DeviceOrientation mOrientation; ///< Current Device orientation
548 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
550 Popup mInstructionsPopup; ///< Info Popup
553 void RunTest(Application& app)
555 MotionBlurExampleApp test(app);
560 // Entry point for Linux & Tizen applications
562 int main(int argc, char **argv)
564 Application app = Application::New(&argc, &argv);