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.15 by elmex, Fri Apr 7 23:05:21 2006 UTC vs.
Revision 1.56 by root, Mon Apr 10 22:16:34 2006 UTC

1package Crossfire::Client::Widget; 1package Crossfire::Client::Widget;
2 2
3use strict; 3use strict;
4
5use Scalar::Util;
6
4use SDL::OpenGL; 7use SDL::OpenGL;
5use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
6 9
7our $FOCUS; # the widget with current focus 10use Crossfire::Client;
8our @ACTIVE_WIDGETS; 11
12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
9 16
10# class methods for events 17# class methods for events
11sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 18sub feed_sdl_key_down_event {
12sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 19 $FOCUS->key_down ($_[0]) if $FOCUS;
13sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } 20}
14sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS } 21
22sub feed_sdl_key_up_event {
23 $FOCUS->key_up ($_[0]) if $FOCUS;
24}
25
26sub feed_sdl_button_down_event {
27 my ($ev) = @_;
28 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
29
30 if (!$BUTTON_STATE) {
31 my $widget = $TOPLEVEL->find_widget ($x, $y);
32
33 $GRAB = $widget;
34 $GRAB->update if $GRAB;
35 }
36
37 $BUTTON_STATE |= 1 << ($ev->button - 1);
38
39 $GRAB->button_down ($ev) if $GRAB;
40}
41
42sub feed_sdl_button_up_event {
43 my ($ev) = @_;
44 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
45
46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
47
48 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
49
50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab;
53 $GRAB->update if $GRAB;
54 }
55}
56
57sub feed_sdl_motion_event {
58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
62
63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget;
65
66 $hover->update if $hover;
67 $HOVER->update if $HOVER;
68 }
69
70 $HOVER->mouse_motion ($ev) if $HOVER;
71}
15 72
16sub new { 73sub new {
17 my $class = shift; 74 my $class = shift;
18 75
19 bless { @_ }, $class 76 bless { @_ }, $class
20} 77}
21 78
22sub activate { 79sub move {
23 push @ACTIVE_WIDGETS, $_[0]; 80 my ($self, $x, $y, $z) = @_;
81 $self->{x} = $x;
82 $self->{y} = $y;
83 $self->{z} = $z if defined $z;
24} 84}
25 85
26sub deactivate { 86sub needs_redraw {
27 @ACTIVE_WIDGETS = 87 0
28 sort { $a->{z} <=> $b->{z} }
29 grep { $_ != $_[0] }
30 @ACTIVE_WIDGETS;
31} 88}
32 89
33sub size_request { 90sub size_request {
91 require Carp;
34 die "size_request is abtract"; 92 Carp::confess "size_request is abtract";
93}
94
95sub size_allocate {
96 my ($self, $w, $h) = @_;
97
98 $self->{w} = $w;
99 $self->{h} = $h;
35} 100}
36 101
37sub focus_in { 102sub focus_in {
38 my ($widget) = @_; 103 my ($self) = @_;
39 $FOCUS = $widget; 104
105 my $focus = $FOCUS; $FOCUS = $self;
106 $focus->update if $focus;
107 $FOCUS->update;
40} 108}
41 109
42sub focus_out { 110sub focus_out {
43 my ($widget) = @_; 111 my ($self) = @_;
44}
45 112
46sub key_down { 113 return unless $FOCUS == $self;
47 my ($widget, $sdlev) = @_;
48}
49 114
50sub key_up { 115 my $focus = $FOCUS; undef $FOCUS;
51 my ($widget, $sdlev) = @_; 116 $focus->update if $focus; #?
52} 117}
53 118
119sub mouse_motion { }
120sub button_up { }
54sub button_down { 121sub button_down { }
55 my ($widget, $sdlev) = @_; 122sub key_down { }
56} 123sub key_up { }
57 124
58sub button_up { 125sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
59 my ($widget, $sdlev) = @_; 126sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
60}
61
62sub w { $_[0]->{w} }
63sub h { $_[0]->{h} }
64sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 127sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
65sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 128sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
66sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 129sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
67 130
68sub draw { 131sub draw {
69 my ($self) = @_; 132 my ($self) = @_;
70 133
71 glPushMatrix; 134 glPushMatrix;
72 glTranslate $self->{x}, $self->{y}, 0; 135 glTranslate $self->{x}, $self->{y}, 0;
73 $self->_draw; 136 $self->_draw;
137 if ($self == $HOVER) {
138 glColor 1, 1, 1, 0.4;
139 glEnable GL_BLEND;
140 glBegin GL_QUADS;
141 glVertex 0 , 0;
142 glVertex $self->{w}, 0;
143 glVertex $self->{w}, $self->{h};
144 glVertex 0 , $self->{h};
145 glEnd;
146 glDisable GL_BLEND;
147 }
74 glPopMatrix; 148 glPopMatrix;
75} 149}
76 150
77sub _draw { 151sub _draw {
78 my ($widget) = @_; 152 my ($self) = @_;
153
154 warn "no draw defined for $self\n";
79} 155}
80 156
81sub bbox { 157sub bbox {
82 my ($widget) = @_; 158 my ($self) = @_;
159 my ($w, $h) = $self->size_request;
160 (
161 $self->{x},
162 $self->{y},
163 $self->{x} = $w,
164 $self->{y} = $h
165 )
83} 166}
167
168sub find_widget {
169 my ($self, $x, $y) = @_;
170
171 return $self
172 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
173 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
174
175 ()
176}
177
178sub del_parent { $_[0]->{parent} = undef }
179
180sub set_parent {
181 my ($self, $par) = @_;
182
183 $self->{parent} = $par;
184 Scalar::Util::weaken $self->{parent};
185}
186
187sub get_parent {
188 $_[0]->{parent}
189}
190
191sub update {
192 my ($self) = @_;
193
194 $self->{parent}->update
195 if $self->{parent};
196}
197
198sub DESTROY {
199 my ($self) = @_;
200
201 #$self->deactivate;
202}
203
204#############################################################################
84 205
85package Crossfire::Client::Widget::Container; 206package Crossfire::Client::Widget::Container;
86 207
87our @ISA = Crossfire::Client::Widget::; 208our @ISA = Crossfire::Client::Widget::;
88 209
210sub new {
211 my ($class, @widgets) = @_;
212
213 my $self = $class->SUPER::new (children => []);
214 $self->add ($_) for @widgets;
215
216 $self
217}
218
219sub add {
220 my ($self, $chld, $expand) = @_;
221
222 $chld->{expand} = $expand;
223 $chld->set_parent ($self);
224
225 @{$self->{children}} =
226 sort { $a->{z} <=> $b->{z} }
227 @{$self->{children}}, $chld;
228
229 $self->size_allocate ($self->{w}, $self->{h})
230 if $self->{w}; #TODO: check for "realised state"
231}
232
233sub remove {
234 my ($self, $widget) = @_;
235
236 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
237
238 $self->size_allocate ($self->{w}, $self->{h});
239}
240
241sub find_widget {
242 my ($self, $x, $y) = @_;
243
244 $x -= $self->{x};
245 $y -= $self->{y};
246
247 my $res;
248
249 for (reverse @{ $self->{children} }) {
250 $res = $_->find_widget ($x, $y)
251 and return $res;
252 }
253
254 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
255}
256
257sub _draw {
258 my ($self) = @_;
259
260 $_->draw for @{$self->{children}};
261}
262
263#############################################################################
264
265package Crossfire::Client::Widget::Bin;
266
267our @ISA = Crossfire::Client::Widget::Container::;
268
269sub child { $_[0]->{children}[0] }
270
271sub size_request {
272 $_[0]{children}[0]->size_request if $_[0]{children}[0];
273}
274
275sub size_allocate {
276 my ($self, $w, $h) = @_;
277
278 $self->SUPER::size_allocate ($w, $h);
279 $self->{children}[0]->size_allocate ($w, $h)
280 if $self->{children}[0]
281}
282
283#############################################################################
284
285package Crossfire::Client::Widget::Window;
286
287our @ISA = Crossfire::Client::Widget::Bin::;
288
89use SDL::OpenGL; 289use SDL::OpenGL;
90 290
91sub add { $_[0]->{child} = $_[1] } 291sub new {
92sub get { $_[0]->{child} } 292 my ($class, $x, $y, $z, $w, $h) = @_;
93 293
94sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } 294 my $self = $class->SUPER::new;
95 295
96sub _draw { die "Containers can't be drawn!" } 296 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
297}
298
299sub update {
300 my ($self) = @_;
301
302 $self->render_chld;
303 $self->SUPER::update;
304}
305
306sub render_chld {
307 my ($self) = @_;
308
309 $self->{texture} =
310 Crossfire::Client::Texture->new_from_opengl (
311 $self->{w}, $self->{h}, sub { $self->child->draw }
312 );
313}
314
315sub size_allocate {
316 my ($self, $w, $h) = @_;
317
318 $self->{w} = $w;
319 $self->{h} = $h;
320
321 $self->child->size_allocate ($w, $h);
322
323 $self->render_chld;
324}
325
326sub _draw {
327 my ($self) = @_;
328
329 my ($w, $h) = ($self->w, $self->h);
330
331 my $tex = $self->{texture}
332 or return;
333
334 glEnable GL_BLEND;
335 glEnable GL_TEXTURE_2D;
336 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
337
338 $tex->draw_quad (0, 0, $w, $h);
339
340 glDisable GL_BLEND;
341 glDisable GL_TEXTURE_2D;
342}
343
344#############################################################################
97 345
98package Crossfire::Client::Widget::Frame; 346package Crossfire::Client::Widget::Frame;
99 347
100our @ISA = Crossfire::Client::Widget::Container::; 348our @ISA = Crossfire::Client::Widget::Bin::;
101 349
102use SDL::OpenGL; 350use SDL::OpenGL;
103 351
104sub size_request { 352sub size_request {
105 my ($self) = @_; 353 my ($self) = @_;
106 my $chld = $self->get 354 my $chld = $self->child
107 or return (0, 0); 355 or return (0, 0);
356
357 $chld->move (2, 2);
358
108 map { $_ + 4 } $chld->size_request; 359 map { $_ + 4 } $chld->size_request;
109} 360}
110 361
362sub size_allocate {
363 my ($self, $w, $h) = @_;
364
365 $self->{w} = $w;
366 $self->{h} = $h;
367
368 $self->child->size_allocate ($w - 4, $h - 4);
369 $self->child->move (2, 2);
370}
371
111sub _draw { 372sub _draw {
112 my ($self) = @_; 373 my ($self) = @_;
113 374
114 my $chld = $self->get; 375 my $chld = $self->child;
115 376
116 my ($w, $h) = $chld->size_request; 377 my ($w, $h) = $chld->size_request;
117 378
118 glColor 1, 0, 0;
119 glBegin GL_QUADS; 379 glBegin GL_QUADS;
120 glTexCoord 0, 0; glVertex 0 , 0; 380 glColor 0, 0, 0;
381 glVertex 0 , 0;
121 glTexCoord 0, 1; glVertex 0 , $h + 4; 382 glVertex 0 , $h + 4;
122 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 383 glVertex $w + 4 , $h + 4;
123 glTexCoord 1, 0; glVertex $w + 4 , 0; 384 glVertex $w + 4 , 0;
124 glEnd; 385 glEnd;
125 386
126 glPushMatrix;
127 glTranslate (2, 2, 0);
128 $chld->_draw; 387 $chld->draw;
129 glPopMatrix;
130} 388}
389
390#############################################################################
391
392package Crossfire::Client::Widget::FancyFrame;
393
394our @ISA = Crossfire::Client::Widget::Bin::;
395
396use SDL::OpenGL;
397
398my @tex =
399 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
400 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
401
402sub size_request {
403 my ($self) = @_;
404
405 my ($w, $h) = $self->SUPER::size_request;
406
407 $h += $tex[1]->{height};
408 $h += $tex[4]->{height};
409 $w += $tex[2]->{width};
410 $w += $tex[3]->{width};
411
412 ($w, $h)
413}
414
415sub size_allocate {
416 my ($self, $w, $h) = @_;
417
418 $self->SUPER::size_allocate ($w, $h);
419
420 $h -= $tex[1]->{height};
421 $h -= $tex[4]->{height};
422 $w -= $tex[2]->{width};
423 $w -= $tex[3]->{width};
424
425 $h = $h < 0 ? 0 : $h;
426 $w = $w < 0 ? 0 : $w;
427
428 $self->child->size_allocate ($w, $h);
429 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
430}
431
432sub _draw {
433 my ($self) = @_;
434
435 my ($w, $h) = ($self->{w}, $self->{h});
436 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
437
438 glEnable GL_BLEND;
439 glEnable GL_TEXTURE_2D;
440 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
441 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
442
443 my $top = $tex[1];
444 $top->draw_quad (0, 0, $w, $top->{height});
445
446 my $left = $tex[3];
447 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
448
449 my $right = $tex[2];
450 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
451
452 my $bottom = $tex[4];
453 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
454
455 my $bg = $tex[0];
456 glBindTexture GL_TEXTURE_2D, $bg->{name};
457 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
458 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
459 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
460
461 my $rep_x = $cw / $bg->{width};
462 my $rep_y = $ch / $bg->{height};
463
464 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
465
466 glDisable GL_BLEND;
467 glDisable GL_TEXTURE_2D;
468
469 $self->child->draw;
470
471}
472
473#############################################################################
131 474
132package Crossfire::Client::Widget::Table; 475package Crossfire::Client::Widget::Table;
133 476
134our @ISA = Crossfire::Client::Widget::Container::; 477our @ISA = Crossfire::Client::Widget::Bin::;
135 478
136use SDL::OpenGL; 479use SDL::OpenGL;
137 480
138sub add { 481sub add {
139 my ($self, $x, $y, $chld) = @_; 482 my ($self, $x, $y, $chld) = @_;
483 my $old_chld = $self->{children}[$y][$x];
484
140 $self->{childs}[$y][$x] = $chld; 485 $self->{children}[$y][$x] = $chld;
486 $chld->set_parent ($self);
487 $self->update;
141} 488}
142 489
143sub max_row_height { 490sub max_row_height {
144 my ($self, $row) = @_; 491 my ($self, $row) = @_;
145 492
146 my $hs = 0; 493 my $hs = 0;
147 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { 494 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) {
148 my $c = $self->{childs}->[$row]->[$xi]; 495 my $c = $self->{children}->[$row]->[$xi];
496 if ($c) {
149 my ($w, $h) = $c->size_request if $c; 497 my ($w, $h) = $c->size_request;
150 if ($hs < $h) { $hs = $h } 498 if ($hs < $h) { $hs = $h }
499 }
151 } 500 }
152 return $hs; 501 return $hs;
153} 502}
154 503
155sub max_col_width { 504sub max_col_width {
156 my ($self, $col) = @_; 505 my ($self, $col) = @_;
157 506
158 my $ws = 0; 507 my $ws = 0;
159 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { 508 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) {
160 my $c = ($self->{childs}->[$yi] || [])->[$col]; 509 my $c = ($self->{children}->[$yi] || [])->[$col];
510 if ($c) {
161 my ($w, $h) = $c->size_request if $c; 511 my ($w, $h) = $c->size_request;
162 if ($ws < $w) { $ws = $w } 512 if ($ws < $w) { $ws = $w }
513 }
163 } 514 }
164 return $ws; 515 return $ws;
165} 516}
166 517
167sub size_request { 518sub size_request {
168 my ($self) = @_; 519 my ($self) = @_;
169 520
170 my ($hs, $ws) = (0, 0); 521 my ($hs, $ws) = (0, 0);
171 522
172 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 523 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
173 $hs += $self->max_row_height ($yi); 524 $hs += $self->max_row_height ($yi);
174 } 525 }
175 526
176 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 527 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
177 my $wm = 0; 528 my $wm = 0;
178 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 529 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
179 $wm += $self->max_col_width ($xi) 530 $wm += $self->max_col_width ($xi)
180 } 531 }
181 if ($ws < $wm) { $ws = $wm } 532 if ($ws < $wm) { $ws = $wm }
182 } 533 }
183 534
186 537
187sub _draw { 538sub _draw {
188 my ($self) = @_; 539 my ($self) = @_;
189 540
190 my $y = 0; 541 my $y = 0;
191 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 542 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
192 my $x = 0; 543 my $x = 0;
193 544
194 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 545 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
195 546
196 glPushMatrix;
197 glTranslate ($x, $y, 0);
198 my $c = $self->{childs}->[$yi]->[$xi]; 547 my $c = $self->{children}->[$yi]->[$xi];
548 if ($c) {
549 $c->move ($x, $y, 0); #TODO: Move to size_request
199 $c->_draw if $c; 550 $c->draw if $c;
200 glPopMatrix; 551 }
201 552
202 $x += $self->max_col_width ($xi); 553 $x += $self->max_col_width ($xi);
203 } 554 }
204 555
205 $y += $self->max_row_height ($yi); 556 $y += $self->max_row_height ($yi);
206 } 557 }
207} 558}
208 559
560#############################################################################
561
209package Crossfire::Client::Widget::VBox; 562package Crossfire::Client::Widget::VBox;
210 563
211our @ISA = Crossfire::Client::Widget::Container::; 564our @ISA = Crossfire::Client::Widget::Container::;
212 565
213use SDL::OpenGL; 566use SDL::OpenGL;
214 567
215sub add {
216 my ($self, $chld) = @_;
217 push @{$self->{childs}}, $chld;
218}
219
220sub size_request { 568sub size_request {
221 my ($self) = @_; 569 my ($self) = @_;
222 570
223 my ($hs, $ws) = (0, 0); 571 my @alloc = map [$_->size_request], @{$self->{children}};
572
573 (
574 (List::Util::max map $_->[0], @alloc),
575 (List::Util::sum map $_->[1], @alloc),
576 )
577}
578
579sub size_allocate {
580 my ($self, $w, $h) = @_;
581
582 $self->w ($w);
583 $self->h ($h);
584
585 my $exp;
586 my @oth;
587 # find expand widget
224 for (@{$self->{childs} || []}) { 588 for (@{$self->{children}}) {
589 if ($_->{expand}) {
590 $exp = $_;
591 last;
592 }
593 push @oth, $_;
594 }
595
596 my ($ow, $oh);
597
598 # get sizes of other widgets
599 for (@oth) {
225 my ($w, $h) = $_->size_request; 600 my ($w, $h) = $_->size_request;
226 $hs += $h; 601 $oh += $h;
227 if ($ws < $w) { $ws = $w } 602 if ($ow < $w) { $ow = $w }
228 } 603 }
229 604
230 return ($ws, $hs); 605 my $y = 0;
231}
232
233sub _draw {
234 my ($self) = @_;
235
236 my ($x, $y);
237 for (@{$self->{childs} || []}) { 606 for (@{$self->{children}}) {
238 glPushMatrix; 607 $_->move (0, $y);
239 glTranslate (0, $y, 0); 608
240 $_->_draw; 609 if ($_ == $exp) {
241 glPopMatrix; 610 $_->size_allocate ($w, $h - $oh);
611 $y += $h - $oh;
612 } else {
242 my ($w, $h) = $_->size_request; 613 my ($cw, $h) = $_->size_request;
614 $_->size_allocate ($w, $h);
243 $y += $h; 615 $y += $h;
616 }
244 } 617 }
245} 618}
619
620#############################################################################
246 621
247package Crossfire::Client::Widget::Label; 622package Crossfire::Client::Widget::Label;
248 623
249our @ISA = Crossfire::Client::Widget::; 624our @ISA = Crossfire::Client::Widget::;
250 625
251use SDL::OpenGL; 626use SDL::OpenGL;
252 627
253sub new { 628sub new {
254 my ($class, $x, $y, $z, $ttf, $text) = @_; 629 my ($class, $x, $y, $z, $height, $text) = @_;
255 630
631 $height ||= $::FONTSIZE;
632
633 # TODO: color, and make height, xyz etc. optional
256 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf); 634 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
257 635
258 $self->set_text ($text); 636 $self->set_text ($text);
259 637
260 $self 638 $self
261} 639}
262 640
263sub set_text { 641sub set_text {
264 my ($self, $text) = @_; 642 my ($self, $text) = @_;
643
644 $self->{text} = $text;
265 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text; 645 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
646
647 $self->update;
266} 648}
267 649
268sub get_text { 650sub get_text {
269 my ($self, $text) = @_; 651 my ($self, $text) = @_;
652
270 $self->{text} 653 $self->{text}
271} 654}
272 655
273sub size_request { 656sub size_request {
274 my ($self) = @_; 657 my ($self) = @_;
275 658
276 ( 659 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
660 (
277 $self->{texture}{width}, 661 $self->{texture}{width},
278 $self->{texture}{height}, 662 $self->{texture}{height},
663 )
664 } else {
665 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
666
667 ($w, $h)
279 ) 668 }
669
280} 670}
281 671
282sub _draw { 672sub _draw {
283 my ($self) = @_; 673 my ($self) = @_;
284 674
285 my $tex = $self->{texture}; 675 my $tex = $self->{texture};
286
287 $self->{x}--;
288 676
289 glEnable GL_BLEND; 677 glEnable GL_BLEND;
290 glEnable GL_TEXTURE_2D; 678 glEnable GL_TEXTURE_2D;
679 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
291 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 680 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
292 glBindTexture GL_TEXTURE_2D, $tex->{name};
293 681
294 glColor 1, 1, 1; 682 glColor 1, 0, 0, 1; # TODO color
295 683
296 glBegin GL_QUADS; 684 $tex->draw_quad (0, 0);
297 glTexCoord 0, 0; glVertex 0 , 0;
298 glTexCoord 0, 1; glVertex 0 , $tex->{height};
299 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
300 glTexCoord 1, 0; glVertex $tex->{width}, 0;
301 glEnd;
302 685
303 glDisable GL_BLEND; 686 glDisable GL_BLEND;
304 glDisable GL_TEXTURE_2D; 687 glDisable GL_TEXTURE_2D;
305} 688}
306 689
690#############################################################################
691
307package Crossfire::Client::Widget::TextView; 692package Crossfire::Client::Widget::Entry;
693
694our @ISA = Crossfire::Client::Widget::Label::;
695
696use SDL;
697use SDL::OpenGL;
698
699sub key_down {
700 my ($self, $ev) = @_;
701
702 my $mod = $ev->key_mod;
703 my $sym = $ev->key_sym;
704
705 my $uni = $ev->key_unicode;
706
707 my $text = $self->get_text;
708
709 if ($sym == SDLK_BACKSPACE) {
710 substr $text, -1, 1, '';
711 } elsif ($uni) {
712 $text .= chr $uni;
713 print "$uni <$text>\n";#d#
714 }
715
716 $self->set_text ($text);
717}
718
719sub button_down {
720 my ($self, $ev) = @_;
721
722 $self->focus_in;
723}
724
725sub _draw {
726 my ($self) = @_;
727
728 if ($FOCUS == $self) {
729 glColor 1, 1, 1;
730 } else {
731 glColor 0.7, 0.7, 0.7;
732 }
733
734 glBegin GL_QUADS;
735 glVertex 0 , 0;
736 glVertex 0 , $self->{h} - 1;
737 glVertex $self->{w} - 1, $self->{h} - 1;
738 glVertex $self->{w} - 1, 0;
739 glEnd;
740
741 $self->SUPER::_draw;
742}
743
744#############################################################################
745
746package Crossfire::Client::Widget::MapWidget;
308 747
309use strict; 748use strict;
310 749
311our @ISA = qw/Crossfire::Client::Widget/; 750use List::Util qw(min max);
312 751
752use SDL;
313use SDL::OpenGL; 753use SDL::OpenGL;
314use SDL::OpenGL::Constants; 754use SDL::OpenGL::Constants;
315 755
316sub add_line {
317 my ($self, $line) = @_;
318 push @{$self->{lines}}, $line;
319}
320
321sub _draw {
322 my ($self) = @_;
323
324}
325
326package Crossfire::Client::Widget::MapWidget;
327
328use strict;
329
330our @ISA = qw/Crossfire::Client::Widget/; 756our @ISA = Crossfire::Client::Widget::;
331
332use SDL::OpenGL;
333use SDL::OpenGL::Constants;
334 757
335sub key_down { 758sub key_down {
336 print "MAPKEYDOWN\n"; 759 print "MAPKEYDOWN\n";
337} 760}
338 761
339sub key_up { 762sub key_up {
340} 763}
341 764
765sub size_request {
766 (
767 1 + int $::WIDTH / 32,
768 1 + int $::HEIGHT / 32,
769 )
770}
771
342sub _draw { 772sub _draw {
773 my ($self) = @_;
774
775 my $mx = $::CONN->{mapx};
776 my $my = $::CONN->{mapy};
777
778 my $map = $::CONN->{map};
779
780 my ($xofs, $yofs);
781
782 my $sw = 1 + int $::WIDTH / 32;
783 my $sh = 1 + int $::HEIGHT / 32;
784
785 if ($::CONN->{mapw} > $sw) {
786 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
787 } else {
788 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
789 }
790
791 if ($::CONN->{maph} > $sh) {
792 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
793 } else {
794 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
795 }
796
343 glEnable GL_TEXTURE_2D; 797 glEnable GL_TEXTURE_2D;
344 glEnable GL_BLEND; 798 glEnable GL_BLEND;
345 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 799 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
346 800
347 my $map = $::CONN->{map}; 801 my $sw4 = ($sw + 3) & ~3;
802 my $lighting = "\x00" x ($sw4 * $sh);
348 803
349 for my $x (0 .. $::CONN->{mapw} - 1) { 804 for my $x (0 .. $sw - 1) {
350 for my $y (0 .. $::CONN->{maph} - 1) { 805 for my $y (0 .. $sh - 1) {
351 806
352 my $cell = $map->[$x][$y] 807 my $cell = $map->[$x + $xofs][$y + $yofs]
353 or next; 808 or next;
354 809
355 my $darkness = $cell->[3] * (1 / 255); 810 my $darkness = $cell->[0] * (1 / 255);
356 glColor $darkness, $darkness, $darkness; 811 if ($darkness < 0) {
812 $darkness = $cell->[1] ? 0.1 : 0;
813 }
814 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
357 815
358 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 816 for my $num (grep $_, @$cell[1,2,3]) {
359 my $tex = $::CONN->{face}[$num]{texture} || next; 817 my $tex = $::CONN->{face}[$num]{texture} || next;
360
361 glBindTexture GL_TEXTURE_2D, $tex->{name};
362
363 glBegin GL_QUADS;
364 glTexCoord 0, 0; glVertex $x * 32 , $y * 32;
365 glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32;
366 glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32;
367 glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32;
368 glEnd; 818
819 my $w = $tex->{width};
820 my $h = $tex->{height};
821
822 my $px = ($x + 1) * 32 - $w;
823 my $py = ($y + 1) * 32 - $h;
824
825 $tex->draw_quad ($px, $py, $w, $h);
369 } 826 }
370 } 827 }
371 } 828 }
372 829
830# if (1) { # higher quality darkness
831# $lighting =~ s/(.)/$1$1$1/gs;
832# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
833#
834# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
835#
836# $lighting = $pb->get_pixels;
837# $lighting =~ s/(.)../$1/gs;
838# }
839
840 $lighting = new Crossfire::Client::Texture
841 width => $sw4,
842 height => $sh,
843 data => $lighting,
844 internalformat => GL_ALPHA,
845 format => GL_ALPHA;
846
847 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
848 glColor 0.45, 0.45, 0.45, 1;
849 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
850 glBindTexture GL_TEXTURE_2D, $lighting->{name};
851 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
852
853 $lighting->draw_quad (0, 0, $sw4 * 32, $sh * 32);
854
373 glDisable GL_TEXTURE_2D; 855 glDisable GL_TEXTURE_2D;
374 glDisable GL_BLEND; 856 glDisable GL_BLEND;
375} 857}
376 858
3771; 859my %DIR = (
860 SDLK_KP8, [1, "north"],
861 SDLK_KP9, [2, "northeast"],
862 SDLK_KP6, [3, "east"],
863 SDLK_KP3, [4, "southeast"],
864 SDLK_KP2, [5, "south"],
865 SDLK_KP1, [6, "southwest"],
866 SDLK_KP4, [7, "west"],
867 SDLK_KP7, [8, "northwest"],
378 868
869 SDLK_UP, [1, "north"],
870 SDLK_RIGHT, [3, "east"],
871 SDLK_DOWN, [5, "south"],
872 SDLK_LEFT, [7, "west"],
873);
874
875sub key_down {
876 my ($self, $ev) = @_;
877
878 my $mod = $ev->key_mod;
879 my $sym = $ev->key_sym;
880
881 if ($sym == SDLK_KP5) {
882 $::CONN->send ("command stay fire");
883 } elsif (exists $DIR{$sym}) {
884 if ($mod & KMOD_SHIFT) {
885 $self->{shft}++;
886 $::CONN->send ("command fire $DIR{$sym}[0]");
887 } elsif ($mod & KMOD_CTRL) {
888 $self->{ctrl}++;
889 $::CONN->send ("command run $DIR{$sym}[0]");
890 } else {
891 $::CONN->send ("command $DIR{$sym}[1]");
892 }
893 }
894}
895
896sub key_up {
897 my ($self, $ev) = @_;
898
899 my $mod = $ev->key_mod;
900 my $sym = $ev->key_sym;
901
902 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
903 $::CONN->send ("command fire_stop");
904 }
905 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
906 $::CONN->send ("command run_stop");
907 }
908}
909
910#############################################################################
911
912package Crossfire::Client::Widget::Animator;
913
914use SDL::OpenGL;
915
916our @ISA = Crossfire::Client::Widget::Bin::;
917
918sub moveto {
919 my ($self, $x, $y) = @_;
920
921 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
922 $self->{speed} = 0.001;
923 $self->{time} = 1;
924
925 ::animation_start $self;
926}
927
928sub animate {
929 my ($self, $interval) = @_;
930
931 printf "%5.2f\n", 1 / $interval;#d#
932
933 $self->{time} -= $interval * $self->{speed};
934 if ($self->{time} <= 0) {
935 $self->{time} = 0;
936 ::animation_stop $self;
937 }
938
939 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
940
941 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
942 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
943}
944
945sub _draw {
946 my ($self) = @_;
947
948 glPushMatrix;
949 glRotate $self->{time} * 1000, 0, 1, 0;
950 $self->{children}[0]->draw;
951 glPopMatrix;
952}
953
954#############################################################################
955
956package Crossfire::Client::Widget::Toplevel;
957
958our @ISA = Crossfire::Client::Widget::Container::;
959
960sub size_request {
961 ($::WIDTH, $::HEIGHT)
962}
963
964sub size_allocate {
965 my ($self, $w, $h) = @_;
966
967 $self->SUPER::size_allocate ($w, $h);
968
969 $_->size_allocate ($_->size_request)
970 for @{$self->{children}};
971}
972
973sub update {
974 my ($self) = @_;
975
976 $self->size_allocate ($self->size_request);
977 ::refresh ();
978}
979
980sub add {
981 my ($self, $widget) = @_;
982
983 $self->SUPER::add ($widget);
984
985 $widget->size_allocate ($widget->size_request);
986}
987
988sub draw {
989 my ($self) = @_;
990
991 $self->_draw;
992}
993
994#############################################################################
995
996package Crossfire::Client::Widget;
997
998$TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
999
10001
1001

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines