/MultiTouchFlashSDK_v1.2.2/src/ch/multitou/Slider.as

https://github.com/dmwallace/STP-Simulation
ActionScript | 133 lines | 81 code | 18 blank | 34 comment | 14 complexity | 9cec9d63b194f5dd7b4a8ef58b92fe5c MD5 | raw file
  1. package ch.multitou
  2. {
  3. import flash.display.DisplayObject;
  4. import flash.display.DisplayObjectContainer;
  5. import flash.display.InteractiveObject;
  6. import flash.display.Sprite;
  7. import flash.geom.Point;
  8. import flash.geom.Rectangle;
  9. /**
  10. * A simple slider class. Uses the supplied graphics and bounds.
  11. */
  12. public class Slider extends Sprite
  13. {
  14. private var base : DisplayObject;
  15. private var bounds : Rectangle;
  16. private var slider : InteractiveObject;
  17. private var grabPoint : Point;
  18. /**
  19. * Constructor. Uses the supplied base and slider graphics to build the slider, and saves the bounds for later use.
  20. * @param base:DisplayObjectContainer The base graphics for the slider
  21. * @param bounds:Rectangle The bounds for the slider to move within.
  22. * @param slider:InteractiveObject The slider graphics.
  23. */
  24. public function Slider(base : DisplayObjectContainer, bounds : Rectangle, slider : InteractiveObject)
  25. {
  26. this.base = base;
  27. this.bounds = bounds;
  28. this.slider = slider;
  29. addChild(base);
  30. base.addChild(slider);
  31. slider.addEventListener(MultiTouchEvent.DOWN, pressDown);
  32. base.addEventListener(MultiTouchEvent.DOWN, update);
  33. }
  34. /**
  35. * When a finger is introduced, try to own it. If successful, save the point of contact and start listening to MOVE and UP events.
  36. * @param e:MultiTouchEvent
  37. */
  38. private function pressDown(e : MultiTouchEvent) : void
  39. {
  40. if (e.manager.requestFingerOwnership(e.finger, slider))
  41. {
  42. grabPoint = new Point();
  43. grabPoint.x = MultiTouchEvent(e).localX;
  44. grabPoint.y = MultiTouchEvent(e).localY;
  45. slider.addEventListener(MultiTouchEvent.MOVE, update);
  46. slider.addEventListener(MultiTouchEvent.UP, pressUp);
  47. }
  48. }
  49. /**
  50. * When the finger is lifted, we remove the MOVE listener.
  51. * @param e:MultiTouchEvent
  52. */
  53. private function pressUp(e : MultiTouchEvent) : void
  54. {
  55. slider.removeEventListener(MultiTouchEvent.MOVE, update);
  56. grabPoint = null;
  57. }
  58. /**
  59. * When the finger is moved, move the slider to meet it, within the slider's bounds.
  60. * Send a MOVE SliderEvent with information on where the slider was moved.
  61. * @param e:MultiTouchEvent
  62. * @eventType ch.multitou.SliderEvent.MOVE
  63. */
  64. private function update(e : MultiTouchEvent) : void
  65. {
  66. var eventX : int;
  67. var eventY : int;
  68. eventX = MultiTouchEvent(e).stageX;
  69. eventY = MultiTouchEvent(e).stageY;
  70. var eventP : Point = new Point(eventX, eventY);
  71. eventP = base.globalToLocal(eventP);
  72. slider.x = eventP.x;
  73. slider.y = eventP.y;
  74. if (grabPoint!=null)
  75. {
  76. slider.x -= grabPoint.x;
  77. slider.y -= grabPoint.y;
  78. }
  79. // Cap the slider's movement to the bounds.
  80. if (slider.x<bounds.left) slider.x = bounds.left;
  81. if (slider.x>bounds.right-slider.width) slider.x = bounds.right-slider.width;
  82. if (slider.y<bounds.top) slider.y = bounds.top;
  83. if (slider.y>bounds.bottom-slider.height) slider.y = bounds.bottom-slider.height;
  84. var sliderPositionX : Number = slider.x;
  85. var sliderPositionY : Number = slider.y;
  86. var sliderRatioX : Number = (slider.x-bounds.left) / (bounds.right-bounds.left-slider.width);
  87. var sliderRatioY : Number = (slider.y-bounds.top) / (bounds.bottom-bounds.top-slider.height);
  88. dispatchEvent(new SliderEvent(SliderEvent.MOVE, false, false, sliderPositionX, sliderPositionY, sliderRatioX, sliderRatioY));
  89. }
  90. /**
  91. * Move the slider to the requested position.
  92. * @param positionX:Number The x coordinate to move to
  93. * @param positionY:Number The y coordinate to move to
  94. */
  95. public function moveToPosition(positionX : Number, positionY : Number) : void
  96. {
  97. if (positionX<bounds.left || positionX>bounds.right-slider.width || positionY<bounds.top || positionY>bounds.bottom-slider.height) throw ("Illegal slider position");
  98. slider.x = positionX;
  99. slider.y = positionY;
  100. }
  101. /**
  102. * Move the slider to the requested position supplied in percents.
  103. * @param percentX:Number The percent in x to move to
  104. * @param percentY:Number The percent in y to move to
  105. */
  106. public function moveToPercent(percentX : Number, percentY : Number) : void
  107. {
  108. if (percentX<0 || percentX>1 || percentY<0 || percentY>1) throw new Error("Illegal slider position");
  109. var positionX : Number = bounds.left + (bounds.right-bounds.left-slider.width)*percentX;
  110. var positionY : Number = bounds.top + (bounds.bottom-bounds.top-slider.height)*percentY;
  111. slider.x = positionX;
  112. slider.y = positionY;
  113. }
  114. }
  115. }