2 * Copyright (c) 2014 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>
26 using namespace Dali::Toolkit;
30 namespace // unnamed namespace
33 ////////////////////////////////////////////////////
35 // Demo setup parameters
38 const float MOTION_STRETCH_ACTOR_WIDTH = 256; // actor size on screen
39 const float MOTION_STRETCH_ACTOR_HEIGHT = 256; // ""
41 const int MOTION_STRETCH_NUM_ACTOR_IMAGES = 5;
42 const char* MOTION_STRETCH_ACTOR_IMAGE1( DALI_IMAGE_DIR "image-with-border-1.jpg" );
43 const char* MOTION_STRETCH_ACTOR_IMAGE2( DALI_IMAGE_DIR "image-with-border-2.jpg" );
44 const char* MOTION_STRETCH_ACTOR_IMAGE3( DALI_IMAGE_DIR "image-with-border-3.jpg" );
45 const char* MOTION_STRETCH_ACTOR_IMAGE4( DALI_IMAGE_DIR "image-with-border-4.jpg" );
46 const char* MOTION_STRETCH_ACTOR_IMAGE5( DALI_IMAGE_DIR "image-with-border-5.jpg" );
48 const char* MOTION_STRETCH_ACTOR_IMAGES[] = {
49 MOTION_STRETCH_ACTOR_IMAGE1,
50 MOTION_STRETCH_ACTOR_IMAGE2,
51 MOTION_STRETCH_ACTOR_IMAGE3,
52 MOTION_STRETCH_ACTOR_IMAGE4,
53 MOTION_STRETCH_ACTOR_IMAGE5,
56 const int NUM_ACTOR_ANIMATIONS = 4;
57 const int NUM_CAMERA_ANIMATIONS = 2;
60 const char* BACKGROUND_IMAGE_PATH = DALI_IMAGE_DIR "background-default.png";
62 const char* TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
63 const char* LAYOUT_IMAGE( DALI_IMAGE_DIR "icon-change.png" );
64 const char* APPLICATION_TITLE( "Motion Stretch" );
65 const char* EFFECTS_OFF_ICON( DALI_IMAGE_DIR "icon-effects-off.png" );
66 const char* EFFECTS_ON_ICON( DALI_IMAGE_DIR "icon-effects-on.png" );
68 // These values depend on the button background image
69 const Vector4 BUTTON_IMAGE_BORDER( Vector4::ONE * 3.0f );
71 const float UI_MARGIN = 4.0f; ///< Screen Margin for placement of UI buttons
73 const Vector3 BUTTON_SIZE_CONSTRAINT( 0.24f, 0.09f, 1.0f );
75 // move this button down a bit so it is visible on target and not covered up by toolbar
76 const float BUTTON_TITLE_LABEL_Y_OFFSET = 0.05f;
78 const float ORIENTATION_DURATION = 0.5f; ///< Time to rotate to new orientation.
79 } // unnamed namespace
85 class MotionStretchExampleApp : public ConnectionTracker
90 * DeviceOrientation describes the four different
91 * orientations the device can be in based on accelerometer reports.
93 enum DeviceOrientation
97 PORTRAIT_INVERSE = 180,
98 LANDSCAPE_INVERSE = 270
103 * @param application class, stored as reference
105 MotionStretchExampleApp(Application &app)
107 mActorEffectsEnabled(false),
108 mCurrentActorAnimation(0),
111 // Connect to the Application's Init signal
112 app.InitSignal().Connect(this, &MotionStretchExampleApp::OnInit);
115 ~MotionStretchExampleApp()
117 // Nothing to do here; everything gets deleted automatically
121 * This method gets called once the main loop of application is up and running
123 void OnInit(Application& app)
125 // The Init signal is received once (only) during the Application lifetime
127 DemoHelper::RequestThemeChange();
129 Stage::GetCurrent().KeyEventSignal().Connect(this, &MotionStretchExampleApp::OnKeyEvent);
131 // Creates a default view with a default tool bar.
132 // The view is added to the stage.
133 mContentLayer = DemoHelper::CreateView( mApplication,
136 BACKGROUND_IMAGE_PATH,
140 //Add an slideshow icon on the right of the title
141 mIconEffectsOff = ResourceImage::New( EFFECTS_OFF_ICON );
142 mIconEffectsOn = ResourceImage::New( EFFECTS_ON_ICON );
143 mActorEffectsButton = Toolkit::PushButton::New();
144 mActorEffectsButton.SetBackgroundImage( mIconEffectsOff );
145 mActorEffectsButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnEffectButtonClicked );
146 mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
148 // Creates a mode button.
149 // Create a effect toggle button. (right of toolbar)
150 Image imageLayout = ResourceImage::New( LAYOUT_IMAGE );
151 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
152 layoutButton.SetBackgroundImage(imageLayout);
153 layoutButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnLayoutButtonClicked);
154 layoutButton.SetLeaveRequired( true );
155 mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
158 mTapGestureDetector = TapGestureDetector::New();
159 mTapGestureDetector.Attach( mContentLayer );
160 mTapGestureDetector.DetectedSignal().Connect( this, &MotionStretchExampleApp::OnTap );
162 // set initial orientation
163 Dali::Window winHandle = app.GetWindow();
164 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
165 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
166 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
167 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
169 winHandle.GetOrientation().ChangedSignal().Connect( this, &MotionStretchExampleApp::OnOrientationChanged );
170 unsigned int degrees = winHandle.GetOrientation().GetDegrees();
171 Rotate( static_cast< DeviceOrientation >( degrees ) );
174 ///////////////////////////////////////////////////////
176 // Motion stretched actor
179 Image image = ResourceImage::New( MOTION_STRETCH_ACTOR_IMAGE1 );
180 mMotionStretchImageActor = ImageActor::New(image);
181 mMotionStretchImageActor.SetParentOrigin( ParentOrigin::CENTER );
182 mMotionStretchImageActor.SetAnchorPoint( AnchorPoint::CENTER );
183 mMotionStretchImageActor.SetSize(MOTION_STRETCH_ACTOR_WIDTH, MOTION_STRETCH_ACTOR_HEIGHT);
185 mContentLayer.Add( mMotionStretchImageActor );
187 // Create shader used for doing motion stretch
188 mMotionStretchEffect = MotionStretchEffect::Apply(mMotionStretchImageActor);
191 //////////////////////////////////////////////////////////////
193 // Device Orientation Support
197 void OnOrientationChanged( Orientation orientation )
199 unsigned int degrees = orientation.GetDegrees();
200 Rotate( static_cast< DeviceOrientation >( degrees ) );
203 void Rotate( DeviceOrientation orientation )
205 // Resize the root actor
206 Vector2 stageSize = Stage::GetCurrent().GetSize();
207 Vector2 targetSize = stageSize;
208 if( orientation == LANDSCAPE ||
209 orientation == LANDSCAPE_INVERSE )
211 targetSize = Vector2( stageSize.y, stageSize.x );
214 if( mOrientation != orientation )
216 mOrientation = orientation;
218 // check if actor is on stage
219 if( mView.GetParent() )
221 // has parent so we expect it to be on stage, start animation
222 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
223 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree( -orientation ) ), Vector3::ZAXIS ), AlphaFunction::EASE_OUT );
224 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_WIDTH ), targetSize.width );
225 mRotateAnimation.AnimateTo( Property( mView, Actor::Property::SIZE_HEIGHT ), targetSize.height );
226 mRotateAnimation.Play();
230 // set the rotation to match the orientation
231 mView.SetOrientation( Degree( -orientation ), Vector3::ZAXIS );
232 mView.SetSize( targetSize );
237 // for first time just set size
238 mView.SetSize( 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 ( mMotionStretchImageActor )
267 mActorTapMovementAnimation.AnimateTo( Property(mMotionStretchImageActor, 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( mMotionStretchImageActor, 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( mMotionStretchImageActor, 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( mMotionStretchImageActor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f) ), Vector3::YAXIS ), AlphaFunction::EASE_IN_OUT );
306 mActorAnimation.AnimateBy( Property( mMotionStretchImageActor, 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( mMotionStretchImageActor, 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.SetBackgroundImage( mIconEffectsOn );
344 mActorEffectsEnabled = false;
345 mActorEffectsButton.SetBackgroundImage( mIconEffectsOff );
349 //////////////////////////////////////////////////////////////
355 bool OnLayoutButtonClicked( Toolkit::Button button )
361 bool OnEffectButtonClicked( Toolkit::Button button )
363 ToggleActorEffects();
368 * Main key event handler
370 void OnKeyEvent(const KeyEvent& event)
372 if(event.state == KeyEvent::Down)
374 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
381 //////////////////////////////////////////////////////////////
391 if(MOTION_STRETCH_NUM_ACTOR_IMAGES == mCurrentImage)
396 Image stretchImage = ResourceImage::New( MOTION_STRETCH_ACTOR_IMAGES[mCurrentImage] );
397 mMotionStretchImageActor.SetImage(stretchImage);
402 Application& mApplication; ///< Application instance
403 Toolkit::Control mView;
404 Toolkit::ToolBar mToolBar;
405 Image mIconEffectsOff;
406 Image mIconEffectsOn;
407 Layer mContentLayer; ///< Content layer (contains actor for this stretch demo)
409 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
412 MotionStretchEffect mMotionStretchEffect;
413 ImageActor mMotionStretchImageActor;
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 void RunTest(Application& app)
435 MotionStretchExampleApp test(app);
440 // Entry point for Linux & Tizen applications
442 int main(int argc, char **argv)
444 Application app = Application::New(&argc, &argv);