[AS3.0] 黒板とチョーク (3)

黒板とチョークみたいなのをAS3.0クラスで作ってみる。 :bouzu:

blackBoard3

:caution: 要 Flash Player 11.8 以上

This movie requires Flash Player 11.8.0

Main.as
package {

  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.display.Shape;
  import flash.display.BitmapData;
  import flash.display.Bitmap;
  import flash.geom.Point;
  import flash.utils.Dictionary;

  import controls.Btn;

  [SWF(backgroundColor="#FFFFFF", width="600", height="430", frameRate="30")]

  public class Main extends Sprite {
    // プロパティ
    private var content:BitmapData;
    private var bitmapData:BitmapData;
    private var seed:uint;
    private var color:uint;
    private static var point:Point = new Point();
    private var canvas:Sprite;
    private var position:Object = {x: 0, y: 0};
    private static var max:uint = 4;
    private var selectedId:uint = 0;
    private var colors:Array;
    private var seeds:Array;
    private var dictionary:Dictionary;
    private var chalks:Array;
    private var clearBtn:Btn;

    // コンストラクタ
    public function Main() {
      init();
    }

    // メソッド
    private function init():void {
      var board:Shape = new Shape();
      board.graphics.beginFill(0x005500);
      board.graphics.drawRect(0, 0, 600, 400);
      board.graphics.endFill();
      addChild(board);
      var base:Shape = new Shape();
      base.graphics.beginFill(0x996633);
      base.graphics.drawRect(0, 400, 600, 30);
      base.graphics.endFill();
      addChild(base);
      bitmapData = new BitmapData(600, 400, true, 0x00000000);
      content = new BitmapData(600, 400, true, 0x00000000);
      addChild(new Bitmap(content));
      colors = [0xFFFFFFFF, 0xFFFFFF66, 0xFFFFAACC, 0xFF66CCFF];
      seeds = [];
      for (var n:uint = 0; n < max; n++) {
        seeds.push(uint(Math.random()*10000));
      }
      canvas = new Sprite();
      color = colors[selectedId];
      seed = seeds[selectedId];
      setup();
      stage.addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
    }
    private function press(evt:MouseEvent):void {
      stage.removeEventListener(MouseEvent.MOUSE_DOWN, press);
      stage.addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
      position.x = stage.mouseX;
      position.y = stage.mouseY;
      addEventListener(Event.ENTER_FRAME, update, false, 0, true);
    }
    private function release(evt:Event):void {
      stage.removeEventListener(MouseEvent.MOUSE_UP, release);
      stage.addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
      removeEventListener(Event.ENTER_FRAME, update);
      canvas.graphics.clear();
    }
    private function update(evt:Event):void {
      var px:Number = stage.mouseX;
      var py:Number = stage.mouseY;
      canvas.graphics.lineStyle(10, 0xFFFFFF);
      canvas.graphics.moveTo(position.x, position.y);
      canvas.graphics.lineTo(px, py);

      position.x = px;
      position.y = py;
      bitmapData.lock();
      bitmapData.fillRect(bitmapData.rect, 0x00000000);
      bitmapData.draw(canvas);
      bitmapData.pixelDissolve(bitmapData, bitmapData.rect, point, seed, 600*400*0.4);
      bitmapData.threshold(bitmapData, bitmapData.rect, point, ">", 0x00000000, color, 0x00FFFFFF);
      bitmapData.unlock();
      content.lock();
      content.draw(bitmapData);
      content.unlock();
    }
    private function select(evt:MouseEvent):void {
      selectedId = dictionary[evt.target];
      color = colors[selectedId];
      seed = seeds[selectedId];
      for (var n:uint = 0; n < max; n++) {
        var chalk:Sprite = chalks[n];
        if (n == selectedId) {
          chalk.rotation = 10;
        } else {
          chalk.rotation = 0;
        }
      }
    }
    private function clear(evt:MouseEvent):void {
      bitmapData.lock();
      bitmapData.fillRect(bitmapData.rect, 0x00000000);
      bitmapData.unlock();
      content.lock();
      content.fillRect(content.rect, 0x00000000);
      content.unlock();
    }
    private function setup():void {
      var white:Sprite = new Sprite();
      white.graphics.beginFill(0xFFFFFF);
      white.graphics.drawRoundRect(-30, -6, 60, 12, 6, 6);
      white.graphics.endFill();
      addChild(white);
      white.x = 40;
      white.y = 415;
      white.addEventListener(MouseEvent.CLICK, select, false, 0, true);
      var yellow:Sprite = new Sprite();
      yellow.graphics.beginFill(0xFFFF66);
      yellow.graphics.drawRoundRect(-30, -6, 60, 12, 6, 6);
      yellow.graphics.endFill();
      addChild(yellow);
      yellow.x = 110;
      yellow.y = 415;
      yellow.addEventListener(MouseEvent.CLICK, select, false, 0, true);
      var pink:Sprite = new Sprite();
      pink.graphics.beginFill(0xFFAACC);
      pink.graphics.drawRoundRect(-30, -6, 60, 12, 6, 6);
      pink.graphics.endFill();
      addChild(pink);
      pink.x = 180;
      pink.y = 415;
      pink.addEventListener(MouseEvent.CLICK, select, false, 0, true);
      var blue:Sprite = new Sprite();
      blue.graphics.beginFill(0x66CCFF);
      blue.graphics.drawRoundRect(-30, -6, 60, 12, 6, 6);
      blue.graphics.endFill();
      addChild(blue);
      blue.x = 250;
      blue.y = 415;
      blue.addEventListener(MouseEvent.CLICK, select, false, 0, true);
      dictionary = new Dictionary(true);
      dictionary[white] = 0;
      dictionary[yellow] = 1;
      dictionary[pink] = 2;
      dictionary[blue] = 3;
      chalks = [white, yellow, pink, blue];
      white.rotation = 10;
      clearBtn = new Btn();
      addChild(clearBtn);
      clearBtn.init({label: "clear"});
      clearBtn.x = 560;
      clearBtn.y = 415;
      clearBtn.addEventListener(MouseEvent.CLICK, clear, false, 0, true);
    }

  }

}