/src/uk/co/soulwire/display/DynamicSprite.as

https://github.com/og2t/SoulwireAS3Framework · ActionScript · 256 lines · 128 code · 49 blank · 79 comment · 11 complexity · 1d2aac7e61df974f10f9520491530e84 MD5 · raw file

  1. /**
  2. *
  3. * uk.co.soulwire.display.DynamicSprite
  4. *
  5. * @version 1.00 | May 21, 2010
  6. * @author Justin Windle
  7. *
  8. **/
  9. package uk.co.soulwire.display
  10. {
  11. import flash.display.Bitmap;
  12. import flash.display.DisplayObject;
  13. import flash.display.PixelSnapping;
  14. import flash.display.Sprite;
  15. import flash.events.Event;
  16. import flash.events.EventDispatcher;
  17. import flash.utils.Dictionary;
  18. import flash.utils.getQualifiedSuperclassName;
  19. /**
  20. * DynamicSprite
  21. */
  22. public class DynamicSprite extends Sprite
  23. {
  24. // ----------------------------------------------------------------
  25. // CONSTANTS
  26. // ----------------------------------------------------------------
  27. /**
  28. * Dispatched from the DynamicSprite class when a library is added or
  29. * updated via the static update method.
  30. */
  31. public static const LIBRARY_UPDATE_START : String = "DynamicSprite::libraryUpdateStart";
  32. /**
  33. * Dispatched from the DynamicSprite class when a library has been added
  34. * or updated and all DynamicSprite instances have been updated from it.
  35. */
  36. public static const LIBRARY_UPDATE_COMPLETE : String = "DynamicSprite::libraryUpdateComplete";
  37. /**
  38. * Dispatched from DynamicSprite instances when their assets have been updated.
  39. */
  40. public static const INSTANCE_UPDATED : String = "DynamicSprite::instanceUpdated";
  41. // ----------------------------------------------------------------
  42. // CLASS MEMBERS
  43. // ----------------------------------------------------------------
  44. private static var _eventDispatcher : EventDispatcher = new EventDispatcher();
  45. private static var _libraries : Dictionary = new Dictionary();
  46. private static var _instances : int = 0;
  47. private static var _updated : int = 0;
  48. // ----------------------------------------------------------------
  49. // PUBLIC CLASS METHODS
  50. // ----------------------------------------------------------------
  51. /**
  52. * Adds or updates an asset library. All instances of DynamicSprite will
  53. * attempt to update themselves each time this method is called.
  54. *
  55. * @param libraryID An ID for this type of library. Use the same ID each time
  56. * you wish to update assets of one type. For example, you could use the ID of
  57. * 'buttons' when setting the library from buttons_EN.swf and buttons_FR.swf.
  58. *
  59. * @param librarySWF The loaded SWF library (normally yourLoaderInstance.content).
  60. * The applicationDomain of this Object will be searched by each DynamicSprite
  61. * instance for it's particular Class which it will update from.
  62. */
  63. public static function update(libraryID : String, librarySWF : DisplayObject) : void
  64. {
  65. _updated = 0;
  66. _libraries[libraryID] = librarySWF;
  67. dispatchEvent( new Event( LIBRARY_UPDATE_START ));
  68. }
  69. // Encapsulate EventDispatcher
  70. public static function addEventListener(type : String, listener : Function, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = false) : void
  71. {
  72. _eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
  73. }
  74. public static function dispatchEvent(event : Event) : Boolean
  75. {
  76. return _eventDispatcher.dispatchEvent(event);
  77. }
  78. public static function hasEventListener(type : String) : Boolean
  79. {
  80. return _eventDispatcher.hasEventListener(type);
  81. }
  82. public static function removeEventListener(type : String, listener : Function, useCapture : Boolean = false) : void
  83. {
  84. _eventDispatcher.removeEventListener(type, listener, useCapture);
  85. }
  86. public static function willTrigger(type : String) : Boolean
  87. {
  88. return _eventDispatcher.willTrigger(type);
  89. }
  90. // ----------------------------------------------------------------
  91. // PRIVATE INSTANCE MEMBERS
  92. // ----------------------------------------------------------------
  93. private var _asset : DisplayObject;
  94. private var _classDefinition : String;
  95. // ----------------------------------------------------------------
  96. // CONSTRUCTOR
  97. // ----------------------------------------------------------------
  98. /**
  99. * Creates a new DynamicSprite instance
  100. *
  101. * @param __classDefinition The fully qualified class name of the
  102. * library asset which this DynamicSprite should use.
  103. */
  104. public function DynamicSprite(__classDefinition : String = '')
  105. {
  106. ++DynamicSprite._instances;
  107. _classDefinition = name = __classDefinition;
  108. DynamicSprite.addEventListener(DynamicSprite.LIBRARY_UPDATE_START, update, false, -1, false);
  109. update();
  110. }
  111. // ----------------------------------------------------------------
  112. // PUBLIC METHODS
  113. // ----------------------------------------------------------------
  114. /**
  115. * Destroys this DynamicSprite instance internally. Remember to also
  116. * remove all external references and listeners before nullifying.
  117. */
  118. public function destroy() : void
  119. {
  120. --DynamicSprite._instances;
  121. DynamicSprite.removeEventListener(DynamicSprite.LIBRARY_UPDATE_START, update);
  122. destroyAsset();
  123. }
  124. // ----------------------------------------------------------------
  125. // PRIVATE METHODS
  126. // ----------------------------------------------------------------
  127. private function destroyAsset() : void
  128. {
  129. if( _asset )
  130. {
  131. if( contains(_asset) )
  132. {
  133. removeChild(_asset);
  134. }
  135. if( _asset is Bitmap )
  136. {
  137. Bitmap(_asset).bitmapData.dispose();
  138. }
  139. _asset = null;
  140. }
  141. }
  142. // ----------------------------------------------------------------
  143. // EVENT HANDLERS
  144. // ----------------------------------------------------------------
  145. private function update(event : Event = null) : void
  146. {
  147. var hasChanged : Boolean = false;
  148. for each ( var lib : DisplayObject in _libraries )
  149. {
  150. if( lib.root.loaderInfo.applicationDomain.hasDefinition(_classDefinition) )
  151. {
  152. try
  153. {
  154. var type : Class = lib.root.loaderInfo.applicationDomain.getDefinition(_classDefinition) as Class;
  155. destroyAsset();
  156. if( getQualifiedSuperclassName(type) == "flash.display::BitmapData" )
  157. {
  158. _asset = new Bitmap(new type(0, 0), PixelSnapping.AUTO, true);
  159. }
  160. else
  161. {
  162. _asset = new type();
  163. }
  164. hasChanged = true;
  165. addChild(_asset);
  166. break;
  167. }
  168. catch( error : Error )
  169. {
  170. trace(error.name + " : " + error.message);
  171. }
  172. }
  173. }
  174. if( hasChanged )
  175. {
  176. dispatchEvent( new Event( DynamicSprite.INSTANCE_UPDATED ) );
  177. }
  178. if( ++_updated == _instances && event )
  179. {
  180. DynamicSprite.dispatchEvent( new Event( DynamicSprite.LIBRARY_UPDATE_COMPLETE ));
  181. }
  182. }
  183. // ----------------------------------------------------------------
  184. // PUBLIC ACCESSORS
  185. // ----------------------------------------------------------------
  186. /**
  187. * The asset for this DynamicSprite instance.
  188. */
  189. public function get asset() : DisplayObject
  190. {
  191. return _asset;
  192. }
  193. /**
  194. * The fully qualified class name of the library asset which this
  195. * DynamicSprite should use.
  196. */
  197. public function get classDefinition() : String
  198. {
  199. return _classDefinition;
  200. }
  201. public function set classDefinition(value : String) : void
  202. {
  203. _classDefinition = name = value;
  204. update();
  205. }
  206. }
  207. }