/src/com/wagerfield/media/VideoPlayPause.as

https://github.com/wagerfield/girder · ActionScript · 371 lines · 283 code · 23 blank · 65 comment · 2 complexity · 851a9835b51c88e6a1ca5a3f4af6ff96 MD5 · raw file

  1. /**
  2. * Copyright (C) 2011 by Matthew Wagerfield
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. package com.wagerfield.media
  23. {
  24. import com.wagerfield.display.SuperSprite;
  25. import com.wagerfield.events.ModMouseEvent;
  26. import com.wagerfield.events.VideoStreamEvent;
  27. import flash.display.Sprite;
  28. /**
  29. * @author Matthew Wagerfield
  30. */
  31. public class VideoPlayPause extends SuperSprite
  32. {
  33. private var _hotspotBorder:Number = 0;
  34. private var _fillColour:uint = 0x000000;
  35. private var _fillAlpha:Number = 1;
  36. private var _strokeColour:uint = 0x000000;
  37. private var _strokeAlpha:Number = 1;
  38. private var _bgRadius:Number = 0;
  39. private var _bgColour:uint = 0xDDDDDD;
  40. private var _bgAlpha:Number = 1;
  41. private var _playWidth:uint = 8;
  42. private var _playHeight:uint = 8;
  43. private var _playStroke:Number = 1.5;
  44. private var _playOffsetX:Number = 0;
  45. private var _pauseWidth:uint = 10;
  46. private var _pauseHeight:uint = 10;
  47. private var _pauseSpacing:uint = 2;
  48. private var _pauseStroke:Number = 0;
  49. private var _video:VideoStream;
  50. private var _bg:Sprite;
  51. private var _symbols:Sprite;
  52. private var _play:Sprite;
  53. private var _pause:Sprite;
  54. private var _hotspot:Sprite;
  55. /**
  56. * Creates an instance of a VideoPlayPause unit for toggling the playback of an attached VideoStream instance.
  57. *
  58. * @param videoStream An instance of the VideoStream class that the VideoPlayPause will react to and modify.
  59. */
  60. public function VideoPlayPause(videoStream:VideoStream):void
  61. {
  62. _video = videoStream;
  63. createClasses();
  64. configClasses();
  65. configDisplay();
  66. drawGraphics();
  67. addEvents();
  68. addChildren();
  69. }
  70. private function createClasses():void
  71. {
  72. _bg = new Sprite();
  73. _symbols = new Sprite();
  74. _play = new Sprite();
  75. _pause = new Sprite();
  76. _hotspot = new Sprite();
  77. }
  78. private function configClasses():void
  79. {
  80. _width = 20;
  81. _height = 20;
  82. _hotspot.buttonMode = true;
  83. }
  84. protected override function configDisplay():void
  85. {
  86. drawGraphics();
  87. _symbols.x = _width * 0.5;
  88. _symbols.y = _height * 0.5;
  89. _play.x = _playOffsetX;
  90. super.configDisplay();
  91. }
  92. private function drawGraphics():void
  93. {
  94. with (_bg.graphics)
  95. {
  96. clear();
  97. beginFill(_bgColour, _bgAlpha);
  98. drawRoundRect(0, 0, _width, _height, _bgRadius);
  99. endFill();
  100. }
  101. with (_play.graphics)
  102. {
  103. clear();
  104. if (_playStroke > 0) lineStyle(_playStroke, _strokeColour, _strokeAlpha);
  105. beginFill(_fillColour, _fillAlpha);
  106. moveTo(-_playWidth / 2, -_playHeight / 2);
  107. lineTo(_playWidth / 2, 0);
  108. lineTo(-_playWidth / 2, _playHeight / 2);
  109. endFill();
  110. }
  111. with (_pause.graphics)
  112. {
  113. clear();
  114. if (_pauseStroke > 0) lineStyle(_pauseStroke, _strokeColour, _strokeAlpha);
  115. beginFill(_fillColour, _fillAlpha);
  116. drawRect(-_pauseWidth / 2, -_pauseHeight / 2, (_pauseWidth - _pauseSpacing) / 2, _pauseHeight);
  117. drawRect(_pauseSpacing / 2, -_pauseHeight / 2, (_pauseWidth - _pauseSpacing) / 2, _pauseHeight);
  118. endFill();
  119. }
  120. with (_hotspot.graphics)
  121. {
  122. clear();
  123. beginFill(0xFF0000, 0);
  124. drawRoundRect(-_hotspotBorder, -_hotspotBorder, _width + _hotspotBorder * 2, _height + _hotspotBorder * 2, _bgRadius + _hotspotBorder);
  125. endFill();
  126. }
  127. }
  128. private function addEvents():void
  129. {
  130. _video.addEventListener(VideoStreamEvent.PLAYING, onPlaying);
  131. _hotspot.addEventListener(ModMouseEvent.CLICK, onMouseClick);
  132. }
  133. private function addChildren():void
  134. {
  135. addBitmapChild(_bg);
  136. addBitmapChild(_symbols);
  137. _symbols.addChild(_pause);
  138. _symbols.addChild(_play);
  139. addChild(_hotspot);
  140. }
  141. private function onMouseClick(e:ModMouseEvent):void
  142. {
  143. _video.togglePause();
  144. }
  145. private function onPlaying(e:VideoStreamEvent):void
  146. {
  147. _play.visible = !_video.playing;
  148. _pause.visible = _video.playing;
  149. configDisplay();
  150. }
  151. /** @param value Sets the width of the play symbol. */
  152. public function set playWidth(value:uint):void
  153. {
  154. _playWidth = value;
  155. configDisplay();
  156. }
  157. /** @param value Sets the height of the play symbol. */
  158. public function set playHeight(value:uint):void
  159. {
  160. _playHeight = value;
  161. configDisplay();
  162. }
  163. /** @param value Sets the x offset of the play symbol. */
  164. public function set playOffsetX(value:Number):void
  165. {
  166. _playOffsetX = value;
  167. configDisplay();
  168. }
  169. /** @param value Sets the stroke thickness around the perimeter of the play symbol. */
  170. public function set playStroke(value:Number):void
  171. {
  172. _playStroke = value;
  173. configDisplay();
  174. }
  175. /** @param value Sets the width of the pause symbol. */
  176. public function set pauseWidth(value:uint):void
  177. {
  178. _pauseWidth = value;
  179. configDisplay();
  180. }
  181. /** @param value Sets the height of the pause symbol. */
  182. public function set pauseHeight(value:uint):void
  183. {
  184. _pauseHeight = value;
  185. configDisplay();
  186. }
  187. /** @param value Sets the spacing between the two rectangles of the pause symbol. */
  188. public function set pauseSpacing(value:uint):void
  189. {
  190. _pauseSpacing = value;
  191. configDisplay();
  192. }
  193. /** @param value Sets the stroke thickness around the perimeter of the pause symbol. */
  194. public function set pauseStroke(value:Number):void
  195. {
  196. _pauseStroke = value;
  197. configDisplay();
  198. }
  199. /** @param value Sets the fill colour value of the symbols. */
  200. public function set fillColour(value:uint):void
  201. {
  202. _fillColour = value;
  203. configDisplay();
  204. }
  205. /** @param value Sets the fill alpha value of the symbols. */
  206. public function set fillAlpha(value:Number):void
  207. {
  208. _fillAlpha = value;
  209. configDisplay();
  210. }
  211. /** @param value Sets the stroke colour value of the symbols. */
  212. public function set strokeColour(value:uint):void
  213. {
  214. _strokeColour = value;
  215. configDisplay();
  216. }
  217. /** @param value Sets the stroke alpha value of the symbols. */
  218. public function set strokeAlpha(value:Number):void
  219. {
  220. _strokeAlpha = value;
  221. configDisplay();
  222. }
  223. /** @param value Sets the radius value of the background rectangle. */
  224. public function set bgRadius(value:Number):void
  225. {
  226. _bgRadius = value;
  227. configDisplay();
  228. }
  229. /** @param value Sets the colour value of the background. */
  230. public function set bgColour(value:uint):void
  231. {
  232. _bgColour = value;
  233. configDisplay();
  234. }
  235. /** @param value Sets the alpha value of the background. */
  236. public function set bgAlpha(value:Number):void
  237. {
  238. _bgAlpha = value;
  239. configDisplay();
  240. }
  241. /** @param value Modifies the invisible padding around the perimeter of the unit to increase or decrease the hotspot surface area. */
  242. public function set hotspotBorder(value:uint):void
  243. {
  244. _hotspotBorder = value;
  245. configDisplay();
  246. }
  247. /** Returns the width of the play symbol. */
  248. public function get playWidth():uint
  249. {
  250. return _playWidth;
  251. }
  252. /** Returns the height of the play symbol. */
  253. public function get playHeight():uint
  254. {
  255. return _playHeight;
  256. }
  257. /** Returns the x offset of the play symbol. */
  258. public function get playOffsetX():Number
  259. {
  260. return _playOffsetX;
  261. }
  262. /** Returns the stroke thickness of the play symbol. */
  263. public function get playStroke():Number
  264. {
  265. return _playStroke;
  266. }
  267. /** Returns the width of the pause symbol. */
  268. public function get pauseWidth():uint
  269. {
  270. return _pauseWidth;
  271. }
  272. /** Returns the height of the pause symbol. */
  273. public function get pauseHeight():uint
  274. {
  275. return _pauseHeight;
  276. }
  277. /** Returns the spacing between the two rectangles of the pause symbol. */
  278. public function get pauseSpacing():uint
  279. {
  280. return _pauseSpacing;
  281. }
  282. /** Returns the stroke thickness of the pause symbol. */
  283. public function get pauseStroke():Number
  284. {
  285. return _pauseStroke;
  286. }
  287. /** Returns the fill colour of the symbols. */
  288. public function get fillColour():uint
  289. {
  290. return _fillColour;
  291. }
  292. /** Returns the fill alpha of the symbols. */
  293. public function get fillAlpha():Number
  294. {
  295. return _fillAlpha;
  296. }
  297. /** Returns the stroke colour of the symbols. */
  298. public function get strokeColour():uint
  299. {
  300. return _strokeColour;
  301. }
  302. /** Returns the stroke alpha of the symbols. */
  303. public function get strokeAlpha():Number
  304. {
  305. return _strokeAlpha;
  306. }
  307. /** Returns the radius of the background rectangle. */
  308. public function get bgRadius():Number
  309. {
  310. return _bgRadius;
  311. }
  312. /** Returns the colour value of the background. */
  313. public function get bgColour():uint
  314. {
  315. return _bgColour;
  316. }
  317. /** Returns the alpha value of the background. */
  318. public function get bgAlpha():Number
  319. {
  320. return _bgAlpha;
  321. }
  322. /** Returns the value of the invisible hotspot border. */
  323. public function get hotspotBorder():uint
  324. {
  325. return _hotspotBorder;
  326. }
  327. /** Returns the symbols Sprite. */
  328. public function get symbols():Sprite
  329. {
  330. return _symbols;
  331. }
  332. /** Returns the play Sprite. */
  333. public function get play():Sprite
  334. {
  335. return _play;
  336. }
  337. /** Returns the pause Sprite. */
  338. public function get pause():Sprite
  339. {
  340. return _pause;
  341. }
  342. /** Returns the background Sprite. */
  343. public function get bg():Sprite
  344. {
  345. return _bg;
  346. }
  347. }
  348. }