·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3.0的Tooltip类示例

时间:2013-05-16 21:55CuPlayer.com
[AS3]as3.0的Tooltip类示例

[AS3]as3.0的Tooltip类示例

  1. package com.flepstudio.text   
  2. {   
  3.     import flash.display.*;   
  4.     import flash.events.*;   
  5.     import flash.text.*;   
  6.         
  7.     /**  
  8.      * ToolTip is a ValueObject for the FlepStudio API.  
  9.      * This class produces a tooltip advice on mouse rollover  
  10.      *  
  11.      * @author Filippo Lughi  
  12.      * @version Actionscript 3.0  
  13.      */   
  14.     public class ToolTip extends MovieClip   
  15.     {   
  16.         private var _bg_color:uint;   
  17.         private var _text_color:uint;   
  18.         private var _text_size:int;   
  19.         private var _font:String;   
  20.         private var _tool_text:String;   
  21.         private var _field_txt:TextField;   
  22.         private var _alpha_color:Number;   
  23.             
  24.         private var ratio:int=10;   
  25.         private var holder_mc:MovieClip;   
  26.         private var bg_mc:MovieClip;   
  27.         private var father:MovieClip;   
  28.             
  29.         /**  
  30.          * Construct a new ToolTip instance  
  31.          *  
  32.          * @param        .bc        uint         -- background color  
  33.          * @param        .tc            uint         -- text color  
  34.          * @param        .ts            int            -- text size  
  35.          * @param        .f            String        -- the font to use  
  36.          * @param        .tt            String        -- text of the tooltip  
  37.          * @param        .n            Number     -- alpha of background color  
  38.          */   
  39.         public function ToolTip(bc:uint,tc:uint,ts:int,f:String,tt:String,n:Number)   
  40.         {   
  41.             bg_color=bc;   
  42.             text_color=tc;   
  43.             text_size=ts;   
  44.             ffont=f;   
  45.             tool_text=tt;   
  46.             alpha_color=n;   
  47.                 
  48.             addEventListener(Event.ADDED_TO_STAGE,init);   
  49.                 
  50.             mouseEnabled=false;   
  51.             alpha=0;   
  52.         }   
  53.             
  54.         /**  
  55.          * Background color  
  56.          */   
  57.         public function get bg_color():uint   
  58.         {   
  59.             return _bg_color;   
  60.         }   
  61.             
  62.         public function set bg_color(c:uint):void   
  63.         {   
  64.             _bg_color=c;   
  65.         }   
  66.             
  67.         /**  
  68.          * Text color  
  69.          */   
  70.         public function get text_color():uint   
  71.         {   
  72.             return _text_color;   
  73.         }   
  74.             
  75.         public function set text_color(c:uint):void   
  76.         {   
  77.             _text_color=c;   
  78.         }   
  79.             
  80.         /**  
  81.          * Text size  
  82.          */   
  83.         public function get text_size():int   
  84.         {   
  85.             return _text_size;   
  86.         }   
  87.             
  88.         public function set text_size(n:int):void   
  89.         {   
  90.             _text_size=n;   
  91.         }   
  92.             
  93.         /**  
  94.          * The font  
  95.          */   
  96.         public function get font():String   
  97.         {   
  98.             return _font;   
  99.         }   
  100.             
  101.         public function set font(s:String):void   
  102.         {   
  103.             _font=s;   
  104.         }   
  105.             
  106.         /**  
  107.          * The text  
  108.          */   
  109.         public function get tool_text():String   
  110.         {   
  111.             return _tool_text;   
  112.         }   
  113.             
  114.         public function set tool_text(s:String):void   
  115.         {   
  116.             _tool_text=s;   
  117.         }   
  118.             
  119.         /**  
  120.          * The text  
  121.          */   
  122.         public function get field_txt():TextField   
  123.         {   
  124.             return _field_txt;   
  125.         }   
  126.             
  127.         public function set field_txt(t:TextField):void   
  128.         {   
  129.             _field_txt=t;   
  130.         }   
  131.             
  132.         /**  
  133.          * The alpha color  
  134.          */   
  135.         public function get alpha_color():Number   
  136.         {   
  137.             return _alpha_color;   
  138.         }   
  139.             
  140.         public function set alpha_color(n:Number):void   
  141.         {   
  142.             _alpha_color=n;   
  143.         }   
  144.             
  145.         /**  
  146.          * Init the class  
  147.          *  
  148.          * @param        .evt                Event  
  149.          */   
  150.         private function init(evt:Event):void   
  151.         {   
  152.             removeEventListener(Event.ADDED_TO_STAGE,init);   
  153.                 
  154.             father=parent as MovieClip;   
  155.                 
  156.             createHolder();   
  157.             createTextField();   
  158.             createBackground();   
  159.             fixPosition();   
  160.             fadeIn();   
  161.             addEventListener(Event.ENTER_FRAME,addMovement);   
  162.         }   
  163.             
  164.         /**  
  165.          * Container MovieClip creation  
  166.          *  
  167.          */   
  168.         private function createHolder():void   
  169.         {   
  170.             holder_mc=new MovieClip();   
  171.             addChild(holder_mc);   
  172.         }   
  173.             
  174.         /**  
  175.          * TextField tooltip creation  
  176.          *  
  177.          */   
  178.         private function createTextField():void   
  179.         {   
  180.             field_txt=new TextField();   
  181.             field_txt.multiline=true;   
  182.             field_txt.selectable=false;   
  183.             field_txt.embedFonts=true;   
  184.             field_txt.antiAliasType=AntiAliasType.ADVANCED;   
  185.             field_txt.autoSize=TextFieldAutoSize.LEFT;   
  186.             field_txt.defaultTextFormat=getFormat();   
  187.             field_txt.htmlText=tool_text+"  ";   
  188.             field_txtfield_txt.width=field_txt.textWidth+10;   
  189.             field_txtfield_txt.height=field_txt.textHeight+20;   
  190.             holder_mc.addChild(field_txt);   
  191.         }   
  192.             
  193.         /**  
  194.          * Get a text format  
  195.          *  
  196.          * @return        
     TextFormat  
    the textfield's format of tooltip  
  197.          */   
  198.         private function getFormat():TextFormat   
  199.         {   
  200.             var format:TextFormat=new TextFormat();   
  201.             format.font=font;   
  202.             format.size=text_size;   
  203.             format.color=text_color;   
  204.             return format;   
  205.         }   
  206.             
  207.         /**  
  208.          * Background MovieClip creation  
  209.          *  
  210.          */   
  211.         private function createBackground():void   
  212.         {   
  213.             bg_mc=new MovieClip();   
  214.             bg_mc.graphics.beginFill(bg_color,alpha_color);   
  215.             bg_mc.graphics.drawRoundRect(-ratio,-ratio,field_txt
    .width+ratio*2,field_txt.height+ratio*2,ratio,ratio);   
  216.             holder_mc.addChild(bg_mc);   
  217.                 
  218.             holder_mc.swapChildren(field_txt,bg_mc);   
  219.         }   
  220.             
  221.         /**  
  222.          * Position the tooltip  
  223.          *  
  224.          */   
  225.         private function fixPosition():void   
  226.         {   
  227.             if(father.mouseX  <  stage.stageWidth/2)   
  228.                 x=father.mouseX;   
  229.             else   
  230.                 x=father.mouseX-width;   
  231.             if(father.mouseY  <  stage.stageHeight/2)   
  232.                 y=father.mouseY+height-ratio*2;   
  233.             else   
  234.                 y=father.mouseY-height;   
  235.         }   
  236.             
  237.         /**  
  238.          * Init fade-in section of tooltip  
  239.          *  
  240.          */   
  241.         private function fadeIn():void   
  242.         {   
  243.             bg_mc.addEventListener(Event.ENTER_FRAME,fadeInToolTip);   
  244.         }   
  245.             
  246.         /**  
  247.          * Fade-in of tooltip  
  248.          *  
  249.          * @param        .evt                Event  
  250.          */   
  251.         private function fadeInToolTip(evt:Event):void   
  252.         {   
  253.             var distance:Number=1-alpha;   
  254.             var inertial:Number=distance*.2;   
  255.             alpha+=inertial;   
  256.             if(Math.abs(distance)  <= .1)   
  257.             {   
  258.                 alpha=1;   
  259.                 bg_mc.removeEventListener(Event.ENTER_FRAME,fadeInToolTip);   
  260.             }   
  261.         }   
  262.             
  263.         /**  
  264.          * Movement of tooltip  
  265.          *  
  266.          * @param        .evt                Event  
  267.          */   
  268.         private function addMovement(evt:Event):void   
  269.         {   
  270.             if(father.mouseX  <  stage.stageWidth/2)   
  271.                 x=father.mouseX;   
  272.             else   
  273.                 x=father.mouseX-width+ratio*2;   
  274.             if(father.mouseY  <  stage.stageHeight/2)   
  275.                 y=father.mouseY+height-ratio*2;   
  276.             else   
  277.                 y=father.mouseY-height;   
  278.                     
  279.             if(x  >  stage.stageWidth-width)   
  280.                 x=stage.stageWidth-width;   
  281.             if(x  <  ratio*2)   
  282.                 x=ratio*2;   
  283.         }   
  284.             
  285.         /**  
  286.          * Remove this instance  
  287.          *  
  288.          */   
  289.         public function destroy():void   
  290.         {   
  291.             removeEventListener(Event.ENTER_FRAME,addMovement);   
  292.             bg_mc.removeEventListener(Event.ENTER_FRAME,fadeInToolTip);   
  293.             father.removeChild(this);   
  294.         }   
  295.     }   
  296. }   

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器