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

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

http://templelibrary.googlecode.com/
ActionScript | 279 lines | 136 code | 32 blank | 111 comment | 13 complexity | 58f113c43b2fc4cc59a0c462415b3923 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.errors.TempleArgumentError;
  38. import temple.core.errors.TempleError;
  39. import temple.core.errors.throwError;
  40. import temple.core.events.CoreEventDispatcher;
  41. import temple.utils.FrameDelay;
  42. import flash.display.DisplayObject;
  43. import flash.display.Stage;
  44. import flash.display.StageAlign;
  45. import flash.display.StageScaleMode;
  46. import flash.events.Event;
  47. import flash.geom.Point;
  48. /**
  49. * Singleton class for wrapping the Stage as a LiquidObject for aligning other LiquidObject to the stage.
  50. * The LiquidStage is not actually a Stage, but has the Stage (composition).
  51. *
  52. * <p>Note: The LiquidStage automatically sets the stageScaleMode to 'noScale' and the stageAlign to 'topLeft'.</p>
  53. *
  54. * @includeExample LiquidExample.as
  55. *
  56. * @author Thijs Broerse
  57. */
  58. public final class LiquidStage extends CoreEventDispatcher implements ILiquidRelatedObject
  59. {
  60. private static var _originalWidth:Number;
  61. private static var _originalHeight:Number;
  62. private static var _instance:LiquidStage;
  63. private var _minimalWidth:Number;
  64. private var _minimalHeight:Number;
  65. private var _resizeDelay:FrameDelay;
  66. private var _offset:Point = new Point(0,0);
  67. /**
  68. * Returns the instance of the LiquidStage
  69. */
  70. public static function getInstance():LiquidStage
  71. {
  72. return LiquidStage._instance;
  73. }
  74. /**
  75. * The original width of the stage. Only needed if you want to use relative positions
  76. */
  77. public static function get originalWidth():Number
  78. {
  79. return LiquidStage._originalWidth;
  80. }
  81. /**
  82. * @private
  83. */
  84. public static function set originalWidth(value:Number):void
  85. {
  86. if (isNaN(value)) throwError(new TempleArgumentError(LiquidStage, "originalWidth can not be set to NaN"));
  87. LiquidStage._originalWidth = value;
  88. }
  89. /**
  90. * The original height of the stage. Only needed if you want to use relative positions
  91. */
  92. public static function get originalHeight():Number
  93. {
  94. return LiquidStage._originalHeight;
  95. }
  96. /**
  97. * @private
  98. */
  99. public static function set originalHeight(value:Number):void
  100. {
  101. if (isNaN(value)) throwError(new TempleArgumentError(LiquidStage, "originalHeight can not be set to NaN"));
  102. LiquidStage._originalHeight = value;
  103. }
  104. private var _stage:Stage;
  105. /**
  106. * Don't call constructor directly, use 'LiquidStage.getInstance()'
  107. */
  108. public function LiquidStage(stage:Stage)
  109. {
  110. if (_instance)
  111. {
  112. throwError(new TempleError(this, "instance already set, use LiquidStage.getInstance()"));
  113. }
  114. if (stage == null) throwError(new TempleArgumentError(this, "Stage can not be null"));
  115. _instance = this;
  116. this._stage = stage;
  117. this._stage.addEventListener(Event.RESIZE, this.handleStageResize, false, -1);
  118. if (this._stage.scaleMode != StageScaleMode.NO_SCALE)
  119. {
  120. this._stage.scaleMode = StageScaleMode.NO_SCALE;
  121. //this.logWarn("LiquidStage changed the StageScaleMode");
  122. }
  123. if (this._stage.align != StageAlign.TOP_LEFT)
  124. {
  125. this._stage.align = StageAlign.TOP_LEFT;
  126. //this.logWarn("LiquidStage changed the StageAlign");
  127. }
  128. new FrameDelay(this.dispatchEvent, 1, [new Event(Event.RESIZE)]);
  129. }
  130. /**
  131. * Returns the stageWidth or minimalWidth
  132. */
  133. public function get width():Number
  134. {
  135. return isNaN(this._minimalWidth) || this._stage.stageWidth > this._minimalWidth ? this._stage.stageWidth : this._minimalWidth;
  136. }
  137. /**
  138. * @inheritDoc
  139. *
  140. * Always 1 on the stage
  141. */
  142. public function get scaleX():Number
  143. {
  144. return 1;
  145. }
  146. /**
  147. * @inheritDoc
  148. */
  149. public function get minimalWidth():Number
  150. {
  151. return this._minimalWidth;
  152. }
  153. /**
  154. * @inheritDoc
  155. */
  156. public function set minimalWidth(value:Number):void
  157. {
  158. this._minimalWidth = value;
  159. }
  160. /**
  161. * Returns the stageHeight or minimalHeight
  162. */
  163. public function get height():Number
  164. {
  165. return isNaN(this._minimalHeight) || this._stage.stageHeight > this._minimalHeight ? this._stage.stageHeight : this._minimalHeight;
  166. }
  167. /**
  168. * @inheritDoc
  169. */
  170. public function get minimalHeight():Number
  171. {
  172. return this._minimalHeight;
  173. }
  174. /**
  175. * @inheritDoc
  176. */
  177. public function set minimalHeight(value:Number):void
  178. {
  179. this._minimalHeight = value;
  180. }
  181. /**
  182. * @inheritDoc
  183. *
  184. * Always 1 on the stage
  185. */
  186. public function get scaleY():Number
  187. {
  188. return 1;
  189. }
  190. /**
  191. * @inheritDoc
  192. */
  193. public function get displayObject():DisplayObject
  194. {
  195. return this._stage;
  196. }
  197. /**
  198. * @inheritDoc
  199. *
  200. * Always null on stage
  201. */
  202. public function get relatedObject():ILiquidRelatedObject
  203. {
  204. return null;
  205. }
  206. /**
  207. * @inheritDoc
  208. *
  209. * Always true on stage
  210. */
  211. public function get resetRelatedScale():Boolean
  212. {
  213. return true;
  214. }
  215. /**
  216. * @inheritDoc
  217. */
  218. public function get offset():Point
  219. {
  220. return this._offset;
  221. }
  222. private function handleStageResize(event:Event):void
  223. {
  224. this.dispatchEvent(event.clone());
  225. if (this._resizeDelay) this._resizeDelay.destruct();
  226. this._resizeDelay = new FrameDelay(this.delayedResize, 2);
  227. }
  228. private function delayedResize():void
  229. {
  230. this._resizeDelay = null;
  231. this.dispatchEvent(new Event(Event.RESIZE));
  232. }
  233. /**
  234. * @inheritDoc
  235. */
  236. override public function destruct():void
  237. {
  238. LiquidStage._instance = null;
  239. if (this._resizeDelay)
  240. {
  241. this._resizeDelay.destruct();
  242. this._resizeDelay = null;
  243. }
  244. super.destruct();
  245. }
  246. }
  247. }