Observer and simple Console

by Cheshir
Just type in the text box "-p" and then press "Enter" 5 times or so...
♥0 | Line 165 | Modified 2014-11-20 23:26:05 | MIT License
play

ActionScript3 source code

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

package {
    import flash.filters.GlowFilter;
    import flash.text.TextField;
    import flash.display.Sprite;

    public class FlashTest extends Sprite {
        private var myConsole:Console;
        private var myObserver:Observer;
        private var galaxyHolder:Sprite;
        
        public function FlashTest() {
            // write as3 code here..
            myConsole = new Console(stage);
            myObserver = new Observer(stage);
            galaxyHolder = new Sprite();
            var galColor:uint = 0xffffff*Math.random()+0xaaaaaa;
            myConsole.log('now color '+galColor);
            galaxyHolder.filters = [new GlowFilter(galColor,.9,16,16,2)];    // my favorite filter
            this.graphics.beginFill(0);
            this.graphics.drawRect(0,0,600,600);
            addChild(galaxyHolder);
            myConsole.addComand('-p',this.drawCircle, 'draw random particle');
            myConsole.addComand('-cl',myObserver.removeAll, 'clear obserber');
            myConsole.addComand('-l-O',this.logObserv,'logged subscribed length')
            myConsole.help();
        }
        private function logObserv():void {
            myConsole.log(myObserver.numSubscr);
        }
        private function drawCircle():void {
            for(var i:int = 0; i<50; i++){
                var p:ISubscr = new Particle(Math.random()*900-450, Math.random()*900-450, stage, galaxyHolder);
                myObserver.addSubscr(p);
            }
        }
    // it would be cool to be able to pass the arguments ... from console
    // okay training is over, now try to do something

    }
}
import flash.geom.Point;
import flash.events.Event;
import flash.utils.Dictionary;
import flash.ui.Keyboard;
import flash.events.FocusEvent;
import flash.text.TextFieldType;
import flash.events.TextEvent;
import flash.events.KeyboardEvent;
import flash.display.Stage;
import flash.text.TextField;
import flash.display.Sprite;

Class{
    class Observer {
        private var subscribers:Array = [];
        public function Observer(stage:Stage) {
            stage.addEventListener(Event.ENTER_FRAME, updateAll);
        }
        public function updateAll(e:Event):void {
            for each(var sub:ISubscr in subscribers){
                sub.update();
            }
        }
        public function addSubscr(sub:ISubscr):void{
            subscribers.push(sub);
        }
        public function removeSubscr(sub:ISubscr):void{
            for(var i:int=0; i<subscribers.length; i++){
                if(subscribers[i] == sub){
                    subscribers.slice(i, 1);
                }
            }
        }
        public function removeAll():void{
            for(var i:int=0; i<subscribers.length; i++){
                subscribers[i].destroy();
                subscribers[i] = null;
            }
            subscribers = [];
        }
        public function get numSubscr():Number {
            return subscribers.length;
        }
    }
}

Class {
    class Particle extends Sprite implements ISubscr {
        private var dx:Number;
        private var dy:Number;
        private var rad:Number;
        private var center:Point;
        public function Particle(x:Number, y:Number, stage:Stage, parent:Sprite){
            this.x = x;
            this.y = y;
            center = new Point(stage.stageWidth/2, stage.stageHeight/2);
            var type:uint = uint(Math.random()*3);
            (type>=1)?this.graphics.beginFill(0xffffff):this.graphics.beginFill(0xffffff,.07);
            (type>=1)?this.graphics.drawCircle(0,0,2*Math.random()*Math.random()) : this.graphics.drawCircle(0,0,11);
            parent.addChild(this);
        }
        public function update():void{
            dx = center.x - this.x;
            dy = center.y - this.y;
            rad = Math.atan2(dy,dx)+1.4835298642;    // Collapse to center 
            this.x += Math.cos(rad);
            this.y += Math.sin(rad);
        }
        public function destroy():void{
            parent.removeChild(this);
        }
    }
}


Class {
    interface ISubscr {
        function update():void;
        function destroy():void;
    }
}


Class
{
    class Console extends Sprite {
        private var consoleField:TextField = new TextField();
        
        private var descript:Array = ['help', 'clear console'];
        
        private var diction:Dictionary = new Dictionary();
        
        public function Console(stage:Stage) {
            consoleField.background = true;
            consoleField.type = TextFieldType.INPUT; 
            consoleField.backgroundColor = 0x333333;
            consoleField.alpha = .6;
            consoleField.textColor = 0xf2f9ff;
            consoleField.width = stage.stageWidth;
            consoleField.scaleX = consoleField.scaleY = 1.4;
            stage.addChild(consoleField);
            log(this);
            consoleField.addEventListener(FocusEvent.FOCUS_IN, clear);
            consoleField.addEventListener(KeyboardEvent.KEY_UP, upKey);
            diction['-h'] = {func: help, descript: 'help function trace'};
            diction['-c'] = {func: clear, descript: 'clear work zone'};
        }
        public function log(str:Object):void {
            if(!str is String){
                str.toString();
            }
            consoleField.appendText(str+'\n');
        }
        
        public function addComand(comand:String, toCall:Function, desc:String="no descript"):void{
            diction[comand] = {func: toCall, descript: desc};
        }

        private function upKey(e:KeyboardEvent):void{
            if(e.keyCode == Keyboard.ENTER){
                // parse
                var key:String = '';
                for(var i:int = 1; i<=4; i++){
                    key = consoleField.text.charAt(consoleField.text.length-i)+key;
                    if(diction[key] != null){
                        (diction[key].func as Function).call();
                        return;
                    }
                }
            }
        }
        
        private function clear(event:FocusEvent = null):void{
            consoleField.text = '';
        }
        
        public function help():void {
            log('This is help - default');
            for (var key:Object in diction) 
            { 
                log(key +"\t is "+ diction[key].descript); 
            } 

        }

    }

}

Forked