2 * Copyright (c) 2021 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-stretch-effect.h>
23 #include <dali/dali.h>
24 #include <dali/devel-api/actors/actor-devel.h>
25 #include "shared/view.h"
28 using namespace Dali::Toolkit;
30 namespace // unnamed namespace
32 ////////////////////////////////////////////////////
34 // Demo setup parameters
37 const float MOTION_STRETCH_ACTOR_WIDTH = 256; // actor size on screen
38 const float MOTION_STRETCH_ACTOR_HEIGHT = 256; // ""
40 const int MOTION_STRETCH_NUM_ACTOR_IMAGES = 5;
41 const char* MOTION_STRETCH_ACTOR_IMAGE1(DEMO_IMAGE_DIR "image-with-border-1.jpg");
42 const char* MOTION_STRETCH_ACTOR_IMAGE2(DEMO_IMAGE_DIR "image-with-border-2.jpg");
43 const char* MOTION_STRETCH_ACTOR_IMAGE3(DEMO_IMAGE_DIR "image-with-border-3.jpg");
44 const char* MOTION_STRETCH_ACTOR_IMAGE4(DEMO_IMAGE_DIR "image-with-border-4.jpg");
45 const char* MOTION_STRETCH_ACTOR_IMAGE5(DEMO_IMAGE_DIR "image-with-border-5.jpg");
47 const char* MOTION_STRETCH_ACTOR_IMAGES[] = {
48 MOTION_STRETCH_ACTOR_IMAGE1,
49 MOTION_STRETCH_ACTOR_IMAGE2,
50 MOTION_STRETCH_ACTOR_IMAGE3,
51 MOTION_STRETCH_ACTOR_IMAGE4,
52 MOTION_STRETCH_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 Stretch");
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 // These values depend on the button background image
70 const Vector4 BUTTON_IMAGE_BORDER(Vector4::ONE * 3.0f);
72 const float UI_MARGIN = 4.0f; ///< Screen Margin for placement of UI buttons
74 const Vector3 BUTTON_SIZE_CONSTRAINT(0.24f, 0.09f, 1.0f);
76 // move this button down a bit so it is visible on target and not covered up by toolbar
77 const float BUTTON_TITLE_LABEL_Y_OFFSET = 0.05f;
79 const float ORIENTATION_DURATION = 0.5f; ///< Time to rotate to new orientation.
80 } // unnamed namespace
83 class MotionStretchExampleApp : public ConnectionTracker
87 * DeviceOrientation describes the four different
88 * orientations the device can be in based on accelerometer reports.
90 enum DeviceOrientation
94 PORTRAIT_INVERSE = 180,
95 LANDSCAPE_INVERSE = 270
100 * @param application class, stored as reference
102 MotionStretchExampleApp(Application& app)
104 mActorEffectsEnabled(false),
105 mCurrentActorAnimation(0),
107 mOrientation(PORTRAIT)
109 // Connect to the Application's Init signal
110 app.InitSignal().Connect(this, &MotionStretchExampleApp::OnInit);
113 ~MotionStretchExampleApp()
115 // Nothing to do here; everything gets deleted automatically
119 * This method gets called once the main loop of application is up and running
121 void OnInit(Application& app)
123 // The Init signal is received once (only) during the Application lifetime
124 Window window = app.GetWindow();
126 window.KeyEventSignal().Connect(this, &MotionStretchExampleApp::OnKeyEvent);
128 // Creates a default view with a default tool bar.
129 // The view is added to the window.
130 mContentLayer = DemoHelper::CreateView(mApplication,
133 BACKGROUND_IMAGE_PATH,
137 // Ensure the content layer is a square so the touch area works in all orientations
138 Vector2 windowSize = window.GetSize();
139 float size = std::max(windowSize.width, windowSize.height);
140 mContentLayer.SetProperty(Actor::Property::SIZE, Vector2(size, size));
142 //Add an slideshow icon on the right of the title
143 mActorEffectsButton = Toolkit::PushButton::New();
144 mActorEffectsButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON);
145 mActorEffectsButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED);
146 mActorEffectsButton.ClickedSignal().Connect(this, &MotionStretchExampleApp::OnEffectButtonClicked);
147 mToolBar.AddControl(mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_CENTER, DemoHelper::DEFAULT_PLAY_PADDING);
149 // Creates a mode button.
150 // Create a effect toggle button. (right of toolbar)
151 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
152 layoutButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE);
153 layoutButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE_SELECTED);
154 layoutButton.ClickedSignal().Connect(this, &MotionStretchExampleApp::OnLayoutButtonClicked);
155 layoutButton.SetProperty(Actor::Property::LEAVE_REQUIRED, true);
156 mToolBar.AddControl(layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_RIGHT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING);
159 mTapGestureDetector = TapGestureDetector::New();
160 mTapGestureDetector.Attach(mContentLayer);
161 mTapGestureDetector.DetectedSignal().Connect(this, &MotionStretchExampleApp::OnTap);
163 // set initial orientation
164 Dali::Window winHandle = app.GetWindow();
165 winHandle.AddAvailableOrientation(Dali::WindowOrientation::PORTRAIT);
166 winHandle.AddAvailableOrientation(Dali::WindowOrientation::LANDSCAPE);
167 winHandle.AddAvailableOrientation(Dali::WindowOrientation::PORTRAIT_INVERSE);
168 winHandle.AddAvailableOrientation(Dali::WindowOrientation::LANDSCAPE_INVERSE);
169 winHandle.ResizeSignal().Connect(this, &MotionStretchExampleApp::OnWindowResized);
171 // set initial orientation
174 ///////////////////////////////////////////////////////
176 // Motion stretched actor
178 mMotionStretchEffect = Toolkit::CreateMotionStretchEffect();
179 mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGE1;
180 mMotionStretchImageView = ImageView::New();
181 mMotionStretchImageView.SetProperty(Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect);
182 mMotionStretchImageView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
183 mMotionStretchImageView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
184 mMotionStretchImageView.SetProperty(Actor::Property::SIZE, Vector2(MOTION_STRETCH_ACTOR_WIDTH, MOTION_STRETCH_ACTOR_HEIGHT));
185 // Add stretch padding
186 mMotionStretchImageView.SetProperty(DevelActor::Property::UPDATE_SIZE_HINT, Vector2(MOTION_STRETCH_ACTOR_WIDTH + 32, MOTION_STRETCH_ACTOR_HEIGHT + 32));
188 mContentLayer.Add(mMotionStretchImageView);
190 // Create shader used for doing motion stretch
191 Toolkit::SetMotionStretchProperties(mMotionStretchImageView);
194 //////////////////////////////////////////////////////////////
196 // Device Orientation Support
200 void OnWindowResized(Window window, Window::WindowSize size)
202 Rotate(size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT);
205 void Rotate(DeviceOrientation orientation)
207 // Resize the root actor
208 const Vector2 targetSize = mApplication.GetWindow().GetSize();
210 if(mOrientation != orientation)
212 mOrientation = orientation;
214 // check if actor is on window
215 if(mView.GetParent())
217 // has parent so we expect it to be on window, start animation
218 mRotateAnimation = Animation::New(ORIENTATION_DURATION);
219 mRotateAnimation.AnimateTo(Property(mView, Actor::Property::SIZE_WIDTH), targetSize.width);
220 mRotateAnimation.AnimateTo(Property(mView, Actor::Property::SIZE_HEIGHT), targetSize.height);
221 mRotateAnimation.Play();
225 mView.SetProperty(Actor::Property::SIZE, targetSize);
230 // for first time just set size
231 mView.SetProperty(Actor::Property::SIZE, targetSize);
235 //////////////////////////////////////////////////////////////
241 // move to point on screen that was tapped
242 void OnTap(Actor actor, const TapGesture& tapGesture)
245 float originOffsetX, originOffsetY;
247 // rotate offset (from top left origin to centre) into actor space
248 Vector2 windowSize = mApplication.GetWindow().GetSize();
249 actor.ScreenToLocal(originOffsetX, originOffsetY, windowSize.width * 0.5f, windowSize.height * 0.5f);
251 // get dest point in local actor space
252 const Vector2& localPoint = tapGesture.GetLocalPoint();
253 destPos.x = localPoint.x - originOffsetX;
254 destPos.y = localPoint.y - originOffsetY;
257 float tapMovementAnimDuration = 0.5f;
258 mActorTapMovementAnimation = Animation::New(tapMovementAnimDuration);
259 if(mMotionStretchImageView)
261 mActorTapMovementAnimation.AnimateTo(Property(mMotionStretchImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(tapMovementAnimDuration));
263 mActorTapMovementAnimation.SetEndAction(Animation::BAKE);
264 mActorTapMovementAnimation.Play();
266 // perform some spinning etc
267 if(mActorEffectsEnabled)
269 switch(mCurrentActorAnimation)
274 float animDuration = 1.0f;
275 mActorAnimation = Animation::New(animDuration);
276 mActorAnimation.AnimateBy(Property(mMotionStretchImageView, Actor::Property::ORIENTATION), Quaternion(Radian(Degree(360.0f)), Vector3::YAXIS), AlphaFunction::EASE_IN_OUT);
277 mActorAnimation.SetEndAction(Animation::BAKE);
278 mActorAnimation.Play();
285 float animDuration = 1.0f;
286 mActorAnimation = Animation::New(animDuration);
287 mActorAnimation.AnimateBy(Property(mMotionStretchImageView, Actor::Property::ORIENTATION), Quaternion(Radian(Degree(360.0f)), Vector3::ZAXIS), AlphaFunction::EASE_IN_OUT);
288 mActorAnimation.SetEndAction(Animation::BAKE);
289 mActorAnimation.Play();
293 // spin around y and z
296 float animDuration = 1.0f;
297 mActorAnimation = Animation::New(animDuration);
298 mActorAnimation.AnimateBy(Property(mMotionStretchImageView, Actor::Property::ORIENTATION), Quaternion(Radian(Degree(360.0f)), Vector3::YAXIS), AlphaFunction::EASE_IN_OUT);
299 mActorAnimation.AnimateBy(Property(mMotionStretchImageView, Actor::Property::ORIENTATION), Quaternion(Radian(Degree(360.0f)), Vector3::ZAXIS), AlphaFunction::EASE_IN_OUT);
300 mActorAnimation.SetEndAction(Animation::BAKE);
301 mActorAnimation.Play();
308 float animDuration = 1.0f;
309 mActorAnimation = Animation::New(animDuration);
310 mActorAnimation.AnimateBy(Property(mMotionStretchImageView, Actor::Property::SCALE), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod(0.0f, 1.0f));
311 mActorAnimation.SetEndAction(Animation::BAKE);
312 mActorAnimation.Play();
320 mCurrentActorAnimation++;
321 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
323 mCurrentActorAnimation = 0;
328 void ToggleActorEffects()
330 if(!mActorEffectsEnabled)
332 mActorEffectsEnabled = true;
333 mActorEffectsButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON);
334 mActorEffectsButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON_SELECTED);
338 mActorEffectsEnabled = false;
339 mActorEffectsButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON);
340 mActorEffectsButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED);
344 //////////////////////////////////////////////////////////////
350 bool OnLayoutButtonClicked(Toolkit::Button button)
356 bool OnEffectButtonClicked(Toolkit::Button button)
358 ToggleActorEffects();
363 * Main key event handler
365 void OnKeyEvent(const KeyEvent& event)
367 if(event.GetState() == KeyEvent::DOWN)
369 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
376 //////////////////////////////////////////////////////////////
385 if(MOTION_STRETCH_NUM_ACTOR_IMAGES == mCurrentImage)
390 mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGES[mCurrentImage];
391 mMotionStretchImageView.SetProperty(Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect);
395 Application& mApplication; ///< Application instance
396 Toolkit::Control mView;
397 Toolkit::ToolBar mToolBar;
398 Layer mContentLayer; ///< Content layer (contains actor for this stretch demo)
400 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
403 Property::Map mMotionStretchEffect;
404 ImageView mMotionStretchImageView;
406 // animate actor to position where user taps screen
407 Animation mActorTapMovementAnimation;
409 // show different animations to demonstrate stretch effect working on an object only movement basis
410 bool mActorEffectsEnabled;
411 Animation mActorAnimation;
412 int mCurrentActorAnimation;
414 // offer a selection of images that user can cycle between
417 TapGestureDetector mTapGestureDetector;
419 DeviceOrientation mOrientation; ///< Current Device orientation
420 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
423 int DALI_EXPORT_API main(int argc, char** argv)
425 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
426 MotionStretchExampleApp test(app);