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