Keys.as 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package elements.labels {
  2. import charts.Base;
  3. import charts.ObjectCollection;
  4. import flash.display.Sprite;
  5. import flash.text.TextField;
  6. import flash.text.TextFormat;
  7. import org.flashdevelop.utils.FlashConnect;
  8. public class Keys extends Sprite {
  9. private var _height:Number = 0;
  10. private var count:Number = 0;
  11. public var colours:Array;
  12. public function Keys( stuff:ObjectCollection )
  13. {
  14. this.colours = new Array();
  15. var key:Number = 0;
  16. for each( var b:Base in stuff.sets )
  17. {
  18. for each( var o:Object in b.get_keys() ) {
  19. this.make_key( o.text, o['font-size'], o.colour );
  20. this.colours.push( o.colour );
  21. key++;
  22. }
  23. }
  24. this.count = key;
  25. }
  26. // each key is a MovieClip with text on it
  27. private function make_key( text:String, font_size:Number, colour:Number ) : void
  28. {
  29. var tf:TextField = new TextField();
  30. tf.text = text;
  31. var fmt:TextFormat = new TextFormat();
  32. fmt.color = colour;
  33. fmt.font = "Verdana";
  34. fmt.size = font_size;
  35. fmt.align = "left";
  36. tf.setTextFormat(fmt);
  37. tf.autoSize="left";
  38. this.addChild(tf);
  39. }
  40. //
  41. // draw the colour block for the data set
  42. //
  43. private function draw_line( x:Number, y:Number, height:Number, colour:Number ):Number {
  44. y += (height / 2);
  45. this.graphics.beginFill( colour, 100 );
  46. this.graphics.drawRect( x, y - 1, 10, 2 );
  47. this.graphics.endFill();
  48. return x+12;
  49. }
  50. // shuffle the keys into place, keeping note of the total
  51. // height the key block has taken up
  52. public function resize( x:Number, y:Number ):void {
  53. if( this.count == 0 )
  54. return;
  55. this.x = x;
  56. this.y = y;
  57. var height:Number = 0;
  58. var x:Number = 0;
  59. var y:Number = 0;
  60. this.graphics.clear();
  61. for( var i:Number=0; i<this.numChildren; i++ )
  62. {
  63. var width:Number = this.getChildAt(i).width;
  64. if( ( this.x + x + width + 12 ) > this.stage.stageWidth )
  65. {
  66. // it is past the edge of the stage, so move it down a line
  67. x = 0;
  68. y += this.getChildAt(i).height;
  69. height += this.getChildAt(i).height;
  70. }
  71. this.draw_line( x, y, this.getChildAt(i).height, this.colours[i] );
  72. x += 12;
  73. this.getChildAt(i).x = x;
  74. this.getChildAt(i).y = y;
  75. // move next key to the left + some padding between keys
  76. x += width + 10;
  77. }
  78. // Ugly code:
  79. height += this.getChildAt(0).height;
  80. this._height = height;
  81. }
  82. public function get_height() : Number {
  83. return this._height;
  84. }
  85. public function die(): void {
  86. this.colours = null;
  87. this.graphics.clear();
  88. while ( this.numChildren > 0 )
  89. this.removeChildAt(0);
  90. }
  91. }
  92. }