package { import dw.IDisposable; import dw.tween.dTween; import dw.tween.props; import dw.tween.tint; import dw.ui.basic.MaskedInteractiveProgressEvent; import dw.ui.basic.UIEvent; import dw.utils.MultipurposeEvent; import fl.motion.easing.Quadratic; import flash.display.MovieClip; import flash.events.Event; import flash.events.MouseEvent; //PROVIDES THE LOGIC OF THE VOLUME CONTROL //ENCAPSULATES THE SLIDER AND THE ON-OFF BUTTON public class Volume extends MovieClip implements IDisposable { //VOLUME BUTTON public var volumeButton:VolumeButton; //VOLUME SLIDER public var volumeSlider:VolumeSlider; protected var _volumeOn:Boolean = true; protected var _oldAudibleVolume:Number =0.5; protected const _audibleTreshold:Number = 0.005; protected var _initVolume:Number = -1; protected var _initialized:Boolean = false; public function Volume() { super(); volumeSlider.addEventListener(MaskedInteractiveProgressEvent.UPDATE_PROGRESS, evthndlVolumeSlided); volumeSlider.addEventListener(UIEvent.INITIALIZED,evthndlInitCompleted); volumeSlider.addEventListener(MaskedInteractiveProgressEvent.UPDATE_STOPPED,evthndlStopVolumeSelection); volumeSlider.addEventListener(MaskedInteractiveProgressEvent.UPDATE_STARTED, evthndlStartVolumeSelection) volumeButton.addEventListener(MouseEvent.CLICK,evthndlVolumeButtonPushed); addEventListener(Event.ADDED_TO_STAGE, evthndlAddedToStage); } protected function evthndlAddedToStage(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE, evthndlAddedToStage); addEventListener(MouseEvent.ROLL_OVER, evthndlRollOver); addEventListener(MouseEvent.ROLL_OUT, evthndlRollOut); stage.addEventListener(Event.MOUSE_LEAVE, evthndlRollOut); } //CHANGE HERE THE TINT VALUE FOR ROLL OUT/NORMAL STATE protected function evthndlRollOut(e:Event):void { dTween(volumeButton["offAsset"]).ease(Quadratic.easeInOut).to(0.5,[new tint(0x5d5d5d,1)]); dTween(volumeSlider["offAsset"]).ease(Quadratic.easeInOut).to(0.5,[new tint(0x5d5d5d,1)]); } //CHANGE HERE THE TINT VALUE FOR ROLL OVER protected function evthndlRollOver(e:Event):void { dTween(volumeButton["offAsset"]).ease(Quadratic.easeInOut).to(0.5,[new tint(0x979797,1)]); dTween(volumeSlider["offAsset"]).ease(Quadratic.easeInOut).to(0.5,[new tint(0x979797,1)]); //d5d5d5 } protected function evthndlStartVolumeSelection(e:MaskedInteractiveProgressEvent):void { evthndlVolumeSlided(e); } protected function evthndlStopVolumeSelection(e:MaskedInteractiveProgressEvent):void { if(e.progress > _audibleTreshold) { _oldAudibleVolume = e.progress; } evthndlVolumeSlided(e); } //DEPENDING ON THE VOLUME, ALSO SETS THE ON OFF BUTTON protected function evthndlVolumeSlided(e:MaskedInteractiveProgressEvent):void { if(e.progress < _audibleTreshold && _volumeOn) { dTween(volumeButton["onAsset"]).ease(Quadratic.easeIn).to(0.2,[new props({alpha:0})]); _volumeOn = false; } else if(e.progress > _audibleTreshold && !_volumeOn){ dTween(volumeButton["onAsset"]).ease(Quadratic.easeOut).to(0.2,[new props({alpha:1})]); _volumeOn = true; } updateVolume(e.progress); } //ALSO RESET THE SLIDER VALUE protected function evthndlVolumeButtonPushed(e:MouseEvent):void { if(volumeSlider.progress > _audibleTreshold) { volumeSlider.progress = 0; dTween(volumeButton["onAsset"]).ease(Quadratic.easeOut).to(0.2,[new props({alpha:0})]); } else { volumeSlider.progress = _oldAudibleVolume; dTween(volumeButton["onAsset"]).ease(Quadratic.easeOut).to(0.2,[new props({alpha:1})]); } } protected function updateVolume(amount:Number):void { dispatchEvent(new MultipurposeEvent("volume",false,false,this,amount)); } protected function evthndlInitCompleted(e:UIEvent):void { _initialized = true; if(_initVolume != -1) { setVolume(_initVolume); } } public function setVolume(v:Number):void { if(!_initialized) { _initVolume = v; } else { volumeSlider.progress = v; } } public function dispose():void { volumeSlider.removeEventListener(MaskedInteractiveProgressEvent.UPDATE_STOPPED,evthndlStopVolumeSelection); volumeSlider.removeEventListener(MaskedInteractiveProgressEvent.UPDATE_STARTED,evthndlStartVolumeSelection); volumeSlider.removeEventListener(MaskedInteractiveProgressEvent.UPDATE_PROGRESS, evthndlVolumeSlided); volumeSlider.removeEventListener(UIEvent.INITIALIZED,evthndlInitCompleted); volumeButton.removeEventListener(MouseEvent.CLICK,evthndlVolumeButtonPushed); //volumeButton.dispose(); volumeSlider.dispose(); removeEventListener(Event.ADDED_TO_STAGE, evthndlAddedToStage); removeEventListener(MouseEvent.ROLL_OVER, evthndlRollOver); removeEventListener(MouseEvent.ROLL_OUT, evthndlRollOut); stage.removeEventListener(Event.MOUSE_LEAVE, evthndlRollOut); } } }