Tooltip.as 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package {
  2. import caurina.transitions.Tweener;
  3. import caurina.transitions.Equations;
  4. import flash.display.Sprite;
  5. import flash.geom.Point;
  6. import flash.geom.Rectangle;
  7. import flash.text.TextField;
  8. import flash.text.TextFormat;
  9. import flash.filters.DropShadowFilter;
  10. // import charts.Elements.Element;
  11. import com.serialization.json.JSON;
  12. import string.Utils;
  13. import string.Css;
  14. import object_helper;
  15. import charts.series.has_tooltip;
  16. public class Tooltip extends Sprite {
  17. // JSON style:
  18. private var style:Object;
  19. private var tip_style:Number;
  20. private var cached_elements:Array;
  21. private var tip_showing:Boolean;
  22. public var tip_text:String;
  23. public static const CLOSEST:Number = 0;
  24. public static const PROXIMITY:Number = 1;
  25. public static const NORMAL:Number = 2; // normal tooltip (ugh -- boring!!)
  26. public function Tooltip( json:Object )
  27. {
  28. //
  29. // we don't want mouseOver events for the
  30. // tooltip or any children (the text fields)
  31. //
  32. this.mouseEnabled = false;
  33. this.tip_showing = false;
  34. this.style = {
  35. shadow: true,
  36. rounded: 6,
  37. stroke: 2,
  38. colour: '#808080',
  39. background: '#f0f0f0',
  40. title: "color: #0000F0; font-weight: bold; font-size: 12;",
  41. body: "color: #000000; font-weight: normal; font-size: 12;",
  42. mouse: Tooltip.CLOSEST,
  43. text: "_default"
  44. };
  45. if( json )
  46. {
  47. this.style = object_helper.merge( json, this.style );
  48. }
  49. this.style.colour = Utils.get_colour( this.style.colour );
  50. this.style.background = Utils.get_colour( this.style.background );
  51. this.style.title = new Css( this.style.title );
  52. this.style.body = new Css( this.style.body );
  53. this.tip_style = this.style.mouse;
  54. this.tip_text = this.style.text;
  55. this.cached_elements = [];
  56. if( this.style.shadow==1 )
  57. {
  58. var dropShadow:DropShadowFilter = new flash.filters.DropShadowFilter();
  59. dropShadow.blurX = 4;
  60. dropShadow.blurY = 4;
  61. dropShadow.distance = 4;
  62. dropShadow.angle = 45;
  63. dropShadow.quality = 2;
  64. dropShadow.alpha = 0.5;
  65. // apply shadow filter
  66. this.filters = [dropShadow];
  67. }
  68. }
  69. public function make_tip( elements:Array ):void {
  70. this.graphics.clear();
  71. while( this.numChildren > 0 )
  72. this.removeChildAt(0);
  73. var height:Number = 0;
  74. var x:Number = 5;
  75. for each ( var e:has_tooltip in elements ) {
  76. var o:Object = this.make_one_tip(e, x);
  77. height = Math.max(height, o.height);
  78. x += o.width + 2;
  79. }
  80. this.graphics.lineStyle(this.style.stroke, this.style.colour, 1);
  81. this.graphics.beginFill(this.style.background, 1);
  82. this.graphics.drawRoundRect(
  83. 0,0,
  84. width+10, height + 5,
  85. this.style.rounded, this.style.rounded );
  86. }
  87. private function make_one_tip( e:has_tooltip, x:Number ):Object {
  88. var tt:String = e.get_tooltip();
  89. var lines:Array = tt.split( '<br>' );
  90. var top:Number = 5;
  91. var width:Number = 0;
  92. if ( lines.length > 1 ) {
  93. var title:TextField = this.make_title(lines.shift());
  94. title.mouseEnabled = false;
  95. title.x = x;
  96. title.y = top;
  97. top += title.height;
  98. width = title.width;
  99. this.addChild( title );
  100. }
  101. var text:TextField = this.make_body(lines.join( '\n' ));
  102. text.mouseEnabled = false;
  103. text.x = x;
  104. text.y = top;
  105. width = Math.max( width, text.width );
  106. this.addChild( text );
  107. top += text.height;
  108. return {width:width, height:top};
  109. }
  110. private function make_title( text:String ):TextField {
  111. var title:TextField = new TextField();
  112. title.mouseEnabled = false;
  113. title.htmlText = text;
  114. /*
  115. *
  116. * Start thinking about just using html formatting
  117. * instead of text format below. We could do away
  118. * with the title textbox entirely and let the user
  119. * use:
  120. * <b>title stuff</b><br>Here is the value
  121. *
  122. */
  123. var fmt:TextFormat = new TextFormat();
  124. fmt.color = this.style.title.color;
  125. fmt.font = "Verdana";
  126. fmt.bold = (this.style.title.font_weight=="bold");
  127. fmt.size = this.style.title.font_size;
  128. fmt.align = "right";
  129. title.setTextFormat(fmt);
  130. title.autoSize = "left";
  131. return title;
  132. }
  133. private function make_body( body:String ):TextField {
  134. var text:TextField = new TextField();
  135. text.mouseEnabled = false;
  136. text.htmlText = body;
  137. var fmt2:TextFormat = new TextFormat();
  138. fmt2.color = this.style.body.color;
  139. fmt2.font = "Verdana";
  140. fmt2.bold = (this.style.body.font_weight=="bold");
  141. fmt2.size = this.style.body.font_size;
  142. fmt2.align = "left";
  143. text.setTextFormat(fmt2);
  144. text.autoSize="left";
  145. return text;
  146. }
  147. private function get_pos( e:has_tooltip ):flash.geom.Point {
  148. var pos:Object = e.get_tip_pos();
  149. var x:Number = (pos.x + this.width + 16) > this.stage.stageWidth ? (this.stage.stageWidth - this.width - 16) : pos.x;
  150. var y:Number = pos.y;
  151. y -= 4;
  152. y -= (this.height + 10 ); // 10 == border size
  153. if( y < 0 )
  154. {
  155. // the tooltip has drifted off the top of the screen, move it down:
  156. y = 0;
  157. }
  158. return new flash.geom.Point(x, y);
  159. }
  160. private function show_tip( e:has_tooltip ):void {
  161. // remove the 'hide' tween
  162. Tweener.removeTweens( this );
  163. var p:flash.geom.Point = this.get_pos( e );
  164. if ( this.style.mouse == Tooltip.CLOSEST )
  165. {
  166. //
  167. // make the tooltip appear (if invisible)
  168. // and shoot to the correct position
  169. //
  170. this.visible = true;
  171. this.alpha = 1
  172. this.x = p.x;
  173. this.y = p.y;
  174. }
  175. else
  176. {
  177. // make the tooltip fade in gently
  178. this.tip_showing = true;
  179. tr.ace('show');
  180. this.alpha = 0
  181. this.visible = true;
  182. this.x = p.x;
  183. this.y = p.y;
  184. Tweener.addTween(
  185. this,
  186. {
  187. alpha:1,
  188. time:0.4,
  189. transition:Equations.easeOutExpo
  190. } );
  191. }
  192. }
  193. public function draw( e:has_tooltip ):void {
  194. if ( this.cached_elements[0] == e )
  195. {
  196. // if the tip is showing, don't make it
  197. // show again because this makes it flicker
  198. if( !this.tip_showing )
  199. this.show_tip(e);
  200. }
  201. else
  202. {
  203. // this is a new tooltip, tell
  204. // the old highlighted item to
  205. // return to ground state
  206. this.untip();
  207. // get the new text and recreate it
  208. this.cached_elements = [e];
  209. this.make_tip( [e] );
  210. this.show_tip(e);
  211. }
  212. }
  213. public function closest( elements:Array ):void {
  214. if( elements.length == 0 )
  215. return;
  216. if( this.is_cached( elements ) )
  217. return;
  218. this.untip();
  219. this.cached_elements = elements;
  220. this.tip();
  221. //
  222. //tr.ace( 'make new tooltip' );
  223. //tr.ace( Math.random() );
  224. //
  225. this.make_tip( elements );
  226. var p:flash.geom.Point = this.get_pos( elements[0] );
  227. this.visible = true;
  228. Tweener.addTween(this, { x:p.x, time:0.3, transition:Equations.easeOutExpo } );
  229. Tweener.addTween(this, { y:p.y, time:0.3, transition:Equations.easeOutExpo } );
  230. }
  231. //
  232. // TODO: if elements has 1 item and cached_elements has 2
  233. // one of which is in elements, this function
  234. // returns true which is wrong
  235. //
  236. private function is_cached( elements:Array ):Boolean {
  237. if ( this.cached_elements.length == 0 )
  238. return false;
  239. for each( var e:has_tooltip in elements )
  240. if ( this.cached_elements.indexOf(e) == -1 )
  241. return false;
  242. return true;
  243. }
  244. private function untip():void {
  245. for each( var e:has_tooltip in this.cached_elements )
  246. e.set_tip( false );
  247. }
  248. private function tip():void {
  249. for each( var e:has_tooltip in this.cached_elements )
  250. e.set_tip( true );
  251. }
  252. private function hideAway() : void {
  253. this.visible = false;
  254. this.untip();
  255. this.cached_elements = new Array();
  256. this.alpha = 1;
  257. }
  258. public function hide():void {
  259. this.tip_showing = false;
  260. tr.ace('hide tooltip');
  261. Tweener.addTween(this, { alpha:0, time:0.6, transition:Equations.easeOutExpo, onComplete:hideAway } );
  262. }
  263. public function get_tip_style():Number {
  264. return this.tip_style;
  265. }
  266. public function set_tip_style( i:Number ):void {
  267. this.tip_style = i;
  268. }
  269. public function die():void {
  270. this.filters = [];
  271. this.graphics.clear();
  272. while( this.numChildren > 0 )
  273. this.removeChildAt(0);
  274. this.style = null;
  275. this.cached_elements = null;
  276. }
  277. }
  278. }