2 * Copyright (c) 2019 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>
27 using namespace Dali::Toolkit;
31 namespace // unnamed namespace
34 ////////////////////////////////////////////////////
36 // Demo setup parameters
39 const float MOTION_BLUR_ACTOR_WIDTH = 256; // actor size on screen
40 const float MOTION_BLUR_ACTOR_HEIGHT = 256; // ""
41 const unsigned int MOTION_BLUR_NUM_SAMPLES = 8;
43 const int MOTION_BLUR_NUM_ACTOR_IMAGES = 5;
44 const char* MOTION_BLUR_ACTOR_IMAGE1( DEMO_IMAGE_DIR "image-with-border-1.jpg" );
45 const char* MOTION_BLUR_ACTOR_IMAGE2( DEMO_IMAGE_DIR "image-with-border-2.jpg" );
46 const char* MOTION_BLUR_ACTOR_IMAGE3( DEMO_IMAGE_DIR "image-with-border-3.jpg" );
47 const char* MOTION_BLUR_ACTOR_IMAGE4( DEMO_IMAGE_DIR "image-with-border-4.jpg" );
48 const char* MOTION_BLUR_ACTOR_IMAGE5( DEMO_IMAGE_DIR "image-with-border-1.jpg" );
50 const char* MOTION_BLUR_ACTOR_IMAGES[] = {
51 MOTION_BLUR_ACTOR_IMAGE1,
52 MOTION_BLUR_ACTOR_IMAGE2,
53 MOTION_BLUR_ACTOR_IMAGE3,
54 MOTION_BLUR_ACTOR_IMAGE4,
55 MOTION_BLUR_ACTOR_IMAGE5,
58 const int NUM_ACTOR_ANIMATIONS = 4;
59 const int NUM_CAMERA_ANIMATIONS = 2;
62 const char* BACKGROUND_IMAGE_PATH = DEMO_IMAGE_DIR "background-default.png";
64 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
65 const char* LAYOUT_IMAGE( DEMO_IMAGE_DIR "icon-change.png" );
66 const char* LAYOUT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
67 const char* APPLICATION_TITLE( "Motion Blur" );
68 const char* EFFECTS_OFF_ICON( DEMO_IMAGE_DIR "icon-effects-off.png" );
69 const char* EFFECTS_OFF_ICON_SELECTED( DEMO_IMAGE_DIR "icon-effects-off-selected.png" );
70 const char* EFFECTS_ON_ICON( DEMO_IMAGE_DIR "icon-effects-on.png" );
71 const char* EFFECTS_ON_ICON_SELECTED( DEMO_IMAGE_DIR "icon-effects-on-selected.png" );
73 const float UI_MARGIN = 4.0f; ///< Screen Margin for placement of UI buttons
75 const Vector3 BUTTON_SIZE_CONSTRAINT( 0.24f, 0.09f, 1.0f );
76 const Vector3 BUTTON_TITLE_LABEL_TAP_HERE_SIZE_CONSTRAINT( 0.55f, 0.06f, 1.0f );
77 const Vector3 BUTTON_TITLE_LABEL_INSTRUCTIONS_POPUP_SIZE_CONSTRAINT( 1.0f, 1.0f, 1.0f );
79 // move this button down a bit so it is visible on target and not covered up by toolbar
80 const float BUTTON_TITLE_LABEL_Y_OFFSET = 0.05f;
82 const float ORIENTATION_DURATION = 0.5f; ///< Time to rotate to new orientation.
85 * @brief Set an image to image view, scaled-down to no more than the dimensions passed in.
87 * Uses SHRINK_TO_FIT which ensures the resulting image is
88 * smaller than or equal to the specified dimensions while preserving its original aspect ratio.
90 void SetImageFittedInBox( ImageView& imageView, Property::Map& shaderEffect, const char * const imagePath, int maxWidth, int maxHeight )
93 map[Visual::Property::TYPE] = Visual::IMAGE;
94 map[ImageVisual::Property::URL] = imagePath;
95 // Load the image nicely scaled-down to fit within the specified max width and height:
96 map[ImageVisual::Property::DESIRED_WIDTH] = maxWidth;
97 map[ImageVisual::Property::DESIRED_HEIGHT] = maxHeight;
98 map[ImageVisual::Property::FITTING_MODE] = FittingMode::SHRINK_TO_FIT;
99 map[ImageVisual::Property::SAMPLING_MODE] = SamplingMode::BOX_THEN_LINEAR;
100 map.Merge( shaderEffect );
102 imageView.SetProperty( ImageView::Property::IMAGE, map );
105 } // unnamed namespace
109 class MotionBlurExampleApp : public ConnectionTracker
114 * DeviceOrientation describes the four different
115 * orientations the device can be in based on accelerometer reports.
117 enum DeviceOrientation
121 PORTRAIT_INVERSE = 180,
122 LANDSCAPE_INVERSE = 270
127 * @param application class, stored as reference
129 MotionBlurExampleApp(Application &app)
131 mActorEffectsEnabled(false),
132 mCurrentActorAnimation(0),
134 mOrientation( PORTRAIT )
136 // Connect to the Application's Init signal
137 app.InitSignal().Connect(this, &MotionBlurExampleApp::OnInit);
140 ~MotionBlurExampleApp()
142 // Nothing to do here; everything gets deleted automatically
146 * This method gets called once the main loop of application is up and running
148 void OnInit(Application& app)
150 // The Init signal is received once (only) during the Application lifetime
152 Stage::GetCurrent().KeyEventSignal().Connect(this, &MotionBlurExampleApp::OnKeyEvent);
155 // Creates a default view with a default tool bar.
156 // The view is added to the stage.
157 mContentLayer = DemoHelper::CreateView( mApplication,
160 BACKGROUND_IMAGE_PATH,
164 // Ensure the content layer is a square so the touch area works in all orientations
165 Vector2 stageSize = Stage::GetCurrent().GetSize();
166 float size = std::max( stageSize.width, stageSize.height );
167 mContentLayer.SetProperty( Actor::Property::SIZE, Vector2( size, size ) );
169 //Add an effects icon on the right of the title
170 mActorEffectsButton = Toolkit::PushButton::New();
171 mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
172 mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
173 mActorEffectsButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnEffectButtonClicked );
174 mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
176 // Creates a mode button.
177 // Create a effect toggle button. (right of toolbar)
178 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
179 layoutButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE );
180 layoutButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE_SELECTED );
181 layoutButton.ClickedSignal().Connect( this, &MotionBlurExampleApp::OnLayoutButtonClicked);
182 layoutButton.SetProperty( Actor::Property::LEAVE_REQUIRED, true );
183 mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
186 mTapGestureDetector = TapGestureDetector::New();
187 mTapGestureDetector.Attach( mContentLayer );
188 mTapGestureDetector.DetectedSignal().Connect( this, &MotionBlurExampleApp::OnTap );
190 Dali::Window winHandle = app.GetWindow();
191 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
192 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
193 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
194 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
195 winHandle.ResizedSignal().Connect( this, &MotionBlurExampleApp::OnWindowResized );
197 // set initial orientation
200 ///////////////////////////////////////////////////////
202 // Motion blurred actor
205 // Scale down actor to fit on very low resolution screens with space to interact:
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 mMotionBlurEffect = CreateMotionBlurEffect();
210 mMotionBlurImageView = ImageView::New();
211 SetImageFittedInBox( mMotionBlurImageView, mMotionBlurEffect, MOTION_BLUR_ACTOR_IMAGE1, mMotionBlurActorSize.x, mMotionBlurActorSize.y );
212 mMotionBlurImageView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
213 mMotionBlurImageView.SetProperty( Actor::Property::SIZE, Vector2(mMotionBlurActorSize.x, mMotionBlurActorSize.y) );
215 mContentLayer.Add( mMotionBlurImageView );
217 // Create shader used for doing motion blur
218 mMotionBlurEffect = CreateMotionBlurEffect();
220 // set actor shader to the blur one
221 Toolkit::SetMotionBlurProperties( mMotionBlurImageView, MOTION_BLUR_NUM_SAMPLES );
225 //////////////////////////////////////////////////////////////
227 // Device Orientation Support
231 void OnWindowResized( Window::WindowSize size )
233 Rotate( size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT );
236 void Rotate( DeviceOrientation orientation )
238 // Resize the root actor
239 const Vector2 targetSize = Stage::GetCurrent().GetSize();
241 if( mOrientation != orientation )
243 mOrientation = orientation;
245 // check if actor is on stage
246 if( mView.GetParent() )
248 // has parent so we expect it to be on stage, start animation
249 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
250 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
251 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
252 mRotateAnimation.Play();
256 mView.SetProperty( Actor::Property::SIZE, targetSize );
261 // for first time just set size
262 mView.SetProperty( Actor::Property::SIZE, targetSize );
267 //////////////////////////////////////////////////////////////
273 // move to point on screen that was tapped
274 void OnTap( Actor actor, const TapGesture& tapGesture )
277 float originOffsetX, originOffsetY;
279 // rotate offset (from top left origin to centre) into actor space
280 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
281 actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
283 // get dest point in local actor space
284 destPos.x = tapGesture.localPoint.x - originOffsetX;
285 destPos.y = tapGesture.localPoint.y - originOffsetY;
288 float animDuration = 0.5f;
289 mActorTapMovementAnimation = Animation::New( animDuration );
290 if ( mMotionBlurImageView )
292 mActorTapMovementAnimation.AnimateTo( Property(mMotionBlurImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
294 mActorTapMovementAnimation.SetEndAction( Animation::Bake );
295 mActorTapMovementAnimation.Play();
298 // perform some spinning etc
299 if(mActorEffectsEnabled)
301 switch(mCurrentActorAnimation)
306 float animDuration = 1.0f;
307 mActorAnimation = Animation::New(animDuration);
308 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
309 mActorAnimation.SetEndAction( Animation::Bake );
310 mActorAnimation.Play();
317 float animDuration = 1.0f;
318 mActorAnimation = Animation::New(animDuration);
319 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
320 mActorAnimation.SetEndAction( Animation::Bake );
321 mActorAnimation.Play();
325 // spin around y and z
328 float animDuration = 1.0f;
329 mActorAnimation = Animation::New(animDuration);
330 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
331 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
332 mActorAnimation.SetEndAction( Animation::Bake );
333 mActorAnimation.Play();
340 float animDuration = 1.0f;
341 mActorAnimation = Animation::New(animDuration);
342 mActorAnimation.AnimateBy( Property( mMotionBlurImageView, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
343 mActorAnimation.SetEndAction( Animation::Bake );
344 mActorAnimation.Play();
352 mCurrentActorAnimation++;
353 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
355 mCurrentActorAnimation = 0;
360 void ToggleActorEffects()
362 if(!mActorEffectsEnabled)
364 mActorEffectsEnabled = true;
365 mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON );
366 mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON_SELECTED );
370 mActorEffectsEnabled = false;
371 mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
372 mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
376 //////////////////////////////////////////////////////////////
382 bool OnLayoutButtonClicked( Toolkit::Button button )
388 bool OnEffectButtonClicked( Toolkit::Button button )
390 ToggleActorEffects();
395 * Main key event handler
397 void OnKeyEvent(const KeyEvent& event)
399 if(event.state == KeyEvent::Down)
401 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
408 //////////////////////////////////////////////////////////////
418 if(MOTION_BLUR_NUM_ACTOR_IMAGES == mCurrentImage)
422 SetImageFittedInBox( mMotionBlurImageView, mMotionBlurEffect, MOTION_BLUR_ACTOR_IMAGES[mCurrentImage], mMotionBlurActorSize.x, mMotionBlurActorSize.y );
428 Application& mApplication; ///< Application instance
429 Toolkit::Control mView;
430 Toolkit::ToolBar mToolBar;
432 Layer mContentLayer; ///< Content layer (contains actor for this blur demo)
434 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
437 Property::Map mMotionBlurEffect;
438 ImageView mMotionBlurImageView;
439 Size mMotionBlurActorSize;
441 // animate actor to position where user taps screen
442 Animation mActorTapMovementAnimation;
444 // show different animations to demonstrate blur effect working on an object only movement basis
445 bool mActorEffectsEnabled;
446 Animation mActorAnimation;
447 int mCurrentActorAnimation;
449 // offer a selection of images that user can cycle between
452 TapGestureDetector mTapGestureDetector;
454 DeviceOrientation mOrientation; ///< Current Device orientation
455 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
458 int DALI_EXPORT_API main(int argc, char **argv)
460 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
461 MotionBlurExampleApp test(app);