flash on 2009-9-24
...
@author PB
/**
* Copyright pb_praha ( http://wonderfl.net/user/pb_praha )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/rlvw
*/
package
{
import com.bit101.components.PushButton;
import com.bit101.components.Window;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Rectangle;
import flash.utils.ByteArray;
import flash.utils.Timer;
/**
* ...
* @author PB
*/
public class Main extends Sprite
{
private var controls:Controls;
private var tick:Timer;
private var canvas:BitmapData;
private var prog:uint;
private var inProc:ByteArray;
private var outProc:ByteArray;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
// CANVAS //
canvas = new BitmapData (stage.stageWidth, stage.stageHeight, false, 0xFFFFFF);
addChild (new Bitmap(canvas)) as Bitmap;
// CONTROLS //
controls = addChild (new Controls(this, 20, 20, 'Controls')) as Controls;
controls.addEventListener ('RUN_RULE', on_RUN_RULE, false, 0, true);
controls.addEventListener ('PAUSE_RULE', on_PAUSE_RULE, false, 0, true);
controls.addEventListener ('RESET_ALL', on_RESET_ALL, false, 0, true);
// TIMER //
tick = new Timer (controls.clockSpeed * 10);
tick.addEventListener (TimerEvent.TIMER, on_TICK, false, 0, true);
on_RESET_ALL(null);
}
private function on_RESET_ALL(e:Event):void
{
on_PAUSE_RULE(e);
canvas.fillRect(canvas.rect, 0x00FF00);
if (!controls.randomInitialConditions){
canvas.setPixel(Math.floor(canvas.width / 2), 0, 0);
}else {
for ( var x:uint = 0; x < canvas.width; x++) {
if (Math.random()>.5) canvas.setPixel(x, 0, 0);
}
}
prog = 0;
}
private function on_PAUSE_RULE(e:Event):void
{
tick.stop();
}
public function on_RUN_RULE(e:Event):void {
tick.start();
}
private function on_TICK(e:TimerEvent):void
{
inProc = canvas.getPixels (new Rectangle (0, prog, canvas.width, 1));
outProc = new ByteArray();
var nnValue:uint;
var i:uint;
var c:uint;
var rule:uint = controls.rule;
for ( i = 0; i < inProc.length ; i += 4 ) {
nnValue = 0;
for ( c = 0; c < 3; c++) {
inProc.position = (i + (c*4)) % inProc.length;
nnValue += ((inProc.readUnsignedInt() - 0xFF000000 == 0) ? 1 : 0) << (2-c);
}
outProc.position = (i + 4 < inProc.length) ? i + 4 : 0;
outProc.writeUnsignedInt ( ( (rule >> (nnValue)) & 1) ? 0 : 0xFFFF0000);
}
prog++;
outProc.position = 0;
canvas.setPixels(new Rectangle (0, prog, canvas.width, 1), outProc);
}
}
}
import com.bit101.components.CheckBox;
import com.bit101.components.InputText;
import com.bit101.components.Knob;
import com.bit101.components.Label;
import com.bit101.components.PushButton;
import com.bit101.components.Text;
import com.bit101.components.Window;
import flash.display.DisplayObjectContainer;
import flash.events.Event;
class Controls extends Window {
private var _clockSpeed:uint = 1;
private var runButton:PushButton;
private var pauseButton:PushButton;
private var resetButton:PushButton;
private var ruleNumber:InputText;
private var plusButton:PushButton;
private var minusButton:PushButton;
private var ricCheckbox:CheckBox;
private var _randomInitialConditions:Boolean;
function Controls (parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, title:String = "Window") {
super (parent, xpos, ypos, title);
setSize (210,200);
minusButton = content.addChild (new PushButton(this, 10, 10, '<', on_CLICK)) as PushButton;
minusButton.setSize (20,20);
ruleNumber = content.addChild (new InputText(this, 35, 10, '30')) as InputText;
ruleNumber.setSize (40, 20);
ruleNumber.restrict = '0-9';
ruleNumber.maxChars = 3;
ruleNumber.addEventListener (Event.CHANGE, on_TEXT_CHANGE, false, 0, true);
plusButton = content.addChild (new PushButton(this, 80, 10, '>', on_CLICK)) as PushButton;
plusButton.setSize (20,20);
ricCheckbox = content.addChild (new CheckBox(this, 10, 40, 'Random initial conditions', on_CLICK)) as CheckBox;
runButton = content.addChild (new PushButton(this, 10, 60, 'Run', on_CLICK)) as PushButton;
runButton.setSize(40, 20);
pauseButton = content.addChild (new PushButton(this, 10, 60, 'Pause', on_CLICK)) as PushButton;
pauseButton.setSize(40, 20);
pauseButton.visible = false;
resetButton = content.addChild (new PushButton(this, 55, 60, 'Reset', on_CLICK)) as PushButton;
resetButton.setSize(40, 20);
}
private function on_TEXT_CHANGE(e:Event):void
{
ruleNumber.text = Math.min (uint (ruleNumber.text), 256).toString();
}
private function on_CLICK(e:Event):void
{
trace (e.target)
switch (e.target) {
case plusButton:
ruleNumber.text = Math.min (256, uint(ruleNumber.text) + 1).toFixed();
break;
case minusButton:
ruleNumber.text = Math.max (1, uint(ruleNumber.text) - 1).toFixed();
break;
case runButton:
runButton.visible = pauseButton.visible;
pauseButton.visible = !runButton.visible ;
dispatchEvent (new Event('RUN_RULE'));
break;
case pauseButton:
pauseButton.visible = runButton.visible ;
runButton.visible = !pauseButton.visible;
dispatchEvent (new Event('PAUSE_RULE'));
break;
case resetButton:
pauseButton.visible = false ;
runButton.visible = true;
dispatchEvent (new Event('RESET_ALL'));
break;
}
}
public function get clockSpeed():uint { return _clockSpeed; }
public function set clockSpeed(value:uint):void
{
_clockSpeed = value;
}
public function get rule():uint { return uint(ruleNumber.text); }
public function get randomInitialConditions():Boolean { return ricCheckbox.selected; }
}