PageRenderTime 162ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/avatar/src/sina/AvatarEditor.as

https://github.com/lvshiling/qizhon
ActionScript | 446 lines | 379 code | 48 blank | 19 comment | 15 complexity | 29ee49079b05d66015d7ade099c19d2a MD5 | raw file
  1. package sina
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.Graphics;
  6. import flash.display.Sprite;
  7. import flash.events.*;
  8. import flash.geom.Rectangle;
  9. import flash.net.FileFilter;
  10. import flash.net.FileReference;
  11. import flash.text.TextField;
  12. import flash.text.TextFieldAutoSize;
  13. import flash.text.TextFormat;
  14. import com.demonsters.debugger.MonsterDebugger;
  15. import qizhon.ui.skin.TakePicBtn;
  16. import sina.avatar.BinaryTransfer;
  17. import sina.avatar.DragBox;
  18. import sina.avatar.EditWindow;
  19. import sina.avatar.PhotoLoader;
  20. import sina.avatar.ThumbButton;
  21. import sina.avatar.VideoWindow;
  22. import sina.avatar.ZoomBar;
  23. import sina.ui.skin.MainLoading;
  24. //import sina.ui.skin.SaveBtn;
  25. //import sina.ui.skin.SelectBtn;
  26. import qizhon.ui.skin.SaveBtn;
  27. import qizhon.ui.skin.SelectBtn;
  28. import sina.ui.skin.SplitLine;
  29. import sina.utils.BaseGemo;
  30. /**
  31. * @author mole<mole1230@gmail.com>
  32. * @version $Id: AvatarEditor.as 7 2010-10-19 10:38:30Z mole1230 $
  33. */
  34. public class AvatarEditor extends Sprite
  35. {
  36. private const MARGIN_X:Number = 20;
  37. private const MARGIN_Y:Number = 10;
  38. private const AVAIL_WIDTH:Number = 670;
  39. private const AVAIL_HEIGHT:Number = 460;
  40. private const HEAD_GROUP_HEIGHT:Number = 40;
  41. private const EDIT_GROUP_HEIGHT:Number = 370;
  42. private const FOOT_GROUP_HEIGHT:Number = 50;
  43. private const EDITWINDOW_HEIGHT:Number = 300;
  44. private const EDITWINDOW_WIDTH:Number = 300;
  45. private const WRAPPER_WIDTH:Number = EDITWINDOW_WIDTH;
  46. private const WRAPPER_HEIGHT:Number = EDIT_GROUP_HEIGHT;
  47. private const SMALL_THUMB_WIDTH:Number = 30;
  48. private const SMALL_THUMB_HEIGHT:Number = 30;
  49. private const MIDDLE_THUMB_WIDTH:Number = 50;
  50. private const MIDDLE_THUMB_HEIGHT:Number = 50;
  51. private const BIG_THUMB_WIDTH:Number = 180;
  52. private const BIG_THUMB_HEIGHT:Number = 180;
  53. private var thumbs:Object;
  54. private var mainLoading:MainLoading;
  55. private var photoLoader:PhotoLoader;
  56. private var binaryTransfer:BinaryTransfer;
  57. private var headGroup:Sprite;
  58. private var footGroup:Sprite;
  59. private var editGroup:Sprite;
  60. private var editLeft:Sprite;
  61. private var editRight:Sprite;
  62. private var editLeftWrapper:Sprite;
  63. private var editRightWrapper:Sprite;
  64. private var splitLine:SplitLine;
  65. private var selectBtn:SelectBtn;
  66. private var zoomBar:ZoomBar;
  67. private var previewWindow:Sprite;
  68. private var smallEditWindow:Sprite;
  69. private var middleEditWindow:Sprite;
  70. private var bigEditWindow:EditWindow;
  71. private var smallThumb:ThumbButton;
  72. private var middleThumb:ThumbButton;
  73. private var bigThumb:ThumbButton;
  74. private var saveBtn:SaveBtn;
  75. private var thumbWrapper:Sprite;
  76. //add by stefan
  77. private var takePicBtn:TakePicBtn;
  78. private var editMiddle:Sprite;
  79. public function AvatarEditor()
  80. {
  81. var gp:Graphics;
  82. this.headGroup = new Sprite();
  83. this.footGroup = new Sprite();
  84. this.editGroup = new Sprite();
  85. gp = this.headGroup.graphics;
  86. gp.beginFill(0xc2edf4, 0);
  87. gp.drawRect(0, 0, AVAIL_WIDTH - 2 * MARGIN_X, HEAD_GROUP_HEIGHT);
  88. gp.endFill();
  89. this.headGroup.x = MARGIN_X;
  90. this.headGroup.y = MARGIN_Y;
  91. gp = this.editGroup.graphics;
  92. gp.beginFill(0xeeb059, 0);
  93. gp.drawRect(0, 0, AVAIL_WIDTH - 2 * MARGIN_X, EDIT_GROUP_HEIGHT);
  94. gp.endFill();
  95. this.editGroup.x = MARGIN_X;
  96. this.editGroup.y = this.headGroup.y + this.headGroup.height;
  97. gp = this.footGroup.graphics;
  98. gp.beginFill(0xCFDEE9, 0);
  99. gp.drawRect(0, 0, AVAIL_WIDTH, FOOT_GROUP_HEIGHT);
  100. gp.endFill();
  101. this.footGroup.x = 0;
  102. this.footGroup.y = this.editGroup.y + this.editGroup.height;
  103. this.addChild(this.headGroup);
  104. this.addChild(this.editGroup);
  105. this.addChild(this.footGroup);
  106. }
  107. public function init(e:Event = null):void
  108. {
  109. removeEventListener(Event.ADDED_TO_STAGE, this.init);
  110. this.addHeadGroup();
  111. this.addEditGroup();
  112. this.addFootGroup();
  113. this.initListeners();
  114. this.resetThumbs();
  115. }
  116. private function initListeners():void
  117. {
  118. this.selectBtn.addEventListener(MouseEvent.CLICK, browser);
  119. this.takePicBtn.addEventListener(MouseEvent.CLICK,takePicHandler);
  120. this.photoLoader.addEventListener(PhotoLoader.COMPLETE, editPhoto);
  121. this.zoomBar.addEventListener(ZoomBar.SEEKING, this.zoomHandler);
  122. this.zoomBar.addEventListener(ZoomBar.SEEKEND, this.zoomHandler);
  123. this.bigEditWindow.addEventListener(EditWindow.SNAP, this.editWindowHandler);
  124. this.saveBtn.addEventListener(MouseEvent.CLICK, this.saveHandler);
  125. this.binaryTransfer.addEventListener(BinaryTransfer.COMPLETE, transferHandler);
  126. this.binaryTransfer.addEventListener(BinaryTransfer.ERROR, transferHandler);
  127. //
  128. }
  129. private function saveHandler(event:MouseEvent):void
  130. {
  131. switch (event.type) {
  132. case MouseEvent.CLICK:
  133. this.transfer('small');
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139. private function transferHandler(event:Event):void
  140. {
  141. switch (event.type) {
  142. case BinaryTransfer.COMPLETE:
  143. var complete:Boolean = true;
  144. for (var type:String in this.thumbs) {
  145. if (!this.thumbs[type]['transfered']) {
  146. complete = false
  147. this.transfer(type);
  148. break;
  149. }
  150. }
  151. if (complete) {
  152. Params.call({code:"A00001"});
  153. }
  154. break;
  155. case BinaryTransfer.ERROR:
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. private function zoomHandler(event:Event):void
  162. {
  163. switch (event.type) {
  164. case ZoomBar.SEEKING:
  165. this.bigEditWindow.photoScale(this.zoomBar.rate);
  166. break;
  167. case ZoomBar.SEEKEND:
  168. this.bigEditWindow.snap();
  169. break;
  170. default:
  171. break;
  172. }
  173. }
  174. private function editWindowHandler(event:Event):void
  175. {
  176. switch (event.target) {
  177. case this.bigEditWindow:
  178. this.bigThumb.draw(this.bigEditWindow.bigThumbData);
  179. this.middleThumb.draw(this.bigEditWindow.middleThumbData);
  180. this.smallThumb.draw(this.bigEditWindow.smallThumbData);
  181. this.resetThumbs();
  182. break;
  183. default:
  184. break;
  185. }
  186. }
  187. private function transfer(type:String):void
  188. {
  189. if (this.thumbs[type]['transfered']) {
  190. return;
  191. }
  192. this.thumbs[type]['transfered'] = true;
  193. var params:Params = Params.getInstance();
  194. var gets:Object = {
  195. size: this.thumbs[type]['size'],
  196. uid: params.uid,
  197. token: params.token
  198. };
  199. this.binaryTransfer.transfer(this.bigEditWindow[this.thumbs[type]['data']], params.saveUrl, gets);
  200. }
  201. private function resetThumbs():void
  202. {
  203. this.thumbs = {
  204. small: { transfered: false, size: SMALL_THUMB_WIDTH + "x" + SMALL_THUMB_HEIGHT, data: "smallThumbData" },
  205. middle: { transfered: false, size: MIDDLE_THUMB_WIDTH + "x" + MIDDLE_THUMB_HEIGHT, data: "middleThumbData" },
  206. big: { transfered: false, size: BIG_THUMB_WIDTH + "x" + BIG_THUMB_HEIGHT, data: "bigThumbData" }
  207. }
  208. }
  209. private function addHeadGroup():void
  210. {
  211. this.selectBtn = new SelectBtn();
  212. this.selectBtn.buttonMode = true;
  213. this.headGroup.addChild(this.selectBtn);
  214. var guideText:TextField = new TextField();
  215. var tf:TextFormat = new TextFormat("宋体", 13, 0x0099FF);
  216. guideText.defaultTextFormat = tf;
  217. guideText.selectable = false;
  218. guideText.autoSize = TextFieldAutoSize.LEFT;
  219. guideText.text = "仅支持JPG、GIF、PNG图片文件,且文件小于10M";
  220. this.headGroup.addChild(guideText);
  221. guideText.x = this.selectBtn.x + this.selectBtn.width + 10;
  222. guideText.y = (this.selectBtn.height - guideText.height) / 2;
  223. this.photoLoader = new PhotoLoader();
  224. this.photoLoader.x = guideText.x + guideText.width + 10;
  225. this.photoLoader.y = (this.selectBtn.height - this.photoLoader.height) / 2
  226. this.headGroup.addChild(this.photoLoader);
  227. //
  228. this.takePicBtn = new TakePicBtn;
  229. this.takePicBtn.buttonMode = true;
  230. takePicBtn.x = this.photoLoader.x + this.photoLoader.width + 10;
  231. takePicBtn.y = (this.photoLoader.height - photoLoader.height) / 2;
  232. this.headGroup.addChild(takePicBtn);
  233. }
  234. private function addEditGroup():void
  235. {
  236. var gp:Graphics;
  237. this.splitLine = new SplitLine();
  238. this.editLeft = new Sprite();
  239. this.editRight = new Sprite();
  240. //
  241. this.editMiddle = new Sprite();
  242. this.editLeftWrapper = new Sprite();
  243. this.editRightWrapper = new Sprite();
  244. this.thumbWrapper = new Sprite();
  245. this.editGroup.addChild(this.splitLine);
  246. this.editGroup.addChild(this.editLeft);
  247. this.editGroup.addChild(this.editRight);
  248. //
  249. this.editGroup.addChild(this.editMiddle);
  250. this.editLeft.addChild(this.editLeftWrapper);
  251. this.editLeft.addChild(this.editRightWrapper);
  252. this.editLeftWrapper.visible = false;
  253. this.splitLine.x = (this.editGroup.width - this.splitLine.width) / 2;
  254. this.splitLine.y = (this.editGroup.height - this.splitLine.height) / 2;
  255. gp = this.editLeft.graphics;
  256. gp.beginFill(0xab8244, 0);
  257. gp.drawRect(0, 0, (this.editGroup.width - this.splitLine.width) / 2, this.editGroup.height);
  258. gp.endFill();
  259. gp = this.editRight.graphics;
  260. gp.beginFill(0xf5d832, 0);
  261. gp.drawRect(0, 0, (this.editGroup.width - this.splitLine.width) / 2, this.editGroup.height);
  262. gp.endFill();
  263. this.editRight.x = this.splitLine.x + this.splitLine.width;
  264. //
  265. gp = this.editMiddle.graphics;
  266. gp.beginFill(0xab8244, 0);
  267. gp.drawRect(0, 0, (this.editGroup.width - this.splitLine.width) / 2, this.editGroup.height);
  268. gp.endFill();
  269. this.editMiddle.x = (this.editGroup.width - this.editMiddle.width) / 2;
  270. this.editLeftWrapper.x = (this.editLeft.width - WRAPPER_WIDTH) / 2;
  271. this.editRightWrapper.x = (this.editRight.width - WRAPPER_WIDTH) / 2;
  272. var guideText:TextField = new TextField();
  273. var tf:TextFormat = new TextFormat("宋体", 13, 0x0099FF);
  274. guideText.defaultTextFormat = tf;
  275. guideText.selectable = false;
  276. guideText.autoSize = TextFieldAutoSize.LEFT;
  277. guideText.text = "您上传的头像会自动生成三种尺寸,\n请注意中小尺寸的头像是否清晰。";
  278. this.editRightWrapper.addChild(guideText);
  279. gp = this.thumbWrapper.graphics;
  280. gp.beginFill(0xFFFFFF, 0);
  281. gp.drawRect(0, 0, this.editRight.width, this.editRight.height - guideText.y - guideText.height - 20);
  282. gp.endFill();
  283. this.editRightWrapper.addChild(this.thumbWrapper);
  284. this.thumbWrapper.y = guideText.y + guideText.height + 20;
  285. this.zoomBar = new ZoomBar();
  286. this.zoomBar.y = EDITWINDOW_HEIGHT + 10;
  287. this.zoomBar.x = this.zoomBar.leftMargin;
  288. this.editLeftWrapper.addChild(this.zoomBar);
  289. this.createWindows();
  290. }
  291. private function addFootGroup():void
  292. {
  293. this.saveBtn = new SaveBtn();
  294. this.saveBtn.buttonMode = true;
  295. this.saveBtn.visible = false;
  296. this.saveBtn.x = (this.footGroup.width - this.saveBtn.width) / 2;
  297. this.saveBtn.y = (this.footGroup.height - this.saveBtn.height) / 2;
  298. this.footGroup.addChild(this.saveBtn);
  299. this.binaryTransfer = new BinaryTransfer();
  300. this.binaryTransfer.x = this.saveBtn.x + this.saveBtn.width + 5;
  301. this.binaryTransfer.y = (this.footGroup.height - this.binaryTransfer.height) / 2;
  302. this.footGroup.addChild(this.binaryTransfer);
  303. }
  304. private function createWindows():void
  305. {
  306. //this.smallEditWindow = new EditWindow(EDITWINDOW_WIDTH, EDITWINDOW_HEIGHT, SMALL_THUMB_WIDTH, SMALL_THUMB_HEIGHT);
  307. //this.middleEditWindow = new EditWindow(EDITWINDOW_WIDTH, EDITWINDOW_HEIGHT, MIDDLE_THUMB_WIDTH, MIDDLE_THUMB_HEIGHT);
  308. this.bigEditWindow = new EditWindow(EDITWINDOW_WIDTH, EDITWINDOW_HEIGHT, BIG_THUMB_WIDTH, BIG_THUMB_HEIGHT);
  309. var text:String = SMALL_THUMB_WIDTH + "x" + SMALL_THUMB_HEIGHT + "像素";
  310. this.smallThumb = new ThumbButton(SMALL_THUMB_WIDTH, SMALL_THUMB_HEIGHT, text);
  311. text = MIDDLE_THUMB_WIDTH + "x" + MIDDLE_THUMB_HEIGHT + "像素";
  312. this.middleThumb = new ThumbButton(MIDDLE_THUMB_WIDTH, MIDDLE_THUMB_HEIGHT, text);
  313. text = BIG_THUMB_WIDTH + "x" + BIG_THUMB_WIDTH + "像素";
  314. this.bigThumb = new ThumbButton(BIG_THUMB_WIDTH, BIG_THUMB_HEIGHT, text);
  315. //this.smallEditWindow.visible = false;
  316. //this.middleEditWindow.visible = false;
  317. //this.bigEditWindow.visible = false;
  318. //this.editLeftWrapper.addChild(this.smallEditWindow);
  319. //this.editLeftWrapper.addChild(this.middleEditWindow);
  320. this.editLeftWrapper.addChild(this.bigEditWindow);
  321. this.thumbWrapper.addChild(this.smallThumb);
  322. this.thumbWrapper.addChild(this.middleThumb);
  323. this.thumbWrapper.addChild(this.bigThumb);
  324. this.middleThumb.x = this.bigThumb.x + this.bigThumb.width + 20;
  325. this.smallThumb.x = this.middleThumb.x;
  326. this.smallThumb.y = this.middleThumb.y + this.middleThumb.height + 30;
  327. this.photoLoader.loadFromRemote(Params.getInstance().photoUrl);
  328. }
  329. private function browser(event:MouseEvent):void
  330. {
  331. this.photoLoader.loadFromLocal();
  332. }
  333. private function takePicHandler(event:MouseEvent):void
  334. {
  335. trace("---trace takepic ---");
  336. this.takePicBtn.mouseChildren = false;
  337. this.takePicBtn.mouseEnabled = false;
  338. this.takePicBtn.enabled = false;
  339. var win:Sprite = new VideoWindow();
  340. editMiddle.addChild(win);
  341. if (!this.editGroup.contains(editMiddle))
  342. {
  343. this.editGroup.addChild(editMiddle);
  344. }
  345. this.editLeftWrapper.visible = false;
  346. this.editRightWrapper.visible = false;
  347. this.saveBtn.visible = false;
  348. this.splitLine.visible = false;
  349. win.addEventListener(Event.COMPLETE, onCaptureComplete);
  350. }
  351. private function editPhoto(event:Event):void
  352. {
  353. if (this.photoLoader.isLocal) {
  354. this.editLeftWrapper.visible = true;
  355. this.saveBtn.visible = true;
  356. if (!this.editRight.contains(this.editRightWrapper)) {
  357. this.editRight.addChild(this.editRightWrapper);
  358. }
  359. this.editRightWrapper.visible = true;
  360. } else {
  361. this.editLeftWrapper.visible = false;
  362. this.saveBtn.visible = false;
  363. }
  364. if (this.editGroup.contains(this.editMiddle))
  365. {
  366. this.editGroup.removeChild(this.editMiddle);
  367. if (this.editMiddle.numChildren > 0)
  368. {
  369. (this.editMiddle.getChildAt(0) as VideoWindow).destroy();
  370. }
  371. }
  372. this.bigEditWindow.edit(event.target.editData, BIG_THUMB_WIDTH, BIG_THUMB_HEIGHT);
  373. this.bigEditWindow.forcePhotoScale(0, this.zoomBar);
  374. this.takePicBtn.mouseChildren = true;
  375. this.takePicBtn.mouseEnabled = true;
  376. }
  377. private function onCaptureComplete(event:Event):void
  378. {
  379. trace("--onCaptureComplete--");
  380. var videoWin:VideoWindow = event.target as VideoWindow;
  381. videoWin.removeEventListener(Event.COMPLETE, onCaptureComplete);
  382. videoWin.destroy();
  383. this.editMiddle.removeChild(videoWin);
  384. this.editGroup.removeChild(this.editMiddle);
  385. this.editLeftWrapper.visible = true;
  386. this.saveBtn.visible = true;
  387. if (!this.editRight.contains(this.editRightWrapper)) {
  388. this.editRight.addChild(this.editRightWrapper);
  389. }
  390. this.editRightWrapper.visible = true;
  391. this.splitLine.visible = true;
  392. this.takePicBtn.mouseChildren = true;
  393. this.takePicBtn.mouseEnabled = true;
  394. this.bigEditWindow.edit(event.target.editData, BIG_THUMB_WIDTH, BIG_THUMB_HEIGHT);
  395. this.bigEditWindow.forcePhotoScale(0, this.zoomBar);
  396. }
  397. }
  398. }