/Basics/src/Basics/Controls/RadialMenu.as

http://vanished-games.googlecode.com/ · ActionScript · 171 lines · 97 code · 14 blank · 60 comment · 16 complexity · c36feb8da7decd651b68fb836ad49425 MD5 · raw file

  1. /*
  2. Copyright (c) 2011 MIT
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. software and associated documentation files (the "Software"), to deal in the Software
  5. without restriction, including without limitation the rights to use, copy, modify,
  6. merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in all copies
  9. or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  11. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  12. PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  13. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  14. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  15. USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. =============================================================
  17. Questions about this code or project? Contact:
  18. Dana Tenneson, Lead Developer (datennes mit.edu)
  19. Caitlin Feeley, Project Manager (cfeeley mit.edu)
  20. Eric Klopfer, Principal Investigator (klopfer mit.edu)
  21. MIT Education Arcade/STEP (http://education.mit.edu)
  22. */
  23. package Basics.Controls
  24. {
  25. import flash.display.DisplayObject;
  26. import flash.display.InteractiveObject;
  27. import flash.events.Event;
  28. import flash.events.MouseEvent;
  29. import mx.core.UIComponent;
  30. import mx.events.FlexMouseEvent;
  31. import mx.events.ResizeEvent;
  32. public class RadialMenu extends Radial
  33. {
  34. /** By default, a RadialMenu is removed from its parent when it closes. Setting closeOnlyHides to true
  35. * makes the menu invisible instead of removed.
  36. */
  37. public var closeOnlyHides:Boolean = false;
  38. /** by default, a RadialMenu closes when a click occurs. Setting noClose to true
  39. * makes the menu permanent.
  40. */
  41. public var noClose:Boolean = false;
  42. /** Constructor.
  43. */
  44. public function RadialMenu()
  45. {
  46. // Handle resizes.
  47. this.addEventListener(ResizeEvent.RESIZE, onResize,false,0,true);
  48. // Handle mouse events outside of the control.
  49. //this.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, onMouseDownOutside,false,0,true);
  50. this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage,false,0,true);
  51. this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage,false,0,true);
  52. }
  53. /** @inheritDoc
  54. */
  55. override public function addChildAt(child:DisplayObject, index:int):DisplayObject
  56. {
  57. var ret:DisplayObject = super.addChildAt(child,index);
  58. // Add our listener
  59. if(child is InteractiveObject)
  60. {
  61. InteractiveObject(child).addEventListener(MouseEvent.CLICK,handleChildClick,false,0,true);
  62. }
  63. return ret;
  64. }
  65. /** @inheritDoc
  66. */
  67. override public function removeChildAt(index:int):DisplayObject
  68. {
  69. var child:DisplayObject = super.removeChildAt(index);
  70. if(child is InteractiveObject)
  71. {
  72. InteractiveObject(child).removeEventListener(MouseEvent.CLICK,handleChildClick);
  73. }
  74. return child;
  75. }
  76. /** Called when we are added to the stage.
  77. */
  78. private function onAddedToStage(event:Event):void
  79. {
  80. this.stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown,false,0,true);
  81. }
  82. /** Called when we are added to the stage.
  83. */
  84. private function onRemovedFromStage(event:Event):void
  85. {
  86. this.stage.removeEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
  87. }
  88. /** Handles stage mouse downs.
  89. */
  90. private function onStageMouseDown(event:MouseEvent):void
  91. {
  92. // Check if we should close if we are visible and attached.
  93. if(this.visible && this.parent != null)
  94. {
  95. // See if the event came from us.
  96. var target:UIComponent = event.target as UIComponent;
  97. while(target != null && target != this)
  98. {
  99. target = target.parent as UIComponent;
  100. }
  101. if(target == null || target != this)
  102. {
  103. close();
  104. }
  105. }
  106. }
  107. /** Launches this radial menu centered at the location of the target. Usually, this is in response
  108. * to a click on the target.
  109. */
  110. public function launch(target:UIComponent):void
  111. {
  112. target.addChild(this);
  113. this.onResize(null);
  114. }
  115. /** Closes the radial menu. All items added to the radial menu will call this method on a click.
  116. */
  117. public function close():void
  118. {
  119. // See if we're supposed to hide
  120. if (!noClose) {
  121. if(this.closeOnlyHides)
  122. {
  123. this.visible = false;
  124. }
  125. // Remove ourselves.
  126. else
  127. {
  128. if(this.parent != null)
  129. {
  130. this.parent.removeChild(this);
  131. }
  132. }
  133. }
  134. }
  135. private function onMouseDownOutside(event:FlexMouseEvent):void
  136. {
  137. // Maybe use this to close the radial?
  138. trace("Mouse down outside");
  139. close();
  140. }
  141. /** Called when any child is clicked. In addition to that child's event, we should close the radial
  142. * menu.
  143. */
  144. private function handleChildClick(event:MouseEvent):void
  145. {
  146. // Close the control.
  147. close();
  148. }
  149. /** Adjusts our position on a resize.
  150. */
  151. private function onResize(event:ResizeEvent):void
  152. {
  153. this.x = -this.width/2
  154. this.y = -this.height/2
  155. }
  156. }
  157. }