/src/com/netoleal/common/Scroller.as

https://github.com/netoleal/com.netoleal
ActionScript | 271 lines | 193 code | 72 blank | 6 comment | 36 complexity | 45330a6400ada56555b2179f8b3e2e1a MD5 | raw file
  1. /**
  2. * ...
  3. * @author Default
  4. * @version 0.1
  5. */
  6. package com.netoleal.common {
  7. import com.netoleal.events.ScrollerEvent;
  8. import flash.display.DisplayObject;
  9. import flash.display.Sprite;
  10. import flash.display.Stage;
  11. import flash.errors.IllegalOperationError;
  12. import flash.events.Event;
  13. import flash.events.EventDispatcher;
  14. import flash.events.MouseEvent;
  15. import flash.events.TimerEvent;
  16. import flash.utils.Timer;
  17. [Event(name="scroll", type="com.netoleal.events.ScrollerEvent")]
  18. [Event(name="stopScrolling", type="com.netoleal.events.ScrollerEvent")]
  19. [Event(name="startScrolling", type="com.netoleal.events.ScrollerEvent")]
  20. public class Scroller extends EventDispatcher {
  21. private var _tracker:Sprite;
  22. private var _track:DisplayObject;
  23. private var _minusButton:DisplayObject;
  24. private var _plusButton:DisplayObject;
  25. private var _dragging:Boolean;
  26. private var _direction:uint;
  27. private var _yDif:Number;
  28. private var _xDif:Number;
  29. private const MIN:uint = 0;
  30. private const MAX:uint = 1;
  31. private const STEP:Number = 5;
  32. private var timer:Timer;
  33. private const delay:Number = 700;
  34. private var _stage:Stage;
  35. public function Scroller( stage:Stage, tracker:Sprite, track:DisplayObject, minusButton:DisplayObject = null, plusButton:DisplayObject = null, direction:uint = 0 ) {
  36. _track = track;
  37. _tracker = tracker;
  38. _minusButton = minusButton;
  39. _plusButton = plusButton;
  40. _direction = direction;
  41. _tracker.addEventListener( MouseEvent.MOUSE_DOWN, this.startDragging );
  42. _stage = stage;
  43. //stage.addEventListener( MouseEvent.MOUSE_UP, this.stopDragging );
  44. if( _minusButton ) {
  45. _minusButton.addEventListener( MouseEvent.MOUSE_DOWN, minusDown );
  46. _minusButton.addEventListener( MouseEvent.MOUSE_UP, minusUp );
  47. }
  48. if( _plusButton ) {
  49. _plusButton.addEventListener( MouseEvent.MOUSE_DOWN, plusDown );
  50. _plusButton.addEventListener( MouseEvent.MOUSE_UP, plusUp );
  51. }
  52. timer = new Timer( delay, 1 );
  53. }
  54. public function dispose( ):void
  55. {
  56. if( _minusButton ) {
  57. _minusButton.removeEventListener( MouseEvent.MOUSE_DOWN, minusDown );
  58. _minusButton.removeEventListener( MouseEvent.MOUSE_UP, minusUp );
  59. }
  60. if( _plusButton ) {
  61. _plusButton.removeEventListener( MouseEvent.MOUSE_DOWN, plusDown );
  62. _plusButton.removeEventListener( MouseEvent.MOUSE_UP, plusUp );
  63. }
  64. _tracker.removeEventListener( MouseEvent.MOUSE_DOWN, this.startDragging );
  65. _stage.removeEventListener( MouseEvent.MOUSE_UP, this.stopDragging );
  66. _stage = null;
  67. _minusButton = null;
  68. _plusButton = null;
  69. _track = null;
  70. _tracker = null;
  71. }
  72. private function minusDown( evt:MouseEvent = null ):void {
  73. scrollUp( );
  74. stopTimer( );
  75. timer.addEventListener( TimerEvent.TIMER, this.startScrollingDown );
  76. timer.start( );
  77. }
  78. private function minusUp( evt:MouseEvent = null ):void {
  79. stopTimer( );
  80. stopScrolling( );
  81. }
  82. private function plusDown( evt:MouseEvent = null ):void {
  83. scrollDown( );
  84. stopTimer( );
  85. timer.addEventListener( TimerEvent.TIMER, this.startScrollingUp );
  86. timer.start( );
  87. }
  88. private function plusUp( evt:MouseEvent = null ):void {
  89. stopTimer( );
  90. stopScrolling( );
  91. }
  92. private function stopScrolling( ):void {
  93. _tracker.removeEventListener( Event.ENTER_FRAME, scrollDown );
  94. _tracker.removeEventListener( Event.ENTER_FRAME, scrollUp );
  95. }
  96. private function stopTimer( ):void {
  97. if( timer.running ) timer.stop( );
  98. timer.removeEventListener( TimerEvent.TIMER, this.startScrollingUp );
  99. timer.removeEventListener( TimerEvent.TIMER, this.startScrollingDown );
  100. }
  101. private function startScrollingDown( evt:TimerEvent = null ):void {
  102. _tracker.addEventListener( Event.ENTER_FRAME, this.scrollUp );
  103. }
  104. private function startScrollingUp( evt:TimerEvent = null ):void {
  105. _tracker.addEventListener( Event.ENTER_FRAME, this.scrollDown );
  106. }
  107. private function scrollUp( evt:Event = null ):void {
  108. if( _direction == ScrollerDirection.VERTICAL ) {
  109. setTrackerY( _tracker.y - STEP );
  110. } else if ( _direction == ScrollerDirection.HORIZONTAL ) {
  111. setTrackerX( _tracker.x - STEP );
  112. } else {
  113. throw new IllegalOperationError( "Illegal value for DIRECTION. Must be ScrollerDirection.VERTICAL or ScrollerDirection.HORIZONTAL" );
  114. }
  115. }
  116. private function scrollDown( evt:Event = null ):void {
  117. if( _direction == ScrollerDirection.VERTICAL ) {
  118. setTrackerY( _tracker.y + STEP );
  119. } else if ( _direction == ScrollerDirection.HORIZONTAL ) {
  120. setTrackerX( _tracker.x + STEP );
  121. } else {
  122. throw new IllegalOperationError( "Illegal value for DIRECTION. Must be ScrollerDirection.VERTICAL or ScrollerDirection.HORIZONTAL" );
  123. }
  124. }
  125. private function setTrackerY( value:Number ):void {
  126. _tracker.y = Math.max( _track.y, Math.min( _track.y + _track.height - _tracker.height, value ) );
  127. this.dispatchEvent( new ScrollerEvent( ScrollerEvent.SCROLL ) );
  128. }
  129. private function setTrackerX( value:Number ):void {
  130. _tracker.x = Math.max( _track.x, Math.min( _track.x + _track.width - _tracker.width, value ) );
  131. this.dispatchEvent( new ScrollerEvent( ScrollerEvent.SCROLL ) );
  132. }
  133. private function startDragging( evt:MouseEvent = null ):void {
  134. _dragging = true;
  135. _yDif = _tracker.parent.mouseY - _tracker.y;
  136. _xDif = _tracker.parent.mouseX - _tracker.x;
  137. _stage.addEventListener( MouseEvent.MOUSE_UP, this.stopDragging );
  138. _stage.addEventListener( MouseEvent.MOUSE_MOVE, this.doDragging );
  139. this.dispatchEvent( new ScrollerEvent( ScrollerEvent.START_SCROLLING ) );
  140. }
  141. private function doDragging( evt:MouseEvent ):void {
  142. if( _direction == ScrollerDirection.VERTICAL ) {
  143. setTrackerY( _tracker.parent.mouseY - _yDif );
  144. } else if ( _direction == ScrollerDirection.HORIZONTAL ) {
  145. setTrackerX( _tracker.parent.mouseX - _xDif );
  146. } else {
  147. throw new IllegalOperationError( "Illegal value for DIRECTION. Must be ScrollerDirection.VERTICAL or ScrollerDirection.HORIZONTAL" );
  148. }
  149. evt.updateAfterEvent( );
  150. }
  151. public function get value( ):Number
  152. {
  153. var result:Number;
  154. if( _direction == ScrollerDirection.VERTICAL )
  155. {
  156. result = ( ( _tracker.y - _track.y ) / ( _track.height - _tracker.height ) * ( MAX - MIN ) ) + MIN ;
  157. return isNaN( result )? 0: result;
  158. }
  159. else if ( _direction == ScrollerDirection.HORIZONTAL )
  160. {
  161. result = ( ( _tracker.x - _track.x ) / ( _track.width - _tracker.width ) * ( MAX - MIN ) ) + MIN ;
  162. return isNaN( result )? 0: result;
  163. }
  164. else
  165. {
  166. throw new IllegalOperationError( "Illegal value for DIRECTION. Must be ScrollerDirection.VERTICAL or ScrollerDirection.HORIZONTAL" );
  167. }
  168. }
  169. public function set value( v:Number ):void
  170. {
  171. var p:Number;
  172. var range:Number;
  173. v -= MIN;
  174. p = v / ( MAX - MIN );
  175. if( isNaN( v ) ) return;
  176. if( _direction == ScrollerDirection.VERTICAL )
  177. {
  178. range = _track.height - _tracker.height;
  179. _tracker.y = ( p * range ) + _track.y;
  180. }
  181. else if ( _direction == ScrollerDirection.HORIZONTAL )
  182. {
  183. range = _track.width - _tracker.width;
  184. _tracker.x = ( p * range ) + _track.x;
  185. }
  186. else
  187. {
  188. throw new IllegalOperationError( "Illegal value for DIRECTION. Must be ScrollerDirection.VERTICAL or ScrollerDirection.HORIZONTAL" );
  189. }
  190. this.dispatchEvent( new ScrollerEvent( ScrollerEvent.SCROLL ) );
  191. }
  192. private function stopDragging( evt:MouseEvent = null ):void {
  193. _dragging = false;
  194. _stage.removeEventListener( MouseEvent.MOUSE_MOVE, this.doDragging );
  195. _stage.removeEventListener( MouseEvent.MOUSE_UP, this.stopDragging );
  196. this.dispatchEvent( new ScrollerEvent( ScrollerEvent.STOP_SCROLLING ) );
  197. }
  198. }
  199. }