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.195 by root, Thu May 11 23:54:31 2006 UTC vs.
Revision 1.205 by root, Mon May 15 00:15:08 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}, $widget->{tooltip_font}); 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 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
37 } 37 if $x + $TOOLTIP->{w} > $::WIDTH;
38 38
39 $TOOLTIP->move ($x, $y); 39 $TOOLTIP->move ($x, $y);
40 $TOOLTIP->check_size;
41 $TOOLTIP->update;
40 } 42 }
41 43
42 return; 44 return;
43 } 45 }
44 } 46 }
131 133
132 for (@$vals) { 134 for (@$vals) {
133 my $i = int $_ + $rem; 135 my $i = int $_ + $rem;
134 $rem += $_ - $i; 136 $rem += $_ - $i;
135 $_ = $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;
136 } 158 }
137} 159}
138 160
139############################################################################# 161#############################################################################
140 162
159 if (/^connect_(.*)$/) { 181 if (/^connect_(.*)$/) {
160 $self->connect ($1 => delete $self->{$_}); 182 $self->connect ($1 => delete $self->{$_});
161 } 183 }
162 } 184 }
163 185
186 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
187
164 $self 188 $self
165} 189}
166 190
167sub destroy { 191sub destroy {
168 my ($self) = @_; 192 my ($self) = @_;
241 265
242sub size_allocate { 266sub size_allocate {
243 # nothing to be done 267 # nothing to be done
244} 268}
245 269
270sub reconfigure {
271 my ($self) = @_;
272
273 $self->check_size (1);
274 $self->update;
275}
276
246sub children { 277sub 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} 278}
260 279
261sub set_max_size { 280sub set_max_size {
262 my ($self, $w, $h) = @_; 281 my ($self, $w, $h) = @_;
263 282
356 glVertex $self->{w}, 0; 375 glVertex $self->{w}, 0;
357 glVertex $self->{w}, $self->{h}; 376 glVertex $self->{w}, $self->{h};
358 glVertex 0 , $self->{h}; 377 glVertex 0 , $self->{h};
359 glEnd; 378 glEnd;
360 glPopMatrix; 379 glPopMatrix;
361 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;
362 } 381 }
363} 382}
364 383
365sub _draw { 384sub _draw {
366 my ($self) = @_; 385 my ($self) = @_;
383sub set_parent { 402sub set_parent {
384 my ($self, $parent) = @_; 403 my ($self, $parent) = @_;
385 404
386 Scalar::Util::weaken ($self->{parent} = $parent); 405 Scalar::Util::weaken ($self->{parent} = $parent);
387 406
407 # TODO: req_w _does_change after ->reconfigure
388 $self->check_size 408 $self->check_size
389 unless exists $self->{req_w}; 409 unless exists $self->{req_w};
390} 410}
391 411
392sub check_size { 412sub check_size {
393 my ($self) = @_; 413 my ($self, $forced) = @_;
394 414
415 $self->{force_alloc} = 1 if $forced;
395 $CFClient::UI::ROOT->{check_size}{$self} = $self; 416 $CFClient::UI::ROOT->{check_size}{$self} = $self;
396} 417}
397 418
398sub update { 419sub update {
399 my ($self) = @_; 420 my ($self) = @_;
415} 436}
416 437
417sub DESTROY { 438sub DESTROY {
418 my ($self) = @_; 439 my ($self) = @_;
419 440
441 delete $WIDGET{$self+0};
420 #$self->deactivate; 442 #$self->deactivate;
421} 443}
422 444
423############################################################################# 445#############################################################################
424 446
509 $self->{children} = [ 531 $self->{children} = [
510 sort { $a->{z} <=> $b->{z} } 532 sort { $a->{z} <=> $b->{z} }
511 @{$self->{children}}, @widgets 533 @{$self->{children}}, @widgets
512 ]; 534 ];
513 535
514 $self->check_size; 536 $self->check_size (1);
515 $self->update; 537 $self->update;
516} 538}
517 539
518sub children { 540sub children {
519 @{ $_[0]{children} } 541 @{ $_[0]{children} }
626} 648}
627 649
628sub update { 650sub update {
629 my ($self) = @_; 651 my ($self) = @_;
630 652
631 $ROOT->on_refresh ($self => sub { $self->render_child }); 653 $ROOT->on_post_alloc ($self => sub { $self->render_child });
632 $self->SUPER::update; 654 $self->SUPER::update;
633} 655}
634 656
635sub size_allocate { 657sub size_allocate {
636 my ($self, $w, $h) = @_; 658 my ($self, $w, $h) = @_;
774 $self->add ($self->{slider}); 796 $self->add ($self->{slider});
775 797
776 $self 798 $self
777} 799}
778 800
779#TODO# update range on size_allocate depeneing on child 801#TODO# update range on size_allocate depending on child
780# update viewport offset on scroll 802# update viewport offset on scroll
781 803
782############################################################################# 804#############################################################################
783 805
784package CFClient::UI::Frame; 806package CFClient::UI::Frame;
788use CFClient::OpenGL; 810use CFClient::OpenGL;
789 811
790sub new { 812sub new {
791 my $class = shift; 813 my $class = shift;
792 814
793 my $self = $class->SUPER::new ( 815 $class->SUPER::new (
794 bg => [1, 1, 1, 1], 816 bg => undef,
795 border_bg => [1, 1, 1, 1],
796 border => 0.8,
797 @_ 817 @_,
798 ); 818 )
799
800 $self
801} 819}
802 820
803sub _draw { 821sub _draw {
804 my ($self) = @_; 822 my ($self) = @_;
805 823
806 my ($w, $h) = ($self->{w}, $self->{h}); 824 if ($self->{bg}) {
825 my ($w, $h) = @$self{qw(w h)};
807 826
808 glEnable GL_BLEND; 827 glEnable GL_BLEND;
809 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 828 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
810 glEnable GL_TEXTURE_2D; 829 glColor @{ $self->{bg} };
811 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
812 830
813# glBegin GL_QUADS; 831 glBegin GL_QUADS;
814# glColor 0, 0, 0, 0;
815# glVertex 0 , 0; 832 glVertex 0 , 0;
816# glVertex 0 , $h; 833 glVertex 0 , $h;
817# glVertex $w, $h; 834 glVertex $w, $h;
818# glVertex $w, 0; 835 glVertex $w, 0;
819# glEnd; 836 glEnd;
820 837
821
822 $self->child->draw;
823 glDisable GL_BLEND; 838 glDisable GL_BLEND;
824 glDisable GL_TEXTURE_2D; 839 }
840
841 $self->SUPER::_draw;
825} 842}
826 843
827############################################################################# 844#############################################################################
828 845
829package CFClient::UI::FancyFrame; 846package CFClient::UI::FancyFrame;
843 860
844 my $self = $class->SUPER::new ( 861 my $self = $class->SUPER::new (
845 bg => [1, 1, 1, 1], 862 bg => [1, 1, 1, 1],
846 border_bg => [1, 1, 1, 1], 863 border_bg => [1, 1, 1, 1],
847 border => 0.6, 864 border => 0.6,
865 toplevel => 1,
848 can_events => 1, 866 can_events => 1,
849 @_ 867 @_
850 ); 868 );
851 869
852 $self->{title} &&= new CFClient::UI::Label 870 $self->{title} &&= new CFClient::UI::Label
945 my ($self) = @_; 963 my ($self) = @_;
946 964
947 my ($w, $h ) = ($self->{w}, $self->{h}); 965 my ($w, $h ) = ($self->{w}, $self->{h});
948 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 966 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
949 967
950 glEnable GL_BLEND;
951 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
952 glEnable GL_TEXTURE_2D; 968 glEnable GL_TEXTURE_2D;
953 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 969 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
954 970
955 my $border = $self->border; 971 my $border = $self->border;
956 972
957 glColor @{ $self->{border_bg} }; 973 glColor @{ $self->{border_bg} };
958 $tex[1]->draw_quad (0, 0, $w, $border); 974 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
959 $tex[3]->draw_quad (0, $border, $border, $ch); 975 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
960 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 976 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
961 $tex[4]->draw_quad (0, $h - $border, $w, $border); 977 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
962 978
963 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 979 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
964 my $bg = $tex[0]; 980 my $bg = $tex[0];
965 981
966 # TODO: repeat texture not scale 982 # TODO: repeat texture not scale
970 glColor @{ $self->{bg} }; 986 glColor @{ $self->{bg} };
971 987
972 $bg->{s} = $rep_x; 988 $bg->{s} = $rep_x;
973 $bg->{t} = $rep_y; 989 $bg->{t} = $rep_y;
974 $bg->{wrap_mode} = 1; 990 $bg->{wrap_mode} = 1;
975 $bg->draw_quad ($border, $border, $cw, $ch); 991 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
992 }
976 993
977 glDisable GL_TEXTURE_2D; 994 glDisable GL_TEXTURE_2D;
978 glDisable GL_BLEND;
979 }
980 995
981 $self->{title}->draw if $self->{title}; 996 $self->{title}->draw if $self->{title};
982 997
983 $self->child->draw; 998 $self->child->draw;
984} 999}
1486 my $sym = $ev->{sym}; 1501 my $sym = $ev->{sym};
1487 my $uni = $ev->{unicode}; 1502 my $uni = $ev->{unicode};
1488 1503
1489 my $text = $self->get_text; 1504 my $text = $self->get_text;
1490 1505
1491 if ($sym == 8) { 1506 if ($uni == 8) {
1492 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1507 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1493 } elsif ($sym == 127) { 1508 } elsif ($uni == 127) {
1494 substr $text, $self->{cursor}, 1, ""; 1509 substr $text, $self->{cursor}, 1, "";
1495 } elsif ($sym == CFClient::SDLK_LEFT) { 1510 } elsif ($sym == CFClient::SDLK_LEFT) {
1496 --$self->{cursor} if $self->{cursor}; 1511 --$self->{cursor} if $self->{cursor};
1497 } elsif ($sym == CFClient::SDLK_RIGHT) { 1512 } elsif ($sym == CFClient::SDLK_RIGHT) {
1498 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1513 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1499 } elsif ($sym == CFClient::SDLK_HOME) { 1514 } elsif ($sym == CFClient::SDLK_HOME) {
1500 $self->{cursor} = 0; 1515 $self->{cursor} = 0;
1501 } elsif ($sym == CFClient::SDLK_END) { 1516 } elsif ($sym == CFClient::SDLK_END) {
1502 $self->{cursor} = length $text; 1517 $self->{cursor} = length $text;
1503 } elsif ($sym == 27) { 1518 } elsif ($uni == 27) {
1504 $self->emit ('escape'); 1519 $self->emit ('escape');
1505 } elsif ($uni) { 1520 } elsif ($uni) {
1506 substr $text, $self->{cursor}++, 0, chr $uni; 1521 substr $text, $self->{cursor}++, 0, chr $uni;
1507 } 1522 }
1508 1523
1735 1750
1736 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1751 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1737 1752
1738 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1753 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1739 1754
1755 my $tex = $self->{state} ? $tex[1] : $tex[0];
1756
1740 glEnable GL_TEXTURE_2D; 1757 glEnable GL_TEXTURE_2D;
1741
1742 my $tex = $self->{state} ? $tex[1] : $tex[0];
1743
1744 $tex->draw_quad_alpha (0, 0, $s, $s); 1758 $tex->draw_quad_alpha (0, 0, $s, $s);
1745
1746 glDisable GL_TEXTURE_2D; 1759 glDisable GL_TEXTURE_2D;
1747} 1760}
1748 1761
1749############################################################################# 1762#############################################################################
1750 1763
2205 2218
2206 return unless $self->{h} > 0; 2219 return unless $self->{h} > 0;
2207 2220
2208 delete $self->{texture}; 2221 delete $self->{texture};
2209 2222
2210 $ROOT->on_refresh ($self, sub { 2223 $ROOT->on_post_alloc ($self, sub {
2211 if (delete $self->{need_reflow}) { 2224 if (delete $self->{need_reflow}) {
2212 my $height = 0; 2225 my $height = 0;
2213 2226
2214 $height += $_->[0] = $self->text_height ($_->[2]) 2227 $height += $_->[0] = $self->text_height ($_->[2])
2215 for @{$self->{par}}; 2228 for @{$self->{par}};
2367 @_, 2380 @_,
2368 can_events => 0, 2381 can_events => 0,
2369 ) 2382 )
2370} 2383}
2371 2384
2372sub set_markup { 2385sub set_tooltip_from {
2373 my ($self, $text, $font) = @_; 2386 my ($self, $widget) = @_;
2374 2387
2375 $self->{label} = new CFClient::UI::Label 2388 $self->add (new CFClient::UI::Label
2389 markup => $widget->{tooltip},
2390 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2376 fontsize => 0.8, 2391 fontsize => 0.8,
2377 fg => [0, 0, 0], 2392 fg => [0, 0, 0, 1],
2378 font => ($font || $::FONT_PROP); 2393 font => ($widget->{tooltip_font} || $::FONT_PROP),
2379 2394 );
2380 $self->{label}->set_max_size ($::WIDTH * 0.3);
2381 $self->{label}->set_markup ($text);
2382 $self->add ($self->{label});
2383} 2395}
2384 2396
2385sub size_request { 2397sub size_request {
2386 my ($self) = @_; 2398 my ($self) = @_;
2387 2399
2397} 2409}
2398 2410
2399sub _draw { 2411sub _draw {
2400 my ($self) = @_; 2412 my ($self) = @_;
2401 2413
2402 glPushMatrix;
2403 glTranslate 0.375, 0.375; 2414 glTranslate 0.375, 0.375;
2404 2415
2405 my ($w, $h) = @$self{qw(w h)}; 2416 my ($w, $h) = @$self{qw(w h)};
2406 2417
2407 glColor 1, 0.8, 0.4; 2418 glColor 1, 0.8, 0.4;
2418 glVertex 0 , $h; 2429 glVertex 0 , $h;
2419 glVertex $w, $h; 2430 glVertex $w, $h;
2420 glVertex $w, 0; 2431 glVertex $w, 0;
2421 glEnd; 2432 glEnd;
2422 2433
2423 glPopMatrix; 2434 glTranslate 2 - 0.375, 2 - 0.375;
2424
2425 glTranslate 2, 2;
2426 $self->SUPER::_draw; 2435 $self->SUPER::_draw;
2427} 2436}
2428 2437
2429############################################################################# 2438#############################################################################
2430 2439
2550 my ($self, $items) = @_; 2559 my ($self, $items) = @_;
2551 2560
2552 $self->{scrolled}->clear; 2561 $self->{scrolled}->clear;
2553 return unless $items; 2562 return unless $items;
2554 2563
2555 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2564 my @items = sort {
2565 ($a->{type} <=> $b->{type})
2566 or ($a->{name} cmp $b->{name})
2567 } @$items;
2556 2568
2557 $self->{real_items} = \@items; 2569 $self->{real_items} = \@items;
2558 2570
2559 for my $item (@items) { 2571 for my $item (@items) {
2560 my $desc = $item->{nrof} < 2 2572 my $desc = $item->{nrof} < 2
2561 ? $item->{name} 2573 ? $item->{name}
2562 : "$item->{nrof} $item->{name_pl}"; 2574 : "$item->{nrof} $item->{name_pl}";
2563 2575
2564 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2576 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2565 } 2577 }
2578
2579 $self->{scrolled}->add (@items);
2566 2580
2567# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2581# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2568} 2582}
2569 2583
2570sub size_request { 2584sub size_request {
2657 while (my ($k, $v) = each %{ $self->{item} }) { 2671 while (my ($k, $v) = each %{ $self->{item} }) {
2658 delete $self->{item}{$k} if $v->{timeout} < $NOW; 2672 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2659 } 2673 }
2660 2674
2661 my @widgets; 2675 my @widgets;
2662 my @items = sort { $a->{time} <=> $b->{time} } values %{ $self->{item} }; 2676
2677 my @items = sort {
2678 $a->{pri} <=> $b->{pri}
2679 or $b->{id} <=> $a->{id}
2680 } values %{ $self->{item} };
2681
2663 my $count = 10 + 1; 2682 my $count = 10 + 1;
2664 for my $item (@items) { 2683 for my $item (@items) {
2665 last unless --$count; 2684 last unless --$count;
2666 2685
2667 push @widgets, $item->{label} ||= do { 2686 push @widgets, $item->{label} ||= do {
2668 # TODO: doesn't handle markup well (read as: at all) 2687 # TODO: doesn't handle markup well (read as: at all)
2669 my $short = delete $item->{text}; 2688 my $short = $item->{count} > 1
2689 ? "<b>$item->{count} ×</b> $item->{text}"
2690 : $item->{text};
2691
2670 for ($short) { 2692 for ($short) {
2671 s/^\s+//; 2693 s/^\s+//;
2672 s/\012.*//s; 2694 s/\s+/ /g;
2673 my $len = int 30 / $item->{fontsize}; 2695 my $len = int 40 / $item->{fontsize};
2674 substr $_, $len, length, "…" if $len < length; 2696 substr $_, $len, length, "…" if $len < length;
2675 } 2697 }
2676 2698
2677 new CFClient::UI::Label 2699 new CFClient::UI::Label
2678 markup => $short, 2700 markup => $short,
2679 tooltip => delete $item->{tooltip}, 2701 tooltip => $item->{tooltip},
2702 tooltip_font => $::FONT_PROP,
2703 tooltip_width => 0.67,
2680 fontsize => delete $item->{fontsize}, 2704 fontsize => $item->{fontsize},
2681 color => delete $item->{color}, 2705 fg => $item->{fg},
2682 can_events => 1, 2706 can_events => 1,
2683 can_hover => 1, 2707 can_hover => 1
2684 }; 2708 };
2685 } 2709 }
2686 2710
2687 $self->clear; 2711 $self->clear;
2688 $self->SUPER::add (@widgets); 2712 $self->SUPER::add (reverse @widgets);
2689} 2713}
2690 2714
2691sub add { 2715sub add {
2692 my ($self, $text, %arg) = @_; 2716 my ($self, $text, %arg) = @_;
2693 2717
2694 my $item = { 2718 $text =~ s/^\s+//;
2695 time => time, 2719 $text =~ s/\s+$//;
2720
2721 my $timeout = time + ((delete $arg{timeout}) || 60);
2722
2723 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2724
2725 if (my $item = $self->{item}{$group}) {
2726 if ($item->{text} eq $text) {
2727 $item->{count}++;
2728 } else {
2729 $item->{count} = 1;
2730 $item->{text} = $item->{tooltip} = $text;
2731 }
2732 $item->{id} = ++$self->{id};
2733 $item->{timeout} = $timeout;
2734 delete $item->{label};
2735 } else {
2736 $self->{item}{$group} = {
2737 id => ++$self->{id},
2696 text => $text, 2738 text => $text,
2697 timeout => 60, 2739 timeout => $timeout,
2698 tooltip => $text, 2740 tooltip => $text,
2699 fontsize => 0.8, 2741 fontsize => 0.8,
2700 color => [0.8, 0.8, 0.8, 0.8], 2742 fg => [0.8, 0.8, 0.8, 0.8],
2743 pri => 0,
2744 count => 1,
2701 %arg, 2745 %arg,
2746 };
2702 }; 2747 }
2703
2704 $item->{timeout} += time;
2705 $item->{group} ||= $item+0;
2706
2707 $item = $self->{item}{$item->{group}} ||= $item;
2708 2748
2709 $self->reorder; 2749 $self->reorder;
2710} 2750}
2711 2751
2712############################################################################# 2752#############################################################################
2746} 2786}
2747 2787
2748sub size_allocate { 2788sub size_allocate {
2749 my ($self, $w, $h) = @_; 2789 my ($self, $w, $h) = @_;
2750 2790
2751 my $old_w = $self->{old_w}; 2791 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2752 my $old_h = $self->{old_h}; 2792 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2753 2793
2754 if ($old_w && $old_h) { 2794 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2755 for my $child ($self->children) { 2795 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2756 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2757 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2758 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2759 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2760 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2761 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2762 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2763 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2764 }
2765 }
2766 2796
2767 for my $child ($self->children) { 2797 for my $child ($self->children) {
2768 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2798 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2769 2799
2800 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
2801 if exists $child->{req_x};
2802
2803 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
2804 if exists $child->{req_y};
2805
2770 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2806 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2771 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2807 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
2808
2772 $child->configure ($X, $Y, $W, $H); 2809 $child->configure ($X, $Y, $W, $H);
2773 } 2810 }
2774
2775 $self->{old_w} = $w;
2776 $self->{old_h} = $h;
2777} 2811}
2778 2812
2779sub coord2local { 2813sub coord2local {
2780 my ($self, $x, $y) = @_; 2814 my ($self, $x, $y) = @_;
2781 2815
2794 $self->check_size; 2828 $self->check_size;
2795 $::WANT_REFRESH++; 2829 $::WANT_REFRESH++;
2796} 2830}
2797 2831
2798sub add { 2832sub add {
2799 my ($self, $child) = @_; 2833 my ($self, @children) = @_;
2800 2834
2835 for my $child (@children) {
2836 $child->{toplevel} = 1;
2837
2801 # integerise window positions 2838 # integerise window positions
2802 $child->{x} = int $child->{x}; 2839 $child->{x} = int $child->{x};
2803 $child->{y} = int $child->{y}; 2840 $child->{y} = int $child->{y};
2841 }
2804 2842
2805 $self->SUPER::add ($child); 2843 $self->SUPER::add (@children);
2806} 2844}
2807 2845
2808sub on_refresh { 2846sub on_refresh {
2809 my ($self, $id, $cb) = @_; 2847 my ($self, $id, $cb) = @_;
2810 2848
2811 $self->{refresh_hook}{$id} = $cb; 2849 $self->{refresh_hook}{$id} = $cb;
2812} 2850}
2813 2851
2852sub on_post_alloc {
2853 my ($self, $id, $cb) = @_;
2854
2855 $self->{post_alloc_hook}{$id} = $cb;
2856}
2857
2814sub draw { 2858sub draw {
2815 my ($self) = @_; 2859 my ($self) = @_;
2860
2861 while ($self->{refresh_hook}) {
2862 $_->()
2863 for values %{delete $self->{refresh_hook}};
2864 }
2816 2865
2817 if ($self->{check_size}) { 2866 if ($self->{check_size}) {
2818 my @queue = ([], []); 2867 my @queue = ([], []);
2819 2868
2820 for (;;) { 2869 for (;;) {
2828 2877
2829 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 2878 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2830 ? @$widget{qw(user_w user_h)} 2879 ? @$widget{qw(user_w user_h)}
2831 : $widget->size_request; 2880 : $widget->size_request;
2832 2881
2882 if (delete $widget->{force_alloc}
2833 if ($w != $widget->{req_w} || $h != $widget->{req_h}) { 2883 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2834 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 2884 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2835 2885
2836 $widget->{req_w} = $w; 2886 $widget->{req_w} = $w;
2837 $widget->{req_h} = $h; 2887 $widget->{req_h} = $h;
2838 2888
2839 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; 2889 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2840 2890
2841 $widget->{parent}->check_size 2891 $widget->{parent}->check_size
2842 if $widget->{parent}; 2892 if $widget->{parent};
2843 } 2893 }
2844 } 2894 }
2856 $widget->size_allocate ($w, $h); 2906 $widget->size_allocate ($w, $h);
2857 $widget->emit (size_allocate => $w, $h); 2907 $widget->emit (size_allocate => $w, $h);
2858 } 2908 }
2859 } 2909 }
2860 2910
2861 while ($self->{refresh_hook}) { 2911 while ($self->{post_alloc_hook}) {
2862 $_->() 2912 $_->()
2863 for values %{delete $self->{refresh_hook}}; 2913 for values %{delete $self->{post_alloc_hook}};
2864 } 2914 }
2865 2915
2866 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2916 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2867 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2917 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2868 glClear GL_COLOR_BUFFER_BIT; 2918 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines