Updated demos to use DALi clang-format
[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-toolkit/dali-toolkit.h>
19 #include <dali/dali.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   VideoViewController(Application& application)
44   : mApplication(application),
45     mIsPlay(false),
46     mIsFullScreen(false),
47     mScale(1.f),
48     mPinchStartScale(1.0f)
49   {
50     // Connect to the Application's Init signal
51     mApplication.InitSignal().Connect(this, &VideoViewController::Create);
52   }
53
54   ~VideoViewController()
55   {
56     mVideoView.Stop();
57   }
58
59   void Create(Application& application)
60   {
61     Window window = application.GetWindow();
62     mWindowSize   = window.GetSize();
63
64     mVideoView = Toolkit::VideoView::New();
65     window.Add(mVideoView);
66     mVideoView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
67     mVideoView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
68     mVideoView.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);
69     mVideoView.SetProperty(Actor::Property::SIZE, Vector2(INIT_WIDTH, INIT_HEIGHT));
70     mVideoView.SetProperty(VideoView::Property::LOOPING, true);
71     mVideoView.SetProperty(VideoView::Property::MUTED, false);
72     mVideoView.SetProperty(VideoView::Property::VIDEO, PLAY_FILE);
73
74     mMenu = Layer::New();
75     mMenu.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_LEFT);
76     mMenu.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_LEFT);
77     mMenu.SetResizePolicy(ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS);
78     mMenu.SetProperty(Actor::Property::SIZE, Vector2(INIT_WIDTH, 120));
79     mVideoView.Add(mMenu);
80
81     mPlayButton = PushButton::New();
82     mPlayButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
83     mPlayButton.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
84     mPlayButton.SetProperty(Dali::Actor::Property::NAME, "Play");
85     mPlayButton.SetResizePolicy(ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS);
86     mPlayButton.SetProperty(Actor::Property::SIZE, Vector2(BUTTON_SIZE, BUTTON_SIZE));
87     mPlayButton.SetProperty(Actor::Property::POSITION, Vector2(40, 10));
88     mPlayButton.ClickedSignal().Connect(this, &VideoViewController::OnButtonClicked);
89
90     mPauseButton = PushButton::New();
91     mPauseButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
92     mPauseButton.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
93     mPauseButton.SetProperty(Dali::Actor::Property::NAME, "Pause");
94     mPauseButton.SetResizePolicy(ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS);
95     mPauseButton.SetProperty(Actor::Property::SIZE, Vector2(BUTTON_SIZE, BUTTON_SIZE));
96     mPauseButton.SetProperty(Actor::Property::POSITION, Vector2(40, 10));
97     mPauseButton.ClickedSignal().Connect(this, &VideoViewController::OnButtonClicked);
98
99     mChangeButton = PushButton::New();
100     mChangeButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
101     mChangeButton.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
102     mChangeButton.SetProperty(Dali::Actor::Property::NAME, "Change");
103     mChangeButton.SetResizePolicy(ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS);
104     mChangeButton.SetProperty(Actor::Property::SIZE, Vector2(BUTTON_SIZE, BUTTON_SIZE));
105     mChangeButton.SetProperty(Actor::Property::POSITION, Vector2(140, 10));
106     mChangeButton.ClickedSignal().Connect(this, &VideoViewController::OnButtonClicked);
107
108     mBackwardButton = PushButton::New();
109     mBackwardButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
110     mBackwardButton.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
111     mBackwardButton.SetProperty(Dali::Actor::Property::NAME, "Backward");
112     mBackwardButton.SetResizePolicy(ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS);
113     mBackwardButton.SetProperty(Actor::Property::SIZE, Vector2(BUTTON_SIZE, BUTTON_SIZE));
114     mBackwardButton.SetProperty(Actor::Property::POSITION, Vector2(240, 10));
115     mBackwardButton.ClickedSignal().Connect(this, &VideoViewController::OnButtonClicked);
116
117     mForwardButton = PushButton::New();
118     mForwardButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
119     mForwardButton.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
120     mForwardButton.SetProperty(Dali::Actor::Property::NAME, "Forward");
121     mForwardButton.SetResizePolicy(ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS);
122     mForwardButton.SetProperty(Actor::Property::SIZE, Vector2(BUTTON_SIZE, BUTTON_SIZE));
123     mForwardButton.SetProperty(Actor::Property::POSITION, Vector2(340, 10));
124     mForwardButton.ClickedSignal().Connect(this, &VideoViewController::OnButtonClicked);
125
126     mMenu.Add(mPlayButton);
127     mMenu.Add(mPauseButton);
128     mMenu.Add(mChangeButton);
129     mMenu.Add(mBackwardButton);
130     mMenu.Add(mForwardButton);
131
132     mPauseButton.SetProperty(Actor::Property::VISIBLE, false);
133     mPauseButton.SetProperty(Button::Property::DISABLED, true);
134     mPlayButton.SetProperty(Actor::Property::VISIBLE, true);
135     mPlayButton.SetProperty(Button::Property::DISABLED, false);
136     mChangeButton.SetProperty(Actor::Property::VISIBLE, true);
137     mChangeButton.SetProperty(Button::Property::DISABLED, false);
138
139     mPlayButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, PLAY_IMAGE);
140     mPlayButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, PLAY_IMAGE);
141     mPauseButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, PAUSE_IMAGE);
142     mPauseButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, PAUSE_IMAGE);
143
144     mChangeButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, CHANGE_IMAGE);
145     mChangeButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, CHANGE_IMAGE);
146
147     mBackwardButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, BACKWARD_IMAGE);
148     mBackwardButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, BACKWARD_IMAGE);
149     mForwardButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, FORWARD_IMAGE);
150     mForwardButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, FORWARD_IMAGE);
151
152     mPanGestureDetector = PanGestureDetector::New();
153     mPanGestureDetector.Attach(mVideoView);
154     mPanGestureDetector.DetectedSignal().Connect(this, &VideoViewController::OnPan);
155
156     mPinchGestureDetector = PinchGestureDetector::New();
157     mPinchGestureDetector.Attach(mVideoView);
158     mPinchGestureDetector.DetectedSignal().Connect(this, &VideoViewController::OnPinch);
159
160     mTapGestureDetector = TapGestureDetector::New();
161     mTapGestureDetector.Attach(mVideoView);
162     mTapGestureDetector.DetectedSignal().Connect(this, &VideoViewController::OnTap);
163
164     mRotationAnimation = Animation::New(2.f);
165     mRotationAnimation.AnimateBy(Property(mVideoView, Actor::Property::ORIENTATION), Quaternion(Degree(0.f), Degree(360.f), Degree(0.f)));
166     mRotationAnimation.SetLooping(false);
167
168     window.KeyEventSignal().Connect(this, &VideoViewController::OnKeyEvent);
169   }
170
171   bool OnButtonClicked(Button button)
172   {
173     if(mPauseButton.GetProperty<int>(Actor::Property::ID) == button.GetProperty<int>(Actor::Property::ID))
174     {
175       if(mIsPlay)
176       {
177         mPauseButton.SetProperty(Actor::Property::VISIBLE, false);
178         mPauseButton.SetProperty(Button::Property::DISABLED, true);
179         mPlayButton.SetProperty(Actor::Property::VISIBLE, true);
180         mPlayButton.SetProperty(Button::Property::DISABLED, false);
181
182         mIsPlay = false;
183         mVideoView.Pause();
184       }
185     }
186     else if(mPlayButton.GetProperty<int>(Actor::Property::ID) == button.GetProperty<int>(Actor::Property::ID))
187     {
188       mPauseButton.SetProperty(Actor::Property::VISIBLE, true);
189       mPauseButton.SetProperty(Button::Property::DISABLED, false);
190       mPlayButton.SetProperty(Actor::Property::VISIBLE, false);
191       mPlayButton.SetProperty(Button::Property::DISABLED, true);
192
193       mIsPlay = true;
194       mVideoView.Play();
195     }
196     else if(mChangeButton.GetProperty<int>(Actor::Property::ID) == button.GetProperty<int>(Actor::Property::ID))
197     {
198       bool underlay = false;
199       underlay      = mVideoView.GetProperty(Toolkit::VideoView::Property::UNDERLAY).Get<bool>();
200       if(underlay)
201       {
202         mVideoView.SetProperty(Toolkit::VideoView::Property::UNDERLAY, false);
203       }
204       else
205       {
206         mVideoView.SetProperty(Toolkit::VideoView::Property::UNDERLAY, true);
207       }
208     }
209     else if(mBackwardButton.GetProperty<int>(Actor::Property::ID) == button.GetProperty<int>(Actor::Property::ID))
210     {
211       mVideoView.Backward(SEEK_POS);
212     }
213     else if(mForwardButton.GetProperty<int>(Actor::Property::ID) == button.GetProperty<int>(Actor::Property::ID))
214     {
215       mVideoView.Forward(SEEK_POS);
216     }
217
218     return true;
219   }
220
221   void OnPan(Actor actor, const PanGesture& gesture)
222   {
223     if(!mIsFullScreen && gesture.GetState() == GestureState::CONTINUING)
224     {
225       mVideoView.TranslateBy(Vector3(gesture.GetDisplacement()));
226     }
227   }
228
229   void OnPinch(Actor actor, const PinchGesture& gesture)
230   {
231     GestureState state = gesture.GetState();
232     if(state == GestureState::STARTED)
233     {
234       mPinchStartScale = mScale;
235     }
236
237     if(state == GestureState::FINISHED)
238     {
239       mScale = mPinchStartScale * gesture.GetScale();
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    * Main key event handler
261    */
262   void OnKeyEvent(const KeyEvent& event)
263   {
264     if(event.GetState() == KeyEvent::DOWN)
265     {
266       if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
267       {
268         mApplication.Quit();
269       }
270     }
271   }
272
273 private:
274   Application& mApplication;
275   VideoView    mVideoView;
276   Layer        mMenu;
277   Vector2      mWindowSize;
278
279   bool mIsPlay;
280   bool mIsFullScreen;
281
282   PushButton mPlayButton;
283   PushButton mPauseButton;
284   PushButton mChangeButton;
285   PushButton mResetButton;
286   PushButton mBackwardButton;
287   PushButton mForwardButton;
288
289   PanGestureDetector   mPanGestureDetector;
290   PinchGestureDetector mPinchGestureDetector;
291   TapGestureDetector   mTapGestureDetector;
292   float                mScale;
293   float                mPinchStartScale;
294
295   Animation mRotationAnimation;
296 };
297
298 int DALI_EXPORT_API main(int argc, char** argv)
299 {
300   Application         application = Application::New(&argc, &argv, DEMO_THEME_PATH);
301   VideoViewController test(application);
302   application.MainLoop();
303   return 0;
304 }