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.75 by root, Tue Apr 11 20:44:49 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
82 83
83 bless { 84 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
89} 92}
90 93
91sub move { 94sub move {
102sub size_request { 105sub size_request {
103 require Carp; 106 require Carp;
104 Carp::confess "size_request is abtract"; 107 Carp::confess "size_request is abtract";
105} 108}
106 109
107sub size_allocate { 110sub _size_allocate {
108 my ($self, $w, $h) = @_; 111 my ($self, $x, $y, $w, $h) = @_;
112
113 $self->{x} = $x;
114 $self->{y} = $y;
115
116 return unless $self->{w} != $w || $self->{h} != $h;
109 117
110 $self->{w} = $w; 118 $self->{w} = $w;
111 $self->{h} = $h; 119 $self->{h} = $h;
120
121 1
122}
123
124sub size_allocate {
125 my ($self, $x, $y, $w, $h) = @_;
126
127 $self->_size_allocate ($x, $y, $w, $h);
112} 128}
113 129
114# translate global koordinates to local coordinate system 130# translate global koordinates to local coordinate system
115sub translate { 131sub translate {
116 my ($self, $x, $y) = @_; 132 my ($self, $x, $y) = @_;
167 if ($self == $HOVER) { 183 if ($self == $HOVER) {
168 my ($x, $y) = @$self{qw(x y)}; 184 my ($x, $y) = @$self{qw(x y)};
169 185
170 glColor 1, 1, 1, 0.1; 186 glColor 1, 1, 1, 0.1;
171 glEnable GL_BLEND; 187 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
172 glBegin GL_QUADS; 189 glBegin GL_QUADS;
173 glVertex $x , $y; 190 glVertex $x , $y;
174 glVertex $x + $self->{w}, $y; 191 glVertex $x + $self->{w}, $y;
175 glVertex $x + $self->{w}, $y + $self->{h}; 192 glVertex $x + $self->{w}, $y + $self->{h};
176 glVertex $x , $y + $self->{h}; 193 glVertex $x , $y + $self->{h};
318 $self->{children} = [ 335 $self->{children} = [
319 sort { $a->{z} <=> $b->{z} } 336 sort { $a->{z} <=> $b->{z} }
320 @{$self->{children}}, $chld 337 @{$self->{children}}, $chld
321 ]; 338 ];
322 339
323 $self->size_allocate ($self->{w}, $self->{h}) 340 $self->{w} = $self->{h} = -1;
324 if $self->{w}; #TODO: check for "realised state" 341 $self->update;
325} 342}
326 343
327sub remove { 344sub remove {
328 my ($self, $widget) = @_; 345 my ($self, $widget) = @_;
329 346
330 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 347 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
331 348
332 $self->size_allocate ($self->{w}, $self->{h}); 349 $self->size_allocate (0, 0, $self->{w}, $self->{h});
333} 350}
334 351
335sub find_widget { 352sub find_widget {
336 my ($self, $x, $y) = @_; 353 my ($self, $x, $y) = @_;
337 354
390sub size_request { 407sub size_request {
391 $_[0]{children}[0]->size_request 408 $_[0]{children}[0]->size_request
392} 409}
393 410
394sub size_allocate { 411sub size_allocate {
395 my ($self, $w, $h) = @_; 412 my ($self, $x, $y, $w, $h) = @_;
396 413
397 return unless $self->{w} != $w || $self->{h} != $h; 414 $self->_size_allocate ($x, $y, $w, $h) or return;
398 415
399 $self->SUPER::size_allocate ($w, $h);
400 $self->{children}[0]->size_allocate ($w, $h); 416 $self->{children}[0]->size_allocate (0, 0, $w, $h);
401} 417}
402 418
403############################################################################# 419#############################################################################
404 420
405package CFClient::UI::Window; 421package CFClient::UI::Window;
430 $self->{w}, $self->{h}, sub { $self->child->draw } 446 $self->{w}, $self->{h}, sub { $self->child->draw }
431 ); 447 );
432} 448}
433 449
434sub size_allocate { 450sub size_allocate {
435 my ($self, $w, $h) = @_; 451 my ($self, $x, $y, $w, $h) = @_;
436 452
437 return unless $self->{w} != $w || $self->{h} != $h; 453 $self->_size_allocate ($x, $y, $w, $h) or return;
438 454
439 $self->{w} = $w;
440 $self->{h} = $h;
441
442 $self->child->size_allocate ($w, $h); 455 $self->child->size_allocate (0, 0, $w, $h);
443 456
444 $self->render_chld; 457 $self->render_chld;
445} 458}
446 459
447sub _draw { 460sub _draw {
451 464
452 my $tex = $self->{texture} 465 my $tex = $self->{texture}
453 or return; 466 or return;
454 467
455 glEnable GL_BLEND; 468 glEnable GL_BLEND;
469 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
456 glEnable GL_TEXTURE_2D; 470 glEnable GL_TEXTURE_2D;
457 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 471 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
458 472
459 $tex->draw_quad (0, 0, $w, $h); 473 $tex->draw_quad (0, 0, $w, $h);
460 474
479 493
480 map { $_ + 4 } $chld->size_request; 494 map { $_ + 4 } $chld->size_request;
481} 495}
482 496
483sub size_allocate { 497sub size_allocate {
484 my ($self, $w, $h) = @_; 498 my ($self, $x, $y, $w, $h) = @_;
485 499
486 return unless $self->{w} != $w || $self->{h} != $h; 500 $self->_size_allocate ($x, $y, $w, $h) or return;
487 501
488 $self->{w} = $w;
489 $self->{h} = $h;
490
491 $self->child->size_allocate ($w - 4, $h - 4); 502 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
492 $self->child->move (2, 2);
493} 503}
494 504
495sub _draw { 505sub _draw {
496 my ($self) = @_; 506 my ($self) = @_;
497 507
534 544
535 ($w, $h) 545 ($w, $h)
536} 546}
537 547
538sub size_allocate { 548sub size_allocate {
539 my ($self, $w, $h) = @_; 549 my ($self, $x, $y, $w, $h) = @_;
540 550
541 return unless $self->{w} != $w || $self->{h} != $h; 551 $self->_size_allocate ($x, $y, $w, $h) or return;
542
543 $self->SUPER::size_allocate ($w, $h);
544 552
545 $h -= $tex[1]->{h}; 553 $h -= $tex[1]->{h};
546 $h -= $tex[4]->{h}; 554 $h -= $tex[4]->{h};
547 $w -= $tex[2]->{w}; 555 $w -= $tex[2]->{w};
548 $w -= $tex[3]->{w}; 556 $w -= $tex[3]->{w};
550 $h = $h < 0 ? 0 : $h; 558 $h = $h < 0 ? 0 : $h;
551 $w = $w < 0 ? 0 : $w; 559 $w = $w < 0 ? 0 : $w;
552 560
553 my $child = $self->child; 561 my $child = $self->child;
554 562
555 $child->size_allocate ($w, $h);
556 $child->move ($tex[3]->{w}, $tex[1]->{h}); 563 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
557} 564}
558 565
559sub _draw { 566sub _draw {
560 my ($self) = @_; 567 my ($self) = @_;
561 568
562 my ($w, $h) = ($self->{w}, $self->{h}); 569 my ($w, $h) = ($self->{w}, $self->{h});
563 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 570 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
564 571
565 glEnable GL_BLEND; 572 glEnable GL_BLEND;
573 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
566 glEnable GL_TEXTURE_2D; 574 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; 575 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
569 576
570 my $top = $tex[1]; 577 my $top = $tex[1];
571 $top->draw_quad (0, 0, $w, $top->{h}); 578 $top->draw_quad (0, 0, $w, $top->{h});
572 579
601 608
602package CFClient::UI::Table; 609package CFClient::UI::Table;
603 610
604our @ISA = CFClient::UI::Base::; 611our @ISA = CFClient::UI::Base::;
605 612
613use List::Util qw(max sum);
614
606use SDL::OpenGL; 615use SDL::OpenGL;
607 616
608sub add { 617sub add {
609 my ($self, $x, $y, $chld) = @_; 618 my ($self, $x, $y, $chld) = @_;
610 my $old_chld = $self->{children}[$y][$x];
611 619
612 $self->{children}[$y][$x] = $chld; 620 $self->{children}[$y][$x] = $chld;
613 $chld->set_parent ($self); 621 $chld->set_parent ($self);
622
623 $self->{w} = $self->{h} = -1;
614 $self->update; 624 $self->update;
615} 625}
616 626
617sub max_row_height { 627sub get_wh {
618 my ($self, $row) = @_; 628 my ($self) = @_;
619 629
620 my $hs = 0; 630 my (@w, @h);
621 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 631
632 for my $y (0 .. $#{$self->{children}}) {
622 my $c = $self->{children}->[$row]->[$xi]; 633 my $row = $self->{children}[$y]
623 if ($c) { 634 or next;
635
636 for my $x (0 .. $#$row) {
637 my $widget = $row->[$x]
638 or next;
624 my ($w, $h) = $c->size_request; 639 my ($w, $h) = $widget->size_request;
625 if ($hs < $h) { $hs = $h } 640
641 $w[$x] = max $w[$x], $w;
642 $h[$y] = max $h[$y], $h;
626 } 643 }
627 } 644 }
628 return $hs;
629}
630 645
631sub max_col_width { 646 (\@w, \@h)
647}
648
649sub size_request {
632 my ($self, $col) = @_; 650 my ($self) = @_;
633 651
652 my ($ws, $hs) = $self->get_wh;
653
654 (
655 (List::Util::sum @$ws),
656 (List::Util::sum @$hs),
657 )
658}
659
660sub size_allocate {
661 my ($self, $x, $y, $w, $h) = @_;
662
663 $self->_size_allocate ($x, $y, $w, $h) or return;
664
665 my ($ws, $hs) = $self->get_wh;
666
667 my $req_w = List::Util::sum @$ws;
668 my $req_h = List::Util::sum @$hs;
669
670 # linearly scale sizes
671 $_ *= $req_w / $w for @$ws;
672 $_ *= $req_h / $h for @$hs;
673
674 my $y;
675
676 for my $r (0 .. $#{$self->{children}}) {
677 my $row = $self->{children}[$r]
678 or next;
679
634 my $ws = 0; 680 my $x = 0;
635 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 681 my $row_h = $hs->[$r];
636 my $c = ($self->{children}->[$yi] || [])->[$col]; 682
637 if ($c) { 683 for my $c (0 .. $#$row) {
638 my ($w, $h) = $c->size_request; 684 my $widget = $row->[$c]
639 if ($ws < $w) { $ws = $w } 685 or next;
686
687 my $col_w = $ws->[$c];
688
689 $widget->size_allocate ($x, $y, $col_w, $row_h);
690
691 $x += $col_w;
640 } 692 }
641 }
642 return $ws;
643}
644 693
645sub size_request { 694 $y += $row_h;
646 my ($self) = @_;
647
648 my ($hs, $ws) = (0, 0);
649
650 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
651 $hs += $self->max_row_height ($yi);
652 } 695 }
653 696
654 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
655 my $wm = 0;
656 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
657 $wm += $self->max_col_width ($xi)
658 }
659 if ($ws < $wm) { $ws = $wm }
660 }
661
662 return ($ws, $hs);
663} 697}
664 698
665sub _draw { 699sub _draw {
666 my ($self) = @_; 700 my ($self) = @_;
667 701
668 my $y = 0; 702 for (grep $_, @{$self->{children}}) {
669 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 703 $_->draw for grep $_, @$_;
670 my $x = 0;
671
672 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
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 } 704 }
685} 705}
686 706
687############################################################################# 707#############################################################################
688 708
702 (List::Util::sum map $_->[1], @alloc), 722 (List::Util::sum map $_->[1], @alloc),
703 ) 723 )
704} 724}
705 725
706sub size_allocate { 726sub size_allocate {
707 my ($self, $w, $h) = @_; 727 my ($self, $x, $y, $w, $h) = @_;
708 728
709 return unless $self->{w} != $w || $self->{h} != $h; 729 $self->_size_allocate ($x, $y, $w, $h) or return;
710
711 $self->{w} = $w;
712 $self->{h} = $h;
713 730
714 return unless $self->{h}; 731 return unless $self->{h};
715 732
716 my $children = $self->{children}; 733 my $children = $self->{children};
717 734
739 756
740 my $y = 0; 757 my $y = 0;
741 for (0 .. $#$children) { 758 for (0 .. $#$children) {
742 my $child = $children->[$_]; 759 my $child = $children->[$_];
743 my $h = $h[$_]; 760 my $h = $h[$_];
744 $child->move (0, $y);
745 $child->size_allocate ($w, $h); 761 $child->size_allocate (0, $y, $w, $h);
746 762
747 $y += $h; 763 $y += $h;
748 } 764 }
749} 765}
750 766
816# ($w, $h) 832# ($w, $h)
817# } 833# }
818} 834}
819 835
820sub size_allocate { 836sub size_allocate {
821 my ($self, $w, $h) = @_; 837 my ($self, $x, $y, $w, $h) = @_;
822 838
823 return unless $self->{w} != $w || $self->{h} != $h; 839 $self->_size_allocate ($x, $y, $w, $h) or return;
824 840
825 $self->SUPER::size_allocate ($w, $h);
826 delete $self->{texture}; 841 delete $self->{texture};
827} 842}
828 843
829sub update { 844sub update {
830 my ($self) = @_; 845 my ($self) = @_;
836sub _draw { 851sub _draw {
837 my ($self) = @_; 852 my ($self) = @_;
838 853
839 my $tex = $self->{texture} ||= do { 854 my $tex = $self->{texture} ||= do {
840 $self->{layout}->set_width ($self->{w}); 855 $self->{layout}->set_width ($self->{w});
841 new_from_layout CFClient::Texture $self->{layout}; 856 new_from_layout CFClient::Texture $self->{layout}
842 }; 857 };
843 858
844 glEnable GL_BLEND; 859 glEnable GL_BLEND;
860 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
845 glEnable GL_TEXTURE_2D; 861 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; 862 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
848 863
849 glColor @{$self->{fg}}; 864 glColor @{$self->{fg}};
850 865
851 my $x = 866 my $x =
852 $self->{align} < 0 ? 0 867 $self->{align} < 0 ? 0
853 : $self->{align} > 0 ? $self->{w} - $self->{texture}{w} 868 : $self->{align} > 0 ? $self->{w} - $tex->{w}
854 : ($self->{w} + $self->{texture}{w}) * 0.5; 869 : ($self->{w} - $tex->{w}) * 0.5;
855 870
856 $tex->draw_quad ($x, 0); 871 $tex->draw_quad ($x, 0);
857 872
873 glDisable GL_TEXTURE_2D;
858 glDisable GL_BLEND; 874 glDisable GL_BLEND;
859 glDisable GL_TEXTURE_2D;
860} 875}
861 876
862############################################################################# 877#############################################################################
863 878
864package CFClient::UI::Entry; 879package CFClient::UI::Entry;
906 921
907 ($w + 1, $h) # add 1 for cursor 922 ($w + 1, $h) # add 1 for cursor
908} 923}
909 924
910sub size_allocate { 925sub size_allocate {
911 my ($self, $w, $h) = @_; 926 my ($self, $x, $y, $w, $h) = @_;
912 927
913 return unless $self->{w} != $w || $self->{h} != $h;
914
915 $self->SUPER::size_allocate ($w, $h); 928 $self->SUPER::size_allocate ($x, $y, $w, $h);
916 929
917 $self->_set_text ($self->{text}); 930 $self->_set_text ($self->{text});
918} 931}
919 932
920sub set_text { 933sub set_text {
1016package CFClient::UI::Slider; 1029package CFClient::UI::Slider;
1017 1030
1018use strict; 1031use strict;
1019 1032
1020use SDL::OpenGL; 1033use SDL::OpenGL;
1021use SDL::OpenGL::Constants;
1022 1034
1023our @ISA = CFClient::UI::DrawBG::; 1035our @ISA = CFClient::UI::DrawBG::;
1024 1036
1025sub size_request { 1037sub size_request {
1026 my ($self) = @_; 1038 my ($self) = @_;
1133 1145
1134use List::Util qw(min max); 1146use List::Util qw(min max);
1135 1147
1136use SDL; 1148use SDL;
1137use SDL::OpenGL; 1149use SDL::OpenGL;
1138use SDL::OpenGL::Constants;
1139 1150
1140our @ISA = CFClient::UI::Base::; 1151our @ISA = CFClient::UI::Base::;
1141 1152
1142sub new { 1153sub new {
1143 my $class = shift; 1154 my $class = shift;
1165 1176
1166sub update { 1177sub update {
1167 my ($self) = @_; 1178 my ($self) = @_;
1168 1179
1169 $self->{need_update} = 1; 1180 $self->{need_update} = 1;
1181 $self->SUPER::update;
1170} 1182}
1171 1183
1172sub _draw { 1184sub _draw {
1173 my ($self) = @_; 1185 my ($self) = @_;
1174 1186
1197 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 1209 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
1198 } 1210 }
1199 1211
1200 glEnable GL_TEXTURE_2D; 1212 glEnable GL_TEXTURE_2D;
1201 glEnable GL_BLEND; 1213 glEnable GL_BLEND;
1214 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1202 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1215 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1203 1216
1204 my $sw4 = ($sw + 3) & ~3; 1217 my $sw4 = ($sw + 3) & ~3;
1205 my $darkness = "\x00" x ($sw4 * $sh); 1218 my $darkness = "\x00" x ($sw4 * $sh);
1206 1219
1236# 1249#
1237# $lighting = $pb->get_pixels; 1250# $lighting = $pb->get_pixels;
1238# $lighting =~ s/(.)../$1/gs; 1251# $lighting =~ s/(.)../$1/gs;
1239# } 1252# }
1240 1253
1241 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1242 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1254 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1243 1255
1244 $darkness = new CFClient::Texture 1256 $darkness = new CFClient::Texture
1245 width => $sw4, 1257 w => $sw4,
1246 height => $sh, 1258 h => $sh,
1247 data => $darkness, 1259 data => $darkness,
1248 internalformat => GL_ALPHA, 1260 internalformat => GL_ALPHA,
1249 format => GL_ALPHA; 1261 format => GL_ALPHA;
1250 1262
1251 glColor 0.45, 0.45, 0.45, 1; 1263 glColor 0.45, 0.45, 0.45, 1;
1362sub size_request { 1374sub size_request {
1363 ($::WIDTH, $::HEIGHT) 1375 ($::WIDTH, $::HEIGHT)
1364} 1376}
1365 1377
1366sub size_allocate { 1378sub size_allocate {
1367 my ($self, $w, $h) = @_; 1379 my ($self, $x, $y, $w, $h) = @_;
1368 1380
1369 $self->SUPER::size_allocate ($w, $h); 1381 $self->_size_allocate ($x, $y, $w, $h);
1370 1382
1371 $_->size_allocate ($_->size_request) 1383 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1372 for @{$self->{children}}; 1384 for @{$self->{children}};
1373} 1385}
1374 1386
1375sub translate { 1387sub translate {
1376 my ($self, $x, $y) = @_; 1388 my ($self, $x, $y) = @_;
1379} 1391}
1380 1392
1381sub update { 1393sub update {
1382 my ($self) = @_; 1394 my ($self) = @_;
1383 1395
1384 $self->size_allocate ($self->size_request); 1396 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1385 ::refresh (); 1397 ::refresh ();
1386} 1398}
1387 1399
1388sub add { 1400sub add {
1389 my ($self, $widget) = @_; 1401 my ($self, $widget) = @_;
1390 1402
1391 $self->SUPER::add ($widget); 1403 $self->SUPER::add ($widget);
1392 1404
1393 $widget->size_allocate ($widget->size_request); 1405 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1394} 1406}
1395 1407
1396sub draw { 1408sub draw {
1397 my ($self) = @_; 1409 my ($self) = @_;
1398 1410

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines