コンテキストメニューで色を変えてみる
// forked from munegon's code on 2008-12-18
// write as3 code here..
package {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
public class DrawApp extends Sprite {
private var canvas_bd:BitmapData;
private var canvas_sp:Sprite;
private var index:int;
private var color:int;
private var colors:Array = [ 0x000000, 0xcc0000, 0xcccc00, 0x00cc00, 0x00cccc, 0x0000cc, 0xcc00cc ];
public function DrawApp() {
addEventListener( Event.ADDED, added );
}
private function added( e:Event ):void {
if ( e.target == this ) {
removeEventListener( e.type, arguments.callee );
setup();
}
}
private function setup():void {
canvas_bd = new BitmapData( stage.stageWidth, stage.stageHeight, false, 0xffffff );
addChild( new Bitmap( canvas_bd ) );
canvas_sp = new Sprite();
addChild( canvas_sp );
index = 0;
color = colors[ index ];
stage.addEventListener( MouseEvent.MOUSE_DOWN, startDraw );
stage.addEventListener( KeyboardEvent.KEY_UP, keyUpHandler );
var menu:ContextMenu = new ContextMenu();
menu.hideBuiltInItems();
menu.customItems = createContextMenuItems();
contextMenu = menu;
contextMenu.customItems[0].dispatchEvent( new ContextMenuEvent(ContextMenuEvent.MENU_ITEM_SELECT) );
}
private static const BLACK:String = "黒";
private static const RED:String = "赤";
private static const BLUE:String = "青";
private static const YELLOW:String = "黄";
private static const PURPLE:String = "紫";
private static const SKYBLUE:String = "水色";
private static const GREEN:String = "緑";
private function createContextMenuItems():Array{
var items:Array = [];
items.push( new ContextMenuItem( BLACK ) );
items.push( new ContextMenuItem( RED ) );
items.push( new ContextMenuItem( YELLOW ) );
items.push( new ContextMenuItem( GREEN) );
items.push( new ContextMenuItem( SKYBLUE) );
items.push( new ContextMenuItem( BLUE ) );
items.push( new ContextMenuItem( PURPLE) );
for each ( var item :ContextMenuItem in items ) {
item.addEventListener( ContextMenuEvent.MENU_ITEM_SELECT, onSelectColor );
}
return items;
}
private function onSelectColor ( e:ContextMenuEvent ):void {
var items:Array = contextMenu.customItems;
for each( var i:ContextMenuItem in items ) {
i.enabled =true;
}
var item:ContextMenuItem = e.target as ContextMenuItem;
item.enabled =false;
var type:String = item.caption;
switch( type ) {
case BLACK:
index = 0;
break;
case RED:
index = 1;
break;
case YELLOW:
index = 2;
break;
case GREEN:
index = 3;
break;
case SKYBLUE:
index = 4;
break;
case BLUE:
index = 5;
break;
case PURPLE:
index = 6;
break;
}
color = colors[ index ];
}
private function keyUpHandler( e:KeyboardEvent ):void {
switch ( e.keyCode ) {
case Keyboard.LEFT:
index = ( colors.length + index - 1 ) % colors.length;
color = colors[ index ];
break;
case Keyboard.RIGHT:
index = ( index + 1 ) % colors.length;
color = colors[ index ];
break;
}
}
private function startDraw( e:MouseEvent ):void {
var g:Graphics = canvas_sp.graphics;
g.clear();
g.lineStyle( 5, color, 0.8 );
g.moveTo( e.stageX, e.stageY );
var up:Function = function( e:MouseEvent ):void {
stage.removeEventListener( MouseEvent.MOUSE_MOVE, move );
stage.removeEventListener( MouseEvent.MOUSE_UP, up );
canvas_bd.draw( canvas_sp );
g.clear();
}
var move:Function = function( e:MouseEvent ):void {
g.lineTo( e.stageX, e.stageY );
}
stage.addEventListener( MouseEvent.MOUSE_MOVE, move );
stage.addEventListener( MouseEvent.MOUSE_UP, up );
}
}
}