在容器内移动 OR 缩放ObjectHandles

        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;
    }
   }
  }
 }
}

 

       效果图:

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值