フリック入力みたいなもの

by tana
iPhoneのフリック入力を真似してみたつもり。
本物を触ったことが無いので間違っているかも
♥0 | Line 131 | Modified 2010-04-13 19:11:39 | MIT License
play

ActionScript3 source code

/**
 * Copyright tana ( http://wonderfl.net/user/tana )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/yLy9
 */

//iPhoneのフリック入力を真似してみたつもり。
//本物を触ったことが無いので間違っているかも
package {
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.display.MovieClip;
    import flash.events.*;
    import flash.text.TextField;
    import flash.geom.Point;
    import flash.text.engine.TextBlock;
    import flash.geom.Point;
    public class FlashTest extends  Sprite {
    	private var textfield:TextField = new TextField();
    	private var HiraganaList:Array = [["あ", "い", "う", "え", "お"],
    	                                  ["か", "き", "く", "け", "こ"],
    	                                  ["さ", "し", "す", "せ", "そ"],
    	                                  ["た", "ち", "つ", "て", "と"],
    	                                  ["な", "に", "ぬ", "ね", "の"],
    	                                  ["は", "ひ", "ふ", "へ", "ほ"],
    	                                  ["ま", "み", "む", "め", "も"],
    	                                  ["や", "い", "ゆ", "え", "よ"],
    	                                  ["ら", "り", "る", "れ", "ろ"],
    	                                  ["わ", "ゐ", "う", "ゑ", "を"]];
    	private var ButtonList:Array = [];
    	private var MousePoint:Point; //マウスが押された時のカーソルの位置
    	private var ButtonText:String; //押されたボタンに書かれている文字
        public function FlashTest() {
            // write as3 code here..
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
            stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
            //var button:Sprite = new TextButton("a",0x808080);
	     //button.x = 100;
	     //button.y = 100;
	     //addChild(button);
	     function addButton(text:String, x:Number, y:Number):void {
	     	//ボタンを描画する。
	     	var button:TextButton = new TextButton(text, 0x808080);
	     	button.x = x;
	     	button.y = y;
	     	addChild(button);
	     }
	     function addLine(text:Array, y:Number):void {
	     	//ボタンを一列(3個)描画する
	     	var width:Number = 51; //一番左にあるボタンの左に空けておく幅
	     	addButton(text[0], width + 0, y);
	     	addButton(text[1], width + 51, y);
	     	addButton(text[2], width + 102, y);
	     }
	     addLine([HiraganaList[0][0], HiraganaList[1][0], HiraganaList[2][0]], 31);
	     addLine([HiraganaList[3][0], HiraganaList[4][0], HiraganaList[5][0]], 62);
	     addLine([HiraganaList[6][0], HiraganaList[7][0], HiraganaList[8][0]], 93);
	     
	     textfield.text = "";
	     textfield.x = 0;
	     textfield.y = 93 + 31;
	     textfield.width = 51 * 4;
	     addChild(textfield);
        }
        public function onDown(e:MouseEvent):void {
        	var list:Array = getObjectsUnderPoint(new Point(e.stageX, e.stageY));
        	//クリックされた場所にあるボタンの文字
        	var text:String = list[list.length - 1].parent.tf.text;
        	var x:Number = list[list.length - 1].parent.x;
        	var y:Number = list[list.length - 1].parent.y;
        	function Filter(arr:Array, i:Number, a:Array):Boolean {
        		return arr[0] == text;
        	}
        	//出てくるボタンに表示する文字のリスト
        	var CharList:Array = HiraganaList.filter(Filter)[0];
        	//var CharList:Array = [10,20,30].map(toString);
        	showPopup(CharList.slice(1), x, y);
        	/*var button:Array = [];
        	button[0] = new TextButton(CharList[1]);
        	ButtonList.push(button[0]);
        	button[0].x = e.stageX;
        	button[0].y = e.stageY;
        	addChild(button[0]);*/
        	MousePoint = new Point(e.stageX, e.stageY);
        	ButtonText = text;
        }
        private function showPopup(t:Array, x:Number, y:Number):void {
        	var button:Array = [];
        	button[0] = new TextButton(t[0]);
        	ButtonList.push(button[0]);
        	button[0].x = x - 50;
        	button[0].y = y;
        	addChild(button[0]);
        	button[1] = new TextButton(t[1]);
        	ButtonList.push(button[1]);
        	button[1].x = x;
        	button[1].y = y - 30;
        	addChild(button[1]);
        	button[2] = new TextButton(t[2]);
        	ButtonList.push(button[2]);
        	button[2].x = x + 50;
        	button[2].y = y;
        	addChild(button[2]);
        	button[3] = new TextButton(t[3]);
        	ButtonList.push(button[3]);
        	button[3].x = x;
        	button[3].y = y + 30;
        	addChild(button[3]);
        }
        private function onUp(e:MouseEvent):void {
        	//入力する行の文字のリスト
        	var CharList:Array = HiraganaList.filter(function (arr:Array, i:Number, a:Array):Boolean {
        		return arr[0] == ButtonText;
        	})[0];
        	
        	//移動した方向を判定
        	if (MousePoint.y - e.stageY > 15) {
        		if (MousePoint.x - e.stageX < 25 && MousePoint.x - e.stageX > -25) {
        			//上に移動した場合
        			textfield.appendText(CharList[2]);
        		}
        	}
        	else if (MousePoint.y - e.stageY < -15) {
        		if (MousePoint.x - e.stageX < 25 && MousePoint.x - e.stageX > -25) {
        			//下に移動した場合
        			textfield.appendText(CharList[4]);
        		}
        	}
        	else if(MousePoint.x - e.stageX < -25) {
        		if (MousePoint.y - e.stageY < 15 && MousePoint.y - e.stageY > -15) {
        			//右に移動した場合
        			textfield.appendText(CharList[3]);
        		}
        	}
        	else if(MousePoint.x - e.stageX > 25) {
        		if (MousePoint.y - e.stageY < 15 && MousePoint.y - e.stageY > -15) {
        			//左に移動した場合
        			textfield.appendText(CharList[1]);
        		}
        	}
        	else {
        		//移動しない場合
        		textfield.appendText(CharList[0]);
        	}
        	
        	removeChild(ButtonList.pop());
        	removeChild(ButtonList.pop());
        	removeChild(ButtonList.pop());
        	removeChild(ButtonList.pop());
        }
    }
}
import flash.display.*;
import flash.text.TextField;
class TextButton extends Sprite {
	public var tf:TextField;
	public function TextButton(text:String = "", color:Number = 0xD3D3D3) {
		graphics.beginFill(color);
		graphics.drawRect(0, 0, 50, 30);
		graphics.endFill();
		tf = new TextField();
		tf.text = text;
		addChild(tf);
	}
}