PageRenderTime 58ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/AnjueLib/src/CommonLib/org/bitmaps/cutBitmap/view/CutPanel.as

http://anjue.googlecode.com/
ActionScript | 409 lines | 359 code | 37 blank | 13 comment | 35 complexity | 335a79f61cdf1ded53b91b9511163b53 MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.bitmaps.cutBitmap.view
  2. {
  3. import flash.display.BitmapData;
  4. import flash.display.Sprite;
  5. import flash.events.Event;
  6. import flash.events.MouseEvent;
  7. import flash.geom.Matrix;
  8. import flash.geom.Point;
  9. import flash.geom.Rectangle;
  10. import flash.text.TextField;
  11. import flash.ui.Mouse;
  12. import flash.utils.ByteArray;
  13. import org.bitmaps.cutBitmap.tool.DrawArrow;
  14. import org.bitmaps.cutBitmap.tool.DrawEllipse;
  15. import org.bitmaps.cutBitmap.tool.DrawRect;
  16. import org.bitmaps.event.BmpEvent;
  17. import org.images.JPGEncoder;
  18. import org.utils.DisplayObjectUtil;
  19. /**
  20. * ????
  21. * @author anjue
  22. */
  23. public class CutPanel extends Sprite
  24. {
  25. public static const CUT_COMPLETE:String = "cut_complete";
  26. public static const CUT_EXIT:String = "CUT_EXIT";
  27. private var panel_w:uint = 300;
  28. private var panel_h:uint = 300;
  29. /**???????**/
  30. private var w_spot_num:uint = 3;
  31. /**???????**/
  32. private var h_spot_num:uint = 3;
  33. private var _container:Sprite;
  34. private var _bg:Sprite;
  35. private var _mc_hand:Sprite;
  36. private var _toolPanel:DrawToolPanel;
  37. private var _txt_w_h:TextField;
  38. private var currentDraw:DrawRect;
  39. private var current_draw_color:uint = 0;
  40. private var drawVec:Vector.<DrawRect>;
  41. private var spotVec:Vector.<CutTouchSpot>;
  42. public function CutPanel($container:Sprite)
  43. {
  44. _container = $container;
  45. initBg();
  46. initSpot();
  47. }
  48. private function initBg():void
  49. {
  50. _bg = new Sprite();
  51. _bg.graphics.beginFill(0xFFFFFF,0.1);
  52. _bg.graphics.lineStyle(1,0);
  53. _bg.graphics.drawRect(0,0,panel_w,panel_h);
  54. _bg.addEventListener(MouseEvent.MOUSE_DOWN,bgDown);
  55. _bg.doubleClickEnabled = true;
  56. _bg.addEventListener(MouseEvent.DOUBLE_CLICK,bgDoubleclick);
  57. _bg.graphics.endFill();
  58. this.addChild(_bg);
  59. _mc_hand = new Sprite();
  60. _mc_hand.graphics.beginFill(0xFF0000);
  61. _mc_hand.graphics.drawRect(0,0,10,10);
  62. _mc_hand.graphics.endFill();
  63. this.addChild(_mc_hand);
  64. _mc_hand.visible = false;
  65. _toolPanel = new DrawToolPanel();
  66. _toolPanel.drawFun = drawFun;
  67. _toolPanel.undoFun = undoRect;
  68. _toolPanel.completeFun = completeFun;
  69. _toolPanel.colorFun = colorFun;
  70. _toolPanel.exitFun = exitFun;
  71. _toolPanel.x = panel_w-_toolPanel.width;
  72. _toolPanel.y = panel_h + 2;
  73. this.addChild(_toolPanel);
  74. _txt_w_h = new TextField();
  75. _txt_w_h.width = 58;
  76. _txt_w_h.height = 16;
  77. _txt_w_h.mouseEnabled = false;
  78. _txt_w_h.textColor = 0xFFFFFF;
  79. _txt_w_h.background = true;
  80. _txt_w_h.backgroundColor = 0x010101;
  81. _txt_w_h.text = panel_w + "*" + panel_h;
  82. this.addChild(_txt_w_h);
  83. drawVec = new Vector.<DrawRect>();
  84. }
  85. private function update(bg_alpha:Number = 0.3):void
  86. {
  87. panel_w = spotVec[7].x-spotVec[0].x;
  88. panel_h = spotVec[7].y-spotVec[0].y;
  89. _bg.graphics.clear();
  90. _bg.graphics.beginFill(0xFFFFFF,bg_alpha);
  91. _bg.graphics.lineStyle(1,0);
  92. _bg.graphics.drawRect(spotVec[0].x,spotVec[0].y,panel_w,panel_h);
  93. _bg.graphics.endFill();
  94. updateSpot();
  95. if(localToGlobal(new Point(spotVec[0].x,spotVec[0].y)).y<16)
  96. {
  97. _txt_w_h.y = spotVec[0].y;
  98. }else
  99. {
  100. _txt_w_h.y = spotVec[0].y-16;
  101. }
  102. _txt_w_h.x = spotVec[0].x;
  103. _txt_w_h.text = panel_w + "*" + panel_h;
  104. }
  105. private function updateSpot():void
  106. {
  107. var per_w:uint = w_spot_num-1;
  108. var per_h:uint = h_spot_num-1;
  109. var i:uint = 0;
  110. var len:uint = spotVec.length;
  111. for(i;i<len;i++)
  112. {
  113. spotVec[i].x = spotVec[0].x + spotVec[i].index_x*(panel_w/per_w);
  114. spotVec[i].y = spotVec[0].y + spotVec[i].index_y*(panel_h/per_w);
  115. }
  116. }
  117. private function drawFun(type:uint = 1):void
  118. {
  119. _bg.removeEventListener(MouseEvent.MOUSE_DOWN,bgDown);
  120. if(!currentDraw || currentDraw.type!=type)
  121. {
  122. if(currentDraw)
  123. {
  124. currentDraw.removeEventListener(DrawRect.DRAW_COMPLETE,drawComplete);
  125. currentDraw.destroy();
  126. }
  127. addDraw(type);
  128. }
  129. }
  130. /**
  131. * ??????
  132. * @param type 1.??? 2.??? 3.???
  133. */
  134. public function addDraw(type:uint = 1):void
  135. {
  136. var rec:DrawRect;
  137. switch(type)
  138. {
  139. case DrawToolPanel.DRAW_RECT:
  140. rec = new DrawRect(_bg.getRect(_container));
  141. break;
  142. case DrawToolPanel.DRAW_ELLIPSE:
  143. rec = new DrawEllipse(_bg.getRect(_container));
  144. break;
  145. case DrawToolPanel.DRAW_ARROW:
  146. rec = new DrawArrow(_bg.getRect(_container));
  147. break;
  148. default:
  149. throw "???????????????";
  150. break;
  151. }
  152. rec.color = current_draw_color;
  153. rec.addEventListener(DrawRect.DRAW_COMPLETE,drawComplete);
  154. _container.addChild(rec);
  155. currentDraw = rec;
  156. }
  157. private function drawComplete(e:Event):void
  158. {
  159. var rec:DrawRect = e.currentTarget as DrawRect;
  160. rec.removeEventListener(DrawRect.DRAW_COMPLETE,drawComplete);
  161. drawVec.push(rec);
  162. addDraw(rec.type);
  163. }
  164. public function clearCurrentDraw():void
  165. {
  166. if(currentDraw)
  167. {
  168. currentDraw.removeEventListener(DrawRect.DRAW_COMPLETE,drawComplete);
  169. currentDraw.destroy();
  170. }
  171. }
  172. private function undoRect():void
  173. {
  174. if(drawVec.length>0)
  175. {
  176. var item:DrawRect = drawVec.pop();
  177. item.destroy();
  178. }
  179. }
  180. private function completeFun():void
  181. {
  182. var bmd:BitmapData = new BitmapData(_bg.width,_bg.height,true,0);
  183. var rec:Rectangle = _bg.getRect(this.parent);
  184. var matrix:Matrix = new Matrix();
  185. matrix.translate(-rec.x,-rec.y);
  186. this.visible = false;
  187. bmd.draw(_container,matrix);
  188. this.visible = true;
  189. var by:ByteArray = new JPGEncoder().encode(bmd);
  190. var eve:BmpEvent = new BmpEvent(CUT_COMPLETE);
  191. eve.data = by;
  192. dispatchEvent(eve);
  193. }
  194. private function colorFun($color:uint):void
  195. {
  196. current_draw_color = $color;
  197. if(currentDraw)
  198. {
  199. currentDraw.color = $color;
  200. }
  201. }
  202. private function exitFun():void
  203. {
  204. var eve:BmpEvent = new BmpEvent(CUT_EXIT);
  205. dispatchEvent(eve);
  206. }
  207. private function initSpot():void
  208. {
  209. spotVec = new Vector.<CutTouchSpot>();
  210. var per_w:uint = w_spot_num-1;
  211. var per_h:uint = h_spot_num-1;
  212. var i:uint = 0;
  213. var j:uint = 0;
  214. var index:uint = 0;
  215. for(i;i<3;i++)
  216. {
  217. j = 0;
  218. for(j;j<3;j++)
  219. {
  220. if(i!=0 && i!=per_w && j!=0 && j!=per_h)
  221. {
  222. continue;
  223. }
  224. var _spot:CutTouchSpot = new CutTouchSpot();
  225. _spot.index = index;
  226. _spot.addEventListener(CutTouchSpot.SPOT_MOVE,spotMove);
  227. _spot.addEventListener(CutTouchSpot.SPOT_DOWN,spotDown);
  228. _spot.addEventListener(CutTouchSpot.SPOT_UP,spotUp);
  229. _spot.x = (panel_w/per_w)*j;
  230. _spot.y = (panel_h/per_h)*i;
  231. _spot.index_x = j;
  232. _spot.index_y = i;
  233. _spot.original_x = _spot.x;
  234. _spot.original_y = _spot.y;
  235. _spot.spotList = spotVec;
  236. this.addChild(_spot);
  237. spotVec.push(_spot);
  238. index++;
  239. }
  240. }
  241. }
  242. private function spotDown(e:Event):void
  243. {
  244. _toolPanel.visible = false;
  245. _txt_w_h.visible = true;
  246. }
  247. private function spotUp(e:Event):void
  248. {
  249. _toolPanel.visible = true;
  250. update(0.1);
  251. updateToolPosition();
  252. }
  253. private function spotMove(e:Event):void
  254. {
  255. var target:CutTouchSpot = e.currentTarget as CutTouchSpot;
  256. if(target.index_x == 2 && target.index_y == 0)
  257. {
  258. spotVec[0].y = spotVec[0].original_y+(target.y-target.original_y);
  259. spotVec[7].x = spotVec[7].original_x+(target.x-target.original_x);
  260. }else if((target.index_x == 0 && target.index_y == 2))
  261. {
  262. spotVec[0].x = spotVec[0].original_x+(target.x-target.original_x);
  263. spotVec[7].y = spotVec[7].original_y+(target.y-target.original_y);
  264. }else if(target.index_x == 0)
  265. {
  266. spotVec[0].x = spotVec[0].original_x+(target.x-target.original_x);
  267. }else if(target.index_y == 0)
  268. {
  269. spotVec[0].y = spotVec[0].original_y+(target.y-target.original_y);
  270. }else if(target.index_x == 2)
  271. {
  272. spotVec[7].x = spotVec[7].original_x+(target.x-target.original_x);
  273. }else if(target.index_y == 2)
  274. {
  275. spotVec[7].y = spotVec[7].original_y+(target.y-target.original_y);
  276. }
  277. update();
  278. }
  279. private function bgDown(e:MouseEvent):void
  280. {
  281. _toolPanel.visible = false;
  282. var rect:Rectangle = DisplayObjectUtil.limitDisplayInRectangle(_bg,new Rectangle(0,0,stage.stageWidth,stage.stageHeight));
  283. this.startDrag(false,rect);
  284. stage.addEventListener(MouseEvent.MOUSE_UP,bgUp);
  285. _bg.addEventListener(MouseEvent.MOUSE_MOVE,bgMove);
  286. this.addEventListener(Event.ENTER_FRAME,enterFrame);
  287. _mc_hand.visible = true;
  288. _mc_hand.x = this.mouseX;
  289. _mc_hand.y = this.mouseY;
  290. Mouse.hide();
  291. }
  292. private function bgMove(e:MouseEvent):void
  293. {
  294. _mc_hand.x = this.mouseX;
  295. _mc_hand.y = this.mouseY;
  296. }
  297. private function bgUp(e:MouseEvent):void
  298. {
  299. _toolPanel.visible = true;
  300. this.removeEventListener(Event.ENTER_FRAME,enterFrame);
  301. _bg.removeEventListener(MouseEvent.MOUSE_MOVE,bgMove);
  302. stage.removeEventListener(MouseEvent.MOUSE_UP,bgUp);
  303. this.stopDrag();
  304. updateToolPosition();
  305. _mc_hand.visible = false;
  306. Mouse.show();
  307. }
  308. private function bgDoubleclick(e:MouseEvent):void
  309. {
  310. completeFun();
  311. }
  312. private function enterFrame(e:Event):void
  313. {
  314. if(localToGlobal(new Point(spotVec[0].x,spotVec[0].y)).y<16)
  315. {
  316. _txt_w_h.y = spotVec[0].y;
  317. }else
  318. {
  319. _txt_w_h.y = spotVec[0].y-16;
  320. }
  321. }
  322. /**
  323. * ???????
  324. */
  325. private function updateToolPosition():void
  326. {
  327. var _w:uint = spotVec[7].x;
  328. var _h:uint = spotVec[7].y;
  329. if(localToGlobal(new Point(_w,_h)).y>stage.stageHeight-24)
  330. {
  331. _toolPanel.x = _w-_toolPanel.width-5;
  332. _toolPanel.y = _h-24;
  333. }else
  334. {
  335. _toolPanel.x = _w-_toolPanel.width-5;
  336. _toolPanel.y = _h;
  337. }
  338. }
  339. public function destroy():void
  340. {
  341. this.removeEventListener(Event.ENTER_FRAME,enterFrame);
  342. stage.removeEventListener(MouseEvent.MOUSE_UP,bgUp);
  343. _bg.removeEventListener(MouseEvent.MOUSE_MOVE,bgMove);
  344. _bg.removeEventListener(MouseEvent.DOUBLE_CLICK,bgDoubleclick);
  345. while(drawVec.length>0)
  346. {
  347. var item:DrawRect = drawVec.pop();
  348. item.destroy();
  349. }
  350. while(spotVec.length>0)
  351. {
  352. var spot:CutTouchSpot = spotVec.pop();
  353. spot.removeEventListener(CutTouchSpot.SPOT_MOVE,spotMove);
  354. spot.destroy();
  355. }
  356. if(currentDraw)
  357. {
  358. currentDraw.removeEventListener(DrawRect.DRAW_COMPLETE,drawComplete);
  359. currentDraw.destroy();
  360. }
  361. if(_toolPanel)
  362. {
  363. _toolPanel.destroy();
  364. _toolPanel = null;
  365. }
  366. if(this.parent)
  367. {
  368. this.parent.removeChild(this);
  369. }
  370. }
  371. }
  372. }