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.249 by root, Sun May 28 01:40:02 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
242 %$self = (); 240 %$self = ();
243} 241}
244 242
245sub show { 243sub show {
246 my ($self) = @_; 244 my ($self) = @_;
245
247 return if $self->{parent}; 246 return if $self->{parent};
248 247
249 $CFClient::UI::ROOT->add ($self); 248 $CFClient::UI::ROOT->add ($self);
250} 249}
251 250
252sub show_centered { 251sub center {
253 my ($self) = @_; 252 my ($self) = @_;
254 return if $self->{parent};
255
256 $self->show;
257 253
258 $CFClient::UI::ROOT->on_post_alloc ( 254 $CFClient::UI::ROOT->on_post_alloc (
259 "centered $self" => sub { 255 "center_$self" => sub {
260 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 256 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
261 }, 257 },
262 ); 258 );
259
260 $self->update;
261}
262
263sub set_visible {
264 my ($self) = @_;
265
266 return if $self->{visible};
267
268 $self->{root} = $self->{parent}{root};
269 $self->{visible} = $self->{parent}{visible} + 1;
270
271 $self->emit (visibility_change => 1);
263} 272}
264 273
265sub set_invisible { 274sub set_invisible {
266 my ($self) = @_; 275 my ($self) = @_;
267 276
303} 312}
304 313
305sub set_size { 314sub set_size {
306 my ($self, $w, $h) = @_; 315 my ($self, $w, $h) = @_;
307 316
308 $self->{user_w} = $w; 317 $self->{def_w} = $w;
309 $self->{user_h} = $h; 318 $self->{def_h} = $h;
310 319
311 $self->check_size; 320 $self->check_size;
312} 321}
313 322
314sub size_request { 323sub size_request {
500sub set_parent { 509sub set_parent {
501 my ($self, $parent) = @_; 510 my ($self, $parent) = @_;
502 511
503 Scalar::Util::weaken ($self->{parent} = $parent); 512 Scalar::Util::weaken ($self->{parent} = $parent);
504 513
505 if ($parent->{visible} || 1) { 514 $self->set_visible; #TODO why breakssssss borked damn if $parent->{visible};
506 $self->{root} = $parent->{root};
507 $self->{visible} = $parent->{visible} + 1;
508 515
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 516 $self->check_size;
515 unless exists $self->{req_w};
516
517 $self->show;
518} 517}
519 518
520sub check_size { 519sub check_size {
521 my ($self, $forced) = @_; 520 my ($self, $forced) = @_;
522 521
835} 834}
836 835
837sub size_request { 836sub size_request {
838 my ($self) = @_; 837 my ($self) = @_;
839 838
840 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 839 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
841 840
842 @$self{qw(child_w child_h)} 841 $w = 10 if $self->{scroll_x};
842 $h = 10 if $self->{scroll_y};
843
844 ($w, $h)
843} 845}
844 846
845sub size_allocate { 847sub size_allocate {
846 my ($self, $w, $h) = @_; 848 my ($self, $w, $h) = @_;
847 849
1003 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1005 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); 1006 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1005 1007
1006sub new { 1008sub new {
1007 my $class = shift; 1009 my $class = shift;
1008
1009 # TODO: user_x, user_y, overwrite moveto?
1010 1010
1011 my $self = $class->SUPER::new ( 1011 my $self = $class->SUPER::new (
1012 bg => [1, 1, 1, 1], 1012 bg => [1, 1, 1, 1],
1013 border_bg => [1, 1, 1, 1], 1013 border_bg => [1, 1, 1, 1],
1014 border => 0.6, 1014 border => 0.6,
1076 my $dx = $ev->{x} - $ox; 1076 my $dx = $ev->{x} - $ox;
1077 my $dy = $ev->{y} - $oy; 1077 my $dy = $ev->{y} - $oy;
1078 1078
1079 $self->{user_x} = $wx + $dx * $mx; 1079 $self->{user_x} = $wx + $dx * $mx;
1080 $self->{user_y} = $wy + $dy * $my; 1080 $self->{user_y} = $wy + $dy * $my;
1081 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1081 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
1082 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1082 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
1083 $self->move ($self->{user_x}, $self->{user_y}); 1083 $self->move ($self->{user_x}, $self->{user_y});
1084 $self->check_size; 1084 $self->check_size;
1085 }; 1085 };
1086 1086
1087 } elsif ($lr ^ $td) { 1087 } elsif ($lr ^ $td) {
1093 1093
1094 ($x, $y) = ($ev->{x}, $ev->{y}); 1094 ($x, $y) = ($ev->{x}, $ev->{y});
1095 1095
1096 $self->{user_x} = $bx + $x - $ox; 1096 $self->{user_x} = $bx + $x - $ox;
1097 $self->{user_y} = $by + $y - $oy; 1097 $self->{user_y} = $by + $y - $oy;
1098
1098 $self->move ($self->{user_x}, $self->{user_y}); 1099 $self->move ($self->{user_x}, $self->{user_y});
1099 $self->update; 1100 $self->update;
1100 }; 1101 };
1101 } 1102 }
1102} 1103}
1301 } 1302 }
1302} 1303}
1303 1304
1304############################################################################# 1305#############################################################################
1305 1306
1306package CFClient::UI::HBox; 1307package CFClient::UI::Box;
1307
1308# TODO: wrap into common Box base class
1309 1308
1310our @ISA = CFClient::UI::Container::; 1309our @ISA = CFClient::UI::Container::;
1311 1310
1312sub size_request { 1311sub size_request {
1313 my ($self) = @_; 1312 my ($self) = @_;
1314 1313
1315 my @alloc = map [$_->size_request], @{$self->{children}}; 1314 $self->{vertical}
1316 1315 ? (
1317 ( 1316 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1318 (List::Util::sum map $_->[0], @alloc), 1317 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1319 (List::Util::max map $_->[1], @alloc), 1318 )
1320 ) 1319 : (
1320 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1321 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1322 )
1321} 1323}
1322 1324
1323sub size_allocate { 1325sub size_allocate {
1324 my ($self, $w, $h) = @_; 1326 my ($self, $w, $h) = @_;
1325 1327
1326 ($h, $w) = ($w, $h); 1328 my $space = $self->{vertical} ? $h : $w;
1327
1328 my $children = $self->{children}; 1329 my $children = $self->{children};
1329 1330
1330 my @h = map $_->{req_w}, @$children; 1331 my @req;
1331 1332
1332 my $req_h = List::Util::sum @h; 1333 if ($self->{homogeneous}) {
1333 1334 @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 { 1335 } else {
1336 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1337 my $req = List::Util::sum @req;
1338
1339 if ($req > $space) {
1340 # ah well, not enough space
1341 $_ *= $space / $req for @req;
1342 } else {
1338 my $exp = List::Util::sum map $_->{expand}, @$children; 1343 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1339 $exp ||= 1;
1340 1344
1345 $space = ($space - $req) / $expand; # remaining space to give away
1346
1347 $req[$_] += $space * $children->[$_]{expand}
1341 for (0 .. $#$children) { 1348 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 } 1349 }
1348 } 1350 }
1349 1351
1350 CFClient::UI::harmonize \@h; 1352 CFClient::UI::harmonize \@req;
1351 1353
1352 my $y = 0; 1354 my $pos = 0;
1353 for (0 .. $#$children) { 1355 for (0 .. $#$children) {
1354 my $child = $children->[$_];
1355 my $h = $h[$_]; 1356 my $alloc = $req[$_];
1356 $child->configure ($y, 0, $h, $w); 1357 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1357 1358
1358 $y += $h; 1359 $pos += $alloc;
1359 } 1360 }
1360 1361
1361 1 1362 1
1362} 1363}
1363 1364
1364############################################################################# 1365#############################################################################
1365 1366
1367package CFClient::UI::HBox;
1368
1369our @ISA = CFClient::UI::Box::;
1370
1371sub new {
1372 my $class = shift;
1373
1374 $class->SUPER::new (
1375 vertical => 0,
1376 @_,
1377 )
1378}
1379
1380#############################################################################
1381
1366package CFClient::UI::VBox; 1382package CFClient::UI::VBox;
1367 1383
1368# TODO: wrap into common Box base class
1369
1370our @ISA = CFClient::UI::Container::; 1384our @ISA = CFClient::UI::Box::;
1371 1385
1372sub size_request { 1386sub new {
1373 my ($self) = @_; 1387 my $class = shift;
1374 1388
1375 my @alloc = map [$_->size_request], @{$self->{children}}; 1389 $class->SUPER::new (
1376 1390 vertical => 1,
1377 ( 1391 @_,
1378 (List::Util::max map $_->[0], @alloc),
1379 (List::Util::sum map $_->[1], @alloc),
1380 ) 1392 )
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} 1393}
1421 1394
1422############################################################################# 1395#############################################################################
1423 1396
1424package CFClient::UI::Label; 1397package CFClient::UI::Label;
2802 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 2775 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2803 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 2776 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2804 2777
2805 $row++; 2778 $row++;
2806 } 2779 }
2807}
2808
2809sub size_request {
2810 my ($self) = @_;
2811 ($self->{req_w}, $self->{req_h});
2812} 2780}
2813 2781
2814############################################################################# 2782#############################################################################
2815 2783
2816package CFClient::UI::Menu; 2784package CFClient::UI::Menu;
3040 if exists $child->{req_x}; 3008 if exists $child->{req_x};
3041 3009
3042 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3010 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3043 if exists $child->{req_y}; 3011 if exists $child->{req_y};
3044 3012
3045 $X = $self->{user_x} if exists $self->{user_x}; 3013 delete @$child{qw(req_x req_y)};
3046 $Y = $self->{user_y} if exists $self->{user_y};
3047 3014
3048 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3015 $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; 3016 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3050 3017
3051 $child->configure ($X, $Y, $W, $H); 3018 $child->configure ($X, $Y, $W, $H);
3071 $::WANT_REFRESH++; 3038 $::WANT_REFRESH++;
3072} 3039}
3073 3040
3074sub add { 3041sub add {
3075 my ($self, @children) = @_; 3042 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 3043
3083 for my $child (@children) { 3044 for my $child (@children) {
3084 $child->{is_toplevel} = 1; 3045 $child->{is_toplevel} = 1;
3085 3046
3086 # integerise window positions 3047 # integerise window positions
3087 $child->{x} = int $child->{x}; 3048 $child->{x} = int $child->{x};
3088 $child->{y} = int $child->{y}; 3049 $child->{y} = int $child->{y};
3089 } 3050 }
3090 3051
3091 $self->SUPER::add (@children); 3052 $self->SUPER::add (@children);
3053
3054 for (my @widgets = @children; my $w = pop @widgets; ) {
3055 push @widgets, $w->children;
3056 $w->set_visible;
3057 }
3058
3092} 3059}
3093 3060
3094sub remove { 3061sub remove {
3095 my ($self, @children) = @_; 3062 my ($self, @children) = @_;
3096 3063
3136 3103
3137 my $widget = pop @queue || last; 3104 my $widget = pop @queue || last;
3138 3105
3139 defined $widget->{visible} or last; # do not resize invisible widgets 3106 defined $widget->{visible} or last; # do not resize invisible widgets
3140 3107
3141 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3108 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3142 ? @$widget{qw(user_w user_h)} 3109 ? @$widget{qw(def_w def_h)}
3143 : $widget->size_request; 3110 : $widget->size_request;
3144 3111
3145 if (delete $widget->{force_alloc} 3112 if (delete $widget->{force_alloc}
3146 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3113 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3147 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3114 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
3156 } 3123 }
3157 } 3124 }
3158 } 3125 }
3159 3126
3160 while ($self->{size_alloc}) { 3127 while ($self->{size_alloc}) {
3128 for (
3129 sort { $a->[0]{visible} <=> $b->[0]{visible} }
3161 for (values %{delete $self->{size_alloc}}) { 3130 values %{delete $self->{size_alloc}}
3131 ) {
3162 my ($widget, $w, $h) = @$_; 3132 my ($widget, $w, $h) = @$_;
3163 3133
3164 $w = 0 if $w < 0; 3134 $w = 0 if $w < 0;
3165 $h = 0 if $h < 0; 3135 $h = 0 if $h < 0;
3166 3136

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines