forked from: Checkmate vol4 Massmedian

by coppieee forked from Checkmate vol4 Massmedian (diff: 171)
セキュリティサンドボックス侵害で詰んだ
これが本当のチェックメイト!
♥0 | Line 229 | Modified 2009-10-24 02:34:40 | MIT License
play

ActionScript3 source code

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

//セキュリティサンドボックス侵害で詰んだ
//これが本当のチェックメイト!

package {
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.geom.*;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
    [SWF(backgroundColor=0x00,frameRate=60)]
    public class FlashTest extends Sprite {
        /*
         * [Checkmate Vol4 by MASSMEDIAN]
         * Please produce the illumination freely with a color palette. 
         *
         * usage of Palette class
         *   A single value is used.
         *     var white:uint = Palette.WHITE;
         *     var green:uint = Palette.GREEN;
         *     var red:uint = Palette.RED;
         *     var gold:uint = Palette.GOLD;
         *     var silver:uint = Palette.SILVER;
         *     var black:uint = Palette.BLACK;
         *   All the values are used.
         *     var colors:Array = Palette.getColors();
         */
        private function init( e:Event = null ):void {
            resize();
            //drawXmasTree();
			
			 new IconGetter(function(b:BitmapData):void {
				 _image = b;
				createSnow();
			 },function(e:SecurityError):void {
				 var tf:TextField = new TextField();
				 tf.textColor = 0xFFFFFF;
				tf.autoSize = TextFieldAutoSize.LEFT;
				var f:TextFormat = new TextFormat();
				f.size = 20
				tf.defaultTextFormat = f;
				
				tf.text = e.toString().substring(0, 10);
				addChild(tf);
				 //var b:BitmapData = new BitmapData(tf.height, tf.width);
				 //b.draw(tf);
				 //_image = b;
				 //createSnow();
			});
			//new TextField().tetC
			//var tf:TextField = new TextField();
			//tf.autoSize = TextFieldAutoSize.LEFT;
			//tf.defaultTextFormat = new TextFormat(null, 20);
			//tf.defaultTextFormat.size
			//tf.text = e.toString();
			
            
            //showPaletteMap();
        }
		private var _image:BitmapData;
        
        public var rect:Rectangle= new Rectangle();
        public var center:Point = new Point();
        
        private const SLOW_LENGTH:int = 1000;
        private var _snow:Vector.<Snow> = new Vector.<Snow>();
        
        private var _twincles:BitmapData;
        private var _canvas:BitmapData;
        
        private var _matrix:Matrix;
        private var _ctf:ColorTransform = new ColorTransform( 0.75, 0.8, 0.8, 0.95 );
        
        private function createSnow():void {
            // The value used for generation is prepared.
            const RADIAN:Number = Math.PI*2;
            const COLORS:Array = Palette.getColors();
            
            // The canvas to draw in illumination.
            var snow:Snow, speed:Number, angle:Number;
			for (var i:int = 0; i < _image.height; i++ )
			{
				for (var j:int = 0; j < _image.width; j++ )
				{
					
				
            //for ( var i :int = 0; i < SLOW_LENGTH; ++i ) {
				
                snow = new Snow();
                //snow.x   = (Math.random()-0.5)*50 + center.x;
                //snow.y   = Math.random()*rect.height;
                //snow.vx = (Math.random()-0.5) * 4;
                //snow.vy = Math.random()*-2;
                //snow.color = COLORS[int(COLORS.length*Math.random())];
				snow.x = i * 202 / _image.width +130;
				snow.y = j * 202 / _image.height+130;
				snow.color = _image.getPixel(i,j);
                _snow.push( snow );
            //}
				}
			}
            
            // The canvas to draw in illumination.
            _canvas = new BitmapData( rect.height, rect.height, true, 0 );
            var cbm :Bitmap = addChild( new Bitmap( _canvas ) ) as Bitmap;
            cbm.smoothing = true;
            
            // The canvas to draw in twincles.
            _twincles = new BitmapData( rect.width/4>>0, rect.height/4>>0, true, 0 );
            var tbm:Bitmap = addChild( new Bitmap( _twincles ) ) as Bitmap;
            tbm.scaleX = tbm.scaleY = 4;
            tbm.smoothing = true;
            tbm.blendMode = BlendMode.ADD;
            
            // Matrix to draw by size of 1/4
            _matrix = new Matrix(0.25, 0, 0, 0.25);
			
			addEventListener( Event.ENTER_FRAME, updateSnow );
			
        }
        
        private function updateSnow(e:Event):void {
            _canvas.lock();
            for each( var snow:Snow in _snow ) {
                // The Brownian motion is added.
                snow.vx += (Math.random()-0.5)*0.01;
                snow.vy += (Math.random()-0.5)*0.01;
                
                // Gravity is added.
                //snow.vy += 0.015;
                
                // The wind drag is added.
                snow.vx *= 0.987;
                snow.vy *= 0.987;
                
                // The speed is added to the position. 
                snow.x += snow.vx;
                snow.y += snow.vy;
                
                // draw to canvas in position.
                _canvas.setPixel32( snow.x, snow.y, snow.color | 0xFF<<24 );
                
                // It resets it when going out of the area. 
                if( snow.x < rect.x -50 ||  snow.y < rect.y -50 || snow.x > rect.width +50 || snow.y > rect.height + 50 ) {
                    snow.x = center.y + (Math.random() -0.5) * 10;
                    snow.y = Math.random() *-30;
                    snow.vx = (Math.random() -0.5) * 3;
                    snow.vy = Math.random() *-3;
                }
            }
            // ColorTransform(effect that dose blackout) is made to adjust. 
            _canvas.colorTransform( _canvas.rect, _ctf );
            _canvas.unlock();
            
             // draw to canvas in twincles by using Matrix.
            _twincles.draw( _canvas, _matrix );
        }
        
        
        private function resize( e:Event = null ):void {
            rect.width = stage.stageWidth;
            rect.height= stage.stageHeight;
            
            center.x = rect.width /2>>0;
            center.y = rect.height /2>>0;
        }
        
        private function drawXmasTree():void {
            var loader :Loader = new Loader();
            addChild( loader );
            loader.load( new URLRequest("http://swf-dev.wonderfl.net/static/assets/checkmate04/tree.jpg"),new LoaderContext(true));
        }
        private function showPaletteMap():void {
            var colors:Array = Palette.getColors();
            var palette:Sprite = addChild( new ColorMap( colors ) ) as Sprite;
            palette.x = ( rect.width - palette.width )  /2 >>0;
            palette.y = ( rect.height- palette.height ) /2 >>0;
        }
        
        public function FlashTest() {
            if( stage ) init();
            else addEventListener( Event.ADDED_TO_STAGE, init );
        }
    }
}
/* for Checkmate */
class Palette {
    public static const WHITE:uint = 0xF0F0F0;
    public static const GREEN:uint = 0x008800;
    public static const RED:uint = 0xCC0000;
    public static const GOLD:uint = 0xFFCC66;
    public static const SILVER:uint = 0xCCCCCC;
    public static const BLACK:uint = 0x101010;
    public static const COLORS:/*uint*/Array = [ WHITE, GREEN,  RED, GOLD, SILVER, BLACK ];
    public static function getColors():Array { return COLORS.slice(); }
}
/* for Example. */
class Snow {
    public var x:Number = 0;
    public var y:Number = 0;
    public var vx:Number = 0;
    public var vy:Number = 0;
    public var color:uint = 0x00;
}
/* for debug. */
import flash.display.*;
class ColorMap extends Sprite {
    public function ColorMap(colors:Array) {
        var l:int = colors.length;
        var rect:Sprite;
        for( var i:int=0; i<l; ++i ) {
            addChild( rect = new ColorRect( colors[ i ] ) );
            rect.x =i * ColorRect.WIDTH;
            rect.y =0;
        }
    }
}
/* for debug. */
class ColorRect extends Sprite {
    public static const WIDTH:int = 40;
    public static const HEIGHT:int = 40;
    public function ColorRect( color:uint ) {
        super();
        graphics.beginFill( color, 1 );
        graphics.drawRect( 0, 0, WIDTH, HEIGHT );
        graphics.endFill();
    }
}
//package 
//{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	//import flash.display.Sprite;
	//import flash.events.Event;
	
	//public
	class IconGetter
	{
		private var l:Loader;
		
		private var _bmpf:Function; //<BitmapData,void>
		private var _errf:Function; //<SecurityError,void>
		public function IconGetter(bmpGetter:Function,errGetter:Function):void 
		{
			_bmpf = bmpGetter;
			_errf = errGetter;
			var icon:TwitterIcon = new TwitterIcon(onIconGet);
			
		}
		private function onIconGet(xs:Array):void {
			l = new Loader();
			l.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			l.load(xs[int(xs.length*Math.random())])
		}
		private function onComplete(e:Event):void 
		{
			var bd:BitmapData = new BitmapData(l.width, l.height);
			try{
				bd.draw(l);
				_bmpf(bd);
			}catch (e:SecurityError) {
				_errf(e);
			}
			
		}
	}
//}
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
//import flash.net.
class TwitterIcon
{
	private var _xs:Array;
	public function TwitterIcon(arraySetter:Function)
	{
		var loadTexts:Function = function (rpp:int, arraySetter:Function):void {
			var textLoader:URLLoader = new URLLoader(new URLRequest("http://search.twitter.com/search.atom?q=%23illumination&rpp=" + rpp));
			textLoader.addEventListener(Event.COMPLETE, function(e:Event):void {
				default xml namespace = new Namespace("http://www.w3.org/2005/Atom");
				var htmlText:XML = XML(textLoader.data);
				//var title:XMLList = htmlText.entry.title;
				var imgs:XMLList = htmlText.entry.link.(@type=="image/png").@href;
				var xs:Array = [];
				for each(var i:XML in  imgs)
				{
					xs.push(new URLRequest(i.toString()));
					//xs.push.apply(null,scan(c.toString(),/\[(.+)\]/));
				}
				if (xs.length >= 1)
				{
					//_texts = xs;
					arraySetter(xs);
				}
			});
		}
		loadTexts(10,arraySetter);
	}
}

/**
 * @see http://takumakei.blogspot.com/2009/05/actionscriptrubystringscan.html
 */ 
//package com.blogspot.takumakei.utils
//{
	//public
    function scan(str:String, re:RegExp):Array
    {
        if(!re.global){
            var flags:String = 'g';
            
            if(re.dotall)
                flags += 's';
            if(re.multiline)
                flags += 'm';
            if(re.ignoreCase)
                flags += 'i';
            if(re.extended)
                flags += 'x';

            re = new RegExp(re.source, flags);                    
        }

        var r:Array = [];
        var m:Array = re.exec(str);
        while(null != m){
            if(1 == m.length)
                r.push(m[0]);
            else
                r.push(m.slice(1, m.length));
            m = re.exec(str);
        }
        return r;
    }
//}