PageRenderTime 144ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/windid/res/swf/avatar/face/com/phpwind/avatar/GIFEditWindow.as

https://gitlab.com/wuhang2003/phpwind
ActionScript | 271 lines | 209 code | 10 blank | 52 comment | 13 complexity | 3b2a3eb56753e92692cb0dca0944b34d MD5 | raw file
  1. package com.phpwind.avatar
  2. {
  3. import com.phpwind.avatar.DragBox;
  4. import fl.controls.Slider;
  5. import com.phpwind.avatar.IEditWindow;
  6. import flash.display.Bitmap;
  7. import flash.display.BitmapData;
  8. import flash.display.BlendMode;
  9. import flash.display.Sprite;
  10. import flash.events.Event;
  11. import flash.events.MouseEvent;
  12. import flash.geom.Matrix;
  13. import flash.geom.Rectangle;
  14. import flash.ui.Mouse;
  15. import org.bytearray.gif.player.GIFPlayer;
  16. public class GIFEditWindow extends Sprite implements IEditWindow
  17. {
  18. private var maskBlock:Sprite;
  19. public var photoSp:GIFPlayer;
  20. private var photoContainer:Sprite;
  21. private var photoMask:Sprite;
  22. private var maskContainer:Sprite;
  23. private var alphaBg:Sprite;
  24. private var sideLength:uint;
  25. private var dragBox:DragBox;
  26. private var scaleMax:Number;
  27. private var scaleMin:Number;
  28. private var smallPreview:Bitmap;
  29. private var normalPreview:Bitmap;
  30. private var slider:Slider;
  31. public function GIFEditWindow(bitmap:GIFPlayer, rect:Rectangle, small:Bitmap, normal:Bitmap, slider:Slider)
  32. {
  33. this.smallPreview = small;
  34. this.normalPreview = normal;
  35. this.slider = slider;
  36. this.photoSp = bitmap;
  37. this.x = rect.x;
  38. this.y = rect.y;
  39. this.sideLength = rect.width;
  40. this.addEventListener(Event.ADDED_TO_STAGE, onStage);
  41. }
  42. private function drawSprite(sideLength:uint, color:uint, alpha:Number=1.0):Sprite
  43. {
  44. var sprite:Sprite = new Sprite();
  45. sprite.graphics.beginFill(color,alpha);
  46. sprite.graphics.drawRect(0, 0, sideLength, sideLength);
  47. sprite.graphics.endFill();
  48. return sprite;
  49. }
  50. //载入图片,画出遮罩
  51. private function onStage(e:Event):void
  52. {
  53. this.removeEventListener(Event.ADDED_TO_STAGE, onStage);
  54. //处理图层
  55. photoContainer = new Sprite();
  56. //photoSp = new Bitmap(photoBitMapData, 'auto', true);
  57. photoContainer.addChild(photoSp);
  58. //计算最大最小的scaleX
  59. calculateZoom();
  60. //处理遮罩层
  61. photoMask = drawSprite(sideLength, 0x00000000);
  62. photoContainer.mask = photoMask;
  63. addChild(photoContainer);
  64. addChild(photoMask);
  65. //处理蒙板层
  66. maskContainer = new Sprite();
  67. alphaBg = drawSprite(sideLength, 0x00000000, 0.5);
  68. maskBlock = drawSprite(Math.min(100, photoSp.width, photoSp.height), 0x00000000);
  69. maskBlock.blendMode = BlendMode.ERASE;
  70. maskContainer.addChild(alphaBg);
  71. maskContainer.addChild(maskBlock);
  72. maskContainer.blendMode = BlendMode.LAYER;
  73. addChild(maskContainer);
  74. //设置位置
  75. maskBlock.x=maskBlock.y=150-maskBlock.width/2;
  76. //画出dragbox
  77. if(!dragBox)
  78. {
  79. dragBox = new DragBox(maskBlock.getBounds(this));
  80. addChild(dragBox);
  81. dragBox.addEventListener(DragBox.START_MOVE, onMoveStart);
  82. dragBox.addEventListener(DragBox.STOPMOVE, onMoveStop);
  83. }
  84. movePhoto();
  85. setHeadPic();
  86. }
  87. /**
  88. * 初始化缩放
  89. */
  90. private function calculateZoom():void
  91. {
  92. if(photoSp.width>sideLength && photoSp.height>sideLength)//大图
  93. {
  94. scaleMax=1.0;
  95. scaleMin=sideLength/Math.max(photoSp.width,photoSp.height);
  96. photoSp.scaleX= photoSp.scaleY = sideLength/Math.min(photoSp.width,photoSp.height);
  97. }
  98. else if(photoSp.width<sideLength && photoSp.height<sideLength)//小图
  99. {
  100. scaleMax = sideLength/Math.min(photoSp.width,photoSp.height);
  101. scaleMin = 1.0;
  102. }
  103. else
  104. {
  105. scaleMax = sideLength/Math.min(photoSp.width,photoSp.height);
  106. scaleMin = sideLength/Math.max(photoSp.width,photoSp.height);
  107. }
  108. slider.minimum = scaleMin*100;
  109. slider.maximum = scaleMax*100;
  110. slider.value = photoSp.scaleX*100;
  111. slider.addEventListener(Event.CHANGE, sliderZoom);
  112. }
  113. private function onMove(event:MouseEvent) : void
  114. {
  115. adjustMask();
  116. movePhoto();
  117. setHeadPic();
  118. //判断是否已经超出边界
  119. var rect:Rectangle = dragBox.getRect(stage);
  120. if( (photoSp.hitTestPoint(rect.x,rect.y) &&
  121. photoSp.hitTestPoint(rect.x+rect.width,rect.y) &&
  122. photoSp.hitTestPoint(rect.x,rect.y+rect.height) &&
  123. photoSp.hitTestPoint(rect.x+rect.width,rect.y+rect.height)) == false )
  124. {
  125. if(photoSp.width>photoSp.height){
  126. photoSp.height = rect.width;
  127. photoSp.scaleX = photoSp.scaleY;
  128. }else{
  129. photoSp.width = rect.height;
  130. photoSp.scaleY = photoSp.scaleX;
  131. }
  132. slider.value = photoSp.scaleX*100;
  133. }
  134. }
  135. /**
  136. * 让洞对准拖拽框
  137. */
  138. private function adjustMask() : void
  139. {
  140. maskBlock.x = dragBox.x;
  141. maskBlock.y = dragBox.y;
  142. maskBlock.width = dragBox.width;
  143. maskBlock.height = dragBox.height;
  144. return;
  145. }
  146. /**
  147. * 开始拖拽
  148. */
  149. private function onMoveStart(event:Event) : void
  150. {
  151. stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
  152. }// end function
  153. /**
  154. * 停止拖拽
  155. */
  156. private function onMoveStop(event:Event) : void
  157. {
  158. stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
  159. setHeadPic();
  160. //adjustMask();
  161. //dragBox.isDragging && movePhoto();
  162. //this.snapp(null);
  163. }
  164. /**
  165. * 移动图片
  166. */
  167. private function movePhoto() : void
  168. {
  169. photoSp.x=(sideLength-photoSp.width)*dragBox.x/(sideLength-dragBox.width);
  170. photoSp.y=(sideLength-photoSp.height)*dragBox.y/(sideLength-dragBox.height);
  171. }
  172. /**
  173. * 放大图片
  174. */
  175. public function zoomIn():void
  176. {
  177. var r:Number = Math.min(photoSp.scaleX+0.05*(scaleMax-scaleMin),scaleMax);
  178. photoSp.scaleY=photoSp.scaleX = r;
  179. movePhoto();
  180. setHeadPic();
  181. slider.value = r*100;
  182. }
  183. /**
  184. * 缩小图片
  185. */
  186. public function zoomOut():void
  187. {
  188. var r:Number = Math.max(photoSp.scaleX+0.05*(scaleMin-scaleMax),scaleMin);
  189. photoSp.scaleY=photoSp.scaleX = r;
  190. reScaledragBox();
  191. movePhoto();
  192. setHeadPic();
  193. slider.value = r*100;
  194. }
  195. /**
  196. * 顺时针旋转
  197. */
  198. public function clockwise():void
  199. {
  200. var matrix:Matrix = new Matrix(0,1,-1,0,photoSp.bitmapData.height,0);
  201. var newBitmapData:BitmapData = new BitmapData(photoSp.bitmapData.height,photoSp.bitmapData.width);
  202. newBitmapData.draw(photoSp.bitmapData,matrix,null,null,null,true);
  203. photoSp.bitmapData = newBitmapData;
  204. //交换x,y;
  205. //photoSp.y=photoSp.x^photoSp.y^(photoSp.x=photoSp.y);
  206. movePhoto();
  207. setHeadPic();
  208. }
  209. /**
  210. * 逆时针旋转
  211. */
  212. public function anticlockwise():void
  213. {
  214. var matrix:Matrix = new Matrix(0,-1,1,0,0,photoSp.bitmapData.width);
  215. var newBitmapData:BitmapData = new BitmapData(photoSp.bitmapData.height,photoSp.bitmapData.width);
  216. newBitmapData.draw(photoSp.bitmapData,matrix,null,null,null,true);
  217. photoSp.bitmapData = newBitmapData;
  218. //交换x,y;
  219. //photoSp.y=photoSp.x^photoSp.y^(photoSp.x=photoSp.y);
  220. movePhoto();
  221. setHeadPic();
  222. }
  223. /**
  224. * 拉动slider
  225. */
  226. private function sliderZoom(e:Event):void
  227. {
  228. photoSp.scaleY=photoSp.scaleX = slider.value/100;
  229. reScaledragBox();
  230. movePhoto();
  231. setHeadPic();
  232. }
  233. /**
  234. * 生成头像
  235. */
  236. private function setHeadPic():void
  237. {
  238. var smallMatrix:Matrix = new Matrix();
  239. smallMatrix.a = smallMatrix.d = 48*photoSp.scaleX/dragBox.width;
  240. smallMatrix.tx = (photoSp.x-dragBox.x)*48/dragBox.width;
  241. smallMatrix.ty = (photoSp.y-dragBox.y)*48/dragBox.height;
  242. smallPreview.bitmapData.draw(photoSp.bitmapData,smallMatrix,null,null, new Rectangle(0,0,48,48),true);
  243. var normalMatrix:Matrix = new Matrix();
  244. normalMatrix.a = normalMatrix.d = 120*photoSp.scaleX/dragBox.width;
  245. normalMatrix.tx = (photoSp.x-dragBox.x)*120/dragBox.width;
  246. normalMatrix.ty = (photoSp.y-dragBox.y)*120/dragBox.height;
  247. normalPreview.bitmapData.draw(photoSp.bitmapData,normalMatrix,null,null, new Rectangle(0,0,120,120),true);
  248. }
  249. /**
  250. * 重绘拖拽框大小
  251. */
  252. private function reScaledragBox():void
  253. {
  254. var m:Number = Math.min(photoSp.width, photoSp.height);
  255. if(dragBox.width>m){
  256. dragBox.rewriteBox(m);
  257. adjustMask();
  258. }
  259. }
  260. }
  261. }