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.197 by root, Fri May 12 01:47:04 2006 UTC vs.
Revision 1.212 by elmex, Wed May 17 14:55:14 2006 UTC

11our ($FOCUS, $HOVER, $GRAB); # various widgets 11our ($FOCUS, $HOVER, $GRAB); # various widgets
12 12
13our $ROOT; 13our $ROOT;
14our $TOOLTIP; 14our $TOOLTIP;
15our $BUTTON_STATE; 15our $BUTTON_STATE;
16
17our %WIDGET; # all widgets, weak-referenced
16 18
17sub check_tooltip { 19sub check_tooltip {
18 if (!$GRAB) { 20 if (!$GRAB) {
19 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
20 if (length $widget->{tooltip}) { 22 if (length $widget->{tooltip}) {
29 $TOOLTIP->set_tooltip_from ($widget); 31 $TOOLTIP->set_tooltip_from ($widget);
30 $TOOLTIP->show; 32 $TOOLTIP->show;
31 33
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33 35
34 if ($x + $TOOLTIP->{w} > $::WIDTH) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
36 } 37 if $x + $TOOLTIP->{w} > $::WIDTH;
37 38
38 $TOOLTIP->move ($x, $y); 39 $TOOLTIP->move ($x, $y);
39 $TOOLTIP->check_size; 40 $TOOLTIP->check_size;
40 $TOOLTIP->update; 41 $TOOLTIP->update;
41 } 42 }
132 133
133 for (@$vals) { 134 for (@$vals) {
134 my $i = int $_ + $rem; 135 my $i = int $_ + $rem;
135 $rem += $_ - $i; 136 $rem += $_ - $i;
136 $_ = $i; 137 $_ = $i;
138 }
139}
140
141# call when resolution changes etc.
142sub rescale_widgets {
143 my ($sx, $sy) = @_;
144
145 for my $widget (values %WIDGET) {
146 if ($widget->{toplevel}) {
147 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
148 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
149 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
150 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
151 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
152 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
153 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
154 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
155 }
156
157 $widget->reconfigure;
137 } 158 }
138} 159}
139 160
140############################################################################# 161#############################################################################
141 162
160 if (/^connect_(.*)$/) { 181 if (/^connect_(.*)$/) {
161 $self->connect ($1 => delete $self->{$_}); 182 $self->connect ($1 => delete $self->{$_});
162 } 183 }
163 } 184 }
164 185
186 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
187
165 $self 188 $self
166} 189}
167 190
168sub destroy { 191sub destroy {
169 my ($self) = @_; 192 my ($self) = @_;
242 265
243sub size_allocate { 266sub size_allocate {
244 # nothing to be done 267 # nothing to be done
245} 268}
246 269
270sub reconfigure {
271 my ($self) = @_;
272
273 $self->check_size (1);
274 $self->update;
275}
276
247sub children { 277sub children {
248}
249
250# call when resolution changes etc.
251sub reconfigure {
252 my ($self) = @_;
253
254 $_->reconfigure
255 for $self->children;
256
257 $self->check_size;
258 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}];
259 $self->update;
260} 278}
261 279
262sub set_max_size { 280sub set_max_size {
263 my ($self, $w, $h) = @_; 281 my ($self, $w, $h) = @_;
264 282
265 delete $self->{max_w}; $self->{max_w} = $w if $w; 283 delete $self->{max_w}; $self->{max_w} = $w if $w;
266 delete $self->{max_h}; $self->{max_h} = $h if $h; 284 delete $self->{max_h}; $self->{max_h} = $h if $h;
285}
286
287sub set_tooltip {
288 my ($self, $tooltip) = @_;
289
290 $self->{tooltip} = $tooltip;
291
292 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
293 delete $CFClient::UI::TOOLTIP->{owner};
294 CFClient::UI::check_tooltip;
295 }
267} 296}
268 297
269# translate global coordinates to local coordinate system 298# translate global coordinates to local coordinate system
270sub coord2local { 299sub coord2local {
271 my ($self, $x, $y) = @_; 300 my ($self, $x, $y) = @_;
357 glVertex $self->{w}, 0; 386 glVertex $self->{w}, 0;
358 glVertex $self->{w}, $self->{h}; 387 glVertex $self->{w}, $self->{h};
359 glVertex 0 , $self->{h}; 388 glVertex 0 , $self->{h};
360 glEnd; 389 glEnd;
361 glPopMatrix; 390 glPopMatrix;
362 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 391 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
363 } 392 }
364} 393}
365 394
366sub _draw { 395sub _draw {
367 my ($self) = @_; 396 my ($self) = @_;
384sub set_parent { 413sub set_parent {
385 my ($self, $parent) = @_; 414 my ($self, $parent) = @_;
386 415
387 Scalar::Util::weaken ($self->{parent} = $parent); 416 Scalar::Util::weaken ($self->{parent} = $parent);
388 417
418 # TODO: req_w _does_change after ->reconfigure
389 $self->check_size 419 $self->check_size
390 unless exists $self->{req_w}; 420 unless exists $self->{req_w};
391} 421}
392 422
393sub check_size { 423sub check_size {
417} 447}
418 448
419sub DESTROY { 449sub DESTROY {
420 my ($self) = @_; 450 my ($self) = @_;
421 451
452 delete $WIDGET{$self+0};
422 #$self->deactivate; 453 #$self->deactivate;
423} 454}
424 455
425############################################################################# 456#############################################################################
426 457
435 my $class = shift; 466 my $class = shift;
436 467
437 # range [value, low, high, page] 468 # range [value, low, high, page]
438 469
439 $class->SUPER::new ( 470 $class->SUPER::new (
440 bg => [0, 0, 0, 0.2], 471 #bg => [0, 0, 0, 0.2],
441 active_bg => [1, 1, 1, 0.5], 472 #active_bg => [1, 1, 1, 0.5],
442 @_ 473 @_
443 ) 474 )
444} 475}
445 476
446sub _draw { 477sub _draw {
447 my ($self) = @_; 478 my ($self) = @_;
448 479
480 my $color = $FOCUS == $self && $self->{active_bg}
481 ? $self->{active_bg}
482 : $self->{bg};
483
484 if ($color && (@$color < 4 || $color->[3])) {
449 my ($w, $h) = @$self{qw(w h)}; 485 my ($w, $h) = @$self{qw(w h)};
450 486
451 glEnable GL_BLEND; 487 glEnable GL_BLEND;
452 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 488 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
453 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 489 glColor @$color;
454 490
455 glBegin GL_QUADS; 491 glBegin GL_QUADS;
456 glVertex 0 , 0; 492 glVertex 0 , 0;
457 glVertex 0 , $h; 493 glVertex 0 , $h;
458 glVertex $w, $h; 494 glVertex $w, $h;
459 glVertex $w, 0; 495 glVertex $w, 0;
460 glEnd; 496 glEnd;
461 497
462 glDisable GL_BLEND; 498 glDisable GL_BLEND;
499 }
463} 500}
464 501
465############################################################################# 502#############################################################################
466 503
467package CFClient::UI::Empty; 504package CFClient::UI::Empty;
628} 665}
629 666
630sub update { 667sub update {
631 my ($self) = @_; 668 my ($self) = @_;
632 669
633 $ROOT->on_refresh ($self => sub { $self->render_child }); 670 $ROOT->on_post_alloc ($self => sub { $self->render_child });
634 $self->SUPER::update; 671 $self->SUPER::update;
635} 672}
636 673
637sub size_allocate { 674sub size_allocate {
638 my ($self, $w, $h) = @_; 675 my ($self, $w, $h) = @_;
776 $self->add ($self->{slider}); 813 $self->add ($self->{slider});
777 814
778 $self 815 $self
779} 816}
780 817
781#TODO# update range on size_allocate depeneing on child 818#TODO# update range on size_allocate depending on child
782# update viewport offset on scroll 819# update viewport offset on scroll
783 820
784############################################################################# 821#############################################################################
785 822
786package CFClient::UI::Frame; 823package CFClient::UI::Frame;
787 824
788our @ISA = CFClient::UI::Bin::; 825our @ISA = CFClient::UI::Bin::;
789 826
790use CFClient::OpenGL; 827use CFClient::OpenGL;
828
829sub new {
830 my $class = shift;
831
832 $class->SUPER::new (
833 bg => undef,
834 @_,
835 )
836}
837
838sub _draw {
839 my ($self) = @_;
840
841 if ($self->{bg}) {
842 my ($w, $h) = @$self{qw(w h)};
843
844 glEnable GL_BLEND;
845 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
846 glColor @{ $self->{bg} };
847
848 glBegin GL_QUADS;
849 glVertex 0 , 0;
850 glVertex 0 , $h;
851 glVertex $w, $h;
852 glVertex $w, 0;
853 glEnd;
854
855 glDisable GL_BLEND;
856 }
857
858 $self->SUPER::_draw;
859}
791 860
792############################################################################# 861#############################################################################
793 862
794package CFClient::UI::FancyFrame; 863package CFClient::UI::FancyFrame;
795 864
808 877
809 my $self = $class->SUPER::new ( 878 my $self = $class->SUPER::new (
810 bg => [1, 1, 1, 1], 879 bg => [1, 1, 1, 1],
811 border_bg => [1, 1, 1, 1], 880 border_bg => [1, 1, 1, 1],
812 border => 0.6, 881 border => 0.6,
882 toplevel => 1,
813 can_events => 1, 883 can_events => 1,
814 @_ 884 @_
815 ); 885 );
816 886
817 $self->{title} &&= new CFClient::UI::Label 887 $self->{title} &&= new CFClient::UI::Label
842 my ($self, $w, $h) = @_; 912 my ($self, $w, $h) = @_;
843 913
844 $h -= List::Util::max 0, $self->border * 2; 914 $h -= List::Util::max 0, $self->border * 2;
845 $w -= List::Util::max 0, $self->border * 2; 915 $w -= List::Util::max 0, $self->border * 2;
846 916
847 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 917 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
848 if $self->{title}; 918 if $self->{title};
849 919
850 $self->child->configure ($self->border, $self->border, $w, $h); 920 $self->child->configure ($self->border, $self->border, $w, $h);
851} 921}
852 922
1213 1283
1214############################################################################# 1284#############################################################################
1215 1285
1216package CFClient::UI::Label; 1286package CFClient::UI::Label;
1217 1287
1218our @ISA = CFClient::UI::Base::; 1288our @ISA = CFClient::UI::DrawBG::;
1219 1289
1220use CFClient::OpenGL; 1290use CFClient::OpenGL;
1221 1291
1222sub new { 1292sub new {
1223 my ($class, %arg) = @_; 1293 my ($class, %arg) = @_;
1224 1294
1225 my $self = $class->SUPER::new ( 1295 my $self = $class->SUPER::new (
1226 fg => [1, 1, 1], 1296 fg => [1, 1, 1],
1297 #bg => none
1298 #active_bg => none
1227 #font => default_font 1299 #font => default_font
1228 #text => initial text 1300 #text => initial text
1229 #markup => initial narkup 1301 #markup => initial narkup
1230 layout => (new CFClient::Layout), 1302 layout => (new CFClient::Layout),
1231 fontsize => 1, 1303 fontsize => 1,
1249 } 1321 }
1250 1322
1251 $self 1323 $self
1252} 1324}
1253 1325
1254sub escape { 1326sub escape($) {
1255 local $_ = $_[1]; 1327 local $_ = $_[0];
1256 1328
1257 s/&/&amp;/g; 1329 s/&/&amp;/g;
1258 s/>/&gt;/g; 1330 s/>/&gt;/g;
1259 s/</&lt;/g; 1331 s/</&lt;/g;
1260 1332
1261 $_[1] 1333 $_
1262} 1334}
1263 1335
1264sub update { 1336sub update {
1265 my ($self) = @_; 1337 my ($self) = @_;
1266 1338
1337 $self->check_size; 1409 $self->check_size;
1338} 1410}
1339 1411
1340sub _draw { 1412sub _draw {
1341 my ($self) = @_; 1413 my ($self) = @_;
1414
1415 $self->SUPER::_draw; # draw background, if applicable
1342 1416
1343 my $tex = $self->{texture} ||= do { 1417 my $tex = $self->{texture} ||= do {
1344 $self->{layout}->set_foreground (@{$self->{fg}}); 1418 $self->{layout}->set_foreground (@{$self->{fg}});
1345 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1419 $self->{layout}->set_font ($self->{font}) if $self->{font};
1346 $self->{layout}->set_width ($self->{w}); 1420 $self->{layout}->set_width ($self->{w});
1347 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1421 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1348 1422
1349 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1423 my $tex = new_from_layout CFClient::Texture $self->{layout};
1350 1424
1351 $self->{ox} = int $self->{align} < 0 ? $self->{padding} 1425 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1352 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1426 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1353 : ($self->{w} - $tex->{w}) * 0.5; 1427 : ($self->{w} - $tex->{w}) * 0.5);
1354 1428
1355 $self->{oy} = int $self->{valign} < 0 ? $self->{padding} 1429 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1356 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1430 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1357 : ($self->{h} - $tex->{h}) * 0.5; 1431 : ($self->{h} - $tex->{h}) * 0.5);
1358 1432
1359 $tex 1433 $tex
1360 }; 1434 };
1361 1435
1362 glEnable GL_TEXTURE_2D; 1436 glEnable GL_TEXTURE_2D;
1417sub set_text { 1491sub set_text {
1418 my ($self, $text) = @_; 1492 my ($self, $text) = @_;
1419 1493
1420 $self->{cursor} = length $text; 1494 $self->{cursor} = length $text;
1421 $self->_set_text ($text); 1495 $self->_set_text ($text);
1496 $self->update;
1422 $self->check_size; 1497 $self->check_size;
1423 $self->update;
1424} 1498}
1425 1499
1426sub get_text { 1500sub get_text {
1427 $_[0]{text} 1501 $_[0]{text}
1428} 1502}
1448 my $sym = $ev->{sym}; 1522 my $sym = $ev->{sym};
1449 my $uni = $ev->{unicode}; 1523 my $uni = $ev->{unicode};
1450 1524
1451 my $text = $self->get_text; 1525 my $text = $self->get_text;
1452 1526
1453 if ($sym == 8) { 1527 if ($uni == 8) {
1454 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1528 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1455 } elsif ($sym == 127) { 1529 } elsif ($uni == 127) {
1456 substr $text, $self->{cursor}, 1, ""; 1530 substr $text, $self->{cursor}, 1, "";
1457 } elsif ($sym == CFClient::SDLK_LEFT) { 1531 } elsif ($sym == CFClient::SDLK_LEFT) {
1458 --$self->{cursor} if $self->{cursor}; 1532 --$self->{cursor} if $self->{cursor};
1459 } elsif ($sym == CFClient::SDLK_RIGHT) { 1533 } elsif ($sym == CFClient::SDLK_RIGHT) {
1460 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1534 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1461 } elsif ($sym == CFClient::SDLK_HOME) { 1535 } elsif ($sym == CFClient::SDLK_HOME) {
1462 $self->{cursor} = 0; 1536 $self->{cursor} = 0;
1463 } elsif ($sym == CFClient::SDLK_END) { 1537 } elsif ($sym == CFClient::SDLK_END) {
1464 $self->{cursor} = length $text; 1538 $self->{cursor} = length $text;
1465 } elsif ($sym == 27) { 1539 } elsif ($uni == 27) {
1466 $self->emit ('escape'); 1540 $self->emit ('escape');
1467 } elsif ($uni) { 1541 } elsif ($uni) {
1468 substr $text, $self->{cursor}++, 0, chr $uni; 1542 substr $text, $self->{cursor}++, 0, chr $uni;
1469 } 1543 }
1470 1544
1471 $self->_set_text ($text); 1545 $self->_set_text ($text);
1472 $self->update; 1546 $self->update;
1547 $self->check_size;
1473} 1548}
1474 1549
1475sub focus_in { 1550sub focus_in {
1476 my ($self) = @_; 1551 my ($self) = @_;
1477 1552
1606 my $class = shift; 1681 my $class = shift;
1607 1682
1608 $class->SUPER::new ( 1683 $class->SUPER::new (
1609 padding => 4, 1684 padding => 4,
1610 fg => [1, 1, 1], 1685 fg => [1, 1, 1],
1611 bg => [1, 1, 1, 0.2],
1612 active_fg => [0, 0, 1], 1686 active_fg => [0, 0, 1],
1613 can_hover => 1, 1687 can_hover => 1,
1614 align => 0, 1688 align => 0,
1615 valign => 0, 1689 valign => 0,
1616 can_events => 1, 1690 can_events => 1,
1664 1738
1665 $class->SUPER::new ( 1739 $class->SUPER::new (
1666 padding => 2, 1740 padding => 2,
1667 fg => [1, 1, 1], 1741 fg => [1, 1, 1],
1668 active_fg => [1, 1, 0], 1742 active_fg => [1, 1, 0],
1743 bg => [0, 0, 0, 0.2],
1744 active_bg => [1, 1, 1, 0.5],
1669 state => 0, 1745 state => 0,
1670 can_hover => 1, 1746 can_hover => 1,
1671 @_ 1747 @_
1672 ) 1748 )
1673} 1749}
1957 qw(s1_slider.png s1_slider_bg.png); 2033 qw(s1_slider.png s1_slider_bg.png);
1958 2034
1959sub new { 2035sub new {
1960 my $class = shift; 2036 my $class = shift;
1961 2037
1962 # range [value, low, high, page] 2038 # range [value, low, high, page, unit]
1963 2039
1964 # TODO: 0-width page 2040 # TODO: 0-width page
1965 # TODO: req_w/h are wrong with vertical 2041 # TODO: req_w/h are wrong with vertical
1966 # TODO: calculations are off 2042 # TODO: calculations are off
1967 my $self = $class->SUPER::new ( 2043 my $self = $class->SUPER::new (
1968 fg => [1, 1, 1], 2044 fg => [1, 1, 1],
1969 active_fg => [0, 0, 0], 2045 active_fg => [0, 0, 0],
2046 bg => [0, 0, 0, 0.2],
2047 active_bg => [1, 1, 1, 0.5],
1970 range => [0, 0, 100, 10], 2048 range => [0, 0, 100, 10],
1971 req_w => $::WIDTH / 80, 2049 req_w => $::WIDTH / 80,
1972 req_h => $::WIDTH / 80, 2050 req_h => $::WIDTH / 80,
1973 vertical => 0, 2051 vertical => 0,
1974 can_hover => 1, 2052 can_hover => 1,
1975 inner_pad => 5, 2053 inner_pad => .05,
1976 @_ 2054 @_
1977 ); 2055 );
1978 2056
2057 $self->set_value ($self->{range}[0]);
2058 $self->update;
2059
1979 $self 2060 $self
2061}
2062
2063sub set_value {
2064 my ($self, $value) = @_;
2065
2066 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2067
2068 $hi = $lo + 1 if $hi <= $lo;
2069
2070 $value = $lo if $value < $lo;
2071 $value = $hi if $value > $hi;
2072
2073 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2074 if $unit;
2075
2076 $page = $hi - $lo if $page > $hi - $lo;
2077
2078 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2079
2080 if ($value != $old_value) {
2081 $self->emit (changed => $value);
2082 $self->update;
2083 }
1980} 2084}
1981 2085
1982sub size_request { 2086sub size_request {
1983 my ($self) = @_; 2087 my ($self) = @_;
1984 2088
1997 2101
1998sub mouse_motion { 2102sub mouse_motion {
1999 my ($self, $ev, $x, $y) = @_; 2103 my ($self, $ev, $x, $y) = @_;
2000 2104
2001 if ($GRAB == $self) { 2105 if ($GRAB == $self) {
2106 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2107
2108 my (undef, $lo, $hi, $page) = @{$self->{range}};
2109
2110 $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad};
2111
2112 $self->set_value ($x * ($hi - $lo) + $lo);
2113 }
2114}
2115
2116sub update {
2117 my ($self) = @_;
2118
2119 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2120 $self->set_value ($self->{range}[0]);
2121
2002 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2122 my ($value, $lo, $hi, $page) = @{$self->{range}};
2003 2123
2004 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2124 my $inner_w = 1 - 2 * $self->{inner_pad};
2005 2125
2006 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2126 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
2007 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2008 2127
2009 $x -= $inner_pad_px; # substract the padding 2128 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
2010 $x = $x * ($hi - $lo) / $inner_w + $lo; 2129 $value = $self->{scale} * ($value - $lo);
2011 $x = $lo if $x < $lo;
2012 $x = $hi - $page if $x > $hi - $page;
2013 $self->{range}[0] = $x;
2014 2130
2015 $self->emit (changed => $x); 2131 $value = $self->{inner_pad} + ($value - $page * 0.5);
2016 $self->update; 2132
2133 $value = 0 if $value < 0;
2134 $page = 1 - $value if $value + $page > 1;
2135
2136 $self->{knob_x} = $value;
2137 $self->{knob_w} = $page;
2017 } 2138 });
2018}
2019 2139
2020# the inner_* stuff is for generating a padding for the slider handle, 2140 $self->SUPER::update;
2021# so that the handle doesn't leave the texture. This calculation isn't 100%
2022# correct propably, but it does the job for now
2023sub _calc_inner_pad_px {
2024 my ($self, $w) = @_;
2025 ($w / 100) * $self->{inner_pad} # % to pixels
2026} 2141}
2027 2142
2028sub _draw { 2143sub _draw {
2029 my ($self) = @_; 2144 my ($self) = @_;
2030 2145
2031 $self->SUPER::_draw (); 2146 $self->SUPER::_draw ();
2032 2147
2033 my ($w, $h) = @$self{qw(w h)}; 2148 glScale $self->{w}, $self->{h};
2034 2149
2035 if ($self->{vertical}) { 2150 if ($self->{vertical}) {
2036 # draw a vertical slider like a rotated horizontal slider 2151 # draw a vertical slider like a rotated horizontal slider
2037 2152
2038 glRotate 90, 0, 0, 1; 2153 glRotate 90, 0, 0, 1;
2039 glTranslate 0, -$self->{w}, 0; 2154 glTranslate 0, 1, 0;
2040
2041 ($w, $h) = ($h, $w);
2042 } 2155 }
2043 2156
2044 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2157 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2045 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2158 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2046 2159
2047 my ($value, $lo, $hi, $page) = @{$self->{range}};
2048
2049 $hi = $value + 1 if $lo == $hi;
2050
2051 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2052 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2053
2054 $page = int $page * $inner_w / ($hi - $lo);
2055 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2056
2057 $w -= $page;
2058 $page &= ~1;
2059 glTranslate $page * 0.5, 0, 0;
2060 $page ||= 2;
2061
2062 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2063 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2064
2065 glEnable GL_TEXTURE_2D; 2160 glEnable GL_TEXTURE_2D;
2066 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2161 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2067 2162
2068 # draw background 2163 # draw background
2069 $tex[1]->draw_quad_alpha (0, 0, $w, $h); 2164 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2070 2165
2071 # draw handle 2166 # draw handle
2072 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h); 2167 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2073 2168
2074 glDisable GL_TEXTURE_2D; 2169 glDisable GL_TEXTURE_2D;
2075} 2170}
2076 2171
2077############################################################################# 2172#############################################################################
2165 2260
2166 return unless $self->{h} > 0; 2261 return unless $self->{h} > 0;
2167 2262
2168 delete $self->{texture}; 2263 delete $self->{texture};
2169 2264
2170 $ROOT->on_refresh ($self, sub { 2265 $ROOT->on_post_alloc ($self, sub {
2171 if (delete $self->{need_reflow}) { 2266 if (delete $self->{need_reflow}) {
2172 my $height = 0; 2267 my $height = 0;
2173 2268
2174 $height += $_->[0] = $self->text_height ($_->[2]) 2269 $height += $_->[0] = $self->text_height ($_->[2])
2175 for @{$self->{par}}; 2270 for @{$self->{par}};
2423 2518
2424package CFClient::UI::InventoryItem; 2519package CFClient::UI::InventoryItem;
2425 2520
2426our @ISA = CFClient::UI::HBox::; 2521our @ISA = CFClient::UI::HBox::;
2427 2522
2523sub _item_to_desc {
2524 my ($item) = @_;
2525
2526 my $desc =
2527 $item->{nrof} < 2
2528 ? $item->{name}
2529 : "$item->{nrof} $item->{name_pl}";
2530
2531 $item->{flags} & Crossfire::Protocol::F_OPEN
2532 and $desc .= " (open)";
2533 $item->{flags} & Crossfire::Protocol::F_APPLIED
2534 and $desc .= " (applied)";
2535 $item->{flags} & Crossfire::Protocol::F_UNPAID
2536 and $desc .= " (unpaid)";
2537 $item->{flags} & Crossfire::Protocol::F_MAGIC
2538 and $desc .= " (magic)";
2539 $item->{flags} & Crossfire::Protocol::F_CURSED
2540 and $desc .= " (cursed)";
2541 $item->{flags} & Crossfire::Protocol::F_DAMNED
2542 and $desc .= " (damned)";
2543 $item->{flags} & Crossfire::Protocol::F_LOCKED
2544 and $desc .= " *";
2545
2546 $desc
2547}
2548
2428sub new { 2549sub new {
2429 my $class = shift; 2550 my $class = shift;
2430 2551
2431 my %args = @_; 2552 my %args = @_;
2432 2553
2433 my $item = delete $args{item}; 2554 my $item = delete $args{item};
2434 2555
2435 my $desc = $item->{nrof} < 2 2556 my $desc = _item_to_desc ($item);
2436 ? $item->{name}
2437 : "$item->{nrof} $item->{name_pl}";
2438
2439 2557
2440 my $self = $class->SUPER::new ( 2558 my $self = $class->SUPER::new (
2441 can_hover => 1, 2559 can_hover => 1,
2442 can_events => 1, 2560 can_events => 1,
2443 tooltip => (CFClient::UI::Label->escape ($desc) 2561 tooltip => ((CFClient::UI::Label::escape $desc)
2444 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2562 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2445 connect_button_down => sub { 2563 connect_button_down => sub {
2446 my ($self, $ev, $x, $y) = @_; 2564 my ($self, $ev, $x, $y) = @_;
2447 2565
2448 # todo: maybe put examine on 1? but should just be a tooltip :( 2566 # todo: maybe put examine on 1? but should just be a tooltip :(
2449 if ($ev->{button} == 1) { 2567 if ($ev->{button} == 1) {
2568 my $targ = $::CONN->{player}{tag};
2569
2570 if ($item->{container} == $::CONN->{player}{tag}) {
2571 $targ = $main::OPENCONT;
2572 }
2573
2450 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2574 $::CONN->send ("move $targ $item->{tag} 0");
2451 } elsif ($ev->{button} == 2) { 2575 } elsif ($ev->{button} == 2) {
2452 $::CONN->send ("apply $item->{tag}"); 2576 $::CONN->send ("apply $item->{tag}");
2453 } elsif ($ev->{button} == 3) { 2577 } elsif ($ev->{button} == 3) {
2454 CFClient::UI::Menu->new ( 2578 my @menu_items = (
2455 items => [
2456 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2579 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2580 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2581 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2582 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2457 [ 2583 [
2458 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", 2584 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2459 sub { $::CONN->send ("lock $item->{tag}") }, 2585 sub { $::CONN->send ("lock $item->{tag}") },
2460 ],
2461 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2462 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2463 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2464 ], 2586 ],
2465 )->popup ($ev); 2587 );
2588
2589 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2466 } 2590 }
2467 2591
2468 1 2592 1
2469 }, 2593 },
2470 %args 2594 %args
2471 ); 2595 );
2596
2472 2597
2473 $self->add (new CFClient::UI::Face 2598 $self->add (new CFClient::UI::Face
2474 can_events => 0, 2599 can_events => 0,
2475 face => $item->{face}, 2600 face => $item->{face},
2476 anim => $item->{anim}, 2601 anim => $item->{anim},
2477 animspeed => $item->{animspeed}, 2602 animspeed => $item->{animspeed},
2478 ); 2603 );
2479 2604
2480 $self->add (new CFClient::UI::Label 2605 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2481 can_events => 0, 2606
2482 text => $desc, 2607 $self->{item} = $item;
2483 ); 2608
2609 $self->update_item;
2484 2610
2485 $self 2611 $self
2612}
2613
2614sub update_item {
2615 my ($self) = @_;
2616
2617 my $desc = _item_to_desc ($self->{item});
2618
2619 $self->{name_lbl}->set_text ($desc);
2486} 2620}
2487 2621
2488############################################################################# 2622#############################################################################
2489 2623
2490package CFClient::UI::Inventory; 2624package CFClient::UI::Inventory;
2506 my ($self, $items) = @_; 2640 my ($self, $items) = @_;
2507 2641
2508 $self->{scrolled}->clear; 2642 $self->{scrolled}->clear;
2509 return unless $items; 2643 return unless $items;
2510 2644
2511 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2645 my @items = sort {
2646 ($a->{type} <=> $b->{type})
2647 or ($a->{name} cmp $b->{name})
2648 } @$items;
2512 2649
2513 $self->{real_items} = \@items; 2650 $self->{real_items} = \@items;
2514 2651
2515 for my $item (@items) { 2652 for my $item (@items) {
2516 my $desc = $item->{nrof} < 2
2517 ? $item->{name}
2518 : "$item->{nrof} $item->{name_pl}";
2519
2520 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2653 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2654 $item->update_item ();
2521 } 2655 }
2656
2657 $self->{scrolled}->add (@items);
2522 2658
2523# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2659# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2524} 2660}
2525 2661
2526sub size_request { 2662sub size_request {
2604 2740
2605package CFClient::UI::Statusbox; 2741package CFClient::UI::Statusbox;
2606 2742
2607our @ISA = CFClient::UI::VBox::; 2743our @ISA = CFClient::UI::VBox::;
2608 2744
2745sub new {
2746 my $class = shift;
2747
2748 $class->SUPER::new (
2749 fontsize => 0.8,
2750 @_,
2751 )
2752}
2753
2609sub reorder { 2754sub reorder {
2610 my ($self) = @_; 2755 my ($self) = @_;
2611 my $NOW = time; 2756 my $NOW = time;
2612 2757
2613 while (my ($k, $v) = each %{ $self->{item} }) { 2758 while (my ($k, $v) = each %{ $self->{item} }) {
2629 # TODO: doesn't handle markup well (read as: at all) 2774 # TODO: doesn't handle markup well (read as: at all)
2630 my $short = $item->{count} > 1 2775 my $short = $item->{count} > 1
2631 ? "<b>$item->{count} ×</b> $item->{text}" 2776 ? "<b>$item->{count} ×</b> $item->{text}"
2632 : $item->{text}; 2777 : $item->{text};
2633 2778
2779 my $fontsize = $item->{fontsize} || $self->{fontsize};
2780
2634 for ($short) { 2781 for ($short) {
2635 s/^\s+//; 2782 s/^\s+//;
2636 s/\012.*/…/s; 2783 s/\s+/ /g;
2637 my $len = int 30 / $item->{fontsize}; 2784 my $len = int 40 / $fontsize;
2638 substr $_, $len, length, "…" if $len < length; 2785 substr $_, $len, length, "…" if $len < length;
2639 } 2786 }
2640 2787
2641 new CFClient::UI::Label 2788 new CFClient::UI::Label
2642 markup => $short, 2789 markup => $short,
2643 tooltip => $item->{tooltip}, 2790 tooltip => $item->{tooltip},
2644 tooltip_font => $::FONT_PROP, 2791 tooltip_font => $::FONT_PROP,
2645 tooltip_width => 0.67, 2792 tooltip_width => 0.67,
2646 fontsize => $item->{fontsize}, 2793 fontsize => $fontsize,
2647 color => $item->{color}, 2794 fg => $item->{fg},
2648 can_events => 1, 2795 can_events => 1,
2649 can_hover => 1 2796 can_hover => 1
2650 }; 2797 };
2651 } 2798 }
2652 2799
2654 $self->SUPER::add (reverse @widgets); 2801 $self->SUPER::add (reverse @widgets);
2655} 2802}
2656 2803
2657sub add { 2804sub add {
2658 my ($self, $text, %arg) = @_; 2805 my ($self, $text, %arg) = @_;
2806
2807 $text =~ s/^\s+//;
2808 $text =~ s/\s+$//;
2659 2809
2660 my $timeout = time + ((delete $arg{timeout}) || 60); 2810 my $timeout = time + ((delete $arg{timeout}) || 60);
2661 2811
2662 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2812 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2663 2813
2666 $item->{count}++; 2816 $item->{count}++;
2667 } else { 2817 } else {
2668 $item->{count} = 1; 2818 $item->{count} = 1;
2669 $item->{text} = $item->{tooltip} = $text; 2819 $item->{text} = $item->{tooltip} = $text;
2670 } 2820 }
2821 $item->{id} = ++$self->{id};
2671 $item->{timeout} = $timeout; 2822 $item->{timeout} = $timeout;
2672 delete $item->{label}; 2823 delete $item->{label};
2673 } else { 2824 } else {
2674 $self->{item}{$group} = { 2825 $self->{item}{$group} = {
2675 id => ++$self->{id}, 2826 id => ++$self->{id},
2676 text => $text, 2827 text => $text,
2677 timeout => $timeout, 2828 timeout => $timeout,
2678 tooltip => $text, 2829 tooltip => $text,
2679 fontsize => 0.8,
2680 color => [0.8, 0.8, 0.8, 0.8], 2830 fg => [0.8, 0.8, 0.8, 0.8],
2681 pri => 0, 2831 pri => 0,
2682 count => 1, 2832 count => 1,
2683 %arg, 2833 %arg,
2684 }; 2834 };
2685 } 2835 }
2724} 2874}
2725 2875
2726sub size_allocate { 2876sub size_allocate {
2727 my ($self, $w, $h) = @_; 2877 my ($self, $w, $h) = @_;
2728 2878
2729 my $old_w = $self->{old_w}; 2879 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2730 my $old_h = $self->{old_h}; 2880 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2731 2881
2732 if ($old_w && $old_h) { 2882 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2733 for my $child ($self->children) { 2883 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2734 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2735 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2736 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2737 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2738 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2739 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2740 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2741 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2742 }
2743 }
2744 2884
2745 for my $child ($self->children) { 2885 for my $child ($self->children) {
2746 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2886 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2747 2887
2888 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
2889 if exists $child->{req_x};
2890
2891 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
2892 if exists $child->{req_y};
2893
2748 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2894 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2749 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2895 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
2896
2750 $child->configure ($X, $Y, $W, $H); 2897 $child->configure ($X, $Y, $W, $H);
2751 } 2898 }
2752
2753 $self->{old_w} = $w;
2754 $self->{old_h} = $h;
2755} 2899}
2756 2900
2757sub coord2local { 2901sub coord2local {
2758 my ($self, $x, $y) = @_; 2902 my ($self, $x, $y) = @_;
2759 2903
2772 $self->check_size; 2916 $self->check_size;
2773 $::WANT_REFRESH++; 2917 $::WANT_REFRESH++;
2774} 2918}
2775 2919
2776sub add { 2920sub add {
2777 my ($self, $child) = @_; 2921 my ($self, @children) = @_;
2778 2922
2923 for my $child (@children) {
2924 $child->{toplevel} = 1;
2925
2779 # integerise window positions 2926 # integerise window positions
2780 $child->{x} = int $child->{x}; 2927 $child->{x} = int $child->{x};
2781 $child->{y} = int $child->{y}; 2928 $child->{y} = int $child->{y};
2929 }
2782 2930
2783 $self->SUPER::add ($child); 2931 $self->SUPER::add (@children);
2784} 2932}
2785 2933
2786sub on_refresh { 2934sub on_refresh {
2787 my ($self, $id, $cb) = @_; 2935 my ($self, $id, $cb) = @_;
2788 2936
2789 $self->{refresh_hook}{$id} = $cb; 2937 $self->{refresh_hook}{$id} = $cb;
2790} 2938}
2791 2939
2940sub on_post_alloc {
2941 my ($self, $id, $cb) = @_;
2942
2943 $self->{post_alloc_hook}{$id} = $cb;
2944}
2945
2792sub draw { 2946sub draw {
2793 my ($self) = @_; 2947 my ($self) = @_;
2948
2949 while ($self->{refresh_hook}) {
2950 $_->()
2951 for values %{delete $self->{refresh_hook}};
2952 }
2794 2953
2795 if ($self->{check_size}) { 2954 if ($self->{check_size}) {
2796 my @queue = ([], []); 2955 my @queue = ([], []);
2797 2956
2798 for (;;) { 2957 for (;;) {
2835 $widget->size_allocate ($w, $h); 2994 $widget->size_allocate ($w, $h);
2836 $widget->emit (size_allocate => $w, $h); 2995 $widget->emit (size_allocate => $w, $h);
2837 } 2996 }
2838 } 2997 }
2839 2998
2840 while ($self->{refresh_hook}) { 2999 while ($self->{post_alloc_hook}) {
2841 $_->() 3000 $_->()
2842 for values %{delete $self->{refresh_hook}}; 3001 for values %{delete $self->{post_alloc_hook}};
2843 } 3002 }
2844 3003
2845 glViewport 0, 0, $::WIDTH, $::HEIGHT; 3004 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2846 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3005 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2847 glClear GL_COLOR_BUFFER_BIT; 3006 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines