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.381 by root, Thu Jul 19 13:45: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) = @_;
1072 row_expand => [1, 0], 1080 row_expand => [1, 0],
1073 %arg, 1081 %arg,
1074 ); 1082 );
1075 1083
1076 $self->{vp} = new CFPlus::UI::ViewPort 1084 $self->{vp} = new CFPlus::UI::ViewPort
1077 expand => 1, 1085 expand => 1,
1078 scroll_x => $self->{scroll_x}, 1086 scroll_x => $self->{scroll_x},
1079 scroll_y => $self->{scroll_y}, 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 },
1080 ; 1096 ;
1081 1097
1082 $self->SUPER::add (0, 0, $self->{vp}); 1098 $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 1099
1086 $self->add ($child) if $child; 1100 $self->add ($child) if $child;
1087 1101
1088 $self 1102 $self
1089} 1103}
1094 my ($self, $widget) = @_; 1108 my ($self, $widget) = @_;
1095 1109
1096 $self->{vp}->add ($self->{child} = $widget); 1110 $self->{vp}->add ($self->{child} = $widget);
1097} 1111}
1098 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
1099sub invoke_mouse_wheel { 1144sub invoke_mouse_wheel {
1100 my ($self, $ev) = @_; 1145 my ($self, $ev) = @_;
1101 1146
1102 return 0 unless $ev->{dy}; # only vertical movements 1147 return 0 unless $ev->{dy}; # only vertical movements for now
1103 1148
1104 $self->{vslider}->emit (mouse_wheel => $ev); 1149 $self->{vslider}->emit (mouse_wheel => $ev);
1105 1150
1106 1 1151 1
1107} 1152}
1108 1153
1109sub update_slider { 1154sub invoke_button_down {
1110 my ($self) = @_; 1155 my ($self, $ev, $x, $y) = @_;
1111 1156
1112 my $child = ($self->{vp} or return)->child; 1157 if ($ev->{button} == 2) {
1158 $self->grab_focus;
1113 1159
1114 my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); 1160 my $ox = $self->{vp}{view_x} + $ev->{x};
1115 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); 1161 my $oy = $self->{vp}{view_y} + $ev->{y};
1116 #$self->{hslider}->set_visibility ($w1 != $w2); 1162
1163 $self->{motion} = sub {
1164 my ($ev, $x, $y) = @_;
1117 1165
1118 my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); 1166 $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y});
1119 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); 1167 $self->update;
1120 #$self->{vslider}->set_visibility ($h1 != $h2); 1168 };
1121}
1122 1169
1123sub update { 1170 return 1;
1124 my ($self) = @_; 1171 }
1125 1172
1126 $self->SUPER::update; 1173 0
1127 $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
1128} 1195}
1129 1196
1130sub invoke_size_allocate { 1197sub invoke_size_allocate {
1131 my ($self, $w, $h) = @_; 1198 my ($self, $w, $h) = @_;
1132 1199
1509 1576
1510sub new { 1577sub new {
1511 my $class = shift; 1578 my $class = shift;
1512 1579
1513 $class->SUPER::new ( 1580 $class->SUPER::new (
1581 children => [],
1514 col_expand => [], 1582 col_expand => [],
1515 row_expand => [], 1583 row_expand => [],
1516 @_, 1584 @_,
1517 ) 1585 )
1518} 1586}
1519 1587
1520sub children { 1588sub children {
1521 grep $_, map @$_, grep $_, @{ $_[0]{children} } 1589 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1522} 1590}
1523 1591
1592# TODO: store row/col info in child widget and use standard add/del
1524sub add { 1593sub add {
1525 my ($self) = shift; 1594 my ($self) = shift;
1526 1595
1527 while (@_) { 1596 while (@_) {
1528 my ($x, $y, $child) = splice @_, 0, 3, (); 1597 my ($x, $y, $child) = splice @_, 0, 3, ();
1529 $child->set_parent ($self); 1598 $child->set_parent ($self);
1530 $self->{children}[$y][$x] = $child; 1599 $self->{children}[$y][$x] = $child;
1531 } 1600 }
1532 1601
1533 $self->{force_realloc} = 1; 1602 $self->{force_realloc} = 1;
1534 $self->{force_size_alloc} = 1; 1603 $self->{force_size_alloc} = 1;
1535 $self->realloc; 1604 $self->realloc;
1536} 1605}
1537 1606
1538sub remove { 1607sub remove {
1539 my ($self, $child) = @_; 1608 my ($self, $child) = @_;
1540 1609
1541 # TODO: not yet implemented 1610 for (@{ $self->{children} }) {
1611 for (@{ $_ || [] }) {
1612 $_ = undef if $_ == $child;
1613 }
1614 }
1542} 1615}
1543 1616
1544# 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?
1545sub clear { 1618sub clear {
1546 my ($self) = @_; 1619 my ($self) = @_;
1659 my ($self) = @_; 1732 my ($self) = @_;
1660 1733
1661 for (grep $_, @{$self->{children}}) { 1734 for (grep $_, @{$self->{children}}) {
1662 $_->draw for grep $_, @$_; 1735 $_->draw for grep $_, @$_;
1663 } 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
1664} 1819}
1665 1820
1666############################################################################# 1821#############################################################################
1667 1822
1668package CFPlus::UI::Box; 1823package CFPlus::UI::Box;
2226 2381
2227use CFPlus::OpenGL; 2382use CFPlus::OpenGL;
2228 2383
2229my @tex = 2384my @tex =
2230 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2385 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2231 qw(b1_button_active.png); 2386 qw(b1_button_inactive.png b1_button_active.png);
2232 2387
2233sub new { 2388sub new {
2234 my $class = shift; 2389 my $class = shift;
2235 2390
2236 $class->SUPER::new ( 2391 $class->SUPER::new (
2237 padding_x => 4, 2392 padding_x => 4,
2238 padding_y => 4, 2393 padding_y => 4,
2239 fg => [1, 1, 1], 2394 fg => [1.0, 1.0, 1.0],
2240 active_fg => [0, 0, 1], 2395 active_fg => [0.8, 0.8, 0.8],
2241 can_hover => 1, 2396 can_hover => 1,
2242 align => 0, 2397 align => 0,
2243 valign => 0, 2398 valign => 0,
2244 can_events => 1, 2399 can_events => 1,
2245 @_ 2400 @_
2263 2418
2264 glEnable GL_TEXTURE_2D; 2419 glEnable GL_TEXTURE_2D;
2265 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2420 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2266 glColor 0, 0, 0, 1; 2421 glColor 0, 0, 0, 1;
2267 2422
2423 my $tex = $tex[$GRAB == $self];
2268 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2424 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2269 2425
2270 glDisable GL_TEXTURE_2D; 2426 glDisable GL_TEXTURE_2D;
2271 2427
2272 $self->SUPER::_draw; 2428 $self->SUPER::_draw;
2273} 2429}
3328 3484
3329sub new { 3485sub new {
3330 my $class = shift; 3486 my $class = shift;
3331 3487
3332 my $self = $class->SUPER::new ( 3488 my $self = $class->SUPER::new (
3489 size_w => 32,
3490 size_h => 8,
3333 aspect => 1, 3491 aspect => 1,
3334 can_events => 0, 3492 can_events => 0,
3335 @_, 3493 @_,
3336 ); 3494 );
3337 3495
3338 if ($self->{anim} && $self->{animspeed}) { 3496 if ($self->{anim} && $self->{animspeed}) {
3339 CFPlus::weaken (my $widget = $self); 3497 CFPlus::weaken (my $widget = $self);
3340 3498
3499 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3500 $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed};
3341 $self->{timer} = Event->timer ( 3501 $self->{timer} = Event->timer (
3342 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3343 hard => 1, 3502 parked => 1,
3344 interval => $self->{animspeed},
3345 cb => sub { 3503 cb => sub {
3346 delete $self->{wait_face}; 3504 return unless $::CONN && $widget;
3505
3347 ++$widget->{frame}; 3506 ++$widget->{frame};
3507 $widget->update_face;
3348 $widget->update; 3508 $widget->update;
3509
3510 $widget->update_timer;
3349 }, 3511 },
3350 ); 3512 );
3513
3514 $self->update_face;
3515 $self->update_timer;
3351 } 3516 }
3352 3517
3353 $self 3518 $self
3354} 3519}
3355 3520
3521sub update_timer {
3522 my ($self) = @_;
3523
3524 return unless $self->{timer};
3525
3526 if ($self->{visible}) {
3527 $self->{timer}->at (
3528 $self->{anim_start}
3529 + $self->{animspeed}
3530 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3531 );
3532 $self->{timer}->start;
3533 } else {
3534 $self->{timer}->stop;
3535 }
3536}
3537
3538sub update_face {
3539 my ($self) = @_;
3540
3541 return unless $::CONN;
3542
3543 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3544 if ($anim && @$anim) {
3545 delete $self->{wait_face};
3546 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3547 }
3548 }
3549}
3550
3356sub size_request { 3551sub size_request {
3357 (32, 8) 3552 my ($self) = @_;
3553
3554 if ($::CONN) {
3555 if (my $faceid = $::CONN->{faceid}[$self->{face}]) {
3556 if (my $tex = $::CONN->{texture}[$faceid]) {
3557 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3558 } else {
3559 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3560 $self->realloc;
3561 });
3562 }
3563 }
3564 }
3565
3566 ($self->{size_w} || 8, $self->{size_h} || 8)
3358} 3567}
3359 3568
3360sub update { 3569sub update {
3361 my ($self) = @_; 3570 my ($self) = @_;
3362 3571
3363 return unless $self->{visible}; 3572 return unless $self->{visible};
3364 3573
3365 $self->SUPER::update; 3574 $self->SUPER::update;
3366} 3575}
3367 3576
3577sub invoke_visibility_change {
3578 my ($self) = @_;
3579
3580 $self->update_timer;
3581
3582 0
3583}
3584
3368sub _draw { 3585sub _draw {
3369 my ($self) = @_; 3586 my ($self) = @_;
3370 3587
3371 return unless $::CONN; 3588 return unless $::CONN;
3372 3589
3373 $self->SUPER::_draw; 3590 $self->SUPER::_draw;
3374 3591
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}] 3592 my $faceid = $::CONN->{faceid}[$self->{face}]
3385 or return; 3593 or return;
3386 3594
3387 my $tex = $::CONN->{texture}[$faceid]; 3595 my $tex = $::CONN->{texture}[$faceid];
3388 3596
3389 if ($tex) { 3597 if ($tex) {
3390 glEnable GL_TEXTURE_2D; 3598 glEnable GL_TEXTURE_2D;
3391 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3599 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3392 glColor 0, 0, 0, 1; 3600 glColor 0, 0, 0, 1;
3393 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3601 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3394 glDisable GL_TEXTURE_2D; 3602 glDisable GL_TEXTURE_2D;
3395 } else {
3396 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3397 $self->update;
3398 });
3399 } 3603 }
3400} 3604}
3401 3605
3402sub destroy { 3606sub destroy {
3403 my ($self) = @_; 3607 my ($self) = @_;
3404 3608
3405 $self->{timer}->cancel 3609 (delete $self->{timer})->cancel
3406 if $self->{timer}; 3610 if $self->{timer};
3407 3611
3408 $self->SUPER::destroy; 3612 $self->SUPER::destroy;
3409} 3613}
3410 3614
4054 while ($self->{post_alloc_hook}) { 4258 while ($self->{post_alloc_hook}) {
4055 $_->() 4259 $_->()
4056 for values %{delete $self->{post_alloc_hook}}; 4260 for values %{delete $self->{post_alloc_hook}};
4057 } 4261 }
4058 4262
4059
4060 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4263 glViewport 0, 0, $::WIDTH, $::HEIGHT;
4061 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4264 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
4062 glClear GL_COLOR_BUFFER_BIT; 4265 glClear GL_COLOR_BUFFER_BIT;
4063 4266
4064 glMatrixMode GL_PROJECTION; 4267 glMatrixMode GL_PROJECTION;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines