home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 2010 Software/Programs / PCGuia_programas.iso / Software / Utils / Livebrush / Install-LivebrushLite.air / livebrush.swf / scripts / com / livebrush / ui / Tooltip.as < prev    next >
Encoding:
Text File  |  2009-10-26  |  3.7 KB  |  129 lines

  1. package com.livebrush.ui
  2. {
  3.    import flash.display.DisplayObject;
  4.    import flash.events.EventPhase;
  5.    import flash.events.MouseEvent;
  6.    import flash.text.TextFieldAutoSize;
  7.    import flash.utils.clearTimeout;
  8.    import flash.utils.setTimeout;
  9.    
  10.    public class Tooltip
  11.    {
  12.       private static var SINGLETON:Tooltip;
  13.       
  14.       private static var TOOLTIP_ASSET:TooltipAsset;
  15.       
  16.       private static var OBJECTS:Array = [];
  17.       
  18.       private static var CURRENT_TIP:Object = null;
  19.       
  20.       private static var TIMEOUT:int = 0;
  21.       
  22.       public function Tooltip()
  23.       {
  24.          super();
  25.          this.init();
  26.       }
  27.       
  28.       public static function getInstance() : Tooltip
  29.       {
  30.          var instance:Tooltip = null;
  31.          if(SINGLETON == null)
  32.          {
  33.             SINGLETON = new Tooltip();
  34.             instance = SINGLETON;
  35.          }
  36.          else
  37.          {
  38.             instance = SINGLETON;
  39.          }
  40.          return instance;
  41.       }
  42.       
  43.       public static function addTip(obj:DisplayObject, label:String, left:Boolean = true, top:Boolean = true) : void
  44.       {
  45.          OBJECTS.push({
  46.             "obj":obj,
  47.             "label":label,
  48.             "left":left,
  49.             "top":top
  50.          });
  51.          obj.addEventListener(MouseEvent.MOUSE_OVER,SINGLETON._mouseListener);
  52.          obj.addEventListener(MouseEvent.MOUSE_OUT,SINGLETON._mouseListener);
  53.       }
  54.       
  55.       private function init() : void
  56.       {
  57.          TOOLTIP_ASSET = new TooltipAsset();
  58.          TOOLTIP_ASSET._label.autoSize = TextFieldAutoSize.LEFT;
  59.       }
  60.       
  61.       private function _hideTooltip() : void
  62.       {
  63.          clearTimeout(TIMEOUT);
  64.          try
  65.          {
  66.             CURRENT_TIP.obj.removeEventListener(MouseEvent.MOUSE_MOVE,this._mouseListener);
  67.             UI.TOOLTIP_HOLDER.removeChild(TOOLTIP_ASSET);
  68.          }
  69.          catch(e:Error)
  70.          {
  71.          }
  72.          CURRENT_TIP = null;
  73.       }
  74.       
  75.       private function _mouseListener(e:MouseEvent) : void
  76.       {
  77.          var obj:Object = null;
  78.          if(e.eventPhase == EventPhase.AT_TARGET)
  79.          {
  80.             obj = this._getObj(e.target);
  81.             if(e.type == MouseEvent.MOUSE_OVER)
  82.             {
  83.                clearTimeout(TIMEOUT);
  84.                TIMEOUT = setTimeout(this._showTooltip,750,obj);
  85.             }
  86.             else if(e.type == MouseEvent.MOUSE_OUT)
  87.             {
  88.                this._hideTooltip();
  89.             }
  90.          }
  91.          if(e.type == MouseEvent.MOUSE_MOVE)
  92.          {
  93.             TOOLTIP_ASSET.x = UI.TOOLTIP_HOLDER.mouseX - (TOOLTIP_ASSET.width + 10);
  94.             TOOLTIP_ASSET.y = UI.TOOLTIP_HOLDER.mouseY;
  95.          }
  96.       }
  97.       
  98.       private function _getObj(obj:Object) : Object
  99.       {
  100.          var dataObj:Object = null;
  101.          for(var i:int = 0; i < OBJECTS.length; i++)
  102.          {
  103.             if(obj == OBJECTS[i].obj)
  104.             {
  105.                dataObj = OBJECTS[i];
  106.             }
  107.          }
  108.          return dataObj;
  109.       }
  110.       
  111.       private function _showTooltip(o:Object) : void
  112.       {
  113.          if(CURRENT_TIP != null)
  114.          {
  115.             this._hideTooltip();
  116.          }
  117.          CURRENT_TIP = o;
  118.          TOOLTIP_ASSET._label.autoSize = TextFieldAutoSize.LEFT;
  119.          TOOLTIP_ASSET._label.text = o.label;
  120.          TOOLTIP_ASSET._bg.width = TOOLTIP_ASSET._label.textWidth + 17;
  121.          CURRENT_TIP.obj.addEventListener(MouseEvent.MOUSE_MOVE,this._mouseListener);
  122.          TOOLTIP_ASSET.x = UI.TOOLTIP_HOLDER.mouseX - (TOOLTIP_ASSET.width + 10);
  123.          TOOLTIP_ASSET.y = UI.TOOLTIP_HOLDER.mouseY;
  124.          UI.TOOLTIP_HOLDER.addChild(TOOLTIP_ASSET);
  125.       }
  126.    }
  127. }
  128.  
  129.