2 * Copyright (c) 2020 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/devel-api/actors/actor-devel.h>
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali-toolkit/devel-api/shader-effects/motion-stretch-effect.h>
28 using namespace Dali::Toolkit;
32 namespace // unnamed namespace
35 ////////////////////////////////////////////////////
37 // Demo setup parameters
40 const float MOTION_STRETCH_ACTOR_WIDTH = 256; // actor size on screen
41 const float MOTION_STRETCH_ACTOR_HEIGHT = 256; // ""
43 const int MOTION_STRETCH_NUM_ACTOR_IMAGES = 5;
44 const char* MOTION_STRETCH_ACTOR_IMAGE1( DEMO_IMAGE_DIR "image-with-border-1.jpg" );
45 const char* MOTION_STRETCH_ACTOR_IMAGE2( DEMO_IMAGE_DIR "image-with-border-2.jpg" );
46 const char* MOTION_STRETCH_ACTOR_IMAGE3( DEMO_IMAGE_DIR "image-with-border-3.jpg" );
47 const char* MOTION_STRETCH_ACTOR_IMAGE4( DEMO_IMAGE_DIR "image-with-border-4.jpg" );
48 const char* MOTION_STRETCH_ACTOR_IMAGE5( DEMO_IMAGE_DIR "image-with-border-5.jpg" );
50 const char* MOTION_STRETCH_ACTOR_IMAGES[] = {
51 MOTION_STRETCH_ACTOR_IMAGE1,
52 MOTION_STRETCH_ACTOR_IMAGE2,
53 MOTION_STRETCH_ACTOR_IMAGE3,
54 MOTION_STRETCH_ACTOR_IMAGE4,
55 MOTION_STRETCH_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 Stretch" );
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 // These values depend on the button background image
74 const Vector4 BUTTON_IMAGE_BORDER( Vector4::ONE * 3.0f );
76 const float UI_MARGIN = 4.0f; ///< Screen Margin for placement of UI buttons
78 const Vector3 BUTTON_SIZE_CONSTRAINT( 0.24f, 0.09f, 1.0f );
80 // move this button down a bit so it is visible on target and not covered up by toolbar
81 const float BUTTON_TITLE_LABEL_Y_OFFSET = 0.05f;
83 const float ORIENTATION_DURATION = 0.5f; ///< Time to rotate to new orientation.
84 } // unnamed namespace
90 class MotionStretchExampleApp : public ConnectionTracker
95 * DeviceOrientation describes the four different
96 * orientations the device can be in based on accelerometer reports.
98 enum DeviceOrientation
102 PORTRAIT_INVERSE = 180,
103 LANDSCAPE_INVERSE = 270
108 * @param application class, stored as reference
110 MotionStretchExampleApp(Application &app)
112 mActorEffectsEnabled(false),
113 mCurrentActorAnimation(0),
115 mOrientation( PORTRAIT )
117 // Connect to the Application's Init signal
118 app.InitSignal().Connect(this, &MotionStretchExampleApp::OnInit);
121 ~MotionStretchExampleApp()
123 // Nothing to do here; everything gets deleted automatically
127 * This method gets called once the main loop of application is up and running
129 void OnInit(Application& app)
131 // The Init signal is received once (only) during the Application lifetime
132 Window window = app.GetWindow();
134 window.KeyEventSignal().Connect(this, &MotionStretchExampleApp::OnKeyEvent);
136 // Creates a default view with a default tool bar.
137 // The view is added to the window.
138 mContentLayer = DemoHelper::CreateView( mApplication,
141 BACKGROUND_IMAGE_PATH,
145 // Ensure the content layer is a square so the touch area works in all orientations
146 Vector2 windowSize = window.GetSize();
147 float size = std::max( windowSize.width, windowSize.height );
148 mContentLayer.SetProperty( Actor::Property::SIZE, Vector2( size, size ) );
150 //Add an slideshow icon on the right of the title
151 mActorEffectsButton = Toolkit::PushButton::New();
152 mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
153 mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
154 mActorEffectsButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnEffectButtonClicked );
155 mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
157 // Creates a mode button.
158 // Create a effect toggle button. (right of toolbar)
159 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
160 layoutButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE );
161 layoutButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE_SELECTED );
162 layoutButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnLayoutButtonClicked);
163 layoutButton.SetProperty( Actor::Property::LEAVE_REQUIRED, true );
164 mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
167 mTapGestureDetector = TapGestureDetector::New();
168 mTapGestureDetector.Attach( mContentLayer );
169 mTapGestureDetector.DetectedSignal().Connect( this, &MotionStretchExampleApp::OnTap );
171 // set initial orientation
172 Dali::Window winHandle = app.GetWindow();
173 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
174 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
175 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
176 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
177 winHandle.ResizeSignal().Connect( this, &MotionStretchExampleApp::OnWindowResized );
179 // set initial orientation
182 ///////////////////////////////////////////////////////
184 // Motion stretched actor
186 mMotionStretchEffect = Toolkit::CreateMotionStretchEffect();
187 mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGE1;
188 mMotionStretchImageView = ImageView::New();
189 mMotionStretchImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect );
190 mMotionStretchImageView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
191 mMotionStretchImageView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
192 mMotionStretchImageView.SetProperty( Actor::Property::SIZE, Vector2( MOTION_STRETCH_ACTOR_WIDTH, MOTION_STRETCH_ACTOR_HEIGHT ) );
193 // Add stretch padding
194 mMotionStretchImageView.SetProperty( DevelActor::Property::UPDATE_SIZE_HINT, Vector2( MOTION_STRETCH_ACTOR_WIDTH+32, MOTION_STRETCH_ACTOR_HEIGHT+32 ) );
196 mContentLayer.Add( mMotionStretchImageView );
198 // Create shader used for doing motion stretch
199 Toolkit::SetMotionStretchProperties( mMotionStretchImageView );
202 //////////////////////////////////////////////////////////////
204 // Device Orientation Support
208 void OnWindowResized( Window window, Window::WindowSize size )
210 Rotate( size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT );
213 void Rotate( DeviceOrientation orientation )
215 // Resize the root actor
216 const Vector2 targetSize = mApplication.GetWindow().GetSize();
218 if( mOrientation != orientation )
220 mOrientation = orientation;
222 // check if actor is on window
223 if( mView.GetParent() )
225 // has parent so we expect it to be on window, start animation
226 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
227 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
228 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
229 mRotateAnimation.Play();
233 mView.SetProperty( Actor::Property::SIZE, targetSize );
238 // for first time just set size
239 mView.SetProperty( Actor::Property::SIZE, targetSize );
243 //////////////////////////////////////////////////////////////
249 // move to point on screen that was tapped
250 void OnTap( Actor actor, const TapGesture& tapGesture )
253 float originOffsetX, originOffsetY;
255 // rotate offset (from top left origin to centre) into actor space
256 Vector2 windowSize = mApplication.GetWindow().GetSize();
257 actor.ScreenToLocal(originOffsetX, originOffsetY, windowSize.width * 0.5f, windowSize.height * 0.5f);
259 // get dest point in local actor space
260 destPos.x = tapGesture.localPoint.x - originOffsetX;
261 destPos.y = tapGesture.localPoint.y - originOffsetY;
264 float animDuration = 0.5f;
265 mActorTapMovementAnimation = Animation::New( animDuration );
266 if ( mMotionStretchImageView )
268 mActorTapMovementAnimation.AnimateTo( Property(mMotionStretchImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
270 mActorTapMovementAnimation.SetEndAction( Animation::BAKE );
271 mActorTapMovementAnimation.Play();
274 // perform some spinning etc
275 if(mActorEffectsEnabled)
277 switch(mCurrentActorAnimation)
282 float animDuration = 1.0f;
283 mActorAnimation = Animation::New(animDuration);
284 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
285 mActorAnimation.SetEndAction( Animation::BAKE );
286 mActorAnimation.Play();
293 float animDuration = 1.0f;
294 mActorAnimation = Animation::New(animDuration);
295 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
296 mActorAnimation.SetEndAction( Animation::BAKE );
297 mActorAnimation.Play();
301 // spin around y and z
304 float animDuration = 1.0f;
305 mActorAnimation = Animation::New(animDuration);
306 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
307 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
308 mActorAnimation.SetEndAction( Animation::BAKE );
309 mActorAnimation.Play();
316 float animDuration = 1.0f;
317 mActorAnimation = Animation::New(animDuration);
318 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
319 mActorAnimation.SetEndAction( Animation::BAKE );
320 mActorAnimation.Play();
328 mCurrentActorAnimation++;
329 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
331 mCurrentActorAnimation = 0;
336 void ToggleActorEffects()
338 if(!mActorEffectsEnabled)
340 mActorEffectsEnabled = true;
341 mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON );
342 mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON_SELECTED );
346 mActorEffectsEnabled = false;
347 mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
348 mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
352 //////////////////////////////////////////////////////////////
358 bool OnLayoutButtonClicked( Toolkit::Button button )
364 bool OnEffectButtonClicked( Toolkit::Button button )
366 ToggleActorEffects();
371 * Main key event handler
373 void OnKeyEvent(const KeyEvent& event)
375 if(event.GetState() == KeyEvent::Down)
377 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
384 //////////////////////////////////////////////////////////////
394 if(MOTION_STRETCH_NUM_ACTOR_IMAGES == mCurrentImage)
399 mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGES[mCurrentImage];
400 mMotionStretchImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect );
405 Application& mApplication; ///< Application instance
406 Toolkit::Control mView;
407 Toolkit::ToolBar mToolBar;
408 Layer mContentLayer; ///< Content layer (contains actor for this stretch demo)
410 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
413 Property::Map mMotionStretchEffect;
414 ImageView mMotionStretchImageView;
416 // animate actor to position where user taps screen
417 Animation mActorTapMovementAnimation;
419 // show different animations to demonstrate stretch effect working on an object only movement basis
420 bool mActorEffectsEnabled;
421 Animation mActorAnimation;
422 int mCurrentActorAnimation;
424 // offer a selection of images that user can cycle between
427 TapGestureDetector mTapGestureDetector;
429 DeviceOrientation mOrientation; ///< Current Device orientation
430 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
434 int DALI_EXPORT_API main(int argc, char **argv)
436 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
437 MotionStretchExampleApp test(app);