/MultiTouchFlashSDK_v1.2.2/src/ch/multitou/Slider.as
ActionScript | 133 lines | 81 code | 18 blank | 34 comment | 14 complexity | 9cec9d63b194f5dd7b4a8ef58b92fe5c MD5 | raw file
- package ch.multitou
- {
- import flash.display.DisplayObject;
- import flash.display.DisplayObjectContainer;
- import flash.display.InteractiveObject;
- import flash.display.Sprite;
- import flash.geom.Point;
- import flash.geom.Rectangle;
-
- /**
- * A simple slider class. Uses the supplied graphics and bounds.
- */
- public class Slider extends Sprite
- {
- private var base : DisplayObject;
- private var bounds : Rectangle;
- private var slider : InteractiveObject;
-
- private var grabPoint : Point;
-
- /**
- * Constructor. Uses the supplied base and slider graphics to build the slider, and saves the bounds for later use.
- * @param base:DisplayObjectContainer The base graphics for the slider
- * @param bounds:Rectangle The bounds for the slider to move within.
- * @param slider:InteractiveObject The slider graphics.
- */
- public function Slider(base : DisplayObjectContainer, bounds : Rectangle, slider : InteractiveObject)
- {
- this.base = base;
- this.bounds = bounds;
- this.slider = slider;
-
- addChild(base);
- base.addChild(slider);
-
- slider.addEventListener(MultiTouchEvent.DOWN, pressDown);
- base.addEventListener(MultiTouchEvent.DOWN, update);
- }
-
- /**
- * When a finger is introduced, try to own it. If successful, save the point of contact and start listening to MOVE and UP events.
- * @param e:MultiTouchEvent
- */
- private function pressDown(e : MultiTouchEvent) : void
- {
- if (e.manager.requestFingerOwnership(e.finger, slider))
- {
- grabPoint = new Point();
- grabPoint.x = MultiTouchEvent(e).localX;
- grabPoint.y = MultiTouchEvent(e).localY;
-
- slider.addEventListener(MultiTouchEvent.MOVE, update);
- slider.addEventListener(MultiTouchEvent.UP, pressUp);
- }
- }
-
- /**
- * When the finger is lifted, we remove the MOVE listener.
- * @param e:MultiTouchEvent
- */
- private function pressUp(e : MultiTouchEvent) : void
- {
- slider.removeEventListener(MultiTouchEvent.MOVE, update);
- grabPoint = null;
- }
-
- /**
- * When the finger is moved, move the slider to meet it, within the slider's bounds.
- * Send a MOVE SliderEvent with information on where the slider was moved.
- * @param e:MultiTouchEvent
- * @eventType ch.multitou.SliderEvent.MOVE
- */
- private function update(e : MultiTouchEvent) : void
- {
- var eventX : int;
- var eventY : int;
-
- eventX = MultiTouchEvent(e).stageX;
- eventY = MultiTouchEvent(e).stageY;
-
- var eventP : Point = new Point(eventX, eventY);
- eventP = base.globalToLocal(eventP);
-
- slider.x = eventP.x;
- slider.y = eventP.y;
- if (grabPoint!=null)
- {
- slider.x -= grabPoint.x;
- slider.y -= grabPoint.y;
- }
-
- // Cap the slider's movement to the bounds.
- if (slider.x<bounds.left) slider.x = bounds.left;
- if (slider.x>bounds.right-slider.width) slider.x = bounds.right-slider.width;
- if (slider.y<bounds.top) slider.y = bounds.top;
- if (slider.y>bounds.bottom-slider.height) slider.y = bounds.bottom-slider.height;
-
- var sliderPositionX : Number = slider.x;
- var sliderPositionY : Number = slider.y;
- var sliderRatioX : Number = (slider.x-bounds.left) / (bounds.right-bounds.left-slider.width);
- var sliderRatioY : Number = (slider.y-bounds.top) / (bounds.bottom-bounds.top-slider.height);
-
- dispatchEvent(new SliderEvent(SliderEvent.MOVE, false, false, sliderPositionX, sliderPositionY, sliderRatioX, sliderRatioY));
- }
-
- /**
- * Move the slider to the requested position.
- * @param positionX:Number The x coordinate to move to
- * @param positionY:Number The y coordinate to move to
- */
- public function moveToPosition(positionX : Number, positionY : Number) : void
- {
- if (positionX<bounds.left || positionX>bounds.right-slider.width || positionY<bounds.top || positionY>bounds.bottom-slider.height) throw ("Illegal slider position");
- slider.x = positionX;
- slider.y = positionY;
- }
-
- /**
- * Move the slider to the requested position supplied in percents.
- * @param percentX:Number The percent in x to move to
- * @param percentY:Number The percent in y to move to
- */
- public function moveToPercent(percentX : Number, percentY : Number) : void
- {
- if (percentX<0 || percentX>1 || percentY<0 || percentY>1) throw new Error("Illegal slider position");
-
- var positionX : Number = bounds.left + (bounds.right-bounds.left-slider.width)*percentX;
- var positionY : Number = bounds.top + (bounds.bottom-bounds.top-slider.height)*percentY;
- slider.x = positionX;
- slider.y = positionY;
- }
- }
- }