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.197 by root, Fri May 12 01:47:04 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}) {
132 134
133 for (@$vals) { 135 for (@$vals) {
134 my $i = int $_ + $rem; 136 my $i = int $_ + $rem;
135 $rem += $_ - $i; 137 $rem += $_ - $i;
136 $_ = $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;
137 } 159 }
138} 160}
139 161
140############################################################################# 162#############################################################################
141 163
160 if (/^connect_(.*)$/) { 182 if (/^connect_(.*)$/) {
161 $self->connect ($1 => delete $self->{$_}); 183 $self->connect ($1 => delete $self->{$_});
162 } 184 }
163 } 185 }
164 186
187 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
188
165 $self 189 $self
166} 190}
167 191
168sub destroy { 192sub destroy {
169 my ($self) = @_; 193 my ($self) = @_;
175sub show { 199sub show {
176 my ($self) = @_; 200 my ($self) = @_;
177 201
178 return if $self->{parent}; 202 return if $self->{parent};
179 203
204 $self->{toplevel} = 1;
180 $CFClient::UI::ROOT->add ($self); 205 $CFClient::UI::ROOT->add ($self);
181} 206}
182 207
183sub hide { 208sub hide {
184 my ($self) = @_; 209 my ($self) = @_;
242 267
243sub size_allocate { 268sub size_allocate {
244 # nothing to be done 269 # nothing to be done
245} 270}
246 271
272sub reconfigure {
273 my ($self) = @_;
274
275 $self->check_size (1);
276 $self->update;
277}
278
247sub children { 279sub children {
248}
249
250# call when resolution changes etc.
251sub reconfigure {
252 my ($self) = @_;
253
254 $_->reconfigure
255 for $self->children;
256
257 $self->check_size;
258 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}];
259 $self->update;
260} 280}
261 281
262sub set_max_size { 282sub set_max_size {
263 my ($self, $w, $h) = @_; 283 my ($self, $w, $h) = @_;
264 284
384sub set_parent { 404sub set_parent {
385 my ($self, $parent) = @_; 405 my ($self, $parent) = @_;
386 406
387 Scalar::Util::weaken ($self->{parent} = $parent); 407 Scalar::Util::weaken ($self->{parent} = $parent);
388 408
409 # TODO: req_w _does_change after ->reconfigure
389 $self->check_size 410 $self->check_size
390 unless exists $self->{req_w}; 411 unless exists $self->{req_w};
391} 412}
392 413
393sub check_size { 414sub check_size {
417} 438}
418 439
419sub DESTROY { 440sub DESTROY {
420 my ($self) = @_; 441 my ($self) = @_;
421 442
443 delete $WIDGET{$self+0};
422 #$self->deactivate; 444 #$self->deactivate;
423} 445}
424 446
425############################################################################# 447#############################################################################
426 448
628} 650}
629 651
630sub update { 652sub update {
631 my ($self) = @_; 653 my ($self) = @_;
632 654
633 $ROOT->on_refresh ($self => sub { $self->render_child }); 655 $ROOT->on_post_alloc ($self => sub { $self->render_child });
634 $self->SUPER::update; 656 $self->SUPER::update;
635} 657}
636 658
637sub size_allocate { 659sub size_allocate {
638 my ($self, $w, $h) = @_; 660 my ($self, $w, $h) = @_;
776 $self->add ($self->{slider}); 798 $self->add ($self->{slider});
777 799
778 $self 800 $self
779} 801}
780 802
781#TODO# update range on size_allocate depeneing on child 803#TODO# update range on size_allocate depending on child
782# update viewport offset on scroll 804# update viewport offset on scroll
783 805
784############################################################################# 806#############################################################################
785 807
786package CFClient::UI::Frame; 808package CFClient::UI::Frame;
787 809
788our @ISA = CFClient::UI::Bin::; 810our @ISA = CFClient::UI::Bin::;
789 811
790use CFClient::OpenGL; 812use CFClient::OpenGL;
813
814sub new {
815 my $class = shift;
816
817 $class->SUPER::new (
818 bg => undef,
819 @_,
820 )
821}
822
823sub _draw {
824 my ($self) = @_;
825
826 if ($self->{bg}) {
827 my ($w, $h) = @$self{qw(w h)};
828
829 glEnable GL_BLEND;
830 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
831 glColor @{ $self->{bg} };
832
833 glBegin GL_QUADS;
834 glVertex 0 , 0;
835 glVertex 0 , $h;
836 glVertex $w, $h;
837 glVertex $w, 0;
838 glEnd;
839
840 glDisable GL_BLEND;
841 }
842
843 $self->SUPER::_draw;
844}
791 845
792############################################################################# 846#############################################################################
793 847
794package CFClient::UI::FancyFrame; 848package CFClient::UI::FancyFrame;
795 849
808 862
809 my $self = $class->SUPER::new ( 863 my $self = $class->SUPER::new (
810 bg => [1, 1, 1, 1], 864 bg => [1, 1, 1, 1],
811 border_bg => [1, 1, 1, 1], 865 border_bg => [1, 1, 1, 1],
812 border => 0.6, 866 border => 0.6,
867 toplevel => 1,
813 can_events => 1, 868 can_events => 1,
814 @_ 869 @_
815 ); 870 );
816 871
817 $self->{title} &&= new CFClient::UI::Label 872 $self->{title} &&= new CFClient::UI::Label
1448 my $sym = $ev->{sym}; 1503 my $sym = $ev->{sym};
1449 my $uni = $ev->{unicode}; 1504 my $uni = $ev->{unicode};
1450 1505
1451 my $text = $self->get_text; 1506 my $text = $self->get_text;
1452 1507
1453 if ($sym == 8) { 1508 if ($uni == 8) {
1454 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1509 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1455 } elsif ($sym == 127) { 1510 } elsif ($uni == 127) {
1456 substr $text, $self->{cursor}, 1, ""; 1511 substr $text, $self->{cursor}, 1, "";
1457 } elsif ($sym == CFClient::SDLK_LEFT) { 1512 } elsif ($sym == CFClient::SDLK_LEFT) {
1458 --$self->{cursor} if $self->{cursor}; 1513 --$self->{cursor} if $self->{cursor};
1459 } elsif ($sym == CFClient::SDLK_RIGHT) { 1514 } elsif ($sym == CFClient::SDLK_RIGHT) {
1460 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1515 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1461 } elsif ($sym == CFClient::SDLK_HOME) { 1516 } elsif ($sym == CFClient::SDLK_HOME) {
1462 $self->{cursor} = 0; 1517 $self->{cursor} = 0;
1463 } elsif ($sym == CFClient::SDLK_END) { 1518 } elsif ($sym == CFClient::SDLK_END) {
1464 $self->{cursor} = length $text; 1519 $self->{cursor} = length $text;
1465 } elsif ($sym == 27) { 1520 } elsif ($uni == 27) {
1466 $self->emit ('escape'); 1521 $self->emit ('escape');
1467 } elsif ($uni) { 1522 } elsif ($uni) {
1468 substr $text, $self->{cursor}++, 0, chr $uni; 1523 substr $text, $self->{cursor}++, 0, chr $uni;
1469 } 1524 }
1470 1525
2165 2220
2166 return unless $self->{h} > 0; 2221 return unless $self->{h} > 0;
2167 2222
2168 delete $self->{texture}; 2223 delete $self->{texture};
2169 2224
2170 $ROOT->on_refresh ($self, sub { 2225 $ROOT->on_post_alloc ($self, sub {
2171 if (delete $self->{need_reflow}) { 2226 if (delete $self->{need_reflow}) {
2172 my $height = 0; 2227 my $height = 0;
2173 2228
2174 $height += $_->[0] = $self->text_height ($_->[2]) 2229 $height += $_->[0] = $self->text_height ($_->[2])
2175 for @{$self->{par}}; 2230 for @{$self->{par}};
2506 my ($self, $items) = @_; 2561 my ($self, $items) = @_;
2507 2562
2508 $self->{scrolled}->clear; 2563 $self->{scrolled}->clear;
2509 return unless $items; 2564 return unless $items;
2510 2565
2511 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;
2512 2570
2513 $self->{real_items} = \@items; 2571 $self->{real_items} = \@items;
2514 2572
2515 for my $item (@items) { 2573 for my $item (@items) {
2516 my $desc = $item->{nrof} < 2 2574 my $desc = $item->{nrof} < 2
2517 ? $item->{name} 2575 ? $item->{name}
2518 : "$item->{nrof} $item->{name_pl}"; 2576 : "$item->{nrof} $item->{name_pl}";
2519 2577
2520 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2578 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2521 } 2579 }
2580
2581 $self->{scrolled}->add (@items);
2522 2582
2523# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2583# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2524} 2584}
2525 2585
2526sub size_request { 2586sub size_request {
2632 : $item->{text}; 2692 : $item->{text};
2633 2693
2634 for ($short) { 2694 for ($short) {
2635 s/^\s+//; 2695 s/^\s+//;
2636 s/\012.*/…/s; 2696 s/\012.*/…/s;
2637 my $len = int 30 / $item->{fontsize}; 2697 my $len = int 40 / $item->{fontsize};
2638 substr $_, $len, length, "…" if $len < length; 2698 substr $_, $len, length, "…" if $len < length;
2639 } 2699 }
2640 2700
2641 new CFClient::UI::Label 2701 new CFClient::UI::Label
2642 markup => $short, 2702 markup => $short,
2655} 2715}
2656 2716
2657sub add { 2717sub add {
2658 my ($self, $text, %arg) = @_; 2718 my ($self, $text, %arg) = @_;
2659 2719
2720 $text =~ s/^\s+//;
2721 $text =~ s/\s+$//;
2722
2660 my $timeout = time + ((delete $arg{timeout}) || 60); 2723 my $timeout = time + ((delete $arg{timeout}) || 60);
2661 2724
2662 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2725 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2663 2726
2664 if (my $item = $self->{item}{$group}) { 2727 if (my $item = $self->{item}{$group}) {
2666 $item->{count}++; 2729 $item->{count}++;
2667 } else { 2730 } else {
2668 $item->{count} = 1; 2731 $item->{count} = 1;
2669 $item->{text} = $item->{tooltip} = $text; 2732 $item->{text} = $item->{tooltip} = $text;
2670 } 2733 }
2734 $item->{id} = ++$self->{id};
2671 $item->{timeout} = $timeout; 2735 $item->{timeout} = $timeout;
2672 delete $item->{label}; 2736 delete $item->{label};
2673 } else { 2737 } else {
2674 $self->{item}{$group} = { 2738 $self->{item}{$group} = {
2675 id => ++$self->{id}, 2739 id => ++$self->{id},
2724} 2788}
2725 2789
2726sub size_allocate { 2790sub size_allocate {
2727 my ($self, $w, $h) = @_; 2791 my ($self, $w, $h) = @_;
2728 2792
2729 my $old_w = $self->{old_w}; 2793 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2730 my $old_h = $self->{old_h}; 2794 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2731 2795
2732 if ($old_w && $old_h) { 2796 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2733 for my $child ($self->children) { 2797 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2734 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2735 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2736 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2737 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2738 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2739 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2740 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2741 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2742 }
2743 }
2744 2798
2745 for my $child ($self->children) { 2799 for my $child ($self->children) {
2746 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)};
2747 2801
2748 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2802 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2749 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2803 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2750 $child->configure ($X, $Y, $W, $H); 2804 $child->configure ($X, $Y, $W, $H);
2751 } 2805 }
2752
2753 $self->{old_w} = $w;
2754 $self->{old_h} = $h;
2755} 2806}
2756 2807
2757sub coord2local { 2808sub coord2local {
2758 my ($self, $x, $y) = @_; 2809 my ($self, $x, $y) = @_;
2759 2810
2787 my ($self, $id, $cb) = @_; 2838 my ($self, $id, $cb) = @_;
2788 2839
2789 $self->{refresh_hook}{$id} = $cb; 2840 $self->{refresh_hook}{$id} = $cb;
2790} 2841}
2791 2842
2843sub on_post_alloc {
2844 my ($self, $id, $cb) = @_;
2845
2846 $self->{post_alloc_hook}{$id} = $cb;
2847}
2848
2792sub draw { 2849sub draw {
2793 my ($self) = @_; 2850 my ($self) = @_;
2851
2852 while ($self->{refresh_hook}) {
2853 $_->()
2854 for values %{delete $self->{refresh_hook}};
2855 }
2794 2856
2795 if ($self->{check_size}) { 2857 if ($self->{check_size}) {
2796 my @queue = ([], []); 2858 my @queue = ([], []);
2797 2859
2798 for (;;) { 2860 for (;;) {
2835 $widget->size_allocate ($w, $h); 2897 $widget->size_allocate ($w, $h);
2836 $widget->emit (size_allocate => $w, $h); 2898 $widget->emit (size_allocate => $w, $h);
2837 } 2899 }
2838 } 2900 }
2839 2901
2840 while ($self->{refresh_hook}) { 2902 while ($self->{post_alloc_hook}) {
2841 $_->() 2903 $_->()
2842 for values %{delete $self->{refresh_hook}}; 2904 for values %{delete $self->{post_alloc_hook}};
2843 } 2905 }
2844 2906
2845 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2907 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2846 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2908 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2847 glClear GL_COLOR_BUFFER_BIT; 2909 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines