videoplayer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. class VideoPlayer extends ScriptedWidgetEventHandler
  2. {
  3. protected Widget m_Root;
  4. protected ButtonWidget m_PlayButton;
  5. protected ButtonWidget m_PauseButton;
  6. protected ButtonWidget m_StopButton;
  7. protected ButtonWidget m_OnceButton;
  8. protected ButtonWidget m_RepeatButton;
  9. protected ButtonWidget m_LoadButton;
  10. protected GridSpacerWidget m_LoadVideo;
  11. protected SliderWidget m_Progress;
  12. protected TextWidget m_CurrentTime;
  13. protected TextWidget m_TotalTime;
  14. protected ImageWidget m_Buffering;
  15. /*protected*/ VideoWidget m_VideoWidget;
  16. void VideoPlayer(Widget parent)
  17. {
  18. m_Root = GetGame().GetWorkspace().CreateWidgets("gui/layouts/new_ui/video_player.layout", parent);
  19. m_Root.SetHandler(this);
  20. m_Root.SetSort(333);
  21. Init();
  22. }
  23. void ~VideoPlayer()
  24. {
  25. }
  26. void Show(bool show)
  27. {
  28. m_Root.Show(show);
  29. }
  30. private void Init()
  31. {
  32. m_PlayButton = ButtonWidget.Cast(m_Root.FindAnyWidget("vp_PlayButton"));
  33. m_PauseButton = ButtonWidget.Cast(m_Root.FindAnyWidget("vp_PauseButton"));
  34. m_StopButton = ButtonWidget.Cast(m_Root.FindAnyWidget("vp_StopButton"));
  35. m_OnceButton = ButtonWidget.Cast(m_Root.FindAnyWidget("vp_OnceButton"));
  36. m_RepeatButton = ButtonWidget.Cast(m_Root.FindAnyWidget("vp_RepeatButton"));
  37. m_LoadButton = ButtonWidget.Cast(m_Root.FindAnyWidget("vp_LoadButton"));
  38. m_LoadVideo = GridSpacerWidget.Cast(m_Root.FindAnyWidget("vp_LoadVideo"));
  39. m_LoadVideo.Show(false);
  40. m_Progress = SliderWidget.Cast(m_Root.FindAnyWidget("vp_Progress"));
  41. m_Progress.SetCurrent(0);
  42. m_CurrentTime = TextWidget.Cast(m_Root.FindAnyWidget("vp_CurrentTime"));
  43. m_TotalTime = TextWidget.Cast(m_Root.FindAnyWidget("vp_TotalTime"));
  44. m_Buffering = ImageWidget.Cast(m_Root.FindAnyWidget("vp_Buffering"));
  45. m_Buffering.Show(false);
  46. m_VideoWidget = VideoWidget.Cast(m_Root.FindAnyWidget("vp_Video"));
  47. m_VideoWidget.SetCallback(VideoCallback.ON_PLAY, OnPlaybackStart);
  48. m_VideoWidget.SetCallback(VideoCallback.ON_PAUSE, OnPlaybackStop);
  49. m_VideoWidget.SetCallback(VideoCallback.ON_STOP, OnPlaybackStop);
  50. m_VideoWidget.SetCallback(VideoCallback.ON_END, OnPlaybackStop);
  51. m_VideoWidget.SetCallback(VideoCallback.ON_LOAD, OnPlaybackStop);
  52. m_VideoWidget.SetCallback(VideoCallback.ON_SEEK, UpdateCurrentTime);
  53. m_VideoWidget.SetCallback(VideoCallback.ON_BUFFERING_START, OnBufferingStart);
  54. m_VideoWidget.SetCallback(VideoCallback.ON_BUFFERING_END, OnBufferingEnd);
  55. }
  56. private void InitVideoLoading()
  57. {
  58. string path = "video\\*";
  59. string fileName;
  60. FileAttr fileAttr;
  61. FindFileHandle handle = FindFile(path, fileName, fileAttr, FindFileFlags.DIRECTORIES);
  62. if (fileName != "")
  63. {
  64. CreateVideoLoadingEntry(fileName);
  65. }
  66. while (FindNextFile(handle, fileName, fileAttr))
  67. {
  68. CreateVideoLoadingEntry(fileName);
  69. }
  70. CloseFindFile(handle);
  71. }
  72. private void CreateVideoLoadingEntry(string entryName)
  73. {
  74. Widget entry = GetGame().GetWorkspace().CreateWidgets("gui/layouts/new_ui/video_player_entry.layout", m_LoadVideo);
  75. ButtonWidget entryButton = ButtonWidget.Cast(entry.GetChildren());
  76. entryButton.SetText(entryName);
  77. entryButton.SetUserID(333);
  78. }
  79. private void UpdateCurrentTime()
  80. {
  81. int time = m_VideoWidget.GetTime();
  82. UpdateTime(m_CurrentTime, time);
  83. m_Progress.SetCurrent(time);
  84. }
  85. // This can be an async op
  86. private void UpdateTotalTime()
  87. {
  88. int time = m_VideoWidget.GetTotalTime();
  89. if (time != 0)
  90. {
  91. UpdateTime(m_TotalTime, time);
  92. m_Progress.SetMinMax(0, time);
  93. GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).Remove(UpdateTotalTime);
  94. }
  95. }
  96. private void UpdateTime(TextWidget widget, int time)
  97. {
  98. FullTimeData timeData = new FullTimeData();
  99. TimeConversions.ConvertSecondsToFullTime(time / 1000, timeData);
  100. widget.SetText(timeData.FormatedAsTimestamp());
  101. }
  102. override bool OnChange(Widget w, int x, int y, bool finished)
  103. {
  104. if (w == m_Progress)
  105. {
  106. m_VideoWidget.SetTime(m_Progress.GetCurrent(), finished);
  107. }
  108. return super.OnChange(w, x, y, finished);
  109. }
  110. override bool OnClick(Widget w, int x, int y, int button)
  111. {
  112. if (w == m_PlayButton)
  113. {
  114. PlayVideo();
  115. }
  116. else if (w == m_PauseButton)
  117. {
  118. PauseVideo();
  119. }
  120. else if (w == m_StopButton)
  121. {
  122. StopVideo();
  123. }
  124. else if (w == m_OnceButton)
  125. {
  126. OnceVideo();
  127. }
  128. else if (w == m_RepeatButton)
  129. {
  130. RepeatVideo();
  131. }
  132. else if (w == m_LoadButton)
  133. {
  134. ToggleVideoSelection();
  135. }
  136. else if (w == m_Progress)
  137. {
  138. Print(x);
  139. Print(y);
  140. Print(button);
  141. }
  142. else if (w.GetUserID() == 333)
  143. {
  144. string name;
  145. ButtonWidget.Cast(w).GetText(name);
  146. LoadVideo(name);
  147. ToggleVideoSelection();
  148. }
  149. return super.OnClick(w, x, y, button);
  150. }
  151. protected void OnPlaybackStart()
  152. {
  153. m_PlayButton.Show(false);
  154. m_PauseButton.Show(true);
  155. GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLater(UpdateCurrentTime, 0, true);
  156. }
  157. protected void OnPlaybackStop()
  158. {
  159. m_PlayButton.Show(true);
  160. m_PauseButton.Show(false);
  161. UpdateCurrentTime();
  162. GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).Remove(UpdateCurrentTime);
  163. }
  164. protected void OnBufferingStart()
  165. {
  166. m_Buffering.Show(true);
  167. }
  168. protected void OnBufferingEnd()
  169. {
  170. m_Buffering.Show(false);
  171. }
  172. void ToggleVideoSelection()
  173. {
  174. if (!m_LoadVideo.IsVisible())
  175. {
  176. InitVideoLoading();
  177. }
  178. else
  179. {
  180. Widget child = m_LoadVideo.GetChildren();
  181. while (child)
  182. {
  183. Widget c = child;
  184. child = child.GetSibling();
  185. c.Unlink();
  186. }
  187. }
  188. m_LoadVideo.Show(!m_LoadVideo.IsVisible());
  189. }
  190. void LoadVideo(string videoPath)
  191. {
  192. string path;
  193. #ifdef PLATFORM_WINDOWS
  194. path = ".\\video\\";
  195. #endif
  196. #ifdef PLATFORM_PS4
  197. path = "/app0/video/";
  198. #endif
  199. #ifdef PLATFORM_XBOX
  200. path = "G:\\video\\";
  201. #endif
  202. m_VideoWidget.Load(path + videoPath, m_VideoWidget.IsLooping());
  203. GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLater(UpdateTotalTime, 0, true);
  204. }
  205. void PlayVideo()
  206. {
  207. m_VideoWidget.Play();
  208. }
  209. void PauseVideo()
  210. {
  211. m_VideoWidget.Pause();
  212. }
  213. void StopVideo()
  214. {
  215. m_VideoWidget.Stop();
  216. m_PlayButton.Show(true);
  217. m_PauseButton.Show(false);
  218. }
  219. void OnceVideo()
  220. {
  221. m_VideoWidget.SetLooping(false);
  222. m_OnceButton.Show(false);
  223. m_RepeatButton.Show(true);
  224. }
  225. void RepeatVideo()
  226. {
  227. m_VideoWidget.SetLooping(true);
  228. m_RepeatButton.Show(false);
  229. m_OnceButton.Show(true);
  230. }
  231. void KillVideo()
  232. {
  233. m_VideoWidget.Unload();
  234. }
  235. }