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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines