Keyboard
キーボード描きました。
/**
* Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/1Wjj
*/
//キーボード描きました。
package {
import flash.display.Sprite;
import com.bit101.components.*;
import flash.events.KeyboardEvent;
import flash.events.Event;
[SWF(width="465", height="465", backgroundColor="0xFFF8EE")]
public class FlashTest extends Sprite {
private var board:KeyboardSprite = new KeyboardSprite();
private var btn:PushButton;
public function FlashTest() {
board.scaleY = board.scaleX = 0.7;
board.y = 300;
board.x = 20;
addChild( board );
var text:TextArea = new TextArea( this, 20, 40 );
text.width = 420;
text.height = 240;
new Label( this, 20, 10, "KEYBOARD" );
btn = new PushButton( this , 340, 10, "Qwerty", _dvorak );
this.addEventListener( "addedToStage", onAdded );
}
private function onAdded(e:Event):void{
this.removeEventListener( "addedToStage", onAdded );
stage.addEventListener( "keyDown", onKeyDown );
stage.addEventListener( "keyUp", onKeyUp );
}
private function onKeyDown( e:KeyboardEvent ):void{
if( e.keyCode == 16 ){ board.shift() }
for each( var key:KeyShape in board.keyShapes ){
if( key.state == "over" ){ key.up() }
if( key.code == e.keyCode ){ key.down(); }
}
}
private function onKeyUp( e:KeyboardEvent ):void{
if( e.keyCode == 16 ){ board.unshift() }
for each( var key:KeyShape in board.keyShapes ){
if( key.code == e.keyCode ){ key.over(); }
}
}
private function _dvorak( e:Event ):void{
board.dvorak = !board.dvorak;
if( board.dvorak ){ btn.label = "Dvorak" }
else{ btn.label = "Qwerty" }
}
}
}
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Graphics;
import flash.display.Sprite;
class KeyboardSprite extends Sprite{
static public const code48:String = "0123456789";
static public const code65:String = "abcdefghijklmnopqrstuvwxyz";
static public const code186:String = ":;,-./@";
static public const code219:String = "[¥]^";
static public const code226:String = "\\";
static public const qwertyKeys:Array = [ "1234567890-^¥", "qwertyuiop@[", "asdfghjkl;:]", "zxcvbnm,./\\" ];
static public const qwertyShiftKeys:Array = [ "!\"#$%&'() =~|", "QWERTYUIOP`{", "ASDFGHJKL+*}", "ZXCVBNM<>?_" ];
static public const dvorakKeys:Array = [ "1234567890[]¥", ":,.pyfgcrl/@", "aoeuidhtns-^", ";qjkxbmwvz\\" ];
static public const dvorakShiftKeys:Array = [ "!\"#$%&'() {}|", "*<>PYFGCRL?`", "AOEUIDHTNS=~", "+QJKXBMWVZ_" ]
public var keys:Array = qwertyKeys;
public var shiftKeys:Array = qwertyShiftKeys;
private var _dvorak:Boolean = false;
public function get dvorak():Boolean{ return _dvorak; }
public function set dvorak( b:Boolean ):void{
if( _dvorak != b ){
_dvorak = b;
if( b ){
keys = dvorakKeys;
shiftKeys = dvorakShiftKeys;
}else{
keys = qwertyKeys;
shiftKeys = qwertyShiftKeys;
}
init();
}
}
static public const indent:Array = [ 1, 1.5, 1.8, 2.3, 5 ]
static public const keySize:Number = 40;
public var keyShapes:Vector.<KeyShape> = new Vector.<KeyShape>;
function KeyboardSprite(){ init() }
private function init():void{
while( this.numChildren > 0 ){ this.removeChildAt(0); }
var h:int = keys.length;
var s:Number = keySize;
for( var i:int = 0; i < h; i++ ){
var w:int = keys[i].length
for( var j:int = 0; j < w; j++ ){
var str:String = keys[i].substr(j,1)
var code:int = 48 + code48.indexOf( str );
if( code == 47 ){ code = 65 + code65.indexOf( str ) }
if( code == 64 ){ code = 186 + code186.indexOf( str ) }
if( code == 185 ){ code = 219 + code219.indexOf( str ) }
if( code == 218 ){ code = 226 + code226.indexOf( str ) }
var mark:Boolean = i == 2 &&( j == 3 || j == 6 )
var key:KeyShape = new KeyShape( s-1, s-1, str, shiftKeys[i].substr(j,1), code, mark );
key.x = ( j + indent[i] ) * s;
key.y = i * s;
addChild( key );
keyShapes.push( key );
}
}
for( i = 0; i < 3; i++ ){
key = new KeyShape( s*indent[i]-1, s-1, "", null, [229,9,22][i] );
key.y = i * s;
addChild( key );
}
var w0:Number = keys[0].length + indent[0];
var w1:Number = keys[1].length + indent[1];
var w2:Number = keys[2].length + indent[2];
var w3:Number = keys[3].length + indent[3];
//back space
key = new KeyShape( s-1, s-1, "BS", "BS", 8 );
key.x = w0 * s;
addChild( key );
keyShapes.push( key );
//shift
key = new KeyShape( indent[3]*s- 1, s-1, "Shift", "Shift", 16 );
key.y = 3 * s;
addChild( key );
keyShapes.push( key );
key = new KeyShape( (w0-w3+1)*s -1, s-1, "Shift", "Shift", 16 );
key.x = w3 * s;
key.y = 3 * s;
addChild( key );
keyShapes.push( key );
//enter
key = new EnterKeyShape( (w0-w1+1)*s -1, (w0-w2+1)*s -1, s-1, 2*s-1 );
key.x = w1 * s;
key.y = 1 * s;
addChild( key );
keyShapes.push( key );
//space
key = new KeyShape( 4*s -1, s-1, " ", " ", 32 );
key.x = indent[4] * s;
key.y = 4 * s;
addChild( key );
keyShapes.push( key );
}
public function shift():void {
for each( var key:KeyShape in keyShapes ){ key.shift() }
}
public function unshift():void {
for each( var key:KeyShape in keyShapes ){ key.unshift() }
}
}
class KeyShape extends Sprite {
public var upFill:uint = 0xEEDDCC;
public var upLine:uint = 0x555555;
public var downFill:uint = 0xDDAA77;
public var downLine:uint = 0x554433;
public var overFill:uint = 0xDDBB99;
public var overLine:uint = 0x554433;
public var fillColor:uint = upFill;
public var lineColor:uint = upLine;
public var state:String = "up";
public var textField:TextField = new TextField();
public var w:Number;
public var h:Number;
public var key:String;
public var shiftKey:String;
public var code:int;
public var shifted:Boolean;
public var mark:Boolean;
function KeyShape( width:Number, height:Number, key:String, shiftKey:String, code:Number, mark:Boolean = false ){
w = width; h = height; this.key = key; this.shiftKey = shiftKey; this.code = code; this.mark = mark
var format:TextFormat = new TextFormat( "_monospace", h/2.33, lineColor, true );
textField.defaultTextFormat = format;
textField.x = 6;
textField.y = 1;
textField.selectable = false;
draw();
addChild( textField );
drawText();
}
public function draw():void{
var g:Graphics = this.graphics;
g.clear();
g.beginFill( fillColor, 1 );
g.lineStyle( 2, lineColor );
g.drawRoundRect( 0,0, w, h, 8 );
g.endFill();
if( mark ){
var cw:Number = w/2;
g.lineStyle( 1, lineColor, 0.8 );
g.moveTo( cw + 4, h - 13 );
g.lineTo( cw - 4, h - 13 );
}
g.lineStyle( 1, 0xFFFFFF, 0.5 );
g.moveTo( 5, 1 );
g.lineTo( 5, h-10 );
g.lineTo( w-5 , h-10 );
g.lineTo( w-5 , 1 );
}
public function drawText():void {
if( shifted ){ textField.text = shiftKey; }
else { textField.text = key; }
}
public function down():void{
fillColor = downFill;
lineColor = downLine;
state = "down";
draw();
}
public function over():void{
fillColor = overFill;
lineColor = overLine;
state = "over";
draw();
}
public function up():void{
fillColor = upFill;
lineColor = upLine;
state = "up";
draw();
}
public function shift():void{
shifted = true;
drawText();
}
public function unshift():void{
shifted = false;
drawText();
}
}
class EnterKeyShape extends KeyShape{
private var w2:Number;
private var h2:Number;
function EnterKeyShape ( width:Number, width2:Number, height:Number, height2:Number ){
w2 = width2; h2 = height2;
super( width, height, "Enter", "Enter", 13 )
addChild( textField );
drawText();
}
override public function draw():void{
var g:Graphics = this.graphics;
g.clear();
g.beginFill( fillColor, 1 );
g.lineStyle( 2, lineColor );
var l:Function = g.lineTo;
var c:Function = g.curveTo;
var r:Number = 8;
var x0:Number = 0, x1:Number = w - w2, x2:Number = w;
var y0:Number = 0, y1:Number = h, y2:Number = h2;
g.moveTo( x0, y0 + r );
l( x0, y1 - r ); c( x0, y1, x0 + r, y1 );
l( x1 , y1 );
l( x1, y2 - r ); c( x1, y2, x1 + r, y2 );
l( x2 - r, y2 ); c( x2, y2, x2, y2 - r );
l( x2, y0 + r ); c( x2, y0, x2 - r, y0 );
l( x0 + r, y0 ); c( x0, y0, x0, y0 + r );
g.endFill();
g.lineStyle( 1, 0xFFFFFF, 0.5 );
x0 = 4, x1 = w - w2 + 4, x2 = w - 4;
y0 = 1, y1 = h - 10, y2 = h2 - 10;
g.moveTo( x0, y0 );
l( x0, y1 ); l( x1, y1 ); l( x1, y2 ); l( x2, y2 ); l( x2, y0 );
}
}