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.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}) {
29 $TOOLTIP->set_tooltip_from ($widget); 31 $TOOLTIP->set_tooltip_from ($widget);
30 $TOOLTIP->show; 32 $TOOLTIP->show;
31 33
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33 35
34 if ($x + $TOOLTIP->{w} > $::WIDTH) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
36 } 37 if $x + $TOOLTIP->{w} > $::WIDTH;
37 38
38 $TOOLTIP->move ($x, $y); 39 $TOOLTIP->move ($x, $y);
39 $TOOLTIP->check_size; 40 $TOOLTIP->check_size;
40 $TOOLTIP->update; 41 $TOOLTIP->update;
41 } 42 }
132 133
133 for (@$vals) { 134 for (@$vals) {
134 my $i = int $_ + $rem; 135 my $i = int $_ + $rem;
135 $rem += $_ - $i; 136 $rem += $_ - $i;
136 $_ = $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;
137 } 158 }
138} 159}
139 160
140############################################################################# 161#############################################################################
141 162
160 if (/^connect_(.*)$/) { 181 if (/^connect_(.*)$/) {
161 $self->connect ($1 => delete $self->{$_}); 182 $self->connect ($1 => delete $self->{$_});
162 } 183 }
163 } 184 }
164 185
186 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
187
165 $self 188 $self
166} 189}
167 190
168sub destroy { 191sub destroy {
169 my ($self) = @_; 192 my ($self) = @_;
242 265
243sub size_allocate { 266sub size_allocate {
244 # nothing to be done 267 # nothing to be done
245} 268}
246 269
270sub reconfigure {
271 my ($self) = @_;
272
273 $self->check_size (1);
274 $self->update;
275}
276
247sub children { 277sub 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} 278}
261 279
262sub set_max_size { 280sub set_max_size {
263 my ($self, $w, $h) = @_; 281 my ($self, $w, $h) = @_;
264 282
357 glVertex $self->{w}, 0; 375 glVertex $self->{w}, 0;
358 glVertex $self->{w}, $self->{h}; 376 glVertex $self->{w}, $self->{h};
359 glVertex 0 , $self->{h}; 377 glVertex 0 , $self->{h};
360 glEnd; 378 glEnd;
361 glPopMatrix; 379 glPopMatrix;
362 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;
363 } 381 }
364} 382}
365 383
366sub _draw { 384sub _draw {
367 my ($self) = @_; 385 my ($self) = @_;
384sub set_parent { 402sub set_parent {
385 my ($self, $parent) = @_; 403 my ($self, $parent) = @_;
386 404
387 Scalar::Util::weaken ($self->{parent} = $parent); 405 Scalar::Util::weaken ($self->{parent} = $parent);
388 406
407 # TODO: req_w _does_change after ->reconfigure
389 $self->check_size 408 $self->check_size
390 unless exists $self->{req_w}; 409 unless exists $self->{req_w};
391} 410}
392 411
393sub check_size { 412sub check_size {
417} 436}
418 437
419sub DESTROY { 438sub DESTROY {
420 my ($self) = @_; 439 my ($self) = @_;
421 440
441 delete $WIDGET{$self+0};
422 #$self->deactivate; 442 #$self->deactivate;
423} 443}
424 444
425############################################################################# 445#############################################################################
426 446
628} 648}
629 649
630sub update { 650sub update {
631 my ($self) = @_; 651 my ($self) = @_;
632 652
633 $ROOT->on_refresh ($self => sub { $self->render_child }); 653 $ROOT->on_post_alloc ($self => sub { $self->render_child });
634 $self->SUPER::update; 654 $self->SUPER::update;
635} 655}
636 656
637sub size_allocate { 657sub size_allocate {
638 my ($self, $w, $h) = @_; 658 my ($self, $w, $h) = @_;
776 $self->add ($self->{slider}); 796 $self->add ($self->{slider});
777 797
778 $self 798 $self
779} 799}
780 800
781#TODO# update range on size_allocate depeneing on child 801#TODO# update range on size_allocate depending on child
782# update viewport offset on scroll 802# update viewport offset on scroll
783 803
784############################################################################# 804#############################################################################
785 805
786package CFClient::UI::Frame; 806package CFClient::UI::Frame;
787 807
788our @ISA = CFClient::UI::Bin::; 808our @ISA = CFClient::UI::Bin::;
789 809
790use CFClient::OpenGL; 810use CFClient::OpenGL;
811
812sub new {
813 my $class = shift;
814
815 $class->SUPER::new (
816 bg => undef,
817 @_,
818 )
819}
820
821sub _draw {
822 my ($self) = @_;
823
824 if ($self->{bg}) {
825 my ($w, $h) = @$self{qw(w h)};
826
827 glEnable GL_BLEND;
828 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
829 glColor @{ $self->{bg} };
830
831 glBegin GL_QUADS;
832 glVertex 0 , 0;
833 glVertex 0 , $h;
834 glVertex $w, $h;
835 glVertex $w, 0;
836 glEnd;
837
838 glDisable GL_BLEND;
839 }
840
841 $self->SUPER::_draw;
842}
791 843
792############################################################################# 844#############################################################################
793 845
794package CFClient::UI::FancyFrame; 846package CFClient::UI::FancyFrame;
795 847
808 860
809 my $self = $class->SUPER::new ( 861 my $self = $class->SUPER::new (
810 bg => [1, 1, 1, 1], 862 bg => [1, 1, 1, 1],
811 border_bg => [1, 1, 1, 1], 863 border_bg => [1, 1, 1, 1],
812 border => 0.6, 864 border => 0.6,
865 toplevel => 1,
813 can_events => 1, 866 can_events => 1,
814 @_ 867 @_
815 ); 868 );
816 869
817 $self->{title} &&= new CFClient::UI::Label 870 $self->{title} &&= new CFClient::UI::Label
1448 my $sym = $ev->{sym}; 1501 my $sym = $ev->{sym};
1449 my $uni = $ev->{unicode}; 1502 my $uni = $ev->{unicode};
1450 1503
1451 my $text = $self->get_text; 1504 my $text = $self->get_text;
1452 1505
1453 if ($sym == 8) { 1506 if ($uni == 8) {
1454 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1507 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1455 } elsif ($sym == 127) { 1508 } elsif ($uni == 127) {
1456 substr $text, $self->{cursor}, 1, ""; 1509 substr $text, $self->{cursor}, 1, "";
1457 } elsif ($sym == CFClient::SDLK_LEFT) { 1510 } elsif ($sym == CFClient::SDLK_LEFT) {
1458 --$self->{cursor} if $self->{cursor}; 1511 --$self->{cursor} if $self->{cursor};
1459 } elsif ($sym == CFClient::SDLK_RIGHT) { 1512 } elsif ($sym == CFClient::SDLK_RIGHT) {
1460 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1513 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1461 } elsif ($sym == CFClient::SDLK_HOME) { 1514 } elsif ($sym == CFClient::SDLK_HOME) {
1462 $self->{cursor} = 0; 1515 $self->{cursor} = 0;
1463 } elsif ($sym == CFClient::SDLK_END) { 1516 } elsif ($sym == CFClient::SDLK_END) {
1464 $self->{cursor} = length $text; 1517 $self->{cursor} = length $text;
1465 } elsif ($sym == 27) { 1518 } elsif ($uni == 27) {
1466 $self->emit ('escape'); 1519 $self->emit ('escape');
1467 } elsif ($uni) { 1520 } elsif ($uni) {
1468 substr $text, $self->{cursor}++, 0, chr $uni; 1521 substr $text, $self->{cursor}++, 0, chr $uni;
1469 } 1522 }
1470 1523
2165 2218
2166 return unless $self->{h} > 0; 2219 return unless $self->{h} > 0;
2167 2220
2168 delete $self->{texture}; 2221 delete $self->{texture};
2169 2222
2170 $ROOT->on_refresh ($self, sub { 2223 $ROOT->on_post_alloc ($self, sub {
2171 if (delete $self->{need_reflow}) { 2224 if (delete $self->{need_reflow}) {
2172 my $height = 0; 2225 my $height = 0;
2173 2226
2174 $height += $_->[0] = $self->text_height ($_->[2]) 2227 $height += $_->[0] = $self->text_height ($_->[2])
2175 for @{$self->{par}}; 2228 for @{$self->{par}};
2506 my ($self, $items) = @_; 2559 my ($self, $items) = @_;
2507 2560
2508 $self->{scrolled}->clear; 2561 $self->{scrolled}->clear;
2509 return unless $items; 2562 return unless $items;
2510 2563
2511 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;
2512 2568
2513 $self->{real_items} = \@items; 2569 $self->{real_items} = \@items;
2514 2570
2515 for my $item (@items) { 2571 for my $item (@items) {
2516 my $desc = $item->{nrof} < 2 2572 my $desc = $item->{nrof} < 2
2517 ? $item->{name} 2573 ? $item->{name}
2518 : "$item->{nrof} $item->{name_pl}"; 2574 : "$item->{nrof} $item->{name_pl}";
2519 2575
2520 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2576 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2521 } 2577 }
2578
2579 $self->{scrolled}->add (@items);
2522 2580
2523# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2581# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2524} 2582}
2525 2583
2526sub size_request { 2584sub size_request {
2631 ? "<b>$item->{count} ×</b> $item->{text}" 2689 ? "<b>$item->{count} ×</b> $item->{text}"
2632 : $item->{text}; 2690 : $item->{text};
2633 2691
2634 for ($short) { 2692 for ($short) {
2635 s/^\s+//; 2693 s/^\s+//;
2636 s/\012.*/…/s; 2694 s/\s+/ /g;
2637 my $len = int 30 / $item->{fontsize}; 2695 my $len = int 40 / $item->{fontsize};
2638 substr $_, $len, length, "…" if $len < length; 2696 substr $_, $len, length, "…" if $len < length;
2639 } 2697 }
2640 2698
2641 new CFClient::UI::Label 2699 new CFClient::UI::Label
2642 markup => $short, 2700 markup => $short,
2643 tooltip => $item->{tooltip}, 2701 tooltip => $item->{tooltip},
2644 tooltip_font => $::FONT_PROP, 2702 tooltip_font => $::FONT_PROP,
2645 tooltip_width => 0.67, 2703 tooltip_width => 0.67,
2646 fontsize => $item->{fontsize}, 2704 fontsize => $item->{fontsize},
2647 color => $item->{color}, 2705 fg => $item->{fg},
2648 can_events => 1, 2706 can_events => 1,
2649 can_hover => 1 2707 can_hover => 1
2650 }; 2708 };
2651 } 2709 }
2652 2710
2654 $self->SUPER::add (reverse @widgets); 2712 $self->SUPER::add (reverse @widgets);
2655} 2713}
2656 2714
2657sub add { 2715sub add {
2658 my ($self, $text, %arg) = @_; 2716 my ($self, $text, %arg) = @_;
2717
2718 $text =~ s/^\s+//;
2719 $text =~ s/\s+$//;
2659 2720
2660 my $timeout = time + ((delete $arg{timeout}) || 60); 2721 my $timeout = time + ((delete $arg{timeout}) || 60);
2661 2722
2662 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2723 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2663 2724
2666 $item->{count}++; 2727 $item->{count}++;
2667 } else { 2728 } else {
2668 $item->{count} = 1; 2729 $item->{count} = 1;
2669 $item->{text} = $item->{tooltip} = $text; 2730 $item->{text} = $item->{tooltip} = $text;
2670 } 2731 }
2732 $item->{id} = ++$self->{id};
2671 $item->{timeout} = $timeout; 2733 $item->{timeout} = $timeout;
2672 delete $item->{label}; 2734 delete $item->{label};
2673 } else { 2735 } else {
2674 $self->{item}{$group} = { 2736 $self->{item}{$group} = {
2675 id => ++$self->{id}, 2737 id => ++$self->{id},
2676 text => $text, 2738 text => $text,
2677 timeout => $timeout, 2739 timeout => $timeout,
2678 tooltip => $text, 2740 tooltip => $text,
2679 fontsize => 0.8, 2741 fontsize => 0.8,
2680 color => [0.8, 0.8, 0.8, 0.8], 2742 fg => [0.8, 0.8, 0.8, 0.8],
2681 pri => 0, 2743 pri => 0,
2682 count => 1, 2744 count => 1,
2683 %arg, 2745 %arg,
2684 }; 2746 };
2685 } 2747 }
2724} 2786}
2725 2787
2726sub size_allocate { 2788sub size_allocate {
2727 my ($self, $w, $h) = @_; 2789 my ($self, $w, $h) = @_;
2728 2790
2729 my $old_w = $self->{old_w}; 2791 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2730 my $old_h = $self->{old_h}; 2792 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2731 2793
2732 if ($old_w && $old_h) { 2794 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2733 for my $child ($self->children) { 2795 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 2796
2745 for my $child ($self->children) { 2797 for my $child ($self->children) {
2746 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)};
2747 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
2748 $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;
2749 $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
2750 $child->configure ($X, $Y, $W, $H); 2809 $child->configure ($X, $Y, $W, $H);
2751 } 2810 }
2752
2753 $self->{old_w} = $w;
2754 $self->{old_h} = $h;
2755} 2811}
2756 2812
2757sub coord2local { 2813sub coord2local {
2758 my ($self, $x, $y) = @_; 2814 my ($self, $x, $y) = @_;
2759 2815
2772 $self->check_size; 2828 $self->check_size;
2773 $::WANT_REFRESH++; 2829 $::WANT_REFRESH++;
2774} 2830}
2775 2831
2776sub add { 2832sub add {
2777 my ($self, $child) = @_; 2833 my ($self, @children) = @_;
2778 2834
2835 for my $child (@children) {
2836 $child->{toplevel} = 1;
2837
2779 # integerise window positions 2838 # integerise window positions
2780 $child->{x} = int $child->{x}; 2839 $child->{x} = int $child->{x};
2781 $child->{y} = int $child->{y}; 2840 $child->{y} = int $child->{y};
2841 }
2782 2842
2783 $self->SUPER::add ($child); 2843 $self->SUPER::add (@children);
2784} 2844}
2785 2845
2786sub on_refresh { 2846sub on_refresh {
2787 my ($self, $id, $cb) = @_; 2847 my ($self, $id, $cb) = @_;
2788 2848
2789 $self->{refresh_hook}{$id} = $cb; 2849 $self->{refresh_hook}{$id} = $cb;
2790} 2850}
2791 2851
2852sub on_post_alloc {
2853 my ($self, $id, $cb) = @_;
2854
2855 $self->{post_alloc_hook}{$id} = $cb;
2856}
2857
2792sub draw { 2858sub draw {
2793 my ($self) = @_; 2859 my ($self) = @_;
2860
2861 while ($self->{refresh_hook}) {
2862 $_->()
2863 for values %{delete $self->{refresh_hook}};
2864 }
2794 2865
2795 if ($self->{check_size}) { 2866 if ($self->{check_size}) {
2796 my @queue = ([], []); 2867 my @queue = ([], []);
2797 2868
2798 for (;;) { 2869 for (;;) {
2835 $widget->size_allocate ($w, $h); 2906 $widget->size_allocate ($w, $h);
2836 $widget->emit (size_allocate => $w, $h); 2907 $widget->emit (size_allocate => $w, $h);
2837 } 2908 }
2838 } 2909 }
2839 2910
2840 while ($self->{refresh_hook}) { 2911 while ($self->{post_alloc_hook}) {
2841 $_->() 2912 $_->()
2842 for values %{delete $self->{refresh_hook}}; 2913 for values %{delete $self->{post_alloc_hook}};
2843 } 2914 }
2844 2915
2845 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2916 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2846 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2917 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2847 glClear GL_COLOR_BUFFER_BIT; 2918 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines