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.198 by root, Fri May 12 02:08:52 2006 UTC vs.
Revision 1.210 by root, Wed May 17 10:14:52 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
247sub children {
248}
249
250# call when resolution changes etc.
251sub reconfigure { 270sub reconfigure {
252 my ($self) = @_; 271 my ($self) = @_;
253
254 $_->reconfigure
255 for $self->children;
256 272
257 $self->check_size (1); 273 $self->check_size (1);
258 $self->update; 274 $self->update;
259} 275}
260 276
277sub children {
278}
279
261sub set_max_size { 280sub set_max_size {
262 my ($self, $w, $h) = @_; 281 my ($self, $w, $h) = @_;
263 282
264 delete $self->{max_w}; $self->{max_w} = $w if $w; 283 delete $self->{max_w}; $self->{max_w} = $w if $w;
265 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 }
266} 296}
267 297
268# translate global coordinates to local coordinate system 298# translate global coordinates to local coordinate system
269sub coord2local { 299sub coord2local {
270 my ($self, $x, $y) = @_; 300 my ($self, $x, $y) = @_;
356 glVertex $self->{w}, 0; 386 glVertex $self->{w}, 0;
357 glVertex $self->{w}, $self->{h}; 387 glVertex $self->{w}, $self->{h};
358 glVertex 0 , $self->{h}; 388 glVertex 0 , $self->{h};
359 glEnd; 389 glEnd;
360 glPopMatrix; 390 glPopMatrix;
361 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;
362 } 392 }
363} 393}
364 394
365sub _draw { 395sub _draw {
366 my ($self) = @_; 396 my ($self) = @_;
383sub set_parent { 413sub set_parent {
384 my ($self, $parent) = @_; 414 my ($self, $parent) = @_;
385 415
386 Scalar::Util::weaken ($self->{parent} = $parent); 416 Scalar::Util::weaken ($self->{parent} = $parent);
387 417
388
389 # TODO: req_w _does_change after ->reconfigure 418 # TODO: req_w _does_change after ->reconfigure
390 $self->check_size 419 $self->check_size
391 unless exists $self->{req_w}; 420 unless exists $self->{req_w};
392} 421}
393 422
418} 447}
419 448
420sub DESTROY { 449sub DESTROY {
421 my ($self) = @_; 450 my ($self) = @_;
422 451
452 delete $WIDGET{$self+0};
423 #$self->deactivate; 453 #$self->deactivate;
424} 454}
425 455
426############################################################################# 456#############################################################################
427 457
436 my $class = shift; 466 my $class = shift;
437 467
438 # range [value, low, high, page] 468 # range [value, low, high, page]
439 469
440 $class->SUPER::new ( 470 $class->SUPER::new (
441 bg => [0, 0, 0, 0.2], 471 #bg => [0, 0, 0, 0.2],
442 active_bg => [1, 1, 1, 0.5], 472 #active_bg => [1, 1, 1, 0.5],
443 @_ 473 @_
444 ) 474 )
445} 475}
446 476
447sub _draw { 477sub _draw {
448 my ($self) = @_; 478 my ($self) = @_;
449 479
480 my $color = $FOCUS == $self && $self->{active_bg}
481 ? $self->{active_bg}
482 : $self->{bg};
483
484 if ($color && (@$color < 4 || $color->[3])) {
450 my ($w, $h) = @$self{qw(w h)}; 485 my ($w, $h) = @$self{qw(w h)};
451 486
452 glEnable GL_BLEND; 487 glEnable GL_BLEND;
453 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 488 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
454 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 489 glColor @$color;
455 490
456 glBegin GL_QUADS; 491 glBegin GL_QUADS;
457 glVertex 0 , 0; 492 glVertex 0 , 0;
458 glVertex 0 , $h; 493 glVertex 0 , $h;
459 glVertex $w, $h; 494 glVertex $w, $h;
460 glVertex $w, 0; 495 glVertex $w, 0;
461 glEnd; 496 glEnd;
462 497
463 glDisable GL_BLEND; 498 glDisable GL_BLEND;
499 }
464} 500}
465 501
466############################################################################# 502#############################################################################
467 503
468package CFClient::UI::Empty; 504package CFClient::UI::Empty;
777 $self->add ($self->{slider}); 813 $self->add ($self->{slider});
778 814
779 $self 815 $self
780} 816}
781 817
782#TODO# update range on size_allocate depeneing on child 818#TODO# update range on size_allocate depending on child
783# update viewport offset on scroll 819# update viewport offset on scroll
784 820
785############################################################################# 821#############################################################################
786 822
787package CFClient::UI::Frame; 823package CFClient::UI::Frame;
788 824
789our @ISA = CFClient::UI::Bin::; 825our @ISA = CFClient::UI::Bin::;
790 826
791use 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}
792 860
793############################################################################# 861#############################################################################
794 862
795package CFClient::UI::FancyFrame; 863package CFClient::UI::FancyFrame;
796 864
809 877
810 my $self = $class->SUPER::new ( 878 my $self = $class->SUPER::new (
811 bg => [1, 1, 1, 1], 879 bg => [1, 1, 1, 1],
812 border_bg => [1, 1, 1, 1], 880 border_bg => [1, 1, 1, 1],
813 border => 0.6, 881 border => 0.6,
882 toplevel => 1,
814 can_events => 1, 883 can_events => 1,
815 @_ 884 @_
816 ); 885 );
817 886
818 $self->{title} &&= new CFClient::UI::Label 887 $self->{title} &&= new CFClient::UI::Label
843 my ($self, $w, $h) = @_; 912 my ($self, $w, $h) = @_;
844 913
845 $h -= List::Util::max 0, $self->border * 2; 914 $h -= List::Util::max 0, $self->border * 2;
846 $w -= List::Util::max 0, $self->border * 2; 915 $w -= List::Util::max 0, $self->border * 2;
847 916
848 $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)
849 if $self->{title}; 918 if $self->{title};
850 919
851 $self->child->configure ($self->border, $self->border, $w, $h); 920 $self->child->configure ($self->border, $self->border, $w, $h);
852} 921}
853 922
1214 1283
1215############################################################################# 1284#############################################################################
1216 1285
1217package CFClient::UI::Label; 1286package CFClient::UI::Label;
1218 1287
1219our @ISA = CFClient::UI::Base::; 1288our @ISA = CFClient::UI::DrawBG::;
1220 1289
1221use CFClient::OpenGL; 1290use CFClient::OpenGL;
1222 1291
1223sub new { 1292sub new {
1224 my ($class, %arg) = @_; 1293 my ($class, %arg) = @_;
1225 1294
1226 my $self = $class->SUPER::new ( 1295 my $self = $class->SUPER::new (
1227 fg => [1, 1, 1], 1296 fg => [1, 1, 1],
1297 #bg => none
1298 #active_bg => none
1228 #font => default_font 1299 #font => default_font
1229 #text => initial text 1300 #text => initial text
1230 #markup => initial narkup 1301 #markup => initial narkup
1231 layout => (new CFClient::Layout), 1302 layout => (new CFClient::Layout),
1232 fontsize => 1, 1303 fontsize => 1,
1250 } 1321 }
1251 1322
1252 $self 1323 $self
1253} 1324}
1254 1325
1255sub escape { 1326sub escape($) {
1256 local $_ = $_[1]; 1327 local $_ = $_[0];
1257 1328
1258 s/&/&amp;/g; 1329 s/&/&amp;/g;
1259 s/>/&gt;/g; 1330 s/>/&gt;/g;
1260 s/</&lt;/g; 1331 s/</&lt;/g;
1261 1332
1262 $_[1] 1333 $_
1263} 1334}
1264 1335
1265sub update { 1336sub update {
1266 my ($self) = @_; 1337 my ($self) = @_;
1267 1338
1338 $self->check_size; 1409 $self->check_size;
1339} 1410}
1340 1411
1341sub _draw { 1412sub _draw {
1342 my ($self) = @_; 1413 my ($self) = @_;
1414
1415 $self->SUPER::_draw; # draw background, if applicable
1343 1416
1344 my $tex = $self->{texture} ||= do { 1417 my $tex = $self->{texture} ||= do {
1345 $self->{layout}->set_foreground (@{$self->{fg}}); 1418 $self->{layout}->set_foreground (@{$self->{fg}});
1346 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1419 $self->{layout}->set_font ($self->{font}) if $self->{font};
1347 $self->{layout}->set_width ($self->{w}); 1420 $self->{layout}->set_width ($self->{w});
1348 $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);
1349 1422
1350 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1423 my $tex = new_from_layout CFClient::Texture $self->{layout};
1351 1424
1352 $self->{ox} = int $self->{align} < 0 ? $self->{padding} 1425 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1353 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1426 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1354 : ($self->{w} - $tex->{w}) * 0.5; 1427 : ($self->{w} - $tex->{w}) * 0.5);
1355 1428
1356 $self->{oy} = int $self->{valign} < 0 ? $self->{padding} 1429 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1357 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1430 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1358 : ($self->{h} - $tex->{h}) * 0.5; 1431 : ($self->{h} - $tex->{h}) * 0.5);
1359 1432
1360 $tex 1433 $tex
1361 }; 1434 };
1362 1435
1363 glEnable GL_TEXTURE_2D; 1436 glEnable GL_TEXTURE_2D;
1418sub set_text { 1491sub set_text {
1419 my ($self, $text) = @_; 1492 my ($self, $text) = @_;
1420 1493
1421 $self->{cursor} = length $text; 1494 $self->{cursor} = length $text;
1422 $self->_set_text ($text); 1495 $self->_set_text ($text);
1496 $self->update;
1423 $self->check_size; 1497 $self->check_size;
1424 $self->update;
1425} 1498}
1426 1499
1427sub get_text { 1500sub get_text {
1428 $_[0]{text} 1501 $_[0]{text}
1429} 1502}
1449 my $sym = $ev->{sym}; 1522 my $sym = $ev->{sym};
1450 my $uni = $ev->{unicode}; 1523 my $uni = $ev->{unicode};
1451 1524
1452 my $text = $self->get_text; 1525 my $text = $self->get_text;
1453 1526
1454 if ($sym == 8) { 1527 if ($uni == 8) {
1455 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1528 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1456 } elsif ($sym == 127) { 1529 } elsif ($uni == 127) {
1457 substr $text, $self->{cursor}, 1, ""; 1530 substr $text, $self->{cursor}, 1, "";
1458 } elsif ($sym == CFClient::SDLK_LEFT) { 1531 } elsif ($sym == CFClient::SDLK_LEFT) {
1459 --$self->{cursor} if $self->{cursor}; 1532 --$self->{cursor} if $self->{cursor};
1460 } elsif ($sym == CFClient::SDLK_RIGHT) { 1533 } elsif ($sym == CFClient::SDLK_RIGHT) {
1461 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1534 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1462 } elsif ($sym == CFClient::SDLK_HOME) { 1535 } elsif ($sym == CFClient::SDLK_HOME) {
1463 $self->{cursor} = 0; 1536 $self->{cursor} = 0;
1464 } elsif ($sym == CFClient::SDLK_END) { 1537 } elsif ($sym == CFClient::SDLK_END) {
1465 $self->{cursor} = length $text; 1538 $self->{cursor} = length $text;
1466 } elsif ($sym == 27) { 1539 } elsif ($uni == 27) {
1467 $self->emit ('escape'); 1540 $self->emit ('escape');
1468 } elsif ($uni) { 1541 } elsif ($uni) {
1469 substr $text, $self->{cursor}++, 0, chr $uni; 1542 substr $text, $self->{cursor}++, 0, chr $uni;
1470 } 1543 }
1471 1544
1472 $self->_set_text ($text); 1545 $self->_set_text ($text);
1473 $self->update; 1546 $self->update;
1547 $self->check_size;
1474} 1548}
1475 1549
1476sub focus_in { 1550sub focus_in {
1477 my ($self) = @_; 1551 my ($self) = @_;
1478 1552
1607 my $class = shift; 1681 my $class = shift;
1608 1682
1609 $class->SUPER::new ( 1683 $class->SUPER::new (
1610 padding => 4, 1684 padding => 4,
1611 fg => [1, 1, 1], 1685 fg => [1, 1, 1],
1612 bg => [1, 1, 1, 0.2],
1613 active_fg => [0, 0, 1], 1686 active_fg => [0, 0, 1],
1614 can_hover => 1, 1687 can_hover => 1,
1615 align => 0, 1688 align => 0,
1616 valign => 0, 1689 valign => 0,
1617 can_events => 1, 1690 can_events => 1,
1665 1738
1666 $class->SUPER::new ( 1739 $class->SUPER::new (
1667 padding => 2, 1740 padding => 2,
1668 fg => [1, 1, 1], 1741 fg => [1, 1, 1],
1669 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],
1670 state => 0, 1745 state => 0,
1671 can_hover => 1, 1746 can_hover => 1,
1672 @_ 1747 @_
1673 ) 1748 )
1674} 1749}
1958 qw(s1_slider.png s1_slider_bg.png); 2033 qw(s1_slider.png s1_slider_bg.png);
1959 2034
1960sub new { 2035sub new {
1961 my $class = shift; 2036 my $class = shift;
1962 2037
1963 # range [value, low, high, page] 2038 # range [value, low, high, page, unit]
1964 2039
1965 # TODO: 0-width page 2040 # TODO: 0-width page
1966 # TODO: req_w/h are wrong with vertical 2041 # TODO: req_w/h are wrong with vertical
1967 # TODO: calculations are off 2042 # TODO: calculations are off
1968 my $self = $class->SUPER::new ( 2043 my $self = $class->SUPER::new (
1969 fg => [1, 1, 1], 2044 fg => [1, 1, 1],
1970 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],
1971 range => [0, 0, 100, 10], 2048 range => [0, 0, 100, 10],
1972 req_w => $::WIDTH / 80, 2049 req_w => $::WIDTH / 80,
1973 req_h => $::WIDTH / 80, 2050 req_h => $::WIDTH / 80,
1974 vertical => 0, 2051 vertical => 0,
1975 can_hover => 1, 2052 can_hover => 1,
1976 inner_pad => 5, 2053 inner_pad => .05,
1977 @_ 2054 @_
1978 ); 2055 );
1979 2056
2057 $self->set_value ($self->{range}[0]);
2058 $self->update;
2059
1980 $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 }
1981} 2084}
1982 2085
1983sub size_request { 2086sub size_request {
1984 my ($self) = @_; 2087 my ($self) = @_;
1985 2088
1998 2101
1999sub mouse_motion { 2102sub mouse_motion {
2000 my ($self, $ev, $x, $y) = @_; 2103 my ($self, $ev, $x, $y) = @_;
2001 2104
2002 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
2003 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2122 my ($value, $lo, $hi, $page) = @{$self->{range}};
2004 2123
2005 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2124 my $inner_w = 1 - 2 * $self->{inner_pad};
2006 2125
2007 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2126 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
2008 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2009 2127
2010 $x -= $inner_pad_px; # substract the padding 2128 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
2011 $x = $x * ($hi - $lo) / $inner_w + $lo; 2129 $value = $self->{scale} * ($value - $lo);
2012 $x = $lo if $x < $lo;
2013 $x = $hi - $page if $x > $hi - $page;
2014 $self->{range}[0] = $x;
2015 2130
2016 $self->emit (changed => $x); 2131 $value = $self->{inner_pad} + ($value - $page * 0.5);
2017 $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;
2018 } 2138 });
2019}
2020 2139
2021# the inner_* stuff is for generating a padding for the slider handle, 2140 $self->SUPER::update;
2022# so that the handle doesn't leave the texture. This calculation isn't 100%
2023# correct propably, but it does the job for now
2024sub _calc_inner_pad_px {
2025 my ($self, $w) = @_;
2026 ($w / 100) * $self->{inner_pad} # % to pixels
2027} 2141}
2028 2142
2029sub _draw { 2143sub _draw {
2030 my ($self) = @_; 2144 my ($self) = @_;
2031 2145
2032 $self->SUPER::_draw (); 2146 $self->SUPER::_draw ();
2033 2147
2034 my ($w, $h) = @$self{qw(w h)}; 2148 glScale $self->{w}, $self->{h};
2035 2149
2036 if ($self->{vertical}) { 2150 if ($self->{vertical}) {
2037 # draw a vertical slider like a rotated horizontal slider 2151 # draw a vertical slider like a rotated horizontal slider
2038 2152
2039 glRotate 90, 0, 0, 1; 2153 glRotate 90, 0, 0, 1;
2040 glTranslate 0, -$self->{w}, 0; 2154 glTranslate 0, 1, 0;
2041
2042 ($w, $h) = ($h, $w);
2043 } 2155 }
2044 2156
2045 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2157 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2046 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2158 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2047 2159
2048 my ($value, $lo, $hi, $page) = @{$self->{range}};
2049
2050 $hi = $value + 1 if $lo == $hi;
2051
2052 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2053 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2054
2055 $page = int $page * $inner_w / ($hi - $lo);
2056 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2057
2058 $w -= $page;
2059 $page &= ~1;
2060 glTranslate $page * 0.5, 0, 0;
2061 $page ||= 2;
2062
2063 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2064 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2065
2066 glEnable GL_TEXTURE_2D; 2160 glEnable GL_TEXTURE_2D;
2067 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2161 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2068 2162
2069 # draw background 2163 # draw background
2070 $tex[1]->draw_quad_alpha (0, 0, $w, $h); 2164 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2071 2165
2072 # draw handle 2166 # draw handle
2073 $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);
2074 2168
2075 glDisable GL_TEXTURE_2D; 2169 glDisable GL_TEXTURE_2D;
2076} 2170}
2077 2171
2078############################################################################# 2172#############################################################################
2435 2529
2436 my $desc = $item->{nrof} < 2 2530 my $desc = $item->{nrof} < 2
2437 ? $item->{name} 2531 ? $item->{name}
2438 : "$item->{nrof} $item->{name_pl}"; 2532 : "$item->{nrof} $item->{name_pl}";
2439 2533
2534 $item->{flags} & Crossfire::Protocol::F_OPEN
2535 and $desc .= " (open)";
2536 $item->{flags} & Crossfire::Protocol::F_APPLIED
2537 and $desc .= " (applied)";
2538 $item->{flags} & Crossfire::Protocol::F_UNPAID
2539 and $desc .= " (unpaid)";
2540 $item->{flags} & Crossfire::Protocol::F_MAGIC
2541 and $desc .= " (magic)";
2542 $item->{flags} & Crossfire::Protocol::F_CURSED
2543 and $desc .= " (cursed)";
2544 $item->{flags} & Crossfire::Protocol::F_DAMNED
2545 and $desc .= " (damned)";
2546 $item->{flags} & Crossfire::Protocol::F_LOCKED
2547 and $desc .= " *";
2440 2548
2441 my $self = $class->SUPER::new ( 2549 my $self = $class->SUPER::new (
2442 can_hover => 1, 2550 can_hover => 1,
2443 can_events => 1, 2551 can_events => 1,
2444 tooltip => (CFClient::UI::Label->escape ($desc) 2552 tooltip => ((CFClient::UI::Label::escape $desc)
2445 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2553 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2446 connect_button_down => sub { 2554 connect_button_down => sub {
2447 my ($self, $ev, $x, $y) = @_; 2555 my ($self, $ev, $x, $y) = @_;
2448 2556
2449 # todo: maybe put examine on 1? but should just be a tooltip :( 2557 # todo: maybe put examine on 1? but should just be a tooltip :(
2450 if ($ev->{button} == 1) { 2558 if ($ev->{button} == 1) {
2451 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2559 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2452 } elsif ($ev->{button} == 2) { 2560 } elsif ($ev->{button} == 2) {
2453 $::CONN->send ("apply $item->{tag}"); 2561 $::CONN->send ("apply $item->{tag}");
2454 } elsif ($ev->{button} == 3) { 2562 } elsif ($ev->{button} == 3) {
2455 CFClient::UI::Menu->new ( 2563 my @menu_items = (
2456 items => [
2457 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2564 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2565 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2566 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2567 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2458 [ 2568 [
2459 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", 2569 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2460 sub { $::CONN->send ("lock $item->{tag}") }, 2570 sub { $::CONN->send ("lock ". () ."$item->{tag}") },
2461 ],
2462 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2463 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2464 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2465 ], 2571 ],
2466 )->popup ($ev); 2572 );
2573
2574 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2467 } 2575 }
2468 2576
2469 1 2577 1
2470 }, 2578 },
2471 %args 2579 %args
2507 my ($self, $items) = @_; 2615 my ($self, $items) = @_;
2508 2616
2509 $self->{scrolled}->clear; 2617 $self->{scrolled}->clear;
2510 return unless $items; 2618 return unless $items;
2511 2619
2512 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2620 my @items = sort {
2621 ($a->{type} <=> $b->{type})
2622 or ($a->{name} cmp $b->{name})
2623 } @$items;
2513 2624
2514 $self->{real_items} = \@items; 2625 $self->{real_items} = \@items;
2515 2626
2516 for my $item (@items) { 2627 for my $item (@items) {
2517 my $desc = $item->{nrof} < 2 2628 my $desc = $item->{nrof} < 2
2518 ? $item->{name} 2629 ? $item->{name}
2519 : "$item->{nrof} $item->{name_pl}"; 2630 : "$item->{nrof} $item->{name_pl}";
2520 2631
2521 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2632 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2522 } 2633 }
2634
2635 $self->{scrolled}->add (@items);
2523 2636
2524# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2637# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2525} 2638}
2526 2639
2527sub size_request { 2640sub size_request {
2605 2718
2606package CFClient::UI::Statusbox; 2719package CFClient::UI::Statusbox;
2607 2720
2608our @ISA = CFClient::UI::VBox::; 2721our @ISA = CFClient::UI::VBox::;
2609 2722
2723sub new {
2724 my $class = shift;
2725
2726 $class->SUPER::new (
2727 fontsize => 0.8,
2728 @_,
2729 )
2730}
2731
2610sub reorder { 2732sub reorder {
2611 my ($self) = @_; 2733 my ($self) = @_;
2612 my $NOW = time; 2734 my $NOW = time;
2613 2735
2614 while (my ($k, $v) = each %{ $self->{item} }) { 2736 while (my ($k, $v) = each %{ $self->{item} }) {
2630 # TODO: doesn't handle markup well (read as: at all) 2752 # TODO: doesn't handle markup well (read as: at all)
2631 my $short = $item->{count} > 1 2753 my $short = $item->{count} > 1
2632 ? "<b>$item->{count} ×</b> $item->{text}" 2754 ? "<b>$item->{count} ×</b> $item->{text}"
2633 : $item->{text}; 2755 : $item->{text};
2634 2756
2757 my $fontsize = $item->{fontsize} || $self->{fontsize};
2758
2635 for ($short) { 2759 for ($short) {
2636 s/^\s+//; 2760 s/^\s+//;
2637 s/\012.*/…/s; 2761 s/\s+/ /g;
2638 my $len = int 40 / $item->{fontsize}; 2762 my $len = int 40 / $fontsize;
2639 substr $_, $len, length, "…" if $len < length; 2763 substr $_, $len, length, "…" if $len < length;
2640 } 2764 }
2641 2765
2642 new CFClient::UI::Label 2766 new CFClient::UI::Label
2643 markup => $short, 2767 markup => $short,
2644 tooltip => $item->{tooltip}, 2768 tooltip => $item->{tooltip},
2645 tooltip_font => $::FONT_PROP, 2769 tooltip_font => $::FONT_PROP,
2646 tooltip_width => 0.67, 2770 tooltip_width => 0.67,
2647 fontsize => $item->{fontsize}, 2771 fontsize => $fontsize,
2648 color => $item->{color}, 2772 fg => $item->{fg},
2649 can_events => 1, 2773 can_events => 1,
2650 can_hover => 1 2774 can_hover => 1
2651 }; 2775 };
2652 } 2776 }
2653 2777
2679 $self->{item}{$group} = { 2803 $self->{item}{$group} = {
2680 id => ++$self->{id}, 2804 id => ++$self->{id},
2681 text => $text, 2805 text => $text,
2682 timeout => $timeout, 2806 timeout => $timeout,
2683 tooltip => $text, 2807 tooltip => $text,
2684 fontsize => 0.8,
2685 color => [0.8, 0.8, 0.8, 0.8], 2808 fg => [0.8, 0.8, 0.8, 0.8],
2686 pri => 0, 2809 pri => 0,
2687 count => 1, 2810 count => 1,
2688 %arg, 2811 %arg,
2689 }; 2812 };
2690 } 2813 }
2729} 2852}
2730 2853
2731sub size_allocate { 2854sub size_allocate {
2732 my ($self, $w, $h) = @_; 2855 my ($self, $w, $h) = @_;
2733 2856
2734 my $old_w = $self->{old_w}; 2857 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2735 my $old_h = $self->{old_h}; 2858 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2736 2859
2737 if ($old_w && $old_h) { 2860 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2738 for my $child ($self->children) { 2861 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2739 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2740 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2741 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2742 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2743 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2744 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2745 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2746 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2747 }
2748 }
2749 2862
2750 for my $child ($self->children) { 2863 for my $child ($self->children) {
2751 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2864 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2752 2865
2866 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
2867 if exists $child->{req_x};
2868
2869 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
2870 if exists $child->{req_y};
2871
2753 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2872 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2754 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2873 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
2874
2755 $child->configure ($X, $Y, $W, $H); 2875 $child->configure ($X, $Y, $W, $H);
2756 } 2876 }
2757
2758 $self->{old_w} = $w;
2759 $self->{old_h} = $h;
2760} 2877}
2761 2878
2762sub coord2local { 2879sub coord2local {
2763 my ($self, $x, $y) = @_; 2880 my ($self, $x, $y) = @_;
2764 2881
2777 $self->check_size; 2894 $self->check_size;
2778 $::WANT_REFRESH++; 2895 $::WANT_REFRESH++;
2779} 2896}
2780 2897
2781sub add { 2898sub add {
2782 my ($self, $child) = @_; 2899 my ($self, @children) = @_;
2783 2900
2901 for my $child (@children) {
2902 $child->{toplevel} = 1;
2903
2784 # integerise window positions 2904 # integerise window positions
2785 $child->{x} = int $child->{x}; 2905 $child->{x} = int $child->{x};
2786 $child->{y} = int $child->{y}; 2906 $child->{y} = int $child->{y};
2907 }
2787 2908
2788 $self->SUPER::add ($child); 2909 $self->SUPER::add (@children);
2789} 2910}
2790 2911
2791sub on_refresh { 2912sub on_refresh {
2792 my ($self, $id, $cb) = @_; 2913 my ($self, $id, $cb) = @_;
2793 2914

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines