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.234 by root, Thu May 25 16:24:44 2006 UTC vs.
Revision 1.244 by root, Sat May 27 21:06:09 2006 UTC

5 5
6use Scalar::Util (); 6use Scalar::Util ();
7use List::Util (); 7use List::Util ();
8 8
9use CFClient; 9use CFClient;
10use CFClient::Texture;
10 11
11our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
12 13
14our $LAYOUT;
13our $ROOT; 15our $ROOT;
14our $TOOLTIP; 16our $TOOLTIP;
15our $BUTTON_STATE; 17our $BUTTON_STATE;
16 18
17our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
20
21sub get_layout {
22 for (grep { $_->{name} } values %WIDGET) {
23 $LAYOUT->{$_->{name}} = {
24 x => $_->{x} / $::WIDTH,
25 y => $_->{y} / $::HEIGHT,
26 w => $_->{w} / $::WIDTH,
27 h => $_->{h} / $::HEIGHT
28 };
29 }
30
31 return $LAYOUT;
32}
33
34sub set_layout {
35 my ($layout) = @_;
36 $LAYOUT = $layout;
37}
18 38
19sub check_tooltip { 39sub check_tooltip {
20 if (!$GRAB) { 40 if (!$GRAB) {
21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 41 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
22 if (length $widget->{tooltip}) { 42 if (length $widget->{tooltip}) {
186 can_events => 1, 206 can_events => 1,
187 @_ 207 @_
188 }, $class; 208 }, $class;
189 209
190 for (keys %$self) { 210 for (keys %$self) {
191 if (/^connect_(.*)$/) { 211 if (/^on_(.*)$/) {
192 $self->connect ($1 => delete $self->{$_}); 212 $self->connect ($1 => delete $self->{$_});
193 } 213 }
194 } 214 }
195 215
196 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 216 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
217
218 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
219 $self->{user_x} = $layout->{x} * $::WIDTH;
220 $self->{user_y} = $layout->{y} * $::HEIGHT;
221 $self->{user_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH;
222 $self->{user_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT;
223 }
197 224
198 $self 225 $self
199} 226}
200 227
201sub destroy { 228sub destroy {
226} 253}
227 254
228sub set_invisible { 255sub set_invisible {
229 my ($self) = @_; 256 my ($self) = @_;
230 257
258 return unless $self->{visible};
259
231 # broken show/hide model 260 # broken show/hide model
232 261
262 delete $self->{root};
233 delete $self->{visible}; 263 delete $self->{visible};
234 264
235 undef $GRAB if $GRAB == $self; 265 undef $GRAB if $GRAB == $self;
236 undef $HOVER if $HOVER == $self; 266 undef $HOVER if $HOVER == $self;
237 267
238 CFClient::UI::check_tooltip 268 CFClient::UI::check_tooltip
239 if $CFClient::UI::TOOLTIP->{owner} == $self; 269 if $CFClient::UI::TOOLTIP->{owner} == $self;
240 270
241 $self->focus_out; 271 $self->focus_out;
272
273 $self->emit (visibility_change => 0);
242} 274}
243 275
244sub hide { 276sub hide {
245 my ($self) = @_; 277 my ($self) = @_;
246 278
458sub set_parent { 490sub set_parent {
459 my ($self, $parent) = @_; 491 my ($self, $parent) = @_;
460 492
461 Scalar::Util::weaken ($self->{parent} = $parent); 493 Scalar::Util::weaken ($self->{parent} = $parent);
462 494
495 if ($parent->{visible} || 1) {
496 $self->{root} = $parent->{root};
497 $self->{visible} = $parent->{visible} + 1;
498
499 $self->emit (visibility_change => 1)
500 unless $self->{parent}{visible};
501 }
502
463 # TODO: req_w _does_change after ->reconfigure 503 # TODO: req_w _does_change after ->reconfigure
464 $self->check_size 504 $self->check_size
465 unless exists $self->{req_w}; 505 unless exists $self->{req_w};
466 506
467 $self->show; 507 $self->show;
496sub emit { 536sub emit {
497 my ($self, $signal, @args) = @_; 537 my ($self, $signal, @args) = @_;
498 538
499 $self->_emit ($signal, @args) 539 $self->_emit ($signal, @args)
500 || $self->$signal (@args); 540 || $self->$signal (@args);
541}
542
543sub visibility_change {
544 #my ($self, $visible) = @_;
501} 545}
502 546
503sub DESTROY { 547sub DESTROY {
504 my ($self) = @_; 548 my ($self) = @_;
505 549
618 delete $child->{parent}; 662 delete $child->{parent};
619 $child->hide; 663 $child->hide;
620 664
621 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 665 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
622 666
623 $self->check_size; 667 $self->check_size (1);
624 $self->update; 668 $self->update;
625} 669}
626 670
627sub clear { 671sub clear {
628 my ($self) = @_; 672 my ($self) = @_;
853 my $class = shift; 897 my $class = shift;
854 898
855 my $self; 899 my $self;
856 900
857 my $slider = new CFClient::UI::Slider 901 my $slider = new CFClient::UI::Slider
858 vertical => 1, 902 vertical => 1,
859 range => [0, 0, 1, 0.01], # HACK fix 903 range => [0, 0, 1, 0.01], # HACK fix
860 connect_changed => sub { 904 on_changed => sub {
861 $self->{vp}->set_offset (0, $_[1]); 905 $self->{vp}->set_offset (0, $_[1]);
862 }, 906 },
863 ; 907 ;
864 908
865 $self = $class->SUPER::new ( 909 $self = $class->SUPER::new (
871 $self->{vp}->add ($self->{scrolled}); 915 $self->{vp}->add ($self->{scrolled});
872 $self->add ($self->{vp}); 916 $self->add ($self->{vp});
873 $self->add ($self->{slider}); 917 $self->add ($self->{slider});
874 918
875 $self 919 $self
920}
921
922sub update {
923 my ($self) = @_;
924
925 $self->SUPER::update;
926
927 # todo: overwrite size_allocate of child
928 my $child = $self->{vp}->child;
929 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
876} 930}
877 931
878sub size_allocate { 932sub size_allocate {
879 my ($self, $w, $h) = @_; 933 my ($self, $w, $h) = @_;
880 934
1010 my ($ev, $x, $y) = @_; 1064 my ($ev, $x, $y) = @_;
1011 1065
1012 my $dx = $ev->{x} - $ox; 1066 my $dx = $ev->{x} - $ox;
1013 my $dy = $ev->{y} - $oy; 1067 my $dy = $ev->{y} - $oy;
1014 1068
1069 $self->{user_x} = $wx + $dx * $mx;
1070 $self->{user_y} = $wy + $dy * $my;
1015 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1071 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
1016 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1072 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
1017 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1073 $self->move ($self->{user_x}, $self->{user_y});
1018 $self->check_size; 1074 $self->check_size;
1019 }; 1075 };
1020 1076
1021 } elsif ($lr ^ $td) { 1077 } elsif ($lr ^ $td) {
1022 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1078 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1025 $self->{motion} = sub { 1081 $self->{motion} = sub {
1026 my ($ev, $x, $y) = @_; 1082 my ($ev, $x, $y) = @_;
1027 1083
1028 ($x, $y) = ($ev->{x}, $ev->{y}); 1084 ($x, $y) = ($ev->{x}, $ev->{y});
1029 1085
1030 $self->move ($bx + $x - $ox, $by + $y - $oy); 1086 $self->{user_x} = $bx + $x - $ox;
1087 $self->{user_y} = $by + $y - $oy;
1088 $self->move ($self->{user_x}, $self->{user_y});
1031 $self->update; 1089 $self->update;
1032 }; 1090 };
1033 } 1091 }
1034} 1092}
1035 1093
1101 col_expand => [], 1159 col_expand => [],
1102 @_, 1160 @_,
1103 ) 1161 )
1104} 1162}
1105 1163
1164sub children {
1165 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1166}
1167
1106sub add { 1168sub add {
1107 my ($self, $x, $y, $child) = @_; 1169 my ($self, $x, $y, $child) = @_;
1108 1170
1109 $child->set_parent ($self); 1171 $child->set_parent ($self);
1110 $self->{children}[$y][$x] = $child; 1172 $self->{children}[$y][$x] = $child;
1111 1173
1112 $child->check_size; 1174 $self->check_size (1);
1113} 1175}
1114 1176
1115sub children {
1116 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1117}
1118
1119# TODO: move to container class maybe? send childs a signal on removal? 1177# TODO: move to container class maybe? send children a signal on removal?
1120sub clear { 1178sub clear {
1121 my ($self) = @_; 1179 my ($self) = @_;
1122 1180
1123 my @children = $self->children; 1181 my @children = $self->children;
1124 delete $self->{children}; 1182 delete $self->{children};
1126 for (@children) { 1184 for (@children) {
1127 delete $_->{parent}; 1185 delete $_->{parent};
1128 $_->hide; 1186 $_->hide;
1129 } 1187 }
1130 1188
1189 $self->check_size (1);
1131 $self->update; 1190 $self->update;
1132} 1191}
1133 1192
1134sub get_wh { 1193sub get_wh {
1135 my ($self) = @_; 1194 my ($self) = @_;
1167sub size_allocate { 1226sub size_allocate {
1168 my ($self, $w, $h) = @_; 1227 my ($self, $w, $h) = @_;
1169 1228
1170 my ($ws, $hs) = $self->get_wh; 1229 my ($ws, $hs) = $self->get_wh;
1171 1230
1172 my $req_w = sum @$ws; 1231 my $req_w = (sum @$ws) || 1;
1173 my $req_h = sum @$hs; 1232 my $req_h = (sum @$hs) || 1;
1174 1233
1175 # TODO: nicer code && do row_expand 1234 # TODO: nicer code && do row_expand
1176 my @col_expand = @{$self->{col_expand}}; 1235 my @col_expand = @{$self->{col_expand}};
1177 @col_expand = (1) x @$ws unless @col_expand; 1236 @col_expand = (1) x @$ws unless @col_expand;
1178 my $col_expand = (sum @col_expand) || 1; 1237 my $col_expand = (sum @col_expand) || 1;
2132} 2191}
2133 2192
2134sub set_range { 2193sub set_range {
2135 my ($self, $range) = @_; 2194 my ($self, $range) = @_;
2136 2195
2137 $self->{range} = $range; 2196 ($range, $self->{range}) = ($self->{range}, $range);
2138 2197
2139 $self->update; 2198 $self->update
2199 if "@$range" ne "@{$self->{range}}";
2140} 2200}
2141 2201
2142sub set_value { 2202sub set_value {
2143 my ($self, $value) = @_; 2203 my ($self, $value) = @_;
2144 2204
2527 2587
2528sub new { 2588sub new {
2529 my $class = shift; 2589 my $class = shift;
2530 2590
2531 my $self = $class->SUPER::new ( 2591 my $self = $class->SUPER::new (
2532 state => 0, 2592 state => 0,
2533 connect_activate => \&toggle_flopper, 2593 on_activate => \&toggle_flopper,
2534 @_ 2594 @_
2535 ); 2595 );
2536 2596
2537 if ($self->{state}) { 2597 if ($self->{state}) {
2538 $self->{state} = 0; 2598 $self->{state} = 0;
2709 $self->SUPER::DESTROY; 2769 $self->SUPER::DESTROY;
2710} 2770}
2711 2771
2712############################################################################# 2772#############################################################################
2713 2773
2714package CFClient::UI::InventoryItem;
2715
2716our @ISA = CFClient::UI::HBox::;
2717
2718sub new {
2719 my $class = shift;
2720
2721 my %args = @_;
2722
2723 my $item = delete $args{item};
2724
2725 my $desc = CFClient::Item::desc_string $item;
2726
2727 my $self = $class->SUPER::new (
2728 can_hover => 1,
2729 can_events => 1,
2730 tooltip => ((CFClient::UI::Label::escape $desc)
2731 . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"),
2732 connect_button_down => sub {
2733 my ($self, $ev, $x, $y) = @_;
2734
2735 # todo: maybe put examine on 1? but should just be a tooltip :(
2736 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
2737 my $targ = $::CONN->{player}{tag};
2738
2739 if ($item->{container} == $::CONN->{player}{tag}) {
2740 $targ = $main::OPENCONT;
2741 }
2742
2743 $::CONN->send ("move $targ $item->{tag} 0");
2744 } elsif ($ev->{button} == 1) {
2745 $::CONN->send ("examine $item->{tag}");
2746 } elsif ($ev->{button} == 2) {
2747 $::CONN->send ("apply $item->{tag}");
2748 } elsif ($ev->{button} == 3) {
2749 my @menu_items = (
2750 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2751 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2752 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2753 (
2754 $item->{flags} & Crossfire::Protocol::F_LOCKED
2755 ? (
2756 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2757 )
2758 : (
2759 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2760 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2761 )
2762 ),
2763 );
2764
2765 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2766 }
2767
2768 1
2769 },
2770 %args
2771 );
2772
2773
2774 $self->add (new CFClient::UI::Face
2775 can_events => 0,
2776 face => $item->{face},
2777 anim => $item->{anim},
2778 animspeed => $item->{animspeed},
2779 );
2780
2781 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2782
2783 $self->{item} = $item;
2784
2785 $self->update_item;
2786
2787 $self
2788}
2789
2790sub update_item {
2791 my ($self) = @_;
2792
2793 my $desc = CFClient::Item::desc_string ($self->{item});
2794
2795 $self->{name_lbl}->set_text ($desc);
2796}
2797
2798#############################################################################
2799
2800package CFClient::UI::Inventory; 2774package CFClient::UI::Inventory;
2801 2775
2802our @ISA = CFClient::UI::ScrolledWindow::; 2776our @ISA = CFClient::UI::ScrolledWindow::;
2803 2777
2804sub new { 2778sub new {
2833 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 2807 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2834 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 2808 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2835 2809
2836 $row++; 2810 $row++;
2837 } 2811 }
2838
2839# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2840} 2812}
2841 2813
2842sub size_request { 2814sub size_request {
2843 my ($self) = @_; 2815 my ($self) = @_;
2844 ($self->{req_w}, $self->{req_h}); 2816 ($self->{req_w}, $self->{req_h});
3036 3008
3037sub new { 3009sub new {
3038 my $class = shift; 3010 my $class = shift;
3039 3011
3040 $class->SUPER::new ( 3012 $class->SUPER::new (
3013 visible => 1,
3041 @_, 3014 @_,
3042 ) 3015 )
3043} 3016}
3044 3017
3045sub configure { 3018sub configure {
3072 if exists $child->{req_x}; 3045 if exists $child->{req_x};
3073 3046
3074 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3047 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3075 if exists $child->{req_y}; 3048 if exists $child->{req_y};
3076 3049
3050 $X = $self->{user_x} if exists $self->{user_x};
3051 $Y = $self->{user_y} if exists $self->{user_y};
3052
3077 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3053 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3078 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; 3054 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3079 3055
3080 $child->configure ($X, $Y, $W, $H); 3056 $child->configure ($X, $Y, $W, $H);
3081 } 3057 }
3103sub add { 3079sub add {
3104 my ($self, @children) = @_; 3080 my ($self, @children) = @_;
3105 3081
3106 for (my @widgets = @children; my $w = pop @widgets; ) { 3082 for (my @widgets = @children; my $w = pop @widgets; ) {
3107 push @widgets, $w->children; 3083 push @widgets, $w->children;
3108 $w->{root} = $self; 3084 $w->{root} = $self;
3109 $w->{visible} = 1; 3085 $w->{visible} = $self->{visible} + 1;
3110 } 3086 }
3111 3087
3112 for my $child (@children) { 3088 for my $child (@children) {
3113 $child->{is_toplevel} = 1; 3089 $child->{is_toplevel} = 1;
3114 3090
3151 $_->() 3127 $_->()
3152 for values %{delete $self->{refresh_hook}}; 3128 for values %{delete $self->{refresh_hook}};
3153 } 3129 }
3154 3130
3155 if ($self->{check_size}) { 3131 if ($self->{check_size}) {
3156 my @queue = ([], []); 3132 my @queue;
3157 3133
3158 for (;;) { 3134 for (;;) {
3159 if ($self->{check_size}) { 3135 if ($self->{check_size}) {
3160 # heuristic: check containers last 3136 #TODO use array-of-depth approach
3161 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3137
3138 @queue = sort { $a->{visible} <=> $b->{visible} }
3162 for values %{delete $self->{check_size}} 3139 @queue, values %{delete $self->{check_size}};
3163 } 3140 }
3164 3141
3165 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3142 my $widget = pop @queue || last;
3143
3144 defined $widget->{visible} or last; # do not resize invisible widgets
3166 3145
3167 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3146 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
3168 ? @$widget{qw(user_w user_h)} 3147 ? @$widget{qw(user_w user_h)}
3169 : $widget->size_request; 3148 : $widget->size_request;
3170 3149

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines