package {
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;

    [SWF(width="360", height="320", backgroundColor="#ffffff", frameRate="15")]

    public class GrayScale extends CameraFramework
    {
        public static const
            VIDEO_WIDTH:int = 320,          ///< ビデオの幅
            VIDEO_HEIGHT:int = 240,         ///< ビデオの高さ
            SCREEN_WIDTH:int = 360,         ///< 画面の幅
            SCREEN_HEIGHT:int = 320;        ///< 画面の高さ

        public static const
            APP_NONE:int = 0,               ///< エフェクト無し
            APP_AVG_GRAY:int = 1,           ///< RGBを1/3して平均をとってグレースケール化
            APP_WEIGHT_GRAY:int = 2;        ///< RGBの重さからグレースケール化

        private var appMode:int = APP_WEIGHT_GRAY;      ///< アプリケーションモード

        public function GrayScale()
        {
            super( SCREEN_WIDTH, SCREEN_HEIGHT, VIDEO_WIDTH, VIDEO_HEIGHT );

            /// 表示文字設定
            label.text = "Gray Scale Camera";

            // ボタン追加
            var buttonY:int = 10, buttonW:int = 100;
            var rgbGrayButton:TextButton = new TextButton(buttonW, 20, "RGB平均");
            rgbGrayButton.x = 10;
            rgbGrayButton.y = buttonY;
            addChild(rgbGrayButton);
            rgbGrayButton.addEventListener( MouseEvent.CLICK, rgbGrayButtonClick );

            var weightAvgButton:TextButton = new TextButton(buttonW, 20, "重み付き平均");
            weightAvgButton.x = rgbGrayButton.x + rgbGrayButton.width + 10;
            weightAvgButton.y = buttonY;
            addChild(weightAvgButton);
            weightAvgButton.addEventListener( MouseEvent.CLICK, weightAvgButtonClick );

            var noneButton:TextButton = new TextButton(buttonW, 20, "NONE");
            noneButton.x = weightAvgButton.x + weightAvgButton.width + 10;
            noneButton.y = buttonY;
            addChild(noneButton);
            noneButton.addEventListener( MouseEvent.CLICK, noneButtonClick );
        }

        /// rgbGrayButton 処理
        private function rgbGrayButtonClick(event:MouseEvent):void {
            appMode = APP_AVG_GRAY;
        }

        /// weightAvgButton 処理
        private function weightAvgButtonClick(event:MouseEvent):void {
            appMode = APP_WEIGHT_GRAY;
        }

        /// noneButton 処理
        private function noneButtonClick(event:MouseEvent):void {
            appMode = APP_NONE;
        }

        /// 描画処理
        public override function draw():void {

            if( appMode == APP_NONE ){
                bd.draw(bdTmp);
                return;
            }

            // グレースケール化
            var cmf:ColorMatrixFilter;

            switch(appMode){
            case APP_AVG_GRAY:
                // RGBを1/3づつ平均をとる
                cmf = new ColorMatrixFilter(
                    [1 / 3, 1 / 3, 1 / 3, 0, 0,
                     1 / 3, 1 / 3, 1 / 3, 0, 0,
                     1 / 3, 1 / 3, 1 / 3, 0, 0]
                );
                break;
            case APP_WEIGHT_GRAY:
                // 重み付き平均をとる
                cmf = new ColorMatrixFilter(
                    [0.299, 0.587, 0.114, 0, 0,
                     0.299, 0.587, 0.114, 0, 0,
                     0.299, 0.587, 0.114, 0, 0]
                );
                break;
            }

            bd.applyFilter(bdTmp, bdTmp.rect, new Point(), cmf);
        }

    }
}