Further Setter/Getter public API removal from Dali::Actor
[platform/core/uifw/dali-demo.git] / examples / video-view / video-view-example.cpp
1 /*
2  * Copyright (c) 2019 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     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
63
64     mStageSize = Stage::GetCurrent().GetSize();
65
66     mVideoView = Toolkit::VideoView::New();
67     Stage::GetCurrent().Add( mVideoView );
68     mVideoView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
69     mVideoView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
70     mVideoView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
71     mVideoView.SetProperty( Actor::Property::SIZE, Vector2( INIT_WIDTH, INIT_HEIGHT ) );
72     mVideoView.SetProperty( VideoView::Property::LOOPING, true );
73     mVideoView.SetProperty( VideoView::Property::MUTED, false );
74     mVideoView.SetProperty( VideoView::Property::VIDEO, PLAY_FILE );
75
76     mMenu = Layer::New();
77     mMenu.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_LEFT );
78     mMenu.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_LEFT );
79     mMenu.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
80     mMenu.SetProperty( Actor::Property::SIZE, Vector2( INIT_WIDTH, 120 ) );
81     mVideoView.Add( mMenu );
82
83     mPlayButton = PushButton::New();
84     mPlayButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
85     mPlayButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
86     mPlayButton.SetProperty( Dali::Actor::Property::NAME, "Play" );
87     mPlayButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
88     mPlayButton.SetProperty( Actor::Property::SIZE, Vector2( BUTTON_SIZE, BUTTON_SIZE ) );
89     mPlayButton.SetProperty( Actor::Property::POSITION, Vector2( 40, 10 ));
90     mPlayButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
91
92     mPauseButton = PushButton::New();
93     mPauseButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
94     mPauseButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
95     mPauseButton.SetProperty( Dali::Actor::Property::NAME, "Pause" );
96     mPauseButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
97     mPauseButton.SetProperty( Actor::Property::SIZE, Vector2( BUTTON_SIZE, BUTTON_SIZE ) );
98     mPauseButton.SetProperty( Actor::Property::POSITION, Vector2( 40, 10 ));
99     mPauseButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
100
101     mChangeButton = PushButton::New();
102     mChangeButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
103     mChangeButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
104     mChangeButton.SetProperty( Dali::Actor::Property::NAME, "Change" );
105     mChangeButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
106     mChangeButton.SetProperty( Actor::Property::SIZE, Vector2( BUTTON_SIZE, BUTTON_SIZE ) );
107     mChangeButton.SetProperty( Actor::Property::POSITION, Vector2( 140, 10 ));
108     mChangeButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
109
110     mBackwardButton = PushButton::New();
111     mBackwardButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
112     mBackwardButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
113     mBackwardButton.SetProperty( Dali::Actor::Property::NAME, "Backward" );
114     mBackwardButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
115     mBackwardButton.SetProperty( Actor::Property::SIZE, Vector2( BUTTON_SIZE, BUTTON_SIZE ) );
116     mBackwardButton.SetProperty( Actor::Property::POSITION, Vector2( 240, 10 ));
117     mBackwardButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
118
119     mForwardButton = PushButton::New();
120     mForwardButton.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
121     mForwardButton.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
122     mForwardButton.SetProperty( Dali::Actor::Property::NAME, "Forward" );
123     mForwardButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
124     mForwardButton.SetProperty( Actor::Property::SIZE, Vector2( BUTTON_SIZE, BUTTON_SIZE ) );
125     mForwardButton.SetProperty( Actor::Property::POSITION, Vector2( 340, 10 ));
126     mForwardButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
127
128     mMenu.Add( mPlayButton );
129     mMenu.Add( mPauseButton );
130     mMenu.Add( mChangeButton );
131     mMenu.Add( mBackwardButton );
132     mMenu.Add( mForwardButton );
133
134     mPauseButton.SetProperty( Actor::Property::VISIBLE, false );
135     mPauseButton.SetProperty( Button::Property::DISABLED, true );
136     mPlayButton.SetProperty( Actor::Property::VISIBLE, true );
137     mPlayButton.SetProperty( Button::Property::DISABLED, false );
138     mChangeButton.SetProperty( Actor::Property::VISIBLE, true );
139     mChangeButton.SetProperty( Button::Property::DISABLED, false );
140
141     mPlayButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, PLAY_IMAGE );
142     mPlayButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, PLAY_IMAGE );
143     mPauseButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, PAUSE_IMAGE );
144     mPauseButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, PAUSE_IMAGE );
145
146     mChangeButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, CHANGE_IMAGE );
147     mChangeButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, CHANGE_IMAGE );
148
149     mBackwardButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, BACKWARD_IMAGE );
150     mBackwardButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, BACKWARD_IMAGE );
151     mForwardButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, FORWARD_IMAGE );
152     mForwardButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, FORWARD_IMAGE );
153
154     mPanGestureDetector = PanGestureDetector::New();
155     mPanGestureDetector.Attach( mVideoView );
156     mPanGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnPan );
157
158     mPinchGestureDetector = PinchGestureDetector::New();
159     mPinchGestureDetector.Attach( mVideoView );
160     mPinchGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnPinch );
161
162     mTapGestureDetector = TapGestureDetector::New();
163     mTapGestureDetector.Attach( mVideoView );
164     mTapGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnTap );
165
166     mRotationAnimation = Animation::New(2.f);
167     mRotationAnimation.AnimateBy( Property(mVideoView, Actor::Property::ORIENTATION), Quaternion(Degree(0.f), Degree(360.f), Degree(0.f)) );
168     mRotationAnimation.SetLooping(false);
169
170     Stage::GetCurrent().KeyEventSignal().Connect( this, &VideoViewController::OnKeyEvent );
171
172   }
173
174   bool OnButtonClicked( Button button )
175   {
176     if( mPauseButton.GetId() == button.GetId())
177     {
178        if( mIsPlay )
179       {
180         mPauseButton.SetProperty( Actor::Property::VISIBLE, false );
181         mPauseButton.SetProperty( Button::Property::DISABLED, true );
182         mPlayButton.SetProperty( Actor::Property::VISIBLE, true );
183         mPlayButton.SetProperty( Button::Property::DISABLED, false );
184
185         mIsPlay = false;
186         mVideoView.Pause();
187       }
188     }
189     else if( mPlayButton.GetId() == button.GetId())
190     {
191       mPauseButton.SetProperty( Actor::Property::VISIBLE, true );
192       mPauseButton.SetProperty( Button::Property::DISABLED, false );
193       mPlayButton.SetProperty( Actor::Property::VISIBLE, false );
194       mPlayButton.SetProperty( Button::Property::DISABLED, true );
195
196       mIsPlay = true;
197       mVideoView.Play();
198     }
199     else if( mChangeButton.GetId() == button.GetId())
200     {
201       bool underlay = false;
202       underlay = mVideoView.GetProperty( Toolkit::VideoView::Property::UNDERLAY ).Get< bool >();
203       if( underlay )
204       {
205         mVideoView.SetProperty( Toolkit::VideoView::Property::UNDERLAY, false );
206       }
207       else
208       {
209         mVideoView.SetProperty( Toolkit::VideoView::Property::UNDERLAY, true );
210       }
211     }
212     else if( mBackwardButton.GetId() == button.GetId())
213     {
214       mVideoView.Backward( SEEK_POS );
215     }
216     else if( mForwardButton.GetId() == button.GetId())
217     {
218       mVideoView.Forward( SEEK_POS );
219     }
220
221     return true;
222   }
223
224   void OnPan( Actor actor, const PanGesture& gesture )
225   {
226     if( !mIsFullScreen && gesture.state == Gesture::Continuing )
227     {
228       mVideoView.TranslateBy( Vector3( gesture.displacement ) );
229     }
230   }
231
232   void OnPinch( Actor actor, const PinchGesture& gesture )
233   {
234     if( gesture.state == Gesture::Started )
235     {
236       mPinchStartScale = mScale;
237     }
238
239     if( gesture.state == Gesture::Finished )
240     {
241       mScale = mPinchStartScale * gesture.scale;
242       mVideoView.SetProperty( Actor::Property::SCALE, mScale );
243     }
244   }
245
246   void OnTap( Actor actor, const TapGesture& tapGesture )
247   {
248     if( !mIsFullScreen )
249     {
250       mVideoView.SetProperty( Actor::Property::SIZE, mStageSize );
251       mIsFullScreen = true;
252     }
253     else
254     {
255       mVideoView.SetProperty( Actor::Property::SIZE, Vector2( INIT_WIDTH, INIT_HEIGHT ) );
256       mIsFullScreen = false;
257     }
258   }
259
260 private:
261
262   /**
263    * Main key event handler
264    */
265   void OnKeyEvent(const KeyEvent& event)
266   {
267     if(event.state == KeyEvent::Down)
268     {
269       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
270       {
271         mApplication.Quit();
272       }
273     }
274   }
275
276 private:
277   Application&  mApplication;
278   VideoView mVideoView;
279   Layer mMenu;
280   Vector2 mStageSize;
281
282   bool mIsPlay;
283   bool mIsFullScreen;
284
285   PushButton mPlayButton;
286   PushButton mPauseButton;
287   PushButton mChangeButton;
288   PushButton mResetButton;
289   PushButton mBackwardButton;
290   PushButton mForwardButton;
291
292   PanGestureDetector mPanGestureDetector;
293   PinchGestureDetector mPinchGestureDetector;
294   TapGestureDetector mTapGestureDetector;
295   float mScale;
296   float mPinchStartScale;
297
298   Animation mRotationAnimation;
299 };
300
301 int DALI_EXPORT_API main( int argc, char **argv )
302 {
303   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
304   VideoViewController test( application );
305   application.MainLoop();
306   return 0;
307 }