JPanel

package xu.love.qq.component.board 
{
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import org.aswing.JPanel;
	import org.aswing.LayoutManager;
	import org.aswing.geom.IntDimension;
	import org.aswing.geom.IntPoint;
	import org.aswing.geom.IntRectangle;
	
	import xu.love.qq.geom.AdsorbRectang;
	
	/**
	 * 让JPanel可以自由拖动,不需要JFrame包装,
	 * 添加一个backgroundInteract:InteractiveObject来包装backgroundChild监听鼠标事件
	 * 这是三个窗口的基类
	 * @author tangzx
	 */
	public class AbstractBoard extends JPanel
	{
		protected static var panes:Array = [];
		
		protected var originalSize:IntDimension;
		private var backgroundChild:DisplayObject;
		private var backgroundInteract:InteractiveObject;//backgroundInteract包装backgroundChild
		
		public function AbstractBoard(layout:LayoutManager = null) 
		{
			super(layout);
			panes.push(this);//是为了方便对面板对象的引用吗?
			this.mouseChildren = true;//保证backgroundInteract监听鼠标事件
		}
		
		
		protected var startDragLocalPoint:IntPoint;
		protected var startDragParentPoint:IntPoint;
		
		/**
		 * 开始拖 
		 * @param e
		 * 
		 */		
		protected function checkStartDrag(e:MouseEvent):void{
			if(getParent())
				getParent().bringToTop(this);
			
			startDragLocalPoint = getLocation();
			startDragParentPoint = new IntPoint(e.stageX, e.stageY);//getParent().getMousePosition();
			stage.addEventListener(MouseEvent.MOUSE_UP, 	checkStopDrag);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,	checkMove);
		}
		
		/**
		 * 拖动过程中 
		 * @param e
		 * 
		 */		
		protected function checkMove(e:MouseEvent):void{
			var parentNowPoint:IntPoint = new IntPoint(e.stageX, e.stageY);//getParent().getMousePosition();得到舞台坐标
			var localNowPoint:IntPoint = startDragLocalPoint.clone();//获得开始拖动的本地坐标
			var localRect:IntRectangle = new IntRectangle;//
			//获拖动过程中的本地坐标
			localNowPoint.move(parentNowPoint.x - startDragParentPoint.x, parentNowPoint.y - startDragParentPoint.y);
			localRect.setLocation(localNowPoint);
			localRect.setSize(getSize());//localRect作用在下面吸附里,打开就可执行
			
			/*var adso:AdsorbRectang = new AdsorbRectang(localRect);
			for each(var pane:AbstractBoard in panes){
				if(pane != this){
					var r:IntPoint = adso.adsorb(pane.getBoundsRect(), 10, 10);
					if(r){
						if(r.x != 0) localNowPoint.x = r.x;
						if(r.y != 0) localNowPoint.y = r.y;
						break;
					}
				}
			}*/
			//更新组件拖动过程中的本地坐标
			this.setLocation(localNowPoint);
			e.updateAfterEvent();
		}
		
		/**
		 * 停止拖动 
		 * @param e
		 * 
		 */		
		protected function checkStopDrag(e:MouseEvent):void{
			stage.removeEventListener(MouseEvent.MOUSE_UP, checkStopDrag);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,	checkMove);
		}
		/**
		 * @see component.setBackgroundDecorator(bg:GroundDecorator)
		 * @param	child == bg.getDisplay(this)
		 */
		override protected function setBackgroundChild(child:DisplayObject=null):void{
			if(child != backgroundChild){
				if(backgroundChild){
					removeChild(backgroundInteract);
					backgroundInteract = null;
				}
				
				backgroundChild = child;
				
				if(backgroundChild){
					if(backgroundChild is InteractiveObject){
						backgroundInteract = backgroundChild as InteractiveObject;
					} else {
						var s:Sprite = new Sprite;
						s.addChild(backgroundChild);
						backgroundInteract = s;
					}
					backgroundInteract.addEventListener(MouseEvent.MOUSE_DOWN, checkStartDrag);
					addChildAt(backgroundInteract, 0);
				}
			}
		}
		
		
		override protected function getBackgroundChild():DisplayObject {
			return backgroundChild;
		}
		
		
		/**
		 * 初始大小 
		 * @return 
		 * 
		 */		
		public function getOriginalSize():IntDimension {
			return originalSize;
		}
		
		public function setOriginalSize(size:IntDimension):void {
			originalSize = size;
		}
		
		
		public function getBoundsRect():IntRectangle{
			return new IntRectangle(getX(), getY(), getWidth(), getHeight());
		}
		
	}

}
posted @ 2012-02-04 18:38  ddw1997  阅读(1092)  评论(0编辑  收藏  举报