な~るほど ザ!ワ…
...
@author katapad
@version 0.2
@since 2008/12/23 23:55
// forked from katapad's おまえの母ちゃん…
package
{
import caurina.transitions.Tweener;
import flash.display.*;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="60")]
/**
* ...
* @author katapad
* @version 0.2
* @since 2008/12/23 23:55
*/
public class LEDSignMainNaruhodo extends Sprite
{
//----------------------------------
// static var/const
//----------------------------------
//public static const DEBUG_MOODE:Boolean = true;
public static const DEBUG_MOODE:Boolean = false;
public static const STAGE_WIDTH:uint = 465;
public static const STAGE_HEIGHT:uint = 465;
public static const LED_CONTAINER_WIDTH:uint = 464;
public static const LED_CONTAINER_HEIGHT:uint = 464;
public static const LED_MARGIN:uint = 2;
public static const LED_RADIUS:uint = 2;
public static const ROWS:uint = 15;
public static const ANIMATION_SPEED:Number = 0.02;
private static const DEFAULT_TEXTFORMAT:TextFormat = new TextFormat("_等幅", 12, 0xFFFFFF, false);
//----------------------------------
// instance var
//----------------------------------
private var _ledContainer:Sprite;
private var _ledList:Array;
private var _canvas:Sprite;
private var _canvasDrawBM:BitmapData;
private var _resetBMRect:Rectangle;
private var _drawMatrix:Matrix = new Matrix();
private var _tfList:/*TextField*/Array;
private var _square:Shape;
private var _words:Array = ["な~るほど ザ!", "ワカメ酒!"];
private var _cols:uint;
public function LEDSignMainNaruhodo()
{
init();
updateOn();
animationStart();
}
private function init():void
{
_cols = Math.floor((LED_CONTAINER_WIDTH - 2 * LED_MARGIN) / (LED_RADIUS * 2 /*+ LED.OFF_GLOW_FILTER.blurX */+ LED_MARGIN))
_resetBMRect = new Rectangle(0, 0, _cols, ROWS);
initStage();
initLEDs();
initCanvas();
}
private function initCanvas():void
{
_canvas = new Sprite();
_canvasDrawBM = new BitmapData(_cols, ROWS, true, 0x000000);
_drawMatrix.translate(0, -2);
createText();
createSquare();
_ledContainer.y = stage.stageHeight * 0.5 - _ledContainer.height * 0.5 ;
//デバッグ確認用
if (DEBUG_MOODE)
{
addChild(_canvas);
_canvas.y = 420;
var bm:Bitmap = new Bitmap(_canvasDrawBM);
addChild(bm);
bm.y = 300;
}
}
private function createSquare():void
{
_square = new Shape();
_square.y = 2;
_canvas.addChild(_square);
_square.graphics.beginFill(0xFFFFFF, 1.0);
_square.graphics.drawRect(0, 0, _cols, ROWS);
_square.graphics.endFill();
_square.blendMode = BlendMode.INVERT;
}
private function createText():void
{
_tfList = [];
for (var i:int = 0, n:int = _words.length; i < n; i++)
{
var tf:TextField = _canvas.addChild(new TextField()) as TextField;
tf.defaultTextFormat = DEFAULT_TEXTFORMAT;
tf.autoSize = TextFieldAutoSize.LEFT;
tf.text = _words[i];
tf.y = 2;
_tfList[i] = tf;
}
}
private function initStage():void
{
//stage.align = StageAlign.TOP_LEFT;
//stage.scaleMode = StageScaleMode.NO_SCALE;
stage.quality = StageQuality.HIGH;
var stageBg:Shape = addChild(new Shape()) as Shape;
stageBg.graphics.lineStyle(1, 0xcccccc);
stageBg.graphics.drawRect(0, 0, STAGE_WIDTH, STAGE_HEIGHT);
}
private function initLEDs():void
{
_ledList = [];
_ledContainer = new Sprite();
addChild(_ledContainer);
var space:uint = LED_RADIUS * 2/* + LED.OFF_GLOW_FILTER.blurX*/ + LED_MARGIN;
for (var i:int = 0; i < ROWS * _cols; i++)
{
var led:LED = _ledContainer.addChild(LEDFactory.create(LED_RADIUS)) as LED;
led.x = (i % _cols) * space + LED_MARGIN;
led.y = Math.floor(i / _cols) * space + space;
_ledList[i] = led;
}
}
private function updateOn():void
{
addEventListener(Event.ENTER_FRAME, update);
}
private function update(event:Event):void
{
render();
}
private function render():void
{
_canvasDrawBM.fillRect(_resetBMRect, 0);
_canvasDrawBM.draw(_canvas, _drawMatrix);
_canvasDrawBM.threshold(_canvasDrawBM, _canvasDrawBM.rect, new Point(), "<", 0x808080, 0);
for (var i:int = 0, n:int = _ledList.length; i < n; i++)
{
var color:uint = _canvasDrawBM.getPixel((i % _cols), Math.floor(i / _cols));
if (color == 0)
LED(_ledList[i]).lightOn()
else
LED(_ledList[i]).lightOff()
}
}
/**
* アニメーション
*/
private function animationStart():void
{
initPos();
var delayTime:Number = 0;
var animTime:Number;
var speed:Number = ANIMATION_SPEED;
for (var i:int = 0, n:int = _tfList.length; i < n; i++)
{
var tf:TextField = _tfList[i];
animTime = LEDAnimation.getTFAnimTime(tf.x, -tf.textWidth - 1, speed);
//フリ
if (i != n -1)
{
delayTime = LEDAnimation.slide(tf, -tf.textWidth - 2, delayTime, animTime);
}
//オチ
else
{
delayTime -= 0.8;
var cx:Number = Math.round((_cols - tf.textWidth) * 0.5);
animTime = LEDAnimation.getTFAnimTime(tf.x, -cx - 1, speed);
delayTime = LEDAnimation.slide(tf, cx, delayTime, animTime);
delayTime += 1.0;
for (var j:int = 0; j < 4; j++)
{
delayTime = LEDAnimation.blink(_square, delayTime);
}
delayTime = LEDAnimation.slide(tf, -tf.textWidth - 2, delayTime + 0.5, LEDAnimation.getTFAnimTime(cx, -tf.textWidth - 1, speed));
}
}
Tweener.addCaller(this, { time: 0.0, delay: delayTime + 2.0, count: 1, onUpdate: animationStart } );
}
private function initPos():void
{
_square.visible = false;
for (var i:int = 0, n:int = _tfList.length; i < n; i++)
{
_tfList[i].x = _cols * 1.5;
}
}
}
}
import caurina.transitions.Tweener;
class LEDAnimation
{
function LEDAnimation()
{
}
public static function blink(target:DisplayObject, delayTime:Number, blinkTime:Number = 0.2):Number
{
Tweener.addTween(target, { visible: true, time: 0.0, delay: delayTime += blinkTime, transition: "linear" } );
Tweener.addTween(target, { visible: false, time: 0.0, delay: delayTime += blinkTime, transition: "linear" } );
return delayTime;
}
public static function slide(target:DisplayObject, destX:Number, delayTime:Number, animTime:Number):Number
{
Tweener.addTween(target, { x: destX, time: animTime, delay: delayTime, transition: "linear" } );
return delayTime + animTime;
}
public static function getTFAnimTime(startX:Number, destX:Number, friction:Number = 0.1):Number
{
return Math.abs((startX - destX) * friction);
}
}
import flash.display.*;
import flash.filters.BlurFilter;
import flash.filters.GlowFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
class LED extends Bitmap
{
public static const ON_COLOR:uint = 0xE9C20E;
public static const OFF_COLOR:uint = 0x454221;
public static const ON_GLOW_FILTER:GlowFilter = new GlowFilter(ON_COLOR, 1.0, 4, 4, 1, 2);
public static const ON_INNER_GLOW_FILTER:GlowFilter = new GlowFilter(0xFFFFFF, 0.8, 4, 4, 1, 2, true);
public static const ON_COLOR_FILTER:ColorTransform = new ColorTransform(0, 0, 0, 1.0, 69, 66, 33);
public static const OFF_GLOW_FILTER:GlowFilter = new GlowFilter(OFF_COLOR, 1.0, 4, 4, 1, 2);
public static const OFF_INNER_GLOW_FILTER:GlowFilter = new GlowFilter(0xFFFFFF, 0.5, 2, 2, 1, 2, true);
public static const OFF_COLOR_FILTER:ColorTransform = new ColorTransform();
private static var _onBM:BitmapData;
private static var _offBM:BitmapData;
public function LED()
{
init();
}
private function init():void
{
this.bitmapData = LED._offBM;
}
public function lightOn():void
{
this.bitmapData = LED._onBM;
}
public function lightOff():void
{
this.bitmapData = LED._offBM;
}
public static function set onBM(value:BitmapData):void
{
_onBM = value;
}
public static function set offBM(value:BitmapData):void
{
_offBM = value;
}
}
class LEDFactory
{
private static var _isCached:Boolean = false;
public function LEDFactory(){}
public static function create(radius:Number = 5):LED
{
if (!_isCached)
{
initCache(radius);
}
return new LED();
}
private static function initCache(radius:Number):void
{
LED.onBM = createBM(radius, LED.ON_COLOR, [LED.ON_GLOW_FILTER, LED.ON_INNER_GLOW_FILTER], LED.ON_COLOR_FILTER);
LED.offBM = createBM(radius, LED.OFF_COLOR, [LED.OFF_GLOW_FILTER, LED.OFF_INNER_GLOW_FILTER], LED.OFF_COLOR_FILTER);
_isCached = true;
}
private static function createBM(radius:Number, color:uint, filters:Array, colortrans:ColorTransform):BitmapData
{
var shape:Shape = new Shape();
shape.graphics.beginFill(color);
shape.graphics.drawCircle(radius, radius, radius);
shape.graphics.endFill();
shape.filters = filters;
shape.transform.colorTransform = colortrans;
var result:BitmapData = new BitmapData(shape.width + 4, shape.height + 4, true, 0x000000);
var mtx:Matrix = new Matrix();
mtx.translate(2, 2);
result.draw(shape, mtx);
return result;
}
}