ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.10 by root, Fri Apr 7 20:34:44 2006 UTC vs.
Revision 1.15 by elmex, Fri Apr 7 23:05:21 2006 UTC

1package Crossfire::Client::Widget; 1package Crossfire::Client::Widget;
2 2
3use strict; 3use strict;
4use SDL::OpenGL;
5use SDL::OpenGL::Constants;
4 6
5our $FOCUS; # the widget with current focus 7our $FOCUS; # the widget with current focus
6our %ACTIVE_WIDGETS; 8our @ACTIVE_WIDGETS;
7 9
8# class methods for events 10# class methods for events
9sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 11sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
10sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 12sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
11sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } 13sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS }
16 18
17 bless { @_ }, $class 19 bless { @_ }, $class
18} 20}
19 21
20sub activate { 22sub activate {
21 $ACTIVE_WIDGETS{$_[0]} = $_[0]; 23 push @ACTIVE_WIDGETS, $_[0];
22} 24}
23 25
24sub deactivate { 26sub deactivate {
25 delete $ACTIVE_WIDGETS{$_[0]}; 27 @ACTIVE_WIDGETS =
28 sort { $a->{z} <=> $b->{z} }
29 grep { $_ != $_[0] }
30 @ACTIVE_WIDGETS;
31}
32
33sub size_request {
34 die "size_request is abtract";
26} 35}
27 36
28sub focus_in { 37sub focus_in {
29 my ($widget) = @_; 38 my ($widget) = @_;
30 $FOCUS = $widget; 39 $FOCUS = $widget;
48 57
49sub button_up { 58sub button_up {
50 my ($widget, $sdlev) = @_; 59 my ($widget, $sdlev) = @_;
51} 60}
52 61
62sub w { $_[0]->{w} }
63sub h { $_[0]->{h} }
64sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} }
65sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} }
66sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} }
67
53sub draw { 68sub draw {
69 my ($self) = @_;
70
71 glPushMatrix;
72 glTranslate $self->{x}, $self->{y}, 0;
73 $self->_draw;
74 glPopMatrix;
75}
76
77sub _draw {
54 my ($widget) = @_; 78 my ($widget) = @_;
55} 79}
56 80
57sub bbox { 81sub bbox {
58 my ($widget) = @_; 82 my ($widget) = @_;
59} 83}
60 84
85package Crossfire::Client::Widget::Container;
86
87our @ISA = Crossfire::Client::Widget::;
88
89use SDL::OpenGL;
90
91sub add { $_[0]->{child} = $_[1] }
92sub get { $_[0]->{child} }
93
94sub size_request { $_[0]->{child}->size_request if $_[0]->{child} }
95
96sub _draw { die "Containers can't be drawn!" }
97
98package Crossfire::Client::Widget::Frame;
99
100our @ISA = Crossfire::Client::Widget::Container::;
101
102use SDL::OpenGL;
103
104sub size_request {
105 my ($self) = @_;
106 my $chld = $self->get
107 or return (0, 0);
108 map { $_ + 4 } $chld->size_request;
109}
110
111sub _draw {
112 my ($self) = @_;
113
114 my $chld = $self->get;
115
116 my ($w, $h) = $chld->size_request;
117
118 glColor 1, 0, 0;
119 glBegin GL_QUADS;
120 glTexCoord 0, 0; glVertex 0 , 0;
121 glTexCoord 0, 1; glVertex 0 , $h + 4;
122 glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
123 glTexCoord 1, 0; glVertex $w + 4 , 0;
124 glEnd;
125
126 glPushMatrix;
127 glTranslate (2, 2, 0);
128 $chld->_draw;
129 glPopMatrix;
130}
131
132package Crossfire::Client::Widget::Table;
133
134our @ISA = Crossfire::Client::Widget::Container::;
135
136use SDL::OpenGL;
137
138sub add {
139 my ($self, $x, $y, $chld) = @_;
140 $self->{childs}[$y][$x] = $chld;
141}
142
143sub max_row_height {
144 my ($self, $row) = @_;
145
146 my $hs = 0;
147 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) {
148 my $c = $self->{childs}->[$row]->[$xi];
149 my ($w, $h) = $c->size_request if $c;
150 if ($hs < $h) { $hs = $h }
151 }
152 return $hs;
153}
154
155sub max_col_width {
156 my ($self, $col) = @_;
157
158 my $ws = 0;
159 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) {
160 my $c = ($self->{childs}->[$yi] || [])->[$col];
161 my ($w, $h) = $c->size_request if $c;
162 if ($ws < $w) { $ws = $w }
163 }
164 return $ws;
165}
166
167sub size_request {
168 my ($self) = @_;
169
170 my ($hs, $ws) = (0, 0);
171
172 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
173 $hs += $self->max_row_height ($yi);
174 }
175
176 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
177 my $wm = 0;
178 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
179 $wm += $self->max_col_width ($xi)
180 }
181 if ($ws < $wm) { $ws = $wm }
182 }
183
184 return ($ws, $hs);
185}
186
187sub _draw {
188 my ($self) = @_;
189
190 my $y = 0;
191 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
192 my $x = 0;
193
194 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
195
196 glPushMatrix;
197 glTranslate ($x, $y, 0);
198 my $c = $self->{childs}->[$yi]->[$xi];
199 $c->_draw if $c;
200 glPopMatrix;
201
202 $x += $self->max_col_width ($xi);
203 }
204
205 $y += $self->max_row_height ($yi);
206 }
207}
208
209package Crossfire::Client::Widget::VBox;
210
211our @ISA = Crossfire::Client::Widget::Container::;
212
213use SDL::OpenGL;
214
215sub add {
216 my ($self, $chld) = @_;
217 push @{$self->{childs}}, $chld;
218}
219
220sub size_request {
221 my ($self) = @_;
222
223 my ($hs, $ws) = (0, 0);
224 for (@{$self->{childs} || []}) {
225 my ($w, $h) = $_->size_request;
226 $hs += $h;
227 if ($ws < $w) { $ws = $w }
228 }
229
230 return ($ws, $hs);
231}
232
233sub _draw {
234 my ($self) = @_;
235
236 my ($x, $y);
237 for (@{$self->{childs} || []}) {
238 glPushMatrix;
239 glTranslate (0, $y, 0);
240 $_->_draw;
241 glPopMatrix;
242 my ($w, $h) = $_->size_request;
243 $y += $h;
244 }
245}
246
61package Crossfire::Client::Widget::Label; 247package Crossfire::Client::Widget::Label;
62 248
249our @ISA = Crossfire::Client::Widget::;
250
63use SDL::OpenGL; 251use SDL::OpenGL;
64 252
65sub new { 253sub new {
66 my ($class, $ttf, $text) = @_; 254 my ($class, $x, $y, $z, $ttf, $text) = @_;
67 255
68 my $self = $class->SUPER::new; 256 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf);
69 257
70 $self->{texture} = new_from_ttf Crossfire::Client::Texture $ttf, $text; 258 $self->set_text ($text);
71 259
72 $self 260 $self
73} 261}
74 262
263sub set_text {
264 my ($self, $text) = @_;
265 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text;
266}
267
268sub get_text {
269 my ($self, $text) = @_;
270 $self->{text}
271}
272
273sub size_request {
274 my ($self) = @_;
275
276 (
277 $self->{texture}{width},
278 $self->{texture}{height},
279 )
280}
281
75sub draw { 282sub _draw {
76 my ($self) = @_; 283 my ($self) = @_;
77 284
78 my $tex = $self->{texture}; 285 my $tex = $self->{texture};
79 286
287 $self->{x}--;
288
289 glEnable GL_BLEND;
80 glEnable GL_TEXTURE_2D; 290 glEnable GL_TEXTURE_2D;
291 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
81 glBindTexture GL_TEXTURE_2D, $tex->{name}; 292 glBindTexture GL_TEXTURE_2D, $tex->{name};
293
294 glColor 1, 1, 1;
82 295
83 glBegin GL_QUADS; 296 glBegin GL_QUADS;
84 glTexCoord 0, 0; glVertex 0 , 0; 297 glTexCoord 0, 0; glVertex 0 , 0;
85 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 298 glTexCoord 0, 1; glVertex 0 , $tex->{height};
86 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 299 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
87 glTexCoord 1, 0; glVertex $tex->{width}, 0; 300 glTexCoord 1, 0; glVertex $tex->{width}, 0;
88 glEnd; 301 glEnd;
89 302
303 glDisable GL_BLEND;
90 glDisable GL_TEXTURE_2D; 304 glDisable GL_TEXTURE_2D;
91} 305}
92 306
93package Crossfire::Client::Widget::TextView; 307package Crossfire::Client::Widget::TextView;
94 308
102sub add_line { 316sub add_line {
103 my ($self, $line) = @_; 317 my ($self, $line) = @_;
104 push @{$self->{lines}}, $line; 318 push @{$self->{lines}}, $line;
105} 319}
106 320
107sub draw { 321sub _draw {
108 my ($self) = @_; 322 my ($self) = @_;
109 323
110} 324}
111 325
112package Crossfire::Client::Widget::MapWidget; 326package Crossfire::Client::Widget::MapWidget;
123} 337}
124 338
125sub key_up { 339sub key_up {
126} 340}
127 341
128sub draw { 342sub _draw {
129 glEnable GL_TEXTURE_2D; 343 glEnable GL_TEXTURE_2D;
130 glEnable GL_BLEND; 344 glEnable GL_BLEND;
345 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
131 346
132 my $map = $::CONN->{map}; 347 my $map = $::CONN->{map};
133 348
134 for my $x (0 .. $::CONN->{mapw} - 1) { 349 for my $x (0 .. $::CONN->{mapw} - 1) {
135 for my $y (0 .. $::CONN->{maph} - 1) { 350 for my $y (0 .. $::CONN->{maph} - 1) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines