PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/se/klandestino/flash/videoplayer/ControlPanel.as

https://github.com/klandestino/videoplayer
ActionScript | 348 lines | 266 code | 46 blank | 36 comment | 58 complexity | 12edc46cf3a4bea0726b9004e90692d2 MD5 | raw file
  1. package se.klandestino.flash.videoplayer {
  2. import flash.display.Sprite;
  3. import flash.events.Event;
  4. import flash.events.MouseEvent;
  5. import flash.geom.Point;
  6. import flash.ui.Mouse;
  7. import flash.utils.clearTimeout;
  8. import flash.utils.setTimeout;
  9. import se.klandestino.flash.debug.Debug;
  10. import se.klandestino.flash.net.MultiLoader;
  11. import se.klandestino.flash.utils.CoordinationTools;
  12. import se.klandestino.flash.utils.StringUtil;
  13. import se.klandestino.flash.videoplayer.events.VideoplayerEvent;
  14. import se.klandestino.flash.videoplayer.Videoplayer;
  15. /**
  16. * Sprite sub class description.
  17. *
  18. * @langversion ActionScript 3.0
  19. * @playerversion Flash 9.0
  20. *
  21. * @author spurge
  22. * @since 2010-04-02
  23. */
  24. public class ControlPanel extends Sprite {
  25. //--------------------------------------
  26. // CLASS CONSTANTS
  27. //--------------------------------------
  28. public static const MOUSE_HIDE_TIMEOUT:int = 3000;
  29. public static const POSITION_BOTTOM:String = 'bottom';
  30. public static const POSITION_CENTER:String = 'center';
  31. public static const POSITION_LEFT:String = 'left';
  32. public static const POSITION_RIGHT:String = 'right';
  33. public static const POSITION_TOP:String = 'top';
  34. public static const SETUP_MOUSE:String = 'mouse';
  35. public static const SETUP_PAUSE:String = 'pause';
  36. public static const SETUP_PLAY:String = 'play';
  37. public static const SHOW_ALWAYS:String = 'always';
  38. public static const SHOW_MOUSE:String = 'mouse';
  39. public static const SHOW_PAUSE:String = 'pause';
  40. public static const SHOW_PLAY:String = 'play';
  41. //--------------------------------------
  42. // CONSTRUCTOR
  43. //--------------------------------------
  44. /**
  45. * @constructor
  46. */
  47. public function ControlPanel () {
  48. super ();
  49. this.visible = false;
  50. this.loader = new MultiLoader ();
  51. this.loader.addEventListener (Event.COMPLETE, this.loaderCompleteHandler, false, 0, true);
  52. }
  53. //--------------------------------------
  54. // PRIVATE VARIABLES
  55. //--------------------------------------
  56. private var _currentSetup:String = ControlPanel.SETUP_PAUSE;
  57. private var loader:MultiLoader;
  58. private var mouseTimeout:uint;
  59. private var pausebutton:Object;
  60. private var playbutton:Object;
  61. private var _videoplayer:Videoplayer;
  62. //--------------------------------------
  63. // GETTER/SETTERS
  64. //--------------------------------------
  65. public function get currentSetup ():String {
  66. return this._currentSetup;
  67. }
  68. public function get videoplayer ():Videoplayer {
  69. return this._videoplayer;
  70. }
  71. public function set videoplayer (videoplayer:Videoplayer):void {
  72. if (this._videoplayer != null) {
  73. this._videoplayer.removeEventListener (Event.ENTER_FRAME, this.videoEnterFrameHandler);
  74. this._videoplayer.removeEventListener (Event.RESIZE, this.videoResizeHandler);
  75. this._videoplayer.removeEventListener (MouseEvent.MOUSE_MOVE, this.videoMouseMoveHandler);
  76. this._videoplayer.removeEventListener (VideoplayerEvent.LOADED, this.videoLoadedHandler);
  77. this._videoplayer.removeEventListener (VideoplayerEvent.PAUSE, this.videoPauseHandler);
  78. this._videoplayer.removeEventListener (VideoplayerEvent.PLAY, this.videoPlayHandler);
  79. this._videoplayer.removeEventListener (VideoplayerEvent.RESUME, this.videoResumeHandler);
  80. this._videoplayer.removeEventListener (VideoplayerEvent.STOP, this.videoStopHandler);
  81. }
  82. this._videoplayer = videoplayer;
  83. this._videoplayer.addEventListener (Event.ENTER_FRAME, this.videoEnterFrameHandler, false, 0, true);
  84. this._videoplayer.addEventListener (Event.RESIZE, this.videoResizeHandler, false, 0, true);
  85. this._videoplayer.addEventListener (MouseEvent.MOUSE_MOVE, this.videoMouseMoveHandler, false, 0, true);
  86. this._videoplayer.addEventListener (VideoplayerEvent.LOADED, this.videoLoadedHandler, false, 0, true);
  87. this._videoplayer.addEventListener (VideoplayerEvent.PAUSE, this.videoPauseHandler, false, 0, true);
  88. this._videoplayer.addEventListener (VideoplayerEvent.PLAY, this.videoPlayHandler, false, 0, true);
  89. this._videoplayer.addEventListener (VideoplayerEvent.RESUME, this.videoResumeHandler, false, 0, true);
  90. this._videoplayer.addEventListener (VideoplayerEvent.STOP, this.videoStopHandler, false, 0, true);
  91. if (this.videoplayer.loaded) {
  92. this.visible = true;
  93. }
  94. }
  95. //--------------------------------------
  96. // PUBLIC METHODS
  97. //--------------------------------------
  98. public function init ():void {
  99. this.loader.load ();
  100. }
  101. public function setup (setup:String):void {
  102. Debug.debug ('Settng up control panel to ' + setup);
  103. switch (setup) {
  104. case ControlPanel.SETUP_MOUSE:
  105. this._currentSetup = ControlPanel.SETUP_MOUSE;
  106. Mouse.show ();
  107. clearTimeout (this.mouseTimeout);
  108. this.mouseTimeout = setTimeout (this.endMouseSetup, MOUSE_HIDE_TIMEOUT);
  109. break;
  110. case ControlPanel.SETUP_PAUSE:
  111. Mouse.show ();
  112. this._currentSetup = ControlPanel.SETUP_PAUSE;
  113. break;
  114. case ControlPanel.SETUP_PLAY:
  115. Mouse.hide ();
  116. this._currentSetup = ControlPanel.SETUP_PLAY;
  117. break;
  118. default:
  119. Debug.warn ('There is no setup with name ' + setup);
  120. return;
  121. }
  122. this.setupButton (this.pausebutton);
  123. this.setupButton (this.playbutton);
  124. }
  125. public function setPauseButton (url:String, params:Object = null):void {
  126. Debug.debug ('Setting pause button to ' + url);
  127. this.hideButton (this.pausebutton);
  128. this.pausebutton = new Object ();
  129. this.pausebutton.url = url;
  130. this.pausebutton.show = params ? (params.show ? params.show : ControlPanel.SHOW_MOUSE) : ControlPanel.SHOW_MOUSE;
  131. this.pausebutton.hide = params ? (params.hide ? params.hide : ControlPanel.SHOW_PLAY) : ControlPanel.SHOW_PLAY;
  132. this.pausebutton.x = params ? (params.x ? params.x : '') : '';
  133. this.pausebutton.y = params ? (params.y ? params.y : '') : '';
  134. this.pausebutton.sprite = new Sprite ();
  135. this.addChild (this.pausebutton.sprite);
  136. this.loader.add (this.pausebutton.url, 'pause', this.pausebutton.sprite);
  137. }
  138. public function setPlayButton (url:String, params:Object = null):void {
  139. Debug.debug ('Setting play button to ' + url);
  140. this.hideButton (this.playbutton);
  141. this.playbutton = new Object ();
  142. this.playbutton.url = url;
  143. this.playbutton.show = params ? (params.show ? params.show : ControlPanel.SHOW_PAUSE) : ControlPanel.SHOW_PAUSE;
  144. this.playbutton.hide = params ? (params.hide ? params.hide : '') : '';
  145. this.playbutton.x = params ? (params.x ? params.x : '') : '';
  146. this.playbutton.y = params ? (params.y ? params.y : '') : '';
  147. this.playbutton.sprite = new Sprite ();
  148. this.addChild (this.playbutton.sprite);
  149. this.loader.add (this.playbutton.url, 'play', this.playbutton.sprite);
  150. }
  151. //--------------------------------------
  152. // EVENT HANDLERS
  153. //--------------------------------------
  154. private function buttonClickHandler (event:MouseEvent):void {
  155. if (this.pausebutton != null && this.videoplayer != null) {
  156. if (event.target === this.pausebutton.sprite) {
  157. Debug.debug ('Pause button clicked');
  158. this.videoplayer.pause ();
  159. }
  160. }
  161. if (this.playbutton != null && this.videoplayer != null) {
  162. if (event.target === this.playbutton.sprite) {
  163. Debug.debug ('Play button clicked');
  164. this.videoplayer.resume ();
  165. }
  166. }
  167. }
  168. private function loaderCompleteHandler (event:Event):void {
  169. Debug.debug ('Loading buttons complete');
  170. this.setup (this.currentSetup);
  171. }
  172. private function videoEnterFrameHandler (event:Event):void {
  173. /*var point:Point = CoordinationTools.localToLocal (this.videoplayer, this);
  174. this.x = point.x;
  175. this.y = point.y;*/
  176. }
  177. private function videoLoadedHandler (event:VideoplayerEvent):void {
  178. Debug.debug ('Handling loaded event from video');
  179. this.visible = true;
  180. }
  181. private function videoMouseMoveHandler (event:MouseEvent):void {
  182. if (this.currentSetup == ControlPanel.SETUP_PLAY) {
  183. this.setup (ControlPanel.SETUP_MOUSE);
  184. }
  185. }
  186. private function videoPauseHandler (event:VideoplayerEvent):void {
  187. Debug.debug ('Handling pause event from video');
  188. this.setup (ControlPanel.SETUP_PAUSE);
  189. }
  190. private function videoPlayHandler (event:VideoplayerEvent):void {
  191. Debug.debug ('Handling play event from video');
  192. this.setup (ControlPanel.SETUP_PLAY);
  193. }
  194. private function videoResizeHandler (event:Event):void {
  195. Debug.debug ('Handling resize event from video ' + this.videoplayer.width + 'x' + this.videoplayer.height);
  196. this.setup (this.currentSetup);
  197. }
  198. private function videoResumeHandler (event:VideoplayerEvent):void {
  199. Debug.debug ('Handling resume event from video');
  200. this.setup (ControlPanel.SETUP_PLAY);
  201. }
  202. private function videoStopHandler (event:VideoplayerEvent):void {
  203. Debug.debug ('Handling pause event from video');
  204. this.setup (ControlPanel.SETUP_PAUSE);
  205. }
  206. //--------------------------------------
  207. // PRIVATE & PROTECTED INSTANCE METHODS
  208. //--------------------------------------
  209. private function hideButton (button:Object):void {
  210. if (button != null) {
  211. button.sprite.removeEventListener (MouseEvent.CLICK, this.buttonClickHandler);
  212. button.sprite.visible = false;
  213. }
  214. }
  215. private function showButton (button:Object):void {
  216. button.sprite.mouseChildren = false;
  217. button.sprite.buttonMode = true;
  218. button.sprite.addEventListener (MouseEvent.CLICK, this.buttonClickHandler, false, 0, true);
  219. this.setupButtonPositions (button);
  220. button.sprite.visible = true;
  221. }
  222. private function setupButton (button:Object):void {
  223. if (button != null) {
  224. switch (this.currentSetup) {
  225. case ControlPanel.SETUP_MOUSE:
  226. if (button.hide == ControlPanel.SHOW_MOUSE) {
  227. this.hideButton (button);
  228. } else if (button.show == ControlPanel.SHOW_MOUSE || button.show == ControlPanel.SHOW_ALWAYS) {
  229. this.showButton (button);
  230. } else {
  231. this.hideButton (button);
  232. }
  233. break;
  234. case ControlPanel.SETUP_PAUSE:
  235. if (button.hide == ControlPanel.SHOW_PAUSE) {
  236. this.hideButton (button);
  237. } else if (button.show == ControlPanel.SHOW_PAUSE || button.show == ControlPanel.SHOW_ALWAYS) {
  238. this.showButton (button);
  239. } else {
  240. this.hideButton (button);
  241. }
  242. break;
  243. case ControlPanel.SETUP_PLAY:
  244. if (button.hide == ControlPanel.SHOW_PLAY) {
  245. this.hideButton (button);
  246. } else if (button.show == ControlPanel.SHOW_PLAY || button.show == ControlPanel.SHOW_ALWAYS) {
  247. this.showButton (button);
  248. } else {
  249. this.hideButton (button);
  250. }
  251. break;
  252. }
  253. }
  254. }
  255. private function setupButtonPositions (button:Object):void {
  256. if (button != null && this.videoplayer) {
  257. var x:Number = (this.videoplayer.width - button.sprite.width) / 2;
  258. var y:Number = (this.videoplayer.height - button.sprite.height) / 2;
  259. if (!StringUtil.isEmpty (button.x)) {
  260. if (!isNaN (parseFloat (button.x))) {
  261. x = parseFloat (button.x);
  262. } else {
  263. switch (button.x) {
  264. case ControlPanel.POSITION_LEFT:
  265. x = 0;
  266. break;
  267. case ControlPanel.POSITION_RIGHT:
  268. x = this.videoplayer.width - button.sprite.width;
  269. break;
  270. default:
  271. Debug.warn ('There x is no position by value ' + button.x);
  272. }
  273. }
  274. }
  275. if (!StringUtil.isEmpty (button.y)) {
  276. if (!isNaN (parseFloat (button.y))) {
  277. y = parseFloat (button.y);
  278. } else {
  279. switch (button.y) {
  280. case ControlPanel.POSITION_BOTTOM:
  281. y = this.videoplayer.height - button.sprite.height;;
  282. break;
  283. case ControlPanel.POSITION_TOP:
  284. y = 0;
  285. break;
  286. default:
  287. Debug.warn ('There y is no position by value ' + button.x);
  288. }
  289. }
  290. }
  291. Debug.debug ('Setting up button positions to ' + x + 'x' + y);
  292. button.sprite.x = x;
  293. button.sprite.y = y;
  294. }
  295. }
  296. private function endMouseSetup ():void {
  297. if (this.currentSetup == ControlPanel.SETUP_MOUSE) {
  298. this.setup (ControlPanel.SETUP_PLAY);
  299. }
  300. }
  301. }
  302. }