SubjectTransformator
/**
* Copyright jozefchutka ( http://wonderfl.net/user/jozefchutka )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/hYLC
*/
package
{
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
[SWF(width="465", height="465", frameRate="30", backgroundColor="#FFFFFF")]
public class Transform extends Sprite
{
private var url1:String = "http://i164.photobucket.com/albums/u7/grissinni/nature-3.jpg";
private var url2:String = "http://i53.photobucket.com/albums/g64/iluvtaylor72/Nature_Loving_Anime_Girl.jpg";
public function Transform()
{
var loader:Loader;
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, complete);
loader.load(new URLRequest(url1));
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, complete);
loader.load(new URLRequest(url2));
}
private function complete(event:Event):void
{
var loaderInfo:LoaderInfo = LoaderInfo(event.currentTarget);
var loader:Loader = loaderInfo.loader;
addChild(loader);
loader.x = Math.random() * 100 + 80;
loader.y = Math.random() * 100 + 120;
loader.width = Math.random() * loader.width + 50;
loader.height = Math.random() * loader.height + 50;
loader.rotation = Math.random() * 30;
var transformator:SubjectTransformator = new SubjectTransformator();
addChild(transformator);
transformator.subject = loader;
transformator.draggable = true;
transformator.addResizer(rs(), SubjectTransformator.POSITION_LT);
transformator.addResizer(rs(), SubjectTransformator.POSITION_RB);
transformator.addRotator(ro(), SubjectTransformator.POSITION_RT);
transformator.addRotator(ro(), SubjectTransformator.POSITION_LB);
transformator.transformatorMatchSubject();
}
private function rs():Sprite
{
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0);
sprite.graphics.drawRect(0, 0, 10, 10);
sprite.graphics.beginFill(0xffffff);
sprite.graphics.drawRect(1, 1, 8, 8);
sprite.graphics.endFill();
return sprite;
}
private function ro():Sprite
{
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0);
sprite.graphics.drawRect(0, 0, 10, 10);
sprite.graphics.beginFill(0xffff00);
sprite.graphics.drawRect(1, 1, 8, 8);
sprite.graphics.endFill();
return sprite;
}
}
}
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
class SubjectTransformator extends Sprite
{
public static const POSITION_LT:uint = 0;
public static const POSITION_RT:uint = 1;
public static const POSITION_RB:uint = 2;
public static const POSITION_LB:uint = 3;
protected var _subject:DisplayObject;
protected var _draggable:Boolean;
protected var resizers:Object = {};
protected var rotators:Object = {};
protected var shape:Sprite;
protected var shapeWidth:Number;
protected var shapeHeight:Number;
protected var lastMouse:Point;
public function SubjectTransformator()
{
super();
}
public function get subject():DisplayObject
{
return _subject;
}
public function set subject(value:DisplayObject):void
{
if(value == _subject)
return;
_subject = value;
}
public function set draggable(value:Boolean):void
{
_draggable = value;
}
public function get draggable():Boolean
{
return _draggable;
}
public function addResizer(resizer:DisplayObject, position:uint):void
{
removeResizer(position);
resizer.x = -resizer.width / 2;
resizer.y = -resizer.height / 2;
var wrapper:Sprite = new Sprite();
wrapper.addChild(resizer);
wrapper.rotation = position * 90;
wrapper.addEventListener(MouseEvent.MOUSE_DOWN, resizerDown);
resizers[position] = wrapper;
addChild(wrapper);
}
public function removeResizer(position:uint):void
{
var wrapper:Sprite = resizers[position];
if(!wrapper)
return;
if(wrapper.parent)
wrapper.parent.removeChild(wrapper);
wrapper.removeEventListener(MouseEvent.MOUSE_DOWN, resizerDown);
delete resizers[position];
}
public function addRotator(rotator:DisplayObject, position:uint):void
{
removeRotator(position);
rotator.x = -rotator.width / 2;
rotator.y = -rotator.height / 2;
var wrapper:Sprite = new Sprite();
wrapper.addChild(rotator);
wrapper.rotation = position * 90;
wrapper.addEventListener(MouseEvent.MOUSE_DOWN, rotatorDown);
rotators[position] = wrapper;
addChild(wrapper);
}
public function removeRotator(position:uint):void
{
var wrapper:Sprite = rotators[position];
if(!wrapper)
return;
if(wrapper.parent)
wrapper.parent.removeChild(wrapper);
wrapper.removeEventListener(MouseEvent.MOUSE_DOWN, rotatorDown);
delete rotators[position];
}
public function subjectMatchTransformator():void
{
if(!subject)
return;
shape.width = shapeWidth;
shape.height = shapeHeight;
subject.rotation = 0;
subject.width = shapeWidth;
subject.height = shapeHeight;
subject.rotation = rotation;
var shapeRectangle:Rectangle = shape.getRect(parent);
var objectRectangle:Rectangle = subject.getRect(parent);
subject.x += shapeRectangle.x - objectRectangle.x;
subject.y += shapeRectangle.y - objectRectangle.y;
positionResizers(shapeWidth, shapeHeight);
positionRotators(shapeWidth, shapeHeight);
}
public function transformatorMatchSubject():void
{
if(!subject)
return;
var r:Number = subject.rotation;
subject.rotation = 0;
var w:Number = subject.width;
var h:Number = subject.height;
subject.rotation = r;
rotation = r;
var objectRectangle:Rectangle = subject.getRect(parent);
x = objectRectangle.x + objectRectangle.width / 2;
y = objectRectangle.y + objectRectangle.height / 2;
createShape(w, h);
positionResizers(w, h);
positionRotators(w, h);
}
protected function createShape(w:Number, h:Number):void
{
shapeWidth = w;
shapeHeight = h;
if(!shape)
{
shape = new Sprite();
shape.addEventListener(MouseEvent.MOUSE_DOWN, shapeDown);
addChildAt(shape, 0);
}
shape.graphics.clear();
shape.graphics.beginFill(0, 0);
shape.graphics.drawRect(-w / 2, -h / 2, w, h);
shape.graphics.endFill();
shape.width = w;
shape.height = h;
}
protected function positionResizers(w:Number, h:Number):void
{
var wrapper:Sprite, key:String, position:uint;
var isTop:Boolean, isLeft:Boolean;
for(key in resizers)
{
position = uint(key);
wrapper = resizers[position];
isLeft = position == POSITION_LT || position == POSITION_LB;
isTop = position == POSITION_LT || position == POSITION_RT;
wrapper.x = isLeft ? (-w / 2) : (w / 2);
wrapper.y = isTop ? (-h / 2) : (h / 2);
}
}
protected function positionRotators(w:Number, h:Number):void
{
var wrapper:Sprite, key:String, position:uint;
var isTop:Boolean, isLeft:Boolean;
for(key in rotators)
{
position = uint(key);
wrapper = rotators[position];
isLeft = position == POSITION_LT || position == POSITION_LB;
isTop = position == POSITION_LT || position == POSITION_RT;
wrapper.x = isLeft ? (-w / 2) : (w / 2);
wrapper.y = isTop ? (-h / 2) : (h / 2);
}
}
protected function resizerDown(event:MouseEvent):void
{
if(lastMouse)
return;
lastMouse = new Point(mouseX, mouseY);
stage.addEventListener(MouseEvent.MOUSE_MOVE, resizerMove);
stage.addEventListener(MouseEvent.MOUSE_UP, resizerUp);
}
protected function resizerMove(event:MouseEvent):void
{
var dx:Number = Math.abs(mouseX) - Math.abs(lastMouse.x);
var dy:Number = Math.abs(mouseY) - Math.abs(lastMouse.y);
shapeWidth += dx * 2;
shapeHeight += dy * 2;
subjectMatchTransformator();
lastMouse = new Point(mouseX, mouseY);
}
protected function resizerUp(event:MouseEvent):void
{
lastMouse = null;
stage.removeEventListener(MouseEvent.MOUSE_MOVE, resizerMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, resizerUp);
}
protected function rotatorDown(event:MouseEvent):void
{
if(lastMouse)
return;
lastMouse = new Point(mouseX, mouseY);
stage.addEventListener(MouseEvent.MOUSE_MOVE, rotatorMove);
stage.addEventListener(MouseEvent.MOUSE_UP, rotatorUp);
}
protected function rotatorMove(event:MouseEvent):void
{
var rad1:Number = Math.atan2(lastMouse.y, lastMouse.x);
var rad2:Number = Math.atan2(mouseY, mouseX);
rotation += (rad2 - rad1) * 180 / Math.PI;
subjectMatchTransformator();
lastMouse = new Point(mouseX, mouseY);
}
protected function rotatorUp(event:MouseEvent):void
{
lastMouse = null;
stage.removeEventListener(MouseEvent.MOUSE_MOVE, rotatorMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, rotatorUp);
}
protected function shapeDown(event:MouseEvent):void
{
if(!draggable)
return;
lastMouse = new Point(mouseX, mouseY);
stage.addEventListener(MouseEvent.MOUSE_MOVE, shapeMove);
stage.addEventListener(MouseEvent.MOUSE_UP, shapeUp);
}
protected function shapeMove(event:MouseEvent):void
{
var p1:Point = localToGlobal(lastMouse);
var p2:Point = localToGlobal(new Point(mouseX, mouseY));
x += p2.x - p1.x;
y += p2.y - p1.y;
subjectMatchTransformator();
lastMouse = new Point(mouseX, mouseY);
}
protected function shapeUp(event:MouseEvent):void
{
lastMouse = null;
stage.removeEventListener(MouseEvent.MOUSE_MOVE, shapeMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, shapeUp);
}
}