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.73 by root, Tue Apr 11 18:06:53 2006 UTC vs.
Revision 1.91 by root, Wed Apr 12 23:15:39 2006 UTC

1package CFClient::UI; 1package CFClient::UI;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util ();
6use List::Util ();
6 7
7use CFClient; 8use CFClient;
8 9
9our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
10 11
42 43
43 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
44 45
45 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
46 47
47 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB;
48 49
49 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
50 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
51 $grab->update if $grab; 52 $grab->update if $grab;
52 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
78use SDL::OpenGL; 79use SDL::OpenGL;
79 80
80sub new { 81sub new {
81 my $class = shift; 82 my $class = shift;
82 83
83 bless { 84 my $self = bless {
84 x => 0, 85 x => 0,
85 y => 0, 86 y => 0,
86 z => 0, 87 z => 0,
88 w => -1,
89 h => -1,
87 @_ 90 @_
88 }, $class 91 }, $class;
92
93 for (keys %$self) {
94 if (/^connect_(.*)$/) {
95 $self->connect ($1 => delete $self->{$_});
96 }
97 }
98
99 $self
89} 100}
90 101
91sub move { 102sub move {
92 my ($self, $x, $y, $z) = @_; 103 my ($self, $x, $y, $z) = @_;
93 $self->{x} = $x; 104 $self->{x} = $x;
102sub size_request { 113sub size_request {
103 require Carp; 114 require Carp;
104 Carp::confess "size_request is abtract"; 115 Carp::confess "size_request is abtract";
105} 116}
106 117
107sub size_allocate { 118sub _size_allocate {
108 my ($self, $w, $h) = @_; 119 my ($self, $x, $y, $w, $h) = @_;
120
121 $self->{x} = $x;
122 $self->{y} = $y;
123
124 return unless $self->{w} != $w || $self->{h} != $h;
109 125
110 $self->{w} = $w; 126 $self->{w} = $w;
111 $self->{h} = $h; 127 $self->{h} = $h;
112}
113 128
129 1
130}
131
132sub size_allocate {
133 my ($self, $x, $y, $w, $h) = @_;
134
135 $self->_size_allocate ($x, $y, $w, $h);
136}
137
114# translate global koordinates to local coordinate system 138# translate global coordinates to local coordinate system
115sub translate { 139sub translate {
116 my ($self, $x, $y) = @_; 140 my ($self, $x, $y) = @_;
117 141
118 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 142 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
119} 143}
165 glPopMatrix; 189 glPopMatrix;
166 190
167 if ($self == $HOVER) { 191 if ($self == $HOVER) {
168 my ($x, $y) = @$self{qw(x y)}; 192 my ($x, $y) = @$self{qw(x y)};
169 193
170 glColor 1, 1, 1, 0.1; 194 glColor 0, 0, 1, 0.2;
171 glEnable GL_BLEND; 195 glEnable GL_BLEND;
196 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
172 glBegin GL_QUADS; 197 glBegin GL_QUADS;
173 glVertex $x , $y; 198 glVertex $x , $y;
174 glVertex $x + $self->{w}, $y; 199 glVertex $x + $self->{w}, $y;
175 glVertex $x + $self->{w}, $y + $self->{h}; 200 glVertex $x + $self->{w}, $y + $self->{h};
176 glVertex $x , $y + $self->{h}; 201 glVertex $x , $y + $self->{h};
183 my ($self) = @_; 208 my ($self) = @_;
184 209
185 warn "no draw defined for $self\n"; 210 warn "no draw defined for $self\n";
186} 211}
187 212
188sub bbox {
189 my ($self) = @_;
190 my ($w, $h) = $self->size_request;
191 (
192 $self->{x},
193 $self->{y},
194 $self->{x} = $w,
195 $self->{y} = $h
196 )
197}
198
199sub find_widget { 213sub find_widget {
200 my ($self, $x, $y) = @_; 214 my ($self, $x, $y) = @_;
201 215
202 return $self 216 return $self
203 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 217 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
204 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 218 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
205 219
206 () 220 ()
207} 221}
208 222
209sub del_parent { $_[0]->{parent} = undef }
210
211sub set_parent { 223sub set_parent {
212 my ($self, $par) = @_; 224 my ($self, $par) = @_;
213 225
214 $self->{parent} = $par; 226 $self->{parent} = $par;
215 Scalar::Util::weaken $self->{parent}; 227 Scalar::Util::weaken $self->{parent};
258 my $class = shift; 270 my $class = shift;
259 271
260 # range [value, low, high, page] 272 # range [value, low, high, page]
261 273
262 $class->SUPER::new ( 274 $class->SUPER::new (
263 bg => [0, 0, 0, 0.4], 275 bg => [0, 0, 0, 0.2],
264 active_bg => [1, 1, 1], 276 active_bg => [1, 1, 1, 0.5],
265 @_ 277 @_
266 ) 278 )
267} 279}
268 280
269sub _draw { 281sub _draw {
270 my ($self) = @_; 282 my ($self) = @_;
271 283
272 my ($w, $h) = @$self{qw(w h)}; 284 my ($w, $h) = @$self{qw(w h)};
273 285
286 glEnable GL_BLEND;
287 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
274 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 288 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
289
275 glBegin GL_QUADS; 290 glBegin GL_QUADS;
276 glVertex 0 , 0; 291 glVertex 0 , 0;
277 glVertex 0 , $h; 292 glVertex 0 , $h;
278 glVertex $w, $h; 293 glVertex $w, $h;
279 glVertex $w, 0; 294 glVertex $w, 0;
280 glEnd; 295 glEnd;
296
297 glDisable GL_BLEND;
281} 298}
282 299
283############################################################################# 300#############################################################################
284 301
285package CFClient::UI::Empty; 302package CFClient::UI::Empty;
308 325
309 $self 326 $self
310} 327}
311 328
312sub add { 329sub add {
313 my ($self, $chld, $expand) = @_; 330 my ($self, $chld) = @_;
314 331
315 $chld->{expand} = $expand;
316 $chld->set_parent ($self); 332 $chld->set_parent ($self);
317 333
318 $self->{children} = [ 334 $self->{children} = [
319 sort { $a->{z} <=> $b->{z} } 335 sort { $a->{z} <=> $b->{z} }
320 @{$self->{children}}, $chld 336 @{$self->{children}}, $chld
321 ]; 337 ];
322 338
323 $self->size_allocate ($self->{w}, $self->{h}) 339 $self->{w} = $self->{h} = -1;
324 if $self->{w}; #TODO: check for "realised state" 340 $self->update;
325} 341}
326 342
327sub remove { 343sub remove {
328 my ($self, $widget) = @_; 344 my ($self, $widget) = @_;
329 345
330 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 346 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
331 347
332 $self->size_allocate ($self->{w}, $self->{h}); 348 $self->size_allocate (0, 0, $self->{w}, $self->{h});
333} 349}
334 350
335sub find_widget { 351sub find_widget {
336 my ($self, $x, $y) = @_; 352 my ($self, $x, $y) = @_;
337 353
390sub size_request { 406sub size_request {
391 $_[0]{children}[0]->size_request 407 $_[0]{children}[0]->size_request
392} 408}
393 409
394sub size_allocate { 410sub size_allocate {
395 my ($self, $w, $h) = @_; 411 my ($self, $x, $y, $w, $h) = @_;
396 412
397 return unless $self->{w} != $w || $self->{h} != $h; 413 $self->_size_allocate ($x, $y, $w, $h) or return;
398 414
399 $self->SUPER::size_allocate ($w, $h);
400 $self->{children}[0]->size_allocate ($w, $h); 415 $self->{children}[0]->size_allocate (0, 0, $w, $h);
401} 416}
402 417
403############################################################################# 418#############################################################################
404 419
405package CFClient::UI::Window; 420package CFClient::UI::Window;
430 $self->{w}, $self->{h}, sub { $self->child->draw } 445 $self->{w}, $self->{h}, sub { $self->child->draw }
431 ); 446 );
432} 447}
433 448
434sub size_allocate { 449sub size_allocate {
435 my ($self, $w, $h) = @_; 450 my ($self, $x, $y, $w, $h) = @_;
436 451
437 return unless $self->{w} != $w || $self->{h} != $h; 452 $self->_size_allocate ($x, $y, $w, $h) or return;
438 453
439 $self->{w} = $w;
440 $self->{h} = $h;
441
442 $self->child->size_allocate ($w, $h); 454 $self->child->size_allocate (0, 0, $w, $h);
443 455
444 $self->render_chld; 456 $self->render_chld;
445} 457}
446 458
447sub _draw { 459sub _draw {
451 463
452 my $tex = $self->{texture} 464 my $tex = $self->{texture}
453 or return; 465 or return;
454 466
455 glEnable GL_BLEND; 467 glEnable GL_BLEND;
468 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
456 glEnable GL_TEXTURE_2D; 469 glEnable GL_TEXTURE_2D;
457 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 470 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
458 471
459 $tex->draw_quad (0, 0, $w, $h); 472 $tex->draw_quad (0, 0, $w, $h);
460 473
479 492
480 map { $_ + 4 } $chld->size_request; 493 map { $_ + 4 } $chld->size_request;
481} 494}
482 495
483sub size_allocate { 496sub size_allocate {
484 my ($self, $w, $h) = @_; 497 my ($self, $x, $y, $w, $h) = @_;
485 498
486 return unless $self->{w} != $w || $self->{h} != $h; 499 $self->_size_allocate ($x, $y, $w, $h) or return;
487 500
488 $self->{w} = $w;
489 $self->{h} = $h;
490
491 $self->child->size_allocate ($w - 4, $h - 4); 501 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
492 $self->child->move (2, 2);
493} 502}
494 503
495sub _draw { 504sub _draw {
496 my ($self) = @_; 505 my ($self) = @_;
497 506
523 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 532 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
524 533
525sub size_request { 534sub size_request {
526 my ($self) = @_; 535 my ($self) = @_;
527 536
537 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
538
528 my ($w, $h) = $self->SUPER::size_request; 539 my ($w, $h) = $self->SUPER::size_request;
529 540
530 $h += $tex[1]->{h}; 541 $h += $tex[1]->{h};
531 $h += $tex[4]->{h}; 542 $h += $tex[4]->{h};
532 $w += $tex[2]->{w}; 543 $w += $tex[2]->{w};
534 545
535 ($w, $h) 546 ($w, $h)
536} 547}
537 548
538sub size_allocate { 549sub size_allocate {
539 my ($self, $w, $h) = @_; 550 my ($self, $x, $y, $w, $h) = @_;
540 551
541 return unless $self->{w} != $w || $self->{h} != $h; 552 $self->_size_allocate ($x, $y, $w, $h) or return;
542 553
543 $self->SUPER::size_allocate ($w, $h);
544
545 $h -= $tex[1]->{h}; 554 $h -= $tex[1]{h};
546 $h -= $tex[4]->{h}; 555 $h -= $tex[4]{h};
547 $w -= $tex[2]->{w}; 556 $w -= $tex[2]{w};
548 $w -= $tex[3]->{w}; 557 $w -= $tex[3]{w};
549 558
550 $h = $h < 0 ? 0 : $h; 559 $h = $h < 0 ? 0 : $h;
551 $w = $w < 0 ? 0 : $w; 560 $w = $w < 0 ? 0 : $w;
552 561
553 my $child = $self->child; 562 my $child = $self->child;
554 563
555 $child->size_allocate ($w, $h);
556 $child->move ($tex[3]->{w}, $tex[1]->{h}); 564 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
565}
566
567sub button_down {
568 my ($self, $ev, $x, $y) = @_;
569
570 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w}
571 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) {
572
573 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
574 my ($bw, $bh) = ($self->{w}, $self->{h});
575
576 $self->{motion} = sub {
577 my ($ev, $x, $y) = @_;
578
579 ($x, $y) = ($ev->motion_x, $ev->motion_y);
580
581 $self->{user_w} = $bw + $x - $ox;
582 $self->{user_h} = $bh + $y - $oy;
583 $self->update;
584 };
585
586 } elsif ($x >= 0 && $x < $self->{w}
587 && $y >= 0 && $y < $tex[1]{h}) {
588
589 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
590 my ($bx, $by) = ($self->{x}, $self->{y});
591
592 $self->{motion} = sub {
593 my ($ev, $x, $y) = @_;
594
595 ($x, $y) = ($ev->motion_x, $ev->motion_y);
596
597 $self->move ($bx + $x - $ox, $by + $y - $oy);
598 $self->update;
599 };
600 }
601}
602
603sub button_up {
604 my ($self, $ev, $x, $y) = @_;
605
606 delete $self->{motion};
607}
608
609sub mouse_motion {
610 my ($self, $ev, $x, $y) = @_;
611
612 $self->{motion}->($ev, $x, $y) if $self->{motion};
557} 613}
558 614
559sub _draw { 615sub _draw {
560 my ($self) = @_; 616 my ($self) = @_;
561 617
562 my ($w, $h) = ($self->{w}, $self->{h}); 618 my ($w, $h) = ($self->{w}, $self->{h});
563 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 619 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
564 620
565 glEnable GL_BLEND; 621 glEnable GL_BLEND;
622 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
566 glEnable GL_TEXTURE_2D; 623 glEnable GL_TEXTURE_2D;
567 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
568 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 624 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
569 625
570 my $top = $tex[1]; 626 my $top = $tex[1];
571 $top->draw_quad (0, 0, $w, $top->{h}); 627 $top->draw_quad (0, 0, $w, $top->{h});
572 628
578 634
579 my $bottom = $tex[4]; 635 my $bottom = $tex[4];
580 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); 636 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
581 637
582 my $bg = $tex[0]; 638 my $bg = $tex[0];
639
583 glBindTexture GL_TEXTURE_2D, $bg->{name}; 640 glBindTexture GL_TEXTURE_2D, $bg->{name};
584 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
585 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
586 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
587 642
588 my $rep_x = $cw / $bg->{w}; 643 my $rep_x = $cw / $bg->{w};
589 my $rep_y = $ch / $bg->{h}; 644 my $rep_y = $ch / $bg->{h};
590 645
591 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); 646 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
592 647
648 glDisable GL_TEXTURE_2D;
593 glDisable GL_BLEND; 649 glDisable GL_BLEND;
594 glDisable GL_TEXTURE_2D;
595 650
596 $self->child->draw; 651 $self->child->draw;
597 652
598} 653}
599 654
601 656
602package CFClient::UI::Table; 657package CFClient::UI::Table;
603 658
604our @ISA = CFClient::UI::Base::; 659our @ISA = CFClient::UI::Base::;
605 660
661use List::Util qw(max sum);
662
606use SDL::OpenGL; 663use SDL::OpenGL;
664
665sub new {
666 my $class = shift;
667
668 $class->SUPER::new (
669 col_expand => [],
670 @_
671 )
672}
607 673
608sub add { 674sub add {
609 my ($self, $x, $y, $chld) = @_; 675 my ($self, $x, $y, $chld) = @_;
610 my $old_chld = $self->{children}[$y][$x];
611 676
612 $self->{children}[$y][$x] = $chld; 677 $self->{children}[$y][$x] = $chld;
613 $chld->set_parent ($self); 678 $chld->set_parent ($self);
679
680 $self->{w} = $self->{h} = -1;
614 $self->update; 681 $self->update;
615} 682}
616 683
617sub max_row_height { 684sub get_wh {
618 my ($self, $row) = @_; 685 my ($self) = @_;
619 686
620 my $hs = 0; 687 my (@w, @h);
621 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 688
689 for my $y (0 .. $#{$self->{children}}) {
622 my $c = $self->{children}->[$row]->[$xi]; 690 my $row = $self->{children}[$y]
623 if ($c) { 691 or next;
692
693 for my $x (0 .. $#$row) {
694 my $widget = $row->[$x]
695 or next;
624 my ($w, $h) = $c->size_request; 696 my ($w, $h) = $widget->size_request;
625 if ($hs < $h) { $hs = $h } 697
698 $w[$x] = max $w[$x], $w;
699 $h[$y] = max $h[$y], $h;
626 } 700 }
627 } 701 }
628 return $hs;
629}
630 702
631sub max_col_width { 703 (\@w, \@h)
704}
705
706sub size_request {
632 my ($self, $col) = @_; 707 my ($self) = @_;
633 708
709 my ($ws, $hs) = $self->get_wh;
710
711 (
712 (sum @$ws),
713 (sum @$hs),
714 )
715}
716
717sub size_allocate {
718 my ($self, $x, $y, $w, $h) = @_;
719
720 $self->_size_allocate ($x, $y, $w, $h) or return;
721
722 my ($ws, $hs) = $self->get_wh;
723
724 my $req_w = sum @$ws;
725 my $req_h = sum @$hs;
726
727 # TODO: nicer code && do row_expand
728 my @col_expand = @{$self->{col_expand}};
729 @col_expand = (1) x @$ws unless @col_expand;
730 my $col_expand = (sum @col_expand) || 1;
731
732 # linearly scale sizes
733 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
734 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
735
736 my $y;
737
738 for my $r (0 .. $#{$self->{children}}) {
739 my $row = $self->{children}[$r]
740 or next;
741
634 my $ws = 0; 742 my $x = 0;
635 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 743 my $row_h = $hs->[$r];
636 my $c = ($self->{children}->[$yi] || [])->[$col]; 744
637 if ($c) { 745 for my $c (0 .. $#$row) {
638 my ($w, $h) = $c->size_request; 746 my $col_w = $ws->[$c];
639 if ($ws < $w) { $ws = $w } 747
748 if (my $widget = $row->[$c]) {
749 $widget->size_allocate ($x, $y, $col_w, $row_h);
750 }
751
752 $x += $col_w;
640 } 753 }
754
755 $y += $row_h;
641 } 756 }
642 return $ws; 757
643} 758}
759
760sub find_widget {
761 my ($self, $x, $y) = @_;
762
763 $x -= $self->{x};
764 $y -= $self->{y};
765
766 my $res;
767
768 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
769 $res = $_->find_widget ($x, $y)
770 and return $res;
771 }
772
773 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
774}
775
776sub _draw {
777 my ($self) = @_;
778
779 for (grep $_, @{$self->{children}}) {
780 $_->draw for grep $_, @$_;
781 }
782}
783
784#############################################################################
785
786package CFClient::UI::HBox;
787
788# TODO: wrap into common Box base class
789
790our @ISA = CFClient::UI::Container::;
644 791
645sub size_request { 792sub size_request {
646 my ($self) = @_; 793 my ($self) = @_;
647 794
795 my @alloc = map [$_->size_request], @{$self->{children}};
796
797 (
798 (List::Util::sum map $_->[0], @alloc),
799 (List::Util::max map $_->[1], @alloc),
800 )
801}
802
803sub size_allocate {
804 my ($self, $x, $y, $w, $h) = @_;
805
806 $self->_size_allocate ($x, $y, $w, $h);
807
648 my ($hs, $ws) = (0, 0); 808 ($h, $w) = ($w, $h);
649 809
650 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 810 my $children = $self->{children};
651 $hs += $self->max_row_height ($yi);
652 }
653 811
654 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 812 my @h = map +($_->size_request)[0], @$children;
655 my $wm = 0; 813
656 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 814 my $req_h = List::Util::sum @h;
657 $wm += $self->max_col_width ($xi) 815
816 if ($req_h > $h) {
817 # ah well, not enough space
818 $_ *= $h / $req_h for @h;
819 } else {
820 my $exp = List::Util::sum map $_->{expand}, @$children;
821 $exp ||= 1;
822
823 for (0 .. $#$children) {
824 my $child = $children->[$_];
825
826 my $alloc_h = $h[$_];
827 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
828 $h[$_] = $alloc_h;
658 } 829 }
659 if ($ws < $wm) { $ws = $wm }
660 } 830 }
661
662 return ($ws, $hs);
663}
664
665sub _draw {
666 my ($self) = @_;
667 831
668 my $y = 0; 832 my $y = 0;
669 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 833 for (0 .. $#$children) {
834 my $child = $children->[$_];
670 my $x = 0; 835 my $h = $h[$_];
836 $child->size_allocate ($y, 0, $h, $w);
671 837
672 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 838 $y += $h;
673
674 my $c = $self->{children}->[$yi]->[$xi];
675 if ($c) {
676 $c->move ($x, $y, 0); #TODO: Move to size_request
677 $c->draw if $c;
678 }
679
680 $x += $self->max_col_width ($xi);
681 }
682
683 $y += $self->max_row_height ($yi);
684 } 839 }
685} 840}
686 841
687############################################################################# 842#############################################################################
688 843
689package CFClient::UI::VBox; 844package CFClient::UI::VBox;
690 845
846# TODO: wrap into common Box base class
847
691our @ISA = CFClient::UI::Container::; 848our @ISA = CFClient::UI::Container::;
692
693use SDL::OpenGL;
694 849
695sub size_request { 850sub size_request {
696 my ($self) = @_; 851 my ($self) = @_;
697 852
698 my @alloc = map [$_->size_request], @{$self->{children}}; 853 my @alloc = map [$_->size_request], @{$self->{children}};
702 (List::Util::sum map $_->[1], @alloc), 857 (List::Util::sum map $_->[1], @alloc),
703 ) 858 )
704} 859}
705 860
706sub size_allocate { 861sub size_allocate {
707 my ($self, $w, $h) = @_; 862 my ($self, $x, $y, $w, $h) = @_;
708 863
709 return unless $self->{w} != $w || $self->{h} != $h; 864 $self->_size_allocate ($x, $y, $w, $h);
710
711 $self->{w} = $w;
712 $self->{h} = $h;
713
714 return unless $self->{h};
715 865
716 my $children = $self->{children}; 866 my $children = $self->{children};
717 867
718 my @h = map +($_->size_request)[1], @$children; 868 my @h = map +($_->size_request)[1], @$children;
719 869
720 my $req_h = List::Util::sum @h; 870 my $req_h = List::Util::sum @h;
721 871
722 if ($req_h > $h) { 872 if ($req_h > $h) {
723 # ah well, not enough space 873 # ah well, not enough space
724 $_ = $h[$_] * $h / $req_h for @h; 874 $_ *= $h / $req_h for @h;
725 } else { 875 } else {
726 my @exp = grep $_->{expand}, @$children; 876 my $exp = List::Util::sum map $_->{expand}, @$children;
727 @exp = @$children unless @exp; 877 $exp ||= 1;
728 878
729 my %exp = map +($_ => 1), @exp;
730
731 for (0 .. $#$children) { 879 for (0 .. $#$children) {
732 my $child = $children->[$_]; 880 my $child = $children->[$_];
733 881
734 my $alloc_h = $h[$_]; 882 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
735 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
736 $h[$_] = $alloc_h;
737 } 883 }
738 } 884 }
739 885
740 my $y = 0; 886 my $y = 0;
741 for (0 .. $#$children) { 887 for (0 .. $#$children) {
742 my $child = $children->[$_]; 888 my $child = $children->[$_];
743 my $h = $h[$_]; 889 my $h = $h[$_];
744 $child->move (0, $y);
745 $child->size_allocate ($w, $h); 890 $child->size_allocate (0, $y, $w, $h);
746 891
747 $y += $h; 892 $y += $h;
748 } 893 }
749} 894}
750 895
758 903
759sub new { 904sub new {
760 my ($class, %arg) = @_; 905 my ($class, %arg) = @_;
761 906
762 my $self = $class->SUPER::new ( 907 my $self = $class->SUPER::new (
763 fg => [1, 1, 1], 908 fg => [1, 1, 1],
764 height => $::FONTSIZE, 909 height => $::FONTSIZE,
765 text => "", 910 text => "",
766 align => -1, 911 align => -1,
912 padding => 2,
767 layout => new CFClient::Layout, 913 layout => new CFClient::Layout,
768 %arg 914 %arg
769 ); 915 );
770 916
771 $self->set_text ($self->{text}); 917 $self->set_text ($self->{text});
772 918
788 934
789 $self->{text} = $text; 935 $self->{text} = $text;
790 $self->{layout}->set_markup ($text); 936 $self->{layout}->set_markup ($text);
791 937
792 delete $self->{texture}; 938 delete $self->{texture};
939# $self->{w} = $self->{h} = -1;
793 $self->update; 940 $self->update;
794} 941}
795 942
796sub get_text { 943sub get_text {
797 my ($self, $text) = @_; 944 my ($self, $text) = @_;
802sub size_request { 949sub size_request {
803 my ($self) = @_; 950 my ($self) = @_;
804 951
805 $self->{layout}->set_width; 952 $self->{layout}->set_width;
806 $self->{layout}->set_height ($self->{height}); 953 $self->{layout}->set_height ($self->{height});
807 $self->{layout}->size 954 my ($w, $h) = $self->{layout}->size;
808# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack 955
809# ( 956 (
810# $self->{texture}{w}, 957 $w + $self->{padding} * 2,
811# $self->{texture}{h}, 958 $h + $self->{padding} * 2,
812# ) 959 )
813# } else {
814# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
815#
816# ($w, $h)
817# }
818} 960}
819 961
820sub size_allocate { 962sub size_allocate {
821 my ($self, $w, $h) = @_; 963 my ($self, $x, $y, $w, $h) = @_;
822 964
823 return unless $self->{w} != $w || $self->{h} != $h; 965 $self->_size_allocate ($x, $y, $w, $h) or return;
824 966
825 $self->SUPER::size_allocate ($w, $h);
826 delete $self->{texture}; 967 delete $self->{texture};
827} 968}
828 969
829sub update { 970sub update {
830 my ($self) = @_; 971 my ($self) = @_;
836sub _draw { 977sub _draw {
837 my ($self) = @_; 978 my ($self) = @_;
838 979
839 my $tex = $self->{texture} ||= do { 980 my $tex = $self->{texture} ||= do {
840 $self->{layout}->set_width ($self->{w}); 981 $self->{layout}->set_width ($self->{w});
982 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
841 new_from_layout CFClient::Texture $self->{layout}; 983 new_from_layout CFClient::Texture $self->{layout}
842 }; 984 };
843 985
844 glEnable GL_BLEND; 986 glEnable GL_BLEND;
987 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
845 glEnable GL_TEXTURE_2D; 988 glEnable GL_TEXTURE_2D;
846 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
847 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 989 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
848 990
849 glColor @{$self->{fg}}; 991 glColor @{$self->{fg}};
850 992
851 my $x = 993 my $x =
852 $self->{align} < 0 ? 0 994 $self->{align} < 0 ? $self->{padding}
853 : $self->{align} > 0 ? $self->{w} - $self->{texture}{w} 995 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
854 : ($self->{w} + $self->{texture}{w}) * 0.5; 996 : ($self->{w} - $tex->{w}) * 0.5;
855 997
998 glTranslate $x, ($self->{h} - $tex->{h}) * 0.5, 0;
856 $tex->draw_quad ($x, 0); 999 $tex->draw_quad (0, 0);
857 1000
1001 glDisable GL_TEXTURE_2D;
858 glDisable GL_BLEND; 1002 glDisable GL_BLEND;
859 glDisable GL_TEXTURE_2D;
860} 1003}
861 1004
862############################################################################# 1005#############################################################################
863 1006
864package CFClient::UI::Entry; 1007package CFClient::UI::Entry;
871sub new { 1014sub new {
872 my $class = shift; 1015 my $class = shift;
873 1016
874 $class->SUPER::new ( 1017 $class->SUPER::new (
875 fg => [1, 1, 1], 1018 fg => [1, 1, 1],
876 bg => [0, 0, 0, 0.4], 1019 bg => [0, 0, 0, 0.2],
877 active_bg => [1, 1, 1], 1020 active_bg => [1, 1, 1, 0.5],
878 active_fg => [0, 0, 0], 1021 active_fg => [0, 0, 0],
879 @_ 1022 @_
880 ) 1023 )
881} 1024}
882 1025
885 1028
886 $self->{last_activity} = $::NOW; 1029 $self->{last_activity} = $::NOW;
887 1030
888 $self->{text} = $text; 1031 $self->{text} = $text;
889 $self->{layout}->set_width ($self->{w}); 1032 $self->{layout}->set_width ($self->{w});
1033 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
890 1034
891 $text =~ s/./*/g if $self->{hidden}; 1035 $text =~ s/./*/g if $self->{hidden};
892 1036
893
894 $self->{layout}->set_markup ($self->escape_text ($text)); 1037 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
895 1038
896 $text = substr $text, 0, $self->{cursor}; 1039 $text = substr $text, 0, $self->{cursor};
897 utf8::encode $text; 1040 utf8::encode $text;
898 1041
899 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 1042 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
906 1049
907 ($w + 1, $h) # add 1 for cursor 1050 ($w + 1, $h) # add 1 for cursor
908} 1051}
909 1052
910sub size_allocate { 1053sub size_allocate {
911 my ($self, $w, $h) = @_; 1054 my ($self, $x, $y, $w, $h) = @_;
912 1055
913 return unless $self->{w} != $w || $self->{h} != $h;
914
915 $self->SUPER::size_allocate ($w, $h); 1056 $self->SUPER::size_allocate ($x, $y, $w, $h);
916 1057
917 $self->_set_text ($self->{text}); 1058 $self->_set_text ($self->{text});
918} 1059}
919 1060
920sub set_text { 1061sub set_text {
941 substr $text, $self->{cursor}, 1, ""; 1082 substr $text, $self->{cursor}, 1, "";
942 } elsif ($sym == SDLK_LEFT) { 1083 } elsif ($sym == SDLK_LEFT) {
943 --$self->{cursor} if $self->{cursor}; 1084 --$self->{cursor} if $self->{cursor};
944 } elsif ($sym == SDLK_RIGHT) { 1085 } elsif ($sym == SDLK_RIGHT) {
945 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1086 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1087 } elsif ($sym == SDLK_HOME) {
1088 $self->{cursor} = 0;
1089 } elsif ($sym == SDLK_END) {
1090 $self->{cursor} = length $text;
946 } elsif ($uni) { 1091 } elsif ($uni) {
947 substr $text, $self->{cursor}++, 0, chr $uni; 1092 substr $text, $self->{cursor}++, 0, chr $uni;
948 } 1093 }
949 1094
950 $self->_set_text ($text); 1095 $self->_set_text ($text);
965 $self->SUPER::button_down ($ev, $x, $y); 1110 $self->SUPER::button_down ($ev, $x, $y);
966 1111
967 my $idx = $self->{layout}->xy_to_index ($x, $y); 1112 my $idx = $self->{layout}->xy_to_index ($x, $y);
968 1113
969 # byte-index to char-index 1114 # byte-index to char-index
970 my $text = $self->{layout}; 1115 my $text = $self->{text};
971 utf8::encode $text; 1116 utf8::encode $text;
972 $self->{cursor} = length substr $text, 0, $idx; 1117 $self->{cursor} = length substr $text, 0, $idx;
973 1118
974 $self->_set_text ($self->{text}); 1119 $self->_set_text ($self->{text});
975 $self->update; 1120 $self->update;
990 $self->{fg} = $self->{active_fg}; 1135 $self->{fg} = $self->{active_fg};
991 } else { 1136 } else {
992 glColor @{$self->{bg}}; 1137 glColor @{$self->{bg}};
993 } 1138 }
994 1139
1140 glEnable GL_BLEND;
1141 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
995 glBegin GL_QUADS; 1142 glBegin GL_QUADS;
996 glVertex 0 , 0; 1143 glVertex 0 , 0;
997 glVertex 0 , $self->{h}; 1144 glVertex 0 , $self->{h};
998 glVertex $self->{w}, $self->{h}; 1145 glVertex $self->{w}, $self->{h};
999 glVertex $self->{w}, 0; 1146 glVertex $self->{w}, 0;
1000 glEnd; 1147 glEnd;
1148 glDisable GL_BLEND;
1001 1149
1002 $self->SUPER::_draw; 1150 $self->SUPER::_draw;
1003 1151
1004 #TODO: force update every cursor change :( 1152 #TODO: force update every cursor change :(
1005 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1153 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1011 } 1159 }
1012} 1160}
1013 1161
1014############################################################################# 1162#############################################################################
1015 1163
1016package CFClient::UI::Slider; 1164package CFClient::UI::Button;
1017 1165
1018use strict; 1166our @ISA = CFClient::UI::Label::;
1019 1167
1168use SDL;
1020use SDL::OpenGL; 1169use SDL::OpenGL;
1021use SDL::OpenGL::Constants;
1022 1170
1023our @ISA = CFClient::UI::DrawBG::; 1171my @tex =
1024 1172 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1025sub size_request { 1173 qw(b1_button_active.png);
1026 my ($self) = @_;
1027
1028 my $w = 50;
1029 my $h = 10;
1030
1031 $self->{vertical} ? ($h, $w) : ($w, $h)
1032}
1033 1174
1034sub new { 1175sub new {
1035 my $class = shift; 1176 my $class = shift;
1036 1177
1178 $class->SUPER::new (
1179 padding => 4,
1180 fg => [1, 1, 1],
1181 bg => [1, 1, 1, 0.2],
1182 active_fg => [1, 1, 0],
1183# active_bg => [0, 0, 0, 0.5],
1184# border_fg => [1, 1, 0],
1185 @_
1186 )
1187}
1188
1189sub button_up {
1190 my ($self, $ev, $x, $y) = @_;
1191
1192 if ($x >= 0 && $x < $self->{w}
1193 && $y >= 0 && $y < $self->{h}) {
1194 $self->emit ("activate");
1195 }
1196}
1197
1198sub _draw {
1199 my ($self) = @_;
1200
1201 local $self->{fg} = $self->{fg};
1202 my $tex = $tex[0];
1203
1204 glEnable GL_BLEND;
1205 glEnable GL_TEXTURE_2D;
1206 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1207
1208 if ($GRAB == $self) {
1209 $self->{fg} = $self->{active_fg};
1210 }
1211
1212 glBindTexture GL_TEXTURE_2D, $tex->{name};
1213 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1214
1215 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
1216
1217 glDisable GL_TEXTURE_2D;
1218 glDisable GL_BLEND;
1219
1220 $self->SUPER::_draw;
1221}
1222
1223#############################################################################
1224
1225package CFClient::UI::CheckBox;
1226
1227our @ISA = CFClient::UI::DrawBG::;
1228
1229use SDL;
1230use SDL::OpenGL;
1231
1232sub new {
1233 my $class = shift;
1234
1235 $class->SUPER::new (
1236 padding => 2,
1237 fg => [1, 1, 1],
1238 active_fg => [1, 1, 0],
1239 state => 0,
1240 @_
1241 )
1242}
1243
1244sub size_request {
1245 my ($self) = @_;
1246
1247 ($self->{padding} * 2 + 6) x 2
1248}
1249
1250sub size_allocate {
1251 my ($self, $x, $y, $w, $h) = @_;
1252
1253 $self->_size_allocate ($x, $y, $w, $h);
1254}
1255
1256sub button_down {
1257 my ($self, $ev, $x, $y) = @_;
1258
1259 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1260 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1261 $self->{state} = !$self->{state};
1262 $self->emit (changed => $self->{state});
1263 }
1264}
1265
1266sub _draw {
1267 my ($self) = @_;
1268
1269 $self->SUPER::_draw;
1270
1271 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1272
1273 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1274
1275 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1276
1277 glBegin GL_LINE_LOOP;
1278 glVertex 0 , 0;
1279 glVertex 0 , $s;
1280 glVertex $s, $s;
1281 glVertex $s, 0;
1282 glEnd;
1283
1284 if ($self->{state}) {
1285 glBegin GL_LINES;
1286 glVertex 0 , 0;
1287 glVertex $s, $s;
1288 glVertex $s, 0;
1289 glVertex 0 , $s;
1290 glEnd;
1291 }
1292}
1293
1294#############################################################################
1295
1296package CFClient::UI::Slider;
1297
1298use strict;
1299
1300use SDL::OpenGL;
1301
1302our @ISA = CFClient::UI::DrawBG::;
1303
1304sub new {
1305 my $class = shift;
1306
1037 # range [value, low, high, page] 1307 # range [value, low, high, page]
1038 1308
1039 $class->SUPER::new ( 1309 my $self = $class->SUPER::new (
1040 fg => [1, 1, 1], 1310 fg => [1, 1, 1],
1041 active_fg => [0, 0, 0], 1311 active_fg => [0, 0, 0],
1042 range => [0, 0, 100, 10], 1312 range => [0, 0, 100, 10],
1313 req_w => 40,
1314 req_h => 10,
1043 vertical => 1, 1315 vertical => 0,
1044 @_ 1316 @_
1045 ) 1317 );
1318
1319 $self
1320}
1321
1322sub size_request {
1323 my ($self) = @_;
1324
1325 my $w = $self->{req_w};
1326 my $h = $self->{req_h};
1327
1328 $self->{vertical} ? ($h, $w) : ($w, $h)
1046} 1329}
1047 1330
1048sub button_down { 1331sub button_down {
1049 my ($self, $ev, $x, $y) = @_; 1332 my ($self, $ev, $x, $y) = @_;
1050 1333
1095 $value = int +($value - $lo) * $w / ($hi - $lo); 1378 $value = int +($value - $lo) * $w / ($hi - $lo);
1096 1379
1097 $w -= $page; 1380 $w -= $page;
1098 $page &= ~1; 1381 $page &= ~1;
1099 glTranslate $page * 0.5, 0, 0; 1382 glTranslate $page * 0.5, 0, 0;
1383 $page ||= 2;
1100 1384
1101 glColor @$fg; 1385 glColor @$fg;
1102 glBegin GL_LINES; 1386 glBegin GL_LINES;
1103 glVertex 0, 0; glVertex 0, $h; 1387 glVertex 0, 0; glVertex 0, $h;
1104 glVertex $w - 1, 0; glVertex $w - 1, $h; 1388 glVertex $w - 1, 0; glVertex $w - 1, $h;
1133 1417
1134use List::Util qw(min max); 1418use List::Util qw(min max);
1135 1419
1136use SDL; 1420use SDL;
1137use SDL::OpenGL; 1421use SDL::OpenGL;
1138use SDL::OpenGL::Constants;
1139 1422
1140our @ISA = CFClient::UI::Base::; 1423our @ISA = CFClient::UI::Base::;
1141 1424
1142sub new { 1425sub new {
1143 my $class = shift; 1426 my $class = shift;
1156sub key_up { 1439sub key_up {
1157} 1440}
1158 1441
1159sub size_request { 1442sub size_request {
1160 ( 1443 (
1161 1 + int $::WIDTH / 32, 1444 1 + 32 * int $::WIDTH / 32,
1162 1 + int $::HEIGHT / 32, 1445 1 + 32 * int $::HEIGHT / 32,
1163 ) 1446 )
1164} 1447}
1165 1448
1166sub update { 1449sub update {
1167 my ($self) = @_; 1450 my ($self) = @_;
1168 1451
1169 $self->{need_update} = 1; 1452 $self->{need_update} = 1;
1453 $self->SUPER::update;
1170} 1454}
1171 1455
1172sub _draw { 1456sub draw {
1173 my ($self) = @_; 1457 my ($self) = @_;
1174 1458
1175 if (delete $self->{need_update}) { 1459 if (delete $self->{need_update}) {
1176 glNewList $self->{list}, GL_COMPILE; 1460 glNewList $self->{list}, GL_COMPILE;
1177 1461
1197 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 1481 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
1198 } 1482 }
1199 1483
1200 glEnable GL_TEXTURE_2D; 1484 glEnable GL_TEXTURE_2D;
1201 glEnable GL_BLEND; 1485 glEnable GL_BLEND;
1486 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1202 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1487 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1488 glColor 1, 1, 1, 1;
1203 1489
1204 my $sw4 = ($sw + 3) & ~3; 1490 my $sw4 = ($sw + 3) & ~3;
1205 my $darkness = "\x00" x ($sw4 * $sh); 1491 my $darkness = "\x00" x ($sw4 * $sh);
1206 1492
1207 for my $x (0 .. $sw - 1) { 1493 for my $x (0 .. $sw - 1) {
1236# 1522#
1237# $lighting = $pb->get_pixels; 1523# $lighting = $pb->get_pixels;
1238# $lighting =~ s/(.)../$1/gs; 1524# $lighting =~ s/(.)../$1/gs;
1239# } 1525# }
1240 1526
1241 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1242 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1527 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1243 1528
1529 if ($::CFG->{fow_smooth}) { # smooth fog of war
1530 my @conv = (
1531 0.05, 0.05, 0.05,
1532 0.05, 0.60, 0.05,
1533 0.05, 0.05, 0.05,
1534 );
1535 CFClient::glConvolutionParameteri GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_REPLICATE_BORDER;
1536 CFClient::glConvolutionFilter2D GL_CONVOLUTION_2D,
1537 GL_ALPHA, 3, 3, GL_ALPHA, GL_FLOAT,
1538 pack "f*", @conv;
1539 glEnable GL_CONVOLUTION_2D;
1540 }
1541
1244 $darkness = new CFClient::Texture 1542 $darkness = new CFClient::Texture
1245 width => $sw4, 1543 w => $sw4,
1246 height => $sh, 1544 h => $sh,
1247 data => $darkness, 1545 data => $darkness,
1248 internalformat => GL_ALPHA, 1546 internalformat => GL_ALPHA,
1249 format => GL_ALPHA; 1547 format => GL_ALPHA;
1250 1548
1251 glColor 0.45, 0.45, 0.45, 1; 1549 glColor +($::CFG->{fow_intensity}) x 3, 1;
1252 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); 1550 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1551
1552 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1253 1553
1254 glDisable GL_TEXTURE_2D; 1554 glDisable GL_TEXTURE_2D;
1255 glDisable GL_BLEND; 1555 glDisable GL_BLEND;
1256 1556
1257 glEndList; 1557 glEndList;
1258 } 1558 }
1259 1559
1260 glCallList $self->{list}; 1560 glCallList $self->{list};
1561
1562 if ($FOCUS != $self) {
1563 glEnable GL_BLEND;
1564 glColor 0, 0, 1, 0.4;
1565 glBegin GL_QUADS;
1566 glVertex 0, 0;
1567 glVertex 0, $::HEIGHT;
1568 glVertex $::WIDTH, $::HEIGHT;
1569 glVertex $::WIDTH, 0;
1570 glEnd;
1571 glDisable GL_BLEND;
1572 }
1261} 1573}
1262 1574
1263my %DIR = ( 1575my %DIR = (
1264 SDLK_KP8, [1, "north"], 1576 SDLK_KP8, [1, "north"],
1265 SDLK_KP9, [2, "northeast"], 1577 SDLK_KP9, [2, "northeast"],
1281 1593
1282 my $mod = $ev->key_mod; 1594 my $mod = $ev->key_mod;
1283 my $sym = $ev->key_sym; 1595 my $sym = $ev->key_sym;
1284 1596
1285 if ($sym == SDLK_KP5) { 1597 if ($sym == SDLK_KP5) {
1286 $::CONN->send ("command stay fire"); 1598 $::CONN->user_send ("command stay fire");
1599 } elsif ($sym == SDLK_a) {
1600 $::CONN->user_send ("command apply");
1287 } elsif (exists $DIR{$sym}) { 1601 } elsif (exists $DIR{$sym}) {
1288 if ($mod & KMOD_SHIFT) { 1602 if ($mod & KMOD_SHIFT) {
1289 $self->{shft}++; 1603 $self->{shft}++;
1290 $::CONN->send ("command fire $DIR{$sym}[0]"); 1604 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1291 } elsif ($mod & KMOD_CTRL) { 1605 } elsif ($mod & KMOD_CTRL) {
1292 $self->{ctrl}++; 1606 $self->{ctrl}++;
1293 $::CONN->send ("command run $DIR{$sym}[0]"); 1607 $::CONN->user_send ("command run $DIR{$sym}[0]");
1294 } else { 1608 } else {
1295 $::CONN->send ("command $DIR{$sym}[1]"); 1609 $::CONN->user_send ("command $DIR{$sym}[1]");
1296 } 1610 }
1297 } 1611 }
1298} 1612}
1299 1613
1300sub key_up { 1614sub key_up {
1302 1616
1303 my $mod = $ev->key_mod; 1617 my $mod = $ev->key_mod;
1304 my $sym = $ev->key_sym; 1618 my $sym = $ev->key_sym;
1305 1619
1306 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { 1620 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1307 $::CONN->send ("command fire_stop"); 1621 $::CONN->user_send ("command fire_stop");
1308 } 1622 }
1309 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 1623 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1310 $::CONN->send ("command run_stop"); 1624 $::CONN->user_send ("command run_stop");
1311 } 1625 }
1312} 1626}
1313 1627
1314############################################################################# 1628#############################################################################
1315 1629
1362sub size_request { 1676sub size_request {
1363 ($::WIDTH, $::HEIGHT) 1677 ($::WIDTH, $::HEIGHT)
1364} 1678}
1365 1679
1366sub size_allocate { 1680sub size_allocate {
1367 my ($self, $w, $h) = @_; 1681 my ($self, $x, $y, $w, $h) = @_;
1368 1682
1369 $self->SUPER::size_allocate ($w, $h); 1683 $self->_size_allocate ($x, $y, $w, $h);
1370 1684
1371 $_->size_allocate ($_->size_request) 1685 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1372 for @{$self->{children}}; 1686 for @{$self->{children}};
1373} 1687}
1374 1688
1375sub translate { 1689sub translate {
1376 my ($self, $x, $y) = @_; 1690 my ($self, $x, $y) = @_;
1379} 1693}
1380 1694
1381sub update { 1695sub update {
1382 my ($self) = @_; 1696 my ($self) = @_;
1383 1697
1384 $self->size_allocate ($self->size_request); 1698 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1385 ::refresh (); 1699 ::refresh ();
1386} 1700}
1387 1701
1388sub add { 1702sub add {
1389 my ($self, $widget) = @_; 1703 my ($self, $widget) = @_;
1390 1704
1391 $self->SUPER::add ($widget); 1705 $self->SUPER::add ($widget);
1392 1706
1393 $widget->size_allocate ($widget->size_request); 1707 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1394} 1708}
1395 1709
1396sub draw { 1710sub draw {
1397 my ($self) = @_; 1711 my ($self) = @_;
1398 1712

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines