ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI/Canvas.pm
Revision: 1.1
Committed: Sat Jul 21 16:07:53 2007 UTC (16 years, 11 months ago) by root
Branch: MAIN
Log Message:
the beginning of a (very simple) canvas widget

File Contents

# User Rev Content
1 root 1.1 package CFPlus::UI::Canvas;
2    
3     use strict;
4     use utf8;
5    
6     use List::Util qw(max min);
7    
8     use CFPlus::OpenGL;
9    
10     our @ISA = CFPlus::UI::Fixed::;
11    
12     sub new {
13     my ($class, %arg) = @_;
14    
15     my $items = delete $arg{items};
16    
17     my $self = $class->SUPER::new (
18     items => [],
19     @_,
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     ((max $w, $self->{item_max_w}), (max $h, $self->{item_max_h}))
52     }
53    
54     my %GLTYPE = (
55     lines => GL_LINES,
56     line_strip => GL_LINE_STRIP,
57     line_loop => GL_LINE_LOOP,
58     quads => GL_QUADS,
59     quad_strip => GL_QUAD_STRIP,
60     triangles => GL_TRIANGLES,
61     triangle_strip => GL_TRIANGLE_STRIP,
62     triangle_fan => GL_TRIANGLE_FAN,
63     polygon => GL_POLYGON,
64     );
65    
66     sub _draw {
67     my ($self) = @_;
68    
69     $self->SUPER::_draw;
70    
71     for my $item (@{ $self->{items} }) {
72     glPushMatrix;
73     glScale $self->{w}, $self->{h} if $item->{coord_mode} eq "rel";
74    
75     glColor @{ $item->{color} };
76     glLineWidth $item->{width} || 1.;
77     glPointSize $item->{size} || 1.;
78    
79     if (my $gltype = $GLTYPE{$item->{type}}) {
80     glBegin $gltype;
81     glVertex @$_ for @{$item->{coord}};
82     glEnd;
83     }
84    
85     glPopMatrix;
86     }
87     }
88    
89     1
90