PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/addictedcompany/skin/Skin.as

https://bitbucket.org/Dukobpa3/addicted-assetsfactory
ActionScript | 659 lines | 494 code | 107 blank | 58 comment | 38 complexity | 944150db50df7f904899e781f4815a4a MD5 | raw file
  1. package com.addictedcompany.skin
  2. {
  3. import adobe.utils.CustomActions;
  4. import flash.display.BitmapData;
  5. import flash.display.DisplayObject;
  6. import flash.display.Graphics;
  7. import flash.display.InteractiveObject;
  8. import flash.display.Sprite;
  9. import flash.events.Event;
  10. import flash.geom.Point;
  11. import flash.geom.Rectangle;
  12. import flash.utils.Dictionary;
  13. import org.bytearray.display.ScaleBitmap;
  14. /**
  15. * ...
  16. * @author Jewelz
  17. */
  18. public class Skin extends Sprite
  19. {
  20. //////////////////////////////////////////////////////////////////////////////////////////////////
  21. // CONSTS //
  22. //////////////////////////////////////////////////////////////////////////////////////////////////
  23. public static const ALIGN_TOP:String = "alignTop";
  24. public static const ALIGN_BOTTOM:String = "alignBottom";
  25. public static const ALIGN_LEFT:String = "alignLeft";
  26. public static const ALIGN_RIGHT:String = "alignRight";
  27. public static const ALIGN_TOP_LEFT:String = "alignTopLeft";
  28. public static const ALIGN_TOP_RIGHT:String = "alignTopRight";
  29. public static const ALIGN_BOTTOM_LEFT:String = "alignBottomLeft";
  30. public static const ALIGN_BOTTOM_RIGHT:String = "alignBottomRight";
  31. public static const ALIGN_CENTER:String = "alignCenter";
  32. public static const NO_ALIGN:String = "noAlign";
  33. //////////////////////////////////////////////////////////////////////////////////////////////////
  34. // PRIVATE/PROTECTED VARS //
  35. //////////////////////////////////////////////////////////////////////////////////////////////////
  36. private var _align:String;
  37. private var _width:int;
  38. private var _height:int;
  39. private var actualWidth:int;
  40. private var actualHeight:int;
  41. private var states:/*SkinState*/Array;
  42. private var currentStateIndex:int;
  43. private var currentState:SkinState;
  44. //////////////////////////////////////////////////////////////////////////////////////////////////
  45. /**
  46. * Контейнер для фона
  47. */
  48. protected var cont:Sprite;
  49. public function Skin(width:int, height:int, align:String = "noAlign")
  50. {
  51. _width = width;
  52. _height = height;
  53. _align = align;
  54. init();
  55. }
  56. //////////////////////////////////////////////////////////////////////////////////////////////////
  57. // PUBLIC METHODS //
  58. //////////////////////////////////////////////////////////////////////////////////////////////////
  59. /**
  60. * Устанавливает размеры скина
  61. * @param width
  62. * @param height
  63. */
  64. public function setSize(width:int, height:int):void
  65. {
  66. _width = width;
  67. _height = height;
  68. resize();
  69. }
  70. /**
  71. * Устанавливает состояние скина
  72. * @param index номер состояния
  73. */
  74. public function setState(index:int):void
  75. {
  76. if (states[index])
  77. {
  78. currentState = states[index];
  79. currentStateIndex = index;
  80. removeOldState();
  81. addNewState();
  82. resize(false);
  83. }
  84. }
  85. private function addNewState():void
  86. {
  87. var i:int;
  88. var length:int = currentState.childs.length;
  89. for (i = 0; i < length; i++)
  90. {
  91. cont.addChild(currentState.childs[i] as DisplayObject);
  92. }
  93. }
  94. private function removeOldState():void
  95. {
  96. while (cont.numChildren) cont.removeChildAt(0);
  97. }
  98. /**
  99. * Добавляет растягиваемый растр со смещениями
  100. * @param bitmapData
  101. * @param scale9grid
  102. * @param offset x - смещение слева, y - сверху, width - справа, height - снизу
  103. */
  104. public function addBitmap(bitmapData:BitmapData, scale9grid:Rectangle, offset:Rectangle, state:int = 0):void
  105. {
  106. var sb:ScaleBitmap = new ScaleBitmap(bitmapData);
  107. sb.scale9Grid = scale9grid;
  108. if (state == 0) cont.addChild(sb);
  109. var s:SkinState = states[state];
  110. if (!s)
  111. {
  112. s = new SkinState();
  113. states[state] = s;
  114. if (state == 0) currentState = s;
  115. }
  116. s.childs.push(sb);
  117. s.bitmaps.push(sb);
  118. s.offsets[sb] = offset;
  119. }
  120. /**
  121. * Добавляет tile для отрисовки методом bitmapDraw
  122. * @param bitmapData
  123. * @param offset
  124. */
  125. public function addDrawBitmap(bitmapData:BitmapData, offset:Rectangle, state:int = 0):void
  126. {
  127. var c:Sprite = new Sprite();
  128. if (state == 0) cont.addChild(c);
  129. var s:SkinState = states[state];
  130. if (!s)
  131. {
  132. s = new SkinState();
  133. states[state] = s;
  134. if (state == 0) currentState = s;
  135. }
  136. s.childs.push(c);
  137. s.bitmapDraws.push(c);
  138. s.offsets[c] = offset;
  139. s.bitmapForDraw[c] = bitmapData;
  140. }
  141. //////////////////////////////////////////////////////////////////////////////////////////////////
  142. // PRIVATE/PROTECTED METHODS //
  143. //////////////////////////////////////////////////////////////////////////////////////////////////
  144. private function init():void
  145. {
  146. if (stage) onAdded(null);
  147. else addEventListener(Event.ADDED_TO_STAGE, onAdded);
  148. cont = new Sprite();
  149. addChild(cont);
  150. states = [];
  151. currentStateIndex = 0;
  152. }
  153. protected function resize(dispatch:Boolean = true):void
  154. {
  155. if (stage)
  156. {
  157. switch (_align)
  158. {
  159. case NO_ALIGN:
  160. alignNo();
  161. break;
  162. case ALIGN_CENTER:
  163. alignCenter()
  164. break;
  165. case ALIGN_LEFT:
  166. alignLeft();
  167. break;
  168. case ALIGN_RIGHT:
  169. alignRight();
  170. break;
  171. case ALIGN_BOTTOM:
  172. alignBottom();
  173. break;
  174. case ALIGN_TOP:
  175. alignTop();
  176. break;
  177. case ALIGN_TOP_LEFT:
  178. alignTopLeft();
  179. break;
  180. case ALIGN_TOP_RIGHT:
  181. alignTopRight();
  182. break;
  183. case ALIGN_BOTTOM_LEFT:
  184. alignBottomLeft();
  185. break;
  186. case ALIGN_BOTTOM_RIGHT:
  187. alignBottomRight();
  188. break;
  189. }
  190. if (dispatch)
  191. {
  192. dispatchEvent(new Event(Event.RESIZE));
  193. }
  194. }
  195. }
  196. private function alignNo():void
  197. {
  198. var offset:Rectangle;
  199. actualWidth = _width;
  200. actualHeight = _height;
  201. var i:int;
  202. var length:int = bitmaps.length;
  203. for (i = 0; i < length; i++)
  204. {
  205. var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
  206. offset = offsets[sb];
  207. sb.x = offset.x;
  208. sb.y = offset.y;
  209. sb.setSize(_width - offset.x - offset.width, _height - offset.height - offset.y);
  210. }
  211. length = currentState.bitmapDraws.length;
  212. for (i = 0; i < length; i++)
  213. {
  214. var bdCont:Sprite = bitmapDraws[i] as Sprite;
  215. var bd:BitmapData = bitmapForDraw[bdCont];
  216. offset = offsets[bdCont];
  217. var g:Graphics = bdCont.graphics;
  218. g.clear();
  219. g.beginBitmapFill(bd);
  220. g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, _height - offset.height - offset.y);
  221. g.endFill();
  222. }
  223. }
  224. private function alignCenter():void
  225. {
  226. this.x = stage.stageWidth / 2 - _width / 2;
  227. this.y = stage.stageHeight / 2 - _height / 2;
  228. actualWidth = _width;
  229. actualHeight = _height;
  230. var offset:Rectangle;
  231. var i:int;
  232. var length:int = bitmaps.length;
  233. for (i = 0; i < length; i++)
  234. {
  235. var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
  236. offset = offsets[sb];
  237. sb.x = offset.x;
  238. sb.y = offset.y;
  239. sb.setSize(_width - offset.x - offset.width, _height - offset.height - offset.y);
  240. }
  241. length = bitmapDraws.length;
  242. for (i = 0; i < length; i++)
  243. {
  244. var bdCont:Sprite = bitmapDraws[i] as Sprite;
  245. var bd:BitmapData = bitmapForDraw[bdCont];
  246. offset = offsets[bdCont];
  247. var g:Graphics = bdCont.graphics;
  248. g.clear();
  249. g.beginBitmapFill(bd);
  250. g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, _height - offset.height - offset.y);
  251. g.endFill();
  252. }
  253. }
  254. private function alignLeft():void
  255. {
  256. this.x = 0;
  257. this.y = 0;
  258. actualWidth = _width;
  259. actualHeight = stage.stageHeight;
  260. var offset:Rectangle;
  261. var i:int;
  262. var length:int = bitmaps.length;
  263. for (i = 0; i < length; i++)
  264. {
  265. var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
  266. offset = offsets[sb];
  267. sb.x = offset.x;
  268. sb.y = offset.y;
  269. sb.setSize(_width - offset.x - offset.width, stage.stageHeight - offset.height - offset.y);
  270. }
  271. length = bitmapDraws.length;
  272. for (i = 0; i < length; i++)
  273. {
  274. var bdCont:Sprite = bitmapDraws[i] as Sprite;
  275. var bd:BitmapData = bitmapForDraw[bdCont];
  276. offset = offsets[bdCont];
  277. var g:Graphics = bdCont.graphics;
  278. g.clear();
  279. g.beginBitmapFill(bd);
  280. g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, stage.stageHeight - offset.height - offset.y);
  281. g.endFill();
  282. }
  283. }
  284. private function alignRight():void
  285. {
  286. this.x = stage.stageWidth - _width;
  287. this.y = 0;
  288. actualWidth = _width;
  289. actualHeight = stage.stageHeight;
  290. var offset:Rectangle;
  291. var i:int;
  292. var length:int = bitmaps.length;
  293. for (i = 0; i < length; i++)
  294. {
  295. var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
  296. offset = offsets[sb];
  297. sb.x = offset.x;
  298. sb.y = offset.y;
  299. sb.setSize(_width - offset.x - offset.width, stage.stageHeight - offset.height - offset.y);
  300. }
  301. length = bitmapDraws.length;
  302. for (i = 0; i < length; i++)
  303. {
  304. var bdCont:Sprite = bitmapDraws[i] as Sprite;
  305. var bd:BitmapData = bitmapForDraw[bdCont];
  306. offset = offsets[bdCont];
  307. var g:Graphics = bdCont.graphics;
  308. g.clear();
  309. g.beginBitmapFill(bd);
  310. g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, stage.stageHeight - offset.height - offset.y);
  311. g.endFill();
  312. }
  313. }
  314. private function alignBottom():void
  315. {
  316. this.x = 0;
  317. this.y = stage.stageHeight - _height;
  318. actualWidth = stage.stageWidth;
  319. actualHeight = _height;
  320. var offset:Rectangle;
  321. var i:int;
  322. var length:int = bitmaps.length;
  323. for (i = 0; i < length; i++)
  324. {
  325. var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
  326. offset = offsets[sb];
  327. sb.x = offset.x;
  328. sb.y = offset.y;
  329. sb.setSize(stage.stageWidth - offset.x - offset.width, _height - offset.height - offset.y);
  330. }
  331. length = bitmapDraws.length;
  332. for (i = 0; i < length; i++)
  333. {
  334. var bdCont:Sprite = bitmapDraws[i] as Sprite;
  335. var bd:BitmapData = bitmapForDraw[bdCont];
  336. offset = offsets[bdCont];
  337. var g:Graphics = bdCont.graphics;
  338. g.clear();
  339. g.beginBitmapFill(bd);
  340. g.drawRect(offset.x, offset.y, stage.stageWidth - offset.x - offset.width, _height - offset.height - offset.y);
  341. g.endFill();
  342. }
  343. }
  344. private function alignTop():void
  345. {
  346. this.x = 0;
  347. this.y = 0;
  348. actualWidth = stage.stageWidth;
  349. actualHeight = _height;
  350. var offset:Rectangle;
  351. var i:int;
  352. var length:int = bitmaps.length;
  353. for (i = 0; i < length; i++)
  354. {
  355. var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
  356. offset = offsets[sb];
  357. sb.x = offset.x;
  358. sb.y = offset.y;
  359. sb.setSize(stage.stageWidth - offset.x - offset.width, _height - offset.height - offset.y);
  360. }
  361. length = bitmapDraws.length;
  362. for (i = 0; i < length; i++)
  363. {
  364. var bdCont:Sprite = bitmapDraws[i] as Sprite;
  365. var bd:BitmapData = bitmapForDraw[bdCont];
  366. offset = offsets[bdCont];
  367. var g:Graphics = bdCont.graphics;
  368. g.clear();
  369. g.beginBitmapFill(bd);
  370. g.drawRect(offset.x, offset.y, stage.stageWidth - offset.x - offset.width, _height - offset.height - offset.y);
  371. g.endFill();
  372. }
  373. }
  374. private function alignBottomLeft():void
  375. {
  376. this.x = 0;
  377. this.y = stage.stageHeight - _height;
  378. actualWidth = _width;
  379. actualHeight = _height;
  380. var offset:Rectangle;
  381. var i:int;
  382. var length:int = bitmaps.length;
  383. for (i = 0; i < length; i++)
  384. {
  385. var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
  386. offset = offsets[sb];
  387. sb.x = offset.x;
  388. sb.y = offset.y;
  389. sb.setSize(_width - offset.x - offset.width, _height - offset.height - offset.y);
  390. }
  391. length = bitmapDraws.length;
  392. for (i = 0; i < length; i++)
  393. {
  394. var bdCont:Sprite = bitmapDraws[i] as Sprite;
  395. var bd:BitmapData = bitmapForDraw[bdCont];
  396. offset = offsets[bdCont];
  397. var g:Graphics = bdCont.graphics;
  398. g.clear();
  399. g.beginBitmapFill(bd);
  400. g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, _height - offset.height - offset.y);
  401. g.endFill();
  402. }
  403. }
  404. private function alignBottomRight():void
  405. {
  406. this.x = stage.stageWidth - _width;
  407. this.y = stage.stageHeight - _height;
  408. actualWidth = _width;
  409. actualHeight = _height;
  410. var offset:Rectangle;
  411. var i:int;
  412. var length:int = bitmaps.length;
  413. for (i = 0; i < length; i++)
  414. {
  415. var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
  416. offset = offsets[sb];
  417. sb.x = offset.x;
  418. sb.y = offset.y;
  419. sb.setSize(_width - offset.x - offset.width, _height - offset.height - offset.y);
  420. }
  421. length = bitmapDraws.length;
  422. for (i = 0; i < length; i++)
  423. {
  424. var bdCont:Sprite = bitmapDraws[i] as Sprite;
  425. var bd:BitmapData = bitmapForDraw[bdCont];
  426. offset = offsets[bdCont];
  427. var g:Graphics = bdCont.graphics;
  428. g.clear();
  429. g.beginBitmapFill(bd);
  430. g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, _height - offset.height - offset.y);
  431. g.endFill();
  432. }
  433. }
  434. private function alignTopLeft():void
  435. {
  436. this.x = 0;
  437. this.y = 0;
  438. actualWidth = _width;
  439. actualHeight = _height;
  440. var offset:Rectangle;
  441. var i:int;
  442. var length:int = bitmaps.length;
  443. for (i = 0; i < length; i++)
  444. {
  445. var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
  446. offset = offsets[sb];
  447. sb.x = offset.x;
  448. sb.y = offset.y;
  449. sb.setSize(_width - offset.x - offset.width, _height - offset.height - offset.y);
  450. }
  451. length = bitmapDraws.length;
  452. for (i = 0; i < length; i++)
  453. {
  454. var bdCont:Sprite = bitmapDraws[i] as Sprite;
  455. var bd:BitmapData = bitmapForDraw[bdCont];
  456. offset = offsets[bdCont];
  457. var g:Graphics = bdCont.graphics;
  458. g.clear();
  459. g.beginBitmapFill(bd);
  460. g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, _height - offset.height - offset.y);
  461. g.endFill();
  462. }
  463. }
  464. private function alignTopRight():void
  465. {
  466. this.x = stage.stageWidth - _width;
  467. this.y = 0;
  468. actualWidth = _width;
  469. actualHeight = _height;
  470. var offset:Rectangle;
  471. var i:int;
  472. var length:int = bitmaps.length;
  473. for (i = 0; i < length; i++)
  474. {
  475. var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
  476. offset = offsets[sb];
  477. sb.x = offset.x;
  478. sb.y = offset.y;
  479. sb.setSize(_width - offset.x - offset.width, _height - offset.height - offset.y);
  480. }
  481. length = bitmapDraws.length;
  482. for (i = 0; i < length; i++)
  483. {
  484. var bdCont:Sprite = bitmapDraws[i] as Sprite;
  485. var bd:BitmapData = bitmapForDraw[bdCont];
  486. offset = offsets[bdCont];
  487. var g:Graphics = bdCont.graphics;
  488. g.clear();
  489. g.beginBitmapFill(bd);
  490. g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, _height - offset.height - offset.y);
  491. g.endFill();
  492. }
  493. }
  494. //////////////////////////////////////////////////////////////////////////////////////////////////
  495. // HANDLERS //
  496. //////////////////////////////////////////////////////////////////////////////////////////////////
  497. private function onAdded(e:Event):void
  498. {
  499. removeEventListener(Event.ADDED_TO_STAGE, onAdded);
  500. stage.addEventListener(Event.RESIZE, onResize);
  501. resize();
  502. }
  503. private function onResize(e:Event):void
  504. {
  505. resize();
  506. }
  507. //////////////////////////////////////////////////////////////////////////////////////////////////
  508. // GETTERS/SETTERS //
  509. //////////////////////////////////////////////////////////////////////////////////////////////////
  510. public function get stateIndex():int { return currentStateIndex; }
  511. public function get align():String { return _align; }
  512. public function set align(value:String):void { _align = value; }
  513. override public function get width():Number { return actualWidth; }
  514. override public function get height():Number { return actualHeight; }
  515. private function get bitmapDraws():/*Sprite*/Array { return currentState.bitmapDraws }
  516. private function get bitmaps():/*ScaleBitmap*/Array { return currentState.bitmaps }
  517. private function get offsets():Dictionary { return currentState.offsets }
  518. private function get bitmapForDraw():Dictionary { return currentState.bitmapForDraw }
  519. }
  520. }
  521. import flash.display.BitmapData;
  522. import flash.utils.Dictionary;
  523. import org.bytearray.display.ScaleBitmap;
  524. class SkinState
  525. {
  526. /**
  527. * Массив контейнеров для bitmapDraw
  528. */
  529. public var bitmapDraws:/*Sprite*/Array;
  530. /**
  531. * Массив scaleBitmap
  532. */
  533. public var bitmaps:/*ScaleBitmap*/Array;
  534. /**
  535. * Словарь смещений (ключ - контейнер, значение - Rectangle)
  536. */
  537. public var offsets:Dictionary;
  538. /**
  539. * Словарь картинок для отрисовки методом bitmapDraw (ключ - контейнер, значение - BitmapData)
  540. */
  541. public var bitmapForDraw:Dictionary;
  542. public var childs:Array;
  543. public var currentChildIndex:int;
  544. public function SkinState()
  545. {
  546. childs = [];
  547. bitmaps = [];
  548. bitmapDraws = [];
  549. offsets = new Dictionary();
  550. bitmapForDraw = new Dictionary();
  551. }
  552. }