ObjectHandles给我们带来了组建缩放移动旋转方便,这使我们在开发过程中对组件的缩放移动旋转省了不少事情,真的不胜感激,在此,请允许我向ObjectHandles的作者说声谢谢!但是你不禁会发现,如果你需要在某一个容器中move OR resize 它的时候,就束手无策了。最近对它做了稍稍修改,基本上实现了在某个固定容器中移动和缩放ObjectHandles,但还有些不足,即缩放向左或向上缩放时不
是很友好,有待有兴趣的朋友继续完善,现将程序代码贴上,欢迎拍砖!
Test Application:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
horizontalAlign="center"
width="800" height="600"
>
<mx:Script>
<![CDATA[
import com.natian.RestrictObjHandler;
private function init():void {
var ojb:RestrictObjHandler = new RestrictObjHandler(myCan);
ojb.width = ojb.height = 120;
myCan.addChild(ojb);
}
]]>
</mx:Script>
<mx:Canvas id="myCan" width="350" height="350" borderStyle="solid" borderColor="0xff0000"/>
<mx:Button label="addObjHandler" click="init()"/>
</mx:Application>
自定义的ObjectHandles------->RestrictObjHandler:
package com.natian
{
import com.roguedevelopment.objecthandles.ObjectHandles;
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFieldType;
import mx.core.Application;
import mx.core.UIComponent;
public class RestrictObjHandler extends ObjectHandles
{
private var parentContainer:DisplayObject;
private var childUi:TextField;
private var u:UIComponent;
private var oW:Number;
private var oH:Number;
private var oPoint:Point = new Point();
private var minW:Number = 100;
private var minH:Number = 100;
public function RestrictObjHandler(pc:DisplayObject)
{
super();
parentContainer = pc;
}
override protected function createChildren():void {
super.createChildren();
childUi = new TextField();
childUi.type = TextFieldType.INPUT;
childUi.mouseEnabled = true;
childUi.wordWrap = true;
childUi.width = this.width;
childUi.height = this.height;
childUi.text = "ObjectHandlerTest";
childUi.border = true;
u = new UIComponent();
u.percentWidth = u.percentHeight = 100;
u.addChild(childUi);
this.addChild(u);
initPos();
}
public function initPos():void {
this.oW = this.width;
this.oH = this.height;
}
override protected function onMouseDown(event:MouseEvent):void {
super.onMouseDown(event);
this.oPoint.x = mouseX;
this.oPoint.y = mouseY;
this.oPoint = this.localToGlobal(oPoint);
childUi.border = false;
}
override protected function onMouseUp(event:MouseEvent):void {
super.onMouseUp(event);
this.stopDrag();
initPos();
/* u.width = this.width;
u.height = this.height; */
childUi.width = this.width;
childUi.height = this.height;
childUi.border = true;
}
override protected function onMouseMove(event:MouseEvent):void {
if(!event.buttonDown){
return;
}
if(isMoving){
this.startDrag(false, new Rectangle(0, 0, parentContainer.width - this.width - 5, parentContainer.height - this.height - 5));
}else {
this.stopDrag();
var xPlus:Number = Application.application.parent.mouseX - this.oPoint.x;
var yPlus:Number = Application.application.parent.mouseY - this.oPoint.y;
var wnum:Number = this.oW + xPlus;
var hnum:Number = this.oH + yPlus;
//限制拉伸范围
if (wnum > minW && wnum < parentContainer.width - this.x - 5) {
this.width = this.oW + xPlus;
}
if (hnum > minH && hnum < parentContainer.height - this.y - 5) {
this.height = this.oH + yPlus;
}
}
}
}
}
效果图: