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.196 by root, Fri May 12 00:16:34 2006 UTC vs.
Revision 1.208 by root, Mon May 15 22:52:17 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);
40 $TOOLTIP->check_size;
41 $TOOLTIP->update;
39 } 42 }
40 43
41 return; 44 return;
42 } 45 }
43 } 46 }
130 133
131 for (@$vals) { 134 for (@$vals) {
132 my $i = int $_ + $rem; 135 my $i = int $_ + $rem;
133 $rem += $_ - $i; 136 $rem += $_ - $i;
134 $_ = $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;
135 } 158 }
136} 159}
137 160
138############################################################################# 161#############################################################################
139 162
158 if (/^connect_(.*)$/) { 181 if (/^connect_(.*)$/) {
159 $self->connect ($1 => delete $self->{$_}); 182 $self->connect ($1 => delete $self->{$_});
160 } 183 }
161 } 184 }
162 185
186 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
187
163 $self 188 $self
164} 189}
165 190
166sub destroy { 191sub destroy {
167 my ($self) = @_; 192 my ($self) = @_;
240 265
241sub size_allocate { 266sub size_allocate {
242 # nothing to be done 267 # nothing to be done
243} 268}
244 269
270sub reconfigure {
271 my ($self) = @_;
272
273 $self->check_size (1);
274 $self->update;
275}
276
245sub children { 277sub children {
246}
247
248# call when resolution changes etc.
249sub reconfigure {
250 my ($self) = @_;
251
252 $_->reconfigure
253 for $self->children;
254
255 $self->check_size;
256 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}];
257 $self->update;
258} 278}
259 279
260sub set_max_size { 280sub set_max_size {
261 my ($self, $w, $h) = @_; 281 my ($self, $w, $h) = @_;
262 282
355 glVertex $self->{w}, 0; 375 glVertex $self->{w}, 0;
356 glVertex $self->{w}, $self->{h}; 376 glVertex $self->{w}, $self->{h};
357 glVertex 0 , $self->{h}; 377 glVertex 0 , $self->{h};
358 glEnd; 378 glEnd;
359 glPopMatrix; 379 glPopMatrix;
360 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 380 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
361 } 381 }
362} 382}
363 383
364sub _draw { 384sub _draw {
365 my ($self) = @_; 385 my ($self) = @_;
382sub set_parent { 402sub set_parent {
383 my ($self, $parent) = @_; 403 my ($self, $parent) = @_;
384 404
385 Scalar::Util::weaken ($self->{parent} = $parent); 405 Scalar::Util::weaken ($self->{parent} = $parent);
386 406
407 # TODO: req_w _does_change after ->reconfigure
387 $self->check_size 408 $self->check_size
388 unless exists $self->{req_w}; 409 unless exists $self->{req_w};
389} 410}
390 411
391sub check_size { 412sub check_size {
392 my ($self) = @_; 413 my ($self, $forced) = @_;
393 414
415 $self->{force_alloc} = 1 if $forced;
394 $CFClient::UI::ROOT->{check_size}{$self} = $self; 416 $CFClient::UI::ROOT->{check_size}{$self} = $self;
395} 417}
396 418
397sub update { 419sub update {
398 my ($self) = @_; 420 my ($self) = @_;
414} 436}
415 437
416sub DESTROY { 438sub DESTROY {
417 my ($self) = @_; 439 my ($self) = @_;
418 440
441 delete $WIDGET{$self+0};
419 #$self->deactivate; 442 #$self->deactivate;
420} 443}
421 444
422############################################################################# 445#############################################################################
423 446
508 $self->{children} = [ 531 $self->{children} = [
509 sort { $a->{z} <=> $b->{z} } 532 sort { $a->{z} <=> $b->{z} }
510 @{$self->{children}}, @widgets 533 @{$self->{children}}, @widgets
511 ]; 534 ];
512 535
513 $self->check_size; 536 $self->check_size (1);
514 $self->update; 537 $self->update;
515} 538}
516 539
517sub children { 540sub children {
518 @{ $_[0]{children} } 541 @{ $_[0]{children} }
625} 648}
626 649
627sub update { 650sub update {
628 my ($self) = @_; 651 my ($self) = @_;
629 652
630 $ROOT->on_refresh ($self => sub { $self->render_child }); 653 $ROOT->on_post_alloc ($self => sub { $self->render_child });
631 $self->SUPER::update; 654 $self->SUPER::update;
632} 655}
633 656
634sub size_allocate { 657sub size_allocate {
635 my ($self, $w, $h) = @_; 658 my ($self, $w, $h) = @_;
773 $self->add ($self->{slider}); 796 $self->add ($self->{slider});
774 797
775 $self 798 $self
776} 799}
777 800
778#TODO# update range on size_allocate depeneing on child 801#TODO# update range on size_allocate depending on child
779# update viewport offset on scroll 802# update viewport offset on scroll
780 803
781############################################################################# 804#############################################################################
782 805
783package CFClient::UI::Frame; 806package CFClient::UI::Frame;
787use CFClient::OpenGL; 810use CFClient::OpenGL;
788 811
789sub new { 812sub new {
790 my $class = shift; 813 my $class = shift;
791 814
792 my $self = $class->SUPER::new ( 815 $class->SUPER::new (
793 bg => [1, 1, 1, 1], 816 bg => undef,
794 border_bg => [1, 1, 1, 1],
795 border => 0.8,
796 @_ 817 @_,
797 ); 818 )
798
799 $self
800} 819}
801 820
802sub _draw { 821sub _draw {
803 my ($self) = @_; 822 my ($self) = @_;
804 823
805 my ($w, $h) = ($self->{w}, $self->{h}); 824 if ($self->{bg}) {
825 my ($w, $h) = @$self{qw(w h)};
806 826
807 glEnable GL_BLEND; 827 glEnable GL_BLEND;
808 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 828 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
809 glEnable GL_TEXTURE_2D; 829 glColor @{ $self->{bg} };
810 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
811 830
812# glBegin GL_QUADS; 831 glBegin GL_QUADS;
813# glColor 0, 0, 0, 0;
814# glVertex 0 , 0; 832 glVertex 0 , 0;
815# glVertex 0 , $h; 833 glVertex 0 , $h;
816# glVertex $w, $h; 834 glVertex $w, $h;
817# glVertex $w, 0; 835 glVertex $w, 0;
818# glEnd; 836 glEnd;
819 837
820
821 $self->child->draw;
822 glDisable GL_BLEND; 838 glDisable GL_BLEND;
823 glDisable GL_TEXTURE_2D; 839 }
840
841 $self->SUPER::_draw;
824} 842}
825 843
826############################################################################# 844#############################################################################
827 845
828package CFClient::UI::FancyFrame; 846package CFClient::UI::FancyFrame;
842 860
843 my $self = $class->SUPER::new ( 861 my $self = $class->SUPER::new (
844 bg => [1, 1, 1, 1], 862 bg => [1, 1, 1, 1],
845 border_bg => [1, 1, 1, 1], 863 border_bg => [1, 1, 1, 1],
846 border => 0.6, 864 border => 0.6,
865 toplevel => 1,
847 can_events => 1, 866 can_events => 1,
848 @_ 867 @_
849 ); 868 );
850 869
851 $self->{title} &&= new CFClient::UI::Label 870 $self->{title} &&= new CFClient::UI::Label
876 my ($self, $w, $h) = @_; 895 my ($self, $w, $h) = @_;
877 896
878 $h -= List::Util::max 0, $self->border * 2; 897 $h -= List::Util::max 0, $self->border * 2;
879 $w -= List::Util::max 0, $self->border * 2; 898 $w -= List::Util::max 0, $self->border * 2;
880 899
881 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 900 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
882 if $self->{title}; 901 if $self->{title};
883 902
884 $self->child->configure ($self->border, $self->border, $w, $h); 903 $self->child->configure ($self->border, $self->border, $w, $h);
885} 904}
886 905
944 my ($self) = @_; 963 my ($self) = @_;
945 964
946 my ($w, $h ) = ($self->{w}, $self->{h}); 965 my ($w, $h ) = ($self->{w}, $self->{h});
947 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 966 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
948 967
949 glEnable GL_BLEND;
950 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
951 glEnable GL_TEXTURE_2D; 968 glEnable GL_TEXTURE_2D;
952 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 969 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
953 970
954 my $border = $self->border; 971 my $border = $self->border;
955 972
956 glColor @{ $self->{border_bg} }; 973 glColor @{ $self->{border_bg} };
957 $tex[1]->draw_quad (0, 0, $w, $border); 974 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
958 $tex[3]->draw_quad (0, $border, $border, $ch); 975 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
959 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 976 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
960 $tex[4]->draw_quad (0, $h - $border, $w, $border); 977 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
961 978
962 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 979 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
963 my $bg = $tex[0]; 980 my $bg = $tex[0];
964 981
965 # TODO: repeat texture not scale 982 # TODO: repeat texture not scale
969 glColor @{ $self->{bg} }; 986 glColor @{ $self->{bg} };
970 987
971 $bg->{s} = $rep_x; 988 $bg->{s} = $rep_x;
972 $bg->{t} = $rep_y; 989 $bg->{t} = $rep_y;
973 $bg->{wrap_mode} = 1; 990 $bg->{wrap_mode} = 1;
974 $bg->draw_quad ($border, $border, $cw, $ch); 991 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
992 }
975 993
976 glDisable GL_TEXTURE_2D; 994 glDisable GL_TEXTURE_2D;
977 glDisable GL_BLEND;
978 }
979 995
980 $self->{title}->draw if $self->{title}; 996 $self->{title}->draw if $self->{title};
981 997
982 $self->child->draw; 998 $self->child->draw;
983} 999}
1383 $self->{layout}->set_width ($self->{w}); 1399 $self->{layout}->set_width ($self->{w});
1384 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1400 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1385 1401
1386 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1402 my $tex = new_from_layout CFClient::Texture $self->{layout};
1387 1403
1388 $self->{ox} = int $self->{align} < 0 ? $self->{padding} 1404 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1389 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1405 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1390 : ($self->{w} - $tex->{w}) * 0.5; 1406 : ($self->{w} - $tex->{w}) * 0.5);
1391 1407
1392 $self->{oy} = int $self->{valign} < 0 ? $self->{padding} 1408 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1393 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1409 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1394 : ($self->{h} - $tex->{h}) * 0.5; 1410 : ($self->{h} - $tex->{h}) * 0.5);
1395 1411
1396 $tex 1412 $tex
1397 }; 1413 };
1398 1414
1399 glEnable GL_TEXTURE_2D; 1415 glEnable GL_TEXTURE_2D;
1454sub set_text { 1470sub set_text {
1455 my ($self, $text) = @_; 1471 my ($self, $text) = @_;
1456 1472
1457 $self->{cursor} = length $text; 1473 $self->{cursor} = length $text;
1458 $self->_set_text ($text); 1474 $self->_set_text ($text);
1475 $self->update;
1459 $self->check_size; 1476 $self->check_size;
1460 $self->update;
1461} 1477}
1462 1478
1463sub get_text { 1479sub get_text {
1464 $_[0]{text} 1480 $_[0]{text}
1465} 1481}
1485 my $sym = $ev->{sym}; 1501 my $sym = $ev->{sym};
1486 my $uni = $ev->{unicode}; 1502 my $uni = $ev->{unicode};
1487 1503
1488 my $text = $self->get_text; 1504 my $text = $self->get_text;
1489 1505
1490 if ($sym == 8) { 1506 if ($uni == 8) {
1491 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1507 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1492 } elsif ($sym == 127) { 1508 } elsif ($uni == 127) {
1493 substr $text, $self->{cursor}, 1, ""; 1509 substr $text, $self->{cursor}, 1, "";
1494 } elsif ($sym == CFClient::SDLK_LEFT) { 1510 } elsif ($sym == CFClient::SDLK_LEFT) {
1495 --$self->{cursor} if $self->{cursor}; 1511 --$self->{cursor} if $self->{cursor};
1496 } elsif ($sym == CFClient::SDLK_RIGHT) { 1512 } elsif ($sym == CFClient::SDLK_RIGHT) {
1497 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1513 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1498 } elsif ($sym == CFClient::SDLK_HOME) { 1514 } elsif ($sym == CFClient::SDLK_HOME) {
1499 $self->{cursor} = 0; 1515 $self->{cursor} = 0;
1500 } elsif ($sym == CFClient::SDLK_END) { 1516 } elsif ($sym == CFClient::SDLK_END) {
1501 $self->{cursor} = length $text; 1517 $self->{cursor} = length $text;
1502 } elsif ($sym == 27) { 1518 } elsif ($uni == 27) {
1503 $self->emit ('escape'); 1519 $self->emit ('escape');
1504 } elsif ($uni) { 1520 } elsif ($uni) {
1505 substr $text, $self->{cursor}++, 0, chr $uni; 1521 substr $text, $self->{cursor}++, 0, chr $uni;
1506 } 1522 }
1507 1523
1508 $self->_set_text ($text); 1524 $self->_set_text ($text);
1509 $self->update; 1525 $self->update;
1526 $self->check_size;
1510} 1527}
1511 1528
1512sub focus_in { 1529sub focus_in {
1513 my ($self) = @_; 1530 my ($self) = @_;
1514 1531
1734 1751
1735 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1752 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1736 1753
1737 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1754 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1738 1755
1756 my $tex = $self->{state} ? $tex[1] : $tex[0];
1757
1739 glEnable GL_TEXTURE_2D; 1758 glEnable GL_TEXTURE_2D;
1740
1741 my $tex = $self->{state} ? $tex[1] : $tex[0];
1742
1743 $tex->draw_quad_alpha (0, 0, $s, $s); 1759 $tex->draw_quad_alpha (0, 0, $s, $s);
1744
1745 glDisable GL_TEXTURE_2D; 1760 glDisable GL_TEXTURE_2D;
1746} 1761}
1747 1762
1748############################################################################# 1763#############################################################################
1749 1764
1996 qw(s1_slider.png s1_slider_bg.png); 2011 qw(s1_slider.png s1_slider_bg.png);
1997 2012
1998sub new { 2013sub new {
1999 my $class = shift; 2014 my $class = shift;
2000 2015
2001 # range [value, low, high, page] 2016 # range [value, low, high, page, unit]
2002 2017
2003 # TODO: 0-width page 2018 # TODO: 0-width page
2004 # TODO: req_w/h are wrong with vertical 2019 # TODO: req_w/h are wrong with vertical
2005 # TODO: calculations are off 2020 # TODO: calculations are off
2006 my $self = $class->SUPER::new ( 2021 my $self = $class->SUPER::new (
2009 range => [0, 0, 100, 10], 2024 range => [0, 0, 100, 10],
2010 req_w => $::WIDTH / 80, 2025 req_w => $::WIDTH / 80,
2011 req_h => $::WIDTH / 80, 2026 req_h => $::WIDTH / 80,
2012 vertical => 0, 2027 vertical => 0,
2013 can_hover => 1, 2028 can_hover => 1,
2014 inner_pad => 5, 2029 inner_pad => .05,
2015 @_ 2030 @_
2016 ); 2031 );
2017 2032
2033 $self->set_value ($self->{range}[0]);
2034 $self->update;
2035
2018 $self 2036 $self
2037}
2038
2039sub set_value {
2040 my ($self, $value) = @_;
2041
2042 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2043
2044 $hi = $lo + 1 if $hi <= $lo;
2045
2046 $value = $lo if $value < $lo;
2047 $value = $hi if $value > $hi;
2048
2049 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2050 if $unit;
2051
2052 $page = $hi - $lo if $page > $hi - $lo;
2053
2054 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2055
2056 if ($value != $old_value) {
2057 $self->emit (changed => $value);
2058 $self->update;
2059 }
2019} 2060}
2020 2061
2021sub size_request { 2062sub size_request {
2022 my ($self) = @_; 2063 my ($self) = @_;
2023 2064
2036 2077
2037sub mouse_motion { 2078sub mouse_motion {
2038 my ($self, $ev, $x, $y) = @_; 2079 my ($self, $ev, $x, $y) = @_;
2039 2080
2040 if ($GRAB == $self) { 2081 if ($GRAB == $self) {
2082 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2083
2084 my (undef, $lo, $hi, $page) = @{$self->{range}};
2085
2086 $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad};
2087
2088 $self->set_value ($x * ($hi - $lo) + $lo);
2089 }
2090}
2091
2092sub update {
2093 my ($self) = @_;
2094
2095 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2096 $self->set_value ($self->{range}[0]);
2097
2041 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2098 my ($value, $lo, $hi, $page) = @{$self->{range}};
2042 2099
2043 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2100 my $inner_w = 1 - 2 * $self->{inner_pad};
2044 2101
2045 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2102 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
2046 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2047 2103
2048 $x -= $inner_pad_px; # substract the padding 2104 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
2049 $x = $x * ($hi - $lo) / $inner_w + $lo; 2105 $value = $self->{scale} * ($value - $lo);
2050 $x = $lo if $x < $lo;
2051 $x = $hi - $page if $x > $hi - $page;
2052 $self->{range}[0] = $x;
2053 2106
2054 $self->emit (changed => $x); 2107 $value = $self->{inner_pad} + ($value - $page * 0.5);
2055 $self->update; 2108
2109 $value = 0 if $value < 0;
2110 $page = 1 - $value if $value + $page > 1;
2111
2112 $self->{knob_x} = $value;
2113 $self->{knob_w} = $page;
2056 } 2114 });
2057}
2058 2115
2059# the inner_* stuff is for generating a padding for the slider handle, 2116 $self->SUPER::update;
2060# so that the handle doesn't leave the texture. This calculation isn't 100%
2061# correct propably, but it does the job for now
2062sub _calc_inner_pad_px {
2063 my ($self, $w) = @_;
2064 ($w / 100) * $self->{inner_pad} # % to pixels
2065} 2117}
2066 2118
2067sub _draw { 2119sub _draw {
2068 my ($self) = @_; 2120 my ($self) = @_;
2069 2121
2070 $self->SUPER::_draw (); 2122 $self->SUPER::_draw ();
2071 2123
2072 my ($w, $h) = @$self{qw(w h)}; 2124 glScale $self->{w}, $self->{h};
2073 2125
2074 if ($self->{vertical}) { 2126 if ($self->{vertical}) {
2075 # draw a vertical slider like a rotated horizontal slider 2127 # draw a vertical slider like a rotated horizontal slider
2076 2128
2077 glRotate 90, 0, 0, 1; 2129 glRotate 90, 0, 0, 1;
2078 glTranslate 0, -$self->{w}, 0; 2130 glTranslate 0, 1, 0;
2079
2080 ($w, $h) = ($h, $w);
2081 } 2131 }
2082 2132
2083 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2133 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2084 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2134 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2085 2135
2086 my ($value, $lo, $hi, $page) = @{$self->{range}};
2087
2088 $hi = $value + 1 if $lo == $hi;
2089
2090 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2091 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2092
2093 $page = int $page * $inner_w / ($hi - $lo);
2094 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2095
2096 $w -= $page;
2097 $page &= ~1;
2098 glTranslate $page * 0.5, 0, 0;
2099 $page ||= 2;
2100
2101 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2102 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2103
2104 glEnable GL_TEXTURE_2D; 2136 glEnable GL_TEXTURE_2D;
2105 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2137 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2106 2138
2107 # draw background 2139 # draw background
2108 $tex[1]->draw_quad_alpha (0, 0, $w, $h); 2140 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2109 2141
2110 # draw handle 2142 # draw handle
2111 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h); 2143 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2112 2144
2113 glDisable GL_TEXTURE_2D; 2145 glDisable GL_TEXTURE_2D;
2114} 2146}
2115 2147
2116############################################################################# 2148#############################################################################
2204 2236
2205 return unless $self->{h} > 0; 2237 return unless $self->{h} > 0;
2206 2238
2207 delete $self->{texture}; 2239 delete $self->{texture};
2208 2240
2209 $ROOT->on_refresh ($self, sub { 2241 $ROOT->on_post_alloc ($self, sub {
2210 if (delete $self->{need_reflow}) { 2242 if (delete $self->{need_reflow}) {
2211 my $height = 0; 2243 my $height = 0;
2212 2244
2213 $height += $_->[0] = $self->text_height ($_->[2]) 2245 $height += $_->[0] = $self->text_height ($_->[2])
2214 for @{$self->{par}}; 2246 for @{$self->{par}};
2369} 2401}
2370 2402
2371sub set_tooltip_from { 2403sub set_tooltip_from {
2372 my ($self, $widget) = @_; 2404 my ($self, $widget) = @_;
2373 2405
2374 $self->{label} = new CFClient::UI::Label 2406 $self->add (new CFClient::UI::Label
2375 markup => $widget->{tooltip}, 2407 markup => $widget->{tooltip},
2376 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 2408 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2377 fontsize => 0.8, 2409 fontsize => 0.8,
2378 fg => [0, 0, 0, 1], 2410 fg => [0, 0, 0, 1],
2379 font => ($widget->{tooltip_font} || $::FONT_PROP); 2411 font => ($widget->{tooltip_font} || $::FONT_PROP),
2380 2412 );
2381 $self->add ($self->{label});
2382} 2413}
2383 2414
2384sub size_request { 2415sub size_request {
2385 my ($self) = @_; 2416 my ($self) = @_;
2386 2417
2396} 2427}
2397 2428
2398sub _draw { 2429sub _draw {
2399 my ($self) = @_; 2430 my ($self) = @_;
2400 2431
2401 glPushMatrix;
2402 glTranslate 0.375, 0.375; 2432 glTranslate 0.375, 0.375;
2403 2433
2404 my ($w, $h) = @$self{qw(w h)}; 2434 my ($w, $h) = @$self{qw(w h)};
2405 2435
2406 glColor 1, 0.8, 0.4; 2436 glColor 1, 0.8, 0.4;
2417 glVertex 0 , $h; 2447 glVertex 0 , $h;
2418 glVertex $w, $h; 2448 glVertex $w, $h;
2419 glVertex $w, 0; 2449 glVertex $w, 0;
2420 glEnd; 2450 glEnd;
2421 2451
2422 glPopMatrix; 2452 glTranslate 2 - 0.375, 2 - 0.375;
2423
2424 glTranslate 2, 2;
2425 $self->SUPER::_draw; 2453 $self->SUPER::_draw;
2426} 2454}
2427 2455
2428############################################################################# 2456#############################################################################
2429 2457
2477 2505
2478 my $desc = $item->{nrof} < 2 2506 my $desc = $item->{nrof} < 2
2479 ? $item->{name} 2507 ? $item->{name}
2480 : "$item->{nrof} $item->{name_pl}"; 2508 : "$item->{nrof} $item->{name_pl}";
2481 2509
2510 $item->{flags} & Crossfire::Protocol::F_OPEN
2511 and $desc .= " (open)";
2512 $item->{flags} & Crossfire::Protocol::F_APPLIED
2513 and $desc .= " (applied)";
2514 $item->{flags} & Crossfire::Protocol::F_UNPAID
2515 and $desc .= " (unpaid)";
2516 $item->{flags} & Crossfire::Protocol::F_MAGIC
2517 and $desc .= " (magic)";
2518 $item->{flags} & Crossfire::Protocol::F_CURSED
2519 and $desc .= " (cursed)";
2520 $item->{flags} & Crossfire::Protocol::F_DAMNED
2521 and $desc .= " (damned)";
2522 $item->{flags} & Crossfire::Protocol::F_LOCKED
2523 and $desc .= " *";
2482 2524
2483 my $self = $class->SUPER::new ( 2525 my $self = $class->SUPER::new (
2484 can_hover => 1, 2526 can_hover => 1,
2485 can_events => 1, 2527 can_events => 1,
2486 tooltip => (CFClient::UI::Label->escape ($desc) 2528 tooltip => (CFClient::UI::Label->escape ($desc)
2492 if ($ev->{button} == 1) { 2534 if ($ev->{button} == 1) {
2493 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2535 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2494 } elsif ($ev->{button} == 2) { 2536 } elsif ($ev->{button} == 2) {
2495 $::CONN->send ("apply $item->{tag}"); 2537 $::CONN->send ("apply $item->{tag}");
2496 } elsif ($ev->{button} == 3) { 2538 } elsif ($ev->{button} == 3) {
2497 CFClient::UI::Menu->new ( 2539 my @menu_items = (
2498 items => [
2499 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2540 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2541 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2542 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2543 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2500 [ 2544 [
2501 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", 2545 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2502 sub { $::CONN->send ("lock $item->{tag}") }, 2546 sub { $::CONN->send ("lock ". () ."$item->{tag}") },
2503 ],
2504 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2505 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2506 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2507 ], 2547 ],
2508 )->popup ($ev); 2548 );
2549
2550 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2509 } 2551 }
2510 2552
2511 1 2553 1
2512 }, 2554 },
2513 %args 2555 %args
2549 my ($self, $items) = @_; 2591 my ($self, $items) = @_;
2550 2592
2551 $self->{scrolled}->clear; 2593 $self->{scrolled}->clear;
2552 return unless $items; 2594 return unless $items;
2553 2595
2554 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2596 my @items = sort {
2597 ($a->{type} <=> $b->{type})
2598 or ($a->{name} cmp $b->{name})
2599 } @$items;
2555 2600
2556 $self->{real_items} = \@items; 2601 $self->{real_items} = \@items;
2557 2602
2558 for my $item (@items) { 2603 for my $item (@items) {
2559 my $desc = $item->{nrof} < 2 2604 my $desc = $item->{nrof} < 2
2560 ? $item->{name} 2605 ? $item->{name}
2561 : "$item->{nrof} $item->{name_pl}"; 2606 : "$item->{nrof} $item->{name_pl}";
2562 2607
2563 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2608 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2564 } 2609 }
2610
2611 $self->{scrolled}->add (@items);
2565 2612
2566# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2613# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2567} 2614}
2568 2615
2569sub size_request { 2616sub size_request {
2656 while (my ($k, $v) = each %{ $self->{item} }) { 2703 while (my ($k, $v) = each %{ $self->{item} }) {
2657 delete $self->{item}{$k} if $v->{timeout} < $NOW; 2704 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2658 } 2705 }
2659 2706
2660 my @widgets; 2707 my @widgets;
2661 my @items = sort { $a->{time} <=> $b->{time} } values %{ $self->{item} }; 2708
2709 my @items = sort {
2710 $a->{pri} <=> $b->{pri}
2711 or $b->{id} <=> $a->{id}
2712 } values %{ $self->{item} };
2713
2662 my $count = 10 + 1; 2714 my $count = 10 + 1;
2663 for my $item (@items) { 2715 for my $item (@items) {
2664 last unless --$count; 2716 last unless --$count;
2665 2717
2666 push @widgets, $item->{label} ||= do { 2718 push @widgets, $item->{label} ||= do {
2667 # TODO: doesn't handle markup well (read as: at all) 2719 # TODO: doesn't handle markup well (read as: at all)
2668 my $short = delete $item->{text}; 2720 my $short = $item->{count} > 1
2721 ? "<b>$item->{count} ×</b> $item->{text}"
2722 : $item->{text};
2723
2669 for ($short) { 2724 for ($short) {
2670 s/^\s+//; 2725 s/^\s+//;
2671 s/\012.*//s; 2726 s/\s+/ /g;
2672 my $len = int 30 / $item->{fontsize}; 2727 my $len = int 40 / $item->{fontsize};
2673 substr $_, $len, length, "…" if $len < length; 2728 substr $_, $len, length, "…" if $len < length;
2674 } 2729 }
2675 2730
2676 new CFClient::UI::Label 2731 new CFClient::UI::Label
2677 markup => $short, 2732 markup => $short,
2678 tooltip => delete $item->{tooltip}, 2733 tooltip => $item->{tooltip},
2679 tooltip_font => $::FONT_PROP, 2734 tooltip_font => $::FONT_PROP,
2680 tooltip_width => 0.75, 2735 tooltip_width => 0.67,
2681 fontsize => delete $item->{fontsize}, 2736 fontsize => $item->{fontsize},
2682 color => delete $item->{color}, 2737 fg => $item->{fg},
2683 can_events => 1, 2738 can_events => 1,
2684 can_hover => 1, 2739 can_hover => 1
2685 }; 2740 };
2686 } 2741 }
2687 2742
2688 $self->clear; 2743 $self->clear;
2689 $self->SUPER::add (@widgets); 2744 $self->SUPER::add (reverse @widgets);
2690} 2745}
2691 2746
2692sub add { 2747sub add {
2693 my ($self, $text, %arg) = @_; 2748 my ($self, $text, %arg) = @_;
2694 2749
2695 my $item = { 2750 $text =~ s/^\s+//;
2696 time => time, 2751 $text =~ s/\s+$//;
2752
2753 my $timeout = time + ((delete $arg{timeout}) || 60);
2754
2755 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2756
2757 if (my $item = $self->{item}{$group}) {
2758 if ($item->{text} eq $text) {
2759 $item->{count}++;
2760 } else {
2761 $item->{count} = 1;
2762 $item->{text} = $item->{tooltip} = $text;
2763 }
2764 $item->{id} = ++$self->{id};
2765 $item->{timeout} = $timeout;
2766 delete $item->{label};
2767 } else {
2768 $self->{item}{$group} = {
2769 id => ++$self->{id},
2697 text => $text, 2770 text => $text,
2698 timeout => 60, 2771 timeout => $timeout,
2699 tooltip => $text, 2772 tooltip => $text,
2700 fontsize => 0.8, 2773 fontsize => 0.8,
2701 color => [0.8, 0.8, 0.8, 0.8], 2774 fg => [0.8, 0.8, 0.8, 0.8],
2775 pri => 0,
2776 count => 1,
2702 %arg, 2777 %arg,
2778 };
2703 }; 2779 }
2704
2705 $item->{timeout} += time;
2706 $item->{group} ||= $item+0;
2707
2708 $item = $self->{item}{$item->{group}} ||= $item;
2709 2780
2710 $self->reorder; 2781 $self->reorder;
2711} 2782}
2712 2783
2713############################################################################# 2784#############################################################################
2747} 2818}
2748 2819
2749sub size_allocate { 2820sub size_allocate {
2750 my ($self, $w, $h) = @_; 2821 my ($self, $w, $h) = @_;
2751 2822
2752 my $old_w = $self->{old_w}; 2823 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2753 my $old_h = $self->{old_h}; 2824 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2754 2825
2755 if ($old_w && $old_h) { 2826 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2756 for my $child ($self->children) { 2827 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2757 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2758 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2759 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2760 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2761 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2762 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2763 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2764 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2765 }
2766 }
2767 2828
2768 for my $child ($self->children) { 2829 for my $child ($self->children) {
2769 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2830 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2770 2831
2832 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
2833 if exists $child->{req_x};
2834
2835 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
2836 if exists $child->{req_y};
2837
2771 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2838 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2772 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2839 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
2840
2773 $child->configure ($X, $Y, $W, $H); 2841 $child->configure ($X, $Y, $W, $H);
2774 } 2842 }
2775
2776 $self->{old_w} = $w;
2777 $self->{old_h} = $h;
2778} 2843}
2779 2844
2780sub coord2local { 2845sub coord2local {
2781 my ($self, $x, $y) = @_; 2846 my ($self, $x, $y) = @_;
2782 2847
2795 $self->check_size; 2860 $self->check_size;
2796 $::WANT_REFRESH++; 2861 $::WANT_REFRESH++;
2797} 2862}
2798 2863
2799sub add { 2864sub add {
2800 my ($self, $child) = @_; 2865 my ($self, @children) = @_;
2801 2866
2867 for my $child (@children) {
2868 $child->{toplevel} = 1;
2869
2802 # integerise window positions 2870 # integerise window positions
2803 $child->{x} = int $child->{x}; 2871 $child->{x} = int $child->{x};
2804 $child->{y} = int $child->{y}; 2872 $child->{y} = int $child->{y};
2873 }
2805 2874
2806 $self->SUPER::add ($child); 2875 $self->SUPER::add (@children);
2807} 2876}
2808 2877
2809sub on_refresh { 2878sub on_refresh {
2810 my ($self, $id, $cb) = @_; 2879 my ($self, $id, $cb) = @_;
2811 2880
2812 $self->{refresh_hook}{$id} = $cb; 2881 $self->{refresh_hook}{$id} = $cb;
2813} 2882}
2814 2883
2884sub on_post_alloc {
2885 my ($self, $id, $cb) = @_;
2886
2887 $self->{post_alloc_hook}{$id} = $cb;
2888}
2889
2815sub draw { 2890sub draw {
2816 my ($self) = @_; 2891 my ($self) = @_;
2892
2893 while ($self->{refresh_hook}) {
2894 $_->()
2895 for values %{delete $self->{refresh_hook}};
2896 }
2817 2897
2818 if ($self->{check_size}) { 2898 if ($self->{check_size}) {
2819 my @queue = ([], []); 2899 my @queue = ([], []);
2820 2900
2821 for (;;) { 2901 for (;;) {
2829 2909
2830 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 2910 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2831 ? @$widget{qw(user_w user_h)} 2911 ? @$widget{qw(user_w user_h)}
2832 : $widget->size_request; 2912 : $widget->size_request;
2833 2913
2914 if (delete $widget->{force_alloc}
2834 if ($w != $widget->{req_w} || $h != $widget->{req_h}) { 2915 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2835 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 2916 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2836 2917
2837 $widget->{req_w} = $w; 2918 $widget->{req_w} = $w;
2838 $widget->{req_h} = $h; 2919 $widget->{req_h} = $h;
2839 2920
2840 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; 2921 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2841 2922
2842 $widget->{parent}->check_size 2923 $widget->{parent}->check_size
2843 if $widget->{parent}; 2924 if $widget->{parent};
2844 } 2925 }
2845 } 2926 }
2857 $widget->size_allocate ($w, $h); 2938 $widget->size_allocate ($w, $h);
2858 $widget->emit (size_allocate => $w, $h); 2939 $widget->emit (size_allocate => $w, $h);
2859 } 2940 }
2860 } 2941 }
2861 2942
2862 while ($self->{refresh_hook}) { 2943 while ($self->{post_alloc_hook}) {
2863 $_->() 2944 $_->()
2864 for values %{delete $self->{refresh_hook}}; 2945 for values %{delete $self->{post_alloc_hook}};
2865 } 2946 }
2866 2947
2867 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2948 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2868 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2949 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2869 glClear GL_COLOR_BUFFER_BIT; 2950 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines