Fl038_Main.as (Main)

Fl038_Main.as
package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import com.borealkiss.controls.P5IconController;

    [SWF(width="600",height="600",backgroundColor="0xFFFFFF")]
    public class Fl038_Main extends Sprite{
        private var _controller:P5IconController;
        
        public function Fl038_Main(){
            var canvas:Sprite = new Sprite();
            this.addChild(canvas);
            _controller = new P5IconController(canvas);
            _controller.draw();
            stage.addEventListener(MouseEvent.CLICK,onClick);
        }
        
        private function onClick(e:MouseEvent):void{
            _controller.clear();
            _controller.draw();
        }
    }
}

com.borealkiss.controls.ViewController.as (Abstract)

ViewController.as
/**
 * @date 21/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.controls{
    import flash.display.DisplayObjectContainer;
    import flash.errors.IllegalOperationError;
    
    /**
     * Abstract class
     */ 
    public class ViewController{
        protected var _target:DisplayObjectContainer;
        
        /**
         * Constructor
         */     
        public function ViewController(target:DisplayObjectContainer){
            _target = target;
            
            if (Object(this) == ViewController){
                throw new IllegalOperationError(
                    "Abstract class must be inherited in a subclass");
            }
        }
        
        public function draw():void{
            //Must be overridden in a subclass.
        }
    }
}

com.borealkiss.controls.P5IconController.as

P5IconController.as
/**
 * @date 27/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.controls{
    import flash.display.Graphics;
    import flash.display.DisplayObjectContainer;
    import flash.filters.DropShadowFilter;
    import com.borealkiss.controls.ViewController;
    import com.borealkiss.display.shapes.Circle;
    import com.borealkiss.display.shapes.RoundRect;
    import com.borealkiss.display.shapes.Line;
    import com.borealkiss.display.StartIcon;
    import com.borealkiss.display.StopIcon;
    import com.borealkiss.display.P;
    
    public class P5IconController extends ViewController{
        protected const STAGE_WIDTH:Number = 600;
        protected const STAGE_HEIGHT:Number = 600;
        protected const ICON_SIZE:Number = 512;
        protected const PADDING_X:Number = (STAGE_WIDTH - ICON_SIZE)/2;
        protected const PADDING_Y:Number = (STAGE_HEIGHT - ICON_SIZE)/2;
        
        //For circles
        protected const LIMIT:uint = 15;
        protected const MAX_RAD:Number = 150;
        protected const ALPHA:Number = 0.2;
        protected const FILL_COLOR:uint = 0x5885;
        protected var _circles:Array;
        
        /**
         * Constructor
         */ 
        public function P5IconController(target:DisplayObjectContainer){
            super(target);
        }
        
        override public function draw():void{
            this.setup();
            
            _circles = new Array();
            for (var i:int=0; i<LIMIT; i++){
                var x:Number = STAGE_WIDTH*Math.random();
                var y:Number = STAGE_HEIGHT*Math.random();
                var radius:Number = MAX_RAD*(Math.random() + 0.4);
                var circle:Circle = new Circle(radius,false,0x0,ALPHA,true,FILL_COLOR);
                circle.x = x;
                circle.y = y;
                _target.addChild(circle);
                _circles.push(circle);
                
                var smallCircle:Circle = new Circle(3,false,0x0,1,true,0xFFFFFF);
                smallCircle.x = circle.x;
                smallCircle.y = circle.y;
                _target.addChild(smallCircle);
            }
            this.connectByLines();
            this.finishUp();
        }
        
        //Icon contour
        protected function setup():void{
            var rect:RoundRect = new RoundRect(PADDING_X,PADDING_Y,
                                            ICON_SIZE,ICON_SIZE,ICON_SIZE/5,false,0x0,1,true,0x0);
            _target.addChild(rect);
            var mask:RoundRect = new RoundRect(PADDING_X,PADDING_Y,
                                            ICON_SIZE,ICON_SIZE,ICON_SIZE/5,false,0x0,1,true,0x0);
            _target.mask = mask;
            _target.filters = [this.dropShadowFilter()];
            
            
        }
        
        protected function dropShadowFilter():DropShadowFilter{
            var shadow:DropShadowFilter = new DropShadowFilter();
            shadow.alpha = 1;
            shadow.blurX = 30;
            shadow.blurY = 30;
            shadow.color = 0x0;
            return shadow;
        }
        
        protected function connectByLines():void{
            for (var i:int=0; i<LIMIT; i++){
                for (var j:int=0; j<LIMIT; j++){
                    if (i != j){
                        if (isOverlapped(_circles[i],_circles[j])){
                            var startX:Number = _circles[i].x;
                            var startY:Number = _circles[i].y;
                            var endX:Number = _circles[j].x;
                            var endY:Number = _circles[j].y;
                            var line:Line = new Line(startX,startY,endX,endY,0,0xFFFFFF,1);
                            _target.addChild(line);
                        }
                    }
                }
            }
        }
        
        //Add icons
        protected function finishUp():void{
            //Start icon            
            var startIcon:StartIcon = new StartIcon(ICON_SIZE/15);
            startIcon.x = PADDING_X + ICON_SIZE*0.65;
            startIcon.y = PADDING_Y + ICON_SIZE*0.8;
            _target.addChild(startIcon);
            
            //Stop icon
            var stopIcon:StopIcon = new StopIcon(ICON_SIZE/15);
            stopIcon.x = PADDING_X + ICON_SIZE*0.8;
            stopIcon.y = startIcon.y;
            _target.addChild(stopIcon);
            
            //P
            var xPos:Number = PADDING_X + ICON_SIZE*0.35;
            var yPos:Number = PADDING_Y + ICON_SIZE*0.15;
            var p:P = new P(_target,ICON_SIZE,xPos,yPos);
            p.x = xPos
            p.y = yPos
            _target.addChild(p);
        }
        
        protected function isOverlapped(circle1:Circle,circle2:Circle):Boolean{
            var dx:Number = circle1.x - circle2.x;
            var dy:Number = circle1.y - circle2.y;
            var distance:Number = Math.sqrt(dx*dx + dy*dy);
            
            if (distance < circle1.radius + circle2.radius){
                return true;
            }
            return false;
        }
        
        public function clear():void{
            while (_target.numChildren > 0){
                _target.removeChildAt(0);
            }
        }
    }
}

com.borealkiss.display.StartIcon.as

StartIcon.as
/**
 * @date 26/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display{
    import flash.display.Shape;
    import flash.display.Graphics;
    
    public class StartIcon extends Shape{ 
        public function StartIcon(radius:Number){
            var g:Graphics = this.graphics;
            g.beginFill(0xFFFFFF);
            g.drawCircle(0,0,radius);
            g.endFill();
            
            g.beginFill(0x0);
            for (var i:int=0; i<3; i++){
                var theta:Number = 2/3*Math.PI*i;
                var x:Number = radius/2*Math.cos(theta);
                var y:Number = radius/2*Math.sin(theta);
                
                if (i==0){
                    g.moveTo(x,y);
                }
                else{
                    g.lineTo(x,y);
                }
            }
            g.endFill();
        }
    }
}

com.borealkiss.display.StopIcon.as

StopIcon.as
/**
 * @date 26/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display{
    import flash.display.Shape;
    import flash.display.Graphics;
    
    public class StopIcon extends Shape{
        public function StopIcon(radius:Number){
            var g:Graphics = this.graphics;
            g.beginFill(0xFFFFFF);
            g.drawCircle(0,0,radius);
            g.endFill();
            
            g.beginFill(0x0);
            var w:Number = radius*0.6;
            g.drawRect(-w/2,-w/2,w,w);
            g.endFill();
        }
    }
}

com.borealkiss.display.P.as

P.as
/**
 * @date 27/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display{
    import flash.display.DisplayObjectContainer;
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Graphics;
    import flash.geom.Matrix;
    
    public class P extends Sprite{
        protected var _target:DisplayObjectContainer;        
        protected var _size:Number;
        protected var _xPos:Number;
        protected var _yPos:Number;
        
        /**
         * Constructor
         * 
         * @param target The parent object.
         * @param size Size of the the parent object.
         * @param xPos x-position of this object on the parent coordinates (i.e., this.x).
         * @param yPos y-position of this object on the parent coordinates (i.e., this.y).
         */ 
        public function P(target:DisplayObjectContainer,size:Number,xPos:Number,yPos:Number){
            _target = target;
            _size = size;
            _xPos = xPos;
            _yPos = yPos;
            this.draw();
        }
        
        protected function draw():void{
            this.addChild(this.outerShape());

            var bitmap:Bitmap = new Bitmap(this.backgroundBitmapData());
            this.addChild(bitmap);
            
            var mask:Sprite = this.innerShape();
            this.addChild(mask);
            bitmap.mask = mask;
        }
        
        //Outer shape of P
        protected function outerShape():Sprite{
            var sprite:Sprite = new Sprite();
            var g:Graphics = sprite.graphics;
            
            g.beginFill(0xFFFFFF);
            g.drawRoundRectComplex(0,0,_size*0.35,_size*0.45,0,_size*0.1,0,_size*0.1);
            g.endFill();
            
            g.beginFill(0xFFFFFF);
            g.drawRect(0,0,_size*0.08,_size*0.7);
            g.endFill();
            
            return sprite;
        }
        
        //Inner shape of P
        protected function innerShape():Sprite{
            var sprite:Sprite = new Sprite();
            var g:Graphics = sprite.graphics;
            
            g.beginFill(0x00FFFF,0.5);
            g.drawRoundRectComplex(_size*0.08,_size*0.08,
                                    _size*0.2,_size*0.3,0,_size*0.05,0,_size*0.05);
            g.endFill();
            
            return sprite;
        }
        
        //Draw the target image at the back of the inner shape of P.
        //_xPos and _yPos will be exactly the same as this.x and this.y, respectively.
        //However these values must be assigned when the constructor is callsed.
        protected function backgroundBitmapData():BitmapData{
            var offsetX:Number = _xPos;
            var offsetY:Number = _yPos;
            var matrix:Matrix = new Matrix();
            matrix.translate(-offsetX,-offsetY);
            
            var bitmapData:BitmapData = new BitmapData(_size,_size,true,0x0);
            bitmapData.draw(_target,matrix);
            return bitmapData;
        }
    }
}

com.borealkiss.display.shapes.Circle.as

Circle.as
/**
 * @date 26/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display.shapes{
    import flash.display.Shape;
    import flash.display.Graphics;
    
    public class Circle extends Shape{
        protected var _radius:Number;
        public function get radius():Number{
            return _radius;
        }
        
        protected var _lineDrawing:Boolean;
        protected var _stroke:uint;
        protected var _alpha:Number;
        protected var _filling:Boolean;
        protected var _fillColor:uint;
        
        /**
         * Constructor
         */ 
        public function Circle(radius:Number,
                                lineDrawing:Boolean=true,stroke:uint=0x0,
                                alpha:Number=0.1,
                                filling:Boolean=false,fillColor:uint=0x0){
            
            _radius = radius;
            _lineDrawing = lineDrawing;
            _stroke = stroke;
            _alpha = alpha;
            _filling = filling;
            _fillColor = fillColor;
            this.init();
        }
        
        protected function init():void{
            var g:Graphics = this.graphics;
            
            if (_lineDrawing){
                g.lineStyle(0,_stroke,_alpha);
            }
            
            if (_filling){
                g.beginFill(_fillColor,_alpha);
            }
            
            g.drawCircle(0,0,_radius);
        }
    }
}

com.borealkiss.display.shapes.Line.as

Line.as
/**
 * @date 26/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display.shapes{
    import flash.display.Shape;
    import flash.display.Graphics;
    
    public class Line extends Shape{
        public function Line(startX:Number,startY:Number,endX:Number,endY:Number,
                                thickness:Number=0,color:uint=0x0,alpha:Number=1){
                                    
            var g:Graphics = this.graphics;
            g.lineStyle(thickness,color,alpha);
            g.moveTo(startX,startY);
            g.lineTo(endX,endY);
        }
    }
}

com.borealkiss.display.shapes.RoundRect.as

RoundRect.as
/**
 * @date 26/11/2008
 * @author borealkiss
 * @link http://blog.boreal-kiss.com/
 */
package com.borealkiss.display.shapes{
    import flash.display.Shape;
    import flash.display.Graphics;
    
    public class RoundRect extends Shape{
        public function RoundRect(x:Number,y:Number,width:Number,height:Number,ellipseWidth:Number,
                                    lineDrawing:Boolean=false,stroke:uint=0x0,
                                    alpha:Number = 0.1,
                                    filling:Boolean=true,fillColor:uint=0x0){
            
            var g:Graphics = this.graphics;
            
            if (lineDrawing){
                g.lineStyle(0,stroke,alpha);
            }
            
            if (filling){
                g.beginFill(fillColor,alpha);
            }
            
            g.drawRoundRect(x,y,width,height,ellipseWidth);
        }
    }
}
Powered by boreal-kiss.com 2008.