ToolTip.as 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package iTop
  2. {
  3. import flash.display.*;
  4. import flash.geom.*;
  5. import flash.text.TextField;
  6. import flash.text.TextFormat;
  7. import flash.text.TextFieldAutoSize;
  8. import flash.text.TextLineMetrics;
  9. import flash.display.BlendMode;
  10. import flash.utils.Timer;
  11. import flash.events.MouseEvent;
  12. public class ToolTip extends Sprite
  13. {
  14. private var _tip:String;
  15. // You'll need this for proper text formatting
  16. private var _tf:TextField = new TextField();
  17. private var _format:TextFormat = new TextFormat();
  18. private static const ROUND:Number = 10;
  19. private static const HEIGHT:Number = 25;
  20. private static const FONT_SIZE:uint = 12;
  21. private static const FONT:String = 'Arial';
  22. private static const PADDING:Number = 5;
  23. private static const MIN_ALPHA:Number = 0.0;
  24. private static const ALPHA_INC:Number = 0.1;
  25. private static const MAX_ALPHA:Number = 1;
  26. private static const REFRESH:Number = (MAX_ALPHA - MIN_ALPHA) / ALPHA_INC;
  27. private static const APPEAR_TIMEOUT = 1000; // ms
  28. // For appearence, fading in and out
  29. public var timer:Timer;
  30. public function ToolTip( tip:String )
  31. {
  32. // Hold onto the tip for posterity
  33. _tip = tip;
  34. // This ensures the textfield inherits this class's
  35. // alpha property. Very important because otherwise tf
  36. // would always have an alpha of 1 meaning it will always be visible
  37. this.blendMode = BlendMode.LAYER;
  38. _format.size = FONT_SIZE;
  39. _format.font = FONT;
  40. // Make sure the text behaves and looks
  41. // the way text on a button should
  42. _tf.defaultTextFormat = _format;
  43. // Always call defaultTextFormat before setting text otherwise
  44. // the text doesn't use the formatting defined in tf
  45. _tf.autoSize = TextFieldAutoSize.LEFT;
  46. // You have to set autoSize to TextFieldAutoSize.LEFT
  47. // for box.textWidth to be accurate
  48. _tf.multiline = true;
  49. _tf.htmlText = tip;
  50. _tf.selectable = false;
  51. _tf.x += PADDING;
  52. _tf.y += PADDING;
  53. addChild( _tf );
  54. // Draw the background
  55. graphics.beginFill( 0xEEEE99, 0.95 );
  56. graphics.drawRoundRect( 0, 0, _tf.textWidth+PADDING*4, _tf.textHeight+PADDING*4, ROUND );
  57. graphics.endFill();
  58. this.alpha = MIN_ALPHA;
  59. }
  60. // You have to call this after
  61. // the tooltip has been added to the
  62. // display list
  63. public function start():void
  64. {
  65. this.parent.addEventListener( MouseEvent.MOUSE_OVER, mouse_over );
  66. }
  67. public function mouse_over( e:MouseEvent ):void
  68. {
  69. // Make the tooltip appear smoothly after a delay
  70. if (this.timer != null)
  71. {
  72. this.timer.stop();
  73. }
  74. this.timer = new Timer( APPEAR_TIMEOUT, 1 );
  75. this.timer.addEventListener( "timer", appear );
  76. this.timer.start();
  77. this.parent.addEventListener( MouseEvent.MOUSE_OUT, mouse_out );
  78. }
  79. public function mouse_out( e:MouseEvent ):void
  80. {
  81. var fadeSpeed:Number = 500 / REFRESH;
  82. if (this.timer != null)
  83. {
  84. this.timer.stop();
  85. }
  86. this.timer = new Timer( fadeSpeed, REFRESH );
  87. this.timer.addEventListener( "timer", fadeOut );
  88. this.timer.start();
  89. this.parent.removeEventListener( MouseEvent.MOUSE_OUT, mouse_out );
  90. }
  91. private function appear(i:uint):void
  92. {
  93. // The delay has elapsed, show (smoothly) the tooltip
  94. // Make sure that the tooltip always appears at se same scale 1:1 even
  95. // if the whole scene is zoomed out
  96. scaleX = 1 / this.parent.parent.scaleX;
  97. scaleY = 1 / this.parent.parent.scaleY;
  98. if (this.timer != null)
  99. {
  100. this.timer.stop();
  101. }
  102. // Reuse the time for the fadeIn
  103. this.parent.setChildIndex( this, this.parent.numChildren-1 )
  104. // Move the tool tip to the top!
  105. var fadeSpeed:Number = 500 / REFRESH;
  106. this.alpha = MIN_ALPHA;
  107. if (this.timer != null)
  108. {
  109. this.timer.stop();
  110. }
  111. this.timer = new Timer( fadeSpeed, REFRESH );
  112. this.timer.addEventListener( "timer", fadeIn );
  113. this.timer.start();
  114. this.parent.addEventListener( MouseEvent.MOUSE_OUT, mouse_out );
  115. }
  116. private function fadeIn( i:uint ):void
  117. {
  118. if (this.alpha < (1.0 - ALPHA_INC))
  119. {
  120. this.alpha += ALPHA_INC;
  121. }
  122. else
  123. {
  124. this.alpha = 1.0;
  125. }
  126. //trace("++ Tooltip alpha: "+this.alpha+" ALPHA_INC:"+ALPHA_INC);
  127. }
  128. private function fadeOut( i:uint ):void
  129. {
  130. if (this.alpha > ALPHA_INC)
  131. {
  132. this.alpha -= ALPHA_INC;
  133. }
  134. else
  135. {
  136. this.alpha = 0.0;
  137. }
  138. //trace("-- Tooltip alpha: "+this.alpha+" ALPHA_INC:"+ALPHA_INC);
  139. }
  140. }
  141. }