2 * Copyright (c) 2016 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/shader-effects/motion-stretch-effect.h>
27 using namespace Dali::Toolkit;
31 namespace // unnamed namespace
34 ////////////////////////////////////////////////////
36 // Demo setup parameters
39 const float MOTION_STRETCH_ACTOR_WIDTH = 256; // actor size on screen
40 const float MOTION_STRETCH_ACTOR_HEIGHT = 256; // ""
42 const int MOTION_STRETCH_NUM_ACTOR_IMAGES = 5;
43 const char* MOTION_STRETCH_ACTOR_IMAGE1( DEMO_IMAGE_DIR "image-with-border-1.jpg" );
44 const char* MOTION_STRETCH_ACTOR_IMAGE2( DEMO_IMAGE_DIR "image-with-border-2.jpg" );
45 const char* MOTION_STRETCH_ACTOR_IMAGE3( DEMO_IMAGE_DIR "image-with-border-3.jpg" );
46 const char* MOTION_STRETCH_ACTOR_IMAGE4( DEMO_IMAGE_DIR "image-with-border-4.jpg" );
47 const char* MOTION_STRETCH_ACTOR_IMAGE5( DEMO_IMAGE_DIR "image-with-border-5.jpg" );
49 const char* MOTION_STRETCH_ACTOR_IMAGES[] = {
50 MOTION_STRETCH_ACTOR_IMAGE1,
51 MOTION_STRETCH_ACTOR_IMAGE2,
52 MOTION_STRETCH_ACTOR_IMAGE3,
53 MOTION_STRETCH_ACTOR_IMAGE4,
54 MOTION_STRETCH_ACTOR_IMAGE5,
57 const int NUM_ACTOR_ANIMATIONS = 4;
58 const int NUM_CAMERA_ANIMATIONS = 2;
61 const char* BACKGROUND_IMAGE_PATH = DEMO_IMAGE_DIR "background-default.png";
63 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
64 const char* LAYOUT_IMAGE( DEMO_IMAGE_DIR "icon-change.png" );
65 const char* LAYOUT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
66 const char* APPLICATION_TITLE( "Motion Stretch" );
67 const char* EFFECTS_OFF_ICON( DEMO_IMAGE_DIR "icon-effects-off.png" );
68 const char* EFFECTS_OFF_ICON_SELECTED( DEMO_IMAGE_DIR "icon-effects-off-selected.png" );
69 const char* EFFECTS_ON_ICON( DEMO_IMAGE_DIR "icon-effects-on.png" );
70 const char* EFFECTS_ON_ICON_SELECTED( DEMO_IMAGE_DIR "icon-effects-on-selected.png" );
72 // These values depend on the button background image
73 const Vector4 BUTTON_IMAGE_BORDER( Vector4::ONE * 3.0f );
75 const float UI_MARGIN = 4.0f; ///< Screen Margin for placement of UI buttons
77 const Vector3 BUTTON_SIZE_CONSTRAINT( 0.24f, 0.09f, 1.0f );
79 // move this button down a bit so it is visible on target and not covered up by toolbar
80 const float BUTTON_TITLE_LABEL_Y_OFFSET = 0.05f;
82 const float ORIENTATION_DURATION = 0.5f; ///< Time to rotate to new orientation.
83 } // unnamed namespace
89 class MotionStretchExampleApp : public ConnectionTracker
94 * DeviceOrientation describes the four different
95 * orientations the device can be in based on accelerometer reports.
97 enum DeviceOrientation
101 PORTRAIT_INVERSE = 180,
102 LANDSCAPE_INVERSE = 270
107 * @param application class, stored as reference
109 MotionStretchExampleApp(Application &app)
111 mActorEffectsEnabled(false),
112 mCurrentActorAnimation(0),
114 mOrientation( PORTRAIT )
116 // Connect to the Application's Init signal
117 app.InitSignal().Connect(this, &MotionStretchExampleApp::OnInit);
120 ~MotionStretchExampleApp()
122 // Nothing to do here; everything gets deleted automatically
126 * This method gets called once the main loop of application is up and running
128 void OnInit(Application& app)
130 // The Init signal is received once (only) during the Application lifetime
132 Stage::GetCurrent().KeyEventSignal().Connect(this, &MotionStretchExampleApp::OnKeyEvent);
134 // Creates a default view with a default tool bar.
135 // The view is added to the stage.
136 mContentLayer = DemoHelper::CreateView( mApplication,
139 BACKGROUND_IMAGE_PATH,
143 //Add an slideshow icon on the right of the title
144 mActorEffectsButton = Toolkit::PushButton::New();
145 mActorEffectsButton.SetUnselectedImage( EFFECTS_OFF_ICON );
146 mActorEffectsButton.SetSelectedImage( EFFECTS_OFF_ICON_SELECTED );
147 mActorEffectsButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnEffectButtonClicked );
148 mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
150 // Creates a mode button.
151 // Create a effect toggle button. (right of toolbar)
152 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
153 layoutButton.SetUnselectedImage( LAYOUT_IMAGE );
154 layoutButton.SetSelectedImage( LAYOUT_IMAGE_SELECTED );
155 layoutButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnLayoutButtonClicked);
156 layoutButton.SetLeaveRequired( true );
157 mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
160 mTapGestureDetector = TapGestureDetector::New();
161 mTapGestureDetector.Attach( mContentLayer );
162 mTapGestureDetector.DetectedSignal().Connect( this, &MotionStretchExampleApp::OnTap );
164 // set initial orientation
165 Dali::Window winHandle = app.GetWindow();
166 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
167 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
168 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
169 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
171 unsigned int degrees = 0;
172 Rotate( static_cast< DeviceOrientation >( degrees ) );
175 ///////////////////////////////////////////////////////
177 // Motion stretched actor
179 mMotionStretchEffect = Toolkit::CreateMotionStretchEffect();
180 mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGE1;
181 mMotionStretchImageView = ImageView::New();
182 mMotionStretchImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect );
183 mMotionStretchImageView.SetParentOrigin( ParentOrigin::CENTER );
184 mMotionStretchImageView.SetAnchorPoint( AnchorPoint::CENTER );
185 mMotionStretchImageView.SetSize( MOTION_STRETCH_ACTOR_WIDTH, MOTION_STRETCH_ACTOR_HEIGHT );
187 mContentLayer.Add( mMotionStretchImageView );
189 // Create shader used for doing motion stretch
190 Toolkit::SetMotionStretchProperties( mMotionStretchImageView );
193 //////////////////////////////////////////////////////////////
195 // Device Orientation Support
199 void Rotate( DeviceOrientation orientation )
201 // Resize the root actor
202 Vector2 stageSize = Stage::GetCurrent().GetSize();
203 Vector2 targetSize = stageSize;
204 if( orientation == LANDSCAPE ||
205 orientation == LANDSCAPE_INVERSE )
207 targetSize = Vector2( stageSize.y, stageSize.x );
210 if( mOrientation != orientation )
212 mOrientation = orientation;
214 // check if actor is on stage
215 if( mView.GetParent() )
217 // has parent so we expect it to be on stage, start animation
218 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
219 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -orientation ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
220 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
221 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
222 mRotateAnimation.Play();
226 // set the rotation to match the orientation
227 mView.SetOrientation( Degree( -orientation ), Vector3::ZAXIS );
228 mView.SetSize( targetSize );
233 // for first time just set size
234 mView.SetSize( targetSize );
238 //////////////////////////////////////////////////////////////
244 // move to point on screen that was tapped
245 void OnTap( Actor actor, const TapGesture& tapGesture )
248 float originOffsetX, originOffsetY;
250 // rotate offset (from top left origin to centre) into actor space
251 Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
252 actor.ScreenToLocal(originOffsetX, originOffsetY, stageSize.width * 0.5f, stageSize.height * 0.5f);
254 // get dest point in local actor space
255 destPos.x = tapGesture.localPoint.x - originOffsetX;
256 destPos.y = tapGesture.localPoint.y - originOffsetY;
259 float animDuration = 0.5f;
260 mActorTapMovementAnimation = Animation::New( animDuration );
261 if ( mMotionStretchImageView )
263 mActorTapMovementAnimation.AnimateTo( Property(mMotionStretchImageView, Actor::Property::POSITION), destPos, AlphaFunction::EASE_IN_OUT_SINE, TimePeriod(animDuration) );
265 mActorTapMovementAnimation.SetEndAction( Animation::Bake );
266 mActorTapMovementAnimation.Play();
269 // perform some spinning etc
270 if(mActorEffectsEnabled)
272 switch(mCurrentActorAnimation)
277 float animDuration = 1.0f;
278 mActorAnimation = Animation::New(animDuration);
279 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
280 mActorAnimation.SetEndAction( Animation::Bake );
281 mActorAnimation.Play();
288 float animDuration = 1.0f;
289 mActorAnimation = Animation::New(animDuration);
290 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), AlphaFunction::EASE_IN_OUT );
291 mActorAnimation.SetEndAction( Animation::Bake );
292 mActorAnimation.Play();
296 // spin around y and z
299 float animDuration = 1.0f;
300 mActorAnimation = Animation::New(animDuration);
301 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
302 mActorAnimation.AnimateBy( Property( mMotionStretchImageView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::ZAXIS ), 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( mMotionStretchImageView, Actor::Property::SCALE ), Vector3(2.0f, 2.0f, 2.0f), AlphaFunction::BOUNCE, TimePeriod( 0.0f, 1.0f ) );
314 mActorAnimation.SetEndAction( Animation::Bake );
315 mActorAnimation.Play();
323 mCurrentActorAnimation++;
324 if(NUM_ACTOR_ANIMATIONS == mCurrentActorAnimation)
326 mCurrentActorAnimation = 0;
331 void ToggleActorEffects()
333 if(!mActorEffectsEnabled)
335 mActorEffectsEnabled = true;
336 mActorEffectsButton.SetUnselectedImage( EFFECTS_ON_ICON );
337 mActorEffectsButton.SetSelectedImage( EFFECTS_ON_ICON_SELECTED );
341 mActorEffectsEnabled = false;
342 mActorEffectsButton.SetUnselectedImage( EFFECTS_OFF_ICON );
343 mActorEffectsButton.SetSelectedImage( EFFECTS_OFF_ICON_SELECTED );
347 //////////////////////////////////////////////////////////////
353 bool OnLayoutButtonClicked( Toolkit::Button button )
359 bool OnEffectButtonClicked( Toolkit::Button button )
361 ToggleActorEffects();
366 * Main key event handler
368 void OnKeyEvent(const KeyEvent& event)
370 if(event.state == KeyEvent::Down)
372 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
379 //////////////////////////////////////////////////////////////
389 if(MOTION_STRETCH_NUM_ACTOR_IMAGES == mCurrentImage)
394 mMotionStretchEffect["url"] = MOTION_STRETCH_ACTOR_IMAGES[mCurrentImage];
395 mMotionStretchImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, mMotionStretchEffect );
400 Application& mApplication; ///< Application instance
401 Toolkit::Control mView;
402 Toolkit::ToolBar mToolBar;
403 Layer mContentLayer; ///< Content layer (contains actor for this stretch demo)
405 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
408 Property::Map mMotionStretchEffect;
409 ImageView mMotionStretchImageView;
411 // animate actor to position where user taps screen
412 Animation mActorTapMovementAnimation;
414 // show different animations to demonstrate stretch effect working on an object only movement basis
415 bool mActorEffectsEnabled;
416 Animation mActorAnimation;
417 int mCurrentActorAnimation;
419 // offer a selection of images that user can cycle between
422 TapGestureDetector mTapGestureDetector;
424 DeviceOrientation mOrientation; ///< Current Device orientation
425 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
429 void RunTest(Application& app)
431 MotionStretchExampleApp test(app);
436 // Entry point for Linux & Tizen applications
438 int DALI_EXPORT_API main(int argc, char **argv)
440 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);