In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: start-end point implementation

Get Adobe Flash player
by kifa 05 Apr 2016
    Embed
/**
 * Copyright kifa ( http://wonderfl.net/user/kifa )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/it22
 */

// forked from kross77's start-end point implementation

// forked from kross77's resize rotation tool

package {

    import flash.utils.Proxy;

    import flash.geom.Rectangle;

    

    import flash.display.Graphics;

    import flash.display.Sprite;

    import flash.events.MouseEvent;

    import flash.text.TextField;

    import flash.geom.Point;

    

    public class FlashTest extends Sprite {

        public var centerPoint:Sprite = new Sprite();

        

        

        

        

        public var topLeftDragPoint:Sprite = new Sprite();

        public var topRightDragPoint:Sprite = new Sprite();

        public var bottomLeftDragPoint:Sprite = new Sprite();

        public var bottomRightDragPoint:Sprite = new Sprite();

        

        

        public var dragPoint:Sprite = new Sprite();

        public var rect:Sprite = new Sprite();

        

        private var updatingAngle:Boolean = false;

        private var startAngle:Number;

        private var figureAngle:Number;

        

        public var unRotatedLayer:Sprite = new Sprite();

        

        private var txt:TextField = new TextField();

        

        private var rotateRect:RotateRectangle;

        

        public var rectModel:Rectangle = new Rectangle(0, 0, 100, 50);

        

        public var startDragPoint:Point;

        public var startRectSizePoint:Point;

        public var startRectPosPoint:Point;

        

        private var startPoint:Point = new Point();

        public var endPoint:Point = new Point();

        

        public var pointsLayer:Sprite = new Sprite();

        

        public function FlashTest() {

            // write as3 code here..

            

            startPoint = new Point(100, 150);

            endPoint = new Point(200, 250);

            

            addChild(rect);

            addChild(unRotatedLayer);

            addChild(centerPoint);

            

            addChild(topLeftDragPoint);

            addChild(topRightDragPoint);

            addChild(bottomLeftDragPoint);

            addChild(bottomRightDragPoint);

            

            addChild(dragPoint);

            addChild(txt);

            

            addChild(pointsLayer);

            pointsLayer.mouseEnabled = pointsLayer.mouseChildren = false;

            

            

            createRect();

            //drawRect(rect.graphics);

            

            //drawPoint(centerPoint.graphics, 0xff0000);

            drawPoint(topLeftDragPoint.graphics, 0x0000ff);

            drawPoint(topRightDragPoint.graphics, 0x0000ff);

            drawPoint(bottomLeftDragPoint.graphics, 0x0000ff);

            drawPoint(bottomRightDragPoint.graphics, 0x0000ff);

            drawPoint(dragPoint.graphics, 0xCCCCC);

            

            initDragItems();

            //updateDrawRectPosition(rect.rotation);

            

            setPoints(startPoint, endPoint);

            

            

        }

        

        public function setPoints(startPoint:Point, endPoint:Point):void {

            pointsLayer.graphics.clear();

            rectModel.width = endPoint.x - startPoint.x;

            rectModel.height = endPoint.y - startPoint.y;

            rectModel.x = startPoint.x + rectModel.width / 2;

            rectModel.y = startPoint.y + rectModel.height / 2;

            updateRect();

        }

        

        

        public function drawPoints():void{

            pointsLayer.graphics.clear();

            DrawUtil.drawPoint(pointsLayer.graphics, rotateRect.topLeft, 0x00eeee, .5);

            DrawUtil.drawPoint(pointsLayer.graphics, rotateRect.bottomRight, 0x00eeee, .5);

            

            DrawUtil.drawPoint(pointsLayer.graphics, startPoint, 0xeeee00, .5);

            DrawUtil.drawPoint(pointsLayer.graphics, endPoint, 0xeeee00, .5);

            

            startPoint = new Point(centerPoint.x-rotateRect.width/2, centerPoint.y-rotateRect.height/2);

            endPoint = new Point(centerPoint.x+rotateRect.width/2, centerPoint.y+rotateRect.height/2);

        }

        

        

        public function log(message:String, ...args):void {

            txt.appendText("\n" + message + args.join(" "));

        }

        

        public function clearLog():void {

            txt.text = "";

        }

        

        public function updateRect():void {

            centerPoint.x = rect.x = rectModel.x;

            centerPoint.y = rect.y = rectModel.y;

            DrawUtil.drawRect(rect.graphics, rectModel, 0xeeeeee);

            updateDrawRectPosition(rect.rotation);

            drawPoints();

            var area:Rectangle = calculateRectangleArea();

            graphics.clear();

            DrawUtil.drawRect2(graphics, area, 0xcccccc);

        }

        

        private function calculateRectangleArea():Rectangle{

            var sp:Point = rotateRect.topLeft.clone();

            var ep:Point = rotateRect.topLeft.clone();

            for each (var p:Point in rotateRect.getPoints()){

                sp.x = p.x < sp.x ? p.x : sp.x;

                sp.y = p.y < sp.y ? p.y: sp.y;

                ep.x = p.x > ep.x ? p.x : ep.x;

                ep.y = p.y > ep.y ? p.y : ep.y;

            }

            return new Rectangle(sp.x, sp.y, ep.x-sp.x, ep.y-sp.y);

        }

        

        private function createRect():void {

            DrawUtil.drawRect(rect.graphics, rectModel, 0xEEEEEE);

            txt.text = String(rectModel.x);

            rect.x = centerPoint.x = rectModel.x;

            rect.y = centerPoint.y = rectModel.y;

            updateDrawRectPosition(rect.rotation);

        }

        

        

        private function createDragItem(s:Sprite,vx:int, vy:int):void {

            new DragItem(s, function (e:*):void {

                var vector:Point = new Point(mouseX - startDragPoint.x, mouseY - startDragPoint.y);

                var nonRotateVector:Point = vector.clone();

                vector = rotateVector(vector, rect.rotation - 90);

                

                //update position

                centerPoint.x = rectModel.x = startRectPosPoint.x + nonRotateVector.x / 2;

                centerPoint.y = rectModel.y = startRectPosPoint.y + nonRotateVector.y / 2;

                

                //set direction

                vector.x = vx*vector.x;

                vector.y = vy*vector.y;

                

                //update size

                var rectSize:Point = startRectSizePoint.add(vector);

                rectModel.width = rectSize.x;

                rectModel.height = rectSize.y;

                updateRect();

            }, null, function (e:flash.events.MouseEvent):void {

                startDragPoint = new Point(mouseX, mouseY);

                startRectSizePoint = new Point(rectModel.width, rectModel.height);

                startRectPosPoint = new Point(rect.x, rect.y);

            });

        }

        

        

        private function initDragItems():void {

            createDragItem(bottomRightDragPoint, 1, -1);

            createDragItem(bottomLeftDragPoint, -1, -1);

            createDragItem(topRightDragPoint, 1, 1);

            createDragItem(topLeftDragPoint, -1, 1);

            

            new DragItem(dragPoint,

                    function (e:*):void {

                        var angle:Number = currentAngle();

                        txt.text = "" + angle;

                        if (updatingAngle) {

                            rect.rotation = figureAngle + (angle - startAngle);

                            updateRect();

                            

                            //updatePoints(startPoint, endPoint);

                            

                        }

                    }, function (e:*):void {

                        updatingAngle = false;

                        

                    }, function (e:*):void {

                        updatingAngle = true;

                        startAngle = currentAngle();

                        figureAngle = rect.rotation;

                    });

            //dragPoint.addEventListener(MouseEvent.MOUSE_DOWN, dragPointMouseDownHandler);

            new DragItem(rect, function (e:*):void {

                var vector:Point = new Point(mouseX - startDragPoint.x, mouseY - startDragPoint.y);

                rectModel.x = startRectPosPoint.x + vector.x;

                rectModel.y = startRectPosPoint.y + vector.y;

                updateRect();

                //updateDrawRectPosition(rect.rotation);

            }, null, function (e:*):void {

                startDragPoint = new Point(mouseX, mouseY);

                startRectPosPoint = new Point(rect.x, rect.y);

            })

        }

        

        public function rotateVector(p:Point, rotationAngle:Number):Point {

            var currentMagnitude:Number = Math.sqrt(Math.pow(p.x, 2) + Math.pow(p.y, 2));

            var newAngleRadians:Number = ((Math.atan2(p.x, p.y) * (180 / Math.PI)) + Number(rotationAngle)) * (Math.PI / 180);

            p.x = fixNumber(currentMagnitude * Math.cos(newAngleRadians));

            p.y = fixNumber(currentMagnitude * Math.sin(newAngleRadians));

            return p;

        }

        

        private function fixNumber(numberValue:Number):Number {

            return isNaN(Number(numberValue)) ? 0 : Math.round(Number(numberValue) * 100000) / 100000;

        }

        

        

        

        public function updateDrawRectPosition(angle:Number):void {

            var rotAngle:int = angle - 90;

            var distance:Number = rectModel.height / 2 + 15;

            dragPoint.x = centerPoint.x + (distance * Math.cos(degToRad(rotAngle)));

            dragPoint.y = centerPoint.y + (distance * Math.sin(degToRad(rotAngle)));

            

            var rx:Number = rectModel.x - (rectModel.width) / 2;

            var ry:Number = rectModel.y - (rectModel.height) / 2;

            rotateRect = new RotateRectangle(rx, ry, rectModel.width, rectModel.height, rect.rotation);

            

            

            

            topLeftDragPoint.x = rotateRect.topLeft.x;

            topLeftDragPoint.y = rotateRect.topLeft.y;

            

            topRightDragPoint.x = rotateRect.topRight.x;

            topRightDragPoint.y = rotateRect.topRight.y;

            

            

            bottomLeftDragPoint.x = rotateRect.bottomLeft.x;

            bottomLeftDragPoint.y = rotateRect.bottomLeft.y;

            

            

            bottomRightDragPoint.x = rotateRect.bottomRight.x;

            bottomRightDragPoint.y = rotateRect.bottomRight.y;

            

            

        }

        

        public function getAngle(x1:Number, y1:Number, x2:Number, y2:Number):Number {

            var dx:Number = x2 - x1;

            var dy:Number = y2 - y1;

            return Math.atan2(dy, dx) * 180 / Math.PI + 90;

        }

        

        public function drawPoint(g:Graphics, color:uint):void {

            g.beginFill(color);

            g.drawCircle(0, 0, 5);

        }

        

        public function updateRectangle(angle:int):void {

            rect.rotation = angle;

        }

        

        public function currentAngle():Number {

            return getAngle(centerPoint.x, centerPoint.y, mouseX, mouseY);

        }

        

        public function degToRad(deg:Number):Number {

            var rad:Number = deg * (Math.PI / 180);

            return rad;

        }

    }

}



import flash.display.Graphics;

import flash.display.Sprite;

import flash.events.MouseEvent;

import flash.geom.Rectangle;

import flash.geom.Point;



class DrawUtil {

    public static function drawPoint(g:Graphics, c:Point, color:uint, alpha:Number = 1):void {

        g.beginFill(color, alpha);

        g.drawCircle(c.x, c.y, 5);

    }

    

    public static function drawRect(g:Graphics, r:Rectangle, color:uint = 0xff0000, alpha:Number = 1):void {

        g.clear();

        g.beginFill(color, alpha);

        g.drawRect(-r.width / 2, -r.height / 2, r.width, r.height);

    }

    

    public static function drawRect2(g:Graphics, r:Rectangle, color:uint = 0xff0000, alpha:Number = 1):void {

        g.clear();

        g.beginFill(color, alpha);

        g.drawRect(r.x, r.y, r.width, r.height);

    }

}



class DragItem {

    public var s:Sprite;

    public var onMoveCallback:Function;

    public var onMouseUpCallback:Function;

    private var onMouseDownCallback:Function;

    

    public function DragItem(s:Sprite, onMoveCallback:Function = null, onMouseUpCallback:Function = null, onMouseDownCallback:Function = null) {

        this.s = s;

        this.onMoveCallback = onMoveCallback;

        this.onMouseUpCallback = onMouseUpCallback;

        this.onMouseDownCallback = onMouseDownCallback;

        s.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, s_mouseDownHandler);

        s.buttonMode = true;

    }

    

    private function s_mouseDownHandler(event:MouseEvent):void {

        s.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);

        s.stage.addEventListener(flash.events.MouseEvent.MOUSE_UP, mouseUpHandler);

        if (onMouseDownCallback != null) {

            onMouseDownCallback(event);

        }

    }

    

    private function mouseMoveHandler(event:MouseEvent):void {

        if (onMoveCallback != null) {

            onMoveCallback(event);

        }

    }

    

    private function mouseUpHandler(event:MouseEvent):void {

        s.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);

        s.stage.removeEventListener(flash.events.MouseEvent.MOUSE_UP, mouseUpHandler);

        if (onMouseUpCallback != null) {

            onMouseUpCallback(event);

        }

    }

}



class Align {

    public static function tl(s1:Sprite, s2:Sprite):void {

        s1.x = s2.x;

        s1.y = s2.y;

    }

    

    public static function tr(s1:Sprite, s2:Sprite):void {

        s1.x = s2.width + s2.x;

        s1.y = s2.y;

    }

    

    public static function bl(s1:Sprite, s2:Sprite):void {

        s1.x = s2.x;

        s1.y = s2.y + s2.height;

    }

    

    public static function br(s1:Sprite, s2:Sprite):void {

        s1.x = s2.width + s2.x;

        s1.y = s2.y + s2.height;

    }

}



import flash.geom.Matrix;

import flash.geom.Point;



class RotateRectangle {

    private var _mat:Matrix;

    

    public function RotateRectangle(ix:Number, iy:Number, width:Number, height:Number, rot:Number = 0) {

        

        _mat = new Matrix(width, 0, 0, height, ix, iy);

        

        if (rot) {

            _mat.translate(-(ix + (width / 2)), -( iy + (height / 2)));

            _mat.rotate((rot / 180) * Math.PI);

            _mat.translate(ix + (width / 2), iy + (height / 2));

        }

    }

    

    public function get x():Number {

        return _mat.tx;

    }

    

    public function set x(value:Number):void {

        _mat.tx = value;

    }

    

    public function get y():Number {

        return _mat.ty;

    }

    

    public function set y(value:Number):void {

        _mat.ty = value;

    }

    

    public function get width():Number {

        return Math.sqrt(_mat.a * _mat.a + _mat.b * _mat.b);

    }

    

    public function set width(value:Number):void {

        var scx:Number = this.width;

        

        if (scx) {

            var ratio:Number = value / scx;

            

            _mat.a *= ratio;

            _mat.b *= ratio;

        }

        else {

            //if tmp was 0, set scaleX from skewY

            var sky:Number = Math.atan2(_mat.b, _mat.a);

            _mat.a = Math.cos(sky) * value;

            _mat.b = Math.sin(sky) * value;

        }

    }

    

    

    public function get height():Number {

        return Math.sqrt(_mat.c * _mat.c + _mat.d * _mat.d);

    }

    

    public function set height(value:Number):void {

        var scy:Number = this.height;

        

        if (scy) {

            var ratio:Number = value / scy;

            

            _mat.c *= ratio;

            _mat.d *= ratio;

        }

        else {

            //if tmp was 0, set scaleY from skewX

            var skx:Number = Math.atan2(-_mat.c, _mat.d);

            

            _mat.c = -Math.sin(skx) * value;

            _mat.d = Math.cos(skx) * value;

        }

    }

    

    

    public function get rotation():Number {

        return Math.atan2(_mat.b, _mat.a);

    }

    

    public function set rotation(value:Number):void {

        _mat.rotate(value - this.rotation);

    }

    

    public function get topLeft():Point {

        return new Point(_mat.tx, _mat.ty);

    }

    

    public function get topRight():Point {

        return new Point(_mat.tx + _mat.a, _mat.ty + _mat.b);

    }

    

    public function get bottomLeft():Point {

        return new Point(_mat.tx + _mat.c, _mat.ty + _mat.d);

    }

    

    public function get bottomRight():Point {

        return new Point(_mat.tx + _mat.a + _mat.c, _mat.ty + _mat.b + _mat.d);

    }

    

    public function getPoints():Array {

        return [topLeft, bottomRight, topRight, bottomLeft];

    }

}