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.17 by elmex, Sat Apr 8 13:34:19 2006 UTC vs.
Revision 1.51 by root, Mon Apr 10 11:41:17 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 glBindTexture GL_TEXTURE_2D, $tex->{name};
338
339 glBegin GL_QUADS;
340 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 glEnd;
345
346 glDisable GL_BLEND;
347 glDisable GL_TEXTURE_2D;
348}
349
350#############################################################################
97 351
98package Crossfire::Client::Widget::Frame; 352package Crossfire::Client::Widget::Frame;
99 353
100our @ISA = Crossfire::Client::Widget::Container::; 354our @ISA = Crossfire::Client::Widget::Bin::;
101 355
102use SDL::OpenGL; 356use SDL::OpenGL;
103 357
104sub size_request { 358sub size_request {
105 my ($self) = @_; 359 my ($self) = @_;
106 my $chld = $self->get 360 my $chld = $self->child
107 or return (0, 0); 361 or return (0, 0);
362
363 $chld->move (2, 2);
364
108 map { $_ + 4 } $chld->size_request; 365 map { $_ + 4 } $chld->size_request;
109} 366}
110 367
368sub size_allocate {
369 my ($self, $w, $h) = @_;
370
371 $self->{w} = $w;
372 $self->{h} = $h;
373
374 $self->child->size_allocate ($w - 4, $h - 4);
375 $self->child->move (2, 2);
376}
377
111sub _draw { 378sub _draw {
112 my ($self) = @_; 379 my ($self) = @_;
113 380
114 my $chld = $self->get; 381 my $chld = $self->child;
115 382
116 my ($w, $h) = $chld->size_request; 383 my ($w, $h) = $chld->size_request;
117 384
118 glColor 1, 0, 0;
119 glBegin GL_QUADS; 385 glBegin GL_QUADS;
386 glColor 0, 0, 0;
120 glTexCoord 0, 0; glVertex 0 , 0; 387 glTexCoord 0, 0; glVertex 0 , 0;
121 glTexCoord 0, 1; glVertex 0 , $h + 4; 388 glTexCoord 0, 1; glVertex 0 , $h + 4;
122 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 389 glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
123 glTexCoord 1, 0; glVertex $w + 4 , 0; 390 glTexCoord 1, 0; glVertex $w + 4 , 0;
124 glEnd; 391 glEnd;
125 392
126 glPushMatrix;
127 glTranslate (2, 2, 0);
128 $chld->_draw; 393 $chld->draw;
129 glPopMatrix;
130} 394}
395
396#############################################################################
397
398package Crossfire::Client::Widget::FancyFrame;
399
400our @ISA = Crossfire::Client::Widget::Bin::;
401
402use SDL::OpenGL;
403
404my @tex =
405 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
406 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
407
408sub size_request {
409 my ($self) = @_;
410
411 my ($w, $h) = $self->SUPER::size_request;
412
413 $h += $tex[1]->{height};
414 $h += $tex[4]->{height};
415 $w += $tex[2]->{width};
416 $w += $tex[3]->{width};
417
418 ($w, $h)
419}
420
421sub size_allocate {
422 my ($self, $w, $h) = @_;
423
424 $self->SUPER::size_allocate ($w, $h);
425
426 $h -= $tex[1]->{height};
427 $h -= $tex[4]->{height};
428 $w -= $tex[2]->{width};
429 $w -= $tex[3]->{width};
430
431 $h = $h < 0 ? 0 : $h;
432 $w = $w < 0 ? 0 : $w;
433
434 $self->child->size_allocate ($w, $h);
435 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
436}
437
438sub _draw {
439 my ($self) = @_;
440
441 my ($w, $h) = ($self->{w}, $self->{h});
442 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
443
444 glEnable GL_BLEND;
445 glEnable GL_TEXTURE_2D;
446 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
447 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
448
449 my $top = $tex[1];
450 glBindTexture GL_TEXTURE_2D, $top->{name};
451
452 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
459 my $left = $tex[3];
460 glBindTexture GL_TEXTURE_2D, $left->{name};
461
462 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
469 my $right = $tex[2];
470 glBindTexture GL_TEXTURE_2D, $right->{name};
471
472 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
479 my $bottom = $tex[4];
480 glBindTexture GL_TEXTURE_2D, $bottom->{name};
481
482 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
489 my $bg = $tex[0];
490 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
495 my $rep_x = $cw / $bg->{width};
496 my $rep_y = $ch / $bg->{height};
497
498 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
505 glDisable GL_BLEND;
506 glDisable GL_TEXTURE_2D;
507
508 $self->child->draw;
509
510}
511
512#############################################################################
131 513
132package Crossfire::Client::Widget::Table; 514package Crossfire::Client::Widget::Table;
133 515
134our @ISA = Crossfire::Client::Widget::Container::; 516our @ISA = Crossfire::Client::Widget::Bin::;
135 517
136use SDL::OpenGL; 518use SDL::OpenGL;
137 519
138sub add { 520sub add {
139 my ($self, $x, $y, $chld) = @_; 521 my ($self, $x, $y, $chld) = @_;
522 my $old_chld = $self->{children}[$y][$x];
523
140 $self->{childs}[$y][$x] = $chld; 524 $self->{children}[$y][$x] = $chld;
525 $chld->set_parent ($self);
526 $self->update;
141} 527}
142 528
143sub max_row_height { 529sub max_row_height {
144 my ($self, $row) = @_; 530 my ($self, $row) = @_;
145 531
146 my $hs = 0; 532 my $hs = 0;
147 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { 533 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) {
148 my $c = $self->{childs}->[$row]->[$xi]; 534 my $c = $self->{children}->[$row]->[$xi];
149 if ($c) { 535 if ($c) {
150 my ($w, $h) = $c->size_request; 536 my ($w, $h) = $c->size_request;
151 if ($hs < $h) { $hs = $h } 537 if ($hs < $h) { $hs = $h }
152 } 538 }
153 } 539 }
156 542
157sub max_col_width { 543sub max_col_width {
158 my ($self, $col) = @_; 544 my ($self, $col) = @_;
159 545
160 my $ws = 0; 546 my $ws = 0;
161 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { 547 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) {
162 my $c = ($self->{childs}->[$yi] || [])->[$col]; 548 my $c = ($self->{children}->[$yi] || [])->[$col];
163 if ($c) { 549 if ($c) {
164 my ($w, $h) = $c->size_request; 550 my ($w, $h) = $c->size_request;
165 if ($ws < $w) { $ws = $w } 551 if ($ws < $w) { $ws = $w }
166 } 552 }
167 } 553 }
171sub size_request { 557sub size_request {
172 my ($self) = @_; 558 my ($self) = @_;
173 559
174 my ($hs, $ws) = (0, 0); 560 my ($hs, $ws) = (0, 0);
175 561
176 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 562 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
177 $hs += $self->max_row_height ($yi); 563 $hs += $self->max_row_height ($yi);
178 } 564 }
179 565
180 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 566 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
181 my $wm = 0; 567 my $wm = 0;
182 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 568 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
183 $wm += $self->max_col_width ($xi) 569 $wm += $self->max_col_width ($xi)
184 } 570 }
185 if ($ws < $wm) { $ws = $wm } 571 if ($ws < $wm) { $ws = $wm }
186 } 572 }
187 573
190 576
191sub _draw { 577sub _draw {
192 my ($self) = @_; 578 my ($self) = @_;
193 579
194 my $y = 0; 580 my $y = 0;
195 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 581 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
196 my $x = 0; 582 my $x = 0;
197 583
198 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 584 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
199 585
200 glPushMatrix;
201 glTranslate ($x, $y, 0);
202 my $c = $self->{childs}->[$yi]->[$xi]; 586 my $c = $self->{children}->[$yi]->[$xi];
587 if ($c) {
588 $c->move ($x, $y, 0); #TODO: Move to size_request
203 $c->_draw if $c; 589 $c->draw if $c;
204 glPopMatrix; 590 }
205 591
206 $x += $self->max_col_width ($xi); 592 $x += $self->max_col_width ($xi);
207 } 593 }
208 594
209 $y += $self->max_row_height ($yi); 595 $y += $self->max_row_height ($yi);
210 } 596 }
211} 597}
212 598
599#############################################################################
600
213package Crossfire::Client::Widget::VBox; 601package Crossfire::Client::Widget::VBox;
214 602
215our @ISA = Crossfire::Client::Widget::Container::; 603our @ISA = Crossfire::Client::Widget::Container::;
216 604
217use SDL::OpenGL; 605use SDL::OpenGL;
218 606
219sub add {
220 my ($self, $chld) = @_;
221 push @{$self->{childs}}, $chld;
222}
223
224sub size_request { 607sub size_request {
225 my ($self) = @_; 608 my ($self) = @_;
226 609
227 my ($hs, $ws) = (0, 0); 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
618sub 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
228 for (@{$self->{childs} || []}) { 627 for (@{$self->{children}}) {
628 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) {
229 my ($w, $h) = $_->size_request; 639 my ($w, $h) = $_->size_request;
230 $hs += $h; 640 $oh += $h;
231 if ($ws < $w) { $ws = $w } 641 if ($ow < $w) { $ow = $w }
232 } 642 }
233 643
234 return ($ws, $hs); 644 my $y = 0;
235}
236
237sub _draw {
238 my ($self) = @_;
239
240 my ($x, $y);
241 for (@{$self->{childs} || []}) { 645 for (@{$self->{children}}) {
242 glPushMatrix; 646 $_->move (0, $y);
243 glTranslate (0, $y, 0); 647
244 $_->_draw; 648 if ($_ == $exp) {
245 glPopMatrix; 649 $_->size_allocate ($w, $h - $oh);
650 $y += $h - $oh;
651 } else {
246 my ($w, $h) = $_->size_request; 652 my ($cw, $h) = $_->size_request;
653 $_->size_allocate ($w, $h);
247 $y += $h; 654 $y += $h;
655 }
248 } 656 }
249} 657}
658
659#############################################################################
250 660
251package Crossfire::Client::Widget::Label; 661package Crossfire::Client::Widget::Label;
252 662
253our @ISA = Crossfire::Client::Widget::; 663our @ISA = Crossfire::Client::Widget::;
254 664
255use SDL::OpenGL; 665use SDL::OpenGL;
256 666
257sub new { 667sub new {
258 my ($class, $x, $y, $z, $ttf, $text) = @_; 668 my ($class, $x, $y, $z, $height, $text) = @_;
259 669
670 $height ||= $::FONTSIZE;
671
672 # TODO: color, and make height, xyz etc. optional
260 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf); 673 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
261 674
262 $self->set_text ($text); 675 $self->set_text ($text);
263 676
264 $self 677 $self
265} 678}
266 679
267sub set_text { 680sub set_text {
268 my ($self, $text) = @_; 681 my ($self, $text) = @_;
682
683 $self->{text} = $text;
269 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text; 684 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
685
686 $self->update;
270} 687}
271 688
272sub get_text { 689sub get_text {
273 my ($self, $text) = @_; 690 my ($self, $text) = @_;
691
274 $self->{text} 692 $self->{text}
275} 693}
276 694
277sub size_request { 695sub size_request {
278 my ($self) = @_; 696 my ($self) = @_;
279 697
280 ( 698 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
699 (
281 $self->{texture}{width}, 700 $self->{texture}{width},
282 $self->{texture}{height}, 701 $self->{texture}{height},
702 )
703 } else {
704 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
705
706 ($w, $h)
283 ) 707 }
708
284} 709}
285 710
286sub _draw { 711sub _draw {
287 my ($self) = @_; 712 my ($self) = @_;
288 713
289 my $tex = $self->{texture}; 714 my $tex = $self->{texture};
290
291 $self->{x}--;
292 715
293 glEnable GL_BLEND; 716 glEnable GL_BLEND;
294 glEnable GL_TEXTURE_2D; 717 glEnable GL_TEXTURE_2D;
718 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 719 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
296 glBindTexture GL_TEXTURE_2D, $tex->{name}; 720 glBindTexture GL_TEXTURE_2D, $tex->{name};
297 721
298 glColor 1, 1, 1; 722 glColor 1, 0, 0, 1; # TODO color
299 723
300 glBegin GL_QUADS; 724 glBegin GL_QUADS;
301 glTexCoord 0, 0; glVertex 0 , 0; 725 glTexCoord 0, 0; glVertex 0 , 0;
302 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 726 glTexCoord 0, 1; glVertex 0 , $tex->{height};
303 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 727 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
306 730
307 glDisable GL_BLEND; 731 glDisable GL_BLEND;
308 glDisable GL_TEXTURE_2D; 732 glDisable GL_TEXTURE_2D;
309} 733}
310 734
735#############################################################################
736
311package Crossfire::Client::Widget::TextView; 737package Crossfire::Client::Widget::Entry;
738
739our @ISA = Crossfire::Client::Widget::Label::;
740
741use SDL;
742use SDL::OpenGL;
743
744sub 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 print "$uni <$text>\n";#d#
759 }
760
761 $self->set_text ($text);
762}
763
764sub button_down {
765 my ($self, $ev) = @_;
766
767 $self->focus_in;
768}
769
770sub _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#############################################################################
790
791package Crossfire::Client::Widget::MapWidget;
312 792
313use strict; 793use strict;
314 794
315our @ISA = qw/Crossfire::Client::Widget/; 795use List::Util qw(min max);
316
317use SDL::OpenGL;
318use SDL::OpenGL::Constants;
319
320sub add_line {
321 my ($self, $line) = @_;
322 push @{$self->{lines}}, $line;
323}
324
325sub _draw {
326 my ($self) = @_;
327
328}
329
330package Crossfire::Client::Widget::MapWidget;
331
332use strict;
333
334our @ISA = qw/Crossfire::Client::Widget/;
335 796
336use SDL; 797use SDL;
337use SDL::OpenGL; 798use SDL::OpenGL;
338use SDL::OpenGL::Constants; 799use SDL::OpenGL::Constants;
339 800
801our @ISA = Crossfire::Client::Widget::;
802
340sub key_down { 803sub key_down {
341 print "MAPKEYDOWN\n"; 804 print "MAPKEYDOWN\n";
342} 805}
343 806
344sub key_up { 807sub key_up {
345} 808}
346 809
810sub size_request {
811
812}
813
814sub size_allocate {
815}
816
347sub _draw { 817sub _draw {
818 my ($self) = @_;
819
820 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 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
832 } else {
833 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
834 }
835
836 if ($::CONN->{maph} > $sh) {
837 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
838 } else {
839 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
840 }
841
348 glEnable GL_TEXTURE_2D; 842 glEnable GL_TEXTURE_2D;
349 glEnable GL_BLEND; 843 glEnable GL_BLEND;
350 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 844 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
351 845
352 my $map = $::CONN->{map}; 846 my $sw4 = ($sw + 3) & ~3;
847 my $lighting = "\x00" x ($sw4 * $sh);
353 848
354 for my $x (0 .. $::CONN->{mapw} - 1) { 849 for my $x (0 .. $sw - 1) {
355 for my $y (0 .. $::CONN->{maph} - 1) { 850 for my $y (0 .. $sh - 1) {
356 851
357 my $cell = $map->[$x][$y] 852 my $cell = $map->[$x + $xofs][$y + $yofs]
358 or next; 853 or next;
359 854
360 my $darkness = $cell->[3] * (1 / 255); 855 my $darkness = $cell->[0] * (1 / 255);
361 glColor $darkness, $darkness, $darkness; 856 if ($darkness < 0) {
857 $darkness = 0.15;
858 }
859 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
362 860
363 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 861 for my $num (grep $_, @$cell[1,2,3]) {
364 my $tex = $::CONN->{face}[$num]{texture} || next; 862 my $tex = $::CONN->{face}[$num]{texture} || next;
365 863
366 glBindTexture GL_TEXTURE_2D, $tex->{name}; 864 glBindTexture GL_TEXTURE_2D, $tex->{name};
367 865
866 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
368 glBegin GL_QUADS; 872 glBegin GL_QUADS;
369 glTexCoord 0, 0; glVertex $x * 32 , $y * 32; 873 glTexCoord 0, 0; glVertex $px , $py;
370 glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32; 874 glTexCoord 0, 1; glVertex $px , $py + $h;
371 glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32; 875 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
372 glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32; 876 glTexCoord 1, 0; glVertex $px + $w, $py;
373 glEnd; 877 glEnd;
374 } 878 }
375 } 879 }
376 } 880 }
377 881
882# 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 internalformat => GL_ALPHA4,
897 format => GL_ALPHA;
898
899 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
900 glColor 0, 0, 0, 0.75;
901 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
378 glDisable GL_TEXTURE_2D; 911 glDisable GL_TEXTURE_2D;
379 glDisable GL_BLEND; 912 glDisable GL_BLEND;
380} 913}
381 914
382my %DIR = ( 915my %DIR = (
383 SDLK_KP8, [1, "north"], 916 SDLK_KP8, [1, "north"],
384 SDLK_KP9, [2, "northest"], 917 SDLK_KP9, [2, "northeast"],
385 SDLK_KP6, [3, "east"], 918 SDLK_KP6, [3, "east"],
386 SDLK_KP3, [4, "southeast"], 919 SDLK_KP3, [4, "southeast"],
387 SDLK_KP2, [5, "south"], 920 SDLK_KP2, [5, "south"],
388 SDLK_KP1, [6, "southwest"], 921 SDLK_KP1, [6, "southwest"],
389 SDLK_KP4, [7, "west"], 922 SDLK_KP4, [7, "west"],
390 SDLK_KP7, [8, "northwest"], 923 SDLK_KP7, [8, "northwest"],
924
925 SDLK_UP, [1, "north"],
926 SDLK_RIGHT, [3, "east"],
927 SDLK_DOWN, [5, "south"],
928 SDLK_LEFT, [7, "west"],
391); 929);
392 930
393sub key_down { 931sub key_down {
394 my ($self, $ev) = @_; 932 my ($self, $ev) = @_;
395 933
398 936
399 if ($sym == SDLK_KP5) { 937 if ($sym == SDLK_KP5) {
400 $::CONN->send ("command stay fire"); 938 $::CONN->send ("command stay fire");
401 } elsif (exists $DIR{$sym}) { 939 } elsif (exists $DIR{$sym}) {
402 if ($mod & KMOD_SHIFT) { 940 if ($mod & KMOD_SHIFT) {
941 $self->{shft}++;
403 $::CONN->send ("command fire $DIR{$sym}[0]"); 942 $::CONN->send ("command fire $DIR{$sym}[0]");
404 } elsif ($mod & KMOD_CTRL) { 943 } elsif ($mod & KMOD_CTRL) {
944 $self->{ctrl}++;
405 $::CONN->send ("command run $DIR{$sym}[0]"); 945 $::CONN->send ("command run $DIR{$sym}[0]");
406 } else { 946 } else {
947 $::CONN->send ("command $DIR{$sym}[1]");
407 } 948 }
408 } 949 }
409} 950}
410 951
411sub key_up { 952sub key_up {
412 my ($self, $ev) = @_; 953 my ($self, $ev) = @_;
413 954
414 my $mod = $ev->key_mod; 955 my $mod = $ev->key_mod;
415 my $sym = $ev->key_sym; 956 my $sym = $ev->key_sym;
416 957
417 if (exists $DIR{$sym}) { 958 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
418 if ($mod & KMOD_SHIFT) {
419 $::CONN->send ("command fire_stop"); 959 $::CONN->send ("command fire_stop");
420 } else { 960 }
961 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
421 $::CONN->send ("command run_stop"); 962 $::CONN->send ("command run_stop");
963 }
964}
965
966#############################################################################
967
968package Crossfire::Client::Widget::Animator;
969
970use SDL::OpenGL;
971
972our @ISA = Crossfire::Client::Widget::Bin::;
973
974sub moveto {
975 my ($self, $x, $y) = @_;
976
977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
978 $self->{speed} = 2;
979 $self->{time} = 1;
980
981 ::animation_start $self;
982}
983
984sub 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}};
422 } 994
423 } 995 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
996 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
424} 997}
425 998
4261; 999sub _draw {
1000 my ($self) = @_;
427 1001
1002 glPushMatrix;
1003 glRotate $self->{time} * 1000, 0, 1, 0;
1004 $self->{children}[0]->draw;
1005 glPopMatrix;
1006}
1007
1008#############################################################################
1009
1010package Crossfire::Client::Widget::Toplevel;
1011
1012our @ISA = Crossfire::Client::Widget::Container::;
1013
1014sub size_request {
1015 ($::WIDTH, $::HEIGHT)
1016}
1017
1018sub 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
1027sub update {
1028 my ($self) = @_;
1029
1030 $self->size_allocate ($self->size_request);
1031 ::refresh ();
1032}
1033
1034sub add {
1035 my ($self, $widget) = @_;
1036
1037 $self->SUPER::add ($widget);
1038
1039 $widget->size_allocate ($widget->size_request);
1040}
1041
1042sub draw {
1043 my ($self) = @_;
1044
1045 $self->_draw;
1046}
1047
1048#############################################################################
1049
1050package Crossfire::Client::Widget;
1051
1052$TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
1053
10541
1055

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines