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
133 Stage::GetCurrent().KeyEventSignal().Connect(this, &MotionStretchExampleApp::OnKeyEvent);
135 // Creates a default view with a default tool bar.
136 // The view is added to the stage.
137 mContentLayer = DemoHelper::CreateView( mApplication,
140 BACKGROUND_IMAGE_PATH,
144 // Ensure the content layer is a square so the touch area works in all orientations
145 Vector2 stageSize = Stage::GetCurrent().GetSize();
146 float size = std::max( stageSize.width, stageSize.height );
147 mContentLayer.SetProperty( Actor::Property::SIZE, Vector2( size, size ) );
149 //Add an slideshow icon on the right of the title
150 mActorEffectsButton = Toolkit::PushButton::New();
151 mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
152 mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
153 mActorEffectsButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnEffectButtonClicked );
154 mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
156 // Creates a mode button.
157 // Create a effect toggle button. (right of toolbar)
158 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
159 layoutButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE );
160 layoutButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE_SELECTED );
161 layoutButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnLayoutButtonClicked);
162 layoutButton.SetProperty( Actor::Property::LEAVE_REQUIRED, true );
163 mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
166 mTapGestureDetector = TapGestureDetector::New();
167 mTapGestureDetector.Attach( mContentLayer );
168 mTapGestureDetector.DetectedSignal().Connect( this, &MotionStretchExampleApp::OnTap );
170 // set initial orientation
171 Dali::Window winHandle = app.GetWindow();
172 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
173 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
174 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
175 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
176 winHandle.ResizeSignal().Connect( this, &MotionStretchExampleApp::OnWindowResized );
178 // set initial orientation
181 ///////////////////////////////////////////////////////
183 // Motion stretched actor
185 mMotionStretchEffect = Toolkit::CreateMotionStretchEffect();
186 mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGE1;
187 mMotionStretchImageView = ImageView::New();
188 mMotionStretchImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect );
189 mMotionStretchImageView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
190 mMotionStretchImageView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
191 mMotionStretchImageView.SetProperty( Actor::Property::SIZE, Vector2( MOTION_STRETCH_ACTOR_WIDTH, MOTION_STRETCH_ACTOR_HEIGHT ) );
192 // Add stretch padding
193 mMotionStretchImageView.SetProperty( DevelActor::Property::UPDATE_SIZE_HINT, Vector2( MOTION_STRETCH_ACTOR_WIDTH+32, MOTION_STRETCH_ACTOR_HEIGHT+32 ) );
195 mContentLayer.Add( mMotionStretchImageView );
197 // Create shader used for doing motion stretch
198 Toolkit::SetMotionStretchProperties( mMotionStretchImageView );
201 //////////////////////////////////////////////////////////////
203 // Device Orientation Support
207 void OnWindowResized( Window window, Window::WindowSize size )
209 Rotate( size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT );
212 void Rotate( DeviceOrientation orientation )
214 // Resize the root actor
215 const Vector2 targetSize = Stage::GetCurrent().GetSize();
217 if( mOrientation != orientation )
219 mOrientation = orientation;
221 // check if actor is on stage
222 if( mView.GetParent() )
224 // has parent so we expect it to be on stage, start animation
225 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
226 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
227 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
228 mRotateAnimation.Play();
232 mView.SetProperty( Actor::Property::SIZE, targetSize );
237 // for first time just set size
238 mView.SetProperty( Actor::Property::SIZE, targetSize );
242 //////////////////////////////////////////////////////////////
248 // move to point on screen that was tapped
249 void OnTap( Actor actor, const TapGesture& tapGesture )
252 float originOffsetX, originOffsetY;
254 // rotate offset (from top left origin to centre) into actor space
255 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
256 actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
258 // get dest point in local actor space
259 destPos.x = tapGesture.localPoint.x - originOffsetX;
260 destPos.y = tapGesture.localPoint.y - originOffsetY;
263 float animDuration = 0.5f;
264 mActorTapMovementAnimation = Animation::New( animDuration );
265 if ( mMotionStretchImageView )
267 mActorTapMovementAnimation.AnimateTo( Property(mMotionStretchImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
269 mActorTapMovementAnimation.SetEndAction( Animation::Bake );
270 mActorTapMovementAnimation.Play();
273 // perform some spinning etc
274 if(mActorEffectsEnabled)
276 switch(mCurrentActorAnimation)
281 float animDuration = 1.0f;
282 mActorAnimation = Animation::New(animDuration);
283 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
284 mActorAnimation.SetEndAction( Animation::Bake );
285 mActorAnimation.Play();
292 float animDuration = 1.0f;
293 mActorAnimation = Animation::New(animDuration);
294 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
295 mActorAnimation.SetEndAction( Animation::Bake );
296 mActorAnimation.Play();
300 // spin around y and z
303 float animDuration = 1.0f;
304 mActorAnimation = Animation::New(animDuration);
305 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
306 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
307 mActorAnimation.SetEndAction( Animation::Bake );
308 mActorAnimation.Play();
315 float animDuration = 1.0f;
316 mActorAnimation = Animation::New(animDuration);
317 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
318 mActorAnimation.SetEndAction( Animation::Bake );
319 mActorAnimation.Play();
327 mCurrentActorAnimation++;
328 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
330 mCurrentActorAnimation = 0;
335 void ToggleActorEffects()
337 if(!mActorEffectsEnabled)
339 mActorEffectsEnabled = true;
340 mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON );
341 mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON_SELECTED );
345 mActorEffectsEnabled = false;
346 mActorEffectsButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
347 mActorEffectsButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
351 //////////////////////////////////////////////////////////////
357 bool OnLayoutButtonClicked( Toolkit::Button button )
363 bool OnEffectButtonClicked( Toolkit::Button button )
365 ToggleActorEffects();
370 * Main key event handler
372 void OnKeyEvent(const KeyEvent& event)
374 if(event.state == KeyEvent::Down)
376 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
383 //////////////////////////////////////////////////////////////
393 if(MOTION_STRETCH_NUM_ACTOR_IMAGES == mCurrentImage)
398 mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGES[mCurrentImage];
399 mMotionStretchImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect );
404 Application& mApplication; ///< Application instance
405 Toolkit::Control mView;
406 Toolkit::ToolBar mToolBar;
407 Layer mContentLayer; ///< Content layer (contains actor for this stretch demo)
409 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
412 Property::Map mMotionStretchEffect;
413 ImageView mMotionStretchImageView;
415 // animate actor to position where user taps screen
416 Animation mActorTapMovementAnimation;
418 // show different animations to demonstrate stretch effect working on an object only movement basis
419 bool mActorEffectsEnabled;
420 Animation mActorAnimation;
421 int mCurrentActorAnimation;
423 // offer a selection of images that user can cycle between
426 TapGestureDetector mTapGestureDetector;
428 DeviceOrientation mOrientation; ///< Current Device orientation
429 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
433 int DALI_EXPORT_API main(int argc, char **argv)
435 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
436 MotionStretchExampleApp test(app);