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>
26 using namespace Dali::Toolkit;
31 namespace // unnamed namespace
34 ////////////////////////////////////////////////////
36 // Demo setup parameters
39 //#define MULTIPLE_MOTION_BLURRED_ACTORS
40 #ifndef MULTIPLE_MOTION_BLURRED_ACTORS
42 const float MOTION_BLUR_ACTOR_WIDTH = 256; // actor size on screen
43 const float MOTION_BLUR_ACTOR_HEIGHT = 256; // ""
45 #else //#ifndef MULTIPLE_MOTION_BLURRED_ACTORS
47 const float MOTION_BLUR_ACTOR_WIDTH = 150; // actor size on screen
48 const float MOTION_BLUR_ACTOR_HEIGHT = 112; // ""
50 #endif //#ifndef MULTIPLE_MOTION_BLURRED_ACTORS
53 const unsigned int MOTION_BLUR_NUM_SAMPLES = 8;
55 const int MOTION_BLUR_NUM_ACTOR_IMAGES = 5;
56 const char* MOTION_BLUR_ACTOR_IMAGE1( DALI_IMAGE_DIR "image-with-border-1.jpg" );
57 const char* MOTION_BLUR_ACTOR_IMAGE2( DALI_IMAGE_DIR "image-with-border-2.jpg" );
58 const char* MOTION_BLUR_ACTOR_IMAGE3( DALI_IMAGE_DIR "image-with-border-3.jpg" );
59 const char* MOTION_BLUR_ACTOR_IMAGE4( DALI_IMAGE_DIR "image-with-border-4.jpg" );
60 const char* MOTION_BLUR_ACTOR_IMAGE5( DALI_IMAGE_DIR "image-with-border-1.jpg" );
62 const char* MOTION_BLUR_ACTOR_IMAGES[] = {
63 MOTION_BLUR_ACTOR_IMAGE1,
64 MOTION_BLUR_ACTOR_IMAGE2,
65 MOTION_BLUR_ACTOR_IMAGE3,
66 MOTION_BLUR_ACTOR_IMAGE4,
67 MOTION_BLUR_ACTOR_IMAGE5,
70 const int NUM_ACTOR_ANIMATIONS = 4;
71 const int NUM_CAMERA_ANIMATIONS = 2;
74 const char* BACKGROUND_IMAGE_PATH = DALI_IMAGE_DIR "background-default.png";
76 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
77 const char* LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-change.png" );
78 const char* APPLICATION_TITLE( "Motion Blur" );
79 const char* EFFECTS_OFF_ICON( DALI_IMAGE_DIR "icon-effects-off.png" );
80 const char* EFFECTS_ON_ICON( DALI_IMAGE_DIR "icon-effects-on.png" );
82 const float UI_MARGIN = 4.0f; ///< Screen Margin for placement of UI buttons
84 const Vector3 BUTTON_SIZE_CONSTRAINT( 0.24f, 0.09f, 1.0f );
85 const Vector3 BUTTON_TITLE_LABEL_TAP_HERE_SIZE_CONSTRAINT( 0.55f, 0.06f, 1.0f );
86 const Vector3 BUTTON_TITLE_LABEL_INSTRUCTIONS_POPUP_SIZE_CONSTRAINT( 1.0f, 1.0f, 1.0f );
88 // move this button down a bit so it is visible on target and not covered up by toolbar
89 const float BUTTON_TITLE_LABEL_Y_OFFSET = 0.05f;
91 const float ORIENTATION_DURATION = 0.5f; ///< Time to rotate to new orientation.
92 } // unnamed namespace
98 class MotionBlurExampleApp : public ConnectionTracker
103 * DeviceOrientation describes the four different
104 * orientations the device can be in based on accelerometer reports.
106 enum DeviceOrientation
110 PORTRAIT_INVERSE = 180,
111 LANDSCAPE_INVERSE = 270
116 * @param application class, stored as reference
118 MotionBlurExampleApp(Application &app)
120 mActorEffectsEnabled(false),
121 mCurrentActorAnimation(0),
124 // Connect to the Application's Init signal
125 app.InitSignal().Connect(this, &MotionBlurExampleApp::OnInit);
128 ~MotionBlurExampleApp()
130 // Nothing to do here; everything gets deleted automatically
134 * This method gets called once the main loop of application is up and running
136 void OnInit(Application& app)
138 // The Init signal is received once (only) during the Application lifetime
140 Stage::GetCurrent().KeyEventSignal().Connect(this, &MotionBlurExampleApp::OnKeyEvent);
143 // Creates a default view with a default tool bar.
144 // The view is added to the stage.
145 mContentLayer = DemoHelper::CreateView( mApplication,
148 BACKGROUND_IMAGE_PATH,
152 //Add an effects icon on the right of the title
153 mIconEffectsOff = Image::New( EFFECTS_OFF_ICON );
154 mIconEffectsOn = Image::New( EFFECTS_ON_ICON );
155 mActorEffectsButton = Toolkit::PushButton::New();
156 mActorEffectsButton.SetBackgroundImage( mIconEffectsOff );
157 mActorEffectsButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnEffectButtonClicked );
158 mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
160 // Creates a mode button.
161 // Create a effect toggle button. (right of toolbar)
162 Image imageLayout = Image::New( LAYOUT_IMAGE );
163 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
164 layoutButton.SetBackgroundImage(imageLayout);
165 layoutButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnLayoutButtonClicked);
166 layoutButton.SetLeaveRequired( true );
167 mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
170 mTapGestureDetector = TapGestureDetector::New();
171 mTapGestureDetector.Attach( mContentLayer );
172 mTapGestureDetector.DetectedSignal().Connect( this, &MotionBlurExampleApp::OnTap );
174 Dali::Window winHandle = app.GetWindow();
175 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
176 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
177 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
178 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
180 // set initial orientation
181 app.GetOrientation().ChangedSignal().Connect( this, &MotionBlurExampleApp::OnOrientationChanged );
182 unsigned int degrees = app.GetOrientation().GetDegrees();
183 Rotate( static_cast< DeviceOrientation >( degrees ) );
186 ///////////////////////////////////////////////////////
188 // Motion blurred actor
191 Image image = Image::New( MOTION_BLUR_ACTOR_IMAGE1 );
192 mMotionBlurImageActor = ImageActor::New(image);
193 mMotionBlurImageActor.SetParentOrigin( ParentOrigin::CENTER );
194 mMotionBlurImageActor.SetSize(MOTION_BLUR_ACTOR_WIDTH, MOTION_BLUR_ACTOR_HEIGHT);
196 mContentLayer.Add( mMotionBlurImageActor );
198 // Create shader used for doing motion blur
199 mMotionBlurEffect = MotionBlurEffect::Apply(mMotionBlurImageActor);
202 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
204 ///////////////////////////////////////////////////////
206 // Motion blurred actor 2
209 mMotionBlurImageActor2 = ImageActor::New(image);
210 mMotionBlurImageActor2.SetParentOrigin( ParentOrigin::CENTER );
211 mMotionBlurImageActor2.SetSize(MOTION_BLUR_ACTOR_WIDTH, MOTION_BLUR_ACTOR_HEIGHT);
212 mMotionBlurImageActor2.SetPosition(MOTION_BLUR_ACTOR_WIDTH * 1.1f, 0.0f);
213 mMotionBlurImageActor.Add( mMotionBlurImageActor2 );
215 // Create shader used for doing motion blur
216 mMotionBlurEffect2 = MotionBlurEffect::New(MOTION_BLUR_NUM_SAMPLES);
218 // set actor shader to the blur one
219 mMotionBlurImageActor2.SetShaderEffect( mMotionBlurEffect2 );
222 ///////////////////////////////////////////////////////
224 // Motion blurred actor 3
227 mMotionBlurImageActor3 = ImageActor::New(image);
228 mMotionBlurImageActor3.SetParentOrigin( ParentOrigin::CENTER );
229 mMotionBlurImageActor3.SetSize(MOTION_BLUR_ACTOR_WIDTH, MOTION_BLUR_ACTOR_HEIGHT);
230 mMotionBlurImageActor3.SetPosition(-MOTION_BLUR_ACTOR_WIDTH * 1.1f, 0.0f);
231 mMotionBlurImageActor.Add( mMotionBlurImageActor3 );
233 // Create shader used for doing motion blur
234 mMotionBlurEffect3 = MotionBlurEffect::New(MOTION_BLUR_NUM_SAMPLES);
236 // set actor shader to the blur one
237 mMotionBlurImageActor3.SetShaderEffect( mMotionBlurEffect3 );
240 ///////////////////////////////////////////////////////
242 // Motion blurred actor 4
245 mMotionBlurImageActor4 = ImageActor::New(image);
246 mMotionBlurImageActor4.SetParentOrigin( ParentOrigin::CENTER );
247 mMotionBlurImageActor4.SetSize(MOTION_BLUR_ACTOR_WIDTH, MOTION_BLUR_ACTOR_HEIGHT);
248 mMotionBlurImageActor4.SetPosition(0.0f, MOTION_BLUR_ACTOR_HEIGHT * 1.1f);
249 mMotionBlurImageActor.Add( mMotionBlurImageActor4 );
251 // Create shader used for doing motion blur
252 mMotionBlurEffect4 = MotionBlurEffect::New(MOTION_BLUR_NUM_SAMPLES);
254 // set actor shader to the blur one
255 mMotionBlurImageActor4.SetShaderEffect( mMotionBlurEffect4 );
258 ///////////////////////////////////////////////////////
260 // Motion blurred actor 5
263 mMotionBlurImageActor5 = ImageActor::New(image);
264 mMotionBlurImageActor5.SetParentOrigin( ParentOrigin::CENTER );
265 mMotionBlurImageActor5.SetSize(MOTION_BLUR_ACTOR_WIDTH, MOTION_BLUR_ACTOR_HEIGHT);
266 mMotionBlurImageActor5.SetPosition(0.0f, -MOTION_BLUR_ACTOR_HEIGHT * 1.1f);
267 mMotionBlurImageActor.Add( mMotionBlurImageActor5 );
269 // Create shader used for doing motion blur
270 mMotionBlurEffect5 = MotionBlurEffect::New(MOTION_BLUR_NUM_SAMPLES);
272 // set actor shader to the blur one
273 mMotionBlurImageActor5.SetShaderEffect( mMotionBlurEffect5 );
274 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
277 //////////////////////////////////////////////////////////////
279 // Device Orientation Support
283 void OnOrientationChanged( Orientation orientation )
285 unsigned int degrees = orientation.GetDegrees();
286 Rotate( static_cast< DeviceOrientation >( degrees ) );
289 void Rotate( DeviceOrientation orientation )
291 // Resize the root actor
292 Vector2 stageSize = Stage::GetCurrent().GetSize();
293 Vector2 targetSize = stageSize;
294 if( orientation == LANDSCAPE ||
295 orientation == LANDSCAPE_INVERSE )
297 targetSize = Vector2( stageSize.y, stageSize.x );
300 if( mOrientation != orientation )
302 mOrientation = orientation;
304 // check if actor is on stage
305 if( mView.GetParent() )
307 // has parent so we expect it to be on stage, start animation
308 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
309 mRotateAnimation.SetDestroyAction( Animation::Bake );
310 mRotateAnimation.RotateTo( mView, Degree( -orientation ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
311 mRotateAnimation.Resize( mView, targetSize.width, targetSize.height );
312 mRotateAnimation.Play();
316 // set the rotation to match the orientation
317 mView.SetRotation( Degree( -orientation ), Vector3::ZAXIS );
318 mView.SetSize( targetSize );
323 // for first time just set size
324 mView.SetSize( targetSize );
329 //////////////////////////////////////////////////////////////
335 // move to point on screen that was tapped
336 void OnTap( Actor actor, TapGesture tapGesture )
339 float originOffsetX, originOffsetY;
341 // rotate offset (from top left origin to centre) into actor space
342 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
343 actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
345 // get dest point in local actor space
346 destPos.x = tapGesture.localPoint.x - originOffsetX;
347 destPos.y = tapGesture.localPoint.y - originOffsetY;
350 float animDuration = 0.5f;
351 mActorTapMovementAnimation = Animation::New( animDuration );
352 if ( mMotionBlurImageActor )
354 mActorTapMovementAnimation.AnimateTo( Property(mMotionBlurImageActor, Actor::POSITION), destPos, AlphaFunctions::EaseInOutSine, TimePeriod(animDuration) );
356 mActorTapMovementAnimation.SetEndAction( Animation::Bake );
357 mActorTapMovementAnimation.Play();
360 // perform some spinning etc
361 if(mActorEffectsEnabled)
363 switch(mCurrentActorAnimation)
368 float animDuration = 1.0f;
369 mActorAnimation = Animation::New(animDuration);
370 mActorAnimation.RotateBy(mMotionBlurImageActor, Degree(720), Vector3::YAXIS, AlphaFunctions::EaseInOut);
371 mActorAnimation.SetEndAction( Animation::Bake );
372 mActorAnimation.Play();
379 float animDuration = 1.0f;
380 mActorAnimation = Animation::New(animDuration);
381 mActorAnimation.RotateBy(mMotionBlurImageActor, Degree(720), Vector3::ZAXIS, AlphaFunctions::EaseInOut);
382 mActorAnimation.SetEndAction( Animation::Bake );
383 mActorAnimation.Play();
387 // spin around y and z
390 float animDuration = 1.0f;
391 mActorAnimation = Animation::New(animDuration);
392 mActorAnimation.RotateBy(mMotionBlurImageActor, Degree(360), Vector3::YAXIS, AlphaFunctions::EaseInOut);
393 mActorAnimation.RotateBy(mMotionBlurImageActor, Degree(360), Vector3::ZAXIS, AlphaFunctions::EaseInOut);
394 mActorAnimation.SetEndAction( Animation::Bake );
395 mActorAnimation.Play();
402 float animDuration = 1.0f;
403 mActorAnimation = Animation::New(animDuration);
404 mActorAnimation.ScaleBy(mMotionBlurImageActor, Vector3(2.0f, 2.0f, 2.0f), AlphaFunctions::Bounce, 0.0f, 1.0f);
405 mActorAnimation.SetEndAction( Animation::Bake );
406 mActorAnimation.Play();
414 mCurrentActorAnimation++;
415 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
417 mCurrentActorAnimation = 0;
422 void ToggleActorEffects()
424 if(!mActorEffectsEnabled)
426 mActorEffectsEnabled = true;
427 mActorEffectsButton.SetBackgroundImage( mIconEffectsOn );
431 mActorEffectsEnabled = false;
432 mActorEffectsButton.SetBackgroundImage( mIconEffectsOff );
436 //////////////////////////////////////////////////////////////
442 bool OnLayoutButtonClicked( Toolkit::Button button )
448 bool OnEffectButtonClicked( Toolkit::Button button )
450 ToggleActorEffects();
455 * Main key event handler
457 void OnKeyEvent(const KeyEvent& event)
459 if(event.state == KeyEvent::Down)
461 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
468 //////////////////////////////////////////////////////////////
478 if(MOTION_BLUR_NUM_ACTOR_IMAGES == mCurrentImage)
483 Image blurImage = Image::New( MOTION_BLUR_ACTOR_IMAGES[mCurrentImage] );
484 mMotionBlurImageActor.SetImage(blurImage);
489 Application& mApplication; ///< Application instance
491 Toolkit::ToolBar mToolBar;
492 TextView mTitleActor; ///< The Toolbar's Title.
493 Image mIconEffectsOff;
494 Image mIconEffectsOn;
496 Layer mContentLayer; ///< Content layer (contains actor for this blur demo)
498 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
501 MotionBlurEffect mMotionBlurEffect;
502 ImageActor mMotionBlurImageActor;
504 #ifdef MULTIPLE_MOTION_BLURRED_ACTORS
505 MotionBlurEffect mMotionBlurEffect2;
506 MotionBlurEffect mMotionBlurEffect3;
507 MotionBlurEffect mMotionBlurEffect4;
508 MotionBlurEffect mMotionBlurEffect5;
510 ImageActor mMotionBlurImageActor2;
511 ImageActor mMotionBlurImageActor3;
512 ImageActor mMotionBlurImageActor4;
513 ImageActor mMotionBlurImageActor5;
514 #endif //#ifdef MULTIPLE_MOTION_BLURRED_ACTORS
516 // animate actor to position where user taps screen
517 Animation mActorTapMovementAnimation;
519 // show different animations to demonstrate blur effect working on an object only movement basis
520 bool mActorEffectsEnabled;
521 Animation mActorAnimation;
522 int mCurrentActorAnimation;
524 // offer a selection of images that user can cycle between
527 TapGestureDetector mTapGestureDetector;
529 DeviceOrientation mOrientation; ///< Current Device orientation
530 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
532 Popup mInstructionsPopup; ///< Info Popup
535 void RunTest(Application& app)
537 MotionBlurExampleApp test(app);
542 // Entry point for Linux & SLP applications
544 int main(int argc, char **argv)
546 Application app = Application::New(&argc, &argv);