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.76 by root, Tue Apr 11 21:24:09 2006 UTC vs.
Revision 1.78 by root, Tue Apr 11 22:49:13 2006 UTC

43 43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
47 47
48 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 52 $grab->update if $grab;
53 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
200 my ($self) = @_; 200 my ($self) = @_;
201 201
202 warn "no draw defined for $self\n"; 202 warn "no draw defined for $self\n";
203} 203}
204 204
205sub bbox {
206 my ($self) = @_;
207 my ($w, $h) = $self->size_request;
208 (
209 $self->{x},
210 $self->{y},
211 $self->{x} = $w,
212 $self->{y} = $h
213 )
214}
215
216sub find_widget { 205sub find_widget {
217 my ($self, $x, $y) = @_; 206 my ($self, $x, $y) = @_;
218 207
219 return $self 208 return $self
220 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 209 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
221 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 210 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
222 211
223 () 212 ()
224} 213}
225
226sub del_parent { $_[0]->{parent} = undef }
227 214
228sub set_parent { 215sub set_parent {
229 my ($self, $par) = @_; 216 my ($self, $par) = @_;
230 217
231 $self->{parent} = $par; 218 $self->{parent} = $par;
330 317
331 $self 318 $self
332} 319}
333 320
334sub add { 321sub add {
335 my ($self, $chld, $expand) = @_; 322 my ($self, $chld) = @_;
336 323
337 $chld->{expand} = $expand;
338 $chld->set_parent ($self); 324 $chld->set_parent ($self);
339 325
340 $self->{children} = [ 326 $self->{children} = [
341 sort { $a->{z} <=> $b->{z} } 327 sort { $a->{z} <=> $b->{z} }
342 @{$self->{children}}, $chld 328 @{$self->{children}}, $chld
538 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 524 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
539 525
540sub size_request { 526sub size_request {
541 my ($self) = @_; 527 my ($self) = @_;
542 528
529 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
530
543 my ($w, $h) = $self->SUPER::size_request; 531 my ($w, $h) = $self->SUPER::size_request;
544 532
545 $h += $tex[1]->{h}; 533 $h += $tex[1]->{h};
546 $h += $tex[4]->{h}; 534 $h += $tex[4]->{h};
547 $w += $tex[2]->{w}; 535 $w += $tex[2]->{w};
553sub size_allocate { 541sub size_allocate {
554 my ($self, $x, $y, $w, $h) = @_; 542 my ($self, $x, $y, $w, $h) = @_;
555 543
556 $self->_size_allocate ($x, $y, $w, $h) or return; 544 $self->_size_allocate ($x, $y, $w, $h) or return;
557 545
558 $h -= $tex[1]->{h}; 546 $h -= $tex[1]{h};
559 $h -= $tex[4]->{h}; 547 $h -= $tex[4]{h};
560 $w -= $tex[2]->{w}; 548 $w -= $tex[2]{w};
561 $w -= $tex[3]->{w}; 549 $w -= $tex[3]{w};
562 550
563 $h = $h < 0 ? 0 : $h; 551 $h = $h < 0 ? 0 : $h;
564 $w = $w < 0 ? 0 : $w; 552 $w = $w < 0 ? 0 : $w;
565 553
566 my $child = $self->child; 554 my $child = $self->child;
567 555
568 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); 556 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
557}
558
559sub button_down {
560 my ($self, $ev, $x, $y) = @_;
561
562 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w}
563 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) {
564
565 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
566 my ($bw, $bh) = ($self->{w}, $self->{h});
567
568 $self->{motion} = sub {
569 my ($ev, $x, $y) = @_;
570
571 ($x, $y) = ($ev->motion_x, $ev->motion_y);
572
573 $self->{user_w} = $bw + $x - $ox;
574 $self->{user_h} = $bh + $y - $oy;
575 $self->update;
576 };
577
578 } elsif ($x >= 0 && $x < $self->{w}
579 && $y >= 0 && $y < $tex[1]{h}) {
580
581 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
582 my ($bx, $by) = ($self->{x}, $self->{y});
583
584 $self->{motion} = sub {
585 my ($ev, $x, $y) = @_;
586
587 ($x, $y) = ($ev->motion_x, $ev->motion_y);
588
589 $self->move ($bx + $x - $ox, $by + $y - $oy);
590 $self->update;
591 };
592 }
593}
594
595sub button_up {
596 my ($self, $ev, $x, $y) = @_;
597
598 delete $self->{motion};
599}
600
601sub mouse_motion {
602 my ($self, $ev, $x, $y) = @_;
603
604 $self->{motion}->($ev, $x, $y) if $self->{motion};
569} 605}
570 606
571sub _draw { 607sub _draw {
572 my ($self) = @_; 608 my ($self) = @_;
573 609
618 654
619use List::Util qw(max sum); 655use List::Util qw(max sum);
620 656
621use SDL::OpenGL; 657use SDL::OpenGL;
622 658
659sub new {
660 my $class = shift;
661
662 $class->SUPER::new (
663 col_expand => [],
664 @_
665 )
666}
667
623sub add { 668sub add {
624 my ($self, $x, $y, $chld) = @_; 669 my ($self, $x, $y, $chld) = @_;
625 670
626 $self->{children}[$y][$x] = $chld; 671 $self->{children}[$y][$x] = $chld;
627 $chld->set_parent ($self); 672 $chld->set_parent ($self);
656 my ($self) = @_; 701 my ($self) = @_;
657 702
658 my ($ws, $hs) = $self->get_wh; 703 my ($ws, $hs) = $self->get_wh;
659 704
660 ( 705 (
661 (List::Util::sum @$ws), 706 (sum @$ws),
662 (List::Util::sum @$hs), 707 (sum @$hs),
663 ) 708 )
664} 709}
665 710
666sub size_allocate { 711sub size_allocate {
667 my ($self, $x, $y, $w, $h) = @_; 712 my ($self, $x, $y, $w, $h) = @_;
668 713
669 $self->_size_allocate ($x, $y, $w, $h) or return; 714 $self->_size_allocate ($x, $y, $w, $h) or return;
670 715
671 my ($ws, $hs) = $self->get_wh; 716 my ($ws, $hs) = $self->get_wh;
672 717
673 my $req_w = List::Util::sum @$ws; 718 my $req_w = sum @$ws;
674 my $req_h = List::Util::sum @$hs; 719 my $req_h = sum @$hs;
720
721 # TODO: nicer code && do row_expand
722 my @col_expand = @{$self->{col_expand}};
723 @col_expand = (1) x @$ws unless @col_expand;
724 my $col_expand = (sum @col_expand) || 1;
675 725
676 # linearly scale sizes 726 # linearly scale sizes
677 $_ *= $req_w / $w for @$ws; 727 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
678 $_ *= $req_h / $h for @$hs; 728 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
679 729
680 my $y; 730 my $y;
681 731
682 for my $r (0 .. $#{$self->{children}}) { 732 for my $r (0 .. $#{$self->{children}}) {
683 my $row = $self->{children}[$r] 733 my $row = $self->{children}[$r]
760 810
761 my $req_h = List::Util::sum @h; 811 my $req_h = List::Util::sum @h;
762 812
763 if ($req_h > $h) { 813 if ($req_h > $h) {
764 # ah well, not enough space 814 # ah well, not enough space
765 $_ = $h[$_] * $h / $req_h for @h; 815 $_ *= $h / $req_h for @h;
766 } else { 816 } else {
767 my @exp = grep $_->{expand}, @$children; 817 my $exp = List::Util::sum map $_->{expand}, @$children;
768 @exp = @$children unless @exp; 818 $exp ||= 1;
769 819
770 my %exp = map +($_ => 1), @exp;
771
772 for (0 .. $#$children) { 820 for (0 .. $#$children) {
773 my $child = $children->[$_]; 821 my $child = $children->[$_];
774 822
775 my $alloc_h = $h[$_]; 823 my $alloc_h = $h[$_];
776 $alloc_h += ($h - $req_h) / @exp if $exp{$child}; 824 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
777 $h[$_] = $alloc_h; 825 $h[$_] = $alloc_h;
778 } 826 }
779 } 827 }
780 828
781 my $y = 0; 829 my $y = 0;
820 868
821 my $req_h = List::Util::sum @h; 869 my $req_h = List::Util::sum @h;
822 870
823 if ($req_h > $h) { 871 if ($req_h > $h) {
824 # ah well, not enough space 872 # ah well, not enough space
825 $_ = $h[$_] * $h / $req_h for @h; 873 $_ *= $h / $req_h for @h;
826 } else { 874 } else {
827 my @exp = grep $_->{expand}, @$children; 875 my $exp = List::Util::sum map $_->{expand}, @$children;
828 @exp = @$children unless @exp; 876 $exp ||= 1;
829 877
830 my %exp = map +($_ => 1), @exp;
831
832 for (0 .. $#$children) { 878 for (0 .. $#$children) {
833 my $child = $children->[$_]; 879 my $child = $children->[$_];
834 880
835 my $alloc_h = $h[$_]; 881 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
836 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
837 $h[$_] = $alloc_h;
838 } 882 }
839 } 883 }
840 884
841 my $y = 0; 885 my $y = 0;
842 for (0 .. $#$children) { 886 for (0 .. $#$children) {
931sub _draw { 975sub _draw {
932 my ($self) = @_; 976 my ($self) = @_;
933 977
934 my $tex = $self->{texture} ||= do { 978 my $tex = $self->{texture} ||= do {
935 $self->{layout}->set_width ($self->{w}); 979 $self->{layout}->set_width ($self->{w});
980 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{height});
936 new_from_layout CFClient::Texture $self->{layout} 981 new_from_layout CFClient::Texture $self->{layout}
937 }; 982 };
938 983
939 glEnable GL_BLEND; 984 glEnable GL_BLEND;
940 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 985 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
946 my $x = 991 my $x =
947 $self->{align} < 0 ? $self->{padding} 992 $self->{align} < 0 ? $self->{padding}
948 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 993 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
949 : ($self->{w} - $tex->{w}) * 0.5; 994 : ($self->{w} - $tex->{w}) * 0.5;
950 995
951 $tex->draw_quad ($x, 0); 996 $tex->draw_quad ($x, $self->{padding});
952 997
953 glDisable GL_TEXTURE_2D; 998 glDisable GL_TEXTURE_2D;
954 glDisable GL_BLEND; 999 glDisable GL_BLEND;
955} 1000}
956 1001
1257sub key_up { 1302sub key_up {
1258} 1303}
1259 1304
1260sub size_request { 1305sub size_request {
1261 ( 1306 (
1262 1 + int $::WIDTH / 32, 1307 1 + 32 * int $::WIDTH / 32,
1263 1 + int $::HEIGHT / 32, 1308 1 + 32 * int $::HEIGHT / 32,
1264 ) 1309 )
1265} 1310}
1266 1311
1267sub update { 1312sub update {
1268 my ($self) = @_; 1313 my ($self) = @_;
1269 1314
1270 $self->{need_update} = 1; 1315 $self->{need_update} = 1;
1271 $self->SUPER::update; 1316 $self->SUPER::update;
1272} 1317}
1273 1318
1274sub _draw { 1319sub draw {
1275 my ($self) = @_; 1320 my ($self) = @_;
1276 1321
1277 if (delete $self->{need_update}) { 1322 if (delete $self->{need_update}) {
1278 glNewList $self->{list}, GL_COMPILE; 1323 glNewList $self->{list}, GL_COMPILE;
1279 1324

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines