package net.xinterface.items { import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.media.Sound; public class ItemButton extends Sprite { public static const OVER:String = "over"; public static const RELEASE:String = "release"; public static const OUT:String = "out"; public static const SELECTED:String = "selected"; public static const UNSELECTED:String = "unselected"; public static const ENABLED:String = "enabled"; public static const DISABLED:String = "disabled"; public static const NOSTATE:String = "nostate"; private var id:String; protected var hit_mc:MovieClip; public var asset:MovieClip; private var volume:Number = 1; private var sClick:Sound; private var sOver:Sound; private var sOut:Sound; private var state:String; // constructor public function ItemButton(_asset:MovieClip, soundClick:Sound=null, soundOver:Sound=null, soundOut:Sound=null) { asset = _asset; addChild(asset); if(MovieClip(asset.hit_mc) != null) { hit_mc = MovieClip(asset.hit_mc); }else { hit_mc = new MovieClip; hit_mc.graphics.beginFill(0x000000,0); hit_mc.graphics.drawRect(0,0,asset.width, asset.height); asset.addChild(hit_mc); } hit_mc.buttonMode = true; sClick = soundClick; sOver = soundOver; sOut = soundOut; state = NOSTATE; Enable(); } // Habilita el botón (se puede pulsar) public function Enable():void { hit_mc.enabled = true; hit_mc.mouseEnabled = true; hit_mc.addEventListener(MouseEvent.CLICK, Click); hit_mc.addEventListener(MouseEvent.MOUSE_OVER, Over); hit_mc.addEventListener(MouseEvent.MOUSE_OUT, Out); asset.alpha = 1; state = ENABLED; } // Deshabilita el botón ( no se puede pulsar ) /** @param setAlpha true para bajar el alpha al deshabilitarlo, false para dejarlo tal cuál * @param keepClick true para que el botón siga funcionando incluso al deshabilitarlo, false para un comportamiento normal (esto me ha servido el alguna ocasión * */ public function Disable(setAlpha:Boolean=true, keepClick:Boolean=false):void { hit_mc.enabled = false; //hit_mc.mouseChildren = false; hit_mc.mouseEnabled = false; if(keepClick==true) hit_mc.mouseEnabled = true; else hit_mc.removeEventListener(MouseEvent.CLICK, Click); hit_mc.removeEventListener(MouseEvent.MOUSE_OVER, Over); hit_mc.removeEventListener(MouseEvent.MOUSE_OUT, Out); if(setAlpha) SetDisableView(); state = DISABLED; } protected function SetDisableView():void { asset.alpha = 0.3; } // Esto es público por si acaso queremos presionar un botón manualmente. "boton.Click(null);" public function Click(evt:MouseEvent=null):void { var event:Event = new Event(RELEASE); dispatchEvent(event); if(sClick!=null) sClick.play().soundTransform.volume = volume; state = RELEASE; } protected function Over(evt:MouseEvent=null):void { //trace("over"); dispatchEvent(new Event(OVER)); asset.removeEventListener(Event.ENTER_FRAME, TimelineBackard); asset.addEventListener(Event.ENTER_FRAME, TimelineFordward); if(sOver!=null) sOver.play().soundTransform.volume = volume; state = OVER; } protected function Out(evt:MouseEvent=null):void { //trace("out"); dispatchEvent(new Event(OUT)); asset.removeEventListener(Event.ENTER_FRAME, TimelineFordward); asset.addEventListener(Event.ENTER_FRAME, TimelineBackard); if(sOut!=null) sOut.play().soundTransform.volume = volume; state = OUT; } // Este método es perfecto para botoneras, al llamarlo, el botón de quedará en el estado "over", // y no se podrá pulsar hasta después de llamar al método "Unselect();" public function Select():void { if(state == SELECTED) return; Disable(false); dispatchEvent(new Event(SELECTED)); Over(); state = SELECTED; //trace("selected"); } // vuelve a dejar el botón como estaba public function Unselect():void { if(state == UNSELECTED) return; Enable(); dispatchEvent(new Event(UNSELECTED)); Out(); state = UNSELECTED; //trace("unselected"); } // Timeline // Hace correr la línea de tiempo del "asset" hasta el último frame private function TimelineFordward(evt:Event):void { asset.gotoAndStop(asset.currentFrame + 1); if(asset.currentFrame == asset.totalFrames) asset.removeEventListener(Event.ENTER_FRAME, TimelineFordward); } // Hace correr hacia el frame 1, la línea de tiempo del "asset" private function TimelineBackard(evt:Event):void { asset.gotoAndStop(asset.currentFrame - 1); if(asset.currentFrame == 1) asset.removeEventListener(Event.ENTER_FRAME, TimelineBackard); } // getters y setters public function get State():String { return state; } public function set Volume(value:Number):void { volume = value; } public function get Volume():Number { return volume; } public function get Id():String { return id; } public function set Id(pId:String):void { id = pId; } } }