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 Stage::GetCurrent().KeyEventSignal().Connect(this, &MotionStretchExampleApp::OnKeyEvent);
129 // Creates a default view with a default tool bar.
130 // The view is added to the stage.
131 mContentLayer = DemoHelper::CreateView( mApplication,
134 BACKGROUND_IMAGE_PATH,
138 //Add an slideshow icon on the right of the title
139 mIconEffectsOff = ResourceImage::New( EFFECTS_OFF_ICON );
140 mIconEffectsOn = ResourceImage::New( EFFECTS_ON_ICON );
141 mActorEffectsButton = Toolkit::PushButton::New();
142 mActorEffectsButton.SetBackgroundImage( mIconEffectsOff );
143 mActorEffectsButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnEffectButtonClicked );
144 mToolBar.AddControl( mActorEffectsButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalCenter, DemoHelper::DEFAULT_PLAY_PADDING );
146 // Creates a mode button.
147 // Create a effect toggle button. (right of toolbar)
148 Image imageLayout = ResourceImage::New( LAYOUT_IMAGE );
149 Toolkit::PushButton layoutButton = Toolkit::PushButton::New();
150 layoutButton.SetBackgroundImage(imageLayout);
151 layoutButton.ClickedSignal().Connect( this, &MotionStretchExampleApp::OnLayoutButtonClicked);
152 layoutButton.SetLeaveRequired( true );
153 mToolBar.AddControl( layoutButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
156 mTapGestureDetector = TapGestureDetector::New();
157 mTapGestureDetector.Attach( mContentLayer );
158 mTapGestureDetector.DetectedSignal().Connect( this, &MotionStretchExampleApp::OnTap );
160 // set initial orientation
161 Dali::Window winHandle = app.GetWindow();
162 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
163 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
164 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
165 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
167 app.GetOrientation().ChangedSignal().Connect( this, &MotionStretchExampleApp::OnOrientationChanged );
168 unsigned int degrees = app.GetOrientation().GetDegrees();
169 Rotate( static_cast< DeviceOrientation >( degrees ) );
172 ///////////////////////////////////////////////////////
174 // Motion stretched actor
177 Image image = ResourceImage::New( MOTION_STRETCH_ACTOR_IMAGE1 );
178 mMotionStretchImageActor = ImageActor::New(image);
179 mMotionStretchImageActor.SetParentOrigin( ParentOrigin::CENTER );
180 mMotionStretchImageActor.SetAnchorPoint( AnchorPoint::CENTER );
181 mMotionStretchImageActor.SetSize(MOTION_STRETCH_ACTOR_WIDTH, MOTION_STRETCH_ACTOR_HEIGHT);
183 mContentLayer.Add( mMotionStretchImageActor );
185 // Create shader used for doing motion stretch
186 mMotionStretchEffect = MotionStretchEffect::Apply(mMotionStretchImageActor);
189 //////////////////////////////////////////////////////////////
191 // Device Orientation Support
195 void OnOrientationChanged( Orientation orientation )
197 unsigned int degrees = orientation.GetDegrees();
198 Rotate( static_cast< DeviceOrientation >( degrees ) );
201 void Rotate( DeviceOrientation orientation )
203 // Resize the root actor
204 Vector2 stageSize = Stage::GetCurrent().GetSize();
205 Vector2 targetSize = stageSize;
206 if( orientation == LANDSCAPE ||
207 orientation == LANDSCAPE_INVERSE )
209 targetSize = Vector2( stageSize.y, stageSize.x );
212 if( mOrientation != orientation )
214 mOrientation = orientation;
216 // check if actor is on stage
217 if( mView.GetParent() )
219 // has parent so we expect it to be on stage, start animation
220 mRotateAnimation = Animation::New( ORIENTATION_DURATION );
221 mRotateAnimation.RotateTo( mView, Degree( -orientation ), Vector3::ZAXIS, AlphaFunctions::EaseOut );
222 mRotateAnimation.Resize( mView, targetSize.width, targetSize.height );
223 mRotateAnimation.Play();
227 // set the rotation to match the orientation
228 mView.SetRotation( 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 ( mMotionStretchImageActor )
264 mActorTapMovementAnimation.AnimateTo( Property(mMotionStretchImageActor, Actor::Property::Position), destPos, AlphaFunctions::EaseInOutSine, 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.RotateBy(mMotionStretchImageActor, Degree(720), Vector3::YAXIS, AlphaFunctions::EaseInOut);
281 mActorAnimation.SetEndAction( Animation::Bake );
282 mActorAnimation.Play();
289 float animDuration = 1.0f;
290 mActorAnimation = Animation::New(animDuration);
291 mActorAnimation.RotateBy(mMotionStretchImageActor, Degree(720), Vector3::ZAXIS, AlphaFunctions::EaseInOut);
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.RotateBy(mMotionStretchImageActor, Degree(360), Vector3::YAXIS, AlphaFunctions::EaseInOut);
303 mActorAnimation.RotateBy(mMotionStretchImageActor, Degree(360), Vector3::ZAXIS, AlphaFunctions::EaseInOut);
304 mActorAnimation.SetEndAction( Animation::Bake );
305 mActorAnimation.Play();
312 float animDuration = 1.0f;
313 mActorAnimation = Animation::New(animDuration);
314 mActorAnimation.ScaleBy(mMotionStretchImageActor, Vector3(2.0f, 2.0f, 2.0f), AlphaFunctions::Bounce, 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.SetBackgroundImage( mIconEffectsOn );
341 mActorEffectsEnabled = false;
342 mActorEffectsButton.SetBackgroundImage( mIconEffectsOff );
346 //////////////////////////////////////////////////////////////
352 bool OnLayoutButtonClicked( Toolkit::Button button )
358 bool OnEffectButtonClicked( Toolkit::Button button )
360 ToggleActorEffects();
365 * Main key event handler
367 void OnKeyEvent(const KeyEvent& event)
369 if(event.state == KeyEvent::Down)
371 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
378 //////////////////////////////////////////////////////////////
388 if(MOTION_STRETCH_NUM_ACTOR_IMAGES == mCurrentImage)
393 Image stretchImage = ResourceImage::New( MOTION_STRETCH_ACTOR_IMAGES[mCurrentImage] );
394 mMotionStretchImageActor.SetImage(stretchImage);
399 Application& mApplication; ///< Application instance
401 Toolkit::ToolBar mToolBar;
402 TextView mTitleActor; ///< The Toolbar's Title.
403 Image mIconEffectsOff;
404 Image mIconEffectsOn;
405 Layer mContentLayer; ///< Content layer (contains actor for this stretch demo)
407 PushButton mActorEffectsButton; ///< The actor effects toggling Button.
410 MotionStretchEffect mMotionStretchEffect;
411 ImageActor mMotionStretchImageActor;
413 // animate actor to position where user taps screen
414 Animation mActorTapMovementAnimation;
416 // show different animations to demonstrate stretch effect working on an object only movement basis
417 bool mActorEffectsEnabled;
418 Animation mActorAnimation;
419 int mCurrentActorAnimation;
421 // offer a selection of images that user can cycle between
424 TapGestureDetector mTapGestureDetector;
426 DeviceOrientation mOrientation; ///< Current Device orientation
427 Animation mRotateAnimation; ///< Animation for rotating between landscape and portrait.
431 void RunTest(Application& app)
433 MotionStretchExampleApp test(app);
438 // Entry point for Linux & SLP applications
440 int main(int argc, char **argv)
442 Application app = Application::New(&argc, &argv);