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.194 by root, Thu May 11 23:41:47 2006 UTC vs.
Revision 1.203 by root, Sun May 14 21:13:11 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}) {
24 26
25 my $tip = $widget->{tooltip}; 27 my $tip = $widget->{tooltip};
26 28
27 $tip = $tip->($widget) if CODE:: eq ref $tip; 29 $tip = $tip->($widget) if CODE:: eq ref $tip;
28 30
29 $TOOLTIP->set_markup ($widget->{tooltip}); 31 $TOOLTIP->set_tooltip_from ($widget);
30
31 $TOOLTIP->show; 32 $TOOLTIP->show;
32 33
33 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
34 35
35 if ($x + $TOOLTIP->{w} > $::WIDTH) { 36 if ($x + $TOOLTIP->{w} > $::WIDTH) {
36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 37 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0);
37 } 38 }
38 39
39 $TOOLTIP->move ($x, $y); 40 $TOOLTIP->move ($x, $y);
41 $TOOLTIP->check_size;
42 $TOOLTIP->update;
40 } 43 }
41 44
42 return; 45 return;
43 } 46 }
44 } 47 }
131 134
132 for (@$vals) { 135 for (@$vals) {
133 my $i = int $_ + $rem; 136 my $i = int $_ + $rem;
134 $rem += $_ - $i; 137 $rem += $_ - $i;
135 $_ = $i; 138 $_ = $i;
139 }
140}
141
142# call when resolution changes etc.
143sub rescale_widgets {
144 my ($sx, $sy) = @_;
145
146 for my $widget (values %WIDGET) {
147 if ($widget->{toplevel}) {
148 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
149 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
150 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
151 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
152 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
153 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
154 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
155 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
156 }
157
158 $widget->reconfigure;
136 } 159 }
137} 160}
138 161
139############################################################################# 162#############################################################################
140 163
159 if (/^connect_(.*)$/) { 182 if (/^connect_(.*)$/) {
160 $self->connect ($1 => delete $self->{$_}); 183 $self->connect ($1 => delete $self->{$_});
161 } 184 }
162 } 185 }
163 186
187 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
188
164 $self 189 $self
165} 190}
166 191
167sub destroy { 192sub destroy {
168 my ($self) = @_; 193 my ($self) = @_;
241 266
242sub size_allocate { 267sub size_allocate {
243 # nothing to be done 268 # nothing to be done
244} 269}
245 270
271sub reconfigure {
272 my ($self) = @_;
273
274 $self->check_size (1);
275 $self->update;
276}
277
246sub children { 278sub children {
247}
248
249# call when resolution changes etc.
250sub reconfigure {
251 my ($self) = @_;
252
253 $_->reconfigure
254 for $self->children;
255
256 $self->check_size;
257 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}];
258 $self->update;
259} 279}
260 280
261sub set_max_size { 281sub set_max_size {
262 my ($self, $w, $h) = @_; 282 my ($self, $w, $h) = @_;
263 283
383sub set_parent { 403sub set_parent {
384 my ($self, $parent) = @_; 404 my ($self, $parent) = @_;
385 405
386 Scalar::Util::weaken ($self->{parent} = $parent); 406 Scalar::Util::weaken ($self->{parent} = $parent);
387 407
408 # TODO: req_w _does_change after ->reconfigure
388 $self->check_size 409 $self->check_size
389 unless exists $self->{req_w}; 410 unless exists $self->{req_w};
390} 411}
391 412
392sub check_size { 413sub check_size {
393 my ($self) = @_; 414 my ($self, $forced) = @_;
394 415
416 $self->{force_alloc} = 1 if $forced;
395 $CFClient::UI::ROOT->{check_size}{$self} = $self; 417 $CFClient::UI::ROOT->{check_size}{$self} = $self;
396} 418}
397 419
398sub update { 420sub update {
399 my ($self) = @_; 421 my ($self) = @_;
415} 437}
416 438
417sub DESTROY { 439sub DESTROY {
418 my ($self) = @_; 440 my ($self) = @_;
419 441
442 delete $WIDGET{$self+0};
420 #$self->deactivate; 443 #$self->deactivate;
421} 444}
422 445
423############################################################################# 446#############################################################################
424 447
509 $self->{children} = [ 532 $self->{children} = [
510 sort { $a->{z} <=> $b->{z} } 533 sort { $a->{z} <=> $b->{z} }
511 @{$self->{children}}, @widgets 534 @{$self->{children}}, @widgets
512 ]; 535 ];
513 536
514 $self->check_size; 537 $self->check_size (1);
515 $self->update; 538 $self->update;
516} 539}
517 540
518sub children { 541sub children {
519 @{ $_[0]{children} } 542 @{ $_[0]{children} }
626} 649}
627 650
628sub update { 651sub update {
629 my ($self) = @_; 652 my ($self) = @_;
630 653
631 $ROOT->on_refresh ($self => sub { $self->render_child }); 654 $ROOT->on_post_alloc ($self => sub { $self->render_child });
632 $self->SUPER::update; 655 $self->SUPER::update;
633} 656}
634 657
635sub size_allocate { 658sub size_allocate {
636 my ($self, $w, $h) = @_; 659 my ($self, $w, $h) = @_;
774 $self->add ($self->{slider}); 797 $self->add ($self->{slider});
775 798
776 $self 799 $self
777} 800}
778 801
779#TODO# update range on size_allocate depeneing on child 802#TODO# update range on size_allocate depending on child
780# update viewport offset on scroll 803# update viewport offset on scroll
781 804
782############################################################################# 805#############################################################################
783 806
784package CFClient::UI::Frame; 807package CFClient::UI::Frame;
788use CFClient::OpenGL; 811use CFClient::OpenGL;
789 812
790sub new { 813sub new {
791 my $class = shift; 814 my $class = shift;
792 815
793 my $self = $class->SUPER::new ( 816 $class->SUPER::new (
794 bg => [1, 1, 1, 1], 817 bg => undef,
795 border_bg => [1, 1, 1, 1],
796 border => 0.8,
797 @_ 818 @_,
798 ); 819 )
799
800 $self
801} 820}
802 821
803sub _draw { 822sub _draw {
804 my ($self) = @_; 823 my ($self) = @_;
805 824
806 my ($w, $h) = ($self->{w}, $self->{h}); 825 if ($self->{bg}) {
826 my ($w, $h) = @$self{qw(w h)};
807 827
808 glEnable GL_BLEND; 828 glEnable GL_BLEND;
809 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 829 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
810 glEnable GL_TEXTURE_2D; 830 glColor @{ $self->{bg} };
811 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
812 831
813# glBegin GL_QUADS; 832 glBegin GL_QUADS;
814# glColor 0, 0, 0, 0;
815# glVertex 0 , 0; 833 glVertex 0 , 0;
816# glVertex 0 , $h; 834 glVertex 0 , $h;
817# glVertex $w, $h; 835 glVertex $w, $h;
818# glVertex $w, 0; 836 glVertex $w, 0;
819# glEnd; 837 glEnd;
820 838
821
822 $self->child->draw;
823 glDisable GL_BLEND; 839 glDisable GL_BLEND;
824 glDisable GL_TEXTURE_2D; 840 }
841
842 $self->SUPER::_draw;
825} 843}
826 844
827############################################################################# 845#############################################################################
828 846
829package CFClient::UI::FancyFrame; 847package CFClient::UI::FancyFrame;
843 861
844 my $self = $class->SUPER::new ( 862 my $self = $class->SUPER::new (
845 bg => [1, 1, 1, 1], 863 bg => [1, 1, 1, 1],
846 border_bg => [1, 1, 1, 1], 864 border_bg => [1, 1, 1, 1],
847 border => 0.6, 865 border => 0.6,
866 toplevel => 1,
848 can_events => 1, 867 can_events => 1,
849 @_ 868 @_
850 ); 869 );
851 870
852 $self->{title} &&= new CFClient::UI::Label 871 $self->{title} &&= new CFClient::UI::Label
945 my ($self) = @_; 964 my ($self) = @_;
946 965
947 my ($w, $h ) = ($self->{w}, $self->{h}); 966 my ($w, $h ) = ($self->{w}, $self->{h});
948 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 967 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
949 968
950 glEnable GL_BLEND;
951 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
952 glEnable GL_TEXTURE_2D; 969 glEnable GL_TEXTURE_2D;
953 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 970 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
954 971
955 my $border = $self->border; 972 my $border = $self->border;
956 973
957 glColor @{ $self->{border_bg} }; 974 glColor @{ $self->{border_bg} };
958 $tex[1]->draw_quad (0, 0, $w, $border); 975 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
959 $tex[3]->draw_quad (0, $border, $border, $ch); 976 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
960 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 977 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
961 $tex[4]->draw_quad (0, $h - $border, $w, $border); 978 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
962 979
963 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 980 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
964 my $bg = $tex[0]; 981 my $bg = $tex[0];
965 982
966 # TODO: repeat texture not scale 983 # TODO: repeat texture not scale
970 glColor @{ $self->{bg} }; 987 glColor @{ $self->{bg} };
971 988
972 $bg->{s} = $rep_x; 989 $bg->{s} = $rep_x;
973 $bg->{t} = $rep_y; 990 $bg->{t} = $rep_y;
974 $bg->{wrap_mode} = 1; 991 $bg->{wrap_mode} = 1;
975 $bg->draw_quad ($border, $border, $cw, $ch); 992 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
993 }
976 994
977 glDisable GL_TEXTURE_2D; 995 glDisable GL_TEXTURE_2D;
978 glDisable GL_BLEND;
979 }
980 996
981 $self->{title}->draw if $self->{title}; 997 $self->{title}->draw if $self->{title};
982 998
983 $self->child->draw; 999 $self->child->draw;
984} 1000}
1486 my $sym = $ev->{sym}; 1502 my $sym = $ev->{sym};
1487 my $uni = $ev->{unicode}; 1503 my $uni = $ev->{unicode};
1488 1504
1489 my $text = $self->get_text; 1505 my $text = $self->get_text;
1490 1506
1491 if ($sym == 8) { 1507 if ($uni == 8) {
1492 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1508 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1493 } elsif ($sym == 127) { 1509 } elsif ($uni == 127) {
1494 substr $text, $self->{cursor}, 1, ""; 1510 substr $text, $self->{cursor}, 1, "";
1495 } elsif ($sym == CFClient::SDLK_LEFT) { 1511 } elsif ($sym == CFClient::SDLK_LEFT) {
1496 --$self->{cursor} if $self->{cursor}; 1512 --$self->{cursor} if $self->{cursor};
1497 } elsif ($sym == CFClient::SDLK_RIGHT) { 1513 } elsif ($sym == CFClient::SDLK_RIGHT) {
1498 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1514 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1499 } elsif ($sym == CFClient::SDLK_HOME) { 1515 } elsif ($sym == CFClient::SDLK_HOME) {
1500 $self->{cursor} = 0; 1516 $self->{cursor} = 0;
1501 } elsif ($sym == CFClient::SDLK_END) { 1517 } elsif ($sym == CFClient::SDLK_END) {
1502 $self->{cursor} = length $text; 1518 $self->{cursor} = length $text;
1503 } elsif ($sym == 27) { 1519 } elsif ($uni == 27) {
1504 $self->emit ('escape'); 1520 $self->emit ('escape');
1505 } elsif ($uni) { 1521 } elsif ($uni) {
1506 substr $text, $self->{cursor}++, 0, chr $uni; 1522 substr $text, $self->{cursor}++, 0, chr $uni;
1507 } 1523 }
1508 1524
1672 1688
1673 if ($GRAB == $self) { 1689 if ($GRAB == $self) {
1674 $self->{fg} = $self->{active_fg}; 1690 $self->{fg} = $self->{active_fg};
1675 } 1691 }
1676 1692
1677 glEnable GL_BLEND;
1678 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1679 glEnable GL_TEXTURE_2D; 1693 glEnable GL_TEXTURE_2D;
1680 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1694 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1681 glColor 0, 0, 0, 1; 1695 glColor 0, 0, 0, 1;
1682 1696
1683 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1697 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1684 1698
1685 glDisable GL_TEXTURE_2D; 1699 glDisable GL_TEXTURE_2D;
1686 glDisable GL_BLEND;
1687 1700
1688 $self->SUPER::_draw; 1701 $self->SUPER::_draw;
1689} 1702}
1690 1703
1691############################################################################# 1704#############################################################################
1738 1751
1739 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;
1740 1753
1741 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1754 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1742 1755
1743 glEnable GL_BLEND; 1756 my $tex = $self->{state} ? $tex[1] : $tex[0];
1757
1744 glEnable GL_TEXTURE_2D; 1758 glEnable GL_TEXTURE_2D;
1745 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1746
1747 my $tex = $self->{state} ? $tex[1] : $tex[0];
1748
1749 $tex->draw_quad (0, 0, $s, $s); 1759 $tex->draw_quad_alpha (0, 0, $s, $s);
1750
1751 glDisable GL_TEXTURE_2D; 1760 glDisable GL_TEXTURE_2D;
1752 glDisable GL_BLEND;
1753} 1761}
1754 1762
1755############################################################################# 1763#############################################################################
1756 1764
1757package CFClient::UI::Image; 1765package CFClient::UI::Image;
1800 glTranslate 0, -$self->{w}, 0; 1808 glTranslate 0, -$self->{w}, 0;
1801 1809
1802 ($w, $h) = ($h, $w); 1810 ($w, $h) = ($h, $w);
1803 } 1811 }
1804 1812
1805 glEnable GL_BLEND;
1806 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1807 glEnable GL_TEXTURE_2D; 1813 glEnable GL_TEXTURE_2D;
1808 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1814 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1809 1815
1810 $tex->draw_quad (0, 0, $w, $h); 1816 $tex->draw_quad_alpha (0, 0, $w, $h);
1811 1817
1812 glDisable GL_BLEND;
1813 glDisable GL_TEXTURE_2D; 1818 glDisable GL_TEXTURE_2D;
1814} 1819}
1815 1820
1816############################################################################# 1821#############################################################################
1817 1822
2109 $page ||= 2; 2114 $page ||= 2;
2110 2115
2111 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2116 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2112 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2117 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2113 2118
2114 glEnable GL_BLEND;
2115 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2116 glEnable GL_TEXTURE_2D; 2119 glEnable GL_TEXTURE_2D;
2117 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2120 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2118 2121
2119 # draw background 2122 # draw background
2120 $tex[1]->draw_quad (0, 0, $w, $h); 2123 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2121 2124
2122 # draw handle 2125 # draw handle
2123 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2126 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h);
2124 2127
2125 glDisable GL_BLEND;
2126 glDisable GL_TEXTURE_2D; 2128 glDisable GL_TEXTURE_2D;
2127} 2129}
2128 2130
2129############################################################################# 2131#############################################################################
2130 2132
2141 fontsize => 1, 2143 fontsize => 1,
2142 can_events => 0, 2144 can_events => 0,
2143 #font => default_font 2145 #font => default_font
2144 @_, 2146 @_,
2145 2147
2146 layout => (new CFClient::Layout), 2148 layout => (new CFClient::Layout 1),
2147 par => [], 2149 par => [],
2148 height => 0, 2150 height => 0,
2149 children => [ 2151 children => [
2150 (new CFClient::UI::Empty expand => 1), 2152 (new CFClient::UI::Empty expand => 1),
2151 (new CFClient::UI::Slider vertical => 1), 2153 (new CFClient::UI::Slider vertical => 1),
2169 2171
2170 my $layout = $self->{layout}; 2172 my $layout = $self->{layout};
2171 2173
2172 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2174 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2173 $layout->set_width ($self->{children}[0]{w}); 2175 $layout->set_width ($self->{children}[0]{w});
2174 $layout->set_text ($text); 2176 $layout->set_markup ($text);
2175 2177
2176 ($layout->size)[1] 2178 ($layout->size)[1]
2177} 2179}
2178 2180
2179sub reflow { 2181sub reflow {
2217 2219
2218 return unless $self->{h} > 0; 2220 return unless $self->{h} > 0;
2219 2221
2220 delete $self->{texture}; 2222 delete $self->{texture};
2221 2223
2222 $ROOT->on_refresh ($self, sub { 2224 $ROOT->on_post_alloc ($self, sub {
2223 if (delete $self->{need_reflow}) { 2225 if (delete $self->{need_reflow}) {
2224 my $height = 0; 2226 my $height = 0;
2225 2227
2226 $height += $_->[0] = $self->text_height ($_->[2]) 2228 $height += $_->[0] = $self->text_height ($_->[2])
2227 for @{$self->{par}}; 2229 for @{$self->{par}};
2235 2237
2236 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2238 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2237 glClearColor 0, 0, 0, 0; 2239 glClearColor 0, 0, 0, 0;
2238 glClear GL_COLOR_BUFFER_BIT; 2240 glClear GL_COLOR_BUFFER_BIT;
2239 2241
2240 glEnable GL_BLEND;
2241 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2242 glEnable GL_TEXTURE_2D; 2242 glEnable GL_TEXTURE_2D;
2243 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2243 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2244 2244
2245 my $top = int $self->{children}[1]{range}[0]; 2245 my $top = int $self->{children}[1]{range}[0];
2246 2246
2255 2255
2256 for my $par (@{$self->{par}}) { 2256 for my $par (@{$self->{par}}) {
2257 my $h = $par->[0]; 2257 my $h = $par->[0];
2258 2258
2259 if ($y0 < $y + $h && $y < $y1) { 2259 if ($y0 < $y + $h && $y < $y1) {
2260 $layout->set_foreground (@{ $par->[1] });
2260 $layout->set_text ($par->[2]); 2261 $layout->set_markup ($par->[2]);
2261 2262
2262 glColor @{ $par->[1] };
2263 my ($W, $H) = $layout->size; 2263 my ($W, $H) = $layout->size;
2264 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2264 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2265 } 2265 }
2266 2266
2267 $y += $h; 2267 $y += $h;
2268 } 2268 }
2269 2269
2270 glDisable GL_TEXTURE_2D; 2270 glDisable GL_TEXTURE_2D;
2271 glDisable GL_BLEND;
2272 }; 2271 };
2273 }); 2272 });
2274} 2273}
2275 2274
2276sub _draw { 2275sub _draw {
2277 my ($self) = @_; 2276 my ($self) = @_;
2278 2277
2279 glEnable GL_BLEND;
2280 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2281 glEnable GL_TEXTURE_2D; 2278 glEnable GL_TEXTURE_2D;
2282 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2279 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2283 glColor 1, 1, 1, 1; 2280 glColor 1, 1, 1, 1;
2284 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2281 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2285 glDisable GL_TEXTURE_2D; 2282 glDisable GL_TEXTURE_2D;
2286 glDisable GL_BLEND;
2287 2283
2288 $self->{children}[1]->draw; 2284 $self->{children}[1]->draw;
2289 2285
2290} 2286}
2291 2287
2385 @_, 2381 @_,
2386 can_events => 0, 2382 can_events => 0,
2387 ) 2383 )
2388} 2384}
2389 2385
2390sub set_markup { 2386sub set_tooltip_from {
2391 my ($self, $text) = @_; 2387 my ($self, $widget) = @_;
2392 2388
2393 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2389 $self->add (new CFClient::UI::Label
2394 $self->{label}->set_markup ($text); 2390 markup => $widget->{tooltip},
2395 $self->add ($self->{label}); 2391 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2392 fontsize => 0.8,
2393 fg => [0, 0, 0, 1],
2394 font => ($widget->{tooltip_font} || $::FONT_PROP),
2395 );
2396} 2396}
2397 2397
2398sub size_request { 2398sub size_request {
2399 my ($self) = @_; 2399 my ($self) = @_;
2400
2401 $self->child->set_max_size ($::WIDTH * 0.3);
2402 2400
2403 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2401 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2404 2402
2405 ($w + 4, $h + 4) 2403 ($w + 4, $h + 4)
2406} 2404}
2412} 2410}
2413 2411
2414sub _draw { 2412sub _draw {
2415 my ($self) = @_; 2413 my ($self) = @_;
2416 2414
2417 glPushMatrix;
2418 glTranslate 0.375, 0.375; 2415 glTranslate 0.375, 0.375;
2419 2416
2420 my ($w, $h) = @$self{qw(w h)}; 2417 my ($w, $h) = @$self{qw(w h)};
2421 2418
2422 glColor 1, 0.8, 0.4; 2419 glColor 1, 0.8, 0.4;
2433 glVertex 0 , $h; 2430 glVertex 0 , $h;
2434 glVertex $w, $h; 2431 glVertex $w, $h;
2435 glVertex $w, 0; 2432 glVertex $w, 0;
2436 glEnd; 2433 glEnd;
2437 2434
2438 glPopMatrix; 2435 glTranslate 2 - 0.375, 2 - 0.375;
2439
2440 glTranslate 2, 2;
2441 $self->SUPER::_draw; 2436 $self->SUPER::_draw;
2442} 2437}
2443 2438
2444############################################################################# 2439#############################################################################
2445 2440
2468 return unless $::CONN;#d# manage and cache textures differently 2463 return unless $::CONN;#d# manage and cache textures differently
2469 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2464 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2470 2465
2471 # TODO animation 2466 # TODO animation
2472 if ($tex) { 2467 if ($tex) {
2473 glEnable GL_BLEND;
2474 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2475 glEnable GL_TEXTURE_2D; 2468 glEnable GL_TEXTURE_2D;
2476 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2469 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2477 glColor 1, 1, 1, 1; 2470 glColor 1, 1, 1, 1;
2478 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2471 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2479 glDisable GL_TEXTURE_2D; 2472 glDisable GL_TEXTURE_2D;
2480 glDisable GL_BLEND;
2481 } 2473 }
2482} 2474}
2483 2475
2484############################################################################# 2476#############################################################################
2485 2477
2568 my ($self, $items) = @_; 2560 my ($self, $items) = @_;
2569 2561
2570 $self->{scrolled}->clear; 2562 $self->{scrolled}->clear;
2571 return unless $items; 2563 return unless $items;
2572 2564
2573 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2565 my @items = sort {
2566 ($a->{type} <=> $b->{type})
2567 or ($a->{name} cmp $b->{name})
2568 } @$items;
2574 2569
2575 $self->{real_items} = \@items; 2570 $self->{real_items} = \@items;
2576 2571
2577 for my $item (@items) { 2572 for my $item (@items) {
2578 my $desc = $item->{nrof} < 2 2573 my $desc = $item->{nrof} < 2
2579 ? $item->{name} 2574 ? $item->{name}
2580 : "$item->{nrof} $item->{name_pl}"; 2575 : "$item->{nrof} $item->{name_pl}";
2581 2576
2582 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2577 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2583 } 2578 }
2579
2580 $self->{scrolled}->add (@items);
2584 2581
2585# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2582# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2586} 2583}
2587 2584
2588sub size_request { 2585sub size_request {
2675 while (my ($k, $v) = each %{ $self->{item} }) { 2672 while (my ($k, $v) = each %{ $self->{item} }) {
2676 delete $self->{item}{$k} if $v->{timeout} < $NOW; 2673 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2677 } 2674 }
2678 2675
2679 my @widgets; 2676 my @widgets;
2680 my @items = sort { $a->{time} <=> $b->{time} } values %{ $self->{item} }; 2677
2678 my @items = sort {
2679 $a->{pri} <=> $b->{pri}
2680 or $b->{id} <=> $a->{id}
2681 } values %{ $self->{item} };
2682
2681 my $count = 10 + 1; 2683 my $count = 10 + 1;
2682 for my $item (@items) { 2684 for my $item (@items) {
2683 last unless --$count; 2685 last unless --$count;
2684 2686
2685 push @widgets, $item->{label} ||= do { 2687 push @widgets, $item->{label} ||= do {
2686 # TODO: doesn't handle markup well (read as: at all) 2688 # TODO: doesn't handle markup well (read as: at all)
2687 my $short = delete $item->{text}; 2689 my $short = $item->{count} > 1
2690 ? "<b>$item->{count} ×</b> $item->{text}"
2691 : $item->{text};
2692
2688 for ($short) { 2693 for ($short) {
2689 s/^\s+//; 2694 s/^\s+//;
2690 s/\012.*//s; 2695 s/\012.*//s;
2691 my $len = int 30 / $item->{fontsize}; 2696 my $len = int 40 / $item->{fontsize};
2692 substr $_, $len, length, "…" if $len < length; 2697 substr $_, $len, length, "…" if $len < length;
2693 } 2698 }
2694 2699
2695 new CFClient::UI::Label 2700 new CFClient::UI::Label
2696 markup => $short, 2701 markup => $short,
2697 tooltip => delete $item->{tooltip}, 2702 tooltip => $item->{tooltip},
2703 tooltip_font => $::FONT_PROP,
2704 tooltip_width => 0.67,
2698 fontsize => delete $item->{fontsize}, 2705 fontsize => $item->{fontsize},
2699 color => delete $item->{color}, 2706 color => $item->{color},
2700 can_events => 1, 2707 can_events => 1,
2701 can_hover => 1, 2708 can_hover => 1
2702 }; 2709 };
2703 } 2710 }
2704 2711
2705 $self->clear; 2712 $self->clear;
2706 $self->SUPER::add (@widgets); 2713 $self->SUPER::add (reverse @widgets);
2707} 2714}
2708 2715
2709sub add { 2716sub add {
2710 my ($self, $text, %arg) = @_; 2717 my ($self, $text, %arg) = @_;
2711 2718
2712 my $item = { 2719 $text =~ s/^\s+//;
2713 time => time, 2720 $text =~ s/\s+$//;
2721
2722 my $timeout = time + ((delete $arg{timeout}) || 60);
2723
2724 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2725
2726 if (my $item = $self->{item}{$group}) {
2727 if ($item->{text} eq $text) {
2728 $item->{count}++;
2729 } else {
2730 $item->{count} = 1;
2731 $item->{text} = $item->{tooltip} = $text;
2732 }
2733 $item->{id} = ++$self->{id};
2734 $item->{timeout} = $timeout;
2735 delete $item->{label};
2736 } else {
2737 $self->{item}{$group} = {
2738 id => ++$self->{id},
2714 text => $text, 2739 text => $text,
2715 timeout => 60, 2740 timeout => $timeout,
2716 tooltip => $text, 2741 tooltip => $text,
2717 fontsize => 0.8, 2742 fontsize => 0.8,
2718 color => [0.8, 0.8, 0.8, 0.8], 2743 color => [0.8, 0.8, 0.8, 0.8],
2744 pri => 0,
2745 count => 1,
2719 %arg, 2746 %arg,
2747 };
2720 }; 2748 }
2721
2722 $item->{timeout} += time;
2723 $item->{group} ||= $item+0;
2724
2725 $item = $self->{item}{$item->{group}} ||= $item;
2726 2749
2727 $self->reorder; 2750 $self->reorder;
2728} 2751}
2729 2752
2730############################################################################# 2753#############################################################################
2764} 2787}
2765 2788
2766sub size_allocate { 2789sub size_allocate {
2767 my ($self, $w, $h) = @_; 2790 my ($self, $w, $h) = @_;
2768 2791
2769 my $old_w = $self->{old_w}; 2792 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2770 my $old_h = $self->{old_h}; 2793 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2771 2794
2772 if ($old_w && $old_h) { 2795 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2773 for my $child ($self->children) { 2796 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2774 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2775 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2776 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2777 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2778 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2779 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2780 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2781 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2782 }
2783 }
2784 2797
2785 for my $child ($self->children) { 2798 for my $child ($self->children) {
2786 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2799 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2787 2800
2788 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2801 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2789 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2802 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2790 $child->configure ($X, $Y, $W, $H); 2803 $child->configure ($X, $Y, $W, $H);
2791 } 2804 }
2792
2793 $self->{old_w} = $w;
2794 $self->{old_h} = $h;
2795} 2805}
2796 2806
2797sub coord2local { 2807sub coord2local {
2798 my ($self, $x, $y) = @_; 2808 my ($self, $x, $y) = @_;
2799 2809
2812 $self->check_size; 2822 $self->check_size;
2813 $::WANT_REFRESH++; 2823 $::WANT_REFRESH++;
2814} 2824}
2815 2825
2816sub add { 2826sub add {
2817 my ($self, $child) = @_; 2827 my ($self, @children) = @_;
2818 2828
2829 for my $child (@children) {
2830 $child->{toplevel} = 1;
2831
2819 # integerise window positions 2832 # integerise window positions
2820 $child->{x} = int $child->{x}; 2833 $child->{x} = int $child->{x};
2821 $child->{y} = int $child->{y}; 2834 $child->{y} = int $child->{y};
2835 }
2822 2836
2823 $self->SUPER::add ($child); 2837 $self->SUPER::add (@children);
2824} 2838}
2825 2839
2826sub on_refresh { 2840sub on_refresh {
2827 my ($self, $id, $cb) = @_; 2841 my ($self, $id, $cb) = @_;
2828 2842
2829 $self->{refresh_hook}{$id} = $cb; 2843 $self->{refresh_hook}{$id} = $cb;
2830} 2844}
2831 2845
2846sub on_post_alloc {
2847 my ($self, $id, $cb) = @_;
2848
2849 $self->{post_alloc_hook}{$id} = $cb;
2850}
2851
2832sub draw { 2852sub draw {
2833 my ($self) = @_; 2853 my ($self) = @_;
2854
2855 while ($self->{refresh_hook}) {
2856 $_->()
2857 for values %{delete $self->{refresh_hook}};
2858 }
2834 2859
2835 if ($self->{check_size}) { 2860 if ($self->{check_size}) {
2836 my @queue = ([], []); 2861 my @queue = ([], []);
2837 2862
2838 for (;;) { 2863 for (;;) {
2846 2871
2847 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 2872 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2848 ? @$widget{qw(user_w user_h)} 2873 ? @$widget{qw(user_w user_h)}
2849 : $widget->size_request; 2874 : $widget->size_request;
2850 2875
2876 if (delete $widget->{force_alloc}
2851 if ($w != $widget->{req_w} || $h != $widget->{req_h}) { 2877 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2852 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 2878 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2853 2879
2854 $widget->{req_w} = $w; 2880 $widget->{req_w} = $w;
2855 $widget->{req_h} = $h; 2881 $widget->{req_h} = $h;
2856 2882
2857 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; 2883 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2858 2884
2859 $widget->{parent}->check_size 2885 $widget->{parent}->check_size
2860 if $widget->{parent}; 2886 if $widget->{parent};
2861 } 2887 }
2862 } 2888 }
2874 $widget->size_allocate ($w, $h); 2900 $widget->size_allocate ($w, $h);
2875 $widget->emit (size_allocate => $w, $h); 2901 $widget->emit (size_allocate => $w, $h);
2876 } 2902 }
2877 } 2903 }
2878 2904
2879 while ($self->{refresh_hook}) { 2905 while ($self->{post_alloc_hook}) {
2880 $_->() 2906 $_->()
2881 for values %{delete $self->{refresh_hook}}; 2907 for values %{delete $self->{post_alloc_hook}};
2882 } 2908 }
2883 2909
2884 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2910 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2885 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2911 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2886 glClear GL_COLOR_BUFFER_BIT; 2912 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines