处理as脚本如下:
public class ObjectHandler
{
//操作的小矩形框的大小
private static const HandlerSize:int = 10;
//本类的公共对象
private static var focusObjectHandler:ObjectHandler;
//对象的容器
private var container:Container;
//实际控制对象
private var widget:UIComponent;
private static var wwidget:UIComponent;
//10个可拖动框
private var rects:Array = [];
//当前正在操作的句柄
private var currRect:Sprite;
//是否可改变大小
private var sizeable:Boolean;
private var drawType:String;
public function ObjectHandler(container:Container, single:Single, drawType:String)
{
if(single == null){
throw new Error("ObjectHandler new error.");
}
this.container = container;
this.drawType = drawType;
rects = [];
for(var i:int = 0; i < 11; i++){
var rect:UIComponent = createRectHandler();
if (i != 5) {
rect.addEventListener(MouseEvent.MOUSE_DOWN, evt_rect_mouse_down);
rect.addEventListener(MouseEvent.MOUSE_UP, evt_rect_mouse_up);
}
rects[i] = rect;
}
container.addEventListener(MouseEvent.MOUSE_UP, evt_container_mouse_up);
}
private function createRectHandler():UIComponent
{
var img:UIComponent = new UIComponent();
// var size:int = 10;
img.graphics.beginFill(0x00ffff, 0.5);
img.graphics.drawRect(-HandlerSize / 2, -HandlerSize / 2, HandlerSize, HandlerSize);
img.graphics.endFill();
img.graphics.lineStyle(1, 0);
img.graphics.drawRect(-HandlerSize / 2, -HandlerSize / 2, HandlerSize, HandlerSize);
return img;
}
private function evt_rect_mouse_down(e:MouseEvent):void
{
var rect:Sprite = e.currentTarget as Sprite;
rect.startDrag();
currRect = rect;
if(rects.indexOf(e.target as Sprite) != 5) {
container.addEventListener(MouseEvent.MOUSE_MOVE, evt_mouse_move);
}
}
private function evt_rect_mouse_up(e:MouseEvent):void
{
var rect:Sprite = e.currentTarget as Sprite;
rect.stopDrag();
currRect = null;
container.removeEventListener(MouseEvent.MOUSE_MOVE, evt_mouse_move);
}
private function evt_rect_key_up(e:KeyboardEvent):void
{
if (e.keyCode == 46)
{
if (wwidget.parent != null)
{
// setFocus(null);
wwidget.parent.removeChild(wwidget);
// var hanlder:Sprite = new Sprite();
// for (var i:int = 0; i < rects.length; i++)
// {
// hanlder = rects[i];
// container.rawChildren.removeChild(hanlder);
// }
container.removeEventListener(KeyboardEvent.KEY_UP, evt_rect_key_up);
}
}
}
private function evt_container_mouse_up(e:MouseEvent):void
{
if (currRect){
currRect.stopDrag();
}
container.removeEventListener(MouseEvent.MOUSE_MOVE, evt_mouse_move);
}
private function evt_mouse_move(e:MouseEvent):void
{
if (currRect) {
updatePosition(currRect);
}
}
public static function addHandler(container:Container, widget:UIComponent, sizeable:Boolean = true, drawType:String = "rect"):void
{
var handler:ObjectHandler = new ObjectHandler(container, new Single(), drawType);
handler.addWidget(widget, sizeable);
}
private function addWidget(_widget:UIComponent, sizeable:Boolean):void
{
if (widget && widget.parent) {
widget.parent.removeChild(widget);
trace("widget remove child..");
}
this.sizeable = sizeable;
widget = _widget;
var p1:Point= new Point(widget.x, widget.y);
var p2:Point = new Point(widget.x + widget.width, widget.y + widget.height);
widget.addEventListener(MouseEvent.CLICK, evt_change_focus);
setRectsPos(p1, p2);
if (focusObjectHandler == null) {
setFocus(this);
}
}
private function evt_change_focus(e:MouseEvent):void
{
setFocus(this);
}
private function updatePosition(rect:Sprite):void
{
var ind:int = rects.indexOf(rect);
var p1:Point = new Point(rects[0].x, rects[0].y);
var p2:Point = new Point(rects[8].x, rects[8].y);
if (ind == 0){//左上角
p1.x = rect.x;
p1.y = rect.y;
} else if (ind == 1){//上部
p1.y = rect.y;
} else if (ind == 2){//右上角
p2.x = rect.x;
p1.y = rect.y;
} else if (ind == 3){//左边
p1.x = rect.x;
} else if (ind == 4){//右边
p2.x = rect.x;
} else if (ind == 5){//最右边
p2.x = rect.x;
} else if (ind == 6){//左下角
p1.x = rect.x;
p2.y = rect.y;
} else if (ind == 7){//下边
p2.y = rect.y;
} else if (ind == 8){//右下角
p2.x = rect.x;
p2.y = rect.y;
} else if (ind == 9){//中间的
var w:Number = p2.x - p1.x;
var h:Number = p2.y - p1.y;
p1.x = rect.x - w / 2;
p1.y = rect.y - h / 2;
p2.x = p1.x + w;
p2.y = p1.y + h;
}else {
throw new Error("handler undefined!");
}
setRectsPos(p1, p2);
}
private function setOperating(value:Boolean):void
{
var i:int;
var handler:Sprite;
var start:int = sizeable? 0 : 9;
if (value){//可操作
for(i = start; i < rects.length; i++){
handler = rects[i];
container.rawChildren.addChild(handler);
}
container.addEventListener(KeyboardEvent.KEY_UP, evt_rect_key_up);
} else {//不可操作
for(i = start; i < rects.length; i++){
handler = rects[i];
// if (handler.parent){
// handler.parent.removeChild(handler);
// }
container.rawChildren.removeChild(handler);
}
}
}
private function setRectsPos(p1:Point, p2:Point):void
{
if (p1.x > p2.x){//判定,并且保证 p1 在 p2 的左上方
var value:Number = p1.x;
p1.x = p2.x;
p2.x = value;
}
if (p1.y > p2.y){//判定,并且保证 p1 在 p2 的左上方
value = p1.y;
p1.y = p2.y;
p2.y = value;
}
rects[0].x = p1.x;
rects[0].y = p1.y;
rects[1].x = (p1.x + p2.x) / 2;
rects[1].y = p1.y;
rects[2].x = p2.x;
rects[2].y = p1.y;
rects[3].x = p1.x;
rects[3].y = (p1.y + p2.y) / 2;
rects[4].x = p2.x;
rects[4].y = (p1.y + p2.y) / 2;
rects[5].x = p2.x + 15;
rects[5].y = (p1.y + p2.y) /2;
rects[6].x = p1.x;
rects[6].y = p2.y;
rects[7].x = (p1.x + p2.x) / 2;
rects[7].y = p2.y;
rects[8].x = p2.x;
rects[8].y = p2.y;
rects[9].x = (p1.x + p2.x) / 2;
rects[9].y = (p1.y + p2.y) / 2;
widget.x = p1.x;
widget.y = p1.y;
widget.width = p2.x - p1.x;
widget.height = p2.y - p1.y;
trace("x:\t" + rects[0].x);
trace("y:\t" + rects[0].y);
Main.resizeLayer(widget);
}
private function setFocus(value:ObjectHandler):void
{
if (focusObjectHandler){
focusObjectHandler.setOperating(false);
}
focusObjectHandler = value;
if(focusObjectHandler){
focusObjectHandler.setOperating(true);
}
wwidget = widget;
}
}
class Single{}
调用代码如下:
ObjectHandler.addHandler(this, layer, true, _drawType);
PS:第三个参数、第四个参数可忽略。 第二个参数为要添加句柄的容器对象(控件)。
效果如图: