Menu
Menu
@author tkinjo
/**
* Copyright tkinjo ( http://wonderfl.net/user/tkinjo )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/8Mfk
*/
// forked from tkinjo's KeyState クラス
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
[SWF(width="465", height="465", backgroundColor="0xffffff", frameRate="60")]
/**
* Menu
*
* @author tkinjo
*/
public class Main extends Sprite
{
private var config:ConfigScene;
private var scene:IScene;
public function Main()
{
config = ConfigScene.instance;
addChild( config );
scene = config;
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
private function enterFrameHandler( event:Event ):void {
scene.run();
scene.paint();
}
private function keyDownHandler( event:KeyboardEvent ):void {
KeyState.pressAtKeyCode( event.keyCode );
}
private function keyUpHandler( event:KeyboardEvent ):void {
KeyState.releaseAtKeyCode( event.keyCode );
}
}
}
import flash.display.Sprite;
import flash.errors.IllegalOperationError;
import flash.text.TextField;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.geom.Matrix;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
class ConfigScene extends Sprite implements IScene {
private var textField:TextField;
private const MENU_ITEMS:Vector.<String> = Vector.<String>( [ "MENU 1", "MENU 2", "MENU 3", "MENU 4", "MENU 5", "MENU 6", "MENU 7", "MENU 8", "MENU 9" ] );
private const MENU_X:Number = 200;
private const MENU_Y:Number = 100;
private const MENU_ITEM_SIZE:Number = 30;
private const MENU_FONT:String = "_sans";
private const MENU_FONT_SIZE:Number = 20;
private const MENU_FONT_BOLT:Boolean = true;
private const SELECT_MARKER_RADIUS:Number = 5;
private const SELECT_MARKER_OFFSET_X:Number = -10;
private const SELECT_MARKER_OFFSET_Y:Number = MENU_FONT_SIZE / 2 + 4;
private const KEY_FIRST_REPEAT_INTERVAL:Number = 20;
private const KEY_REPEAT_INTERVAL:Number = 5;
private var selectedIndex:uint;
private var keyRepeatedCounter:Number;
public function ConfigScene():void {
if ( _instance )
throw new IllegalOperationError ("Use Setting.instance to get the instance");
textField = new TextField();
textField.autoSize = TextFieldAutoSize.LEFT;
textField.defaultTextFormat = new TextFormat( MENU_FONT, MENU_FONT_SIZE, null, MENU_FONT_BOLT );
}
public function run():void {
var keyRepeatCount:uint;
if ( KeyState.getRepeated( KeyState.UP ) && selectedIndex > 0 ) {
if ( KeyState.getPressed( KeyState.UP ) ) {
selectedIndex--;
} else {
KeyState.manualRepeatCount( KeyState.UP );
keyRepeatCount = KeyState.getManualRepeatedCount( KeyState.UP );
if (
keyRepeatCount == KEY_FIRST_REPEAT_INTERVAL ||
(
keyRepeatCount > KEY_FIRST_REPEAT_INTERVAL &&
( keyRepeatCount - KEY_FIRST_REPEAT_INTERVAL ) % KEY_REPEAT_INTERVAL == 0
)
) {
selectedIndex--;
}
}
} else if ( KeyState.getRepeated( KeyState.DOWN ) && selectedIndex < MENU_ITEMS.length - 1 ) {
if ( KeyState.getPressed( KeyState.DOWN ) ) {
selectedIndex++;
} else {
KeyState.manualRepeatCount( KeyState.DOWN );
keyRepeatCount = KeyState.getManualRepeatedCount( KeyState.DOWN );
if (
keyRepeatCount == KEY_FIRST_REPEAT_INTERVAL ||
(
keyRepeatCount > KEY_FIRST_REPEAT_INTERVAL &&
( keyRepeatCount - KEY_FIRST_REPEAT_INTERVAL ) % KEY_REPEAT_INTERVAL == 0
)
) {
selectedIndex++;
}
}
}
}
public function paint():void {
graphics.clear();
for ( var i:uint = 0; i < MENU_ITEMS.length; i++ )
drawString( graphics, textField, MENU_X, MENU_Y + MENU_ITEM_SIZE * i, MENU_ITEMS[ i ] );
graphics.beginFill( 0 );
graphics.drawCircle( MENU_X + SELECT_MARKER_OFFSET_X, MENU_Y + MENU_ITEM_SIZE * selectedIndex + SELECT_MARKER_OFFSET_Y, SELECT_MARKER_RADIUS );
graphics.endFill();
}
private static var _instance:ConfigScene;
public static function get instance():ConfigScene {
if ( _instance == null )
_instance = new ConfigScene();
return _instance;
}
}
interface IScene {
function run():void;
function paint():void;
}
class KeyState {
private static var repeated:Vector.<uint> = new Vector.<uint>( 255 );
private static var pressed :Vector.<Boolean> = new Vector.<Boolean>( 255 );
private static var released:Vector.<Boolean> = new Vector.<Boolean>( 255 );
private static var manualRepeatedCounter:Vector.<uint> = new Vector.<uint>( 255 );
/** --------------------------------------------------
*
*/
public static const SHIFT:String = "shift";
public static const CTRL :String = "ctrl";
public static const ALT :String = "alt";
public static const LEFT :String = "left";
public static const UP :String = "up";
public static const RIGHT:String = "right";
public static const DOWN :String = "down";
/** --------------------------------------------------
*
*/
public static const KEY_SHIFT:uint = 16;
public static const KEY_CTRL :uint = 17;
public static const KEY_ALT :uint = 18;
public static const KEY_LEFT :uint = 37;
public static const KEY_UP :uint = 38;
public static const KEY_RIGHT:uint = 39;
public static const KEY_DOWN :uint = 40;
public static const CHAR_LARGE_A:uint = 65;
public static const CHAR_SMALL_A:uint = 97;
public static const KEY_A :uint = 65;
public static const NUM_ALPHABET:uint = 26;
public static const CHAR_0 :uint = 48;
public static const KEY_0 :uint = 0;
public static const KEY_NUMPAD_0:uint = 96;
public static const NUM_NUMBER:uint = 10;
/** ==================================================
*
*/
public static function press( string:String ):void {
var keyCode:Number = getKeyCode( string );
if ( keyCode > 0 )
pressAtKeyCode( keyCode );
}
public static function release( string:String ):void {
var keyCode:Number = getKeyCode( string );
if ( keyCode > 0 )
releaseAtKeyCode( keyCode );
}
public static function manualRepeatCount( string:String ):void {
var keyCode:Number = getKeyCode( string );
if ( keyCode > 0 )
manualRepeatCountAtKeyCode( keyCode );
}
/** --------------------------------------------------
*
*/
public static function pressAtKeyCode( keyCode:uint ):void {
if ( repeated[ keyCode ] ) {
repeated[ keyCode ]++;
return;
}
repeated[ keyCode ] = 1;
pressed[ keyCode ] = true;
released[ keyCode ] = false;
manualRepeatedCounter[ keyCode ] = 1;
}
public static function releaseAtKeyCode( keyCode:uint ):void {
repeated[ keyCode ] = 0;
pressed[ keyCode ] = false;
released[ keyCode ] = true;
manualRepeatedCounter[ keyCode ] = 0;
}
public static function manualRepeatCountAtKeyCode( keyCode:uint ):void {
manualRepeatedCounter[ keyCode ]++;
}
/** --------------------------------------------------
*
*/
public static function getRepeated( string:String ):Boolean {
var keyCode:Number = getKeyCode( string );
if( keyCode > 0 )
return getRepeatedAtKeyCode( keyCode );
return false;
}
public static function getRepeatedCount( string:String ):uint {
var keyCode:Number = getKeyCode( string );
if( keyCode > 0 )
return getRepeatedCountAtKeyCode( keyCode );
return 0;
}
public static function getReleased( string:String ):Boolean {
var keyCode:Number = getKeyCode( string );
if( keyCode > 0 )
return getReleasedAtKeyCode( keyCode );
return false;
}
public static function getPressed( string:String ):Boolean {
var keyCode:Number = getKeyCode( string );
if( keyCode > 0 )
return getPressedAtKeyCode( keyCode );
return false;
}
public static function getManualRepeatedCount( string:String ):uint {
var keyCode:Number = getKeyCode( string );
if( keyCode > 0 )
return getManualRepeatedCountAtKeyCode( keyCode );
return 0;
}
/** --------------------------------------------------
*
*/
public static function getRepeatedAtKeyCode( keyCode:uint ):Boolean {
return ( repeated[ keyCode ] > 0 ) ? true : false;
}
public static function getRepeatedCountAtKeyCode( keyCode:uint ):uint {
return repeated[ keyCode ];
}
public static function getReleasedAtKeyCode( keyCode:uint ):Boolean {
var result:Boolean = released[ keyCode ];
released[ keyCode ] = false;
return result;
}
public static function getPressedAtKeyCode( keyCode:uint ):Boolean {
var result:Boolean = pressed[ keyCode ];
pressed[ keyCode ] = false;
return result;
}
public static function getManualRepeatedCountAtKeyCode( keyCode:uint ):uint {
return manualRepeatedCounter[ keyCode ];
}
/** --------------------------------------------------
*
*/
public static function getKeyCode( string:String ):Number {
if( string.length == 1 ) {
return charCodeToKeyCode( string.charCodeAt() );
}
switch ( string ) {
case SHIFT:
return KEY_SHIFT;
case CTRL:
return KEY_CTRL;
case ALT:
return KEY_ALT;
case LEFT:
return KEY_LEFT;
case UP:
return KEY_UP;
case RIGHT:
return KEY_RIGHT;
case DOWN:
return KEY_DOWN;
}
return -1;
}
public static function charCodeToKeyCode( charCode:uint ):Number {
// A-Z
if ( CHAR_LARGE_A <= charCode && ( charCode - CHAR_LARGE_A ) < NUM_ALPHABET ) {
return charCode;
// a-z
} else if ( CHAR_SMALL_A <= charCode && ( charCode - CHAR_SMALL_A ) < NUM_ALPHABET ) {
return KEY_A + ( charCode - CHAR_SMALL_A );
// 0-9
} else if ( CHAR_0 <= charCode && ( charCode - CHAR_0 ) < NUM_NUMBER ) {
return charCode;
}
return -1;
}
}
var drawStringMatrix:Matrix = new Matrix();
function drawString( graphics:Graphics, textField:TextField, x:Number, y:Number, text:String = "" ):void {
if ( text )
textField.text = text;
var bitmapData:BitmapData = new BitmapData( textField.width, textField.height, true, 0 );
bitmapData.draw( textField );
drawStringMatrix.identity();
drawStringMatrix.translate( x, y );
graphics.beginBitmapFill( bitmapData, drawStringMatrix );
graphics.drawRect( x, y, textField.width, textField.height );
graphics.endFill();
}