/src/sg/camolite/display/GSelection.as

https://github.com/stewymac07/SG-Camo-Collections · ActionScript · 149 lines · 86 code · 31 blank · 32 comment · 17 complexity · b4350c6b208d4f37739c4eca4d209a63 MD5 · raw file

  1. package sg.camolite.display
  2. {
  3. import flash.events.Event;
  4. import flash.events.IEventDispatcher;
  5. import sg.camo.notifications.GDisplayNotifications;
  6. import sg.camo.interfaces.ISelectable;
  7. import sg.camo.events.SelectionEvent;
  8. import sg.camo.interfaces.ISelectioner;
  9. import sg.camo.interfaces.IDestroyable;
  10. import sg.camo.interfaces.ISelectionScanner;
  11. import flash.display.DisplayObject;
  12. import flash.display.Sprite;
  13. /**
  14. * Selection display-based class that handles and perform single selections similar to
  15. * <code>SelectionBehaviour</code>.
  16. * <br/>Also includes other functionalities to execute selections by string id through
  17. * extended <code>ISelectioner</code> interface.
  18. * <br/>It also has <code>ISelectionScanner</code> functionality.
  19. *
  20. * @see sg.camo.behaviour.SelectionBehaviour
  21. *
  22. * @author Glenn Ko
  23. */
  24. public class GSelection extends GBase implements ISelectioner, IDestroyable, ISelectionScanner
  25. {
  26. /** @private TODO */
  27. public var defaultSelected:String = ""; // flag to automatically select a sub/content id ISelectable item by default
  28. public var autoSelect:Boolean = true; // flag to determine if selection is done automatically by the class itself
  29. public var bubbling:Boolean = true; // whether dispatched selectionEvent is bubbling or not
  30. /** @private */
  31. protected var _curSelected:IEventDispatcher;
  32. /**
  33. * Current data hash of scanned selectables (if any)
  34. */
  35. protected var data:Object = { };
  36. public function GSelection()
  37. {
  38. super();
  39. addEventListener (GDisplayNotifications.SELECT, selectionHandler, false, 1, true);
  40. }
  41. // -- IReflectClass
  42. override public function get reflectClass():Class {
  43. return GSelection;
  44. }
  45. /**
  46. * ISelectionScanner method to MANUALLY scan a display list for ISelectables, which are registered to the data hash.
  47. * @param targ A target Sprite container to run the scan on, calling processSelectableChild() on any found ISelectable instances.
  48. *
  49. * @see sg.camo.interfaces.ISelectable
  50. */
  51. public function scan(targ:Sprite):void {
  52. var total:int = targ.numChildren;
  53. for (var i:int=0; i<total; i++) {
  54. var child:DisplayObject = targ.getChildAt(i);
  55. if (!(child is ISelectable)) continue;
  56. processSelectableChild(child);
  57. }
  58. }
  59. public function clearSelection():void {
  60. if (_curSelected == null) return;
  61. if (_curSelected is ISelectable) (_curSelected as ISelectable).selected = false;
  62. _curSelected.dispatchEvent ( new Event(GDisplayNotifications.UNSELECTED) );
  63. _curSelected = null;
  64. }
  65. /**
  66. * Registers scanned ISelectable child to data hash. By default,
  67. * uses the child's name as the key to the data hash.
  68. * @param child
  69. */
  70. protected function processSelectableChild(child:DisplayObject):void {
  71. data[child.name] = child;
  72. }
  73. public function destroy():void {
  74. data = null;
  75. removeEventListener(GDisplayNotifications.SELECT, selectionHandler);
  76. }
  77. // -- Selection handlers/methods
  78. /** @private */
  79. protected function selectionHandler (e:Event):void {
  80. if (autoSelect) doSelection (e.target) // do first than dispatch notification
  81. else dispatchSelection(e.target); // dispatch notification only
  82. }
  83. /** @private */
  84. protected function dispatchSelection(sel:*):void {
  85. dispatchEvent (new SelectionEvent(SelectionEvent.SELECT, sel, bubbling));
  86. }
  87. // manual remote runtime public setter based on subId/contentId string
  88. public function set selection(str:String):void {
  89. if (str == null) {
  90. clearSelection();
  91. return;
  92. }
  93. var chk:* = data[str];
  94. if (chk == null) chk = getChildByName(str);
  95. doSelection(chk);
  96. }
  97. public function get curSelected ():* {
  98. return _curSelected;
  99. }
  100. public function doSelection (targ:*):Boolean {
  101. if (!targ is IEventDispatcher) {
  102. trace("GSelection doSelection() halt! targ isn't IEventDispatcher or null!");
  103. return false;
  104. }
  105. if (_curSelected!=null) {
  106. if (_curSelected is ISelectable) (_curSelected as ISelectable).selected = false
  107. _curSelected.dispatchEvent( new Event(GDisplayNotifications.UNSELECTED) );
  108. }
  109. var sel:ISelectable = targ as ISelectable;
  110. if (sel != null) sel.selected = true
  111. else trace ("Warning: GSelection doSelection(); targ isn't ISelectable");
  112. _curSelected = targ as IEventDispatcher;
  113. _curSelected.dispatchEvent( new Event(GDisplayNotifications.SELECTED) );
  114. dispatchSelection(targ);
  115. return true;
  116. }
  117. }
  118. }