package com.meandmybadself.ui { import flash.display.* import flash.events.*; import org.casaframework.math.Percent; import org.casaframework.util.NumberUtil; public class Slider extends MovieClip{ public var $thumb:MovieClip; public var $thumbBar:MovieClip; public var $bg:MovieClip; private var $width:Number; public function Slider(_width:Number = 0) { $thumb.x = 0; $thumbBar.width = 0; $thumb.buttonMode = true; $thumb.addEventListener(MouseEvent.MOUSE_DOWN, $thumb_PRESS); $thumb.addEventListener(MouseEvent.MOUSE_UP, $thumb_RELEASE); $thumbBar.addEventListener(MouseEvent.MOUSE_DOWN, $thumb_PRESS); $thumbBar.addEventListener(MouseEvent.MOUSE_UP, $thumb_RELEASE); $thumbBar.buttonMode = true; $bg.addEventListener(MouseEvent.MOUSE_DOWN, $thumb_PRESS); $bg.addEventListener(MouseEvent.MOUSE_UP, $thumb_RELEASE); $bg.buttonMode = true; setWidth(_width); } public function setWidth(_width:Number):void { //keep $thumb's x percentage. var _tX:Number = $thumb.x / $bg.width; $bg.width = _width; if (isFinite(_tX)) { $thumbBar.width = $thumb.x = $bg.width * _tX; } else { $thumbBar.width = $thumb.x = 0; } } public function unlock():void { mouseEnabled = true; mouseChildren = true; } public function lock():void { mouseEnabled = false; mouseChildren = false; } internal function $thumb_PRESS(me:MouseEvent):void { stage.addEventListener(MouseEvent.MOUSE_UP, $thumb_RELEASE); addEventListener(MouseEvent.MOUSE_MOVE, $drag); stage.addEventListener(MouseEvent.MOUSE_MOVE, $drag); stage.addEventListener(MouseEvent.MOUSE_UP, $thumb_RELEASE); parent.addEventListener(MouseEvent.MOUSE_UP, $thumb_RELEASE); $drag(); } internal function $drag(e:Event = null):void { $thumbBar.width = $thumb.x = NumberUtil.makeBetween(mouseX, $bg.x, $bg.x + $bg.width); dispatchEvent(new Event(Event.CHANGE, true)); } internal function $thumb_RELEASE(me:MouseEvent):void { removeEventListener(MouseEvent.MOUSE_MOVE, $drag); stage.removeEventListener(MouseEvent.MOUSE_MOVE, $drag); stage.removeEventListener(MouseEvent.MOUSE_UP, $thumb_RELEASE); parent.removeEventListener(MouseEvent.MOUSE_UP, $thumb_RELEASE); } public function setValue(_per:Percent):void { var _valueNum:Number = NumberUtil.makeBetween(_per.decimalPercentage, 0, 1); $thumbBar.width = $thumb.x = ($bg.width) * _valueNum; } public function getValue():Percent { return new Percent($thumb.x / $bg.width); } } }