ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.53
Committed: Mon Apr 10 11:56:28 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.52: +1 -1 lines
Log Message:
trying grey, for fog effect

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 glBindTexture GL_TEXTURE_2D, $tex->{name};
338    
339     glBegin GL_QUADS;
340 root 1.23 glTexCoord 0, 0; glVertex 0, 0;
341     glTexCoord 0, 1; glVertex 0, $h;
342     glTexCoord 1, 1; glVertex $w, $h;
343     glTexCoord 1, 0; glVertex $w, 0;
344 elmex 1.20 glEnd;
345    
346     glDisable GL_BLEND;
347     glDisable GL_TEXTURE_2D;
348     }
349    
350 root 1.39 #############################################################################
351    
352 elmex 1.15 package Crossfire::Client::Widget::Frame;
353    
354 root 1.35 our @ISA = Crossfire::Client::Widget::Bin::;
355 elmex 1.15
356     use SDL::OpenGL;
357    
358     sub size_request {
359     my ($self) = @_;
360 root 1.39 my $chld = $self->child
361 elmex 1.15 or return (0, 0);
362 root 1.30
363     $chld->move (2, 2);
364    
365 elmex 1.15 map { $_ + 4 } $chld->size_request;
366     }
367    
368 elmex 1.36 sub size_allocate {
369     my ($self, $w, $h) = @_;
370 root 1.42
371     $self->{w} = $w;
372     $self->{h} = $h;
373 elmex 1.36
374 root 1.39 $self->child->size_allocate ($w - 4, $h - 4);
375     $self->child->move (2, 2);
376 elmex 1.36 }
377    
378 elmex 1.15 sub _draw {
379     my ($self) = @_;
380    
381 root 1.39 my $chld = $self->child;
382 elmex 1.15
383     my ($w, $h) = $chld->size_request;
384    
385     glBegin GL_QUADS;
386 root 1.30 glColor 0, 0, 0;
387 elmex 1.15 glTexCoord 0, 0; glVertex 0 , 0;
388     glTexCoord 0, 1; glVertex 0 , $h + 4;
389     glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
390     glTexCoord 1, 0; glVertex $w + 4 , 0;
391     glEnd;
392    
393 root 1.23 $chld->draw;
394 elmex 1.15 }
395    
396 root 1.39 #############################################################################
397    
398 elmex 1.31 package Crossfire::Client::Widget::FancyFrame;
399    
400 root 1.41 our @ISA = Crossfire::Client::Widget::Bin::;
401 elmex 1.31
402     use SDL::OpenGL;
403    
404 root 1.41 my @tex =
405 elmex 1.34 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
406 root 1.41 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
407 elmex 1.34
408     sub size_request {
409     my ($self) = @_;
410 root 1.39
411     my ($w, $h) = $self->SUPER::size_request;
412 elmex 1.34
413 root 1.41 $h += $tex[1]->{height};
414     $h += $tex[4]->{height};
415     $w += $tex[2]->{width};
416     $w += $tex[3]->{width};
417 elmex 1.34
418 elmex 1.36 ($w, $h)
419     }
420    
421     sub size_allocate {
422     my ($self, $w, $h) = @_;
423    
424 root 1.40 $self->SUPER::size_allocate ($w, $h);
425    
426 root 1.41 $h -= $tex[1]->{height};
427     $h -= $tex[4]->{height};
428     $w -= $tex[2]->{width};
429     $w -= $tex[3]->{width};
430 elmex 1.36
431     $h = $h < 0 ? 0 : $h;
432     $w = $w < 0 ? 0 : $w;
433 root 1.43
434 root 1.39 $self->child->size_allocate ($w, $h);
435 root 1.43 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
436 elmex 1.34 }
437    
438     sub _draw {
439     my ($self) = @_;
440    
441 root 1.43 my ($w, $h) = ($self->{w}, $self->{h});
442     my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
443 elmex 1.34
444     glEnable GL_BLEND;
445     glEnable GL_TEXTURE_2D;
446     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
447 elmex 1.36 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
448 elmex 1.34
449 root 1.41 my $top = $tex[1];
450 elmex 1.36 glBindTexture GL_TEXTURE_2D, $top->{name};
451 elmex 1.34
452 elmex 1.36 glBegin GL_QUADS;
453     glTexCoord 0, 0; glVertex 0 , 0;
454     glTexCoord 0, 1; glVertex 0 , $top->{height};
455     glTexCoord 1, 1; glVertex $w , $top->{height};
456     glTexCoord 1, 0; glVertex $w , 0;
457     glEnd;
458 elmex 1.34
459 root 1.41 my $left = $tex[3];
460 elmex 1.36 glBindTexture GL_TEXTURE_2D, $left->{name};
461 elmex 1.34
462 elmex 1.36 glBegin GL_QUADS;
463     glTexCoord 0, 0; glVertex 0 , $top->{height};
464     glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
465     glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
466     glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
467     glEnd;
468 elmex 1.34
469 root 1.41 my $right = $tex[2];
470 elmex 1.36 glBindTexture GL_TEXTURE_2D, $right->{name};
471 elmex 1.34
472 elmex 1.36 glBegin GL_QUADS;
473     glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
474     glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
475     glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
476     glTexCoord 1, 0; glVertex $w , $top->{height};
477     glEnd;
478 elmex 1.34
479 root 1.41 my $bottom = $tex[4];
480 elmex 1.36 glBindTexture GL_TEXTURE_2D, $bottom->{name};
481 elmex 1.34
482 elmex 1.36 glBegin GL_QUADS;
483     glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
484     glTexCoord 0, 1; glVertex 0 , $h;
485     glTexCoord 1, 1; glVertex $w , $h;
486     glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
487     glEnd;
488 elmex 1.34
489 root 1.41 my $bg = $tex[0];
490 elmex 1.36 glBindTexture GL_TEXTURE_2D, $bg->{name};
491     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
492     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
493     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
494 elmex 1.34
495 elmex 1.36 my $rep_x = $cw / $bg->{width};
496     my $rep_y = $ch / $bg->{height};
497 elmex 1.34
498 elmex 1.36 glBegin GL_QUADS;
499     glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
500     glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
501     glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
502     glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
503     glEnd;
504 elmex 1.34
505     glDisable GL_BLEND;
506     glDisable GL_TEXTURE_2D;
507 elmex 1.36
508 root 1.39 $self->child->draw;
509 elmex 1.36
510 elmex 1.34 }
511 elmex 1.31
512 root 1.39 #############################################################################
513    
514 elmex 1.15 package Crossfire::Client::Widget::Table;
515    
516 root 1.35 our @ISA = Crossfire::Client::Widget::Bin::;
517 elmex 1.15
518     use SDL::OpenGL;
519    
520     sub add {
521     my ($self, $x, $y, $chld) = @_;
522 root 1.38 my $old_chld = $self->{children}[$y][$x];
523 elmex 1.32
524 root 1.38 $self->{children}[$y][$x] = $chld;
525 elmex 1.32 $chld->set_parent ($self);
526     $self->update;
527 elmex 1.15 }
528    
529     sub max_row_height {
530     my ($self, $row) = @_;
531    
532     my $hs = 0;
533 root 1.38 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) {
534     my $c = $self->{children}->[$row]->[$xi];
535 elmex 1.17 if ($c) {
536     my ($w, $h) = $c->size_request;
537     if ($hs < $h) { $hs = $h }
538     }
539 elmex 1.15 }
540     return $hs;
541     }
542    
543     sub max_col_width {
544     my ($self, $col) = @_;
545    
546     my $ws = 0;
547 root 1.38 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) {
548     my $c = ($self->{children}->[$yi] || [])->[$col];
549 elmex 1.17 if ($c) {
550     my ($w, $h) = $c->size_request;
551     if ($ws < $w) { $ws = $w }
552     }
553 elmex 1.15 }
554     return $ws;
555     }
556    
557     sub size_request {
558     my ($self) = @_;
559    
560     my ($hs, $ws) = (0, 0);
561    
562 root 1.38 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
563 elmex 1.15 $hs += $self->max_row_height ($yi);
564     }
565    
566 root 1.38 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
567 elmex 1.15 my $wm = 0;
568 root 1.38 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
569 elmex 1.15 $wm += $self->max_col_width ($xi)
570     }
571     if ($ws < $wm) { $ws = $wm }
572     }
573    
574     return ($ws, $hs);
575     }
576    
577     sub _draw {
578     my ($self) = @_;
579    
580     my $y = 0;
581 root 1.38 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
582 elmex 1.15 my $x = 0;
583    
584 root 1.38 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
585 elmex 1.15
586 root 1.38 my $c = $self->{children}->[$yi]->[$xi];
587 elmex 1.26 if ($c) {
588     $c->move ($x, $y, 0); #TODO: Move to size_request
589     $c->draw if $c;
590     }
591 elmex 1.15
592     $x += $self->max_col_width ($xi);
593     }
594    
595     $y += $self->max_row_height ($yi);
596     }
597     }
598    
599 root 1.39 #############################################################################
600    
601 elmex 1.15 package Crossfire::Client::Widget::VBox;
602    
603 root 1.38 our @ISA = Crossfire::Client::Widget::Container::;
604 elmex 1.15
605     use SDL::OpenGL;
606    
607 root 1.43 sub size_request {
608     my ($self) = @_;
609    
610     my @alloc = map [$_->size_request], @{$self->{children}};
611    
612     (
613     (List::Util::max map $_->[0], @alloc),
614     (List::Util::sum map $_->[1], @alloc),
615     )
616     }
617    
618 elmex 1.36 sub size_allocate {
619     my ($self, $w, $h) = @_;
620    
621     $self->w ($w);
622     $self->h ($h);
623    
624     my $exp;
625     my @oth;
626     # find expand widget
627 root 1.38 for (@{$self->{children}}) {
628 elmex 1.36 if ($_->{expand}) {
629     $exp = $_;
630     last;
631     }
632     push @oth, $_;
633     }
634    
635     my ($ow, $oh);
636    
637     # get sizes of other widgets
638     for (@oth) {
639     my ($w, $h) = $_->size_request;
640     $oh += $h;
641     if ($ow < $w) { $ow = $w }
642     }
643    
644     my $y = 0;
645 root 1.38 for (@{$self->{children}}) {
646 elmex 1.36 $_->move (0, $y);
647    
648     if ($_ == $exp) {
649     $_->size_allocate ($w, $h - $oh);
650     $y += $h - $oh;
651     } else {
652     my ($cw, $h) = $_->size_request;
653     $_->size_allocate ($w, $h);
654     $y += $h;
655     }
656     }
657     }
658    
659 root 1.39 #############################################################################
660    
661 root 1.10 package Crossfire::Client::Widget::Label;
662    
663 root 1.12 our @ISA = Crossfire::Client::Widget::;
664    
665 root 1.10 use SDL::OpenGL;
666    
667     sub new {
668 root 1.28 my ($class, $x, $y, $z, $height, $text) = @_;
669 root 1.10
670 root 1.51 $height ||= $::FONTSIZE;
671    
672 root 1.33 # TODO: color, and make height, xyz etc. optional
673 root 1.28 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
674 root 1.10
675 elmex 1.15 $self->set_text ($text);
676 root 1.10
677     $self
678     }
679    
680 elmex 1.15 sub set_text {
681     my ($self, $text) = @_;
682 root 1.28
683     $self->{text} = $text;
684 elmex 1.32 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
685 root 1.28
686 elmex 1.32 $self->update;
687 elmex 1.15 }
688    
689     sub get_text {
690     my ($self, $text) = @_;
691 root 1.28
692 elmex 1.15 $self->{text}
693     }
694    
695 root 1.14 sub size_request {
696     my ($self) = @_;
697    
698 root 1.51 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
699     (
700     $self->{texture}{width},
701     $self->{texture}{height},
702     )
703     } else {
704     my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
705    
706     ($w, $h)
707     }
708    
709 root 1.14 }
710    
711 elmex 1.11 sub _draw {
712 root 1.10 my ($self) = @_;
713    
714     my $tex = $self->{texture};
715    
716 root 1.12 glEnable GL_BLEND;
717 root 1.10 glEnable GL_TEXTURE_2D;
718 root 1.30 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
719 root 1.28 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
720 root 1.10 glBindTexture GL_TEXTURE_2D, $tex->{name};
721    
722 elmex 1.36 glColor 1, 0, 0, 1; # TODO color
723 root 1.12
724 root 1.10 glBegin GL_QUADS;
725     glTexCoord 0, 0; glVertex 0 , 0;
726     glTexCoord 0, 1; glVertex 0 , $tex->{height};
727     glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
728     glTexCoord 1, 0; glVertex $tex->{width}, 0;
729     glEnd;
730    
731 root 1.12 glDisable GL_BLEND;
732 root 1.10 glDisable GL_TEXTURE_2D;
733     }
734    
735 root 1.39 #############################################################################
736    
737 root 1.51 package Crossfire::Client::Widget::Entry;
738 elmex 1.31
739     our @ISA = Crossfire::Client::Widget::Label::;
740    
741     use SDL;
742     use SDL::OpenGL;
743    
744     sub key_down {
745     my ($self, $ev) = @_;
746    
747     my $mod = $ev->key_mod;
748     my $sym = $ev->key_sym;
749    
750     my $uni = $ev->key_unicode;
751    
752     my $text = $self->get_text;
753    
754     if ($sym == SDLK_BACKSPACE) {
755     substr $text, -1, 1, '';
756     } elsif ($uni) {
757     $text .= chr $uni;
758 root 1.51 print "$uni <$text>\n";#d#
759 elmex 1.31 }
760 root 1.51
761 elmex 1.31 $self->set_text ($text);
762     }
763    
764 root 1.51 sub button_down {
765     my ($self, $ev) = @_;
766    
767     $self->focus_in;
768     }
769    
770     sub _draw {
771     my ($self) = @_;
772    
773     if ($FOCUS == $self) {
774     glColor 1, 1, 1;
775     } else {
776     glColor 0.7, 0.7, 0.7;
777     }
778    
779     glBegin GL_QUADS;
780     glVertex 0 , 0;
781     glVertex 0 , $self->{h} - 1;
782     glVertex $self->{w} - 1, $self->{h} - 1;
783     glVertex $self->{w} - 1, 0;
784     glEnd;
785    
786     $self->SUPER::_draw;
787     }
788    
789 root 1.39 #############################################################################
790    
791 elmex 1.9 package Crossfire::Client::Widget::MapWidget;
792 root 1.4
793 elmex 1.2 use strict;
794 elmex 1.7
795 root 1.25 use List::Util qw(min max);
796 elmex 1.2
797 root 1.16 use SDL;
798 elmex 1.2 use SDL::OpenGL;
799     use SDL::OpenGL::Constants;
800    
801 root 1.25 our @ISA = Crossfire::Client::Widget::;
802    
803 elmex 1.2 sub key_down {
804     print "MAPKEYDOWN\n";
805     }
806    
807     sub key_up {
808     }
809    
810 elmex 1.36 sub size_request {
811 root 1.52 (
812     1 + int $::WIDTH / 32,
813     1 + int $::HEIGHT / 32,
814     )
815 elmex 1.36 }
816    
817 elmex 1.11 sub _draw {
818 root 1.21 my ($self) = @_;
819    
820 root 1.25 my $mx = $::CONN->{mapx};
821     my $my = $::CONN->{mapy};
822    
823     my $map = $::CONN->{map};
824    
825     my ($xofs, $yofs);
826    
827     my $sw = 1 + int $::WIDTH / 32;
828     my $sh = 1 + int $::HEIGHT / 32;
829    
830     if ($::CONN->{mapw} > $sw) {
831 root 1.27 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
832 root 1.25 } else {
833     $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
834     }
835    
836     if ($::CONN->{maph} > $sh) {
837 root 1.27 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
838 root 1.25 } else {
839     $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
840     }
841    
842 elmex 1.2 glEnable GL_TEXTURE_2D;
843     glEnable GL_BLEND;
844 root 1.35 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
845    
846     my $sw4 = ($sw + 3) & ~3;
847     my $lighting = "\x00" x ($sw4 * $sh);
848 elmex 1.2
849 root 1.25 for my $x (0 .. $sw - 1) {
850     for my $y (0 .. $sh - 1) {
851 elmex 1.2
852 root 1.25 my $cell = $map->[$x + $xofs][$y + $yofs]
853 elmex 1.2 or next;
854    
855 root 1.21 my $darkness = $cell->[0] * (1 / 255);
856     if ($darkness < 0) {
857 root 1.52 $darkness = $cell->[1] ? 0.2 : 0;
858 root 1.21 }
859 root 1.35 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
860 elmex 1.2
861 root 1.21 for my $num (grep $_, @$cell[1,2,3]) {
862 root 1.4 my $tex = $::CONN->{face}[$num]{texture} || next;
863 elmex 1.2
864 root 1.4 glBindTexture GL_TEXTURE_2D, $tex->{name};
865 elmex 1.2
866 root 1.19 my $w = $tex->{width};
867     my $h = $tex->{height};
868    
869     my $px = ($x + 1) * 32 - $w;
870     my $py = ($y + 1) * 32 - $h;
871    
872 elmex 1.2 glBegin GL_QUADS;
873 root 1.19 glTexCoord 0, 0; glVertex $px , $py;
874     glTexCoord 0, 1; glVertex $px , $py + $h;
875     glTexCoord 1, 1; glVertex $px + $w, $py + $h;
876     glTexCoord 1, 0; glVertex $px + $w, $py;
877 elmex 1.2 glEnd;
878     }
879     }
880     }
881    
882 root 1.35 # if (1) { # higher quality darkness
883     # $lighting =~ s/(.)/$1$1$1/gs;
884     # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
885     #
886     # $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
887     #
888     # $lighting = $pb->get_pixels;
889     # $lighting =~ s/(.)../$1/gs;
890     # }
891    
892     $lighting = new Crossfire::Client::Texture
893     width => $sw4,
894     height => $sh,
895     data => $lighting,
896 root 1.52 internalformat => GL_ALPHA,
897 root 1.35 format => GL_ALPHA;
898    
899     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
900 root 1.53 glColor 0.5, 0.5, 0.5, 1;
901 root 1.35 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
902     glBindTexture GL_TEXTURE_2D, $lighting->{name};
903     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
904     glBegin GL_QUADS;
905     glTexCoord 0, 0; glVertex 0 , 0;
906     glTexCoord 0, 1; glVertex 0 , $sh * 32;
907     glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
908     glTexCoord 1, 0; glVertex $sw4 * 32, 0;
909     glEnd;
910    
911 elmex 1.2 glDisable GL_TEXTURE_2D;
912     glDisable GL_BLEND;
913     }
914    
915 root 1.16 my %DIR = (
916     SDLK_KP8, [1, "north"],
917 root 1.18 SDLK_KP9, [2, "northeast"],
918 root 1.16 SDLK_KP6, [3, "east"],
919     SDLK_KP3, [4, "southeast"],
920     SDLK_KP2, [5, "south"],
921     SDLK_KP1, [6, "southwest"],
922     SDLK_KP4, [7, "west"],
923     SDLK_KP7, [8, "northwest"],
924 root 1.18
925     SDLK_UP, [1, "north"],
926     SDLK_RIGHT, [3, "east"],
927     SDLK_DOWN, [5, "south"],
928     SDLK_LEFT, [7, "west"],
929 root 1.16 );
930    
931     sub key_down {
932     my ($self, $ev) = @_;
933    
934     my $mod = $ev->key_mod;
935     my $sym = $ev->key_sym;
936    
937     if ($sym == SDLK_KP5) {
938     $::CONN->send ("command stay fire");
939     } elsif (exists $DIR{$sym}) {
940     if ($mod & KMOD_SHIFT) {
941 root 1.18 $self->{shft}++;
942 root 1.16 $::CONN->send ("command fire $DIR{$sym}[0]");
943     } elsif ($mod & KMOD_CTRL) {
944 root 1.18 $self->{ctrl}++;
945 root 1.16 $::CONN->send ("command run $DIR{$sym}[0]");
946     } else {
947 root 1.18 $::CONN->send ("command $DIR{$sym}[1]");
948 root 1.16 }
949     }
950     }
951    
952     sub key_up {
953     my ($self, $ev) = @_;
954    
955     my $mod = $ev->key_mod;
956     my $sym = $ev->key_sym;
957    
958 root 1.18 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
959     $::CONN->send ("command fire_stop");
960     }
961     if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
962     $::CONN->send ("command run_stop");
963 root 1.16 }
964     }
965    
966 root 1.39 #############################################################################
967    
968 root 1.35 package Crossfire::Client::Widget::Animator;
969    
970     use SDL::OpenGL;
971    
972     our @ISA = Crossfire::Client::Widget::Bin::;
973    
974     sub moveto {
975     my ($self, $x, $y) = @_;
976    
977     $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
978 root 1.51 $self->{speed} = 2;
979 root 1.35 $self->{time} = 1;
980    
981     ::animation_start $self;
982     }
983    
984     sub animate {
985     my ($self, $interval) = @_;
986    
987     $self->{time} -= $interval * $self->{speed};
988     if ($self->{time} <= 0) {
989     $self->{time} = 0;
990     ::animation_stop $self;
991     }
992    
993     my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
994    
995     $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
996     $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
997     }
998    
999     sub _draw {
1000     my ($self) = @_;
1001    
1002     glPushMatrix;
1003 root 1.51 glRotate $self->{time} * 1000, 0, 1, 0;
1004 root 1.38 $self->{children}[0]->draw;
1005 root 1.35 glPopMatrix;
1006     }
1007    
1008 root 1.51 #############################################################################
1009    
1010     package Crossfire::Client::Widget::Toplevel;
1011    
1012     our @ISA = Crossfire::Client::Widget::Container::;
1013    
1014     sub size_request {
1015     ($::WIDTH, $::HEIGHT)
1016     }
1017    
1018     sub size_allocate {
1019     my ($self, $w, $h) = @_;
1020    
1021     $self->SUPER::size_allocate ($w, $h);
1022    
1023     $_->size_allocate ($_->size_request)
1024     for @{$self->{children}};
1025     }
1026    
1027     sub update {
1028     my ($self) = @_;
1029    
1030     $self->size_allocate ($self->size_request);
1031     ::refresh ();
1032     }
1033    
1034     sub add {
1035     my ($self, $widget) = @_;
1036    
1037     $self->SUPER::add ($widget);
1038    
1039     $widget->size_allocate ($widget->size_request);
1040     }
1041    
1042     sub draw {
1043     my ($self) = @_;
1044    
1045     $self->_draw;
1046     }
1047    
1048     #############################################################################
1049    
1050     package Crossfire::Client::Widget;
1051    
1052     $TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
1053    
1054     1
1055 root 1.5