ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.56
Committed: Mon Apr 10 22:16:34 2006 UTC (18 years, 2 months ago) by root
Branch: MAIN
Changes since 1.55: +18 -72 lines
Log Message:
support npot tetxures and use an utility function to draw quads

File Contents

# User Rev Content
1 elmex 1.6 package Crossfire::Client::Widget;
2 root 1.8
3 elmex 1.1 use strict;
4 root 1.18
5     use Scalar::Util;
6    
7 elmex 1.11 use SDL::OpenGL;
8     use SDL::OpenGL::Constants;
9 elmex 1.1
10 root 1.41 use Crossfire::Client;
11    
12 root 1.51 our ($FOCUS, $HOVER, $GRAB); # various widgets
13    
14     our $TOPLEVEL;
15     our $BUTTON_STATE;
16    
17 elmex 1.1 # class methods for events
18 root 1.51 sub feed_sdl_key_down_event {
19     $FOCUS->key_down ($_[0]) if $FOCUS;
20     }
21    
22     sub feed_sdl_key_up_event {
23     $FOCUS->key_up ($_[0]) if $FOCUS;
24     }
25    
26     sub 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    
42     sub 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    
57     sub 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     }
72 elmex 1.1
73     sub new {
74     my $class = shift;
75 root 1.10
76     bless { @_ }, $class
77 elmex 1.1 }
78    
79 root 1.18 sub move {
80     my ($self, $x, $y, $z) = @_;
81     $self->{x} = $x;
82     $self->{y} = $y;
83     $self->{z} = $z if defined $z;
84     }
85    
86 elmex 1.20 sub needs_redraw {
87     0
88     }
89    
90 root 1.14 sub size_request {
91 elmex 1.36 require Carp;
92     Carp::confess "size_request is abtract";
93     }
94    
95     sub size_allocate {
96     my ($self, $w, $h) = @_;
97 root 1.40
98     $self->{w} = $w;
99     $self->{h} = $h;
100 root 1.14 }
101    
102 elmex 1.1 sub focus_in {
103 root 1.51 my ($self) = @_;
104    
105     my $focus = $FOCUS; $FOCUS = $self;
106     $focus->update if $focus;
107     $FOCUS->update;
108 elmex 1.1 }
109 root 1.4
110 elmex 1.1 sub focus_out {
111 root 1.51 my ($self) = @_;
112 root 1.4
113 root 1.51 return unless $FOCUS == $self;
114 root 1.4
115 root 1.51 my $focus = $FOCUS; undef $FOCUS;
116     $focus->update if $focus; #?
117 elmex 1.1 }
118 root 1.4
119 root 1.51 sub mouse_motion { }
120     sub button_up { }
121     sub button_down { }
122     sub key_down { }
123     sub key_up { }
124    
125     sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
126     sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
127     sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
128     sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
129     sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
130 elmex 1.11
131 elmex 1.1 sub draw {
132 elmex 1.11 my ($self) = @_;
133    
134     glPushMatrix;
135 root 1.12 glTranslate $self->{x}, $self->{y}, 0;
136 elmex 1.11 $self->_draw;
137 root 1.51 if ($self == $HOVER) {
138 root 1.48 glColor 1, 1, 1, 0.4;
139 root 1.50 glEnable GL_BLEND;
140 root 1.48 glBegin GL_QUADS;
141 root 1.51 glVertex 0 , 0;
142     glVertex $self->{w}, 0;
143     glVertex $self->{w}, $self->{h};
144     glVertex 0 , $self->{h};
145 root 1.47 glEnd;
146 root 1.50 glDisable GL_BLEND;
147 root 1.47 }
148 elmex 1.11 glPopMatrix;
149     }
150    
151     sub _draw {
152 root 1.38 my ($self) = @_;
153    
154     warn "no draw defined for $self\n";
155 elmex 1.1 }
156 root 1.4
157 elmex 1.1 sub bbox {
158 elmex 1.32 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     )
166     }
167    
168 root 1.38 sub 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    
178 elmex 1.32 sub del_parent { $_[0]->{parent} = undef }
179    
180     sub set_parent {
181     my ($self, $par) = @_;
182    
183     $self->{parent} = $par;
184     Scalar::Util::weaken $self->{parent};
185     }
186    
187     sub get_parent {
188     $_[0]->{parent}
189     }
190    
191     sub update {
192     my ($self) = @_;
193    
194     $self->{parent}->update
195     if $self->{parent};
196 elmex 1.1 }
197 elmex 1.2
198 root 1.18 sub DESTROY {
199     my ($self) = @_;
200    
201 elmex 1.32 #$self->deactivate;
202 root 1.18 }
203    
204 root 1.39 #############################################################################
205    
206 root 1.38 package Crossfire::Client::Widget::Container;
207 elmex 1.15
208     our @ISA = Crossfire::Client::Widget::;
209    
210 root 1.38 sub new {
211     my ($class, @widgets) = @_;
212    
213     my $self = $class->SUPER::new (children => []);
214     $self->add ($_) for @widgets;
215    
216     $self
217     }
218    
219     sub 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 root 1.43 $self->size_allocate ($self->{w}, $self->{h})
230     if $self->{w}; #TODO: check for "realised state"
231 root 1.38 }
232 root 1.35
233 elmex 1.32 sub remove {
234 root 1.38 my ($self, $widget) = @_;
235    
236     $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
237    
238     $self->size_allocate ($self->{w}, $self->{h});
239     }
240    
241     sub find_widget {
242     my ($self, $x, $y) = @_;
243    
244 root 1.45 $x -= $self->{x};
245     $y -= $self->{y};
246    
247 root 1.38 my $res;
248    
249 root 1.46 for (reverse @{ $self->{children} }) {
250 root 1.45 $res = $_->find_widget ($x, $y)
251 root 1.38 and return $res;
252     }
253    
254 root 1.46 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
255 elmex 1.32 }
256 elmex 1.15
257 root 1.35 sub _draw {
258     my ($self) = @_;
259    
260 root 1.38 $_->draw for @{$self->{children}};
261 root 1.35 }
262 elmex 1.15
263 root 1.39 #############################################################################
264    
265 root 1.38 package Crossfire::Client::Widget::Bin;
266 elmex 1.32
267 root 1.38 our @ISA = Crossfire::Client::Widget::Container::;
268 elmex 1.32
269 root 1.39 sub child { $_[0]->{children}[0] }
270 elmex 1.32
271 root 1.38 sub size_request {
272     $_[0]{children}[0]->size_request if $_[0]{children}[0];
273     }
274 elmex 1.32
275 root 1.38 sub size_allocate {
276     my ($self, $w, $h) = @_;
277 root 1.42
278 root 1.38 $self->SUPER::size_allocate ($w, $h);
279     $self->{children}[0]->size_allocate ($w, $h)
280     if $self->{children}[0]
281     }
282 elmex 1.32
283 root 1.39 #############################################################################
284    
285 elmex 1.20 package Crossfire::Client::Widget::Window;
286    
287 root 1.35 our @ISA = Crossfire::Client::Widget::Bin::;
288 elmex 1.20
289     use SDL::OpenGL;
290    
291 root 1.42 sub new {
292     my ($class, $x, $y, $z, $w, $h) = @_;
293    
294     my $self = $class->SUPER::new;
295 elmex 1.32
296 root 1.42 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
297 elmex 1.32 }
298    
299     sub update {
300     my ($self) = @_;
301 root 1.42
302 elmex 1.32 $self->render_chld;
303 root 1.42 $self->SUPER::update;
304 elmex 1.20 }
305    
306     sub render_chld {
307     my ($self) = @_;
308    
309     $self->{texture} =
310     Crossfire::Client::Texture->new_from_opengl (
311 root 1.42 $self->{w}, $self->{h}, sub { $self->child->draw }
312 elmex 1.20 );
313 elmex 1.36 }
314    
315     sub size_allocate {
316     my ($self, $w, $h) = @_;
317    
318 root 1.42 $self->{w} = $w;
319     $self->{h} = $h;
320    
321     $self->child->size_allocate ($w, $h);
322 elmex 1.36
323 root 1.42 $self->render_chld;
324 elmex 1.20 }
325    
326     sub _draw {
327     my ($self) = @_;
328    
329 elmex 1.36 my ($w, $h) = ($self->w, $self->h);
330 root 1.29
331 elmex 1.20 my $tex = $self->{texture}
332     or return;
333    
334     glEnable GL_BLEND;
335     glEnable GL_TEXTURE_2D;
336 root 1.35 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
337 elmex 1.20
338 root 1.56 $tex->draw_quad (0, 0, $w, $h);
339 elmex 1.20
340     glDisable GL_BLEND;
341     glDisable GL_TEXTURE_2D;
342     }
343    
344 root 1.39 #############################################################################
345    
346 elmex 1.15 package Crossfire::Client::Widget::Frame;
347    
348 root 1.35 our @ISA = Crossfire::Client::Widget::Bin::;
349 elmex 1.15
350     use SDL::OpenGL;
351    
352     sub size_request {
353     my ($self) = @_;
354 root 1.39 my $chld = $self->child
355 elmex 1.15 or return (0, 0);
356 root 1.30
357     $chld->move (2, 2);
358    
359 elmex 1.15 map { $_ + 4 } $chld->size_request;
360     }
361    
362 elmex 1.36 sub size_allocate {
363     my ($self, $w, $h) = @_;
364 root 1.42
365     $self->{w} = $w;
366     $self->{h} = $h;
367 elmex 1.36
368 root 1.39 $self->child->size_allocate ($w - 4, $h - 4);
369     $self->child->move (2, 2);
370 elmex 1.36 }
371    
372 elmex 1.15 sub _draw {
373     my ($self) = @_;
374    
375 root 1.39 my $chld = $self->child;
376 elmex 1.15
377     my ($w, $h) = $chld->size_request;
378    
379     glBegin GL_QUADS;
380 root 1.30 glColor 0, 0, 0;
381 root 1.56 glVertex 0 , 0;
382     glVertex 0 , $h + 4;
383     glVertex $w + 4 , $h + 4;
384     glVertex $w + 4 , 0;
385 elmex 1.15 glEnd;
386    
387 root 1.23 $chld->draw;
388 elmex 1.15 }
389    
390 root 1.39 #############################################################################
391    
392 elmex 1.31 package Crossfire::Client::Widget::FancyFrame;
393    
394 root 1.41 our @ISA = Crossfire::Client::Widget::Bin::;
395 elmex 1.31
396     use SDL::OpenGL;
397    
398 root 1.41 my @tex =
399 elmex 1.34 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
400 root 1.41 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
401 elmex 1.34
402     sub size_request {
403     my ($self) = @_;
404 root 1.39
405     my ($w, $h) = $self->SUPER::size_request;
406 elmex 1.34
407 root 1.41 $h += $tex[1]->{height};
408     $h += $tex[4]->{height};
409     $w += $tex[2]->{width};
410     $w += $tex[3]->{width};
411 elmex 1.34
412 elmex 1.36 ($w, $h)
413     }
414    
415     sub size_allocate {
416     my ($self, $w, $h) = @_;
417    
418 root 1.40 $self->SUPER::size_allocate ($w, $h);
419    
420 root 1.41 $h -= $tex[1]->{height};
421     $h -= $tex[4]->{height};
422     $w -= $tex[2]->{width};
423     $w -= $tex[3]->{width};
424 elmex 1.36
425     $h = $h < 0 ? 0 : $h;
426     $w = $w < 0 ? 0 : $w;
427 root 1.43
428 root 1.39 $self->child->size_allocate ($w, $h);
429 root 1.43 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
430 elmex 1.34 }
431    
432     sub _draw {
433     my ($self) = @_;
434    
435 root 1.43 my ($w, $h) = ($self->{w}, $self->{h});
436     my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
437 elmex 1.34
438     glEnable GL_BLEND;
439     glEnable GL_TEXTURE_2D;
440     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
441 elmex 1.36 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
442 elmex 1.34
443 root 1.41 my $top = $tex[1];
444 root 1.56 $top->draw_quad (0, 0, $w, $top->{height});
445 elmex 1.34
446 root 1.41 my $left = $tex[3];
447 root 1.56 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
448 elmex 1.34
449 root 1.41 my $right = $tex[2];
450 root 1.56 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
451 elmex 1.34
452 root 1.41 my $bottom = $tex[4];
453 root 1.56 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
454 elmex 1.34
455 root 1.41 my $bg = $tex[0];
456 elmex 1.36 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 elmex 1.34
461 elmex 1.36 my $rep_x = $cw / $bg->{width};
462     my $rep_y = $ch / $bg->{height};
463 elmex 1.34
464 root 1.56 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
465 elmex 1.34
466     glDisable GL_BLEND;
467     glDisable GL_TEXTURE_2D;
468 elmex 1.36
469 root 1.39 $self->child->draw;
470 elmex 1.36
471 elmex 1.34 }
472 elmex 1.31
473 root 1.39 #############################################################################
474    
475 elmex 1.15 package Crossfire::Client::Widget::Table;
476    
477 root 1.35 our @ISA = Crossfire::Client::Widget::Bin::;
478 elmex 1.15
479     use SDL::OpenGL;
480    
481     sub add {
482     my ($self, $x, $y, $chld) = @_;
483 root 1.38 my $old_chld = $self->{children}[$y][$x];
484 elmex 1.32
485 root 1.38 $self->{children}[$y][$x] = $chld;
486 elmex 1.32 $chld->set_parent ($self);
487     $self->update;
488 elmex 1.15 }
489    
490     sub max_row_height {
491     my ($self, $row) = @_;
492    
493     my $hs = 0;
494 root 1.38 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) {
495     my $c = $self->{children}->[$row]->[$xi];
496 elmex 1.17 if ($c) {
497     my ($w, $h) = $c->size_request;
498     if ($hs < $h) { $hs = $h }
499     }
500 elmex 1.15 }
501     return $hs;
502     }
503    
504     sub max_col_width {
505     my ($self, $col) = @_;
506    
507     my $ws = 0;
508 root 1.38 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) {
509     my $c = ($self->{children}->[$yi] || [])->[$col];
510 elmex 1.17 if ($c) {
511     my ($w, $h) = $c->size_request;
512     if ($ws < $w) { $ws = $w }
513     }
514 elmex 1.15 }
515     return $ws;
516     }
517    
518     sub size_request {
519     my ($self) = @_;
520    
521     my ($hs, $ws) = (0, 0);
522    
523 root 1.38 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
524 elmex 1.15 $hs += $self->max_row_height ($yi);
525     }
526    
527 root 1.38 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
528 elmex 1.15 my $wm = 0;
529 root 1.38 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
530 elmex 1.15 $wm += $self->max_col_width ($xi)
531     }
532     if ($ws < $wm) { $ws = $wm }
533     }
534    
535     return ($ws, $hs);
536     }
537    
538     sub _draw {
539     my ($self) = @_;
540    
541     my $y = 0;
542 root 1.38 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
543 elmex 1.15 my $x = 0;
544    
545 root 1.38 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
546 elmex 1.15
547 root 1.38 my $c = $self->{children}->[$yi]->[$xi];
548 elmex 1.26 if ($c) {
549     $c->move ($x, $y, 0); #TODO: Move to size_request
550     $c->draw if $c;
551     }
552 elmex 1.15
553     $x += $self->max_col_width ($xi);
554     }
555    
556     $y += $self->max_row_height ($yi);
557     }
558     }
559    
560 root 1.39 #############################################################################
561    
562 elmex 1.15 package Crossfire::Client::Widget::VBox;
563    
564 root 1.38 our @ISA = Crossfire::Client::Widget::Container::;
565 elmex 1.15
566     use SDL::OpenGL;
567    
568 root 1.43 sub size_request {
569     my ($self) = @_;
570    
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    
579 elmex 1.36 sub 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
588 root 1.38 for (@{$self->{children}}) {
589 elmex 1.36 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) {
600     my ($w, $h) = $_->size_request;
601     $oh += $h;
602     if ($ow < $w) { $ow = $w }
603     }
604    
605     my $y = 0;
606 root 1.38 for (@{$self->{children}}) {
607 elmex 1.36 $_->move (0, $y);
608    
609     if ($_ == $exp) {
610     $_->size_allocate ($w, $h - $oh);
611     $y += $h - $oh;
612     } else {
613     my ($cw, $h) = $_->size_request;
614     $_->size_allocate ($w, $h);
615     $y += $h;
616     }
617     }
618     }
619    
620 root 1.39 #############################################################################
621    
622 root 1.10 package Crossfire::Client::Widget::Label;
623    
624 root 1.12 our @ISA = Crossfire::Client::Widget::;
625    
626 root 1.10 use SDL::OpenGL;
627    
628     sub new {
629 root 1.28 my ($class, $x, $y, $z, $height, $text) = @_;
630 root 1.10
631 root 1.51 $height ||= $::FONTSIZE;
632    
633 root 1.33 # TODO: color, and make height, xyz etc. optional
634 root 1.28 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
635 root 1.10
636 elmex 1.15 $self->set_text ($text);
637 root 1.10
638     $self
639     }
640    
641 elmex 1.15 sub set_text {
642     my ($self, $text) = @_;
643 root 1.28
644     $self->{text} = $text;
645 elmex 1.32 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
646 root 1.28
647 elmex 1.32 $self->update;
648 elmex 1.15 }
649    
650     sub get_text {
651     my ($self, $text) = @_;
652 root 1.28
653 elmex 1.15 $self->{text}
654     }
655    
656 root 1.14 sub size_request {
657     my ($self) = @_;
658    
659 root 1.51 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
660     (
661     $self->{texture}{width},
662     $self->{texture}{height},
663     )
664     } else {
665     my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
666    
667     ($w, $h)
668     }
669    
670 root 1.14 }
671    
672 elmex 1.11 sub _draw {
673 root 1.10 my ($self) = @_;
674    
675     my $tex = $self->{texture};
676    
677 root 1.12 glEnable GL_BLEND;
678 root 1.10 glEnable GL_TEXTURE_2D;
679 root 1.30 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
680 root 1.28 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
681 root 1.10
682 elmex 1.36 glColor 1, 0, 0, 1; # TODO color
683 root 1.12
684 root 1.56 $tex->draw_quad (0, 0);
685 root 1.10
686 root 1.12 glDisable GL_BLEND;
687 root 1.10 glDisable GL_TEXTURE_2D;
688     }
689    
690 root 1.39 #############################################################################
691    
692 root 1.51 package Crossfire::Client::Widget::Entry;
693 elmex 1.31
694     our @ISA = Crossfire::Client::Widget::Label::;
695    
696     use SDL;
697     use SDL::OpenGL;
698    
699     sub 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 root 1.51 print "$uni <$text>\n";#d#
714 elmex 1.31 }
715 root 1.51
716 elmex 1.31 $self->set_text ($text);
717     }
718    
719 root 1.51 sub button_down {
720     my ($self, $ev) = @_;
721    
722     $self->focus_in;
723     }
724    
725     sub _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 root 1.39 #############################################################################
745    
746 elmex 1.9 package Crossfire::Client::Widget::MapWidget;
747 root 1.4
748 elmex 1.2 use strict;
749 elmex 1.7
750 root 1.25 use List::Util qw(min max);
751 elmex 1.2
752 root 1.16 use SDL;
753 elmex 1.2 use SDL::OpenGL;
754     use SDL::OpenGL::Constants;
755    
756 root 1.25 our @ISA = Crossfire::Client::Widget::;
757    
758 elmex 1.2 sub key_down {
759     print "MAPKEYDOWN\n";
760     }
761    
762     sub key_up {
763     }
764    
765 elmex 1.36 sub size_request {
766 root 1.52 (
767     1 + int $::WIDTH / 32,
768     1 + int $::HEIGHT / 32,
769     )
770 elmex 1.36 }
771    
772 elmex 1.11 sub _draw {
773 root 1.21 my ($self) = @_;
774    
775 root 1.25 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 root 1.27 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
787 root 1.25 } else {
788     $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
789     }
790    
791     if ($::CONN->{maph} > $sh) {
792 root 1.27 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
793 root 1.25 } else {
794     $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
795     }
796    
797 elmex 1.2 glEnable GL_TEXTURE_2D;
798     glEnable GL_BLEND;
799 root 1.35 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
800    
801     my $sw4 = ($sw + 3) & ~3;
802     my $lighting = "\x00" x ($sw4 * $sh);
803 elmex 1.2
804 root 1.25 for my $x (0 .. $sw - 1) {
805     for my $y (0 .. $sh - 1) {
806 elmex 1.2
807 root 1.25 my $cell = $map->[$x + $xofs][$y + $yofs]
808 elmex 1.2 or next;
809    
810 root 1.21 my $darkness = $cell->[0] * (1 / 255);
811     if ($darkness < 0) {
812 root 1.55 $darkness = $cell->[1] ? 0.1 : 0;
813 root 1.21 }
814 root 1.35 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
815 elmex 1.2
816 root 1.21 for my $num (grep $_, @$cell[1,2,3]) {
817 root 1.4 my $tex = $::CONN->{face}[$num]{texture} || next;
818 root 1.56
819 root 1.19 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 root 1.56 $tex->draw_quad ($px, $py, $w, $h);
826 elmex 1.2 }
827     }
828     }
829    
830 root 1.35 # 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 root 1.52 internalformat => GL_ALPHA,
845 root 1.35 format => GL_ALPHA;
846    
847     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
848 root 1.55 glColor 0.45, 0.45, 0.45, 1;
849 root 1.35 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 root 1.56
853     $lighting->draw_quad (0, 0, $sw4 * 32, $sh * 32);
854 root 1.35
855 elmex 1.2 glDisable GL_TEXTURE_2D;
856     glDisable GL_BLEND;
857     }
858    
859 root 1.16 my %DIR = (
860     SDLK_KP8, [1, "north"],
861 root 1.18 SDLK_KP9, [2, "northeast"],
862 root 1.16 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"],
868 root 1.18
869     SDLK_UP, [1, "north"],
870     SDLK_RIGHT, [3, "east"],
871     SDLK_DOWN, [5, "south"],
872     SDLK_LEFT, [7, "west"],
873 root 1.16 );
874    
875     sub 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 root 1.18 $self->{shft}++;
886 root 1.16 $::CONN->send ("command fire $DIR{$sym}[0]");
887     } elsif ($mod & KMOD_CTRL) {
888 root 1.18 $self->{ctrl}++;
889 root 1.16 $::CONN->send ("command run $DIR{$sym}[0]");
890     } else {
891 root 1.18 $::CONN->send ("command $DIR{$sym}[1]");
892 root 1.16 }
893     }
894     }
895    
896     sub key_up {
897     my ($self, $ev) = @_;
898    
899     my $mod = $ev->key_mod;
900     my $sym = $ev->key_sym;
901    
902 root 1.18 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 root 1.16 }
908     }
909    
910 root 1.39 #############################################################################
911    
912 root 1.35 package Crossfire::Client::Widget::Animator;
913    
914     use SDL::OpenGL;
915    
916     our @ISA = Crossfire::Client::Widget::Bin::;
917    
918     sub moveto {
919     my ($self, $x, $y) = @_;
920    
921     $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
922 root 1.56 $self->{speed} = 0.001;
923 root 1.35 $self->{time} = 1;
924    
925     ::animation_start $self;
926     }
927    
928     sub animate {
929     my ($self, $interval) = @_;
930    
931 root 1.56 printf "%5.2f\n", 1 / $interval;#d#
932    
933 root 1.35 $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    
945     sub _draw {
946     my ($self) = @_;
947    
948     glPushMatrix;
949 root 1.51 glRotate $self->{time} * 1000, 0, 1, 0;
950 root 1.38 $self->{children}[0]->draw;
951 root 1.35 glPopMatrix;
952     }
953    
954 root 1.51 #############################################################################
955    
956     package Crossfire::Client::Widget::Toplevel;
957    
958     our @ISA = Crossfire::Client::Widget::Container::;
959    
960     sub size_request {
961     ($::WIDTH, $::HEIGHT)
962     }
963    
964     sub 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    
973     sub update {
974     my ($self) = @_;
975    
976     $self->size_allocate ($self->size_request);
977     ::refresh ();
978     }
979    
980     sub add {
981     my ($self, $widget) = @_;
982    
983     $self->SUPER::add ($widget);
984    
985     $widget->size_allocate ($widget->size_request);
986     }
987    
988     sub draw {
989     my ($self) = @_;
990    
991     $self->_draw;
992     }
993    
994     #############################################################################
995    
996     package Crossfire::Client::Widget;
997    
998     $TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
999    
1000     1
1001 root 1.5