TransformTool 的使用
import flash.display.Shape;
import com.senocular.display.TransformTool;
// default tool
var defaultTool:TransformTool = new TransformTool();
addChild(defaultTool);
// custom tool with some custom options
var customTool:TransformTool = new TransformTool();
addChild(customTool);
customTool.raiseNewTargets = false;
customTool.moveNewTargets = true;
customTool.moveUnderObjects = false;
customTool.registrationEnabled = false;
customTool.rememberRegistration = false;
customTool.rotationEnabled = false;
customTool.constrainRotation = true;
customTool.constrainRotationAngle = 90/4;
customTool.constrainScale = true;
customTool.maxScaleX = 2;
customTool.maxScaleY = 2;
customTool.skewEnabled = false;
customTool.setSkin(TransformTool.SCALE_TOP_LEFT, new ScaleCircle());
customTool.setSkin(TransformTool.SCALE_TOP_RIGHT, new ScaleCircle());
customTool.setSkin(TransformTool.SCALE_BOTTOM_RIGHT, new ScaleCircle());
customTool.setSkin(TransformTool.SCALE_BOTTOM_LEFT, new ScaleCircle());
customTool.setSkin(TransformTool.SCALE_TOP, new ScaleCircle());
customTool.setSkin(TransformTool.SCALE_RIGHT, new ScaleCircle());
customTool.setSkin(TransformTool.SCALE_BOTTOM, new ScaleCircle());
customTool.setSkin(TransformTool.SCALE_LEFT, new ScaleCircle());
customTool.addControl(new CustomRotationControl());
customTool.addControl(new CustomResetControl());
// keep track of the current tool being used
var currTool:TransformTool = defaultTool;
// selecting objects on the screen
function select(event){
if (event.target is Stage) {
currTool.target = null;
}else if (event.target is Sprite) {
currTool.target = event.target as Sprite;
toolInit();
}
}
// changing tools using the toolChange button
function setTool(event:MouseEvent):void {
// get other tool
var newTool:TransformTool = (currTool == defaultTool) ? customTool : defaultTool;
// make sure moveNewTargets is not set when setting tool this way
var moveTargets:Boolean = newTool.moveNewTargets;
newTool.moveNewTargets = false;
newTool.target = currTool.target;
newTool.moveNewTargets = moveTargets;
// unset currTool
currTool.target = null;
currTool = newTool;
toolInit();
}
// for setting a new tool
function toolInit():void {
// raise
currTool.parent.setChildIndex(currTool, currTool.parent.numChildren - 1);
// center registration for customTool
if (currTool == customTool) {
currTool.registration = currTool.boundsCenter;
}
}
// event handlers for selecting objects and changing tools
stage.addEventListener(MouseEvent.MOUSE_DOWN, select);
toolChange.addEventListener(MouseEvent.CLICK, setTool);
import com.senocular.display.TransformTool;
// default tool
var defaultTool:TransformTool = new TransformTool();
addChild(defaultTool);
// custom tool with some custom options
var customTool:TransformTool = new TransformTool();
addChild(customTool);
customTool.raiseNewTargets = false;
customTool.moveNewTargets = true;
customTool.moveUnderObjects = false;
customTool.registrationEnabled = false;
customTool.rememberRegistration = false;
customTool.rotationEnabled = false;
customTool.constrainRotation = true;
customTool.constrainRotationAngle = 90/4;
customTool.constrainScale = true;
customTool.maxScaleX = 2;
customTool.maxScaleY = 2;
customTool.skewEnabled = false;
customTool.setSkin(TransformTool.SCALE_TOP_LEFT, new ScaleCircle());
customTool.setSkin(TransformTool.SCALE_TOP_RIGHT, new ScaleCircle());
customTool.setSkin(TransformTool.SCALE_BOTTOM_RIGHT, new ScaleCircle());
customTool.setSkin(TransformTool.SCALE_BOTTOM_LEFT, new ScaleCircle());
customTool.setSkin(TransformTool.SCALE_TOP, new ScaleCircle());
customTool.setSkin(TransformTool.SCALE_RIGHT, new ScaleCircle());
customTool.setSkin(TransformTool.SCALE_BOTTOM, new ScaleCircle());
customTool.setSkin(TransformTool.SCALE_LEFT, new ScaleCircle());
customTool.addControl(new CustomRotationControl());
customTool.addControl(new CustomResetControl());
// keep track of the current tool being used
var currTool:TransformTool = defaultTool;
// selecting objects on the screen
function select(event){
if (event.target is Stage) {
currTool.target = null;
}else if (event.target is Sprite) {
currTool.target = event.target as Sprite;
toolInit();
}
}
// changing tools using the toolChange button
function setTool(event:MouseEvent):void {
// get other tool
var newTool:TransformTool = (currTool == defaultTool) ? customTool : defaultTool;
// make sure moveNewTargets is not set when setting tool this way
var moveTargets:Boolean = newTool.moveNewTargets;
newTool.moveNewTargets = false;
newTool.target = currTool.target;
newTool.moveNewTargets = moveTargets;
// unset currTool
currTool.target = null;
currTool = newTool;
toolInit();
}
// for setting a new tool
function toolInit():void {
// raise
currTool.parent.setChildIndex(currTool, currTool.parent.numChildren - 1);
// center registration for customTool
if (currTool == customTool) {
currTool.registration = currTool.boundsCenter;
}
}
// event handlers for selecting objects and changing tools
stage.addEventListener(MouseEvent.MOUSE_DOWN, select);
toolChange.addEventListener(MouseEvent.CLICK, setTool);
fla上代码
下面是两个同目录下类:
package {
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import com.senocular.display.TransformTool;
import com.senocular.display.TransformToolControl;
import com.senocular.display.TransformToolCursor;
public class CustomResetControl extends TransformToolControl {
public function CustomResetControl() {
addEventListener(TransformTool.CONTROL_INIT, init, false, 0, true);
}
private function init(event:Event):void {
// add event listeners
transformTool.addEventListener(TransformTool.NEW_TARGET, update, false, 0, true);
transformTool.addEventListener(TransformTool.TRANSFORM_TOOL, update, false, 0, true);
transformTool.addEventListener(TransformTool.CONTROL_TRANSFORM_TOOL, update, false, 0, true);
addEventListener(MouseEvent.CLICK, resetClick);
// initial positioning
update();
}
private function update(event:Event = null):void {
if (transformTool.target) {
// find to bottom right of selection
var maxX:Number = Math.max(transformTool.boundsTopLeft.x, transformTool.boundsTopRight.x);
maxX = Math.max(maxX, transformTool.boundsBottomRight.x);
maxX = Math.max(maxX, transformTool.boundsBottomLeft.x);
var maxY:Number = Math.max(transformTool.boundsTopLeft.y, transformTool.boundsTopRight.y);
maxY = Math.max(maxY, transformTool.boundsBottomRight.y);
maxY = Math.max(maxY, transformTool.boundsBottomLeft.y);
// set location to found values
x = maxX;
y = maxY;
}
}
private function resetClick(event:MouseEvent):void {
// reset the matrix but keep the current location by
// noting the change in the registration point
var origReg:Point = transformTool.registration;
// global matrix as a default matrix (identity)
transformTool.globalMatrix = new Matrix();
// find change in positioning based on registration
// Note: registration location is based within
// the coordinate space of the tool (not global)
var regDiff = origReg.subtract(transformTool.registration);
// update the tool matrix with the change in position
// offsetting movement from the new matrix to have
// the old and new registration points match
var toolMatrix:Matrix = transformTool.toolMatrix;
toolMatrix.tx += regDiff.x;
toolMatrix.ty += regDiff.y;
transformTool.toolMatrix = toolMatrix;
// apply the new matrix to the target
transformTool.apply();
}
}
}
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import com.senocular.display.TransformTool;
import com.senocular.display.TransformToolControl;
import com.senocular.display.TransformToolCursor;
public class CustomResetControl extends TransformToolControl {
public function CustomResetControl() {
addEventListener(TransformTool.CONTROL_INIT, init, false, 0, true);
}
private function init(event:Event):void {
// add event listeners
transformTool.addEventListener(TransformTool.NEW_TARGET, update, false, 0, true);
transformTool.addEventListener(TransformTool.TRANSFORM_TOOL, update, false, 0, true);
transformTool.addEventListener(TransformTool.CONTROL_TRANSFORM_TOOL, update, false, 0, true);
addEventListener(MouseEvent.CLICK, resetClick);
// initial positioning
update();
}
private function update(event:Event = null):void {
if (transformTool.target) {
// find to bottom right of selection
var maxX:Number = Math.max(transformTool.boundsTopLeft.x, transformTool.boundsTopRight.x);
maxX = Math.max(maxX, transformTool.boundsBottomRight.x);
maxX = Math.max(maxX, transformTool.boundsBottomLeft.x);
var maxY:Number = Math.max(transformTool.boundsTopLeft.y, transformTool.boundsTopRight.y);
maxY = Math.max(maxY, transformTool.boundsBottomRight.y);
maxY = Math.max(maxY, transformTool.boundsBottomLeft.y);
// set location to found values
x = maxX;
y = maxY;
}
}
private function resetClick(event:MouseEvent):void {
// reset the matrix but keep the current location by
// noting the change in the registration point
var origReg:Point = transformTool.registration;
// global matrix as a default matrix (identity)
transformTool.globalMatrix = new Matrix();
// find change in positioning based on registration
// Note: registration location is based within
// the coordinate space of the tool (not global)
var regDiff = origReg.subtract(transformTool.registration);
// update the tool matrix with the change in position
// offsetting movement from the new matrix to have
// the old and new registration points match
var toolMatrix:Matrix = transformTool.toolMatrix;
toolMatrix.tx += regDiff.x;
toolMatrix.ty += regDiff.y;
transformTool.toolMatrix = toolMatrix;
// apply the new matrix to the target
transformTool.apply();
}
}
}
package {
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Point;
import com.senocular.display.TransformTool;
import com.senocular.display.TransformToolControl;
import com.senocular.display.TransformToolCursor;
public class CustomRotationControl extends TransformToolControl {
private var length:Number = 20;
private var circle:ScaleCircle;
public function CustomRotationControl() {
addEventListener(TransformTool.CONTROL_INIT, init, false, 0, true);
circle = new ScaleCircle();
addChild(circle);
}
private function init(event:Event):void {
// add event listeners
transformTool.addEventListener(TransformTool.NEW_TARGET, update, false, 0, true);
transformTool.addEventListener(TransformTool.TRANSFORM_TOOL, update, false, 0, true);
transformTool.addEventListener(TransformTool.CONTROL_TRANSFORM_TOOL, update, false, 0, true);
transformTool.addEventListener(TransformTool.CONTROL_DOWN, controlMouseDown, false, 0, true);
transformTool.addEventListener(TransformTool.CONTROL_MOVE, controlMove, false, 0, true);
// set this as a reference for the rotation cursor
var cursor:TransformToolCursor = transformTool.rotationCursor;
cursor.addReference(this);
// initial positioning
update();
}
private function update(event:Event = null):void {
if (transformTool.target) {
// move circle to point
var top:Point = transformTool.boundsTop;
var bottom:Point = transformTool.boundsBottom;
var diff = top.subtract(bottom);
var angle = Math.atan2(diff.y, diff.x);
circle.x = top.x + length * Math.cos(angle);
circle.y = top.y + length * Math.sin(angle);
// draw connecting line
graphics.clear();
graphics.lineStyle(0,0);
// draw from top of top ScaleCircle
var offset:Number = circle.height/2;
graphics.moveTo(top.x + offset * Math.cos(angle), top.y + offset * Math.sin(angle));
graphics.lineTo(circle.x, circle.y);
}
}
private function controlMouseDown(event:Event):void {
if (transformTool.currentControl == this) {
// if this tool is being clicked, set
// the reference point to be the mouse location
_referencePoint = transformTool.mouse;
}
}
private function controlMove(event:Event):void {
if (transformTool.currentControl == this) {
// use default tool rotation if this tool is being used
transformTool.rotationInteraction();
}
}
}
}
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Point;
import com.senocular.display.TransformTool;
import com.senocular.display.TransformToolControl;
import com.senocular.display.TransformToolCursor;
public class CustomRotationControl extends TransformToolControl {
private var length:Number = 20;
private var circle:ScaleCircle;
public function CustomRotationControl() {
addEventListener(TransformTool.CONTROL_INIT, init, false, 0, true);
circle = new ScaleCircle();
addChild(circle);
}
private function init(event:Event):void {
// add event listeners
transformTool.addEventListener(TransformTool.NEW_TARGET, update, false, 0, true);
transformTool.addEventListener(TransformTool.TRANSFORM_TOOL, update, false, 0, true);
transformTool.addEventListener(TransformTool.CONTROL_TRANSFORM_TOOL, update, false, 0, true);
transformTool.addEventListener(TransformTool.CONTROL_DOWN, controlMouseDown, false, 0, true);
transformTool.addEventListener(TransformTool.CONTROL_MOVE, controlMove, false, 0, true);
// set this as a reference for the rotation cursor
var cursor:TransformToolCursor = transformTool.rotationCursor;
cursor.addReference(this);
// initial positioning
update();
}
private function update(event:Event = null):void {
if (transformTool.target) {
// move circle to point
var top:Point = transformTool.boundsTop;
var bottom:Point = transformTool.boundsBottom;
var diff = top.subtract(bottom);
var angle = Math.atan2(diff.y, diff.x);
circle.x = top.x + length * Math.cos(angle);
circle.y = top.y + length * Math.sin(angle);
// draw connecting line
graphics.clear();
graphics.lineStyle(0,0);
// draw from top of top ScaleCircle
var offset:Number = circle.height/2;
graphics.moveTo(top.x + offset * Math.cos(angle), top.y + offset * Math.sin(angle));
graphics.lineTo(circle.x, circle.y);
}
}
private function controlMouseDown(event:Event):void {
if (transformTool.currentControl == this) {
// if this tool is being clicked, set
// the reference point to be the mouse location
_referencePoint = transformTool.mouse;
}
}
private function controlMove(event:Event):void {
if (transformTool.currentControl == this) {
// use default tool rotation if this tool is being used
transformTool.rotationInteraction();
}
}
}
}
效果:
另外有一个test
fla如下:
import com.senocular.display.TransformTool;
var currTool:TransformTool = new TransformTool();
addChild(currTool);
currTool.target = a;
stage.addEventListener(MouseEvent.MOUSE_DOWN, select);
function select(event) {
if (event.target is Stage) {
currTool.target = null;
} else if (event.target is Sprite) {
currTool.target = event.target as Sprite;
// toolInit();
}
}
var currTool:TransformTool = new TransformTool();
addChild(currTool);
currTool.target = a;
stage.addEventListener(MouseEvent.MOUSE_DOWN, select);
function select(event) {
if (event.target is Stage) {
currTool.target = null;
} else if (event.target is Sprite) {
currTool.target = event.target as Sprite;
// toolInit();
}
}