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.18 by root, Sat Apr 8 13:36:26 2006 UTC vs.
Revision 1.57 by root, Mon Apr 10 22:53:49 2006 UTC

5use Scalar::Util; 5use Scalar::Util;
6 6
7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10our $FOCUS; # the widget with current focus 10use Crossfire::Client;
11our @ACTIVE_WIDGETS; 11
12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
12 16
13# class methods for events 17# class methods for events
14sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 18sub feed_sdl_key_down_event {
15sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 19 $FOCUS->key_down ($_[0]) if $FOCUS;
16sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } 20}
17sub 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}
18 72
19sub new { 73sub new {
20 my $class = shift; 74 my $class = shift;
21 75
22 bless { @_ }, $class 76 bless { @_ }, $class
23}
24
25sub activate {
26 push @ACTIVE_WIDGETS, $_[0];
27 Scalar::Util::weaken $ACTIVE_WIDGETS[-1];
28}
29
30sub deactivate {
31 @ACTIVE_WIDGETS =
32 sort { $a->{z} <=> $b->{z} }
33 grep { $_ && $_ != $_[0] }
34 @ACTIVE_WIDGETS;
35} 77}
36 78
37sub move { 79sub move {
38 my ($self, $x, $y, $z) = @_; 80 my ($self, $x, $y, $z) = @_;
39 $self->{x} = $x; 81 $self->{x} = $x;
40 $self->{y} = $y; 82 $self->{y} = $y;
41 $self->{z} = $z if defined $z; 83 $self->{z} = $z if defined $z;
42} 84}
43 85
86sub needs_redraw {
87 0
88}
89
44sub size_request { 90sub size_request {
91 require Carp;
45 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;
46} 100}
47 101
48sub focus_in { 102sub focus_in {
49 my ($widget) = @_; 103 my ($self) = @_;
50 $FOCUS = $widget; 104
105 my $focus = $FOCUS; $FOCUS = $self;
106 $focus->update if $focus;
107 $FOCUS->update;
51} 108}
52 109
53sub focus_out { 110sub focus_out {
54 my ($widget) = @_; 111 my ($self) = @_;
55}
56 112
57sub key_down { 113 return unless $FOCUS == $self;
58 my ($widget, $sdlev) = @_;
59}
60 114
61sub key_up { 115 my $focus = $FOCUS; undef $FOCUS;
62 my ($widget, $sdlev) = @_; 116 $focus->update if $focus; #?
63} 117}
64 118
119sub mouse_motion { }
120sub button_up { }
65sub button_down { 121sub button_down { }
66 my ($widget, $sdlev) = @_; 122sub key_down { }
67} 123sub key_up { }
68 124
69sub button_up { 125sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
70 my ($widget, $sdlev) = @_; 126sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
71}
72
73sub w { $_[0]->{w} }
74sub h { $_[0]->{h} }
75sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 127sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
76sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 128sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
77sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 129sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
78 130
79sub draw { 131sub draw {
80 my ($self) = @_; 132 my ($self) = @_;
81 133
82 glPushMatrix; 134 glPushMatrix;
83 glTranslate $self->{x}, $self->{y}, 0; 135 glTranslate $self->{x}, $self->{y}, 0;
84 $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 }
85 glPopMatrix; 148 glPopMatrix;
86} 149}
87 150
88sub _draw { 151sub _draw {
89 my ($widget) = @_; 152 my ($self) = @_;
153
154 warn "no draw defined for $self\n";
90} 155}
91 156
92sub bbox { 157sub bbox {
93 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 )
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};
94} 196}
95 197
96sub DESTROY { 198sub DESTROY {
97 my ($self) = @_; 199 my ($self) = @_;
98 200
99 $self->deactivate; 201 #$self->deactivate;
100} 202}
203
204#############################################################################
101 205
102package Crossfire::Client::Widget::Container; 206package Crossfire::Client::Widget::Container;
103 207
104our @ISA = Crossfire::Client::Widget::; 208our @ISA = Crossfire::Client::Widget::;
105 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
106use SDL::OpenGL; 289use SDL::OpenGL;
107 290
108sub add { $_[0]->{child} = $_[1] } 291sub new {
109sub get { $_[0]->{child} } 292 my ($class, $x, $y, $z, $w, $h) = @_;
110 293
111sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } 294 my $self = $class->SUPER::new;
112 295
113sub _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#############################################################################
114 345
115package Crossfire::Client::Widget::Frame; 346package Crossfire::Client::Widget::Frame;
116 347
117our @ISA = Crossfire::Client::Widget::Container::; 348our @ISA = Crossfire::Client::Widget::Bin::;
118 349
119use SDL::OpenGL; 350use SDL::OpenGL;
120 351
121sub size_request { 352sub size_request {
122 my ($self) = @_; 353 my ($self) = @_;
123 my $chld = $self->get 354 my $chld = $self->child
124 or return (0, 0); 355 or return (0, 0);
356
357 $chld->move (2, 2);
358
125 map { $_ + 4 } $chld->size_request; 359 map { $_ + 4 } $chld->size_request;
126} 360}
127 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
128sub _draw { 372sub _draw {
129 my ($self) = @_; 373 my ($self) = @_;
130 374
131 my $chld = $self->get; 375 my $chld = $self->child;
132 376
133 my ($w, $h) = $chld->size_request; 377 my ($w, $h) = $chld->size_request;
134 378
135 glColor 1, 0, 0;
136 glBegin GL_QUADS; 379 glBegin GL_QUADS;
137 glTexCoord 0, 0; glVertex 0 , 0; 380 glColor 0, 0, 0;
381 glVertex 0 , 0;
138 glTexCoord 0, 1; glVertex 0 , $h + 4; 382 glVertex 0 , $h + 4;
139 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 383 glVertex $w + 4 , $h + 4;
140 glTexCoord 1, 0; glVertex $w + 4 , 0; 384 glVertex $w + 4 , 0;
141 glEnd; 385 glEnd;
142 386
143 glPushMatrix;
144 glTranslate (2, 2, 0);
145 $chld->_draw; 387 $chld->draw;
146 glPopMatrix;
147} 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#############################################################################
148 474
149package Crossfire::Client::Widget::Table; 475package Crossfire::Client::Widget::Table;
150 476
151our @ISA = Crossfire::Client::Widget::Container::; 477our @ISA = Crossfire::Client::Widget::Bin::;
152 478
153use SDL::OpenGL; 479use SDL::OpenGL;
154 480
155sub add { 481sub add {
156 my ($self, $x, $y, $chld) = @_; 482 my ($self, $x, $y, $chld) = @_;
483 my $old_chld = $self->{children}[$y][$x];
484
157 $self->{childs}[$y][$x] = $chld; 485 $self->{children}[$y][$x] = $chld;
486 $chld->set_parent ($self);
487 $self->update;
158} 488}
159 489
160sub max_row_height { 490sub max_row_height {
161 my ($self, $row) = @_; 491 my ($self, $row) = @_;
162 492
163 my $hs = 0; 493 my $hs = 0;
164 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { 494 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) {
165 my $c = $self->{childs}->[$row]->[$xi]; 495 my $c = $self->{children}->[$row]->[$xi];
166 if ($c) { 496 if ($c) {
167 my ($w, $h) = $c->size_request; 497 my ($w, $h) = $c->size_request;
168 if ($hs < $h) { $hs = $h } 498 if ($hs < $h) { $hs = $h }
169 } 499 }
170 } 500 }
173 503
174sub max_col_width { 504sub max_col_width {
175 my ($self, $col) = @_; 505 my ($self, $col) = @_;
176 506
177 my $ws = 0; 507 my $ws = 0;
178 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { 508 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) {
179 my $c = ($self->{childs}->[$yi] || [])->[$col]; 509 my $c = ($self->{children}->[$yi] || [])->[$col];
180 if ($c) { 510 if ($c) {
181 my ($w, $h) = $c->size_request; 511 my ($w, $h) = $c->size_request;
182 if ($ws < $w) { $ws = $w } 512 if ($ws < $w) { $ws = $w }
183 } 513 }
184 } 514 }
188sub size_request { 518sub size_request {
189 my ($self) = @_; 519 my ($self) = @_;
190 520
191 my ($hs, $ws) = (0, 0); 521 my ($hs, $ws) = (0, 0);
192 522
193 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 523 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
194 $hs += $self->max_row_height ($yi); 524 $hs += $self->max_row_height ($yi);
195 } 525 }
196 526
197 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 527 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
198 my $wm = 0; 528 my $wm = 0;
199 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 529 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
200 $wm += $self->max_col_width ($xi) 530 $wm += $self->max_col_width ($xi)
201 } 531 }
202 if ($ws < $wm) { $ws = $wm } 532 if ($ws < $wm) { $ws = $wm }
203 } 533 }
204 534
207 537
208sub _draw { 538sub _draw {
209 my ($self) = @_; 539 my ($self) = @_;
210 540
211 my $y = 0; 541 my $y = 0;
212 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 542 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
213 my $x = 0; 543 my $x = 0;
214 544
215 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 545 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
216 546
217 glPushMatrix;
218 glTranslate ($x, $y, 0);
219 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
220 $c->_draw if $c; 550 $c->draw if $c;
221 glPopMatrix; 551 }
222 552
223 $x += $self->max_col_width ($xi); 553 $x += $self->max_col_width ($xi);
224 } 554 }
225 555
226 $y += $self->max_row_height ($yi); 556 $y += $self->max_row_height ($yi);
227 } 557 }
228} 558}
229 559
560#############################################################################
561
230package Crossfire::Client::Widget::VBox; 562package Crossfire::Client::Widget::VBox;
231 563
232our @ISA = Crossfire::Client::Widget::Container::; 564our @ISA = Crossfire::Client::Widget::Container::;
233 565
234use SDL::OpenGL; 566use SDL::OpenGL;
235 567
236sub add {
237 my ($self, $chld) = @_;
238 push @{$self->{childs}}, $chld;
239}
240
241sub size_request { 568sub size_request {
242 my ($self) = @_; 569 my ($self) = @_;
243 570
244 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
245 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) {
246 my ($w, $h) = $_->size_request; 600 my ($w, $h) = $_->size_request;
247 $hs += $h; 601 $oh += $h;
248 if ($ws < $w) { $ws = $w } 602 if ($ow < $w) { $ow = $w }
249 } 603 }
250 604
251 return ($ws, $hs); 605 my $y = 0;
252}
253
254sub _draw {
255 my ($self) = @_;
256
257 my ($x, $y);
258 for (@{$self->{childs} || []}) { 606 for (@{$self->{children}}) {
259 glPushMatrix; 607 $_->move (0, $y);
260 glTranslate (0, $y, 0); 608
261 $_->_draw; 609 if ($_ == $exp) {
262 glPopMatrix; 610 $_->size_allocate ($w, $h - $oh);
611 $y += $h - $oh;
612 } else {
263 my ($w, $h) = $_->size_request; 613 my ($cw, $h) = $_->size_request;
614 $_->size_allocate ($w, $h);
264 $y += $h; 615 $y += $h;
616 }
265 } 617 }
266} 618}
619
620#############################################################################
267 621
268package Crossfire::Client::Widget::Label; 622package Crossfire::Client::Widget::Label;
269 623
270our @ISA = Crossfire::Client::Widget::; 624our @ISA = Crossfire::Client::Widget::;
271 625
272use SDL::OpenGL; 626use SDL::OpenGL;
273 627
274sub new { 628sub new {
275 my ($class, $x, $y, $z, $ttf, $text) = @_; 629 my ($class, $x, $y, $z, $height, $text) = @_;
276 630
631 $height ||= $::FONTSIZE;
632
633 # TODO: color, and make height, xyz etc. optional
277 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);
278 635
279 $self->set_text ($text); 636 $self->set_text ($text);
280 637
281 $self 638 $self
282} 639}
283 640
284sub set_text { 641sub set_text {
285 my ($self, $text) = @_; 642 my ($self, $text) = @_;
643
644 $self->{text} = $text;
286 $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;
287} 648}
288 649
289sub get_text { 650sub get_text {
290 my ($self, $text) = @_; 651 my ($self, $text) = @_;
652
291 $self->{text} 653 $self->{text}
292} 654}
293 655
294sub size_request { 656sub size_request {
295 my ($self) = @_; 657 my ($self) = @_;
296 658
297 ( 659 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
660 (
298 $self->{texture}{width}, 661 $self->{texture}{width},
299 $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)
300 ) 668 }
669
301} 670}
302 671
303sub _draw { 672sub _draw {
304 my ($self) = @_; 673 my ($self) = @_;
305 674
306 my $tex = $self->{texture}; 675 my $tex = $self->{texture};
307 676
308 glEnable GL_BLEND; 677 glEnable GL_BLEND;
309 glEnable GL_TEXTURE_2D; 678 glEnable GL_TEXTURE_2D;
679 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
310 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 680 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
311 glBindTexture GL_TEXTURE_2D, $tex->{name};
312 681
313 glColor 1, 1, 1; 682 glColor 1, 0, 0, 1; # TODO color
314 683
315 glBegin GL_QUADS; 684 $tex->draw_quad (0, 0);
316 glTexCoord 0, 0; glVertex 0 , 0;
317 glTexCoord 0, 1; glVertex 0 , $tex->{height};
318 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
319 glTexCoord 1, 0; glVertex $tex->{width}, 0;
320 glEnd;
321 685
322 glDisable GL_BLEND; 686 glDisable GL_BLEND;
323 glDisable GL_TEXTURE_2D; 687 glDisable GL_TEXTURE_2D;
324} 688}
325 689
690#############################################################################
691
326package 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;
327 747
328use strict; 748use strict;
329 749
330our @ISA = qw/Crossfire::Client::Widget/; 750use List::Util qw(min max);
331
332use SDL::OpenGL;
333use SDL::OpenGL::Constants;
334
335sub add_line {
336 my ($self, $line) = @_;
337 push @{$self->{lines}}, $line;
338}
339
340sub _draw {
341 my ($self) = @_;
342
343}
344
345package Crossfire::Client::Widget::MapWidget;
346
347use strict;
348
349our @ISA = qw/Crossfire::Client::Widget/;
350 751
351use SDL; 752use SDL;
352use SDL::OpenGL; 753use SDL::OpenGL;
353use SDL::OpenGL::Constants; 754use SDL::OpenGL::Constants;
354 755
756our @ISA = Crossfire::Client::Widget::;
757
355sub key_down { 758sub key_down {
356 print "MAPKEYDOWN\n"; 759 print "MAPKEYDOWN\n";
357} 760}
358 761
359sub key_up { 762sub key_up {
360} 763}
361 764
765sub size_request {
766 (
767 1 + int $::WIDTH / 32,
768 1 + int $::HEIGHT / 32,
769 )
770}
771
362sub _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
363 glEnable GL_TEXTURE_2D; 797 glEnable GL_TEXTURE_2D;
364 glEnable GL_BLEND; 798 glEnable GL_BLEND;
365 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 799 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
366 800
367 my $map = $::CONN->{map}; 801 my $sw4 = ($sw + 3) & ~3;
802 my $darkness = "\x00" x ($sw4 * $sh);
368 803
369 for my $x (0 .. $::CONN->{mapw} - 1) { 804 for my $x (0 .. $sw - 1) {
805 my $row = $map->[$x + $xofs];
370 for my $y (0 .. $::CONN->{maph} - 1) { 806 for my $y (0 .. $sh - 1) {
371 807
372 my $cell = $map->[$x][$y] 808 my $cell = $row->[$y + $yofs]
373 or next; 809 or next;
374 810
375 my $darkness = $cell->[3] * (1 / 255); 811 my $dark = $cell->[0];
376 glColor $darkness, $darkness, $darkness; 812 if ($dark < 0) {
813 substr $darkness, $y * $sw4 + $x, 1, chr 224;
814 } else {
815 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
816 }
377 817
378 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 818 for my $num (grep $_, @$cell[1,2,3]) {
379 my $tex = $::CONN->{face}[$num]{texture} || next; 819 my $tex = $::CONN->{face}[$num]{texture} || next;
380
381 glBindTexture GL_TEXTURE_2D, $tex->{name};
382
383 glBegin GL_QUADS;
384 glTexCoord 0, 0; glVertex $x * 32 , $y * 32;
385 glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32;
386 glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32;
387 glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32;
388 glEnd; 820
821 my $w = $tex->{width};
822 my $h = $tex->{height};
823
824 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
389 } 825 }
390 } 826 }
391 } 827 }
828
829# if (1) { # higher quality darkness
830# $lighting =~ s/(.)/$1$1$1/gs;
831# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
832#
833# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
834#
835# $lighting = $pb->get_pixels;
836# $lighting =~ s/(.)../$1/gs;
837# }
838
839 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
840 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
841
842 $darkness = new Crossfire::Client::Texture
843 width => $sw4,
844 height => $sh,
845 data => $darkness,
846 internalformat => GL_ALPHA,
847 format => GL_ALPHA;
848
849 glColor 0.45, 0.45, 0.45, 1;
850 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
392 851
393 glDisable GL_TEXTURE_2D; 852 glDisable GL_TEXTURE_2D;
394 glDisable GL_BLEND; 853 glDisable GL_BLEND;
395} 854}
396 855
443 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 902 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
444 $::CONN->send ("command run_stop"); 903 $::CONN->send ("command run_stop");
445 } 904 }
446} 905}
447 906
4481; 907#############################################################################
449 908
909package Crossfire::Client::Widget::Animator;
910
911use SDL::OpenGL;
912
913our @ISA = Crossfire::Client::Widget::Bin::;
914
915sub moveto {
916 my ($self, $x, $y) = @_;
917
918 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
919 $self->{speed} = 0.001;
920 $self->{time} = 1;
921
922 ::animation_start $self;
923}
924
925sub animate {
926 my ($self, $interval) = @_;
927
928 printf "%5.2f\n", 1 / $interval if $interval;#d#
929
930 $self->{time} -= $interval * $self->{speed};
931 if ($self->{time} <= 0) {
932 $self->{time} = 0;
933 ::animation_stop $self;
934 }
935
936 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
937
938 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
939 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
940}
941
942sub _draw {
943 my ($self) = @_;
944
945 glPushMatrix;
946 glRotate $self->{time} * 1000, 0, 1, 0;
947 $self->{children}[0]->draw;
948 glPopMatrix;
949}
950
951#############################################################################
952
953package Crossfire::Client::Widget::Toplevel;
954
955our @ISA = Crossfire::Client::Widget::Container::;
956
957sub size_request {
958 ($::WIDTH, $::HEIGHT)
959}
960
961sub size_allocate {
962 my ($self, $w, $h) = @_;
963
964 $self->SUPER::size_allocate ($w, $h);
965
966 $_->size_allocate ($_->size_request)
967 for @{$self->{children}};
968}
969
970sub update {
971 my ($self) = @_;
972
973 $self->size_allocate ($self->size_request);
974 ::refresh ();
975}
976
977sub add {
978 my ($self, $widget) = @_;
979
980 $self->SUPER::add ($widget);
981
982 $widget->size_allocate ($widget->size_request);
983}
984
985sub draw {
986 my ($self) = @_;
987
988 $self->_draw;
989}
990
991#############################################################################
992
993package Crossfire::Client::Widget;
994
995$TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
996
9971
998

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines