テキストからPOV用データを作成する
テキストデータからPOV用データを作成する
LEDイメージをクリックすることで微調整も可能です
/**
* Copyright SOBARCO ( http://wonderfl.net/user/SOBARCO )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/aBqZ
*/
// forked from SOBARCO's flash on 2010-6-5
package
{
import flash.display.*;
import flash.events.Event;
import flash.text.*;
import flash.geom.Matrix;
//テキストデータからPOV用データを作成する
//LEDイメージをクリックすることで微調整も可能です
public class Txt2Pov extends Sprite {
private var pxs:Array = [];
private var leds:Array = []
private var pov_data:TextField = new TextField();
public function Txt2Pov() {
//変換文字列
var text:String = "ABC";
//利用LED数
var size:int = 7;
var i:int = size;
while (true) {
pxs = []
pxs = get_data(text, i);
if (pxs.length == size) {
break;
}
if (pxs.length > size) {
var diff:int = pxs.length - size;
i = i - 1;
pxs = []
pxs = get_data(text, i);
var line:Array = [];
for (var j:int = 0; j < pxs[0].length; j++) {
line.push(0);
}
for (j = 0; j < diff;j++) {
pxs.push(line);
}
break;
}
i = i + 1;
}
var led_size:int = 5;
var led_board:Sprite = new Sprite();
led_board.x = 0;
led_board.y = 20;
for (var y:int = 0; y < pxs.length; y++) {
for (var x:int = 0; x < pxs[y].length; x++) {
var led:Led = new Led(x, y,pxs[y][x],0xFF0000,led_size);
leds.push(led)
led_board.addChild(led);
}
}
addChild(led_board);
print_data();
addEventListener("change_data", changeLed);
}
private function changeLed(event:Event):void {
print_data();
}
private function print_data():void {
for (var i:int = 0; i < leds.length; i++) {
var d:String = "0";
if (leds[i].light) {
d = "1";
}
pxs[leds[i].yy][leds[i].xx] = d;
}
var data:String = ""
for (x = 0; x < pxs[0].length; x++) {
data = data + "0b0";
for (y = pxs.length-1; y >= 0; y--) {
data = data + pxs[y][x];
}
data = data + ",\n";
}
data = data + "0b10000000";
pov_data.text = data;
addChild(pov_data);
pov_data.height = 200;
pov_data.y = 100;
}
private function get_data(txt:String, size:int):Array {
var txtFld:TextField = new TextField();
txtFld.text = toZenkaku(txt);
txtFld.textColor = 0xFF0000;
txtFld.autoSize = TextFieldAutoSize.LEFT;
var fmt:TextFormat = new TextFormat();
fmt.size = size;
fmt.letterSpacing = 1;
fmt.leftMargin = 1;
txtFld.setTextFormat(fmt);
var bmp:BitmapData = new BitmapData(txtFld.textWidth, txtFld.textHeight, false, 0);
var matrix:Matrix = new Matrix();
matrix.translate(-2, -2);
bmp.draw(txtFld,matrix);
var bm:Bitmap = new Bitmap(bmp);
addChild(bm);
var p:Array = [];
for (var h:int = 0; h < txtFld.textHeight; h++) {
var find_c:Boolean = false;
var line:Array = [];
for (var w:int = 0; w < txtFld.textWidth+fmt.leftMargin; w++) {
var color:Number = bmp.getPixel(w, h);
if(color) {
line.push(true);
}else {
line.push(false);
}
if (color) {
find_c = true;
}
}
if (find_c) {
p.push(line);
}
}
return p;
}
//半角から全角に変換した方がいい感じに変換できたので変換するための関数
public static function toZenkaku(src:String):String {
var str:String = "";
var len:int = src.length;
for (var i:int= 0; i < len; i++) {
var c:Number = src.charCodeAt(i);
if ( c >= 33 && c <= 126 && c != 92 && c != 46) {
str += String.fromCharCode(c + 65248);
} else if (c == 39) {
str += String.fromCharCode(8217);
} else if (c == 34) {
str += String.fromCharCode(8221);
} else if (c == 32) {
str += String.fromCharCode(12288);
} else if (c == 126) {
str += String.fromCharCode(65507);
} else if (c == 92) {
str += String.fromCharCode(65509);
} else {
str += src.charAt(i);
}
}
return str;
}
}
}
import flash.display.*;
import flash.events.*;
import flash.text.*;
class Led extends Sprite {
public var xx:int;
public var yy:int;
public var size:int;
public var light:Boolean = false;
public var color:Number;
public function Led(x:int,y:int,l:Boolean,c:Number,s:int=5) {
xx = x;
yy = y;
size = s;
light = l;
color = c;
set_color();
addEventListener(MouseEvent.CLICK, changeColor);
}
private function set_color():void {
if (light) {
graphics.beginFill(color);
} else {
graphics.beginFill(0);
}
graphics.drawCircle(size + size*2*xx, size+size*2*yy, size);
}
private function changeColor(event:MouseEvent):void{
light = !light;
set_color();
dispatchEvent(new Event("change_data",true));
}
}