| 1 |
package DC::UI::Canvas; |
| 2 |
|
| 3 |
use common::sense; |
| 4 |
|
| 5 |
use List::Util qw(max min); |
| 6 |
|
| 7 |
use DC::OpenGL; |
| 8 |
|
| 9 |
our @ISA = DC::UI::Fixed::; |
| 10 |
|
| 11 |
sub new { |
| 12 |
my ($class, %arg) = @_; |
| 13 |
|
| 14 |
my $items = delete $arg{items}; |
| 15 |
|
| 16 |
my $self = $class->SUPER::new ( |
| 17 |
items => [], |
| 18 |
@_, |
| 19 |
); |
| 20 |
|
| 21 |
$self->add (@$items) |
| 22 |
if $items && @$items; |
| 23 |
|
| 24 |
$self |
| 25 |
} |
| 26 |
|
| 27 |
sub add_items { |
| 28 |
my ($self, @items) = @_; |
| 29 |
|
| 30 |
push @{$self->{items}}, @items; |
| 31 |
|
| 32 |
my @coords = |
| 33 |
map @{ $_->{coord} }, |
| 34 |
grep $_->{coord_mode} ne "rel", |
| 35 |
@{ $self->{items} }; |
| 36 |
|
| 37 |
$self->{item_max_w} = max map $_->[0], @coords; |
| 38 |
$self->{item_max_h} = max map $_->[1], @coords; |
| 39 |
|
| 40 |
$self->realloc; |
| 41 |
|
| 42 |
map $_+0, @items |
| 43 |
} |
| 44 |
|
| 45 |
sub size_request { |
| 46 |
my ($self) = @_; |
| 47 |
|
| 48 |
my ($w, $h) = $self->SUPER::size_request; |
| 49 |
|
| 50 |
( |
| 51 |
(max $w, $self->{item_max_w}), |
| 52 |
(max $h, $self->{item_max_h}), |
| 53 |
) |
| 54 |
} |
| 55 |
|
| 56 |
my %GLTYPE = ( |
| 57 |
lines => GL_LINES, |
| 58 |
line_strip => GL_LINE_STRIP, |
| 59 |
line_loop => GL_LINE_LOOP, |
| 60 |
quads => GL_QUADS, |
| 61 |
quad_strip => GL_QUAD_STRIP, |
| 62 |
triangles => GL_TRIANGLES, |
| 63 |
triangle_strip => GL_TRIANGLE_STRIP, |
| 64 |
triangle_fan => GL_TRIANGLE_FAN, |
| 65 |
polygon => GL_POLYGON, |
| 66 |
); |
| 67 |
|
| 68 |
sub _draw { |
| 69 |
my ($self) = @_; |
| 70 |
|
| 71 |
$self->SUPER::_draw; |
| 72 |
|
| 73 |
for my $item (@{ $self->{items} }) { |
| 74 |
glPushMatrix; |
| 75 |
glScale $self->{w}, $self->{h} if $item->{coord_mode} eq "rel"; |
| 76 |
|
| 77 |
glColor @{ $item->{color} }; |
| 78 |
glLineWidth $item->{width} || 1.; |
| 79 |
glPointSize $item->{size} || 1.; |
| 80 |
|
| 81 |
if (my $gltype = $GLTYPE{$item->{type}}) { |
| 82 |
glBegin $gltype; |
| 83 |
glVertex @$_ for @{$item->{coord}}; |
| 84 |
glEnd; |
| 85 |
} |
| 86 |
|
| 87 |
glPopMatrix; |
| 88 |
} |
| 89 |
|
| 90 |
glLineWidth 1; |
| 91 |
glPointSize 1; |
| 92 |
} |
| 93 |
|
| 94 |
1 |
| 95 |
|