/modules/Sample/ui/src/classes/Document.as

https://github.com/daevid/xJSFL
ActionScript | 159 lines | 72 code | 39 blank | 48 comment | 2 complexity | 136f1a0e1b0e82e5e9dc99e41fd531b5 MD5 | raw file
  1. package
  2. {
  3. import flash.display.DisplayObject;
  4. import flash.display.Sprite;
  5. import flash.display.StageAlign;
  6. import flash.events.Event;
  7. import flash.events.MouseEvent;
  8. import flash.text.TextField;
  9. import fl.controls.Button;
  10. import com.xjsfl.jsfl.modules.AbstractModule;
  11. import SampleModule;
  12. /**
  13. * ...
  14. * @author Dave Stewart
  15. */
  16. public class Document extends Sprite
  17. {
  18. // ---------------------------------------------------------------------------------------------------------------------
  19. // { region: Variables
  20. // stage instances
  21. public var btnMethod :Button;
  22. public var btnConfig :Button;
  23. public var btnAssets :Button;
  24. public var tfResults :TextField;
  25. // properties
  26. protected var module :SampleModule;
  27. // ---------------------------------------------------------------------------------------------------------------------
  28. // { region: Instatiation
  29. public function Document()
  30. {
  31. // stage
  32. stage.align = StageAlign.TOP_LEFT;
  33. visible = false;
  34. // setup module
  35. module = AbstractModule.create(SampleModule, this) as SampleModule;
  36. // assign handler to module, which is called when all assets have loaded
  37. module.addEventListener(Event.COMPLETE, onModuleLoaded);
  38. // generic resize handler
  39. addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  40. }
  41. // ---------------------------------------------------------------------------------------------------------------------
  42. // { region: Configuration
  43. // ---------------------------------------------------------------------------------------------------------------------
  44. // { region: Public methods
  45. // ---------------------------------------------------------------------------------------------------------------------
  46. // { region: Accessors
  47. // ---------------------------------------------------------------------------------------------------------------------
  48. // { region: Handlers
  49. /**
  50. * Add button event listeners only when the module has loaded all its assets
  51. * @param event
  52. */
  53. protected function onModuleLoaded(event:Event):void
  54. {
  55. btnMethod.addEventListener(MouseEvent.CLICK, onButtonClick);
  56. btnConfig.addEventListener(MouseEvent.CLICK, onButtonClick);
  57. btnAssets.addEventListener(MouseEvent.CLICK, onButtonClick);
  58. visible = true;
  59. }
  60. /**
  61. * Take action when the buttons are pressed: run a command, show config, and loaded assets
  62. * @param event
  63. */
  64. protected function onButtonClick(event:MouseEvent):void
  65. {
  66. switch (event.target)
  67. {
  68. // call module method
  69. case btnMethod:
  70. tfResults.text = module.test();
  71. break;
  72. // trace module config (demonstrates accessing SampleModule using its static instance property)
  73. case btnConfig:
  74. tfResults.text = SampleModule.instance.settings.toXMLString();
  75. break;
  76. // add module asset
  77. case btnAssets:
  78. var item:DisplayObject = addChild(module.butterfly);
  79. item.x = (stage.stageWidth / 2) - 16;
  80. item.y = tfResults.y + (tfResults.height / 2) - 16;
  81. break;
  82. }
  83. }
  84. /**
  85. * Generic onAddedToStage handler
  86. * @param event
  87. */
  88. protected function onAddedToStage(event:Event):void
  89. {
  90. removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  91. stage.addEventListener(Event.RESIZE, onResize);
  92. onResize();
  93. }
  94. /**
  95. * Generic onResize handler
  96. * @param event
  97. */
  98. protected function onResize(event:Event = null):void
  99. {
  100. // variables
  101. var x :Number = 5;
  102. var y :Number = 5;
  103. var children :Array = [btnMethod, btnConfig, btnAssets, tfResults];
  104. // layout
  105. for each(var child:DisplayObject in children)
  106. {
  107. child.width = stage.stageWidth - 10;
  108. child.x = x;
  109. child.y = y;
  110. y += 5 + child.height;
  111. }
  112. // textfield
  113. tfResults.height = stage.stageHeight - tfResults.y - 5;
  114. }
  115. // ---------------------------------------------------------------------------------------------------------------------
  116. // { region: Protected methods
  117. // ---------------------------------------------------------------------------------------------------------------------
  118. // { region: Utilities
  119. }
  120. }