PageRenderTime 60ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/flash/Glyph.hx

http://github.com/brentp/gobe
Haxe | 227 lines | 183 code | 33 blank | 11 comment | 9 complexity | f27d2d7610f8df8b39000ee5abaae258 MD5 | raw file
  1. import flash.events.Event;
  2. import flash.display.Sprite;
  3. import flash.display.Bitmap;
  4. import flash.display.Loader;
  5. import flash.net.URLRequest;
  6. import StringTools;
  7. import Annotation;
  8. import Gobe;
  9. /*
  10. extra graphics for Gobe, modeled after GBROWSE <http://gmod.org/wiki/Glyphs_and_Glyph_Options>
  11. feature_type will maps to glyph in the stylesheet
  12. */
  13. class Glyph {
  14. static public function draw(a:Annotation) {
  15. // factory method, dispatch draw function based on glyph type
  16. var glyph = a.style.glyph;
  17. if (StringTools.startsWith(glyph, "avatar")) {
  18. var img_src = glyph.split("(\"")[1].split("\")")[0];
  19. var avatar = new Avatar(a, img_src);
  20. avatar.draw(a);
  21. }
  22. else {
  23. switch (glyph) {
  24. case "circle": Circle.draw(a);
  25. case "cross": Cross.draw(a);
  26. case "square": Square.draw(a);
  27. case "star": Star.draw(a);
  28. case "mask": Mask.draw(a);
  29. case "text": TextGlyph.draw(a);
  30. case "caret": Caret.draw(a);
  31. default: Box.draw(a);
  32. }
  33. }
  34. }
  35. public static inline function get_color(a:Annotation):UInt{
  36. // try to get the color. if it's not set, then get it from the style.
  37. var c:UInt;
  38. if(a.color != 0x000001){
  39. c = a.color;
  40. }
  41. else {
  42. c = a.is_hsp ? a.subtrack.fill_color : a.style.fill_color;
  43. }
  44. return c;
  45. }
  46. }
  47. class Box {
  48. static public function draw(a:Annotation) {
  49. var tw = a.pxmax - a.pxmin;
  50. var alen = a.style.arrow_len * tw * (a.strand == 0 ? 1 : a.strand);
  51. var xstart = (a.strand != -1) ? 0 : tw;
  52. var xend = (a.strand != -1) ? tw : 0;
  53. var g = a.graphics;
  54. var c = Glyph.get_color(a);
  55. g.clear();
  56. g.lineStyle(a.style.line_width, c, 0.3);
  57. g.moveTo(xstart, a.h/2);
  58. var m = new flash.geom.Matrix();
  59. m.createGradientBox(tw, a.h/3, 290, 0, -a.h/6);
  60. g.beginGradientFill(flash.display.GradientType.LINEAR,
  61. [Util.color_shift(c, -24), Util.color_shift(c, 24)],
  62. [a.style.fill_alpha, a.style.fill_alpha],
  63. [0x00, 0xFF], m);
  64. g.lineTo(xstart, -a.h/2);
  65. g.lineTo(xend - alen, -a.h/2);
  66. g.lineTo(xend, 0);
  67. g.lineTo(xend - alen, a.h/2);
  68. g.endFill();
  69. }
  70. }
  71. class Circle {
  72. static public function draw(a:Annotation) {
  73. var g = a.graphics;
  74. var c = Glyph.get_color(a);
  75. var x = (a.pxmax - a.pxmin)/2;
  76. g.beginFill(c, a.style.fill_alpha);
  77. g.drawCircle(x, 0, a.h/2);
  78. g.endFill();
  79. }
  80. }
  81. class Cross {
  82. static public function draw(a:Annotation) {
  83. var g = a.graphics;
  84. var c = Glyph.get_color(a);
  85. var x = (a.pxmax - a.pxmin)/2;
  86. g.beginFill(c, a.style.fill_alpha);
  87. g.drawRect(x-a.h/2, -a.h/6, a.h, a.h/3);
  88. g.endFill();
  89. g.beginFill(c, a.style.fill_alpha);
  90. g.drawRect(x-a.h/6, -a.h/2, a.h/3, a.h);
  91. g.endFill();
  92. }
  93. }
  94. class Star {
  95. static public function draw(a:Annotation) {
  96. var g = a.graphics;
  97. var c = Glyph.get_color(a);
  98. var x = (a.pxmax - a.pxmin)/2; // x-coordinate of center point
  99. var y = -a.h/2; // y-coordinate of top of the star
  100. var r = a.h/2; // radius of the star
  101. // code from <http://lionpath.com/haxeflashtutorial/release/library.html>
  102. var phi : Float = 2*Math.PI/5; // 2/5 of a circle
  103. var r2 : Float = ((Math.cos(phi*1)*r)-r)*((Math.sin(phi*2)*r)-0)/(Math.cos(phi*2)*r-r);
  104. r2 = Math.sqrt(r2*r2+Math.pow(Math.cos(phi)*r,2));
  105. g.beginFill(c, a.style.fill_alpha);
  106. g.moveTo(x,y);
  107. g.lineTo(x+Math.sin(0.5*phi)*r2,y+r-Math.cos(0.5*phi)*r2);
  108. g.lineTo(x+Math.sin(1*phi)*r,y+r-Math.cos(1*phi)*r);
  109. g.lineTo(x+Math.sin(1.5*phi)*r2,y+r-Math.cos(1.5*phi)*r2);
  110. g.lineTo(x+Math.sin(2*phi)*r,y+r-Math.cos(2*phi)*r);
  111. g.lineTo(x+Math.sin(2.5*phi)*r2,y+r-Math.cos(2.5*phi)*r2);
  112. g.lineTo(x+Math.sin(3*phi)*r,y+r-Math.cos(3*phi)*r);
  113. g.lineTo(x+Math.sin(3.5*phi)*r2,y+r-Math.cos(3.5*phi)*r2);
  114. g.lineTo(x+Math.sin(4*phi)*r,y+r-Math.cos(4*phi)*r);
  115. g.lineTo(x+Math.sin(4.5*phi)*r2,y+r-Math.cos(4.5*phi)*r2);
  116. g.endFill();
  117. }
  118. }
  119. class Square {
  120. static public function draw(a:Annotation) {
  121. var g = a.graphics;
  122. var c = Glyph.get_color(a);
  123. var x = (a.pxmax - a.pxmin)/2;
  124. g.beginFill(c, a.style.fill_alpha);
  125. g.drawRoundRect(x-a.h/2, -a.h/2, a.h, a.h, .2*a.h);
  126. g.endFill();
  127. }
  128. }
  129. class TextGlyph extends Sprite {
  130. public static function draw(a:Annotation){
  131. var t = new MTextField();
  132. a.addChild(t);
  133. t.htmlText = "<p>" + a.fname + "</p>";
  134. //t.y = -a.h/2;
  135. t.autoSize = flash.text.TextFieldAutoSize.LEFT;
  136. // start at the left edge.
  137. //t.x = -t.width;
  138. t.multiline = false;
  139. var s = a.style;
  140. var color = s.style_object.color ? s.style_object.color : "#" + StringTools.hex(s.line_color);
  141. var fw = s.style_object.fontWeight ? s.style_object.fontWeight : 'normal';
  142. var fs = s.style_object.fontSize ? Std.parseInt(s.style_object.fontSize) : Gobe.fontSize;
  143. t.styleSheet.setStyle('p', {fontSize: fs, display: 'inline', color: color, fontWeight: fw});
  144. t.y -= t.height / 2;
  145. }
  146. }
  147. class Mask {
  148. static public function draw(a:Annotation) {
  149. var g = a.graphics;
  150. var c = Glyph.get_color(a);
  151. var tw = a.pxmax - a.pxmin;
  152. g.beginFill(c, a.style.fill_alpha);
  153. g.drawRect(0, -a.h/2, tw, a.h);
  154. g.endFill();
  155. }
  156. }
  157. class Avatar extends Sprite {
  158. var feature:Annotation;
  159. private var _loader:Loader;
  160. public function new(a:Annotation, path:String) {
  161. super();
  162. this._loader = new Loader();
  163. this._loader.load(new URLRequest(path));
  164. this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
  165. this.feature = a;
  166. }
  167. private function onComplete(e:Event) {
  168. var image = cast(this._loader.content, Bitmap);
  169. image.smoothing = true;
  170. image.width = image.height = this.feature.h;
  171. this.addChild(image);
  172. }
  173. public function draw(a:Annotation) {
  174. var x = (a.pxmax - a.pxmin)/2;
  175. this.x = x-a.h/2;
  176. this.y = -a.h/2;
  177. a.addChild(this);
  178. }
  179. }
  180. class Caret {
  181. static public function draw(a:Annotation) {
  182. var g = a.graphics;
  183. var c = Glyph.get_color(a);
  184. var tw = a.pxmax - a.pxmin;
  185. var xstart = 0;
  186. var xend = tw;
  187. var xmiddle = tw / 2;
  188. g.lineStyle(a.style.line_width, c);
  189. g.moveTo(0, -a.h / 2);
  190. g.lineTo(xmiddle, -a.h);
  191. g.lineTo(xend, -a.h / 2);
  192. }
  193. }