Use Window instead of Stage
[platform/core/uifw/dali-demo.git] / examples / video-view / video-view-example.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20
21 using namespace Dali;
22 using namespace Toolkit;
23
24 namespace
25 {
26   const int SEEK_POS( 5000 );
27   const int INIT_WIDTH( 600 );
28   const int INIT_HEIGHT( 400 );
29   const int BUTTON_SIZE( 80 );
30
31   const char* const PLAY_FILE = DEMO_VIDEO_DIR "demoVideo.mp4";
32   const char* const PLAY_IMAGE = DEMO_IMAGE_DIR "icon-play.png";
33   const char* const PAUSE_IMAGE = DEMO_IMAGE_DIR "Pause.png";
34   const char* const CHANGE_IMAGE = DEMO_IMAGE_DIR "icon-change.png";
35   const char* const FORWARD_IMAGE = DEMO_IMAGE_DIR "Forward.png";
36   const char* const BACKWARD_IMAGE = DEMO_IMAGE_DIR "Backward.png";
37
38 }  // namespace
39
40 class VideoViewController: public ConnectionTracker
41 {
42  public:
43
44   VideoViewController( Application& application )
45     : mApplication( application ),
46       mIsPlay( false ),
47       mIsFullScreen( false ),
48       mScale( 1.f ),
49       mPinchStartScale( 1.0f )
50   {
51     // Connect to the Application's Init signal
52     mApplication.InitSignal().Connect( this, &VideoViewController::Create );
53   }
54
55   ~VideoViewController()
56   {
57     mVideoView.Stop();
58   }
59
60   void Create( Application& application )
61   {
62     Window window = application.GetWindow();
63     mWindowSize = window.GetSize();
64
65     mVideoView = Toolkit::VideoView::New();
66     window.Add( mVideoView );
67     mVideoView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
68     mVideoView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
69     mVideoView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
70     mVideoView.SetProperty( Actor::Property::SIZE, Vector2( INIT_WIDTH, INIT_HEIGHT ) );
71     mVideoView.SetProperty( VideoView::Property::LOOPING, true );
72     mVideoView.SetProperty( VideoView::Property::MUTED, false );
73     mVideoView.SetProperty( VideoView::Property::VIDEO, PLAY_FILE );
74
75     mMenu = Layer::New();
76     mMenu.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_LEFT );
77     mMenu.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_LEFT );
78     mMenu.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
79     mMenu.SetProperty( Actor::Property::SIZE, Vector2( INIT_WIDTH, 120 ) );
80     mVideoView.Add( mMenu );
81
82     mPlayButton = PushButton::New();
83     mPlayButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
84     mPlayButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
85     mPlayButton.SetProperty( Dali::Actor::Property::NAME, "Play" );
86     mPlayButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
87     mPlayButton.SetProperty( Actor::Property::SIZE, Vector2( BUTTON_SIZE, BUTTON_SIZE ) );
88     mPlayButton.SetProperty( Actor::Property::POSITION, Vector2( 40, 10 ));
89     mPlayButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
90
91     mPauseButton = PushButton::New();
92     mPauseButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
93     mPauseButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
94     mPauseButton.SetProperty( Dali::Actor::Property::NAME, "Pause" );
95     mPauseButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
96     mPauseButton.SetProperty( Actor::Property::SIZE, Vector2( BUTTON_SIZE, BUTTON_SIZE ) );
97     mPauseButton.SetProperty( Actor::Property::POSITION, Vector2( 40, 10 ));
98     mPauseButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
99
100     mChangeButton = PushButton::New();
101     mChangeButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
102     mChangeButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
103     mChangeButton.SetProperty( Dali::Actor::Property::NAME, "Change" );
104     mChangeButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
105     mChangeButton.SetProperty( Actor::Property::SIZE, Vector2( BUTTON_SIZE, BUTTON_SIZE ) );
106     mChangeButton.SetProperty( Actor::Property::POSITION, Vector2( 140, 10 ));
107     mChangeButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
108
109     mBackwardButton = PushButton::New();
110     mBackwardButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
111     mBackwardButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
112     mBackwardButton.SetProperty( Dali::Actor::Property::NAME, "Backward" );
113     mBackwardButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
114     mBackwardButton.SetProperty( Actor::Property::SIZE, Vector2( BUTTON_SIZE, BUTTON_SIZE ) );
115     mBackwardButton.SetProperty( Actor::Property::POSITION, Vector2( 240, 10 ));
116     mBackwardButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
117
118     mForwardButton = PushButton::New();
119     mForwardButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
120     mForwardButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
121     mForwardButton.SetProperty( Dali::Actor::Property::NAME, "Forward" );
122     mForwardButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
123     mForwardButton.SetProperty( Actor::Property::SIZE, Vector2( BUTTON_SIZE, BUTTON_SIZE ) );
124     mForwardButton.SetProperty( Actor::Property::POSITION, Vector2( 340, 10 ));
125     mForwardButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
126
127     mMenu.Add( mPlayButton );
128     mMenu.Add( mPauseButton );
129     mMenu.Add( mChangeButton );
130     mMenu.Add( mBackwardButton );
131     mMenu.Add( mForwardButton );
132
133     mPauseButton.SetProperty( Actor::Property::VISIBLE, false );
134     mPauseButton.SetProperty( Button::Property::DISABLED, true );
135     mPlayButton.SetProperty( Actor::Property::VISIBLE, true );
136     mPlayButton.SetProperty( Button::Property::DISABLED, false );
137     mChangeButton.SetProperty( Actor::Property::VISIBLE, true );
138     mChangeButton.SetProperty( Button::Property::DISABLED, false );
139
140     mPlayButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, PLAY_IMAGE );
141     mPlayButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, PLAY_IMAGE );
142     mPauseButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, PAUSE_IMAGE );
143     mPauseButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, PAUSE_IMAGE );
144
145     mChangeButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, CHANGE_IMAGE );
146     mChangeButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, CHANGE_IMAGE );
147
148     mBackwardButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, BACKWARD_IMAGE );
149     mBackwardButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, BACKWARD_IMAGE );
150     mForwardButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, FORWARD_IMAGE );
151     mForwardButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, FORWARD_IMAGE );
152
153     mPanGestureDetector = PanGestureDetector::New();
154     mPanGestureDetector.Attach( mVideoView );
155     mPanGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnPan );
156
157     mPinchGestureDetector = PinchGestureDetector::New();
158     mPinchGestureDetector.Attach( mVideoView );
159     mPinchGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnPinch );
160
161     mTapGestureDetector = TapGestureDetector::New();
162     mTapGestureDetector.Attach( mVideoView );
163     mTapGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnTap );
164
165     mRotationAnimation = Animation::New(2.f);
166     mRotationAnimation.AnimateBy( Property(mVideoView, Actor::Property::ORIENTATION), Quaternion(Degree(0.f), Degree(360.f), Degree(0.f)) );
167     mRotationAnimation.SetLooping(false);
168
169     window.KeyEventSignal().Connect( this, &VideoViewController::OnKeyEvent );
170   }
171
172   bool OnButtonClicked( Button button )
173   {
174     if( mPauseButton.GetProperty< int >( Actor::Property::ID ) == button.GetProperty< int >( Actor::Property::ID ))
175     {
176        if( mIsPlay )
177       {
178         mPauseButton.SetProperty( Actor::Property::VISIBLE, false );
179         mPauseButton.SetProperty( Button::Property::DISABLED, true );
180         mPlayButton.SetProperty( Actor::Property::VISIBLE, true );
181         mPlayButton.SetProperty( Button::Property::DISABLED, false );
182
183         mIsPlay = false;
184         mVideoView.Pause();
185       }
186     }
187     else if( mPlayButton.GetProperty< int >( Actor::Property::ID ) == button.GetProperty< int >( Actor::Property::ID ))
188     {
189       mPauseButton.SetProperty( Actor::Property::VISIBLE, true );
190       mPauseButton.SetProperty( Button::Property::DISABLED, false );
191       mPlayButton.SetProperty( Actor::Property::VISIBLE, false );
192       mPlayButton.SetProperty( Button::Property::DISABLED, true );
193
194       mIsPlay = true;
195       mVideoView.Play();
196     }
197     else if( mChangeButton.GetProperty< int >( Actor::Property::ID ) == button.GetProperty< int >( Actor::Property::ID ))
198     {
199       bool underlay = false;
200       underlay = mVideoView.GetProperty( Toolkit::VideoView::Property::UNDERLAY ).Get< bool >();
201       if( underlay )
202       {
203         mVideoView.SetProperty( Toolkit::VideoView::Property::UNDERLAY, false );
204       }
205       else
206       {
207         mVideoView.SetProperty( Toolkit::VideoView::Property::UNDERLAY, true );
208       }
209     }
210     else if( mBackwardButton.GetProperty< int >( Actor::Property::ID ) == button.GetProperty< int >( Actor::Property::ID ))
211     {
212       mVideoView.Backward( SEEK_POS );
213     }
214     else if( mForwardButton.GetProperty< int >( Actor::Property::ID ) == button.GetProperty< int >( Actor::Property::ID ))
215     {
216       mVideoView.Forward( SEEK_POS );
217     }
218
219     return true;
220   }
221
222   void OnPan( Actor actor, const PanGesture& gesture )
223   {
224     if( !mIsFullScreen && gesture.state == Gesture::Continuing )
225     {
226       mVideoView.TranslateBy( Vector3( gesture.displacement ) );
227     }
228   }
229
230   void OnPinch( Actor actor, const PinchGesture& gesture )
231   {
232     if( gesture.state == Gesture::Started )
233     {
234       mPinchStartScale = mScale;
235     }
236
237     if( gesture.state == Gesture::Finished )
238     {
239       mScale = mPinchStartScale * gesture.scale;
240       mVideoView.SetProperty( Actor::Property::SCALE, mScale );
241     }
242   }
243
244   void OnTap( Actor actor, const TapGesture& tapGesture )
245   {
246     if( !mIsFullScreen )
247     {
248       mVideoView.SetProperty( Actor::Property::SIZE, mWindowSize );
249       mIsFullScreen = true;
250     }
251     else
252     {
253       mVideoView.SetProperty( Actor::Property::SIZE, Vector2( INIT_WIDTH, INIT_HEIGHT ) );
254       mIsFullScreen = false;
255     }
256   }
257
258 private:
259
260   /**
261    * Main key event handler
262    */
263   void OnKeyEvent(const KeyEvent& event)
264   {
265     if(event.state == KeyEvent::Down)
266     {
267       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
268       {
269         mApplication.Quit();
270       }
271     }
272   }
273
274 private:
275   Application&  mApplication;
276   VideoView mVideoView;
277   Layer mMenu;
278   Vector2 mWindowSize;
279
280   bool mIsPlay;
281   bool mIsFullScreen;
282
283   PushButton mPlayButton;
284   PushButton mPauseButton;
285   PushButton mChangeButton;
286   PushButton mResetButton;
287   PushButton mBackwardButton;
288   PushButton mForwardButton;
289
290   PanGestureDetector mPanGestureDetector;
291   PinchGestureDetector mPinchGestureDetector;
292   TapGestureDetector mTapGestureDetector;
293   float mScale;
294   float mPinchStartScale;
295
296   Animation mRotationAnimation;
297 };
298
299 int DALI_EXPORT_API main( int argc, char **argv )
300 {
301   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
302   VideoViewController test( application );
303   application.MainLoop();
304   return 0;
305 }