ToolTip.as 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package iTop
  2. {
  3. import flash.display.Sprite;
  4. import flash.text.TextField;
  5. import flash.text.TextFormat;
  6. import flash.text.TextFieldAutoSize;
  7. import flash.text.TextLineMetrics;
  8. import flash.display.BlendMode;
  9. import flash.utils.Timer;
  10. import flash.events.MouseEvent;
  11. public class ToolTip extends Sprite
  12. {
  13. private var _tip:String;
  14. // You'll need this for proper text formatting
  15. private var _tf:TextField = new TextField();
  16. private var _format:TextFormat = new TextFormat();
  17. private static const ROUND:Number = 2;
  18. private static const HEIGHT:Number = 25;
  19. private static const FONT_SIZE:uint = 12;
  20. private static const FONT:String = 'Arial';
  21. private static const PADDING:Number = 5;
  22. private static const MIN_ALPHA:Number = 0;
  23. private static const ALPHA_INC:Number = .1;
  24. private static const MAX_ALPHA:Number = 1;
  25. private static const REFRESH:Number = MAX_ALPHA / ALPHA_INC;
  26. // For fading in and out
  27. private var _timer:Timer;
  28. public function ToolTip( tip:String )
  29. {
  30. // Hold onto the tip for posterity
  31. _tip = tip;
  32. // This ensures the textfield inherits this class's
  33. // alpha property. Very important because otherwise tf
  34. // would always have an alpha of 1 meaning it will always be visible
  35. this.blendMode = BlendMode.LAYER;
  36. _format.size = FONT_SIZE;
  37. _format.font = FONT;
  38. // Make sure the text behaves and looks
  39. // the way text on a button should
  40. _tf.defaultTextFormat = _format;
  41. // Always call defaultTextFormat before setting text otherwise
  42. // the text doesn't use the formatting defined in tf
  43. _tf.autoSize = TextFieldAutoSize.LEFT;
  44. // You have to set autoSize to TextFieldAutoSize.LEFT
  45. // for box.textWidth to be accurate
  46. _tf.multiline = true;
  47. _tf.htmlText = tip;
  48. _tf.selectable = false;
  49. _tf.x += PADDING;
  50. _tf.y += PADDING;
  51. addChild( _tf );
  52. // Draw the background
  53. graphics.beginFill( 0xF6F6F1, 1 );
  54. graphics.drawRoundRect( 0, 0, _tf.textWidth+PADDING*4, _tf.textHeight+PADDING*4, ROUND );
  55. graphics.endFill();
  56. this.alpha = MIN_ALPHA;
  57. }
  58. // You have to call this after
  59. // the tooltip has been added to the
  60. // display list
  61. public function start():void
  62. {
  63. this.parent.addEventListener( MouseEvent.MOUSE_OVER, mouse_over );
  64. }
  65. public function mouse_over( e:MouseEvent ):void
  66. {
  67. this.parent.setChildIndex( this, this.parent.numChildren-1 )
  68. // Move the tool tip to the top!
  69. var fadeSpeed:Number = 500 / REFRESH;
  70. _timer = new Timer( fadeSpeed, REFRESH );
  71. _timer.addEventListener( "timer", fadeIn );
  72. _timer.start();
  73. this.parent.addEventListener( MouseEvent.MOUSE_OUT, mouse_out );
  74. }
  75. public function mouse_out( e:MouseEvent ):void
  76. {
  77. var fadeSpeed:Number = 500 / REFRESH;
  78. _timer = new Timer( fadeSpeed, REFRESH );
  79. _timer.addEventListener( "timer", fadeOut );
  80. _timer.start();
  81. }
  82. private function fadeIn( i:uint ):void
  83. {
  84. this.alpha += ALPHA_INC;
  85. }
  86. private function fadeOut( i:uint ):void
  87. {
  88. this.alpha -= ALPHA_INC;
  89. }
  90. }
  91. }