package 
{

	import flash.display.*;

	import flash.events.*;

	import flash.geom.*;

	import flash.ui.Mouse;



	public class DragBox extends Sprite
	{

		private var gripperMinWidth:Number;//拖动框最小值

		private var gripperMaxWidth:Number;//拖动框最大值

		private var resizeFlag:Boolean = false;//是否可以resize

		private var originalScale:Number;//窗体的高宽比

		private var dragBoundary:Rectangle;//可以拖动的范围

		private var dragOffset:Number;//dragbox窗体的偏移量

		private var mc_cursor:Sprite=new Sprite();//鼠标样式

		private var mc_imageThumb:Sprite=new Sprite();//缩略图

		public function DragBox(rect:Rectangle,minWidth:uint = 75, maxWidth:uint=200,scale:Number=1,offset:Number=0)
		{

			this.gripperMinWidth = minWidth;

			this.gripperMaxWidth = maxWidth;

			this.originalScale = scale;

			this.dragBoundary = rect;

			this.dragOffset = offset;

			this.addEventListener(Event.ADDED_TO_STAGE, onStage);

		}

		private function onStage(_e:Event):void
		{

			this.removeEventListener(Event.ADDED_TO_STAGE, onStage);

			initStage();

			initListeners();

		}

		private function initStage()
		{

			this.alpha = 0.3;

			mc_imageThumb = Sprite(parent.getChildByName("mc_imageThumb"));

			addChild(mc_cursor);

			mc_cursor.visible = false;

		}

		//绘制边框

		private function drawBorder():void
		{

			this.graphics.clear();

			this.graphics.beginFill(0, 0);

			this.graphics.lineStyle(1, 0x111111);

			this.graphics.drawRect(0, 0, mc_gripper.width, mc_gripper.height);

			this.graphics.endFill();

		}

		//添加侦听器

		private function initListeners():void
		{

			stage.addEventListener(MouseEvent.MOUSE_UP, onDragBoxMouseUp);

			mc_gripper.addEventListener(MouseEvent.MOUSE_OVER, onRectMouseOver);

			mc_gripper.addEventListener(MouseEvent.MOUSE_OUT, onRectMouseOut);

			mc_gripper.addEventListener(MouseEvent.MOUSE_DOWN, onRectDown);

			mc_gripper.addEventListener(MouseEvent.MOUSE_UP, onRectUp);

			mc_corner.addEventListener(MouseEvent.MOUSE_OVER, onCornerOver);

			mc_corner.addEventListener(MouseEvent.MOUSE_DOWN, onCornerDown);

			mc_corner.addEventListener(MouseEvent.MOUSE_UP, onCornerUp);

		}

		private function onDragBoxMouseUp(_e:MouseEvent)
		{

			resizeFlag = false;

			mc_gripper.stopDrag();

			mc_corner.stopDrag();

		}

		private function onRectMouseOver(_e:MouseEvent)
		{

			Mouse.hide();

			mc_cursor.visible = true;

			mouseFollow();

			this.addEventListener(MouseEvent.MOUSE_MOVE, onBgMouseMove);

		}

		public function onBgMouseMove(_e:MouseEvent)
		{

			mouseFollow();

			_e.updateAfterEvent();

		}

		private function mouseFollow()
		{

			mc_cursor.x = mouseX;

			mc_cursor.y = mouseY;

		}

		private function onRectMouseOut(_e:MouseEvent)
		{

			Mouse.show();

			mc_cursor.visible = false;

		}

		private function onRectDown(_e:MouseEvent)
		{

			this.startDrag(false,new Rectangle(dragBoundary.x, dragBoundary.y, dragBoundary.width - mc_gripper.width, dragBoundary.height - mc_gripper.height));

		}

		private function onRectUp(_e:MouseEvent)
		{

			createBitmap(parent.getChildByName("video"),mc_gripper);

			this.stopDrag();

		}

		private function onCornerOver(_e:MouseEvent)
		{

			Mouse.show();

			mc_cursor.visible = false;

		}

		private function onCornerDown(_e:MouseEvent)
		{

			resizeFlag = true;

			mc_corner.addEventListener(MouseEvent.MOUSE_MOVE, onCornerMove);

		}

		private function onCornerUp(_e:MouseEvent)
		{

			mc_corner.stopDrag();

			mc_corner.x = mc_gripper.width - mc_corner.width;

			mc_corner.y = mc_gripper.height - mc_corner.height;

		}

		private function onCornerMove(_e:MouseEvent)
		{

			if (resizeFlag)
			{

				resizeBox();

				drawBorder();

				_e.updateAfterEvent();

			}

		}

		//窗体缩放部分

		private function resizeBox()
		{

			//开始拖动

			mc_corner.startDrag();

			//设置宽高

			mc_gripper.width = mc_corner.x + mc_corner.width;

			mc_gripper.height = mc_gripper.width * originalScale;

			//设置右下角那个图标的位置

			mc_cornerBg.x = mc_gripper.width - mc_cornerBg.width;

			mc_cornerBg.y = mc_gripper.height - mc_cornerBg.height;

			checkWall();

			drawBorder();

			createBitmap(parent.getChildByName("video"),mc_gripper);

		}

		//边缘检测

		private function checkWall()
		{

			//宽度小于最小允许宽度时的处理

			if (mc_gripper.width <= gripperMinWidth)
			{

				mc_gripper.width = gripperMinWidth;

				mc_cornerBg.x = gripperMinWidth - mc_cornerBg.width;

			}

			//宽度大于最大允许宽度时的处理

			if (mc_gripper.width >= gripperMaxWidth)
			{

				mc_gripper.width = gripperMaxWidth;

				mc_cornerBg.x = gripperMaxWidth - mc_cornerBg.width;

			}

			//高度小于最小允许高度时的处理

			if (mc_gripper.height <= gripperMinWidth * originalScale)
			{

				mc_gripper.height = gripperMinWidth * originalScale;

				mc_cornerBg.y = gripperMinWidth * originalScale - mc_cornerBg.height;

			}

			//高度大于最大允许高度时的处理

			if (mc_gripper.height >= gripperMaxWidth * originalScale)
			{

				mc_gripper.height = gripperMaxWidth * originalScale;

				mc_cornerBg.y = gripperMaxWidth * originalScale - mc_cornerBg.width;

			}

			//拖动到dragBoundary范围外的处理

			if (this.x+mc_gripper.width>=(dragBoundary.width+dragOffset) || this.y+mc_gripper.height>=(dragBoundary.height+dragOffset))
			{

				var offsetX:Number=this.x+mc_gripper.width-(dragBoundary.width+dragOffset);

				if (offsetX > 0)
				{

					this.x -=  offsetX;

					onCornerUp(null);

				}

				var offsetY:Number=this.y+mc_gripper.height-(dragBoundary.height+dragOffset);

				if (offsetY > 0)
				{

					this.y -=  offsetY;

					onCornerUp(null);

				}

			}

		}

		//建立bitmap

		private function createBitmap(sp:Object,box:Sprite)
		{

			removeChildren(mc_imageThumb);

			var jpgSource = new BitmapData(dragBoundary.width,dragBoundary.height);

			jpgSource.draw(sp);

			var pt:Point = new Point(box.x,box.y);

			pt = box.localToGlobal(pt);

			var rect:Rectangle = new Rectangle(pt.x - dragOffset,pt.y - dragOffset,box.width,box.height);//设置复制的大小和起始点

			var jpgOut:BitmapData = new BitmapData(box.width,box.height);

			jpgOut.copyPixels(jpgSource, rect,new Point(0, 0));
			//复制,并设置复制到的起始点;

			var bp:Bitmap = new Bitmap(jpgOut);

			bp.width = gripperMaxWidth;

			bp.height = gripperMaxWidth * originalScale;

			mc_imageThumb.addChild(bp);

		}

		//删除子元件的函数

		private function removeChildren(dObj:DisplayObjectContainer)
		{

			while (dObj.numChildren>0)
			{

				dObj.removeChildAt(0);

			}

		}

	}

}