package com.a51integrated.ui { import flash.display.Sprite; import flash.display.Stage; import flash.events.Event; import flash.geom.Point; import flash.text.TextField; import flash.text.TextFieldAutoSize; /** * Tool tip class for displaying tool tips in an app. It is a singleton class. * Use it by simply calling ToolTip.show( 'Tool tip goes here' ) and providing the tip * You need to explicitly call the ToolTip.hide() method when you want it to dissapear. * * NOTE: You must call ToolTip.init( stage ) and pass a reference to the stage before you call ToolTip.show(); * This only needs to happen once per project * * ... * @author Phillip Chertok */ public class ToolTip extends Sprite { private var _tip: TextField; private var _stage: Stage; private static var _instance: ToolTip; private static var _allowInstance: Boolean; /** * Returns an instance of the tool tip */ private static function get instance():ToolTip { if (ToolTip._instance) { return ToolTip._instance; } ToolTip._allowInstance = true; ToolTip._instance = new ToolTip(); ToolTip._allowInstance = false; return ToolTip._instance; } /** * Consrtuctor does not get explicitly called * */ public function ToolTip() { if (!ToolTip._allowInstance) { throw new Error("Error: Use ToolTip.instance instead of the new keyword."); }else { this.mouseEnabled = false; initTextField(); } } /** * We need to pass at least one reference to the stage */ public static function init($stage:Stage):void { ToolTip.instance._stage = $stage; } /** * Creates a text field that will be used to display the tooltip */ private function initTextField():void { _tip = new TextField(); _tip.selectable = false; _tip.mouseEnabled = false; //_tip.wordWrap = true; //_tip.multiline = true; _tip.autoSize = TextFieldAutoSize.LEFT; _tip.border = true; _tip.borderColor = 0x000000; _tip.background = true; _tip.backgroundColor = 0xFFFF9F; addChild(_tip); } /** * Gets our instance and calls the internal method to display the tooltip * * @param $tip */ public static function show($tip):void { ToolTip.instance.showTip($tip); } /** * * * @param $tip */ private function showTip($tip):void { _tip.text = $tip; //Add an event listener to follow our mouse movement this.addEventListener(Event.ENTER_FRAME, followMouse, false, 0, true); //Explicitly call our mouse tracking method to make sure the tool tip appears in the correct place followMouse(null); //Add the tip to the stage _stage.addChild(this); } /** * Public function that calls our instance's internal hide method */ public static function hide():void { ToolTip.instance.hideTip(); } /** * Removes the tup from the stage */ private function hideTip():void { removeEventListener(Event.ENTER_FRAME, followMouse, false); if (_stage.contains(this)) stage.removeChild(this); } /** * Tracks the mouse position and follows accordingly * * @param $e */ private function followMouse(e:Event):void { var newX:Number = _stage.mouseX; var newY:Number = _stage.mouseY - _tip.height - 2; this.x = newX; this.y = newY; } } }