// mxmlc -output ./bin/RotButton.swf -benchmark=true -default-size 40 40 -default-frame-rate=60 RotButton.as

package {
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;
    import flash.utils.*;
    import flash.filters.*;

    [SWF(backgroundColor=0x00000000, width=40, height=40)]

    public class RotButton extends Sprite {

        private var outRect:Shape;
        private var inRect:Shape;
        private var base:Sprite;
        private var label:TextField;

        private var glow:Number = 0;
        private var state:uint = 0;
        private static const
            S_UP:uint = 0,
            S_DOWN:uint = 1,
            S_OVER:uint = 2,
            WIDTH:Number = 40,
            HEIGHT:Number = 40,
            IN_W:Number = 15,
            IN_H:Number = 15;

        private var
            sx:Number,
            sy:Number,
            sw:Number,
            sh:Number;

        public function RotButton() {

            base = new Sprite();
            base.graphics.beginFill(0x000000);
            base.graphics.drawRect(0,0,40,40);
            base.graphics.endFill();
            base.buttonMode = true;
            addChild(base);

            label=new TextField();
            label.autoSize=TextFieldAutoSize.LEFT;
            label.textColor = 0xffffff;
            addChild(label);

            outRect = makeRect(WIDTH, HEIGHT, 0x000000, 0xffffff);
            base.addChild(outRect);

            inRect = makeRect(WIDTH, HEIGHT, 0x00000000, 0xffffff);
            base.addChild(inRect);
            sx = inRect.x;
            sy = inRect.y;
            sw = inRect.width;
            sh = inRect.height;

            //イベントリスナーの追加
            base.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
            base.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
            base.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler);
            base.addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler);

            //タイマーの追加
            var timer:Timer=new Timer(33,0);
            timer.addEventListener(TimerEvent.TIMER,onTick);
            timer.start();

            setState(S_UP);
        }

        private function setState( newstate:uint ):void {

            switch(newstate){
            case S_UP:
                inRect.width = WIDTH;
                inRect.height = HEIGHT;
                break;
            case S_DOWN:
                inRect.width = IN_W;
                inRect.height = IN_H;
                break;
            case S_OVER:
                inRect.width = IN_W;
                inRect.height = IN_H;
                glow = 0;
                break;
            }

            state = newstate;

            setInRect();
        }

        //マウスダウンイベントの処理
        private function mouseDownHandler(evt:MouseEvent):void {
            setState(S_DOWN);
        }

        //マウスアップイベントの処理
        private function mouseUpHandler(evt:MouseEvent):void {
            setState(S_UP);
        }

        //マウスオーバーイベントの処理
        private function mouseOverHandler(evt:MouseEvent):void {
            setState(S_OVER);
        }

        private function mouseOutHandler(evt:MouseEvent):void {
            setState(S_UP);
        }

        private function setInRect():void{
            inRect.x = (sw - inRect.width)/2 + sx;
            inRect.y = (sh - inRect.height)/2 + sy;
        }
        //タイマーイベントの処理
        private function onTick(evt:TimerEvent):void {

            var a:Number = 3;
            switch(state){
            case S_UP:
                inRect.width -= a;
                inRect.height -= a;
                if( inRect.width < IN_W ){
                    inRect.width = IN_W;
                }
                if( inRect.height < IN_H ){
                    inRect.height = IN_H;
                }
                break;
            case S_DOWN:
            case S_OVER:
                inRect.width += a;
                inRect.height += a;
                if( inRect.width > WIDTH ){
                    inRect.width = WIDTH;
                }
                if( inRect.height > HEIGHT ){
                    inRect.height = HEIGHT;
                }
                break;
            }

            var filter:Array = new Array();
            switch(state){
            case S_UP:
            case S_DOWN:
                outRect.filters = filter;
                inRect.filters = filter;
                break;
            case S_OVER:
                filter.push(new GlowFilter(0xFFFFFF, glow, 20,20, 2, BitmapFilterQuality.HIGH, false, false));
                outRect.filters = filter;
                inRect.filters = filter;
                glow += 0.1;
                if( glow > 0.8 ){
                    glow = 0.8;
                }
                break;
            }

            setInRect();

        }

        //矩形の生成
        private function makeRect(w:Number,h:Number,color:uint, lineColor:uint):Shape {
            var rect:Shape=new Shape();
            rect.graphics.lineStyle(2,lineColor);//線幅・線色
            //rect.graphics.beginFill(color);     //塗り潰し色
            rect.graphics.drawRect(0,0,w,h);    //XY座標,幅,高さ
            rect.graphics.endFill();            //塗り潰し終了
            return rect;
        }

    }
}