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.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}, $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 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
1735 1751
1736 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;
1737 1753
1738 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1754 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1739 1755
1756 my $tex = $self->{state} ? $tex[1] : $tex[0];
1757
1740 glEnable GL_TEXTURE_2D; 1758 glEnable GL_TEXTURE_2D;
1741
1742 my $tex = $self->{state} ? $tex[1] : $tex[0];
1743
1744 $tex->draw_quad_alpha (0, 0, $s, $s); 1759 $tex->draw_quad_alpha (0, 0, $s, $s);
1745
1746 glDisable GL_TEXTURE_2D; 1760 glDisable GL_TEXTURE_2D;
1747} 1761}
1748 1762
1749############################################################################# 1763#############################################################################
1750 1764
2205 2219
2206 return unless $self->{h} > 0; 2220 return unless $self->{h} > 0;
2207 2221
2208 delete $self->{texture}; 2222 delete $self->{texture};
2209 2223
2210 $ROOT->on_refresh ($self, sub { 2224 $ROOT->on_post_alloc ($self, sub {
2211 if (delete $self->{need_reflow}) { 2225 if (delete $self->{need_reflow}) {
2212 my $height = 0; 2226 my $height = 0;
2213 2227
2214 $height += $_->[0] = $self->text_height ($_->[2]) 2228 $height += $_->[0] = $self->text_height ($_->[2])
2215 for @{$self->{par}}; 2229 for @{$self->{par}};
2367 @_, 2381 @_,
2368 can_events => 0, 2382 can_events => 0,
2369 ) 2383 )
2370} 2384}
2371 2385
2372sub set_markup { 2386sub set_tooltip_from {
2373 my ($self, $text, $font) = @_; 2387 my ($self, $widget) = @_;
2374 2388
2375 $self->{label} = new CFClient::UI::Label 2389 $self->add (new CFClient::UI::Label
2390 markup => $widget->{tooltip},
2391 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2376 fontsize => 0.8, 2392 fontsize => 0.8,
2377 fg => [0, 0, 0], 2393 fg => [0, 0, 0, 1],
2378 font => ($font || $::FONT_PROP); 2394 font => ($widget->{tooltip_font} || $::FONT_PROP),
2379 2395 );
2380 $self->{label}->set_max_size ($::WIDTH * 0.3);
2381 $self->{label}->set_markup ($text);
2382 $self->add ($self->{label});
2383} 2396}
2384 2397
2385sub size_request { 2398sub size_request {
2386 my ($self) = @_; 2399 my ($self) = @_;
2387 2400
2397} 2410}
2398 2411
2399sub _draw { 2412sub _draw {
2400 my ($self) = @_; 2413 my ($self) = @_;
2401 2414
2402 glPushMatrix;
2403 glTranslate 0.375, 0.375; 2415 glTranslate 0.375, 0.375;
2404 2416
2405 my ($w, $h) = @$self{qw(w h)}; 2417 my ($w, $h) = @$self{qw(w h)};
2406 2418
2407 glColor 1, 0.8, 0.4; 2419 glColor 1, 0.8, 0.4;
2418 glVertex 0 , $h; 2430 glVertex 0 , $h;
2419 glVertex $w, $h; 2431 glVertex $w, $h;
2420 glVertex $w, 0; 2432 glVertex $w, 0;
2421 glEnd; 2433 glEnd;
2422 2434
2423 glPopMatrix; 2435 glTranslate 2 - 0.375, 2 - 0.375;
2424
2425 glTranslate 2, 2;
2426 $self->SUPER::_draw; 2436 $self->SUPER::_draw;
2427} 2437}
2428 2438
2429############################################################################# 2439#############################################################################
2430 2440
2550 my ($self, $items) = @_; 2560 my ($self, $items) = @_;
2551 2561
2552 $self->{scrolled}->clear; 2562 $self->{scrolled}->clear;
2553 return unless $items; 2563 return unless $items;
2554 2564
2555 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;
2556 2569
2557 $self->{real_items} = \@items; 2570 $self->{real_items} = \@items;
2558 2571
2559 for my $item (@items) { 2572 for my $item (@items) {
2560 my $desc = $item->{nrof} < 2 2573 my $desc = $item->{nrof} < 2
2561 ? $item->{name} 2574 ? $item->{name}
2562 : "$item->{nrof} $item->{name_pl}"; 2575 : "$item->{nrof} $item->{name_pl}";
2563 2576
2564 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2577 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2565 } 2578 }
2579
2580 $self->{scrolled}->add (@items);
2566 2581
2567# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2582# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2568} 2583}
2569 2584
2570sub size_request { 2585sub size_request {
2657 while (my ($k, $v) = each %{ $self->{item} }) { 2672 while (my ($k, $v) = each %{ $self->{item} }) {
2658 delete $self->{item}{$k} if $v->{timeout} < $NOW; 2673 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2659 } 2674 }
2660 2675
2661 my @widgets; 2676 my @widgets;
2662 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
2663 my $count = 10 + 1; 2683 my $count = 10 + 1;
2664 for my $item (@items) { 2684 for my $item (@items) {
2665 last unless --$count; 2685 last unless --$count;
2666 2686
2667 push @widgets, $item->{label} ||= do { 2687 push @widgets, $item->{label} ||= do {
2668 # TODO: doesn't handle markup well (read as: at all) 2688 # TODO: doesn't handle markup well (read as: at all)
2669 my $short = delete $item->{text}; 2689 my $short = $item->{count} > 1
2690 ? "<b>$item->{count} ×</b> $item->{text}"
2691 : $item->{text};
2692
2670 for ($short) { 2693 for ($short) {
2671 s/^\s+//; 2694 s/^\s+//;
2672 s/\012.*//s; 2695 s/\012.*//s;
2673 my $len = int 30 / $item->{fontsize}; 2696 my $len = int 40 / $item->{fontsize};
2674 substr $_, $len, length, "…" if $len < length; 2697 substr $_, $len, length, "…" if $len < length;
2675 } 2698 }
2676 2699
2677 new CFClient::UI::Label 2700 new CFClient::UI::Label
2678 markup => $short, 2701 markup => $short,
2679 tooltip => delete $item->{tooltip}, 2702 tooltip => $item->{tooltip},
2703 tooltip_font => $::FONT_PROP,
2704 tooltip_width => 0.67,
2680 fontsize => delete $item->{fontsize}, 2705 fontsize => $item->{fontsize},
2681 color => delete $item->{color}, 2706 color => $item->{color},
2682 can_events => 1, 2707 can_events => 1,
2683 can_hover => 1, 2708 can_hover => 1
2684 }; 2709 };
2685 } 2710 }
2686 2711
2687 $self->clear; 2712 $self->clear;
2688 $self->SUPER::add (@widgets); 2713 $self->SUPER::add (reverse @widgets);
2689} 2714}
2690 2715
2691sub add { 2716sub add {
2692 my ($self, $text, %arg) = @_; 2717 my ($self, $text, %arg) = @_;
2693 2718
2694 my $item = { 2719 $text =~ s/^\s+//;
2695 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},
2696 text => $text, 2739 text => $text,
2697 timeout => 60, 2740 timeout => $timeout,
2698 tooltip => $text, 2741 tooltip => $text,
2699 fontsize => 0.8, 2742 fontsize => 0.8,
2700 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,
2701 %arg, 2746 %arg,
2747 };
2702 }; 2748 }
2703
2704 $item->{timeout} += time;
2705 $item->{group} ||= $item+0;
2706
2707 $item = $self->{item}{$item->{group}} ||= $item;
2708 2749
2709 $self->reorder; 2750 $self->reorder;
2710} 2751}
2711 2752
2712############################################################################# 2753#############################################################################
2746} 2787}
2747 2788
2748sub size_allocate { 2789sub size_allocate {
2749 my ($self, $w, $h) = @_; 2790 my ($self, $w, $h) = @_;
2750 2791
2751 my $old_w = $self->{old_w}; 2792 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2752 my $old_h = $self->{old_h}; 2793 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2753 2794
2754 if ($old_w && $old_h) { 2795 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2755 for my $child ($self->children) { 2796 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 2797
2767 for my $child ($self->children) { 2798 for my $child ($self->children) {
2768 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)};
2769 2800
2770 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2801 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2771 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2802 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2772 $child->configure ($X, $Y, $W, $H); 2803 $child->configure ($X, $Y, $W, $H);
2773 } 2804 }
2774
2775 $self->{old_w} = $w;
2776 $self->{old_h} = $h;
2777} 2805}
2778 2806
2779sub coord2local { 2807sub coord2local {
2780 my ($self, $x, $y) = @_; 2808 my ($self, $x, $y) = @_;
2781 2809
2794 $self->check_size; 2822 $self->check_size;
2795 $::WANT_REFRESH++; 2823 $::WANT_REFRESH++;
2796} 2824}
2797 2825
2798sub add { 2826sub add {
2799 my ($self, $child) = @_; 2827 my ($self, @children) = @_;
2800 2828
2829 for my $child (@children) {
2830 $child->{toplevel} = 1;
2831
2801 # integerise window positions 2832 # integerise window positions
2802 $child->{x} = int $child->{x}; 2833 $child->{x} = int $child->{x};
2803 $child->{y} = int $child->{y}; 2834 $child->{y} = int $child->{y};
2835 }
2804 2836
2805 $self->SUPER::add ($child); 2837 $self->SUPER::add (@children);
2806} 2838}
2807 2839
2808sub on_refresh { 2840sub on_refresh {
2809 my ($self, $id, $cb) = @_; 2841 my ($self, $id, $cb) = @_;
2810 2842
2811 $self->{refresh_hook}{$id} = $cb; 2843 $self->{refresh_hook}{$id} = $cb;
2812} 2844}
2813 2845
2846sub on_post_alloc {
2847 my ($self, $id, $cb) = @_;
2848
2849 $self->{post_alloc_hook}{$id} = $cb;
2850}
2851
2814sub draw { 2852sub draw {
2815 my ($self) = @_; 2853 my ($self) = @_;
2854
2855 while ($self->{refresh_hook}) {
2856 $_->()
2857 for values %{delete $self->{refresh_hook}};
2858 }
2816 2859
2817 if ($self->{check_size}) { 2860 if ($self->{check_size}) {
2818 my @queue = ([], []); 2861 my @queue = ([], []);
2819 2862
2820 for (;;) { 2863 for (;;) {
2828 2871
2829 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 2872 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2830 ? @$widget{qw(user_w user_h)} 2873 ? @$widget{qw(user_w user_h)}
2831 : $widget->size_request; 2874 : $widget->size_request;
2832 2875
2876 if (delete $widget->{force_alloc}
2833 if ($w != $widget->{req_w} || $h != $widget->{req_h}) { 2877 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2834 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#
2835 2879
2836 $widget->{req_w} = $w; 2880 $widget->{req_w} = $w;
2837 $widget->{req_h} = $h; 2881 $widget->{req_h} = $h;
2838 2882
2839 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; 2883 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2840 2884
2841 $widget->{parent}->check_size 2885 $widget->{parent}->check_size
2842 if $widget->{parent}; 2886 if $widget->{parent};
2843 } 2887 }
2844 } 2888 }
2856 $widget->size_allocate ($w, $h); 2900 $widget->size_allocate ($w, $h);
2857 $widget->emit (size_allocate => $w, $h); 2901 $widget->emit (size_allocate => $w, $h);
2858 } 2902 }
2859 } 2903 }
2860 2904
2861 while ($self->{refresh_hook}) { 2905 while ($self->{post_alloc_hook}) {
2862 $_->() 2906 $_->()
2863 for values %{delete $self->{refresh_hook}}; 2907 for values %{delete $self->{post_alloc_hook}};
2864 } 2908 }
2865 2909
2866 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2910 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2867 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2911 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2868 glClear GL_COLOR_BUFFER_BIT; 2912 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines