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.371 by root, Sun Jul 15 22:07:48 2007 UTC vs.
Revision 1.379 by root, Tue Jul 17 12:04:18 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
991 my $x = max 0, min $self->child->{w} - $self->{w}, int $x;
992 my $y = max 0, min $self->child->{h} - $self->{h}, int $y;
993
994 if ($x != $self->{view_x} or $y != $self->{view_y}) {
989 $self->{view_x} = int $x; 995 $self->{view_x} = $x;
990 $self->{view_y} = int $y; 996 $self->{view_y} = $y;
991 997
998 $self->emit (changed => $x, $y);
992 $self->update; 999 $self->update;
1000 }
993} 1001}
994 1002
995# hmm, this does not work for topleft of $self... but we should not ask for that 1003# hmm, this does not work for topleft of $self... but we should not ask for that
996sub coord2local { 1004sub coord2local {
997 my ($self, $x, $y) = @_; 1005 my ($self, $x, $y) = @_;
1061 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1069 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1062 }, 1070 },
1063 ; 1071 ;
1064 1072
1065 $self = $class->SUPER::new ( 1073 $self = $class->SUPER::new (
1066 vp => (new CFPlus::UI::ViewPort expand => 1), 1074 scroll_x => 0,
1075 scroll_y => 1,
1067 can_events => 1, 1076 can_events => 1,
1068 hslider => $hslider, 1077 hslider => $hslider,
1069 vslider => $vslider, 1078 vslider => $vslider,
1070 col_expand => [1, 0], 1079 col_expand => [1, 0],
1071 row_expand => [1, 0], 1080 row_expand => [1, 0],
1072 %arg, 1081 %arg,
1073 ); 1082 );
1074 1083
1084 $self->{vp} = new CFPlus::UI::ViewPort
1085 expand => 1,
1086 scroll_x => $self->{scroll_x},
1087 scroll_y => $self->{scroll_y},
1088 on_changed => sub {
1089 my ($vp, $x, $y) = @_;
1090
1091 $vp->{parent}{hslider}->set_value ($x);
1092 $vp->{parent}{vslider}->set_value ($y);
1093
1094 0
1095 },
1096 ;
1097
1075 $self->SUPER::add (0, 0, $self->{vp}); 1098 $self->SUPER::add (0, 0, $self->{vp});
1076 $self->SUPER::add (1, 0, $self->{vslider});
1077 $self->SUPER::add (0, 1, $self->{hslider});
1078 1099
1079 $self->add ($child) if $child; 1100 $self->add ($child) if $child;
1080 1101
1081 $self 1102 $self
1082} 1103}
1087 my ($self, $widget) = @_; 1108 my ($self, $widget) = @_;
1088 1109
1089 $self->{vp}->add ($self->{child} = $widget); 1110 $self->{vp}->add ($self->{child} = $widget);
1090} 1111}
1091 1112
1113sub update_slider {
1114 my ($self) = @_;
1115
1116 my $child = ($self->{vp} or return)->child;
1117
1118 my ($w1, $w2) = ($child->{w}, $self->{vp}{w});
1119 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1120
1121 my $visible = $w1 > $w2;
1122 if ($visible != $self->{hslider}{visible}) {
1123 $visible ? $self->SUPER::add (0, 1, $self->{hslider})
1124 : $self->{hslider}->hide;
1125 }
1126
1127 my ($h1, $h2) = ($child->{h}, $self->{vp}{h});
1128 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1129
1130 my $visible = $h1 > $h2;
1131 if ($visible != $self->{vslider}{visible}) {
1132 $visible ? $self->SUPER::add (1, 0, $self->{vslider})
1133 : $self->{vslider}->hide;
1134 }
1135}
1136
1137sub update {
1138 my ($self) = @_;
1139
1140 $self->SUPER::update;
1141 $self->update_slider;
1142}
1143
1092sub invoke_mouse_wheel { 1144sub invoke_mouse_wheel {
1093 my ($self, $ev) = @_; 1145 my ($self, $ev) = @_;
1094 1146
1095 return 0 unless $ev->{dy}; # only vertical movements 1147 return 0 unless $ev->{dy}; # only vertical movements for now
1096 1148
1097 $self->{vslider}->emit (mouse_wheel => $ev); 1149 $self->{vslider}->emit (mouse_wheel => $ev);
1098 1150
1099 1 1151 1
1100} 1152}
1101 1153
1102sub update_slider { 1154sub invoke_button_down {
1103 my ($self) = @_; 1155 my ($self, $ev, $x, $y) = @_;
1104 1156
1105 my $child = ($self->{vp} or return)->child; 1157 if ($ev->{button} == 2) {
1158 $self->grab_focus;
1106 1159
1107 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1160 my $ox = $self->{vp}{view_x} + $ev->{x};
1108 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); 1161 my $oy = $self->{vp}{view_y} + $ev->{y};
1109 #$self->{hslider}->set_visibility ($w1 != $w2); 1162
1163 $self->{motion} = sub {
1164 my ($ev, $x, $y) = @_;
1110 1165
1111 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1166 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1112 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); 1167 $self->update;
1113 #$self->{vslider}->set_visibility ($h1 != $h2); 1168 };
1114}
1115 1169
1116sub update { 1170 return 1;
1117 my ($self) = @_; 1171 }
1118 1172
1119 $self->SUPER::update; 1173 0
1120 $self->update_slider; 1174}
1175
1176sub invoke_button_up {
1177 my ($self, $ev, $x, $y) = @_;
1178
1179 if (delete $self->{motion}) {
1180 return 1;
1181 }
1182
1183 0
1184}
1185
1186sub invoke_mouse_motion {
1187 my ($self, $ev, $x, $y) = @_;
1188
1189 if ($self->{motion}) {
1190 $self->{motion}->($ev, $x, $y);
1191 return 1;
1192 }
1193
1194 0
1121} 1195}
1122 1196
1123sub invoke_size_allocate { 1197sub invoke_size_allocate {
1124 my ($self, $w, $h) = @_; 1198 my ($self, $w, $h) = @_;
1125 1199
1502 1576
1503sub new { 1577sub new {
1504 my $class = shift; 1578 my $class = shift;
1505 1579
1506 $class->SUPER::new ( 1580 $class->SUPER::new (
1581 children => [],
1507 col_expand => [], 1582 col_expand => [],
1508 row_expand => [], 1583 row_expand => [],
1509 @_, 1584 @_,
1510 ) 1585 )
1511} 1586}
1512 1587
1513sub children { 1588sub children {
1514 grep $_, map @$_, grep $_, @{ $_[0]{children} } 1589 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1515} 1590}
1516 1591
1592# TODO: store row/col info in child widget and use standard add/del
1517sub add { 1593sub add {
1518 my ($self) = shift; 1594 my ($self) = shift;
1519 1595
1520 while (@_) { 1596 while (@_) {
1521 my ($x, $y, $child) = splice @_, 0, 3, (); 1597 my ($x, $y, $child) = splice @_, 0, 3, ();
1522 $child->set_parent ($self); 1598 $child->set_parent ($self);
1523 $self->{children}[$y][$x] = $child; 1599 $self->{children}[$y][$x] = $child;
1524 } 1600 }
1525 1601
1526 $self->{force_realloc} = 1; 1602 $self->{force_realloc} = 1;
1527 $self->{force_size_alloc} = 1; 1603 $self->{force_size_alloc} = 1;
1528 $self->realloc; 1604 $self->realloc;
1529} 1605}
1530 1606
1531sub remove { 1607sub remove {
1532 my ($self, $child) = @_; 1608 my ($self, $child) = @_;
1533 1609
1534 # TODO: not yet implemented 1610 for (@{ $self->{children} }) {
1611 for (@{ $_ || [] }) {
1612 $_ = undef if $_ == $child;
1613 }
1614 }
1535} 1615}
1536 1616
1537# TODO: move to container class maybe? send children a signal on removal? 1617# TODO: move to container class maybe? send children a signal on removal?
1538sub clear { 1618sub clear {
1539 my ($self) = @_; 1619 my ($self) = @_;
1652 my ($self) = @_; 1732 my ($self) = @_;
1653 1733
1654 for (grep $_, @{$self->{children}}) { 1734 for (grep $_, @{$self->{children}}) {
1655 $_->draw for grep $_, @$_; 1735 $_->draw for grep $_, @$_;
1656 } 1736 }
1737}
1738
1739#############################################################################
1740
1741package CFPlus::UI::Fixed;
1742
1743use List::Util qw(min max);
1744
1745our @ISA = CFPlus::UI::Container::;
1746
1747sub add {
1748 my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_;
1749
1750 $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h];
1751 $self->SUPER::add ($child);
1752}
1753
1754sub _scale($$$) {
1755 my ($mode, $val, $max) = @_;
1756
1757 $mode eq "abs" ? $val
1758 : $mode eq "rel" ? $val * $max
1759 : 0
1760}
1761
1762sub size_request {
1763 my ($self) = @_;
1764
1765 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1766
1767 # determine overall size by querying abs widgets
1768 for my $child ($self->visible_children) {
1769 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1770
1771 if ($pos eq "abs") {
1772 $w = _scale $size, $w, $child->{req_w};
1773 $h = _scale $size, $h, $child->{req_h};
1774
1775 $x1 = min $x1, $x; $x2 = max $x2, $x + $w;
1776 $y1 = min $y1, $y; $y2 = max $y2, $y + $h;
1777 }
1778 }
1779
1780 my $W = $x2 - $x1;
1781 my $H = $y2 - $y1;
1782
1783 # now layout remaining widgets
1784 for my $child ($self->visible_children) {
1785 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1786
1787 if ($pos ne "abs") {
1788 $x = _scale $pos, $x, $W;
1789 $y = _scale $pos, $x, $H;
1790 $w = _scale $size, $w, $child->{req_w};
1791 $h = _scale $size, $h, $child->{req_h};
1792
1793 $x1 = min $x1, $x; $x2 = max $x2, $x + $w;
1794 $y1 = min $y1, $y; $y2 = max $y2, $y + $h;
1795 }
1796 }
1797
1798 my $W = $x2 - $x1;
1799 my $H = $y2 - $y1;
1800
1801 ($W, $H)
1802}
1803
1804sub invoke_size_allocate {
1805 my ($self, $W, $H) = @_;
1806
1807 for my $child ($self->visible_children) {
1808 my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} };
1809
1810 $x = _scale $pos, $x, $W;
1811 $y = _scale $pos, $x, $H;
1812 $w = _scale $size, $w, $child->{req_w};
1813 $h = _scale $size, $h, $child->{req_h};
1814
1815 $child->configure ($x, $y, $w, $h);
1816 }
1817
1818 1
1657} 1819}
1658 1820
1659############################################################################# 1821#############################################################################
1660 1822
1661package CFPlus::UI::Box; 1823package CFPlus::UI::Box;
3321 3483
3322sub new { 3484sub new {
3323 my $class = shift; 3485 my $class = shift;
3324 3486
3325 my $self = $class->SUPER::new ( 3487 my $self = $class->SUPER::new (
3488 size_w => 32,
3489 size_h => 8,
3326 aspect => 1, 3490 aspect => 1,
3327 can_events => 0, 3491 can_events => 0,
3328 @_, 3492 @_,
3329 ); 3493 );
3330 3494
3331 if ($self->{anim} && $self->{animspeed}) { 3495 if ($self->{anim} && $self->{animspeed}) {
3332 CFPlus::weaken (my $widget = $self); 3496 CFPlus::weaken (my $widget = $self);
3333 3497
3498 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3499 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed};
3334 $self->{timer} = Event->timer ( 3500 $self->{timer} = Event->timer (
3335 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3336 hard => 1, 3501 parked => 1,
3337 interval => $self->{animspeed},
3338 cb => sub { 3502 cb => sub {
3339 delete $self->{wait_face}; 3503 return unless $::CONN;
3504
3505 if (!$widget) {#d#
3506 warn "animation widget timer active but widgetd estroyed\n";#d#
3507 warn $_[0]->w->active;#d#
3508 }#d#
3509
3340 ++$widget->{frame}; 3510 ++$widget->{frame};
3511 $widget->update_face;
3341 $widget->update; 3512 $widget->update;
3513
3514 $widget->update_timer;
3342 }, 3515 },
3343 ); 3516 );
3517
3518 $self->update_face;
3519 $self->update_timer;
3344 } 3520 }
3345 3521
3346 $self 3522 $self
3347} 3523}
3348 3524
3525sub update_timer {
3526 my ($self) = @_;
3527
3528 return unless $self->{timer};
3529
3530 if ($self->{visible}) {
3531 $self->{timer}->at (
3532 $self->{anim_start}
3533 + $self->{animspeed}
3534 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3535 );
3536 $self->{timer}->start;
3537 } else {
3538 $self->{timer}->stop;
3539 }
3540}
3541
3542sub update_face {
3543 my ($self) = @_;
3544
3545 return unless $::CONN;
3546
3547 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3548 if ($anim && @$anim) {
3549 delete $self->{wait_face};
3550 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3551 }
3552 }
3553}
3554
3349sub size_request { 3555sub size_request {
3350 (32, 8) 3556 my ($self) = @_;
3557
3558 if ($::CONN) {
3559 if (my $faceid = $::CONN->{faceid}[$self->{face}]) {
3560 if (my $tex = $::CONN->{texture}[$faceid]) {
3561 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3562 } else {
3563 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3564 $self->realloc;
3565 });
3566 }
3567 }
3568 }
3569
3570 ($self->{size_w} || 8, $self->{size_h} || 8)
3351} 3571}
3352 3572
3353sub update { 3573sub update {
3354 my ($self) = @_; 3574 my ($self) = @_;
3355 3575
3356 return unless $self->{visible}; 3576 return unless $self->{visible};
3357 3577
3358 $self->SUPER::update; 3578 $self->SUPER::update;
3359} 3579}
3360 3580
3581sub invoke_visibility_change {
3582 my ($self) = @_;
3583
3584 $self->update_timer;
3585
3586 0
3587}
3588
3361sub _draw { 3589sub _draw {
3362 my ($self) = @_; 3590 my ($self) = @_;
3363 3591
3364 return unless $::CONN; 3592 return unless $::CONN;
3365 3593
3366 $self->SUPER::_draw; 3594 $self->SUPER::_draw;
3367 3595
3368 my $face;
3369
3370 if ($self->{frame}) {
3371 my $anim = $::CONN->{anim}[$self->{anim}];
3372
3373 $face = $anim->[ $self->{frame} % @$anim ]
3374 if $anim && @$anim;
3375 }
3376
3377 my $faceid = $::CONN->{faceid}[$face || $self->{face}] 3596 my $faceid = $::CONN->{faceid}[$self->{face}]
3378 or return; 3597 or return;
3379 3598
3380 my $tex = $::CONN->{texture}[$faceid]; 3599 my $tex = $::CONN->{texture}[$faceid];
3381 3600
3382 if ($tex) { 3601 if ($tex) {
3383 glEnable GL_TEXTURE_2D; 3602 glEnable GL_TEXTURE_2D;
3384 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3603 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3385 glColor 0, 0, 0, 1; 3604 glColor 0, 0, 0, 1;
3386 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3605 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3387 glDisable GL_TEXTURE_2D; 3606 glDisable GL_TEXTURE_2D;
3388 } else {
3389 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3390 $self->update;
3391 });
3392 } 3607 }
3393} 3608}
3394 3609
3395sub destroy { 3610sub destroy {
3396 my ($self) = @_; 3611 my ($self) = @_;
3397 3612
3398 $self->{timer}->cancel 3613 (delete $self->{timer})->cancel
3399 if $self->{timer}; 3614 if $self->{timer};
3400 3615
3401 $self->SUPER::destroy; 3616 $self->SUPER::destroy;
3402} 3617}
3403 3618

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines