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.207 by elmex, Mon May 15 18:23:33 2006 UTC vs.
Revision 1.229 by root, Wed May 24 21:49:58 2006 UTC

136 $rem += $_ - $i; 136 $rem += $_ - $i;
137 $_ = $i; 137 $_ = $i;
138 } 138 }
139} 139}
140 140
141sub full_refresh {
142 # make a copy, otherwise for complains about freed values.
143 my @widgets = values %WIDGET;
144
145 $_->update
146 for @widgets;
147}
148
141# call when resolution changes etc. 149# call when resolution changes etc.
142sub rescale_widgets { 150sub rescale_widgets {
143 my ($sx, $sy) = @_; 151 my ($sx, $sy) = @_;
144 152
153 # make a copy, otherwise for complains about freed values.
145 for my $widget (values %WIDGET) { 154 my @widgets = values %WIDGET;
155
156 for my $widget (@widgets) {
146 if ($widget->{toplevel}) { 157 if ($widget->{toplevel}) {
147 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 158 $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}; 159 $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}; 160 $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}; 161 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
195 %$self = (); 206 %$self = ();
196} 207}
197 208
198sub show { 209sub show {
199 my ($self) = @_; 210 my ($self) = @_;
200
201 return if $self->{parent}; 211 return if $self->{parent};
202 212
203 $CFClient::UI::ROOT->add ($self); 213 $CFClient::UI::ROOT->add ($self);
214}
215
216sub show_centered {
217 my ($self) = @_;
218 return if $self->{parent};
219
220 $self->show;
221
222 $CFClient::UI::ROOT->on_post_alloc (
223 "centered $self" => sub {
224 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5);
225 },
226 );
204} 227}
205 228
206sub hide { 229sub hide {
207 my ($self) = @_; 230 my ($self) = @_;
208 231
280sub set_max_size { 303sub set_max_size {
281 my ($self, $w, $h) = @_; 304 my ($self, $w, $h) = @_;
282 305
283 delete $self->{max_w}; $self->{max_w} = $w if $w; 306 delete $self->{max_w}; $self->{max_w} = $w if $w;
284 delete $self->{max_h}; $self->{max_h} = $h if $h; 307 delete $self->{max_h}; $self->{max_h} = $h if $h;
308}
309
310sub set_tooltip {
311 my ($self, $tooltip) = @_;
312
313 $self->{tooltip} = $tooltip;
314
315 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
316 delete $CFClient::UI::TOOLTIP->{owner};
317 CFClient::UI::check_tooltip;
318 }
285} 319}
286 320
287# translate global coordinates to local coordinate system 321# translate global coordinates to local coordinate system
288sub coord2local { 322sub coord2local {
289 my ($self, $x, $y) = @_; 323 my ($self, $x, $y) = @_;
405 Scalar::Util::weaken ($self->{parent} = $parent); 439 Scalar::Util::weaken ($self->{parent} = $parent);
406 440
407 # TODO: req_w _does_change after ->reconfigure 441 # TODO: req_w _does_change after ->reconfigure
408 $self->check_size 442 $self->check_size
409 unless exists $self->{req_w}; 443 unless exists $self->{req_w};
444
445 $self->show;
410} 446}
411 447
412sub check_size { 448sub check_size {
413 my ($self, $forced) = @_; 449 my ($self, $forced) = @_;
414 450
455 my $class = shift; 491 my $class = shift;
456 492
457 # range [value, low, high, page] 493 # range [value, low, high, page]
458 494
459 $class->SUPER::new ( 495 $class->SUPER::new (
460 bg => [0, 0, 0, 0.2], 496 #bg => [0, 0, 0, 0.2],
461 active_bg => [1, 1, 1, 0.5], 497 #active_bg => [1, 1, 1, 0.5],
462 @_ 498 @_
463 ) 499 )
464} 500}
465 501
466sub _draw { 502sub _draw {
467 my ($self) = @_; 503 my ($self) = @_;
468 504
505 my $color = $FOCUS == $self && $self->{active_bg}
506 ? $self->{active_bg}
507 : $self->{bg};
508
509 if ($color && (@$color < 4 || $color->[3])) {
469 my ($w, $h) = @$self{qw(w h)}; 510 my ($w, $h) = @$self{qw(w h)};
470 511
471 glEnable GL_BLEND; 512 glEnable GL_BLEND;
472 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 513 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
473 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 514 glColor @$color;
474 515
475 glBegin GL_QUADS; 516 glBegin GL_QUADS;
476 glVertex 0 , 0; 517 glVertex 0 , 0;
477 glVertex 0 , $h; 518 glVertex 0 , $h;
478 glVertex $w, $h; 519 glVertex $w, $h;
479 glVertex $w, 0; 520 glVertex $w, 0;
480 glEnd; 521 glEnd;
481 522
482 glDisable GL_BLEND; 523 glDisable GL_BLEND;
524 }
483} 525}
484 526
485############################################################################# 527#############################################################################
486 528
487package CFClient::UI::Empty; 529package CFClient::UI::Empty;
671 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 713 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
672 glClearColor 0, 0, 0, 0; 714 glClearColor 0, 0, 0, 0;
673 glClear GL_COLOR_BUFFER_BIT; 715 glClear GL_COLOR_BUFFER_BIT;
674 716
675 $self->_render; 717 $self->_render;
676# glColorMask 1, 1, 1, 0;
677# glEnable GL_BLEND;
678# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
679# glRasterPos 0, 0;
680# glCopyPixels 0, 0, $self->{w}, $self->{h};
681# glDisable GL_BLEND;
682# glColorMask 1, 1, 1, 1;
683 }; 718 };
684} 719}
685 720
686sub _draw { 721sub _draw {
687 my ($self) = @_; 722 my ($self) = @_;
691 my $tex = $self->{texture} 726 my $tex = $self->{texture}
692 or return; 727 or return;
693 728
694 glEnable GL_TEXTURE_2D; 729 glEnable GL_TEXTURE_2D;
695 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 730 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
696 glColor 0, 0, 0, 1; 731 glColor 1, 1, 1, 1;
697 732
698 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 733 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
699 734
700 glDisable GL_TEXTURE_2D; 735 glDisable GL_TEXTURE_2D;
701} 736}
708 743
709sub size_request { 744sub size_request {
710 my ($self) = @_; 745 my ($self) = @_;
711 746
712 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 747 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
713 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
714 748
715 @$self{qw(child_w child_h)} 749 @$self{qw(child_w child_h)}
716} 750}
717 751
718sub size_allocate { 752sub size_allocate {
719 my ($self, $w, $h) = @_; 753 my ($self, $w, $h) = @_;
720 754
755 my ($cw, $ch) = @$self{qw(child_w child_h)};
756# $w = $self->{w};
757 $self->child->configure (0, 0, $cw, $ch);
721 $self->update; 758 $self->update;
722} 759}
723 760
724sub set_offset { 761sub set_offset {
725 my ($self, $x, $y) = @_; 762 my ($self, $x, $y) = @_;
779 816
780 my $slider = new CFClient::UI::Slider 817 my $slider = new CFClient::UI::Slider
781 vertical => 1, 818 vertical => 1,
782 range => [0, 0, 1, 0.01], # HACK fix 819 range => [0, 0, 1, 0.01], # HACK fix
783 connect_changed => sub { 820 connect_changed => sub {
784 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); 821 $self->{vp}->set_offset (0, $_[1]);
785 }, 822 },
786 ; 823 ;
787 824
788 $self = $class->SUPER::new ( 825 $self = $class->SUPER::new (
789 vp => (new CFClient::UI::ViewPort), 826 vp => (new CFClient::UI::ViewPort expand => 1),
790 slider => $slider, 827 slider => $slider,
791 @_, 828 @_,
792 ); 829 );
793 830
794 $self->{vp}->add ($self->{scrolled}); 831 $self->{vp}->add ($self->{scrolled});
795 $self->add ($self->{vp}); 832 $self->add ($self->{vp});
796 $self->add ($self->{slider}); 833 $self->add ($self->{slider});
797 834
798 $self 835 $self
836}
837
838sub size_allocate {
839 my ($self, $w, $h) = @_;
840
841 $self->SUPER::size_allocate ($w, $h);
842
843 my $child = $self->{vp}->child;
844 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
799} 845}
800 846
801#TODO# update range on size_allocate depending on child 847#TODO# update range on size_allocate depending on child
802# update viewport offset on scroll 848# update viewport offset on scroll
803 849
895 my ($self, $w, $h) = @_; 941 my ($self, $w, $h) = @_;
896 942
897 $h -= List::Util::max 0, $self->border * 2; 943 $h -= List::Util::max 0, $self->border * 2;
898 $w -= List::Util::max 0, $self->border * 2; 944 $w -= List::Util::max 0, $self->border * 2;
899 945
900 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 946 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
901 if $self->{title}; 947 if $self->{title};
902 948
903 $self->child->configure ($self->border, $self->border, $w, $h); 949 $self->child->configure ($self->border, $self->border, $w, $h);
904} 950}
905 951
1266 1312
1267############################################################################# 1313#############################################################################
1268 1314
1269package CFClient::UI::Label; 1315package CFClient::UI::Label;
1270 1316
1271our @ISA = CFClient::UI::Base::; 1317our @ISA = CFClient::UI::DrawBG::;
1272 1318
1273use CFClient::OpenGL; 1319use CFClient::OpenGL;
1274 1320
1275sub new { 1321sub new {
1276 my ($class, %arg) = @_; 1322 my ($class, %arg) = @_;
1277 1323
1278 my $self = $class->SUPER::new ( 1324 my $self = $class->SUPER::new (
1279 fg => [1, 1, 1], 1325 fg => [1, 1, 1],
1326 #bg => none
1327 #active_bg => none
1280 #font => default_font 1328 #font => default_font
1281 #text => initial text 1329 #text => initial text
1282 #markup => initial narkup 1330 #markup => initial narkup
1331 #max_w => maximum pixel width
1332 ellipsise => 3, # end
1283 layout => (new CFClient::Layout), 1333 layout => (new CFClient::Layout),
1284 fontsize => 1, 1334 fontsize => 1,
1285 align => -1, 1335 align => -1,
1286 valign => -1, 1336 valign => -1,
1287 padding => 2, 1337 padding => 2,
1302 } 1352 }
1303 1353
1304 $self 1354 $self
1305} 1355}
1306 1356
1307sub escape { 1357sub escape($) {
1308 local $_ = $_[1]; 1358 local $_ = $_[0];
1309 1359
1310 s/&/&amp;/g; 1360 s/&/&amp;/g;
1311 s/>/&gt;/g; 1361 s/>/&gt;/g;
1312 s/</&lt;/g; 1362 s/</&lt;/g;
1313 1363
1314 $_[1] 1364 $_
1315} 1365}
1316 1366
1317sub update { 1367sub update {
1318 my ($self) = @_; 1368 my ($self) = @_;
1319 1369
1352sub size_request { 1402sub size_request {
1353 my ($self) = @_; 1403 my ($self) = @_;
1354 1404
1355 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1405 $self->{layout}->set_font ($self->{font}) if $self->{font};
1356 $self->{layout}->set_width ($self->{max_w} || -1); 1406 $self->{layout}->set_width ($self->{max_w} || -1);
1407 $self->{layout}->set_ellipsise ($self->{ellipsise});
1408 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1357 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1409 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1358 1410
1359 my ($w, $h) = $self->{layout}->size; 1411 my ($w, $h) = $self->{layout}->size;
1360 1412
1361 if (exists $self->{template}) { 1413 if (exists $self->{template}) {
1391} 1443}
1392 1444
1393sub _draw { 1445sub _draw {
1394 my ($self) = @_; 1446 my ($self) = @_;
1395 1447
1448 $self->SUPER::_draw; # draw background, if applicable
1449
1396 my $tex = $self->{texture} ||= do { 1450 my $tex = $self->{texture} ||= do {
1397 $self->{layout}->set_foreground (@{$self->{fg}}); 1451 $self->{layout}->set_foreground (@{$self->{fg}});
1398 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1452 $self->{layout}->set_font ($self->{font}) if $self->{font};
1399 $self->{layout}->set_width ($self->{w}); 1453 $self->{layout}->set_width ($self->{w});
1454 $self->{layout}->set_ellipsise ($self->{ellipsise});
1455 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1400 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1456 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1401 1457
1402 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1458 my $tex = new_from_layout CFClient::Texture $self->{layout};
1403 1459
1404 $self->{ox} = int $self->{align} < 0 ? $self->{padding} 1460 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1405 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1461 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1406 : ($self->{w} - $tex->{w}) * 0.5; 1462 : ($self->{w} - $tex->{w}) * 0.5);
1407 1463
1408 $self->{oy} = int $self->{valign} < 0 ? $self->{padding} 1464 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1409 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1465 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1410 : ($self->{h} - $tex->{h}) * 0.5; 1466 : ($self->{h} - $tex->{h}) * 0.5);
1411 1467
1412 $tex 1468 $tex
1413 }; 1469 };
1414 1470
1415 glEnable GL_TEXTURE_2D; 1471 glEnable GL_TEXTURE_2D;
1443 active_fg => [0, 0, 0], 1499 active_fg => [0, 0, 0],
1444 can_hover => 1, 1500 can_hover => 1,
1445 can_focus => 1, 1501 can_focus => 1,
1446 valign => 0, 1502 valign => 0,
1447 can_events => 1, 1503 can_events => 1,
1504 #text => ...
1448 @_ 1505 @_
1449 ) 1506 )
1450} 1507}
1451 1508
1452sub _set_text { 1509sub _set_text {
1470sub set_text { 1527sub set_text {
1471 my ($self, $text) = @_; 1528 my ($self, $text) = @_;
1472 1529
1473 $self->{cursor} = length $text; 1530 $self->{cursor} = length $text;
1474 $self->_set_text ($text); 1531 $self->_set_text ($text);
1532 $self->update;
1475 $self->check_size; 1533 $self->check_size;
1476 $self->update;
1477} 1534}
1478 1535
1479sub get_text { 1536sub get_text {
1480 $_[0]{text} 1537 $_[0]{text}
1481} 1538}
1521 substr $text, $self->{cursor}++, 0, chr $uni; 1578 substr $text, $self->{cursor}++, 0, chr $uni;
1522 } 1579 }
1523 1580
1524 $self->_set_text ($text); 1581 $self->_set_text ($text);
1525 $self->update; 1582 $self->update;
1583 $self->check_size;
1526} 1584}
1527 1585
1528sub focus_in { 1586sub focus_in {
1529 my ($self) = @_; 1587 my ($self) = @_;
1530 1588
1659 my $class = shift; 1717 my $class = shift;
1660 1718
1661 $class->SUPER::new ( 1719 $class->SUPER::new (
1662 padding => 4, 1720 padding => 4,
1663 fg => [1, 1, 1], 1721 fg => [1, 1, 1],
1664 bg => [1, 1, 1, 0.2],
1665 active_fg => [0, 0, 1], 1722 active_fg => [0, 0, 1],
1666 can_hover => 1, 1723 can_hover => 1,
1667 align => 0, 1724 align => 0,
1668 valign => 0, 1725 valign => 0,
1669 can_events => 1, 1726 can_events => 1,
1717 1774
1718 $class->SUPER::new ( 1775 $class->SUPER::new (
1719 padding => 2, 1776 padding => 2,
1720 fg => [1, 1, 1], 1777 fg => [1, 1, 1],
1721 active_fg => [1, 1, 0], 1778 active_fg => [1, 1, 0],
1779 bg => [0, 0, 0, 0.2],
1780 active_bg => [1, 1, 1, 0.5],
1722 state => 0, 1781 state => 0,
1723 can_hover => 1, 1782 can_hover => 1,
1724 @_ 1783 @_
1725 ) 1784 )
1726} 1785}
2018 # TODO: req_w/h are wrong with vertical 2077 # TODO: req_w/h are wrong with vertical
2019 # TODO: calculations are off 2078 # TODO: calculations are off
2020 my $self = $class->SUPER::new ( 2079 my $self = $class->SUPER::new (
2021 fg => [1, 1, 1], 2080 fg => [1, 1, 1],
2022 active_fg => [0, 0, 0], 2081 active_fg => [0, 0, 0],
2082 bg => [0, 0, 0, 0.2],
2083 active_bg => [1, 1, 1, 0.5],
2023 range => [0, 0, 100, 10], 2084 range => [0, 0, 100, 10, 0],
2024 req_w => $::WIDTH / 80, 2085 req_w => $::WIDTH / 80,
2025 req_h => $::WIDTH / 80, 2086 req_h => $::WIDTH / 80,
2026 vertical => 0, 2087 vertical => 0,
2027 can_hover => 1, 2088 can_hover => 1,
2028 inner_pad => .05, 2089 inner_pad => 0.02,
2029 @_ 2090 @_
2030 ); 2091 );
2031 2092
2032 $self->set_value ($self->{range}[0]); 2093 $self->set_value ($self->{range}[0]);
2033 $self->update; 2094 $self->update;
2034 2095
2035 $self 2096 $self
2036} 2097}
2037 2098
2099sub set_range {
2100 my ($self, $range) = @_;
2101
2102 $self->{range} = $range;
2103
2104 $self->update;
2105}
2106
2038sub set_value { 2107sub set_value {
2039 my ($self, $value) = @_; 2108 my ($self, $value) = @_;
2040 2109
2041 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 2110 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2042 2111
2043 $hi = $lo + 1 if $hi <= $lo; 2112 $hi = $lo + 1 if $hi <= $lo;
2044 2113
2114 $page = $hi - $lo if $page > $hi - $lo;
2115
2045 $value = $lo if $value < $lo; 2116 $value = $lo if $value < $lo;
2046 $value = $hi if $value > $hi; 2117 $value = $hi - $page if $value > $hi - $page;
2047 2118
2048 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 2119 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2049 if $unit; 2120 if $unit;
2050
2051 $page = $hi - $lo if $page > $hi - $lo;
2052 2121
2053 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2122 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2054 2123
2055 if ($value != $old_value) { 2124 if ($value != $old_value) {
2056 $self->emit (changed => $value); 2125 $self->emit (changed => $value);
2069 2138
2070sub button_down { 2139sub button_down {
2071 my ($self, $ev, $x, $y) = @_; 2140 my ($self, $ev, $x, $y) = @_;
2072 2141
2073 $self->SUPER::button_down ($ev, $x, $y); 2142 $self->SUPER::button_down ($ev, $x, $y);
2143
2144 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2145
2074 $self->mouse_motion ($ev, $x, $y); 2146 $self->mouse_motion ($ev, $x, $y);
2075} 2147}
2076 2148
2077sub mouse_motion { 2149sub mouse_motion {
2078 my ($self, $ev, $x, $y) = @_; 2150 my ($self, $ev, $x, $y) = @_;
2080 if ($GRAB == $self) { 2152 if ($GRAB == $self) {
2081 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2153 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2082 2154
2083 my (undef, $lo, $hi, $page) = @{$self->{range}}; 2155 my (undef, $lo, $hi, $page) = @{$self->{range}};
2084 2156
2085 $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad}; 2157 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2086 2158
2087 $self->set_value ($x * ($hi - $lo) + $lo); 2159 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2088 } 2160 }
2089} 2161}
2090 2162
2091sub update { 2163sub update {
2092 my ($self) = @_; 2164 my ($self) = @_;
2093 2165
2094 $CFClient::UI::ROOT->on_post_alloc ($self => sub { 2166 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2095 $self->set_value ($self->{range}[0]); 2167 $self->set_value ($self->{range}[0]);
2096 2168
2097 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2169 my ($value, $lo, $hi, $page) = @{$self->{range}};
2170 my $range = ($hi - $page - $lo) || 1e-100;
2098 2171
2099 my $inner_w = 1 - 2 * $self->{inner_pad}; 2172 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1;
2100 2173
2101 $self->{scale} = ($inner_w / ($hi - $lo)) || 1; 2174 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
2175 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2102 2176
2103 $page = $self->{scale} * $page || 10 / ($self->{w} || 1); 2177 $value = ($value - $lo) / $range;
2104 $value = $self->{scale} * ($value - $lo); 2178 $value = $value * $self->{scale} + $self->{offset};
2105 2179
2106 $value = $self->{inner_pad} + ($value - $page * 0.5);
2107
2108 $value = 0 if $value < 0;
2109 $page = 1 - $value if $value + $page > 1;
2110
2111 $self->{knob_x} = $value; 2180 $self->{knob_x} = $value - $knob_w * 0.5;
2112 $self->{knob_w} = $page; 2181 $self->{knob_w} = $knob_w;
2113 }); 2182 });
2114 2183
2115 $self->SUPER::update; 2184 $self->SUPER::update;
2116} 2185}
2117 2186
2123 glScale $self->{w}, $self->{h}; 2192 glScale $self->{w}, $self->{h};
2124 2193
2125 if ($self->{vertical}) { 2194 if ($self->{vertical}) {
2126 # draw a vertical slider like a rotated horizontal slider 2195 # draw a vertical slider like a rotated horizontal slider
2127 2196
2197 glTranslate 1, 0, 0;
2128 glRotate 90, 0, 0, 1; 2198 glRotate 90, 0, 0, 1;
2129 glTranslate 0, 1, 0;
2130 } 2199 }
2131 2200
2132 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2201 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2133 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2202 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2134 2203
2141 # draw handle 2210 # draw handle
2142 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1); 2211 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2143 2212
2144 glDisable GL_TEXTURE_2D; 2213 glDisable GL_TEXTURE_2D;
2145} 2214}
2215
2216#############################################################################
2217
2218package CFClient::UI::ValSlider;
2219
2220our @ISA = CFClient::UI::HBox::;
2221
2222sub new {
2223 my ($class, %arg) = @_;
2224
2225 my $range = delete $arg{range};
2226
2227 my $self = $class->SUPER::new (
2228 slider => (new CFClient::UI::Slider expand => 1, range => $range),
2229 entry => (new CFClient::UI::Label text => "", template => delete $arg{template}),
2230 to_value => sub { shift },
2231 from_value => sub { shift },
2232 %arg,
2233 );
2234
2235 $self->{slider}->connect (changed => sub {
2236 my ($self, $value) = @_;
2237 $self->{parent}{entry}->set_text ($self->{parent}{to_value}->($value));
2238 $self->{parent}->emit (changed => $value);
2239 });
2240
2241# $self->{entry}->connect (changed => sub {
2242# my ($self, $value) = @_;
2243# $self->{parent}{slider}->set_value ($self->{parent}{from_value}->($value));
2244# $self->{parent}->emit (changed => $value);
2245# });
2246
2247 $self->add ($self->{slider}, $self->{entry});
2248
2249 $self->{slider}->emit (changed => $self->{slider}{range}[0]);
2250
2251 $self
2252}
2253
2254sub set_range { shift->{slider}->set_range (@_) }
2255sub set_value { shift->{slider}->set_value (@_) }
2146 2256
2147############################################################################# 2257#############################################################################
2148 2258
2149package CFClient::UI::TextView; 2259package CFClient::UI::TextView;
2150 2260
2180 2290
2181 $self->{fontsize} = $fontsize; 2291 $self->{fontsize} = $fontsize;
2182 $self->reflow; 2292 $self->reflow;
2183} 2293}
2184 2294
2185sub text_height {
2186 my ($self, $text) = @_;
2187
2188 my $layout = $self->{layout};
2189
2190 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2191 $layout->set_width ($self->{children}[0]{w});
2192 $layout->set_markup ($text);
2193
2194 ($layout->size)[1]
2195}
2196
2197sub reflow {
2198 my ($self) = @_;
2199
2200 $self->{need_reflow}++;
2201 $self->update;
2202}
2203
2204sub size_allocate { 2295sub size_allocate {
2205 my ($self, $w, $h) = @_; 2296 my ($self, $w, $h) = @_;
2206 2297
2207 $self->SUPER::size_allocate ($w, $h); 2298 $self->SUPER::size_allocate ($w, $h);
2208 2299
2211 $self->{layout}->set_width ($self->{children}[0]{w}); 2302 $self->{layout}->set_width ($self->{children}[0]{w});
2212 2303
2213 $self->reflow; 2304 $self->reflow;
2214} 2305}
2215 2306
2307sub text_size {
2308 my ($self, $text, $indent) = @_;
2309
2310 my $layout = $self->{layout};
2311
2312 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2313 $layout->set_width ($self->{children}[0]{w} - $indent);
2314 $layout->set_markup ($text);
2315
2316 $layout->size
2317}
2318
2319sub reflow {
2320 my ($self) = @_;
2321
2322 $self->{need_reflow}++;
2323 $self->update;
2324}
2325
2326sub set_offset {
2327 my ($self, $offset) = @_;
2328
2329 # todo: base offset on lines or so, not on pixels
2330 $self->{children}[1]->set_value ($offset);
2331}
2332
2333sub clear {
2334 my ($self) = @_;
2335
2336 $self->{par} = [];
2337 $self->{height} = 0;
2338 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2339}
2340
2216sub add_paragraph { 2341sub add_paragraph {
2217 my ($self, $color, $text) = @_; 2342 my ($self, $color, $text, $indent) = @_;
2218 2343
2219 #TODO: intelligently "reformat" paragraph 2344 for my $line (split /\n/, $text) {
2220 2345 my ($w, $h) = $self->text_size ($line);
2221 my $height = $self->text_height ($text);
2222
2223 $self->{height} += $height; 2346 $self->{height} += $h;
2347 push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line];
2348 }
2224 2349
2225 push @{$self->{par}}, [$height, $color, $text];
2226
2227 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; 2350 $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]);
2228 $self->{children}[1]->update;
2229} 2351}
2230 2352
2231sub update { 2353sub update {
2232 my ($self) = @_; 2354 my ($self) = @_;
2233 2355
2236 return unless $self->{h} > 0; 2358 return unless $self->{h} > 0;
2237 2359
2238 delete $self->{texture}; 2360 delete $self->{texture};
2239 2361
2240 $ROOT->on_post_alloc ($self, sub { 2362 $ROOT->on_post_alloc ($self, sub {
2363 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2364
2241 if (delete $self->{need_reflow}) { 2365 if (delete $self->{need_reflow}) {
2242 my $height = 0; 2366 my $height = 0;
2243 2367
2244 $height += $_->[0] = $self->text_height ($_->[2]) 2368 my $layout = $self->{layout};
2369
2370 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2371
2245 for @{$self->{par}}; 2372 for (@{$self->{par}}) {
2373 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support
2374 $layout->set_width ($W - $_->[3]);
2375 $layout->set_markup ($_->[4]);
2376 my ($w, $h) = $layout->size;
2377 $_->[0] = $w + $_->[3];
2378 $_->[1] = $h;
2379 }
2380
2381 $height += $_->[1];
2382 }
2246 2383
2247 $self->{height} = $height; 2384 $self->{height} = $height;
2248 2385
2249 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2386 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]);
2250 2387
2251 delete $self->{texture}; 2388 delete $self->{texture};
2252 } 2389 }
2253 2390
2254 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2391 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2255 glClearColor 0, 0, 0, 0; 2392 glClearColor 0.5, 0.5, 0.5, 0;
2256 glClear GL_COLOR_BUFFER_BIT; 2393 glClear GL_COLOR_BUFFER_BIT;
2257 2394
2258 glEnable GL_TEXTURE_2D;
2259 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2260
2261 my $top = int $self->{children}[1]{range}[0]; 2395 my $top = int $self->{children}[1]{range}[0];
2262 2396
2263 my $y0 = $top; 2397 my $y0 = $top;
2264 my $y1 = $top + $self->{h}; 2398 my $y1 = $top + $H;
2265 2399
2266 my $y = 0; 2400 my $y = 0;
2267 2401
2268 my $layout = $self->{layout}; 2402 my $layout = $self->{layout};
2269 2403
2270 $layout->set_font ($self->{font}) if $self->{font}; 2404 $layout->set_font ($self->{font}) if $self->{font};
2271 2405
2406 glEnable GL_BLEND;
2407 #TODO# not correct in windows where rgba is forced off
2408 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2409
2272 for my $par (@{$self->{par}}) { 2410 for my $par (@{$self->{par}}) {
2273 my $h = $par->[0]; 2411 my $h = $par->[1];
2274 2412
2275 if ($y0 < $y + $h && $y < $y1) { 2413 if ($y0 < $y + $h && $y < $y1) {
2276 $layout->set_foreground (@{ $par->[1] }); 2414 $layout->set_foreground (@{ $par->[2] });
2415 $layout->set_width ($W - $par->[3]);
2277 $layout->set_markup ($par->[2]); 2416 $layout->set_markup ($par->[4]);
2278 2417
2279 my ($W, $H) = $layout->size; 2418 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2280 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); 2419
2420 glRasterPos $par->[3], $y - $y0;
2421 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2281 } 2422 }
2282 2423
2283 $y += $h; 2424 $y += $h;
2284 } 2425 }
2285 2426
2286 glDisable GL_TEXTURE_2D; 2427 glDisable GL_BLEND;
2287 }; 2428 };
2288 }); 2429 });
2289} 2430}
2290 2431
2291sub _draw { 2432sub _draw {
2292 my ($self) = @_; 2433 my ($self) = @_;
2293 2434
2294 glEnable GL_TEXTURE_2D; 2435 glEnable GL_TEXTURE_2D;
2295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2436 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2296 glColor 1, 1, 1, 1; 2437 glColor 1, 1, 1, 1;
2297 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2438 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2298 glDisable GL_TEXTURE_2D; 2439 glDisable GL_TEXTURE_2D;
2299 2440
2300 $self->{children}[1]->draw; 2441 $self->{children}[1]->draw;
2301 2442
2302} 2443}
2401 2542
2402sub set_tooltip_from { 2543sub set_tooltip_from {
2403 my ($self, $widget) = @_; 2544 my ($self, $widget) = @_;
2404 2545
2405 $self->add (new CFClient::UI::Label 2546 $self->add (new CFClient::UI::Label
2406 markup => $widget->{tooltip}, 2547 markup => $widget->{tooltip},
2407 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 2548 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2408 fontsize => 0.8, 2549 fontsize => 0.8,
2409 fg => [0, 0, 0, 1], 2550 fg => [0, 0, 0, 1],
2551 ellipsise => 0,
2410 font => ($widget->{tooltip_font} || $::FONT_PROP), 2552 font => ($widget->{tooltip_font} || $::FONT_PROP),
2411 ); 2553 );
2412} 2554}
2413 2555
2414sub size_request { 2556sub size_request {
2415 my ($self) = @_; 2557 my ($self) = @_;
2461use CFClient::OpenGL; 2603use CFClient::OpenGL;
2462 2604
2463sub new { 2605sub new {
2464 my $class = shift; 2606 my $class = shift;
2465 2607
2466 $class->SUPER::new ( 2608 my $self = $class->SUPER::new (
2467 aspect => 1, 2609 aspect => 1,
2468 @_, 2610 @_,
2469 ) 2611 );
2612
2613 if ($self->{anim} && $self->{animspeed}) {
2614 Scalar::Util::weaken (my $widget = $self);
2615
2616 $self->{timer} = Event->timer (
2617 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
2618 hard => 1,
2619 interval => $self->{animspeed},
2620 cb => sub {
2621 ++$widget->{frame};
2622 $widget->update;
2623 },
2624 );
2625 }
2626
2627 $self
2470} 2628}
2471 2629
2472sub size_request { 2630sub size_request {
2473 (32, 8) 2631 (32, 8)
2474} 2632}
2475 2633
2634sub update {
2635 my ($self) = @_;
2636
2637 return unless $self->{visible};
2638
2639 $self->SUPER::update;
2640}
2641
2476sub _draw { 2642sub _draw {
2477 my ($self) = @_; 2643 my ($self) = @_;
2478 2644
2479 return unless $::CONN;#d# manage and cache textures differently 2645 return unless $::CONN;
2646
2647 my $face;
2648
2649 if ($self->{frame}) {
2650 my $anim = $::CONN->{anim}[$self->{anim}];
2651
2652 $face = $anim->[ $self->{frame} % @$anim ]
2653 if $anim && @$anim;
2654 }
2655
2480 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2656 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2481 2657
2482 # TODO animation
2483 if ($tex) { 2658 if ($tex) {
2484 glEnable GL_TEXTURE_2D; 2659 glEnable GL_TEXTURE_2D;
2485 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2660 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2486 glColor 1, 1, 1, 1; 2661 glColor 1, 1, 1, 1;
2487 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2662 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2488 glDisable GL_TEXTURE_2D; 2663 glDisable GL_TEXTURE_2D;
2489 } 2664 }
2490} 2665}
2491 2666
2667sub DESTROY {
2668 my ($self) = @_;
2669
2670 $self->{timer}->cancel
2671 if $self->{timer};
2672
2673 $self->SUPER::DESTROY;
2674}
2675
2492############################################################################# 2676#############################################################################
2493 2677
2494package CFClient::UI::InventoryItem; 2678package CFClient::UI::InventoryItem;
2495 2679
2496our @ISA = CFClient::UI::HBox::; 2680our @ISA = CFClient::UI::HBox::;
2497 2681
2498sub new { 2682sub _item_to_desc {
2499 my $class = shift; 2683 my ($item) = @_;
2500 2684
2501 my %args = @_; 2685 my $desc =
2502
2503 my $item = delete $args{item};
2504
2505 my $desc = $item->{nrof} < 2 2686 $item->{nrof} < 2
2506 ? $item->{name} 2687 ? $item->{name}
2507 : "$item->{nrof} $item->{name_pl}"; 2688 : "$item->{nrof} × $item->{name_pl}";
2508 2689
2509 $item->{flags} & Crossfire::Protocol::F_OPEN 2690 $item->{flags} & Crossfire::Protocol::F_OPEN
2510 and $desc .= " (open)"; 2691 and $desc .= " (open)";
2511 $item->{flags} & Crossfire::Protocol::F_APPLIED 2692 $item->{flags} & Crossfire::Protocol::F_APPLIED
2512 and $desc .= " (applied)"; 2693 and $desc .= " (applied)";
2519 $item->{flags} & Crossfire::Protocol::F_DAMNED 2700 $item->{flags} & Crossfire::Protocol::F_DAMNED
2520 and $desc .= " (damned)"; 2701 and $desc .= " (damned)";
2521 $item->{flags} & Crossfire::Protocol::F_LOCKED 2702 $item->{flags} & Crossfire::Protocol::F_LOCKED
2522 and $desc .= " *"; 2703 and $desc .= " *";
2523 2704
2705 $desc
2706}
2707
2708sub new {
2709 my $class = shift;
2710
2711 my %args = @_;
2712
2713 my $item = delete $args{item};
2714
2715 my $desc = _item_to_desc ($item);
2716
2524 my $self = $class->SUPER::new ( 2717 my $self = $class->SUPER::new (
2525 can_hover => 1, 2718 can_hover => 1,
2526 can_events => 1, 2719 can_events => 1,
2527 tooltip => (CFClient::UI::Label->escape ($desc) 2720 tooltip => ((CFClient::UI::Label::escape $desc)
2528 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2721 . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"),
2529 connect_button_down => sub { 2722 connect_button_down => sub {
2530 my ($self, $ev, $x, $y) = @_; 2723 my ($self, $ev, $x, $y) = @_;
2531 2724
2532 # todo: maybe put examine on 1? but should just be a tooltip :( 2725 # todo: maybe put examine on 1? but should just be a tooltip :(
2726 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
2727 my $targ = $::CONN->{player}{tag};
2728
2729 if ($item->{container} == $::CONN->{player}{tag}) {
2730 $targ = $main::OPENCONT;
2731 }
2732
2733 $::CONN->send ("move $targ $item->{tag} 0");
2533 if ($ev->{button} == 1) { 2734 } elsif ($ev->{button} == 1) {
2534 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2735 $::CONN->send ("examine $item->{tag}");
2535 } elsif ($ev->{button} == 2) { 2736 } elsif ($ev->{button} == 2) {
2536 $::CONN->send ("apply $item->{tag}"); 2737 $::CONN->send ("apply $item->{tag}");
2537 } elsif ($ev->{button} == 3) { 2738 } elsif ($ev->{button} == 3) {
2538 my @menu_items = ( 2739 my @menu_items = (
2539 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2740 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2540 ["mark", sub { $::CONN->send ("mark $item->{tag}") }], 2741 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2541 ["apply", sub { $::CONN->send ("apply $item->{tag}") }], 2742 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2743 (
2744 $item->{flags} & Crossfire::Protocol::F_LOCKED
2745 ? (
2746 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2747 )
2748 : (
2749 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2542 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], 2750 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2543 [ 2751 )
2544 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2545 sub { $::CONN->send ("lock ". () ."$item->{tag}") },
2546 ], 2752 ),
2547 ); 2753 );
2548 2754
2549 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); 2755 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2550 } 2756 }
2551 2757
2552 1 2758 1
2553 }, 2759 },
2554 %args 2760 %args
2555 ); 2761 );
2762
2556 2763
2557 $self->add (new CFClient::UI::Face 2764 $self->add (new CFClient::UI::Face
2558 can_events => 0, 2765 can_events => 0,
2559 face => $item->{face}, 2766 face => $item->{face},
2560 anim => $item->{anim}, 2767 anim => $item->{anim},
2561 animspeed => $item->{animspeed}, 2768 animspeed => $item->{animspeed},
2562 ); 2769 );
2563 2770
2564 $self->add (new CFClient::UI::Label 2771 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2565 can_events => 0, 2772
2566 text => $desc, 2773 $self->{item} = $item;
2567 ); 2774
2775 $self->update_item;
2568 2776
2569 $self 2777 $self
2778}
2779
2780sub update_item {
2781 my ($self) = @_;
2782
2783 my $desc = _item_to_desc ($self->{item});
2784
2785 $self->{name_lbl}->set_text ($desc);
2570} 2786}
2571 2787
2572############################################################################# 2788#############################################################################
2573 2789
2574package CFClient::UI::Inventory; 2790package CFClient::UI::Inventory;
2577 2793
2578sub new { 2794sub new {
2579 my $class = shift; 2795 my $class = shift;
2580 2796
2581 my $self = $class->SUPER::new ( 2797 my $self = $class->SUPER::new (
2582 scrolled => (new CFClient::UI::VBox), 2798 scrolled => (new CFClient::UI::Table),
2583 @_, 2799 @_,
2584 ); 2800 );
2585 2801
2586 $self 2802 $self
2587} 2803}
2598 } @$items; 2814 } @$items;
2599 2815
2600 $self->{real_items} = \@items; 2816 $self->{real_items} = \@items;
2601 2817
2602 for my $item (@items) { 2818 for my $item (@items) {
2603 my $desc = $item->{nrof} < 2 2819 $item->{item} = $item;
2604 ? $item->{name}
2605 : "$item->{nrof} $item->{name_pl}";
2606
2607 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; 2820 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2821 $item->update_item ();
2608 } 2822 }
2609 2823
2824 my $i = 0;
2825 for (@items) {
2610 $self->{scrolled}->add (@items); 2826 $self->{scrolled}->add (0, $i, $_);
2827 my $nrof = $_->{item}->{nrof} || 1;
2828 $self->{scrolled}->add (1, $i++, new CFClient::UI::Label text => ($_->{item}->{weight} * $nrof) / 1000);
2829 }
2611 2830
2612# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2831# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2613} 2832}
2614 2833
2615sub size_request { 2834sub size_request {
2693 2912
2694package CFClient::UI::Statusbox; 2913package CFClient::UI::Statusbox;
2695 2914
2696our @ISA = CFClient::UI::VBox::; 2915our @ISA = CFClient::UI::VBox::;
2697 2916
2917sub new {
2918 my $class = shift;
2919
2920 $class->SUPER::new (
2921 fontsize => 0.8,
2922 @_,
2923 )
2924}
2925
2698sub reorder { 2926sub reorder {
2699 my ($self) = @_; 2927 my ($self) = @_;
2700 my $NOW = time; 2928 my $NOW = time;
2701 2929
2702 while (my ($k, $v) = each %{ $self->{item} }) { 2930 while (my ($k, $v) = each %{ $self->{item} }) {
2721 : $item->{text}; 2949 : $item->{text};
2722 2950
2723 for ($short) { 2951 for ($short) {
2724 s/^\s+//; 2952 s/^\s+//;
2725 s/\s+/ /g; 2953 s/\s+/ /g;
2726 my $len = int 40 / $item->{fontsize};
2727 substr $_, $len, length, "…" if $len < length;
2728 } 2954 }
2729 2955
2730 new CFClient::UI::Label 2956 new CFClient::UI::Label
2731 markup => $short, 2957 markup => $short,
2732 tooltip => $item->{tooltip}, 2958 tooltip => $item->{tooltip},
2733 tooltip_font => $::FONT_PROP, 2959 tooltip_font => $::FONT_PROP,
2734 tooltip_width => 0.67, 2960 tooltip_width => 0.67,
2735 fontsize => $item->{fontsize}, 2961 fontsize => $item->{fontsize} || $self->{fontsize},
2962 max_w => $::WIDTH * 0.44,
2736 fg => $item->{fg}, 2963 fg => $item->{fg},
2737 can_events => 1, 2964 can_events => 1,
2738 can_hover => 1 2965 can_hover => 1
2739 }; 2966 };
2740 } 2967 }
2767 $self->{item}{$group} = { 2994 $self->{item}{$group} = {
2768 id => ++$self->{id}, 2995 id => ++$self->{id},
2769 text => $text, 2996 text => $text,
2770 timeout => $timeout, 2997 timeout => $timeout,
2771 tooltip => $text, 2998 tooltip => $text,
2772 fontsize => 0.8,
2773 fg => [0.8, 0.8, 0.8, 0.8], 2999 fg => [0.8, 0.8, 0.8, 0.8],
2774 pri => 0, 3000 pri => 0,
2775 count => 1, 3001 count => 1,
2776 %arg, 3002 %arg,
2777 }; 3003 };
2778 } 3004 }
2779 3005
2780 $self->reorder; 3006 $self->reorder;
2781} 3007}
2782 3008
3009sub reconfigure {
3010 my ($self) = @_;
3011
3012 delete $_->{label}
3013 for values %{ $self->{item} || {} };
3014
3015 $self->reorder;
3016 $self->SUPER::reconfigure;
3017}
3018
2783############################################################################# 3019#############################################################################
2784 3020
2785package CFClient::UI::Root; 3021package CFClient::UI::Root;
2786 3022
2787our @ISA = CFClient::UI::Container::; 3023our @ISA = CFClient::UI::Container::;
2861} 3097}
2862 3098
2863sub add { 3099sub add {
2864 my ($self, @children) = @_; 3100 my ($self, @children) = @_;
2865 3101
3102 for (my @widgets = @children; my $w = pop @widgets; ) {
3103 push @widgets, $w->children;
3104 $w->{root} = $self;
3105 $w->{visible} = 1;
3106 }
3107
2866 for my $child (@children) { 3108 for my $child (@children) {
2867 $child->{toplevel} = 1; 3109 $child->{toplevel} = 1;
2868 3110
2869 # integerise window positions 3111 # integerise window positions
2870 $child->{x} = int $child->{x}; 3112 $child->{x} = int $child->{x};
2871 $child->{y} = int $child->{y}; 3113 $child->{y} = int $child->{y};
2872 } 3114 }
2873 3115
2874 $self->SUPER::add (@children); 3116 $self->SUPER::add (@children);
3117}
3118
3119sub remove {
3120 my ($self, @children) = @_;
3121
3122 $self->SUPER::remove (@children);
3123
3124 while (@children) {
3125 my $w = pop @children;
3126 push @children, $w->children;
3127 delete $w->{visible};
3128 }
2875} 3129}
2876 3130
2877sub on_refresh { 3131sub on_refresh {
2878 my ($self, $id, $cb) = @_; 3132 my ($self, $id, $cb) = @_;
2879 3133
2948 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3202 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2949 glClear GL_COLOR_BUFFER_BIT; 3203 glClear GL_COLOR_BUFFER_BIT;
2950 3204
2951 glMatrixMode GL_PROJECTION; 3205 glMatrixMode GL_PROJECTION;
2952 glLoadIdentity; 3206 glLoadIdentity;
2953 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; 3207 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
2954 glMatrixMode GL_MODELVIEW; 3208 glMatrixMode GL_MODELVIEW;
2955 glLoadIdentity; 3209 glLoadIdentity;
2956 3210
2957 $self->_draw; 3211 $self->_draw;
2958} 3212}
2960############################################################################# 3214#############################################################################
2961 3215
2962package CFClient::UI; 3216package CFClient::UI;
2963 3217
2964$ROOT = new CFClient::UI::Root; 3218$ROOT = new CFClient::UI::Root;
2965$TOOLTIP = new CFClient::UI::Tooltip; 3219$TOOLTIP = new CFClient::UI::Tooltip z => 900;
2966 3220
29671 32211
2968 3222

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines