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

/src/code/base/view/Display.as

http://github.com/hexagonstar/tetragon
ActionScript | 448 lines | 123 code | 61 blank | 264 comment | 8 complexity | 29032da9249d891e87e55b79b12676ad MD5 | raw file
Possible License(s): MIT
  1. /*
  2. * _________ __ __
  3. * _/ / / /____ / /________ ____ ____ ___
  4. * _/ / / __/ -_) __/ __/ _ `/ _ `/ _ \/ _ \
  5. * _/________/ \__/\__/\__/_/ \_,_/\_, /\___/_//_/
  6. * /___/
  7. *
  8. * tetragon : Engine for Flash-based web and desktop games.
  9. * Licensed under the MIT License.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  12. * this software and associated documentation files (the "Software"), to deal in
  13. * the Software without restriction, including without limitation the rights to
  14. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  15. * the Software, and to permit persons to whom the Software is furnished to do so,
  16. * subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in all
  19. * copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  23. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  24. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  25. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  26. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package base.view
  29. {
  30. import base.Main;
  31. import base.io.resource.ResourceManager;
  32. import com.hexagonstar.util.reflection.getClassName;
  33. import flash.display.Sprite;
  34. /**
  35. * The abstract base class for all display classes, inclusive screens.
  36. *
  37. * A Display is a container that can contain any kind and number of other display
  38. * objects (Sprites, MovieClips, Shapes, Bitmaps etc.) and which is added for
  39. * display on a screen.
  40. */
  41. public class Display extends Sprite
  42. {
  43. //-----------------------------------------------------------------------------------------
  44. // Properties
  45. //-----------------------------------------------------------------------------------------
  46. private var _main:Main;
  47. private var _screen:Screen;
  48. private var _enabled:Boolean = true;
  49. private var _paused:Boolean = false;
  50. //-----------------------------------------------------------------------------------------
  51. // Constructor
  52. //-----------------------------------------------------------------------------------------
  53. /**
  54. * Creates a new Display instance.
  55. */
  56. public function Display()
  57. {
  58. _main = Main.instance;
  59. }
  60. //-----------------------------------------------------------------------------------------
  61. // Public Methods
  62. //-----------------------------------------------------------------------------------------
  63. /**
  64. * Starts the display after it has been initialized. You normally don't call this
  65. * method manually. Instead the parent screen calls it automatically when the
  66. * screen is being started.
  67. *
  68. * <p>This is an abstract method. Override this method in your display sub-class and
  69. * place any instructions into it that need to be done when the display is being
  70. * started, for example a display might contain animated display children that
  71. * should start playing after this method has been called.</p>
  72. */
  73. public function start():void
  74. {
  75. /* Abstract method! */
  76. }
  77. /**
  78. * Updates the display. This method is called automatically by the parent screen
  79. * before it is being started. It updates any display text that the display might
  80. * contain and lays out it's display children.
  81. *
  82. * <p>This method can be called if child objects of the display need to be
  83. * updated, e.g. after localization has been changed or if the display children
  84. * need to be re-layouted.</p>
  85. */
  86. public function update():void
  87. {
  88. updateDisplayText();
  89. layoutChildren();
  90. }
  91. /**
  92. * Used to put the display into it's initial state like it was right after the
  93. * display has been instantiated for the first time. This method can be called to
  94. * reset properties and child objects in case the display should be re-used
  95. * without the need to re-instantiate it.
  96. *
  97. * <p>This is an abstract method. You can override this method in your display
  98. * sub-class and place any instructions into it that need to be done to reset the
  99. * display.</p>
  100. */
  101. public function reset():void
  102. {
  103. /* Abstract method! */
  104. }
  105. /**
  106. * Stops the display if it has been started. You normally don't call this method
  107. * manually. Instead the parent screen calls it automatically when the screen is
  108. * being stopped.
  109. *
  110. * <p>This is an abstract method. You can override this method in your display
  111. * sub-class and place any instructions into it that need to be done to stop the
  112. * display, for example stopping an animation.</p>
  113. */
  114. public function stop():void
  115. {
  116. /* Abstract method! */
  117. }
  118. /**
  119. * Disposes the display to clean up resources that are no longer needed. A call to
  120. * this method stops the display and removes any event/signal listeners. You
  121. * normally don't call this method manually. Instead the parent screen calls it
  122. * automatically when the screen is being disposed.
  123. *
  124. * <p>If you want to override this method make sure to call super.dispose() at the
  125. * start of your overriden dispose method.</p>
  126. */
  127. public function dispose():void
  128. {
  129. stop();
  130. removeListeners();
  131. }
  132. /**
  133. * Returns a String representation of the display.
  134. *
  135. * @return A String representation of the display.
  136. */
  137. override public function toString():String
  138. {
  139. return getClassName(this);
  140. }
  141. //-----------------------------------------------------------------------------------------
  142. // Getters & Setters
  143. //-----------------------------------------------------------------------------------------
  144. /**
  145. * Determines if the display is enabled or disabled. On a disabled display any
  146. * display children are disabled so that no user interaction may occur until the
  147. * display is enabled again. Set this property to either <code>true</code>
  148. * (enabled) or <code>false</code> (disabled).
  149. *
  150. * @default <code>true</code>
  151. */
  152. public function get enabled():Boolean
  153. {
  154. return _enabled;
  155. }
  156. public function set enabled(v:Boolean):void
  157. {
  158. if (v == _enabled) return;
  159. _enabled = v;
  160. if (_enabled) enableChildren();
  161. else disableChildren();
  162. }
  163. /**
  164. * Determines whether the display is in paused state or not. If paused, any child
  165. * objects of the display are being paused too, if possible. This property should
  166. * be used if the display needs to be pausable, for example if it contains any
  167. * animation that should not play while the application is in a paused state.
  168. *
  169. * @default <code>false</code>
  170. */
  171. public function get paused():Boolean
  172. {
  173. return _paused;
  174. }
  175. public function set paused(v:Boolean):void
  176. {
  177. if (v == _paused) return;
  178. _paused = v;
  179. if (_paused) pauseChildren();
  180. else unpauseChildren();
  181. }
  182. /**
  183. * A reference to the display's parent screen, for use in sub-classes. This
  184. * property is set automatically by the display's parent screen when the display
  185. * is registered with the screen by using <code>registerDisplay()</code>.
  186. */
  187. public function get screen():Screen
  188. {
  189. return _screen;
  190. }
  191. public function set screen(v:Screen):void
  192. {
  193. _screen = v;
  194. }
  195. /**
  196. * A reference to Main for use in sub-classes.
  197. */
  198. protected function get main():Main
  199. {
  200. return _main;
  201. }
  202. /**
  203. * A reference to the resource manager for use in sub-classes.
  204. */
  205. protected function get resourceManager():ResourceManager
  206. {
  207. return _main.resourceManager;
  208. }
  209. //-----------------------------------------------------------------------------------------
  210. // Internal Methods
  211. //-----------------------------------------------------------------------------------------
  212. /**
  213. * Used to initialize the display.
  214. *
  215. * <p>You never have to call this method manually. Instead the parent screen
  216. * calls it automatically when the screen is being opened.</p>
  217. */
  218. internal function init():void
  219. {
  220. createChildren();
  221. addChildren();
  222. addListeners();
  223. }
  224. //-----------------------------------------------------------------------------------------
  225. // Private Methods
  226. //-----------------------------------------------------------------------------------------
  227. /**
  228. * Used to create any display children (and other objects) that the display might
  229. * contain. Note that child display objects should not be added to the display
  230. * list here. Instead they are added in the <code>addChildren()</code> method.
  231. *
  232. * <p>This is an abstract method. Override it in your sub-display class and create
  233. * any display child objects here that are part of the display.</p>
  234. */
  235. protected function createChildren():void
  236. {
  237. /* Abstract method! */
  238. }
  239. /**
  240. * Used to add child display objects to the display list that were created in the
  241. * <code>createChildren()</code> method.
  242. *
  243. * <p>This is an abstract method. Override it in your sub-display class and add
  244. * any display children to the display list here that are part of the display.</p>
  245. */
  246. protected function addChildren():void
  247. {
  248. /* Abstract method! */
  249. }
  250. /**
  251. * Used to add any event or signal listeners to child objects of the display.
  252. *
  253. * <p>This is an abstract method. Override this method and add any listeners to
  254. * objects that require event/signal listening.</p>
  255. *
  256. * @see removeListeners
  257. */
  258. protected function addListeners():void
  259. {
  260. /* Abstract method! */
  261. }
  262. /**
  263. * Used to remove any event or signal listeners from child objects that were added
  264. * inside the <code>addListeners()</code> method. Called automatically when the
  265. * display is being disposed.
  266. *
  267. * <p>This is an abstract method. Override this method and remove any event/signal
  268. * listeners here that were added in <code>addListeners()</code>.</p>
  269. *
  270. * @see addListeners
  271. */
  272. protected function removeListeners():void
  273. {
  274. /* Abstract method! */
  275. }
  276. /**
  277. * Used to update any display text that the display might contain. Typically any
  278. * textfield text should be set here with strings from the application's currently
  279. * used text resources. Called whenever the display's <code>update()</code> method
  280. * is called.
  281. *
  282. * <p>This is an abstract method. Override it in your sub-display class and set
  283. * strings from text resources to any text displays if they require it.</p>
  284. */
  285. protected function updateDisplayText():void
  286. {
  287. /* Abstract method! */
  288. }
  289. /**
  290. * Used to layout the display children of the display. This method is called
  291. * initially to set the position and size of any child objects and should be
  292. * called whenever the children need to update their position or size because the
  293. * layout has changed, for example after the application window has been resized.
  294. *
  295. * <p>This is an abstract method. Override it in your sub-display class and set
  296. * the position and size of all child display objects here.</p>
  297. */
  298. protected function layoutChildren():void
  299. {
  300. /* Abstract method! */
  301. }
  302. /**
  303. * Used to enable any child display objects. Called whenever the
  304. * <code>enabled</code> property of the display is set to <code>true</code>.
  305. *
  306. * <p>This is an abstract method. Override it in your sub-display class and enable
  307. * any child display objects here that should be enabled when the display is being
  308. * enabled.</p>
  309. *
  310. * @see enabled
  311. * @see disableChildren
  312. */
  313. protected function enableChildren():void
  314. {
  315. /* Abstract method! */
  316. }
  317. /**
  318. * Used to disable any child display objects. Called whenever the
  319. * <code>enabled</code> property of the display is set to <code>false</code>.
  320. *
  321. * <p>This is an abstract method. Override it in your sub-display class and disable
  322. * any child display objects here that should be disabled when the display is being
  323. * disabled.</p>
  324. *
  325. * @see enabled
  326. * @see enableChildren
  327. */
  328. protected function disableChildren():void
  329. {
  330. /* Abstract method! */
  331. }
  332. /**
  333. * Used to pause any child display objects. Called whenever the
  334. * <code>paused</code> property of the display is set to <code>true</code>.
  335. *
  336. * <p>This is an abstract method. Override it in your sub-display class and pause
  337. * any child display objects here that should be paused when the display is being
  338. * put into paused mode.</p>
  339. *
  340. * @see paused
  341. * @see unpauseChildren
  342. */
  343. protected function pauseChildren():void
  344. {
  345. /* Abstract method! */
  346. }
  347. /**
  348. * Used to unpause any child display objects that were paused. Called whenever the
  349. * <code>paused</code> property of the display is set to <code>false</code>.
  350. *
  351. * <p>This is an abstract method. Override it in your sub-display class and unpause
  352. * any child display objects here that should be unpaused when the display is being
  353. * put into unpaused mode.</p>
  354. *
  355. * @see paused
  356. * @see pauseChildren
  357. */
  358. protected function unpauseChildren():void
  359. {
  360. /* Abstract method! */
  361. }
  362. //-----------------------------------------------------------------------------------------
  363. // Helper Methods
  364. //-----------------------------------------------------------------------------------------
  365. /**
  366. * Helper method to get a resource's content from the resource index. The type
  367. * depends on the content type of the resource.
  368. *
  369. * @param resourceID The ID of the resource.
  370. * @return The resource content or <code>null</code>.
  371. */
  372. protected static function getResource(resourceID:String):*
  373. {
  374. return Main.instance.resourceManager.resourceIndex.getResourceContent(resourceID);
  375. }
  376. /**
  377. * Helper method to get a string from the string index.
  378. *
  379. * @param stringID The ID of the string.
  380. * @return The requested string.
  381. */
  382. protected static function getString(stringID:String):String
  383. {
  384. return Main.instance.resourceManager.stringIndex.get(stringID);
  385. }
  386. }
  387. }