PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/flash/Annotation.hx

http://github.com/brentp/gobe
Haxe | 191 lines | 165 code | 14 blank | 12 comment | 11 complexity | 48ecacd91e4e019952102eea60f54419 MD5 | raw file
  1. import flash.display.Sprite;
  2. import flash.display.Shape;
  3. import flash.events.MouseEvent;
  4. import flash.events.Event;
  5. import flash.geom.Point;
  6. import flash.utils.Timer;
  7. import flash.events.TimerEvent;
  8. import flash.external.ExternalInterface;
  9. import Gobe;
  10. import Track;
  11. import Glyph;
  12. class Edge extends Sprite {
  13. public var a:Annotation;
  14. public var b:Annotation;
  15. public var strength:Float;
  16. public var i:Int;
  17. public var drawn:Bool;
  18. public function new(a:Annotation, b:Annotation, s:Float){
  19. super();
  20. this.a = a; this.b = b; this.strength = s;
  21. this.drawn = false;
  22. this.addEventListener(MouseEvent.CLICK, onClick);
  23. this.visible = false;
  24. }
  25. public function draw(?force:Bool=false){
  26. if(this.drawn){
  27. this.visible = true;
  28. return;
  29. }
  30. var g = this.graphics;
  31. g.clear();
  32. var aa = this.a;
  33. var bb = this.b;
  34. if(aa.y > bb.y){
  35. aa = this.b;
  36. bb = this.a;
  37. }
  38. else {
  39. }
  40. // TODO use visible.
  41. if (this.drawn){
  42. // probably force because they want to draw every edge. but this is
  43. // already drawn, so leave it.
  44. this.drawn = force;
  45. return;
  46. }
  47. var ul = aa.localToGlobal(new flash.geom.Point(0, 0));
  48. var ur = aa.localToGlobal(new flash.geom.Point(aa.pxmax - aa.pxmin, 0));
  49. var ll = bb.localToGlobal(new flash.geom.Point(0, 0));
  50. var lr = bb.localToGlobal(new flash.geom.Point(bb.pxmax - bb.pxmin, 0));
  51. // alternating linestyle is to draw only lines on the y, not along the x
  52. g.lineStyle(0.0, 0.0);
  53. g.beginFill(aa.subtrack.fill_color, 0.3);
  54. g.moveTo(ul.x, ul.y);
  55. g.lineTo(ur.x, ur.y);
  56. g.lineStyle(0, aa.subtrack.fill_color);
  57. g.lineTo(lr.x, lr.y);
  58. g.lineStyle(0, 0.0);
  59. g.lineTo(ll.x, ll.y);
  60. g.lineStyle(0, aa.subtrack.fill_color);
  61. g.lineTo(ul.x, ul.y);
  62. g.endFill();
  63. this.drawn = true;
  64. this.visible = true;
  65. }
  66. public function onClick(e:MouseEvent){
  67. this.visible = false;
  68. }
  69. }
  70. // this is the base class for drawable annotations.
  71. class BaseAnnotation extends Sprite {
  72. public var ftype:String;
  73. public var is_hsp:Bool;
  74. public var id:String; // key for anntations hash.
  75. public var pxmin:Float;
  76. public var pxmax:Float;
  77. public var strand:Int; // -1, 0, or 1
  78. public var bpmin:Int;
  79. public var bpmax:Int;
  80. public var style:Style;
  81. public var track:Track;
  82. public var subtrack:SubTrack;
  83. public var track_id:String;
  84. public var h:Float;
  85. public var fname:String;
  86. public function new(l:Array<String>){
  87. super();
  88. //#id,track_id,start,end,type,strand,name, [color]
  89. this.id = l[0];
  90. this.track_id = l[1];
  91. this.bpmin = Std.parseInt(l[2]);
  92. this.bpmax = Std.parseInt(l[3]);
  93. this.ftype = l[4].toLowerCase();
  94. this.strand = l[5] == '-' ? -1 : (l[5] == '+' ? 1 : 0);
  95. this.fname = l.length < 6 ? l[0] : l[6];
  96. this.addEventListener(Event.ADDED_TO_STAGE, added);
  97. }
  98. public function draw(){}
  99. public inline function set_extents(){
  100. this.pxmin = track.rw2pix(this.bpmin);
  101. this.pxmax = track.rw2pix(this.bpmax);
  102. }
  103. public function added(e:Event){
  104. this.set_extents();
  105. this.draw();
  106. }
  107. }
  108. class Annotation extends BaseAnnotation {
  109. public var edges:Array<Int>;
  110. public var color:UInt;
  111. public var subanno_id:String; // whatever's after the ":"
  112. // empty_color is just a sentinel to show the color
  113. // hasn't been set since a uint can't be undefined.
  114. public static var empty_color:UInt = 0x000001;
  115. public function new(l:Array<String>){
  116. super(l);
  117. this.color = Annotation.empty_color;
  118. this.edges = new Array<Int>();
  119. this.is_hsp = this.ftype.substr(0, 3) == "hsp";
  120. this.addEventListener(MouseEvent.CLICK, onClick);
  121. // this only happens once its track is set.
  122. this.subanno_id = (this.track_id.indexOf(":") == -1) ? "" : this.track_id.split(":")[1];
  123. if (l.length > 7){
  124. //trace('setting color');
  125. this.color = Util.color_string_to_uint(l[7]);
  126. }
  127. }
  128. public override function draw(){
  129. this.x = pxmin;
  130. this.y = -this.subtrack.track_height / 2;
  131. this.h = style.feat_height * this.subtrack.track_height;
  132. Glyph.draw(this);
  133. }
  134. public function onClick(e:MouseEvent){
  135. var te = this.edges;
  136. for(i in 0 ... te.length){
  137. Gobe.edges[te[i]].draw();
  138. }
  139. //trace([this.id, this.fname, (this.bpmax + this.bpmin)/ 2, e.stageX, this.track_id].join(","));
  140. if(! e.shiftKey){
  141. Gobe.js_onclick(this.id, this.fname, this.ftype, this.bpmin, this.bpmax, this.track_id);
  142. }
  143. }
  144. }
  145. class Style {
  146. public var ftype:String; // *LOWER*case feature type.
  147. public var glyph:String; // arrow, box, diamond, ...
  148. public var fill_color:UInt;
  149. public var fill_alpha:Float;
  150. public var line_width:Float;
  151. public var line_color:UInt;
  152. public var arrow_len:Float;
  153. public var feat_height:Float; // in pct;
  154. public var zindex:Int;
  155. public var style_object:Dynamic;
  156. public function new(ftype:String, style_o:Dynamic){
  157. this.style_object = style_o;
  158. this.ftype = ftype.toLowerCase();
  159. this.glyph = style_o.glyph ? style_o.glyph: "generic";
  160. this.fill_color = Util.color_string_to_uint(style_o.fill_color);
  161. this.fill_alpha = style_o.fill_alpha ?
  162. Std.parseFloat(style_o.fill_alpha) : 1.0;
  163. this.line_width = style_o.line_width ?
  164. Std.parseFloat(style_o.line_width) : 0.1;
  165. this.line_color = style_o.line_color ?
  166. Util.color_string_to_uint(style_o.line_color) : 0xffffff;
  167. this.feat_height = style_o.height ?
  168. Std.parseFloat(style_o.height) : 0.5;
  169. this.arrow_len = style_o.arrow_len ?
  170. Std.parseFloat(style_o.arrow_len) : 0.0;
  171. this.zindex = style_o.z ? Std.parseInt(style_o.z) : 5;
  172. //trace(this.toString());
  173. }
  174. public function toString(){
  175. return "Style(" + this.ftype + "," + this.glyph + "," + this.fill_color + "," + this.fill_alpha +")";
  176. }
  177. }