Dot Point Maker

by littlepad
♥0 | Line 107 | Modified 2013-04-27 15:00:22 | MIT License
play

ActionScript3 source code

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

package {
    import flash.text.TextField;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        private const HORIZONTAL_NUM:uint = 11;
        private const VERTICAL_NUM:uint = 11;
        private const DOT_WIDTH:uint = 18;
        private const DOT_HEIGHT:uint = 18;
        private const DOT_MARGIN:uint = 1;
        private var _tf:TextField;
        private var _dots:Array;
        
        public function FlashTest() {
            init();
        }
        
        private function init():void
        {
            _dots = new Array();
            for(var i:uint = 0; i < HORIZONTAL_NUM; i++){
                for (var j:uint = 0; j < VERTICAL_NUM; j++){
                    var dot:Dot = new Dot(i, j, DOT_WIDTH, DOT_HEIGHT, callback);
                    dot.x = (dot.width + DOT_MARGIN) * i;
                    dot.y = (dot.height + DOT_MARGIN) * j;
                    _dots.push(dot);
                    addChild(dot);
                }
            }
            
            _tf = new TextField();
            _tf.width = 400;
            _tf.height = 200;
            _tf.background = true;
            _tf.backgroundColor = 0xEEEEEE;
            _tf.multiline = true;
            _tf.wordWrap = true;
            _tf.y = 230;
            addChild(_tf);
        }
        
        private function callback():void
        {
            var pointStr:String = "";
            for each(var dot:Dot in _dots){
                if(dot.isSelected){
                    if(pointStr.length != 0) pointStr += ", ";
                    pointStr += "{x:" + dot.point.x + ", y:" + dot.point.y + "}";
                }
            }
            _tf.text = pointStr;
        }
    }
}
import flash.geom.Point;
import flash.events.MouseEvent;
import flash.display.Sprite;

class Dot extends Sprite {
    private var _posX:int;
    private var _posY:int;
    private var _w:uint;
    private var _h:uint;
    private var _callback:Function;
    private var _isSelected:Boolean;
    private const DEFAULT_COLOR:uint = 0xFF0000;
    private const SELECTED_COLOR:uint = 0x000000;
    
    public function Dot(posX:int, posY:int, w:uint, h:uint, callback:Function)
    {
        _posX = posX;
        _posY = posY;
        _w = w;
        _h = h;
        _callback = callback;
        init();
    }
    
    private function init():void
    {
        setDefaultColor();
        this.buttonMode = true;
        this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
    }

    private function onMouseDownHandler(event:MouseEvent):void
    {
        if(_isSelected){
            setDefaultColor();
        } else {
            setSelectedColor();
        }
        _isSelected = !_isSelected;
        _callback();
    }
    
    private function setDefaultColor():void
    {
        this.graphics.beginFill(DEFAULT_COLOR);
        this.graphics.drawRect(0, 0, _w, _h);
        this.graphics.endFill();
    }
    
    private function setSelectedColor():void
    {
        this.graphics.beginFill(SELECTED_COLOR);
        this.graphics.drawRect(0, 0, _w, _h);
        this.graphics.endFill();
    }
    
    public function get isSelected():Boolean
    {
        return _isSelected;
    }

    public function get point():Point
    {
        return new Point(_posX, _posY);
    }


}