/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
- /*
- Copyright (c) 2011 MIT
- Permission is hereby granted, free of charge, to any person obtaining a copy of this
- software and associated documentation files (the "Software"), to deal in the Software
- without restriction, including without limitation the rights to use, copy, modify,
- merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
- permit persons to whom the Software is furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all copies
- or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
- USE OR OTHER DEALINGS IN THE SOFTWARE.
- =============================================================
- Questions about this code or project? Contact:
- Dana Tenneson, Lead Developer (datennes mit.edu)
- Caitlin Feeley, Project Manager (cfeeley mit.edu)
- Eric Klopfer, Principal Investigator (klopfer mit.edu)
- MIT Education Arcade/STEP (http://education.mit.edu)
- */
- package Basics.Controls
- {
- import flash.display.DisplayObject;
- import flash.display.InteractiveObject;
- import flash.events.Event;
- import flash.events.MouseEvent;
-
- import mx.core.UIComponent;
- import mx.events.FlexMouseEvent;
- import mx.events.ResizeEvent;
-
- public class RadialMenu extends Radial
- {
- /** By default, a RadialMenu is removed from its parent when it closes. Setting closeOnlyHides to true
- * makes the menu invisible instead of removed.
- */
- public var closeOnlyHides:Boolean = false;
- /** by default, a RadialMenu closes when a click occurs. Setting noClose to true
- * makes the menu permanent.
- */
- public var noClose:Boolean = false;
-
- /** Constructor.
- */
- public function RadialMenu()
- {
- // Handle resizes.
- this.addEventListener(ResizeEvent.RESIZE, onResize,false,0,true);
-
- // Handle mouse events outside of the control.
- //this.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, onMouseDownOutside,false,0,true);
- this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage,false,0,true);
- this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage,false,0,true);
- }
-
- /** @inheritDoc
- */
- override public function addChildAt(child:DisplayObject, index:int):DisplayObject
- {
- var ret:DisplayObject = super.addChildAt(child,index);
- // Add our listener
- if(child is InteractiveObject)
- {
- InteractiveObject(child).addEventListener(MouseEvent.CLICK,handleChildClick,false,0,true);
- }
- return ret;
- }
-
- /** @inheritDoc
- */
- override public function removeChildAt(index:int):DisplayObject
- {
- var child:DisplayObject = super.removeChildAt(index);
- if(child is InteractiveObject)
- {
- InteractiveObject(child).removeEventListener(MouseEvent.CLICK,handleChildClick);
- }
- return child;
- }
-
- /** Called when we are added to the stage.
- */
- private function onAddedToStage(event:Event):void
- {
- this.stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown,false,0,true);
- }
-
- /** Called when we are added to the stage.
- */
- private function onRemovedFromStage(event:Event):void
- {
- this.stage.removeEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
- }
-
- /** Handles stage mouse downs.
- */
- private function onStageMouseDown(event:MouseEvent):void
- {
- // Check if we should close if we are visible and attached.
- if(this.visible && this.parent != null)
- {
- // See if the event came from us.
- var target:UIComponent = event.target as UIComponent;
- while(target != null && target != this)
- {
- target = target.parent as UIComponent;
- }
- if(target == null || target != this)
- {
- close();
- }
- }
- }
-
- /** Launches this radial menu centered at the location of the target. Usually, this is in response
- * to a click on the target.
- */
- public function launch(target:UIComponent):void
- {
- target.addChild(this);
- this.onResize(null);
- }
-
- /** Closes the radial menu. All items added to the radial menu will call this method on a click.
- */
- public function close():void
- {
- // See if we're supposed to hide
- if (!noClose) {
- if(this.closeOnlyHides)
- {
- this.visible = false;
- }
- // Remove ourselves.
- else
- {
- if(this.parent != null)
- {
- this.parent.removeChild(this);
- }
- }
- }
- }
-
- private function onMouseDownOutside(event:FlexMouseEvent):void
- {
- // Maybe use this to close the radial?
- trace("Mouse down outside");
- close();
- }
-
- /** Called when any child is clicked. In addition to that child's event, we should close the radial
- * menu.
- */
- private function handleChildClick(event:MouseEvent):void
- {
- // Close the control.
- close();
- }
-
- /** Adjusts our position on a resize.
- */
- private function onResize(event:ResizeEvent):void
- {
- this.x = -this.width/2
- this.y = -this.height/2
- }
- }
- }