2 * Copyright (c) 2017 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/controls/buttons/button-devel.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 //Add an slideshow icon on the right of the title
145 mActorEffectsButton = Toolkit::PushButton::New();
146 mActorEffectsButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
147 mActorEffectsButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
148 mActorEffectsButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnEffectButtonClicked );
149 mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
151 // Creates a mode button.
152 // Create a effect toggle button. (right of toolbar)
153 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
154 layoutButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE );
155 layoutButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, LAYOUT_IMAGE_SELECTED );
156 layoutButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnLayoutButtonClicked);
157 layoutButton.SetLeaveRequired( true );
158 mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
161 mTapGestureDetector = TapGestureDetector::New();
162 mTapGestureDetector.Attach( mContentLayer );
163 mTapGestureDetector.DetectedSignal().Connect( this, &MotionStretchExampleApp::OnTap );
165 // set initial orientation
166 Dali::Window winHandle = app.GetWindow();
167 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
168 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
169 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
170 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
172 unsigned int degrees = 0;
173 Rotate( static_cast< DeviceOrientation >( degrees ) );
176 ///////////////////////////////////////////////////////
178 // Motion stretched actor
180 mMotionStretchEffect = Toolkit::CreateMotionStretchEffect();
181 mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGE1;
182 mMotionStretchImageView = ImageView::New();
183 mMotionStretchImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect );
184 mMotionStretchImageView.SetParentOrigin( ParentOrigin::CENTER );
185 mMotionStretchImageView.SetAnchorPoint( AnchorPoint::CENTER );
186 mMotionStretchImageView.SetSize( MOTION_STRETCH_ACTOR_WIDTH, MOTION_STRETCH_ACTOR_HEIGHT );
188 mContentLayer.Add( mMotionStretchImageView );
190 // Create shader used for doing motion stretch
191 Toolkit::SetMotionStretchProperties( mMotionStretchImageView );
194 //////////////////////////////////////////////////////////////
196 // Device Orientation Support
200 void Rotate( DeviceOrientation orientation )
202 // Resize the root actor
203 Vector2 stageSize = Stage::GetCurrent().GetSize();
204 Vector2 targetSize = stageSize;
205 if( orientation == LANDSCAPE ||
206 orientation == LANDSCAPE_INVERSE )
208 targetSize = Vector2( stageSize.y, stageSize.x );
211 if( mOrientation != orientation )
213 mOrientation = orientation;
215 // check if actor is on stage
216 if( mView.GetParent() )
218 // has parent so we expect it to be on stage, start animation
219 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
220 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -orientation ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
221 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
222 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
223 mRotateAnimation.Play();
227 // set the rotation to match the orientation
228 mView.SetOrientation( Degree( -orientation ), Vector3::ZAXIS );
229 mView.SetSize( targetSize );
234 // for first time just set size
235 mView.SetSize( targetSize );
239 //////////////////////////////////////////////////////////////
245 // move to point on screen that was tapped
246 void OnTap( Actor actor, const TapGesture& tapGesture )
249 float originOffsetX, originOffsetY;
251 // rotate offset (from top left origin to centre) into actor space
252 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
253 actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
255 // get dest point in local actor space
256 destPos.x = tapGesture.localPoint.x - originOffsetX;
257 destPos.y = tapGesture.localPoint.y - originOffsetY;
260 float animDuration = 0.5f;
261 mActorTapMovementAnimation = Animation::New( animDuration );
262 if ( mMotionStretchImageView )
264 mActorTapMovementAnimation.AnimateTo( Property(mMotionStretchImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
266 mActorTapMovementAnimation.SetEndAction( Animation::Bake );
267 mActorTapMovementAnimation.Play();
270 // perform some spinning etc
271 if(mActorEffectsEnabled)
273 switch(mCurrentActorAnimation)
278 float animDuration = 1.0f;
279 mActorAnimation = Animation::New(animDuration);
280 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
281 mActorAnimation.SetEndAction( Animation::Bake );
282 mActorAnimation.Play();
289 float animDuration = 1.0f;
290 mActorAnimation = Animation::New(animDuration);
291 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
292 mActorAnimation.SetEndAction( Animation::Bake );
293 mActorAnimation.Play();
297 // spin around y and z
300 float animDuration = 1.0f;
301 mActorAnimation = Animation::New(animDuration);
302 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
303 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
304 mActorAnimation.SetEndAction( Animation::Bake );
305 mActorAnimation.Play();
312 float animDuration = 1.0f;
313 mActorAnimation = Animation::New(animDuration);
314 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
315 mActorAnimation.SetEndAction( Animation::Bake );
316 mActorAnimation.Play();
324 mCurrentActorAnimation++;
325 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
327 mCurrentActorAnimation = 0;
332 void ToggleActorEffects()
334 if(!mActorEffectsEnabled)
336 mActorEffectsEnabled = true;
337 mActorEffectsButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON );
338 mActorEffectsButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_ON_ICON_SELECTED );
342 mActorEffectsEnabled = false;
343 mActorEffectsButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON );
344 mActorEffectsButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, EFFECTS_OFF_ICON_SELECTED );
348 //////////////////////////////////////////////////////////////
354 bool OnLayoutButtonClicked( Toolkit::Button button )
360 bool OnEffectButtonClicked( Toolkit::Button button )
362 ToggleActorEffects();
367 * Main key event handler
369 void OnKeyEvent(const KeyEvent& event)
371 if(event.state == KeyEvent::Down)
373 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
380 //////////////////////////////////////////////////////////////
390 if(MOTION_STRETCH_NUM_ACTOR_IMAGES == mCurrentImage)
395 mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGES[mCurrentImage];
396 mMotionStretchImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect );
401 Application& mApplication; ///< Application instance
402 Toolkit::Control mView;
403 Toolkit::ToolBar mToolBar;
404 Layer mContentLayer; ///< Content layer (contains actor for this stretch demo)
406 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
409 Property::Map mMotionStretchEffect;
410 ImageView mMotionStretchImageView;
412 // animate actor to position where user taps screen
413 Animation mActorTapMovementAnimation;
415 // show different animations to demonstrate stretch effect working on an object only movement basis
416 bool mActorEffectsEnabled;
417 Animation mActorAnimation;
418 int mCurrentActorAnimation;
420 // offer a selection of images that user can cycle between
423 TapGestureDetector mTapGestureDetector;
425 DeviceOrientation mOrientation; ///< Current Device orientation
426 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
430 int DALI_EXPORT_API main(int argc, char **argv)
432 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
433 MotionStretchExampleApp test(app);