« blib/lib/Bio/Graphics/Glyph/ex.pm
↑ Summary
blib/lib/Bio/Graphics/Glyph/heterogeneous_segments.pm »
| 1 | package Bio::Graphics::Glyph::diamond; | ||
| 2 | # DAS-compatible package to use for drawing a colored diamond | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use base qw(Bio::Graphics::Glyph::point_glyph); | ||
| 6 | |||
| 7 | sub my_description { | ||
| 8 | return <<END; | ||
| 9 | This glyph draws a diamond of fixed size, positioned in the center of | ||
| 10 | the feature. The height and width of the diamond are set by the | ||
| 11 | "height" option. | ||
| 12 | END | ||
| 13 | } | ||
| 14 | sub my_options { | ||
| 15 | { | ||
| 16 | point => [ | ||
| 17 | 'boolean', | ||
| 18 | undef, | ||
| 19 | 'Draw the glyph at a fixed point at the center of the feature.'], | ||
| 20 | fallback_to_rectangle => [ | ||
| 21 | 'boolean', | ||
| 22 | undef, | ||
| 23 | 'Draw a diamond if the feature is 1 base long.', | ||
| 24 | 'Draw a standard box if the feature is >1 base long.'], | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | sub draw_component { | ||
| 29 | my $self = shift; | ||
| 30 | my $gd = shift; | ||
| 31 | my $fg = $self->fgcolor; | ||
| 32 | |||
| 33 | # find the center and vertices | ||
| 34 | my ($x1,$y1,$x2,$y2) = $self->calculate_boundaries(@_); | ||
| 35 | |||
| 36 | my $xmid = ($x1+$x2)/2; | ||
| 37 | my $ymid = ($y1+$y2)/2; | ||
| 38 | |||
| 39 | my $h = $self->option('height')/2; | ||
| 40 | $y1 = $ymid - $h; | ||
| 41 | $y2 = $ymid + $h; | ||
| 42 | |||
| 43 | # if it's a point-like feature, then draw symmetrically | ||
| 44 | # around the midpoint | ||
| 45 | if ($self->option('point') || $x2 - $x1 < $h*2) { | ||
| 46 | $x1 = $xmid - $h; | ||
| 47 | $x2 = $xmid + $h; | ||
| 48 | } | ||
| 49 | |||
| 50 | elsif ($self->option('fallback_to_rectangle')) { | ||
| 51 | return $self->SUPER::draw_component($gd,@_); | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | my $poly_pkg = $self->polygon_package; | ||
| 56 | my $polygon = $poly_pkg->new(); | ||
| 57 | $polygon->addPt($x1,$ymid); | ||
| 58 | $polygon->addPt($xmid,$y1); | ||
| 59 | $polygon->addPt($x2,$ymid); | ||
| 60 | $polygon->addPt($xmid,$y2); | ||
| 61 | |||
| 62 | # Have to draw TWO polygons for fills in order to get an outline | ||
| 63 | # because filledPolygon in GD croaks with extra parameters (and | ||
| 64 | # doesn't support drawing of stroke anyways). | ||
| 65 | if (my $c = $self->bgcolor) { | ||
| 66 | $gd->filledPolygon($polygon,$c); | ||
| 67 | $gd->polygon($polygon,$fg); | ||
| 68 | } else { | ||
| 69 | $gd->polygon($polygon,$fg); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | 1; | ||
| 75 | |||
| 76 | __END__ |