PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/ui/source/temple/ui/layout/liquid/LiquidSprite.as

http://templelibrary.googlecode.com/
ActionScript | 622 lines | 326 code | 68 blank | 228 comment | 52 complexity | 541cfeba4c2f05becd4df8e1943db862 MD5 | raw file
  1. /*
  2. * Temple Library for ActionScript 3.0
  3. * Copyright Š MediaMonks B.V.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by MediaMonks B.V.
  16. * 4. Neither the name of MediaMonks B.V. nor the
  17. * names of its contributors may be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY MEDIAMONKS B.V. ''AS IS'' AND ANY
  21. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL MEDIAMONKS B.V. BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. *
  32. * Note: This license does not apply to 3rd party classes inside the Temple
  33. * repository with their own license!
  34. */
  35. package temple.ui.layout.liquid
  36. {
  37. import temple.core.display.CoreSprite;
  38. import temple.utils.FrameDelay;
  39. import flash.display.DisplayObject;
  40. import flash.geom.Point;
  41. /**
  42. * The LiquidSprite extends the CoreMovieClip but has already LiquidBehavior implemented.
  43. * All liquid properties are also available on the LiquidSprite.
  44. *
  45. * <p>If the LiquidSprite is set as component, all liquid properties can be set in the Component Inspector in the Flash IDE</p>
  46. *
  47. * @see temple.ui.layout.liquid.LiquidBehavior
  48. *
  49. * @includeExample LiquidExample.as
  50. *
  51. * @author Thijs Broerse
  52. */
  53. public class LiquidSprite extends CoreSprite implements ILiquidObject
  54. {
  55. private var _liquidBehavior:LiquidBehavior;
  56. private var _debug:Boolean;
  57. public function LiquidSprite(initObject:Object = null, relatedObject:ILiquidObject = null)
  58. {
  59. super();
  60. this._liquidBehavior = new LiquidBehavior(this, initObject, relatedObject);
  61. // Call init after a framedelay, so all inspetables are already set
  62. if (!initObject) new FrameDelay(this.initLiquid);
  63. }
  64. /**
  65. * @inheritDoc
  66. */
  67. public function get left():Number
  68. {
  69. return this._liquidBehavior ? this._liquidBehavior.left : NaN;
  70. }
  71. /**
  72. * @inheritDoc
  73. */
  74. public function set left(value:Number):void
  75. {
  76. this.liquidBehavior.left = value;
  77. }
  78. /**
  79. * @private
  80. */
  81. [Inspectable(name="Left", type="String")]
  82. public function set inspectableLeft(value:String):void
  83. {
  84. if (value != '' && !isNaN(Number(value)))
  85. {
  86. this.left = Number(value);
  87. }
  88. }
  89. /**
  90. * @inheritDoc
  91. */
  92. public function get right():Number
  93. {
  94. return this._liquidBehavior ? this._liquidBehavior.right : NaN;
  95. }
  96. /**
  97. * @inheritDoc
  98. */
  99. public function set right(value:Number):void
  100. {
  101. this.liquidBehavior.right = value;
  102. }
  103. /**
  104. * @private
  105. */
  106. [Inspectable(name="Right", type="String")]
  107. public function set inspectableRight(value:String):void
  108. {
  109. if (value != '' && !isNaN(Number(value)))
  110. {
  111. this.right = Number(value);
  112. }
  113. }
  114. /**
  115. * @inheritDoc
  116. */
  117. public function get horizontalCenter():Number
  118. {
  119. return this._liquidBehavior ? this._liquidBehavior.horizontalCenter : NaN;
  120. }
  121. /**
  122. * @inheritDoc
  123. */
  124. public function set horizontalCenter(value:Number):void
  125. {
  126. this.liquidBehavior.horizontalCenter = value;
  127. }
  128. /**
  129. * @private
  130. */
  131. [Inspectable(name="Horizontal center", type="String")]
  132. public function set inspectableHorizontalCenter(value:String):void
  133. {
  134. if (value != '' && !isNaN(Number(value)))
  135. {
  136. this.horizontalCenter = Number(value);
  137. }
  138. }
  139. /**
  140. * @inheritDoc
  141. */
  142. public function get relativeX():Number
  143. {
  144. return this.liquidBehavior.relativeX;
  145. }
  146. /**
  147. * @inheritDoc
  148. */
  149. public function set relativeX(value:Number):void
  150. {
  151. this.liquidBehavior.relativeX = value;
  152. }
  153. /**
  154. * @private
  155. */
  156. [Inspectable(name="Relative X", type="String")]
  157. public function set inspectableRelativeX(value:String):void
  158. {
  159. if (value != '' && !isNaN(Number(value)))
  160. {
  161. this.relativeX = Number(value);
  162. }
  163. }
  164. /**
  165. * @inheritDoc
  166. */
  167. public function get minimalWidth():Number
  168. {
  169. return this._liquidBehavior ? this._liquidBehavior.minimalWidth : NaN;
  170. }
  171. /**
  172. * @inheritDoc
  173. */
  174. public function set minimalWidth(value:Number):void
  175. {
  176. if (this.liquidBehavior) this.liquidBehavior.minimalWidth = value;
  177. }
  178. /**
  179. * @private
  180. */
  181. [Inspectable(name="Minimal width", type="String")]
  182. public function set inspectableMinimalWidth(value:String):void
  183. {
  184. if (value != '' && !isNaN(Number(value)))
  185. {
  186. this.minimalWidth = Number(value);
  187. }
  188. }
  189. /**
  190. * @inheritDoc
  191. */
  192. public function get relativeWidth():Number
  193. {
  194. return this._liquidBehavior ? this._liquidBehavior.relativeWidth : NaN;
  195. }
  196. /**
  197. * @inheritDoc
  198. */
  199. public function set relativeWidth(value:Number):void
  200. {
  201. if (this.liquidBehavior) this.liquidBehavior.relativeWidth = value;
  202. }
  203. /**
  204. * @private
  205. */
  206. [Inspectable(name="Relative Width", type="String")]
  207. public function set inspectableRelativeWidth(value:String):void
  208. {
  209. if (value != '' && !isNaN(Number(value)))
  210. {
  211. this.liquidBehavior.relativeWidth = Number(value);
  212. }
  213. else if (value.indexOf('%'))
  214. {
  215. value = value.replace('%', '');
  216. if (value != '' && !isNaN(Number(value)))
  217. {
  218. this.liquidBehavior.relativeWidth = Number(value) * 0.01;
  219. }
  220. }
  221. }
  222. /**
  223. * @inheritDoc
  224. */
  225. public function get absoluteWidth():Number
  226. {
  227. return this._liquidBehavior.absoluteWidth;
  228. }
  229. /**
  230. * @inheritDoc
  231. */
  232. public function set absoluteWidth(value:Number):void
  233. {
  234. this._liquidBehavior.absoluteWidth = value;
  235. }
  236. /**
  237. * @inheritDoc
  238. */
  239. public function get top():Number
  240. {
  241. return this._liquidBehavior ? this._liquidBehavior.top : NaN;
  242. }
  243. /**
  244. * @inheritDoc
  245. */
  246. public function set top(value:Number):void
  247. {
  248. this.liquidBehavior.top = value;
  249. }
  250. /**
  251. * @private
  252. */
  253. [Inspectable(name="Top", type="String")]
  254. public function set inspectableTop(value:String):void
  255. {
  256. if (value != '' && !isNaN(Number(value)))
  257. {
  258. this.top = Number(value);
  259. }
  260. }
  261. /**
  262. * @inheritDoc
  263. */
  264. public function get bottom():Number
  265. {
  266. return this._liquidBehavior ? this._liquidBehavior.bottom : NaN;
  267. }
  268. /**
  269. * @inheritDoc
  270. */
  271. public function set bottom(value:Number):void
  272. {
  273. this.liquidBehavior.bottom = value;
  274. }
  275. /**
  276. * @private
  277. */
  278. [Inspectable(name="Bottom", type="String")]
  279. public function set inspectableBottom(value:String):void
  280. {
  281. if (value != '' && !isNaN(Number(value)))
  282. {
  283. this.bottom = Number(value);
  284. }
  285. }
  286. /**
  287. * @inheritDoc
  288. */
  289. public function get verticalCenter():Number
  290. {
  291. return this._liquidBehavior ? this._liquidBehavior.verticalCenter : NaN;
  292. }
  293. /**
  294. * @inheritDoc
  295. */
  296. public function set verticalCenter(value:Number):void
  297. {
  298. this.liquidBehavior.verticalCenter = value;
  299. }
  300. /**
  301. * @private
  302. */
  303. [Inspectable(name="Vertical center", type="String")]
  304. public function set inspectableVerticalCenter(value:String):void
  305. {
  306. if (value != '' && !isNaN(Number(value)))
  307. {
  308. this.verticalCenter = Number(value);
  309. }
  310. }
  311. /**
  312. * @inheritDoc
  313. */
  314. public function get relativeY():Number
  315. {
  316. return this.liquidBehavior.relativeY;
  317. }
  318. /**
  319. * @inheritDoc
  320. */
  321. public function set relativeY(value:Number):void
  322. {
  323. this.liquidBehavior.relativeY = value;
  324. }
  325. /**
  326. * @private
  327. */
  328. [Inspectable(name="Relative Y", type="String")]
  329. public function set inspectableRelativeY(value:String):void
  330. {
  331. if (value != '' && !isNaN(Number(value)))
  332. {
  333. this.relativeY = Number(value);
  334. }
  335. }
  336. /**
  337. * @inheritDoc
  338. */
  339. public function get minimalHeight():Number
  340. {
  341. return this._liquidBehavior ? this._liquidBehavior.minimalHeight : NaN;
  342. }
  343. /**
  344. * @inheritDoc
  345. */
  346. public function set minimalHeight(value:Number):void
  347. {
  348. if (this.liquidBehavior) this.liquidBehavior.minimalHeight = value;
  349. }
  350. /**
  351. * @inheritDoc
  352. */
  353. public function get relativeHeight():Number
  354. {
  355. return this._liquidBehavior ? this._liquidBehavior.relativeHeight : NaN;
  356. }
  357. /**
  358. * @inheritDoc
  359. */
  360. public function set relativeHeight(value:Number):void
  361. {
  362. if (this.liquidBehavior) this.liquidBehavior.relativeHeight = value;
  363. }
  364. /**
  365. * @inheritDoc
  366. */
  367. public function get absoluteHeight():Number
  368. {
  369. return this._liquidBehavior.absoluteHeight;
  370. }
  371. /**
  372. * @inheritDoc
  373. */
  374. public function set absoluteHeight(value:Number):void
  375. {
  376. this._liquidBehavior.absoluteHeight = value;
  377. }
  378. /**
  379. * @private
  380. */
  381. [Inspectable(name="Relative Height", type="String")]
  382. public function set inspectableRelativeHeight(value:String):void
  383. {
  384. if (value != '' && !isNaN(Number(value)))
  385. {
  386. this.liquidBehavior.relativeHeight = Number(value);
  387. }
  388. else if (value.indexOf('%'))
  389. {
  390. value = value.replace('%', '');
  391. if (value != '' && !isNaN(Number(value)))
  392. {
  393. this.liquidBehavior.relativeHeight = Number(value) * 0.01;
  394. }
  395. }
  396. }
  397. /**
  398. * @inheritDoc
  399. */
  400. public function get relatedObject():ILiquidRelatedObject
  401. {
  402. return this._liquidBehavior ? this._liquidBehavior.relatedObject : null;
  403. }
  404. /**
  405. * @inheritDoc
  406. */
  407. public function set relatedObject(value:ILiquidRelatedObject):void
  408. {
  409. this.liquidBehavior.relatedObject = value;
  410. }
  411. /**
  412. * @inheritDoc
  413. */
  414. public function update():void
  415. {
  416. if (this._liquidBehavior) this._liquidBehavior.update();
  417. }
  418. /**
  419. * @inheritDoc
  420. */
  421. public function isLiquid():Boolean
  422. {
  423. return this._liquidBehavior ? this._liquidBehavior.isLiquid() : false;
  424. }
  425. /**
  426. * @inheritDoc
  427. */
  428. public function get resetRelatedScale():Boolean
  429. {
  430. return this._liquidBehavior ? this._liquidBehavior.resetRelatedScale : false;
  431. }
  432. /**
  433. * @inheritDoc
  434. */
  435. public function set resetRelatedScale(value:Boolean):void
  436. {
  437. if (this._liquidBehavior) this._liquidBehavior.resetRelatedScale = value;
  438. }
  439. /**
  440. * @inheritDoc
  441. */
  442. public function get offset():Point
  443. {
  444. return this._liquidBehavior ? this._liquidBehavior.offset : null;
  445. }
  446. /**
  447. * If set to true all values will be rounded to pixels
  448. */
  449. public function get snapToPixels():Boolean
  450. {
  451. return this._liquidBehavior ? this._liquidBehavior.snapToPixels : false;
  452. }
  453. /**
  454. * @private
  455. */
  456. [Inspectable(name="Snap to pixels", type="Boolean", defaultValue="true")]
  457. public function set snapToPixels(value:Boolean):void
  458. {
  459. if (this._liquidBehavior) this._liquidBehavior.snapToPixels = value;
  460. }
  461. /**
  462. * @inheritDoc
  463. */
  464. public function get keepAspectRatio():Boolean
  465. {
  466. return this._liquidBehavior ? this._liquidBehavior.keepAspectRatio : false;
  467. }
  468. /**
  469. * @inheritDoc
  470. */
  471. [Inspectable(name="Keep aspect ratio", type="Boolean", defaultValue="false")]
  472. public function set keepAspectRatio(value:Boolean):void
  473. {
  474. this.liquidBehavior.keepAspectRatio = value;
  475. }
  476. /**
  477. * @inheritDoc
  478. */
  479. public function get adjustRelated():Boolean
  480. {
  481. return this._liquidBehavior ? this._liquidBehavior.adjustRelated : true;
  482. }
  483. /**
  484. * @inheritDoc
  485. */
  486. public function set adjustRelated(value:Boolean):void
  487. {
  488. this.liquidBehavior.adjustRelated = value;
  489. }
  490. /**
  491. * @inheritDoc
  492. */
  493. public function get displayObject():DisplayObject
  494. {
  495. return this;
  496. }
  497. /**
  498. * Indicates if the LiquidBehavior is enabled
  499. */
  500. public function get liquidEnabled():Boolean
  501. {
  502. return this._liquidBehavior ? this._liquidBehavior.enabled : false;
  503. }
  504. /**
  505. * @private
  506. */
  507. [Inspectable(name="Liquid enabled", type="Boolean", defaultValue="true")]
  508. public function set liquidEnabled(value:Boolean):void
  509. {
  510. this.liquidBehavior.enabled = value;
  511. }
  512. /**
  513. * Returns a reference of the LiquidBehavior.
  514. * The LiquidBehavior is only created when it's actually needed.
  515. */
  516. public function get liquidBehavior():LiquidBehavior
  517. {
  518. return this._liquidBehavior;
  519. }
  520. /**
  521. * @inheritDoc
  522. */
  523. public function get debug():Boolean
  524. {
  525. return this._debug;
  526. }
  527. /**
  528. * @inheritDoc
  529. */
  530. [Inspectable(name="Debug", type="Boolean", defaultValue="false")]
  531. public function set debug(value:Boolean):void
  532. {
  533. this.liquidBehavior.debug = this._debug = value;
  534. }
  535. /**
  536. * Initializes the liquid behavior
  537. */
  538. protected function initLiquid():void
  539. {
  540. if (this.isLiquid())
  541. {
  542. this._liquidBehavior.init();
  543. }
  544. }
  545. /**
  546. * @inheritDoc
  547. */
  548. override public function destruct():void
  549. {
  550. this._liquidBehavior = null;
  551. super.destruct();
  552. }
  553. }
  554. }