2 * Copyright (c) 2022 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 <dali-toolkit/dali-toolkit.h>
22 #include <dali-toolkit/devel-api/shader-effects/motion-blur-effect.h>
23 #include <dali/dali.h>
24 #include "shared/view.h"
27 using namespace Dali::Toolkit;
29 namespace // unnamed namespace
31 ////////////////////////////////////////////////////
33 // Demo setup parameters
36 const float MOTION_BLUR_ACTOR_WIDTH = 256; // actor size on screen
37 const float MOTION_BLUR_ACTOR_HEIGHT = 256; // ""
38 const unsigned int MOTION_BLUR_NUM_SAMPLES = 8;
40 const int MOTION_BLUR_NUM_ACTOR_IMAGES = 5;
41 const char* MOTION_BLUR_ACTOR_IMAGE1(DEMO_IMAGE_DIR "image-with-border-1.jpg");
42 const char* MOTION_BLUR_ACTOR_IMAGE2(DEMO_IMAGE_DIR "image-with-border-2.jpg");
43 const char* MOTION_BLUR_ACTOR_IMAGE3(DEMO_IMAGE_DIR "image-with-border-3.jpg");
44 const char* MOTION_BLUR_ACTOR_IMAGE4(DEMO_IMAGE_DIR "image-with-border-4.jpg");
45 const char* MOTION_BLUR_ACTOR_IMAGE5(DEMO_IMAGE_DIR "image-with-border-1.jpg");
47 const char* MOTION_BLUR_ACTOR_IMAGES[] = {
48 MOTION_BLUR_ACTOR_IMAGE1,
49 MOTION_BLUR_ACTOR_IMAGE2,
50 MOTION_BLUR_ACTOR_IMAGE3,
51 MOTION_BLUR_ACTOR_IMAGE4,
52 MOTION_BLUR_ACTOR_IMAGE5,
55 const int NUM_ACTOR_ANIMATIONS = 4;
56 const int NUM_CAMERA_ANIMATIONS = 2;
58 const char* BACKGROUND_IMAGE_PATH = DEMO_IMAGE_DIR "background-default.png";
60 const char* TOOLBAR_IMAGE(DEMO_IMAGE_DIR "top-bar.png");
61 const char* LAYOUT_IMAGE(DEMO_IMAGE_DIR "icon-change.png");
62 const char* LAYOUT_IMAGE_SELECTED(DEMO_IMAGE_DIR "icon-change-selected.png");
63 const char* APPLICATION_TITLE("Motion Blur");
64 const char* EFFECTS_OFF_ICON(DEMO_IMAGE_DIR "icon-effects-off.png");
65 const char* EFFECTS_OFF_ICON_SELECTED(DEMO_IMAGE_DIR "icon-effects-off-selected.png");
66 const char* EFFECTS_ON_ICON(DEMO_IMAGE_DIR "icon-effects-on.png");
67 const char* EFFECTS_ON_ICON_SELECTED(DEMO_IMAGE_DIR "icon-effects-on-selected.png");
69 const float UI_MARGIN = 4.0f; ///< Screen Margin for placement of UI buttons
71 const Vector3 BUTTON_SIZE_CONSTRAINT(0.24f, 0.09f, 1.0f);
72 const Vector3 BUTTON_TITLE_LABEL_TAP_HERE_SIZE_CONSTRAINT(0.55f, 0.06f, 1.0f);
73 const Vector3 BUTTON_TITLE_LABEL_INSTRUCTIONS_POPUP_SIZE_CONSTRAINT(1.0f, 1.0f, 1.0f);
75 // move this button down a bit so it is visible on target and not covered up by toolbar
76 const float BUTTON_TITLE_LABEL_Y_OFFSET = 0.05f;
78 const float ORIENTATION_DURATION = 0.5f; ///< Time to rotate to new orientation.
81 * @brief Set an image to image view, scaled-down to no more than the dimensions passed in.
83 * Uses SHRINK_TO_FIT which ensures the resulting image is
84 * smaller than or equal to the specified dimensions while preserving its original aspect ratio.
86 void SetImageFittedInBox(ImageView& imageView, Property::Map& shaderEffect, const char* const imagePath, int maxWidth, int maxHeight)
89 map[Visual::Property::TYPE] = Visual::IMAGE;
90 map[ImageVisual::Property::URL] = imagePath;
91 // Load the image nicely scaled-down to fit within the specified max width and height:
92 map[ImageVisual::Property::DESIRED_WIDTH] = maxWidth;
93 map[ImageVisual::Property::DESIRED_HEIGHT] = maxHeight;
94 map[ImageVisual::Property::FITTING_MODE] = FittingMode::SHRINK_TO_FIT;
95 map[ImageVisual::Property::SAMPLING_MODE] = SamplingMode::BOX_THEN_LINEAR;
96 map.Merge(shaderEffect);
98 imageView.SetProperty(ImageView::Property::IMAGE, map);
101 } // unnamed namespace
104 class MotionBlurExampleApp : public ConnectionTracker
108 * DeviceOrientation describes the four different
109 * orientations the device can be in based on accelerometer reports.
111 enum DeviceOrientation
115 PORTRAIT_INVERSE = 180,
116 LANDSCAPE_INVERSE = 270
121 * @param application class, stored as reference
123 MotionBlurExampleApp(Application& app)
125 mActorEffectsEnabled(false),
126 mCurrentActorAnimation(0),
128 mOrientation(PORTRAIT)
130 // Connect to the Application's Init signal
131 app.InitSignal().Connect(this, &MotionBlurExampleApp::OnInit);
134 ~MotionBlurExampleApp()
136 // Nothing to do here; everything gets deleted automatically
140 * This method gets called once the main loop of application is up and running
142 void OnInit(Application& app)
144 // The Init signal is received once (only) during the Application lifetime
145 Window window = app.GetWindow();
147 window.KeyEventSignal().Connect(this, &MotionBlurExampleApp::OnKeyEvent);
149 // Creates a default view with a default tool bar.
150 // The view is added to the window.
151 mContentLayer = DemoHelper::CreateView(mApplication,
154 BACKGROUND_IMAGE_PATH,
158 // Ensure the content layer is a square so the touch area works in all orientations
159 Vector2 windowSize = window.GetSize();
160 float size = std::max(windowSize.width, windowSize.height);
161 mContentLayer.SetProperty(Actor::Property::SIZE, Vector2(size, size));
163 //Add an effects icon on the right of the title
164 mActorEffectsButton = Toolkit::PushButton::New();
165 mActorEffectsButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON);
166 mActorEffectsButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED);
167 mActorEffectsButton.ClickedSignal().Connect(this, &MotionBlurExampleApp::OnEffectButtonClicked);
168 mToolBar.AddControl(mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_CENTER, DemoHelper::DEFAULT_PLAY_PADDING);
170 // Creates a mode button.
171 // Create a effect toggle button. (right of toolbar)
172 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
173 layoutButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE);
174 layoutButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE_SELECTED);
175 layoutButton.ClickedSignal().Connect(this, &MotionBlurExampleApp::OnLayoutButtonClicked);
176 layoutButton.SetProperty(Actor::Property::LEAVE_REQUIRED, true);
177 mToolBar.AddControl(layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_RIGHT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING);
180 mTapGestureDetector = TapGestureDetector::New();
181 mTapGestureDetector.Attach(mContentLayer);
182 mTapGestureDetector.DetectedSignal().Connect(this, &MotionBlurExampleApp::OnTap);
184 Dali::Window winHandle = app.GetWindow();
185 winHandle.AddAvailableOrientation(Dali::WindowOrientation::PORTRAIT);
186 winHandle.AddAvailableOrientation(Dali::WindowOrientation::LANDSCAPE);
187 winHandle.AddAvailableOrientation(Dali::WindowOrientation::PORTRAIT_INVERSE);
188 winHandle.AddAvailableOrientation(Dali::WindowOrientation::LANDSCAPE_INVERSE);
189 winHandle.ResizeSignal().Connect(this, &MotionBlurExampleApp::OnWindowResized);
191 // set initial orientation
194 ///////////////////////////////////////////////////////
196 // Motion blurred actor
199 // Scale down actor to fit on very low resolution screens with space to interact:
200 mMotionBlurActorSize = Size(std::min(windowSize.x * 0.3f, MOTION_BLUR_ACTOR_WIDTH), std::min(windowSize.y * 0.3f, MOTION_BLUR_ACTOR_HEIGHT));
201 mMotionBlurActorUpdateSize = Size(std::max(mMotionBlurActorSize.x, mMotionBlurActorSize.y), std::max(mMotionBlurActorSize.x, mMotionBlurActorSize.y));
202 mMotionBlurActorSize = Size(std::min(mMotionBlurActorSize.x, mMotionBlurActorSize.y), std::min(mMotionBlurActorSize.x, mMotionBlurActorSize.y));
204 mMotionBlurEffect = CreateMotionBlurEffect();
205 mMotionBlurImageView = ImageView::New();
206 SetImageFittedInBox(mMotionBlurImageView, mMotionBlurEffect, MOTION_BLUR_ACTOR_IMAGE1, mMotionBlurActorSize.x, mMotionBlurActorSize.y);
207 mMotionBlurImageView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
208 mMotionBlurImageView.SetProperty(Actor::Property::SIZE, mMotionBlurActorUpdateSize);
209 mMotionBlurImageView.SetProperty(Actor::Property::UPDATE_AREA_HINT, Vector4(0, 0, mMotionBlurActorUpdateSize.width, mMotionBlurActorUpdateSize.height));
211 mContentLayer.Add(mMotionBlurImageView);
213 // Create shader used for doing motion blur
214 mMotionBlurEffect = CreateMotionBlurEffect();
216 // set actor shader to the blur one
217 Toolkit::SetMotionBlurProperties(mMotionBlurImageView, MOTION_BLUR_NUM_SAMPLES);
220 //////////////////////////////////////////////////////////////
222 // Device Orientation Support
226 void OnWindowResized(Window window, Window::WindowSize size)
228 Rotate(size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT);
231 void Rotate(DeviceOrientation orientation)
233 // Resize the root actor
234 const Vector2 targetSize = mApplication.GetWindow().GetSize();
236 if(mOrientation != orientation)
238 mOrientation = orientation;
240 // check if actor is on window
241 if(mView.GetParent())
243 // has parent so we expect it to be on window, start animation
244 mRotateAnimation = Animation::New(ORIENTATION_DURATION);
245 mRotateAnimation.AnimateTo(Property(mView, Actor::Property::SIZE_WIDTH), targetSize.width);
246 mRotateAnimation.AnimateTo(Property(mView, Actor::Property::SIZE_HEIGHT), targetSize.height);
247 mRotateAnimation.Play();
251 mView.SetProperty(Actor::Property::SIZE, targetSize);
256 // for first time just set size
257 mView.SetProperty(Actor::Property::SIZE, targetSize);
261 //////////////////////////////////////////////////////////////
267 // move to point on screen that was tapped
268 void OnTap(Actor actor, const TapGesture& tapGesture)
271 float originOffsetX, originOffsetY;
273 // rotate offset (from top left origin to centre) into actor space
274 Vector2 windowSize = mApplication.GetWindow().GetSize();
275 actor.ScreenToLocal(originOffsetX, originOffsetY, windowSize.width * 0.5f, windowSize.height * 0.5f);
277 // get dest point in local actor space
278 const Vector2& localPoint = tapGesture.GetLocalPoint();
279 destPos.x = localPoint.x - originOffsetX;
280 destPos.y = localPoint.y - originOffsetY;
283 float tapMovementAnimDuration = 0.5f;
284 mActorTapMovementAnimation = Animation::New(tapMovementAnimDuration);
285 if(mMotionBlurImageView)
287 mActorTapMovementAnimation.AnimateTo(Property(mMotionBlurImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(tapMovementAnimDuration));
289 mActorTapMovementAnimation.SetEndAction(Animation::BAKE);
290 mActorTapMovementAnimation.Play();
292 // perform some spinning etc
293 if(mActorEffectsEnabled)
295 switch(mCurrentActorAnimation)
300 float animDuration = 1.0f;
301 mActorAnimation = Animation::New(animDuration);
302 mActorAnimation.AnimateBy(Property(mMotionBlurImageView, Actor::Property::ORIENTATION), Quaternion(Radian(Degree(360.0f)), Vector3::YAXIS), AlphaFunction::EASE_IN_OUT);
303 mActorAnimation.SetEndAction(Animation::BAKE);
304 mActorAnimation.Play();
311 float animDuration = 1.0f;
312 mActorAnimation = Animation::New(animDuration);
313 mActorAnimation.AnimateBy(Property(mMotionBlurImageView, Actor::Property::ORIENTATION), Quaternion(Radian(Degree(360.0f)), Vector3::ZAXIS), AlphaFunction::EASE_IN_OUT);
314 mActorAnimation.SetEndAction(Animation::BAKE);
315 mActorAnimation.Play();
319 // spin around y and z
322 float animDuration = 1.0f;
323 mActorAnimation = Animation::New(animDuration);
324 mActorAnimation.AnimateBy(Property(mMotionBlurImageView, Actor::Property::ORIENTATION), Quaternion(Radian(Degree(360.0f)), Vector3::YAXIS), AlphaFunction::EASE_IN_OUT);
325 mActorAnimation.AnimateBy(Property(mMotionBlurImageView, Actor::Property::ORIENTATION), Quaternion(Radian(Degree(360.0f)), Vector3::ZAXIS), AlphaFunction::EASE_IN_OUT);
326 mActorAnimation.SetEndAction(Animation::BAKE);
327 mActorAnimation.Play();
334 float animDuration = 1.0f;
335 mActorAnimation = Animation::New(animDuration);
336 mActorAnimation.AnimateBy(Property(mMotionBlurImageView, Actor::Property::SCALE), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod(0.0f, 1.0f));
337 mActorAnimation.SetEndAction(Animation::BAKE);
338 mActorAnimation.Play();
346 mCurrentActorAnimation++;
347 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
349 mCurrentActorAnimation = 0;
354 void ToggleActorEffects()
356 if(!mActorEffectsEnabled)
358 mActorEffectsEnabled = true;
359 mActorEffectsButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON);
360 mActorEffectsButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON_SELECTED);
364 mActorEffectsEnabled = false;
365 mActorEffectsButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON);
366 mActorEffectsButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED);
370 //////////////////////////////////////////////////////////////
376 bool OnLayoutButtonClicked(Toolkit::Button button)
382 bool OnEffectButtonClicked(Toolkit::Button button)
384 ToggleActorEffects();
389 * Main key event handler
391 void OnKeyEvent(const KeyEvent& event)
393 if(event.GetState() == KeyEvent::DOWN)
395 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
402 //////////////////////////////////////////////////////////////
411 if(MOTION_BLUR_NUM_ACTOR_IMAGES == mCurrentImage)
415 SetImageFittedInBox(mMotionBlurImageView, mMotionBlurEffect, MOTION_BLUR_ACTOR_IMAGES[mCurrentImage], mMotionBlurActorSize.x, mMotionBlurActorSize.y);
419 Application& mApplication; ///< Application instance
420 Toolkit::Control mView;
421 Toolkit::ToolBar mToolBar;
423 Layer mContentLayer; ///< Content layer (contains actor for this blur demo)
425 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
428 Property::Map mMotionBlurEffect;
429 ImageView mMotionBlurImageView;
430 Size mMotionBlurActorSize;
431 Size mMotionBlurActorUpdateSize;
433 // animate actor to position where user taps screen
434 Animation mActorTapMovementAnimation;
436 // show different animations to demonstrate blur effect working on an object only movement basis
437 bool mActorEffectsEnabled;
438 Animation mActorAnimation;
439 int mCurrentActorAnimation;
441 // offer a selection of images that user can cycle between
444 TapGestureDetector mTapGestureDetector;
446 DeviceOrientation mOrientation; ///< Current Device orientation
447 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
450 int DALI_EXPORT_API main(int argc, char** argv)
452 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
453 MotionBlurExampleApp test(app);