PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/facecontrol/gui/Photo.as

https://github.com/vkflashmedia/Facecontrol
ActionScript | 452 lines | 374 code | 73 blank | 5 comment | 49 complexity | bd6f5f0a42e88ebea375d936cbe799dc MD5 | raw file
  1. package com.facecontrol.gui
  2. {
  3. import com.facecontrol.util.Images;
  4. import com.flashmedia.basics.GameObject;
  5. import com.flashmedia.basics.GameScene;
  6. import com.flashmedia.util.BitmapUtil;
  7. import flash.display.Bitmap;
  8. import flash.display.Sprite;
  9. import flash.geom.Matrix;
  10. public class Photo extends GameObject
  11. {
  12. public static const BORDER_TYPE_ROUND_RECT:uint = 0;
  13. public static const BORDER_TYPE_RECT:uint = 1;
  14. public static const ALIGN_LEFT:uint = 0;
  15. public static const ALIGN_CENTER:uint = 1;
  16. public static const VERTICAL_ALIGN_TOP:uint = 0;
  17. public static const VERTICAL_ALIGN_CENTER:uint = 1;
  18. public static const HORIZONTAL_ALIGN_LEFT:uint = 0;
  19. public static const HORIZONTAL_ALIGN_CENTER:uint = 1;
  20. public static const VERTICAL_SCALE_AUTO:uint = 0;
  21. public static const VERTICAL_SCALE_ALWAYS:uint = 1;
  22. public static const HORIZONTAL_SCALE_AUTO:uint = 0;
  23. public static const HORIZONTAL_SCALE_ALWAYS:uint = 1;
  24. public var index:int = 0;
  25. private var _align:uint = ALIGN_LEFT;
  26. private var _photoBorder:uint;
  27. private var _photoBorderColor:int;
  28. private var _borderType:uint = BORDER_TYPE_ROUND_RECT;
  29. private var _photo:Bitmap;
  30. private var _thumbnail:Bitmap;
  31. private var _transparentSquare:Sprite;
  32. private var _squareMask:Sprite;
  33. private var _valign:uint;
  34. private var _halign:uint;
  35. private var _vscale:uint;
  36. private var _hscale:uint;
  37. private var _photoWidth:int;
  38. private var _photoHeight:int;
  39. private var _frameIndex:int;
  40. private var _photoRaw:Object;
  41. public function Photo(value:GameScene, image:Bitmap, x:int, y:int, width:int, height:int, type:uint=0, frameIndex:int=0)
  42. {
  43. super(value);
  44. // _photoRaw = photoRaw;
  45. _photoBorderColor = 0x000000;
  46. _borderType = type;
  47. _photoBorder = 2;
  48. this.x = x;
  49. this.y = y;
  50. this.width = width;
  51. this.height = height;
  52. autoSize = false;
  53. photo = image;
  54. }
  55. public function set verticalScale(value:uint):void {
  56. _vscale = value;
  57. update();
  58. }
  59. public function set horizontalScale(value:uint):void {
  60. _hscale = value;
  61. update();
  62. }
  63. public function set verticalAlign(value:uint):void {
  64. _valign = value;
  65. update();
  66. }
  67. public function set horizontalAlign(value:uint):void {
  68. _halign = value;
  69. update();
  70. }
  71. public function set photoBorder(value:uint):void {
  72. _photoBorder = value;
  73. update();
  74. }
  75. public function set photoBorderColor(value:int):void {
  76. _photoBorderColor = value;
  77. update();
  78. }
  79. public function set borderType(value:uint):void {
  80. _borderType = value;
  81. update();
  82. }
  83. public function set align(value:uint):void {
  84. _align = value;
  85. update();
  86. }
  87. private function update():void {
  88. // graphics.beginFill(0x00ff00, 1);
  89. // graphics.drawRect(0, 0, width, height);
  90. while (numChildren > 0) {
  91. removeChildAt(0);
  92. }
  93. if (_photo) {
  94. _thumbnail = new Bitmap(_photo.bitmapData, 'auto', true);
  95. if (_hscale == HORIZONTAL_SCALE_ALWAYS && _vscale == VERTICAL_SCALE_ALWAYS) {
  96. var s:Number = (width - _photoBorder*2) / _thumbnail.width;
  97. var matrix:Matrix = new Matrix();
  98. matrix.scale(s, s);
  99. _thumbnail.transform.matrix = matrix;
  100. if (_thumbnail.height < (height - _photoBorder*2)) {
  101. _thumbnail = new Bitmap(_photo.bitmapData, 'auto', true);
  102. s = (height - _photoBorder*2) / _thumbnail.height;
  103. matrix = new Matrix();
  104. matrix.scale(s, s);
  105. _thumbnail.transform.matrix = matrix;
  106. }
  107. }
  108. else if (_hscale == HORIZONTAL_SCALE_ALWAYS && _vscale == VERTICAL_SCALE_AUTO) {
  109. _thumbnail = new Bitmap(_photo.bitmapData, 'auto', true);
  110. s = (width - _photoBorder*2) / _thumbnail.width;
  111. matrix = new Matrix();
  112. matrix.scale(s, s);
  113. _thumbnail.transform.matrix = matrix;
  114. }
  115. else if (_hscale == HORIZONTAL_SCALE_AUTO && _vscale == VERTICAL_SCALE_ALWAYS) {
  116. _thumbnail = new Bitmap(_photo.bitmapData, 'auto', true);
  117. s = (height - _photoBorder*2) / _thumbnail.height;
  118. matrix = new Matrix();
  119. matrix.scale(s, s);
  120. _thumbnail.transform.matrix = matrix;
  121. }
  122. else if (_hscale == HORIZONTAL_SCALE_AUTO && _vscale == VERTICAL_SCALE_AUTO) {
  123. if (_thumbnail.width > (width - _photoBorder*2)) {
  124. s = (width - _photoBorder*2) / _thumbnail.width;
  125. if (s < 1) {
  126. matrix = new Matrix();
  127. matrix.scale(s, s);
  128. _thumbnail.transform.matrix = matrix;
  129. }
  130. else matrix = null;
  131. }
  132. if (_thumbnail.height < (height - _photoBorder*2)) {
  133. _thumbnail = new Bitmap(_photo.bitmapData, 'auto', true);
  134. s = (height - _photoBorder*2) / _thumbnail.height;
  135. if (s < 1) {
  136. matrix = new Matrix();
  137. matrix.scale(s, s);
  138. _thumbnail.transform.matrix = matrix;
  139. }
  140. else matrix = null;
  141. }
  142. }
  143. _photoWidth = width;
  144. _photoHeight = height;
  145. var xIndent:int = 0;
  146. var yIndent:int = 0;
  147. _photoWidth = (_thumbnail.width < width) ? _thumbnail.width : width;
  148. _photoHeight = (_thumbnail.height < height) ? _thumbnail.height : height;
  149. if (!matrix) {
  150. if (_align == ALIGN_CENTER) {
  151. xIndent = (width - _photoWidth) / 2;
  152. yIndent = (height - _photoHeight) / 2;
  153. }
  154. }
  155. var squareWidth:Number = _photoWidth - _photoBorder * 2;
  156. var squareHeight:Number = _photoHeight - _photoBorder * 2;
  157. _squareMask = new Sprite();
  158. _squareMask.graphics.beginFill(_photoBorderColor);
  159. switch (_borderType) {
  160. case BORDER_TYPE_ROUND_RECT:
  161. _squareMask.graphics.drawRoundRect(xIndent + _photoBorder, yIndent + _photoBorder, squareWidth, squareHeight, 15, 15);
  162. break;
  163. case BORDER_TYPE_RECT:
  164. _squareMask.graphics.drawRect(xIndent + _photoBorder, yIndent + _photoBorder, squareWidth, squareHeight);
  165. break;
  166. }
  167. var transparentWidth:Number = squareWidth + _photoBorder*2;
  168. var transparentHeight:Number = squareHeight + _photoBorder*2;
  169. _transparentSquare = new Sprite();
  170. _transparentSquare.graphics.beginFill(_photoBorderColor, 0.5);
  171. switch (_borderType) {
  172. case BORDER_TYPE_ROUND_RECT:
  173. _transparentSquare.graphics.drawRoundRect(xIndent, yIndent, transparentWidth, transparentHeight, 15, 15);
  174. break;
  175. case BORDER_TYPE_RECT:
  176. _transparentSquare.graphics.drawRect(xIndent, yIndent, transparentWidth, transparentHeight);
  177. break;
  178. }
  179. addChild(_transparentSquare);
  180. addChild(_thumbnail);
  181. addChild(_squareMask);
  182. switch (_valign) {
  183. case VERTICAL_ALIGN_TOP:
  184. _thumbnail.y = yIndent;
  185. break;
  186. case VERTICAL_ALIGN_CENTER:
  187. _thumbnail.y = yIndent + (_photoHeight - _thumbnail.height) / 2;
  188. break;
  189. }
  190. switch (_halign) {
  191. case HORIZONTAL_ALIGN_LEFT:
  192. _thumbnail.x = xIndent;
  193. break;
  194. case HORIZONTAL_ALIGN_CENTER:
  195. _thumbnail.x = xIndent + (_photoWidth - _thumbnail.width) / 2;
  196. break;
  197. }
  198. if (_thumbnail.x > xIndent + _photoBorder) _thumbnail.x = xIndent + _photoBorder;
  199. if (_thumbnail.y > yIndent + _photoBorder) _thumbnail.y = yIndent + _photoBorder;
  200. _thumbnail.mask = _squareMask;
  201. // _frameIndex = randomNumber(1, 8);
  202. // _frameIndex = 8;
  203. if (_frameIndex > 0) {
  204. var bottomFrame:Bitmap;
  205. switch (_frameIndex) {
  206. case 1:
  207. bottomFrame = BitmapUtil.cloneImageNamed(Images.FRAME_DRAGON);
  208. var bottomFrameScale:Number = 1;
  209. var maxFrameHeight:Number = (2/3)*photoHeight;
  210. if (bottomFrame.height > maxFrameHeight) {
  211. bottomFrameScale = maxFrameHeight / bottomFrame.height;
  212. var bottomFrameMatrix:Matrix = new Matrix();
  213. bottomFrameMatrix.scale(bottomFrameScale, bottomFrameScale);
  214. bottomFrame.transform.matrix = bottomFrameMatrix;
  215. }
  216. bottomFrame.x = xIndent + photoWidth - bottomFrame.width + 27*bottomFrameScale;
  217. bottomFrame.y = yIndent + photoHeight - bottomFrame.height - 6*bottomFrameScale;
  218. break;
  219. case 2:
  220. bottomFrame = BitmapUtil.cloneImageNamed(Images.FRAME_FLOWERS);
  221. bottomFrameScale = 1;
  222. maxFrameHeight = photoHeight;
  223. if (bottomFrame.height > maxFrameHeight) {
  224. bottomFrameScale = maxFrameHeight / bottomFrame.height;
  225. bottomFrameMatrix = new Matrix();
  226. bottomFrameMatrix.scale(bottomFrameScale, bottomFrameScale);
  227. bottomFrame.transform.matrix = bottomFrameMatrix;
  228. }
  229. bottomFrame.x = xIndent + photoWidth - bottomFrame.width + 45*bottomFrameScale;
  230. bottomFrame.y = yIndent + photoHeight - bottomFrame.height + 28*bottomFrameScale;
  231. break;
  232. case 3:
  233. bottomFrame = BitmapUtil.cloneImageNamed(Images.FRAME_GRAFFITI);
  234. bottomFrameScale = 1;
  235. maxFrameHeight = (2/3)*photoHeight;
  236. if (bottomFrame.height > maxFrameHeight) {
  237. bottomFrameScale = maxFrameHeight / bottomFrame.height;
  238. bottomFrameMatrix = new Matrix();
  239. bottomFrameMatrix.scale(bottomFrameScale, bottomFrameScale);
  240. bottomFrame.transform.matrix = bottomFrameMatrix;
  241. }
  242. bottomFrame.x = xIndent + photoWidth - bottomFrame.width + 25*bottomFrameScale;
  243. bottomFrame.y = yIndent + photoHeight - bottomFrame.height + 18*bottomFrameScale;
  244. break;
  245. case 4:
  246. var topFrame:Bitmap = BitmapUtil.cloneImageNamed(Images.FRAME_HEARTS1);
  247. var topFrameScale:Number = 1;
  248. maxFrameHeight = (1/5)*photoHeight;
  249. if (topFrame.height > maxFrameHeight) {
  250. topFrameScale = maxFrameHeight / topFrame.height;
  251. var topFrameMatrix:Matrix = new Matrix();
  252. topFrameMatrix.scale(topFrameScale, topFrameScale);
  253. topFrame.transform.matrix = topFrameMatrix;
  254. }
  255. topFrame.x = xIndent - 18*topFrameScale;
  256. topFrame.y = yIndent - 5*topFrameScale;
  257. addChild(topFrame);
  258. bottomFrame = BitmapUtil.cloneImageNamed(Images.FRAME_HEARTS2);
  259. bottomFrameScale = topFrameScale;
  260. bottomFrameMatrix = new Matrix();
  261. bottomFrameMatrix.scale(bottomFrameScale, bottomFrameScale);
  262. bottomFrame.transform.matrix = bottomFrameMatrix;
  263. bottomFrame.x = xIndent + photoWidth - bottomFrame.width + 20*bottomFrameScale;
  264. bottomFrame.y = yIndent + photoHeight - bottomFrame.height + 9*bottomFrameScale;
  265. break;
  266. case 5:
  267. bottomFrame = BitmapUtil.cloneImageNamed(Images.FRAME_MUSIC);
  268. bottomFrameScale = 1;
  269. maxFrameHeight = (2/3)*photoHeight;
  270. if (bottomFrame.height > maxFrameHeight) {
  271. bottomFrameScale = maxFrameHeight / bottomFrame.height;
  272. bottomFrameMatrix = new Matrix();
  273. bottomFrameMatrix.scale(bottomFrameScale, bottomFrameScale);
  274. bottomFrame.transform.matrix = bottomFrameMatrix;
  275. }
  276. bottomFrame.x = xIndent + photoWidth - bottomFrame.width + 15*bottomFrameScale;
  277. bottomFrame.y = yIndent + photoHeight - bottomFrame.height + 19*bottomFrameScale;
  278. break;
  279. case 6:
  280. topFrame = BitmapUtil.cloneImageNamed(Images.FRAME_PINK1);
  281. topFrameScale = 1;
  282. maxFrameHeight = (1/3)*photoHeight;
  283. if (topFrame.height > maxFrameHeight) {
  284. topFrameScale = maxFrameHeight / topFrame.height;
  285. topFrameMatrix = new Matrix();
  286. topFrameMatrix.scale(topFrameScale, topFrameScale);
  287. topFrame.transform.matrix = topFrameMatrix;
  288. }
  289. topFrame.x = xIndent - 11*topFrameScale;
  290. topFrame.y = yIndent - 11*topFrameScale;
  291. addChild(topFrame);
  292. bottomFrame = BitmapUtil.cloneImageNamed(Images.FRAME_PINK2);
  293. bottomFrameScale = topFrameScale;
  294. bottomFrameMatrix = new Matrix();
  295. bottomFrameMatrix.scale(bottomFrameScale, bottomFrameScale);
  296. bottomFrame.transform.matrix = bottomFrameMatrix;
  297. bottomFrame.x = xIndent + photoWidth - bottomFrame.width + 10*bottomFrameScale;
  298. bottomFrame.y = yIndent + photoHeight - bottomFrame.height + 8*bottomFrameScale;
  299. break;
  300. case 7:
  301. topFrame = BitmapUtil.cloneImageNamed(Images.FRAME_SOM1);
  302. topFrameScale = 1;
  303. maxFrameHeight = (2/3)*photoHeight;
  304. if (topFrame.height > maxFrameHeight) {
  305. topFrameScale = maxFrameHeight / topFrame.height;
  306. topFrameMatrix = new Matrix();
  307. topFrameMatrix.scale(topFrameScale, topFrameScale);
  308. topFrame.transform.matrix = topFrameMatrix;
  309. }
  310. topFrame.x = xIndent - 40*topFrameScale;
  311. topFrame.y = yIndent + photoHeight - topFrame.height;
  312. addChild(topFrame);
  313. bottomFrame = BitmapUtil.cloneImageNamed(Images.FRAME_SOM2);
  314. bottomFrameScale = topFrameScale;
  315. bottomFrameMatrix = new Matrix();
  316. bottomFrameMatrix.scale(bottomFrameScale, bottomFrameScale);
  317. bottomFrame.transform.matrix = bottomFrameMatrix;
  318. bottomFrame.x = xIndent + photoWidth - bottomFrame.width + 68*bottomFrameScale;
  319. bottomFrame.y = yIndent - 50*bottomFrameScale;
  320. break;
  321. case 8:
  322. topFrame = BitmapUtil.cloneImageNamed(Images.FRAME_STR2);
  323. topFrameScale = 1;
  324. var maxFrameWidth:Number = (6/5)*photoWidth;
  325. if (topFrame.width > maxFrameWidth) {
  326. topFrameScale = maxFrameWidth / topFrame.width;
  327. topFrameMatrix = new Matrix();
  328. topFrameMatrix.scale(topFrameScale, topFrameScale);
  329. topFrame.transform.matrix = topFrameMatrix;
  330. }
  331. topFrame.x = xIndent + (photoWidth - topFrame.width) / 2 - 10*topFrameScale;
  332. topFrame.y = yIndent - 46*topFrameScale;
  333. addChild(topFrame);
  334. bottomFrame = BitmapUtil.cloneImageNamed(Images.FRAME_STR1);
  335. bottomFrameScale = topFrameScale;
  336. bottomFrameMatrix = new Matrix();
  337. bottomFrameMatrix.scale(bottomFrameScale, bottomFrameScale);
  338. bottomFrame.transform.matrix = bottomFrameMatrix;
  339. bottomFrame.x = xIndent + (photoWidth - bottomFrame.width) / 2 + 19*bottomFrameScale;
  340. bottomFrame.y = yIndent + photoHeight - bottomFrame.height + 6*bottomFrameScale;
  341. break;
  342. }
  343. addChild(bottomFrame);
  344. }
  345. }
  346. }
  347. public function set frameIndex(value:int):void {
  348. _frameIndex = value;
  349. update();
  350. }
  351. public function get photoWidth():int {
  352. return (_photoWidth > 0) ? _photoWidth + _photoBorder*2 : width;
  353. }
  354. public function get photoHeight():int {
  355. return (_photoHeight > 0) ? _photoHeight + _photoBorder*2 : height;
  356. }
  357. public function set photo(value:Bitmap):void {
  358. _photo = (value) ? BitmapUtil.cloneBitmap(value) : null;
  359. update();
  360. }
  361. public function get photo():Bitmap {
  362. return _photo;
  363. }
  364. private function randomNumber(low:Number=NaN, high:Number=NaN):Number
  365. {
  366. var low:Number = low;
  367. var high:Number = high;
  368. if(isNaN(low))
  369. {
  370. throw new Error("low must be defined");
  371. }
  372. if(isNaN(high))
  373. {
  374. throw new Error("high must be defined");
  375. }
  376. return Math.round(Math.random() * (high - low)) + low;
  377. }
  378. }
  379. }