/src/sg/camogxmlgaia/assets/domain/SWFDisplaySourceAsset.as

https://github.com/stewymac07/SG-Camo-Collections · ActionScript · 163 lines · 103 code · 40 blank · 20 comment · 6 complexity · 5fcc636d9801bcddfa8accbe2b396275 MD5 · raw file

  1. package sg.camogxmlgaia.assets.domain
  2. {
  3. import flash.display.DisplayObject;
  4. import flash.display.Sprite;
  5. import flash.events.Event;
  6. import sg.camo.interfaces.IDestroyable;
  7. import sg.camo.interfaces.IDisplayRender;
  8. import sg.camo.interfaces.IDisplayRenderSource;
  9. import sg.camogxmlgaia.api.ISourceAsset;
  10. import sg.camogxmlgaia.assets.SWFLibraryAsset;
  11. import sg.camogxml.dummy.SpriteRenderProxy;
  12. import sg.camogxml.dummy.DisplayObjectRenderProxy;
  13. /**
  14. * A SWFLibraryAsset that allows you to retrieve IDisplayRender instances from Flash library linkages.
  15. *
  16. * @author Glenn Ko
  17. */
  18. public class SWFDisplaySourceAsset extends SWFLibraryAsset implements IDisplayRenderSource, ISourceAsset
  19. {
  20. /** @private */
  21. protected var _destroyList:Array;
  22. public function SWFDisplaySourceAsset()
  23. {
  24. }
  25. override public function init():void {
  26. super.init();
  27. _destroyList = [];
  28. }
  29. public function getDisplayRenders():Array {
  30. return _destroyList.concat();
  31. }
  32. public function get source():* {
  33. return this;
  34. }
  35. public function get sourceType():String {
  36. return "render";
  37. }
  38. override protected function onComplete(event:Event):void
  39. {
  40. super.onComplete(event);
  41. for (var i:* in classesDictionary) {
  42. var renderDef:DisplayRenderDefinition = new DisplayRenderDefinition( i, classesDictionary[i]);
  43. _destroyList.push( renderDef );
  44. }
  45. }
  46. override public function destroy():void
  47. {
  48. super.destroy();
  49. for each (var iDes:IDestroyable in _destroyList) {
  50. iDes.destroy();
  51. }
  52. _destroyList = null;
  53. }
  54. // --IDisplayRenderSource
  55. /**
  56. * Retrieves a fresh new valid IDisplayRender instance locally with a linkage id from the Flash Library of the SWF. <br/><br/>
  57. * All retrieved linkage display objects are assumed to implement the IDisplayRender interface, otherwise a dummy
  58. * SpriteRenderProxy or DisplayObjectRenderProxy is used over the retrieved display object instance.
  59. * @see sg.camogxml.dummy.SpriteRenderProxy
  60. * @see sg.camogxml.dummy.DisplayObjectRenderProxy
  61. *
  62. * @param id Gets IDisplayRender instance through linkage id.
  63. * @return A valid IDisplayRender instance or null if all options fail.
  64. */
  65. public function getRenderById(id:String):IDisplayRender {
  66. if (!hasDefinition(id)) return null;
  67. var targ:Object = new (getDefinition(id))();
  68. return targ is IDisplayRender ? targ as IDisplayRender : targ is Sprite ? new SpriteRenderProxy(id, targ as Sprite) : targ is DisplayObject ? new DisplayObjectRenderProxy(id, targ as DisplayObject) : null;
  69. }
  70. override public function toString():String
  71. {
  72. return "[SWFDisplaySourceAsset] " + _id;
  73. }
  74. }
  75. }
  76. import flash.display.DisplayObject;
  77. import flash.display.Sprite;
  78. import sg.camo.interfaces.IDestroyable;
  79. import sg.camo.interfaces.IDisplayRender;
  80. import sg.camogxml.dummy.DisplayObjectRenderProxy;
  81. import sg.camogxml.dummy.SpriteRenderProxy;
  82. internal class DisplayRenderDefinition implements IDisplayRender, IDestroyable {
  83. protected var _renderId:String;
  84. protected var _isActive:Boolean = false;
  85. protected var _class:Class;
  86. protected var _displayRender:IDisplayRender;
  87. public function DisplayRenderDefinition(renderId:String, classe:Class) {
  88. _renderId = renderId;
  89. _class = classe;
  90. }
  91. /** render ID for registering with asset managers. */
  92. public function get renderId():String {
  93. return _renderId;
  94. }
  95. /** the current main display object instance */
  96. public function get rendered():DisplayObject {
  97. return _displayRender ? _displayRender.rendered : newRender.rendered;
  98. }
  99. private function get newRender():IDisplayRender {
  100. var tryDisp:DisplayObject = new _class() as DisplayObject;
  101. if (tryDisp == null) {
  102. trace("SWFDisplaySourceAsset :: DisplayRenderDefinition newRender() halt: Retrieved library class definition instance isn't DisplayObject");
  103. return null;
  104. }
  105. _displayRender = tryDisp is IDisplayRender ? tryDisp as IDisplayRender : tryDisp is Sprite ? new SpriteRenderProxy(_renderId, tryDisp as Sprite) : new DisplayObjectRenderProxy(_renderId, tryDisp);
  106. return _displayRender;
  107. }
  108. public function getRenderedById(id:String):DisplayObject {
  109. return _displayRender.getRenderedById(id);
  110. }
  111. /** support for restoring back any default settings that were changed during the course of the application */
  112. public function restore(changedHash:Object = null):void {
  113. }
  114. public function destroy():void {
  115. if (_displayRender is IDestroyable) (_displayRender as IDestroyable).destroy();
  116. }
  117. public function set isActive(boo:Boolean):void {
  118. _isActive = boo;
  119. }
  120. public function get isActive():Boolean {
  121. return _isActive;
  122. }
  123. }