/src/org/osmf/player/chrome/widgets/Slider.as

https://github.com/aravindakumar/StrobeMediaPlayback · ActionScript · 229 lines · 155 code · 33 blank · 41 comment · 17 complexity · f951e88fe1cbc113a72a52a0edd307d7 MD5 · raw file

  1. /***********************************************************
  2. * Copyright 2010 Adobe Systems Incorporated. All Rights Reserved.
  3. *
  4. * *********************************************************
  5. * The contents of this file are subject to the Berkeley Software Distribution (BSD) Licence
  6. * (the "License"); you may not use this file except in
  7. * compliance with the License.
  8. *
  9. * Software distributed under the License is distributed on an "AS IS"
  10. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  11. * License for the specific language governing rights and limitations
  12. * under the License.
  13. *
  14. *
  15. * The Initial Developer of the Original Code is Adobe Systems Incorporated.
  16. * Portions created by Adobe Systems Incorporated are Copyright (C) 2010 Adobe Systems
  17. * Incorporated. All Rights Reserved.
  18. *
  19. **********************************************************/
  20. package org.osmf.player.chrome.widgets
  21. {
  22. import flash.display.DisplayObject;
  23. import flash.display.Sprite;
  24. import flash.events.MouseEvent;
  25. import flash.events.TimerEvent;
  26. import flash.geom.Rectangle;
  27. import flash.utils.Timer;
  28. import flashx.textLayout.formats.VerticalAlign;
  29. import org.osmf.player.chrome.events.ScrubberEvent;
  30. [Event(name="scrubStart", type="org.osmf.samples.controlbar.ScrubberEvent")]
  31. [Event(name="scrubUpdate", type="org.osmf.samples.controlbar.ScrubberEvent")]
  32. [Event(name="scrubEnd", type="org.osmf.samples.controlbar.ScrubberEvent")]
  33. /**
  34. * Slider class implements the behaviour of a slider and it is used by both the volume control and ScrubBar.
  35. */
  36. public class Slider extends Sprite
  37. {
  38. public function Slider(up:DisplayObject, down:DisplayObject, disabled:DisplayObject)
  39. {
  40. this.up = up;
  41. this.down = down;
  42. this.disabled = disabled;
  43. scrubTimer = new Timer(UPDATE_INTERVAL);
  44. scrubTimer.addEventListener(TimerEvent.TIMER, onDraggingTimer);
  45. updateFace(this.up);
  46. addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  47. super();
  48. }
  49. public function get sliding():Boolean
  50. {
  51. return _sliding;
  52. }
  53. public function set enabled(value:Boolean):void
  54. {
  55. if (value != _enabled)
  56. {
  57. _enabled = value;
  58. mouseEnabled = value;
  59. updateFace(_enabled ? up : disabled);
  60. }
  61. }
  62. public function set origin(value:Number):void
  63. {
  64. _origin = value;
  65. }
  66. public function get origin():Number
  67. {
  68. return _origin;
  69. }
  70. public function set rangeX(value:Number):void
  71. {
  72. _rangeX = value;
  73. }
  74. public function get rangeX():Number
  75. {
  76. return _rangeX;
  77. }
  78. public function set rangeY(value:Number):void
  79. {
  80. _rangeY = value;
  81. }
  82. public function get rangeY():Number
  83. {
  84. return _rangeY;
  85. }
  86. public function start(lockCenter:Boolean = true):void
  87. {
  88. if (_enabled && _sliding == false)
  89. {
  90. _sliding = true;
  91. stage.addEventListener(MouseEvent.MOUSE_UP, onStageExitDrag);
  92. updateFace(down);
  93. scrubTimer.start();
  94. dispatchEvent(new ScrubberEvent(ScrubberEvent.SCRUB_START));
  95. startDrag
  96. ( lockCenter
  97. , new Rectangle
  98. ( rangeY == 0.0 ? _origin : x
  99. , rangeX == 0.0 ? _origin : y
  100. , _rangeX
  101. , _rangeY
  102. )
  103. );
  104. // INJECTION Change 692020: extracting (height/2) from the Y range fixed ST-204(the slider is allowed to
  105. // be dragged to far south (the calculations not taking into account the actual height
  106. // of the scrubber itself))
  107. // but also caused the (ST-220) Control bar was injected in build 692020.
  108. // startDrag
  109. // ( lockCenter
  110. // , new Rectangle
  111. // ( rangeY == 0 ? _origin : x
  112. // , rangeX == 0 ? _origin : y
  113. // , _rangeX
  114. // , _rangeY - (height/2)
  115. // )
  116. // );
  117. }
  118. }
  119. public function stop():void
  120. {
  121. if (_enabled && _sliding)
  122. {
  123. scrubTimer.stop();
  124. stopDrag();
  125. updateFace(up);
  126. _sliding = false;
  127. try
  128. {
  129. stage.removeEventListener(MouseEvent.MOUSE_UP, onStageExitDrag);
  130. }
  131. catch (e:Error)
  132. {
  133. // swallow this, it means that we already removed
  134. // the event listened in a previous stop() call
  135. }
  136. dispatchEvent(new ScrubberEvent(ScrubberEvent.SCRUB_END));
  137. }
  138. }
  139. // Overrides
  140. //
  141. override public function set x(value:Number):void
  142. {
  143. if (_sliding == false)
  144. {
  145. super.x = value;
  146. }
  147. }
  148. override public function set y(value:Number):void
  149. {
  150. if (_sliding == false)
  151. {
  152. super.y = value;
  153. }
  154. }
  155. // Internals
  156. //
  157. private function updateFace(face:DisplayObject):void
  158. {
  159. if (currentFace != face)
  160. {
  161. if (currentFace)
  162. {
  163. removeChild(currentFace);
  164. }
  165. currentFace = face;
  166. if (currentFace)
  167. {
  168. addChild(currentFace);
  169. }
  170. }
  171. }
  172. private function onMouseDown(event:MouseEvent):void
  173. {
  174. start(false);
  175. }
  176. private function onStageExitDrag(event:MouseEvent):void
  177. {
  178. stop();
  179. }
  180. private function onDraggingTimer(event:TimerEvent):void
  181. {
  182. dispatchEvent(new ScrubberEvent(ScrubberEvent.SCRUB_UPDATE));
  183. }
  184. private const UPDATE_INTERVAL:int = 40
  185. private var currentFace:DisplayObject;
  186. private var up:DisplayObject;
  187. private var down:DisplayObject;
  188. private var disabled:DisplayObject;
  189. private var _enabled:Boolean = true;
  190. private var _origin:Number = 0.0;
  191. private var _rangeX:Number = 100.0;
  192. private var _rangeY:Number = 100.0;
  193. private var _sliding:Boolean;
  194. private var scrubTimer:Timer;
  195. }
  196. }