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.372 by root, Sun Jul 15 22:13:43 2007 UTC vs.
Revision 1.377 by root, Mon Jul 16 20:20:30 2007 UTC

944 944
945############################################################################# 945#############################################################################
946 946
947package CFPlus::UI::ViewPort; 947package CFPlus::UI::ViewPort;
948 948
949use List::Util qw(min max);
950
949our @ISA = CFPlus::UI::Window::; 951our @ISA = CFPlus::UI::Window::;
950 952
951sub new { 953sub new {
952 my $class = shift; 954 my $class = shift;
953 955
984} 986}
985 987
986sub set_offset { 988sub set_offset {
987 my ($self, $x, $y) = @_; 989 my ($self, $x, $y) = @_;
988 990
989 $self->{view_x} = int $x; 991 $self->{view_x} = max 0, min $self->child->{w} - $self->{w}, int $x;
990 $self->{view_y} = int $y; 992 $self->{view_y} = max 0, min $self->child->{h} - $self->{h}, int $y;
991 993
994 $self->emit (changed => $self->{view_x}, $self->{view_y});
992 $self->update; 995 $self->update;
993} 996}
994 997
995# hmm, this does not work for topleft of $self... but we should not ask for that 998# hmm, this does not work for topleft of $self... but we should not ask for that
996sub coord2local { 999sub coord2local {
1072 row_expand => [1, 0], 1075 row_expand => [1, 0],
1073 %arg, 1076 %arg,
1074 ); 1077 );
1075 1078
1076 $self->{vp} = new CFPlus::UI::ViewPort 1079 $self->{vp} = new CFPlus::UI::ViewPort
1077 expand => 1, 1080 expand => 1,
1078 scroll_x => $self->{scroll_x}, 1081 scroll_x => $self->{scroll_x},
1079 scroll_y => $self->{scroll_y}, 1082 scroll_y => $self->{scroll_y},
1083 on_changed => sub {
1084 my ($vp, $x, $y) = @_;
1085
1086 $vp->{parent}{hslider}->set_value ($x);
1087 $vp->{parent}{vslider}->set_value ($y);
1088
1089 0
1090 },
1080 ; 1091 ;
1081 1092
1082 $self->SUPER::add (0, 0, $self->{vp}); 1093 $self->SUPER::add (0, 0, $self->{vp});
1083 $self->SUPER::add (1, 0, $self->{vslider}) if $self->{scroll_y};
1084 $self->SUPER::add (0, 1, $self->{hslider}) if $self->{scroll_x};
1085 1094
1086 $self->add ($child) if $child; 1095 $self->add ($child) if $child;
1087 1096
1088 $self 1097 $self
1089} 1098}
1094 my ($self, $widget) = @_; 1103 my ($self, $widget) = @_;
1095 1104
1096 $self->{vp}->add ($self->{child} = $widget); 1105 $self->{vp}->add ($self->{child} = $widget);
1097} 1106}
1098 1107
1108sub update_slider {
1109 my ($self) = @_;
1110
1111 my $child = ($self->{vp} or return)->child;
1112
1113 my ($w1, $w2) = ($child->{w}, $self->{vp}{w});
1114 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1115
1116 my $visible = $w1 > $w2;
1117 if ($visible != $self->{hslider}{visible}) {
1118 $visible ? $self->SUPER::add (0, 1, $self->{hslider})
1119 : $self->{hslider}->hide;
1120 }
1121
1122 my ($h1, $h2) = ($child->{h}, $self->{vp}{h});
1123 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1124
1125 my $visible = $h1 > $h2;
1126 if ($visible != $self->{vslider}{visible}) {
1127 $visible ? $self->SUPER::add (1, 0, $self->{vslider})
1128 : $self->{vslider}->hide;
1129 }
1130}
1131
1132sub update {
1133 my ($self) = @_;
1134
1135 $self->SUPER::update;
1136 $self->update_slider;
1137}
1138
1099sub invoke_mouse_wheel { 1139sub invoke_mouse_wheel {
1100 my ($self, $ev) = @_; 1140 my ($self, $ev) = @_;
1101 1141
1102 return 0 unless $ev->{dy}; # only vertical movements 1142 return 0 unless $ev->{dy}; # only vertical movements for now
1103 1143
1104 $self->{vslider}->emit (mouse_wheel => $ev); 1144 $self->{vslider}->emit (mouse_wheel => $ev);
1105 1145
1106 1 1146 1
1107} 1147}
1108 1148
1109sub update_slider { 1149sub invoke_button_down {
1110 my ($self) = @_; 1150 my ($self, $ev, $x, $y) = @_;
1111 1151
1112 my $child = ($self->{vp} or return)->child; 1152 if ($ev->{button} == 2) {
1153 $self->grab_focus;
1113 1154
1114 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1155 my $ox = $self->{vp}{view_x} + $ev->{x};
1115 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); 1156 my $oy = $self->{vp}{view_y} + $ev->{y};
1116 #$self->{hslider}->set_visibility ($w1 != $w2); 1157
1158 $self->{motion} = sub {
1159 my ($ev, $x, $y) = @_;
1117 1160
1118 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1161 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1119 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); 1162 $self->update;
1120 #$self->{vslider}->set_visibility ($h1 != $h2); 1163 };
1121}
1122 1164
1123sub update { 1165 return 1;
1124 my ($self) = @_; 1166 }
1125 1167
1126 $self->SUPER::update; 1168 0
1127 $self->update_slider; 1169}
1170
1171sub invoke_button_up {
1172 my ($self, $ev, $x, $y) = @_;
1173
1174 if (delete $self->{motion}) {
1175 return 1;
1176 }
1177
1178 0
1179}
1180
1181sub invoke_mouse_motion {
1182 my ($self, $ev, $x, $y) = @_;
1183
1184 if ($self->{motion}) {
1185 $self->{motion}->($ev, $x, $y);
1186 return 1;
1187 }
1188
1189 0
1128} 1190}
1129 1191
1130sub invoke_size_allocate { 1192sub invoke_size_allocate {
1131 my ($self, $w, $h) = @_; 1193 my ($self, $w, $h) = @_;
1132 1194
1509 1571
1510sub new { 1572sub new {
1511 my $class = shift; 1573 my $class = shift;
1512 1574
1513 $class->SUPER::new ( 1575 $class->SUPER::new (
1576 children => [],
1514 col_expand => [], 1577 col_expand => [],
1515 row_expand => [], 1578 row_expand => [],
1516 @_, 1579 @_,
1517 ) 1580 )
1518} 1581}
1519 1582
1520sub children { 1583sub children {
1521 grep $_, map @$_, grep $_, @{ $_[0]{children} } 1584 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1522} 1585}
1523 1586
1587# TODO: store row/col info in child widget and use standard add/del
1524sub add { 1588sub add {
1525 my ($self) = shift; 1589 my ($self) = shift;
1526 1590
1527 while (@_) { 1591 while (@_) {
1528 my ($x, $y, $child) = splice @_, 0, 3, (); 1592 my ($x, $y, $child) = splice @_, 0, 3, ();
1529 $child->set_parent ($self); 1593 $child->set_parent ($self);
1530 $self->{children}[$y][$x] = $child; 1594 $self->{children}[$y][$x] = $child;
1531 } 1595 }
1532 1596
1533 $self->{force_realloc} = 1; 1597 $self->{force_realloc} = 1;
1534 $self->{force_size_alloc} = 1; 1598 $self->{force_size_alloc} = 1;
1535 $self->realloc; 1599 $self->realloc;
1536} 1600}
1537 1601
1538sub remove { 1602sub remove {
1539 my ($self, $child) = @_; 1603 my ($self, $child) = @_;
1540 1604
1541 # TODO: not yet implemented 1605 for (@{ $self->{children} }) {
1606 for (@{ $_ || [] }) {
1607 $_ = undef if $_ == $child;
1608 }
1609 }
1542} 1610}
1543 1611
1544# TODO: move to container class maybe? send children a signal on removal? 1612# TODO: move to container class maybe? send children a signal on removal?
1545sub clear { 1613sub clear {
1546 my ($self) = @_; 1614 my ($self) = @_;
1659 my ($self) = @_; 1727 my ($self) = @_;
1660 1728
1661 for (grep $_, @{$self->{children}}) { 1729 for (grep $_, @{$self->{children}}) {
1662 $_->draw for grep $_, @$_; 1730 $_->draw for grep $_, @$_;
1663 } 1731 }
1732}
1733
1734#############################################################################
1735
1736package CFPlus::UI::Fixed;
1737
1738use List::Util qw(min max);
1739
1740our @ISA = CFPlus::UI::Container::;
1741
1742sub add {
1743 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1744
1745 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1746 $self->SUPER::add ($child);
1747}
1748
1749sub _scale($$$) {
1750 my ($mode, $val, $max) = @_;
1751
1752 $mode eq "abs" ? $val
1753 : $mode eq "rel" ? $val * $max
1754 : 0
1755}
1756
1757sub size_request {
1758 my ($self) = @_;
1759
1760 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1761
1762 # determine overall size by querying abs widgets
1763 for my $child ($self->visible_children) {
1764 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1765
1766 if ($pos eq "abs") {
1767 $w = _scale $size, $w, $child->{req_w};
1768 $h = _scale $size, $h, $child->{req_h};
1769
1770 $x1 = min $x1, $x; $x2 = max $x2, $x + $w;
1771 $y1 = min $y1, $y; $y2 = max $y2, $y + $h;
1772 }
1773 }
1774
1775 my $W = $x2 - $x1;
1776 my $H = $y2 - $y1;
1777
1778 # now layout remaining widgets
1779 for my $child ($self->visible_children) {
1780 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1781
1782 if ($pos ne "abs") {
1783 $x = _scale $pos, $x, $W;
1784 $y = _scale $pos, $x, $H;
1785 $w = _scale $size, $w, $child->{req_w};
1786 $h = _scale $size, $h, $child->{req_h};
1787
1788 $x1 = min $x1, $x; $x2 = max $x2, $x + $w;
1789 $y1 = min $y1, $y; $y2 = max $y2, $y + $h;
1790 }
1791 }
1792
1793 my $W = $x2 - $x1;
1794 my $H = $y2 - $y1;
1795
1796 ($W, $H)
1797}
1798
1799sub invoke_size_allocate {
1800 my ($self, $W, $H) = @_;
1801
1802 for my $child ($self->visible_children) {
1803 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1804
1805 $x = _scale $pos, $x, $W;
1806 $y = _scale $pos, $x, $H;
1807 $w = _scale $size, $w, $child->{req_w};
1808 $h = _scale $size, $h, $child->{req_h};
1809
1810 $child->configure ($x, $y, $w, $h);
1811 }
1812
1813 1
1664} 1814}
1665 1815
1666############################################################################# 1816#############################################################################
1667 1817
1668package CFPlus::UI::Box; 1818package CFPlus::UI::Box;
3328 3478
3329sub new { 3479sub new {
3330 my $class = shift; 3480 my $class = shift;
3331 3481
3332 my $self = $class->SUPER::new ( 3482 my $self = $class->SUPER::new (
3483 size_w => 32,
3484 size_h => 8,
3333 aspect => 1, 3485 aspect => 1,
3334 can_events => 0, 3486 can_events => 0,
3335 @_, 3487 @_,
3336 ); 3488 );
3337 3489
3341 $self->{timer} = Event->timer ( 3493 $self->{timer} = Event->timer (
3342 at => $self->{animspeed} * int $::NOW / $self->{animspeed}, 3494 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3343 hard => 1, 3495 hard => 1,
3344 interval => $self->{animspeed}, 3496 interval => $self->{animspeed},
3345 cb => sub { 3497 cb => sub {
3346 delete $self->{wait_face}; 3498 return unless $::CONN;
3499
3347 ++$widget->{frame}; 3500 ++$widget->{frame};
3501 $self->update_face;
3348 $widget->update; 3502 $self->update;
3349 }, 3503 },
3350 ); 3504 );
3505
3506 $self->update_face;
3351 } 3507 }
3352 3508
3353 $self 3509 $self
3354} 3510}
3355 3511
3512sub update_face {
3513 my ($self) = @_;
3514
3515 return unless $::CONN;
3516
3517 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3518 if ($anim && @$anim) {
3519 delete $self->{wait_face};
3520 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3521 }
3522 }
3523}
3524
3356sub size_request { 3525sub size_request {
3357 (32, 8) 3526 my ($self) = @_;
3527
3528 if ($::CONN) {
3529 if (my $faceid = $::CONN->{faceid}[$self->{face}]) {
3530 if (my $tex = $::CONN->{texture}[$faceid]) {
3531 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3532 } else {
3533 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3534 $self->realloc;
3535 });
3536 }
3537 }
3538 }
3539
3540 ($self->{size_w} || 8, $self->{size_h} || 8)
3358} 3541}
3359 3542
3360sub update { 3543sub update {
3361 my ($self) = @_; 3544 my ($self) = @_;
3362 3545
3370 3553
3371 return unless $::CONN; 3554 return unless $::CONN;
3372 3555
3373 $self->SUPER::_draw; 3556 $self->SUPER::_draw;
3374 3557
3375 my $face;
3376
3377 if ($self->{frame}) {
3378 my $anim = $::CONN->{anim}[$self->{anim}];
3379
3380 $face = $anim->[ $self->{frame} % @$anim ]
3381 if $anim && @$anim;
3382 }
3383
3384 my $faceid = $::CONN->{faceid}[$face || $self->{face}] 3558 my $faceid = $::CONN->{faceid}[$self->{face}]
3385 or return; 3559 or return;
3386 3560
3387 my $tex = $::CONN->{texture}[$faceid]; 3561 my $tex = $::CONN->{texture}[$faceid];
3388 3562
3389 if ($tex) { 3563 if ($tex) {
3390 glEnable GL_TEXTURE_2D; 3564 glEnable GL_TEXTURE_2D;
3391 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3565 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3392 glColor 0, 0, 0, 1; 3566 glColor 0, 0, 0, 1;
3393 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3567 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3394 glDisable GL_TEXTURE_2D; 3568 glDisable GL_TEXTURE_2D;
3395 } else {
3396 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3397 $self->update;
3398 });
3399 } 3569 }
3400} 3570}
3401 3571
3402sub destroy { 3572sub destroy {
3403 my ($self) = @_; 3573 my ($self) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines