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.255 by root, Mon May 29 02:01:56 2006 UTC vs.
Revision 1.257 by root, Tue May 30 02:22:02 2006 UTC

17our $BUTTON_STATE; 17our $BUTTON_STATE;
18 18
19our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
20 20
21sub get_layout { 21sub get_layout {
22 my $layout;
23
22 for (grep { $_->{name} } values %WIDGET) { 24 for (grep { $_->{name} } values %WIDGET) {
23 $LAYOUT->{$_->{name}} = { 25 my $win = $layout->{$_->{name}} = { };
24 x => $_->{x} / $::WIDTH,
25 y => $_->{y} / $::HEIGHT,
26 w => $_->{w} / $::WIDTH,
27 h => $_->{h} / $::HEIGHT
28 }; 26
27 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/;
28 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/;
29 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w};
30 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h};
29 } 31 }
30 32
31 return $LAYOUT; 33 $layout
32} 34}
33 35
34sub set_layout { 36sub set_layout {
35 my ($layout) = @_; 37 my ($layout) = @_;
36 $LAYOUT = $layout; 38 $LAYOUT = $layout;
165sub rescale_widgets { 167sub rescale_widgets {
166 my ($sx, $sy) = @_; 168 my ($sx, $sy) = @_;
167 169
168 for my $widget (values %WIDGET) { 170 for my $widget (values %WIDGET) {
169 if ($widget->{is_toplevel}) { 171 if ($widget->{is_toplevel}) {
172 $widget->{x} += $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
173 $widget->{y} += $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
174
170 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 175 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
171 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 176 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
172 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 177 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w};
173 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 178 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
174 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 179 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
175 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 180 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
181
182 $widget->{x} -= $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
183 $widget->{y} -= $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
184
176 } 185 }
177 } 186 }
178 187
179 reconfigure_widgets; 188 reconfigure_widgets;
180} 189}
189 198
190sub new { 199sub new {
191 my $class = shift; 200 my $class = shift;
192 201
193 my $self = bless { 202 my $self = bless {
194 x => 0, 203 x => "center",
195 y => 0, 204 y => "center",
196 z => 0, 205 z => 0,
206 w => undef,
207 h => undef,
197 can_events => 1, 208 can_events => 1,
198 @_ 209 @_
199 }, $class; 210 }, $class;
211
212 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
213 $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{w} if exists $layout->{x};
214 $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{h} if exists $layout->{y};
215 $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{w} if exists $layout->{w};
216 $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{h} if exists $layout->{h};
217
218 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
219 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
220 }
221
222 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
200 223
201 for (keys %$self) { 224 for (keys %$self) {
202 if (/^on_(.*)$/) { 225 if (/^on_(.*)$/) {
203 $self->connect ($1 => delete $self->{$_}); 226 $self->connect ($1 => delete $self->{$_});
204 } 227 }
205 } 228 }
206 229
207 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
208
209 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
210 $self->{req_x} = $layout->{x} * $::WIDTH;
211 $self->{req_y} = $layout->{y} * $::HEIGHT;
212 $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH;
213 $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT;
214 }
215
216 $self 230 $self
217} 231}
218 232
219sub destroy { 233sub destroy {
220 my ($self) = @_; 234 my ($self) = @_;
227 my ($self) = @_; 241 my ($self) = @_;
228 242
229 return if $self->{parent}; 243 return if $self->{parent};
230 244
231 $CFClient::UI::ROOT->add ($self); 245 $CFClient::UI::ROOT->add ($self);
232}
233
234sub center {
235 my ($self) = @_;
236
237 $CFClient::UI::ROOT->on_post_alloc (
238 "center_$self" => sub {
239 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
240 },
241 );
242
243 $self->update;
244} 246}
245 247
246sub set_visible { 248sub set_visible {
247 my ($self) = @_; 249 my ($self) = @_;
248 250
303 305
304 $self->{parent}->remove ($self) 306 $self->{parent}->remove ($self)
305 if $self->{parent}; 307 if $self->{parent};
306} 308}
307 309
308sub move { 310sub move_abs {
309 my ($self, $x, $y, $z) = @_; 311 my ($self, $x, $y, $z) = @_;
310 312
311 $self->{x} = int $x; 313 $self->{x} = List::Util::max 0, int $x;
312 $self->{y} = int $y; 314 $self->{y} = List::Util::max 0, int $y;
313 $self->{z} = $z if defined $z; 315 $self->{z} = $z if defined $z;
314 316
315 $self->update; 317 $self->update;
316} 318}
317 319
318sub set_size { 320sub set_size {
319 my ($self, $w, $h) = @_; 321 my ($self, $w, $h) = @_;
320 322
321 $self->{def_w} = $w; 323 $self->{force_w} = $w;
322 $self->{def_h} = $h; 324 $self->{force_h} = $h;
323 325
324 $self->realloc; 326 $self->realloc;
325} 327}
326 328
327sub size_request { 329sub size_request {
331 333
332sub configure { 334sub configure {
333 my ($self, $x, $y, $w, $h) = @_; 335 my ($self, $x, $y, $w, $h) = @_;
334 336
335 if ($self->{aspect}) { 337 if ($self->{aspect}) {
338 my ($ow, $oh) = ($w, $h);
339
336 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 340 $w = List::Util::min $w, int $h * $self->{aspect};
337 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 341 $h = List::Util::min $h, int $w / $self->{aspect};
338 342
339 # use alignment to adjust x, y 343 # use alignment to adjust x, y
340 344
341 $x += int +($w - $w2) * 0.5; 345 $x += int 0.5 * ($ow - $w);
342 $y += int +($h - $h2) * 0.5; 346 $y += int 0.5 * ($oh - $h);
343
344 ($w, $h) = ($w2, $h2);
345 } 347 }
346 348
347 if ($self->{x} != $x || $self->{y} != $y) { 349 if ($self->{x} ne $x || $self->{y} ne $y) {
348 $self->{x} = $x; 350 $self->{x} = $x;
349 $self->{y} = $y; 351 $self->{y} = $y;
350 $self->update; 352 $self->update;
351 } 353 }
352 354
506sub update { 508sub update {
507 my ($self) = @_; 509 my ($self) = @_;
508 510
509 $self->{parent}->update 511 $self->{parent}->update
510 if $self->{parent}; 512 if $self->{parent};
513}
514
515sub reconfigure {
516 my ($self) = @_;
517
518 $self->realloc;
519 $self->update;
511} 520}
512 521
513sub draw { 522sub draw {
514 my ($self) = @_; 523 my ($self) = @_;
515 524
533 glVertex $x , $y + $self->{h}; 542 glVertex $x , $y + $self->{h};
534 glEnd; 543 glEnd;
535 glDisable GL_BLEND; 544 glDisable GL_BLEND;
536 } 545 }
537 546
538 if ($ENV{PCLIENT_DEBUG}) { 547 if ($ENV{CFPLUS_DEBUG}) {
539 glPushMatrix; 548 glPushMatrix;
540 glColor 1, 1, 0, 1; 549 glColor 1, 1, 0, 1;
541 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 550 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
542 glBegin GL_LINE_LOOP; 551 glBegin GL_LINE_LOOP;
543 glVertex 0 , 0; 552 glVertex 0 , 0;
619 my ($class, %arg) = @_; 628 my ($class, %arg) = @_;
620 $class->SUPER::new (can_events => 0, %arg); 629 $class->SUPER::new (can_events => 0, %arg);
621} 630}
622 631
623sub size_request { 632sub size_request {
624 (0, 0) 633 my ($self) = @_;
634
635 ($self->{w} + 0, $self->{h} + 0)
625} 636}
626 637
627sub draw { } 638sub draw { }
628 639
629############################################################################# 640#############################################################################
1015 1026
1016 my $self = $class->SUPER::new ( 1027 my $self = $class->SUPER::new (
1017 bg => [1, 1, 1, 1], 1028 bg => [1, 1, 1, 1],
1018 border_bg => [1, 1, 1, 1], 1029 border_bg => [1, 1, 1, 1],
1019 border => 0.6, 1030 border => 0.6,
1020 is_toplevel => 1,
1021 can_events => 1, 1031 can_events => 1,
1022 @_ 1032 @_
1023 ); 1033 );
1024 1034
1025 $self->{title} &&= new CFClient::UI::Label 1035 $self->{title} &&= new CFClient::UI::Label
1081 my ($ev, $x, $y) = @_; 1091 my ($ev, $x, $y) = @_;
1082 1092
1083 my $dx = $ev->{x} - $ox; 1093 my $dx = $ev->{x} - $ox;
1084 my $dy = $ev->{y} - $oy; 1094 my $dy = $ev->{y} - $oy;
1085 1095
1086 $self->{user_x} = $wx + $dx * $mx;
1087 $self->{user_y} = $wy + $dy * $my;
1088 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1); 1096 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
1089 $self->{def_h} = $bh + $dy * ($my ? -1 : 1); 1097 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1090 $self->move ($self->{user_x}, $self->{user_y}); 1098
1091 $self->realloc; 1099 $self->realloc;
1100 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1092 }; 1101 };
1093 1102
1094 } elsif ($lr ^ $td) { 1103 } elsif ($lr ^ $td) {
1095 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1104 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1096 my ($bx, $by) = ($self->{x}, $self->{y}); 1105 my ($bx, $by) = ($self->{x}, $self->{y});
1098 $self->{motion} = sub { 1107 $self->{motion} = sub {
1099 my ($ev, $x, $y) = @_; 1108 my ($ev, $x, $y) = @_;
1100 1109
1101 ($x, $y) = ($ev->{x}, $ev->{y}); 1110 ($x, $y) = ($ev->{x}, $ev->{y});
1102 1111
1103 $self->{user_x} = $bx + $x - $ox; 1112 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1104 $self->{user_y} = $by + $y - $oy;
1105
1106 $self->move ($self->{user_x}, $self->{user_y});
1107 $self->realloc;
1108 }; 1113 };
1109 } 1114 }
1110} 1115}
1111 1116
1112sub button_up { 1117sub button_up {
2158 fg => [1, 1, 1], 2163 fg => [1, 1, 1],
2159 active_fg => [0, 0, 0], 2164 active_fg => [0, 0, 0],
2160 bg => [0, 0, 0, 0.2], 2165 bg => [0, 0, 0, 0.2],
2161 active_bg => [1, 1, 1, 0.5], 2166 active_bg => [1, 1, 1, 0.5],
2162 range => [0, 0, 100, 10, 0], 2167 range => [0, 0, 100, 10, 0],
2163 req_w => $::WIDTH / 80, 2168 min_w => $::WIDTH / 80,
2164 req_h => $::WIDTH / 80, 2169 min_h => $::WIDTH / 80,
2165 vertical => 0, 2170 vertical => 0,
2166 can_hover => 1, 2171 can_hover => 1,
2167 inner_pad => 0.02, 2172 inner_pad => 0.02,
2168 @_ 2173 @_
2169 ); 2174 );
2209} 2214}
2210 2215
2211sub size_request { 2216sub size_request {
2212 my ($self) = @_; 2217 my ($self) = @_;
2213 2218
2214 my $w = $self->{req_w}; 2219 ($self->{req_w}, $self->{req_h})
2215 my $h = $self->{req_h};
2216
2217 $self->{vertical} ? ($h, $w) : ($w, $h)
2218} 2220}
2219 2221
2220sub button_down { 2222sub button_down {
2221 my ($self, $ev, $x, $y) = @_; 2223 my ($self, $ev, $x, $y) = @_;
2222 2224
2649 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 2651 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2650 2652
2651 ($x, $y) = $widget->coord2global (-$self->{w}, 0) 2653 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2652 if $x + $self->{w} > $::WIDTH; 2654 if $x + $self->{w} > $::WIDTH;
2653 2655
2654 $self->move ($x, $y); 2656 $self->move_abs ($x, $y);
2655 }); 2657 });
2656} 2658}
2657 2659
2658sub _draw { 2660sub _draw {
2659 my ($self) = @_; 2661 my ($self) = @_;
3028 my ($self) = @_; 3030 my ($self) = @_;
3029 3031
3030 ($self->{w}, $self->{h}) 3032 ($self->{w}, $self->{h})
3031} 3033}
3032 3034
3035sub _to_pixel {
3036 my ($coord, $size, $max) = @_;
3037
3038 $coord =
3039 $coord eq "center" ? ($max - $size) * 0.5
3040 : $coord eq "max" ? $max
3041 : $coord;
3042
3043 $coord = 0 if $coord < 0;
3044 $coord = $max - $size if $coord > $max - $size;
3045
3046 int $coord + 0.5
3047}
3048
3033sub size_allocate { 3049sub size_allocate {
3034 my ($self, $w, $h, $changed) = @_; 3050 my ($self, $w, $h, $changed) = @_;
3035 3051
3036 for my $child ($self->children) { 3052 for my $child ($self->children) {
3037 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3053 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3038 3054
3039 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3055 $X = $child->{force_x} if exists $child->{force_x};
3040 if exists $child->{req_x}; 3056 $Y = $child->{force_y} if exists $child->{force_y};
3041 3057
3042 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3058 $X = _to_pixel $X, $W, $self->{w};
3043 if exists $child->{req_y}; 3059 $Y = _to_pixel $Y, $H, $self->{h};
3044
3045 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3046
3047 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3048 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3049 3060
3050 $child->configure ($X, $Y, $W, $H); 3061 $child->configure ($X, $Y, $W, $H);
3051 } 3062 }
3052} 3063}
3053 3064
3070} 3081}
3071 3082
3072sub add { 3083sub add {
3073 my ($self, @children) = @_; 3084 my ($self, @children) = @_;
3074 3085
3075 for my $child (@children) {
3076 $child->{is_toplevel} = 1; 3086 $_->{is_toplevel} = 1
3077 3087 for @children;
3078 # integerise window positions
3079 $child->{x} = int $child->{x};
3080 $child->{y} = int $child->{y};
3081 }
3082 3088
3083 $self->SUPER::add (@children); 3089 $self->SUPER::add (@children);
3084} 3090}
3085 3091
3086sub remove { 3092sub remove {
3087 my ($self, @children) = @_; 3093 my ($self, @children) = @_;
3088 3094
3089 $self->SUPER::remove (@children); 3095 $self->SUPER::remove (@children);
3096
3097 delete $self->{is_toplevel}
3098 for @children;
3090 3099
3091 while (@children) { 3100 while (@children) {
3092 my $w = pop @children; 3101 my $w = pop @children;
3093 push @children, $w->children; 3102 push @children, $w->children;
3094 $w->set_invisible; 3103 $w->set_invisible;
3128 3137
3129 my $widget = pop @queue || last; 3138 my $widget = pop @queue || last;
3130 3139
3131 $widget->{visible} or last; # do not resize invisible widgets 3140 $widget->{visible} or last; # do not resize invisible widgets
3132 3141
3133 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3134 ? @$widget{qw(def_w def_h)}
3135 : $widget->size_request; 3142 my ($w, $h) = $widget->size_request;
3136 3143
3137 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3144 my $min_size = $widget->{is_toplevel} ? 16 : 0;
3138 3145
3146 $w = List::Util::max $widget->{min_w} || $widget->{is_toplevel} * 16, $w;
3147 $h = List::Util::max $widget->{min_h} || $widget->{is_toplevel} * 16, $h;
3148
3149 $w = $widget->{force_w} if exists $widget->{force_w};
3150 $h = $widget->{force_h} if exists $widget->{force_h};
3151
3139 $widget->{req_w} = $w; 3152 $widget->{req_w} = $w;
3140 $widget->{req_h} = $h; 3153 $widget->{req_h} = $h;
3141 3154
3142 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3155 $self->{size_alloc}{$widget} = [$widget, undef, undef];
3143 3156
3144 push @queue, $widget->{parent} 3157 push @queue, $widget->{parent}
3145 if $widget->{parent}; 3158 if ($self->{w} != $w || $self->{h} != $h) && $widget->{parent};
3146 } 3159 }
3147 } 3160 }
3148 3161
3149 while (my $size_alloc = delete $self->{size_alloc}) { 3162 while (my $size_alloc = delete $self->{size_alloc}) {
3150 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible}, 3163 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3151 values %$size_alloc; 3164 values %$size_alloc;
3152 3165
3153 while () { 3166 while () {
3154 my ($widget, $w, $h) = @{ pop @queue or last }; 3167 my ($widget, $w, $h) = @{ pop @queue or last };
3155 3168
3169 $w = $widget->{w} || $widget->{req_w} unless defined $w;
3170 $h = $widget->{h} || $widget->{req_h} unless defined $h;
3171
3156 $w = 0 if $w < 0; 3172 $w = 0 if $w < 0;
3157 $h = 0 if $h < 0; 3173 $h = 0 if $h < 0;
3174
3175 $w = int $w + 0.5;
3176 $h = int $h + 0.5;
3158 3177
3159 my $changed = $widget->{w} != $w || $widget->{h} != $h; 3178 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3160 3179
3161 $widget->{w} = $w; 3180 $widget->{w} = $w;
3162 $widget->{h} = $h; 3181 $widget->{h} = $h;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines