PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/VideoAuth/src/sina/AvatarEditor.as

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