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.245 by elmex, Sat May 27 21:15:57 2006 UTC vs.
Revision 1.247 by root, Sun May 28 01:16:04 2006 UTC

175 for my $widget (values %WIDGET) { 175 for my $widget (values %WIDGET) {
176 if ($widget->{is_toplevel}) { 176 if ($widget->{is_toplevel}) {
177 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 177 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
178 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 178 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
179 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 179 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
180 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
181 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 180 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
182 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 181 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
183 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 182 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
184 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
185 } 183 }
186 } 184 }
187 185
188 reconfigure_widgets; 186 reconfigure_widgets;
189} 187}
214 } 212 }
215 213
216 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 214 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
217 215
218 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { 216 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
219 $self->{user_x} = $layout->{x} * $::WIDTH; 217 $self->{req_x} = $layout->{x} * $::WIDTH;
220 $self->{user_y} = $layout->{y} * $::HEIGHT; 218 $self->{req_y} = $layout->{y} * $::HEIGHT;
221 $self->{user_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH; 219 $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH;
222 $self->{user_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT; 220 $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT;
223 } 221 }
224 222
225 $self 223 $self
226} 224}
227 225
247 return if $self->{parent}; 245 return if $self->{parent};
248 246
249 $CFClient::UI::ROOT->add ($self); 247 $CFClient::UI::ROOT->add ($self);
250} 248}
251 249
252sub show_centered { 250sub center {
253 my ($self) = @_; 251 my ($self) = @_;
254 return if $self->{parent};
255
256 $self->show;
257 252
258 $CFClient::UI::ROOT->on_post_alloc ( 253 $CFClient::UI::ROOT->on_post_alloc (
259 "centered $self" => sub { 254 "center_$self" => sub {
260 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 255 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5);
261 }, 256 },
262 ); 257 );
258
259 $self->update;
260}
261
262sub set_visible {
263 my ($self) = @_;
264
265 return if $self->{visible};
266
267 $self->{root} = $self->{parent}{root};
268 $self->{visible} = $self->{parent}{visible} + 1;
269
270 $self->emit (visibility_change => 1)
263} 271}
264 272
265sub set_invisible { 273sub set_invisible {
266 my ($self) = @_; 274 my ($self) = @_;
267 275
303} 311}
304 312
305sub set_size { 313sub set_size {
306 my ($self, $w, $h) = @_; 314 my ($self, $w, $h) = @_;
307 315
308 $self->{user_w} = $w; 316 $self->{def_w} = $w;
309 $self->{user_h} = $h; 317 $self->{def_h} = $h;
310 318
311 $self->check_size; 319 $self->check_size;
312} 320}
313 321
314sub size_request { 322sub size_request {
500sub set_parent { 508sub set_parent {
501 my ($self, $parent) = @_; 509 my ($self, $parent) = @_;
502 510
503 Scalar::Util::weaken ($self->{parent} = $parent); 511 Scalar::Util::weaken ($self->{parent} = $parent);
504 512
505 if ($parent->{visible} || 1) { 513 $self->set_visible; #TODO why breakssssss borked damn if $parent->{visible};
506 $self->{root} = $parent->{root};
507 $self->{visible} = $parent->{visible} + 1;
508 514
509 $self->emit (visibility_change => 1)
510 unless $self->{parent}{visible};
511 }
512
513 # TODO: req_w _does_change after ->reconfigure
514 $self->check_size 515 $self->check_size;
515 unless exists $self->{req_w};
516
517 $self->show;
518} 516}
519 517
520sub check_size { 518sub check_size {
521 my ($self, $forced) = @_; 519 my ($self, $forced) = @_;
522 520
835} 833}
836 834
837sub size_request { 835sub size_request {
838 my ($self) = @_; 836 my ($self) = @_;
839 837
840 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 838 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
841 839
842 @$self{qw(child_w child_h)} 840 $w = 10 if $self->{scroll_x};
841 $h = 10 if $self->{scroll_y};
842
843 ($w, $h)
843} 844}
844 845
845sub size_allocate { 846sub size_allocate {
846 my ($self, $w, $h) = @_; 847 my ($self, $w, $h) = @_;
847 848
1003 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1004 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1004 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1005 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1005 1006
1006sub new { 1007sub new {
1007 my $class = shift; 1008 my $class = shift;
1008
1009 # TODO: user_x, user_y, overwrite moveto?
1010 1009
1011 my $self = $class->SUPER::new ( 1010 my $self = $class->SUPER::new (
1012 bg => [1, 1, 1, 1], 1011 bg => [1, 1, 1, 1],
1013 border_bg => [1, 1, 1, 1], 1012 border_bg => [1, 1, 1, 1],
1014 border => 0.6, 1013 border => 0.6,
1076 my $dx = $ev->{x} - $ox; 1075 my $dx = $ev->{x} - $ox;
1077 my $dy = $ev->{y} - $oy; 1076 my $dy = $ev->{y} - $oy;
1078 1077
1079 $self->{user_x} = $wx + $dx * $mx; 1078 $self->{user_x} = $wx + $dx * $mx;
1080 $self->{user_y} = $wy + $dy * $my; 1079 $self->{user_y} = $wy + $dy * $my;
1081 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1080 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
1082 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1081 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
1083 $self->move ($self->{user_x}, $self->{user_y}); 1082 $self->move ($self->{user_x}, $self->{user_y});
1084 $self->check_size; 1083 $self->check_size;
1085 }; 1084 };
1086 1085
1087 } elsif ($lr ^ $td) { 1086 } elsif ($lr ^ $td) {
1093 1092
1094 ($x, $y) = ($ev->{x}, $ev->{y}); 1093 ($x, $y) = ($ev->{x}, $ev->{y});
1095 1094
1096 $self->{user_x} = $bx + $x - $ox; 1095 $self->{user_x} = $bx + $x - $ox;
1097 $self->{user_y} = $by + $y - $oy; 1096 $self->{user_y} = $by + $y - $oy;
1097
1098 $self->move ($self->{user_x}, $self->{user_y}); 1098 $self->move ($self->{user_x}, $self->{user_y});
1099 $self->update; 1099 $self->update;
1100 }; 1100 };
1101 } 1101 }
1102} 1102}
1301 } 1301 }
1302} 1302}
1303 1303
1304############################################################################# 1304#############################################################################
1305 1305
1306package CFClient::UI::HBox; 1306package CFClient::UI::Box;
1307
1308# TODO: wrap into common Box base class
1309 1307
1310our @ISA = CFClient::UI::Container::; 1308our @ISA = CFClient::UI::Container::;
1311 1309
1312sub size_request { 1310sub size_request {
1313 my ($self) = @_; 1311 my ($self) = @_;
1314 1312
1315 my @alloc = map [$_->size_request], @{$self->{children}}; 1313 $self->{vertical}
1316 1314 ? (
1317 ( 1315 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1318 (List::Util::sum map $_->[0], @alloc), 1316 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1319 (List::Util::max map $_->[1], @alloc), 1317 )
1320 ) 1318 : (
1319 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1320 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1321 )
1321} 1322}
1322 1323
1323sub size_allocate { 1324sub size_allocate {
1324 my ($self, $w, $h) = @_; 1325 my ($self, $w, $h) = @_;
1325 1326
1326 ($h, $w) = ($w, $h); 1327 my $space = $self->{vertical} ? $h : $w;
1327
1328 my $children = $self->{children}; 1328 my $children = $self->{children};
1329 1329
1330 my @h = map $_->{req_w}, @$children; 1330 my @req;
1331 1331
1332 my $req_h = List::Util::sum @h; 1332 if ($self->{homogeneous}) {
1333 1333 @req = ($space / (@$children || 1)) x @$children;
1334 if ($req_h > $h) {
1335 # ah well, not enough space
1336 $_ *= $h / $req_h for @h;
1337 } else { 1334 } else {
1335 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1336 my $req = List::Util::sum @req;
1337
1338 if ($req > $space) {
1339 # ah well, not enough space
1340 $_ *= $space / $req for @req;
1341 } else {
1338 my $exp = List::Util::sum map $_->{expand}, @$children; 1342 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1339 $exp ||= 1;
1340 1343
1344 $space = ($space - $req) / $expand; # remaining space to give away
1345
1346 $req[$_] += $space * $children->[$_]{expand}
1341 for (0 .. $#$children) { 1347 for 0 .. $#$children;
1342 my $child = $children->[$_];
1343
1344 my $alloc_h = $h[$_];
1345 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1346 $h[$_] = $alloc_h;
1347 } 1348 }
1348 } 1349 }
1349 1350
1350 CFClient::UI::harmonize \@h; 1351 CFClient::UI::harmonize \@req;
1351 1352
1352 my $y = 0; 1353 my $pos = 0;
1353 for (0 .. $#$children) { 1354 for (0 .. $#$children) {
1354 my $child = $children->[$_];
1355 my $h = $h[$_]; 1355 my $alloc = $req[$_];
1356 $child->configure ($y, 0, $h, $w); 1356 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1357 1357
1358 $y += $h; 1358 $pos += $alloc;
1359 } 1359 }
1360 1360
1361 1 1361 1
1362} 1362}
1363 1363
1364############################################################################# 1364#############################################################################
1365 1365
1366package CFClient::UI::HBox;
1367
1368our @ISA = CFClient::UI::Box::;
1369
1370sub new {
1371 my $class = shift;
1372
1373 $class->SUPER::new (
1374 vertical => 0,
1375 @_,
1376 )
1377}
1378
1379#############################################################################
1380
1366package CFClient::UI::VBox; 1381package CFClient::UI::VBox;
1367 1382
1368# TODO: wrap into common Box base class
1369
1370our @ISA = CFClient::UI::Container::; 1383our @ISA = CFClient::UI::Box::;
1371 1384
1372sub size_request { 1385sub new {
1373 my ($self) = @_; 1386 my $class = shift;
1374 1387
1375 my @alloc = map [$_->size_request], @{$self->{children}}; 1388 $class->SUPER::new (
1376 1389 vertical => 1,
1377 ( 1390 @_,
1378 (List::Util::max map $_->[0], @alloc),
1379 (List::Util::sum map $_->[1], @alloc),
1380 ) 1391 )
1381}
1382
1383sub size_allocate {
1384 my ($self, $w, $h) = @_;
1385
1386 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1387
1388 my $children = $self->{children};
1389
1390 my @h = map $_->{req_h}, @$children;
1391
1392 my $req_h = List::Util::sum @h;
1393
1394 if ($req_h > $h) {
1395 # ah well, not enough space
1396 $_ *= $h / $req_h for @h;
1397 } else {
1398 my $exp = List::Util::sum map $_->{expand}, @$children;
1399 $exp ||= 1;
1400
1401 for (0 .. $#$children) {
1402 my $child = $children->[$_];
1403
1404 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1405 }
1406 }
1407
1408 CFClient::UI::harmonize \@h;
1409
1410 my $y = 0;
1411 for (0 .. $#$children) {
1412 my $child = $children->[$_];
1413 my $h = $h[$_];
1414 $child->configure (0, $y, $w, $h);
1415
1416 $y += $h;
1417 }
1418
1419 1
1420} 1392}
1421 1393
1422############################################################################# 1394#############################################################################
1423 1395
1424package CFClient::UI::Label; 1396package CFClient::UI::Label;
2802 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 2774 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2803 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 2775 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2804 2776
2805 $row++; 2777 $row++;
2806 } 2778 }
2807}
2808
2809sub size_request {
2810 my ($self) = @_;
2811 ($self->{req_w}, $self->{req_h});
2812} 2779}
2813 2780
2814############################################################################# 2781#############################################################################
2815 2782
2816package CFClient::UI::Menu; 2783package CFClient::UI::Menu;
3040 if exists $child->{req_x}; 3007 if exists $child->{req_x};
3041 3008
3042 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3009 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3043 if exists $child->{req_y}; 3010 if exists $child->{req_y};
3044 3011
3045 $X = $self->{user_x} if exists $self->{user_x}; 3012 delete @$child{qw(req_x req_y)};
3046 $Y = $self->{user_y} if exists $self->{user_y};
3047 3013
3048 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3014 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3049 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; 3015 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3050 3016
3051 $child->configure ($X, $Y, $W, $H); 3017 $child->configure ($X, $Y, $W, $H);
3071 $::WANT_REFRESH++; 3037 $::WANT_REFRESH++;
3072} 3038}
3073 3039
3074sub add { 3040sub add {
3075 my ($self, @children) = @_; 3041 my ($self, @children) = @_;
3076
3077 for (my @widgets = @children; my $w = pop @widgets; ) {
3078 push @widgets, $w->children;
3079 $w->{root} = $self;
3080 $w->{visible} = $self->{visible} + 1;
3081 }
3082 3042
3083 for my $child (@children) { 3043 for my $child (@children) {
3084 $child->{is_toplevel} = 1; 3044 $child->{is_toplevel} = 1;
3085 3045
3086 # integerise window positions 3046 # integerise window positions
3087 $child->{x} = int $child->{x}; 3047 $child->{x} = int $child->{x};
3088 $child->{y} = int $child->{y}; 3048 $child->{y} = int $child->{y};
3089 } 3049 }
3090 3050
3091 $self->SUPER::add (@children); 3051 $self->SUPER::add (@children);
3052
3053 for (my @widgets = @children; my $w = pop @widgets; ) {
3054 push @widgets, $w->children;
3055 $w->set_visible;
3056 }
3057
3092} 3058}
3093 3059
3094sub remove { 3060sub remove {
3095 my ($self, @children) = @_; 3061 my ($self, @children) = @_;
3096 3062
3136 3102
3137 my $widget = pop @queue || last; 3103 my $widget = pop @queue || last;
3138 3104
3139 defined $widget->{visible} or last; # do not resize invisible widgets 3105 defined $widget->{visible} or last; # do not resize invisible widgets
3140 3106
3141 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3107 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3142 ? @$widget{qw(user_w user_h)} 3108 ? @$widget{qw(def_w def_h)}
3143 : $widget->size_request; 3109 : $widget->size_request;
3144 3110
3145 if (delete $widget->{force_alloc} 3111 if (delete $widget->{force_alloc}
3146 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3112 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3147 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3113 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines