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.250 by root, Sun May 28 02:31:03 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}
227
228sub toggle_visibility {
229 my ($self) = @_;
230
231 if ($self->{visible}) {
232 $self->hide;
233 } else {
234 $self->show;
235 }
236} 224}
237 225
238sub destroy { 226sub destroy {
239 my ($self) = @_; 227 my ($self) = @_;
240 228
242 %$self = (); 230 %$self = ();
243} 231}
244 232
245sub show { 233sub show {
246 my ($self) = @_; 234 my ($self) = @_;
235
247 return if $self->{parent}; 236 return if $self->{parent};
248 237
249 $CFClient::UI::ROOT->add ($self); 238 $CFClient::UI::ROOT->add ($self);
250} 239}
251 240
252sub show_centered { 241sub center {
253 my ($self) = @_; 242 my ($self) = @_;
254 return if $self->{parent};
255
256 $self->show;
257 243
258 $CFClient::UI::ROOT->on_post_alloc ( 244 $CFClient::UI::ROOT->on_post_alloc (
259 "centered $self" => sub { 245 "center_$self" => sub {
260 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 246 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
261 }, 247 },
262 ); 248 );
249
250 $self->update;
251}
252
253sub set_visible {
254 my ($self) = @_;
255
256 return if $self->{visible};
257
258 $self->{root} = $self->{parent}{root};
259 $self->{visible} = $self->{parent}{visible} + 1;
260
261 $self->emit (visibility_change => 1);
263} 262}
264 263
265sub set_invisible { 264sub set_invisible {
266 my ($self) = @_; 265 my ($self) = @_;
267 266
279 if $CFClient::UI::TOOLTIP->{owner} == $self; 278 if $CFClient::UI::TOOLTIP->{owner} == $self;
280 279
281 $self->focus_out; 280 $self->focus_out;
282 281
283 $self->emit (visibility_change => 0); 282 $self->emit (visibility_change => 0);
283}
284
285sub set_visibility {
286 my ($self, $visible) = @_;
287
288 return if $self->{visible} == $visible;
289
290 $visible ? $self->hide
291 : $self->show;
292}
293
294sub toggle_visibility {
295 my ($self) = @_;
296
297 $self->{visible}
298 ? $self->hide
299 : $self->show;
284} 300}
285 301
286sub hide { 302sub hide {
287 my ($self) = @_; 303 my ($self) = @_;
288 304
303} 319}
304 320
305sub set_size { 321sub set_size {
306 my ($self, $w, $h) = @_; 322 my ($self, $w, $h) = @_;
307 323
308 $self->{user_w} = $w; 324 $self->{def_w} = $w;
309 $self->{user_h} = $h; 325 $self->{def_h} = $h;
310 326
311 $self->check_size; 327 $self->check_size;
312} 328}
313 329
314sub size_request { 330sub size_request {
336 $self->{y} = $y; 352 $self->{y} = $y;
337 $self->update; 353 $self->update;
338 } 354 }
339 355
340 if ($self->{w} != $w || $self->{h} != $h) { 356 if ($self->{w} != $w || $self->{h} != $h) {
341 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 357 $CFClient::UI::ROOT->{size_alloc}{$self+0} = [$self, $w, $h];
342 } 358 }
343} 359}
344 360
345sub size_allocate { 361sub size_allocate {
346 # nothing to be done 362 # nothing to be done
500sub set_parent { 516sub set_parent {
501 my ($self, $parent) = @_; 517 my ($self, $parent) = @_;
502 518
503 Scalar::Util::weaken ($self->{parent} = $parent); 519 Scalar::Util::weaken ($self->{parent} = $parent);
504 520
505 if ($parent->{visible} || 1) { 521 $self->set_visible; #TODO why breakssssss borked damn if $parent->{visible};
506 $self->{root} = $parent->{root};
507 $self->{visible} = $parent->{visible} + 1;
508 522
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 523 $self->check_size;
515 unless exists $self->{req_w};
516
517 $self->show;
518} 524}
519 525
520sub check_size { 526sub check_size {
521 my ($self, $forced) = @_; 527 my ($self, $forced) = @_;
522 528
835} 841}
836 842
837sub size_request { 843sub size_request {
838 my ($self) = @_; 844 my ($self) = @_;
839 845
840 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 846 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
841 847
842 @$self{qw(child_w child_h)} 848 $w = 10 if $self->{scroll_x};
849 $h = 10 if $self->{scroll_y};
850
851 ($w, $h)
843} 852}
844 853
845sub size_allocate { 854sub size_allocate {
846 my ($self, $w, $h) = @_; 855 my ($self, $w, $h) = @_;
847 856
1003 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1012 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); 1013 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1005 1014
1006sub new { 1015sub new {
1007 my $class = shift; 1016 my $class = shift;
1008
1009 # TODO: user_x, user_y, overwrite moveto?
1010 1017
1011 my $self = $class->SUPER::new ( 1018 my $self = $class->SUPER::new (
1012 bg => [1, 1, 1, 1], 1019 bg => [1, 1, 1, 1],
1013 border_bg => [1, 1, 1, 1], 1020 border_bg => [1, 1, 1, 1],
1014 border => 0.6, 1021 border => 0.6,
1076 my $dx = $ev->{x} - $ox; 1083 my $dx = $ev->{x} - $ox;
1077 my $dy = $ev->{y} - $oy; 1084 my $dy = $ev->{y} - $oy;
1078 1085
1079 $self->{user_x} = $wx + $dx * $mx; 1086 $self->{user_x} = $wx + $dx * $mx;
1080 $self->{user_y} = $wy + $dy * $my; 1087 $self->{user_y} = $wy + $dy * $my;
1081 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1088 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
1082 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1089 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
1083 $self->move ($self->{user_x}, $self->{user_y}); 1090 $self->move ($self->{user_x}, $self->{user_y});
1084 $self->check_size; 1091 $self->check_size;
1085 }; 1092 };
1086 1093
1087 } elsif ($lr ^ $td) { 1094 } elsif ($lr ^ $td) {
1093 1100
1094 ($x, $y) = ($ev->{x}, $ev->{y}); 1101 ($x, $y) = ($ev->{x}, $ev->{y});
1095 1102
1096 $self->{user_x} = $bx + $x - $ox; 1103 $self->{user_x} = $bx + $x - $ox;
1097 $self->{user_y} = $by + $y - $oy; 1104 $self->{user_y} = $by + $y - $oy;
1105
1098 $self->move ($self->{user_x}, $self->{user_y}); 1106 $self->move ($self->{user_x}, $self->{user_y});
1099 $self->update; 1107 $self->update;
1100 }; 1108 };
1101 } 1109 }
1102} 1110}
1301 } 1309 }
1302} 1310}
1303 1311
1304############################################################################# 1312#############################################################################
1305 1313
1306package CFClient::UI::HBox; 1314package CFClient::UI::Box;
1307
1308# TODO: wrap into common Box base class
1309 1315
1310our @ISA = CFClient::UI::Container::; 1316our @ISA = CFClient::UI::Container::;
1311 1317
1312sub size_request { 1318sub size_request {
1313 my ($self) = @_; 1319 my ($self) = @_;
1314 1320
1315 my @alloc = map [$_->size_request], @{$self->{children}}; 1321 $self->{vertical}
1316 1322 ? (
1317 ( 1323 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1318 (List::Util::sum map $_->[0], @alloc), 1324 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1319 (List::Util::max map $_->[1], @alloc), 1325 )
1320 ) 1326 : (
1327 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1328 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1329 )
1321} 1330}
1322 1331
1323sub size_allocate { 1332sub size_allocate {
1324 my ($self, $w, $h) = @_; 1333 my ($self, $w, $h) = @_;
1325 1334
1326 ($h, $w) = ($w, $h); 1335 my $space = $self->{vertical} ? $h : $w;
1327
1328 my $children = $self->{children}; 1336 my $children = $self->{children};
1329 1337
1330 my @h = map $_->{req_w}, @$children; 1338 my @req;
1331 1339
1332 my $req_h = List::Util::sum @h; 1340 if ($self->{homogeneous}) {
1333 1341 @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 { 1342 } else {
1343 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1344 my $req = List::Util::sum @req;
1345
1346 if ($req > $space) {
1347 # ah well, not enough space
1348 $_ *= $space / $req for @req;
1349 } else {
1338 my $exp = List::Util::sum map $_->{expand}, @$children; 1350 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1339 $exp ||= 1;
1340 1351
1352 $space = ($space - $req) / $expand; # remaining space to give away
1353
1354 $req[$_] += $space * $children->[$_]{expand}
1341 for (0 .. $#$children) { 1355 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 } 1356 }
1348 } 1357 }
1349 1358
1350 CFClient::UI::harmonize \@h; 1359 CFClient::UI::harmonize \@req;
1351 1360
1352 my $y = 0; 1361 my $pos = 0;
1353 for (0 .. $#$children) { 1362 for (0 .. $#$children) {
1354 my $child = $children->[$_];
1355 my $h = $h[$_]; 1363 my $alloc = $req[$_];
1356 $child->configure ($y, 0, $h, $w); 1364 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1357 1365
1358 $y += $h; 1366 $pos += $alloc;
1359 } 1367 }
1360 1368
1361 1 1369 1
1362} 1370}
1363 1371
1364############################################################################# 1372#############################################################################
1365 1373
1374package CFClient::UI::HBox;
1375
1376our @ISA = CFClient::UI::Box::;
1377
1378sub new {
1379 my $class = shift;
1380
1381 $class->SUPER::new (
1382 vertical => 0,
1383 @_,
1384 )
1385}
1386
1387#############################################################################
1388
1366package CFClient::UI::VBox; 1389package CFClient::UI::VBox;
1367 1390
1368# TODO: wrap into common Box base class
1369
1370our @ISA = CFClient::UI::Container::; 1391our @ISA = CFClient::UI::Box::;
1371 1392
1372sub size_request { 1393sub new {
1373 my ($self) = @_; 1394 my $class = shift;
1374 1395
1375 my @alloc = map [$_->size_request], @{$self->{children}}; 1396 $class->SUPER::new (
1376 1397 vertical => 1,
1377 ( 1398 @_,
1378 (List::Util::max map $_->[0], @alloc),
1379 (List::Util::sum map $_->[1], @alloc),
1380 ) 1399 )
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} 1400}
1421 1401
1422############################################################################# 1402#############################################################################
1423 1403
1424package CFClient::UI::Label; 1404package CFClient::UI::Label;
2802 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 2782 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2803 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 2783 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2804 2784
2805 $row++; 2785 $row++;
2806 } 2786 }
2807}
2808
2809sub size_request {
2810 my ($self) = @_;
2811 ($self->{req_w}, $self->{req_h});
2812} 2787}
2813 2788
2814############################################################################# 2789#############################################################################
2815 2790
2816package CFClient::UI::Menu; 2791package CFClient::UI::Menu;
3040 if exists $child->{req_x}; 3015 if exists $child->{req_x};
3041 3016
3042 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3017 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3043 if exists $child->{req_y}; 3018 if exists $child->{req_y};
3044 3019
3045 $X = $self->{user_x} if exists $self->{user_x}; 3020 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3046 $Y = $self->{user_y} if exists $self->{user_y};
3047 3021
3048 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3022 $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; 3023 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3050 3024
3051 $child->configure ($X, $Y, $W, $H); 3025 $child->configure ($X, $Y, $W, $H);
3071 $::WANT_REFRESH++; 3045 $::WANT_REFRESH++;
3072} 3046}
3073 3047
3074sub add { 3048sub add {
3075 my ($self, @children) = @_; 3049 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 3050
3083 for my $child (@children) { 3051 for my $child (@children) {
3084 $child->{is_toplevel} = 1; 3052 $child->{is_toplevel} = 1;
3085 3053
3086 # integerise window positions 3054 # integerise window positions
3087 $child->{x} = int $child->{x}; 3055 $child->{x} = int $child->{x};
3088 $child->{y} = int $child->{y}; 3056 $child->{y} = int $child->{y};
3089 } 3057 }
3090 3058
3091 $self->SUPER::add (@children); 3059 $self->SUPER::add (@children);
3060
3061 for (my @widgets = @children; my $w = pop @widgets; ) {
3062 push @widgets, $w->children;
3063 $w->set_visible;
3064 }
3065
3092} 3066}
3093 3067
3094sub remove { 3068sub remove {
3095 my ($self, @children) = @_; 3069 my ($self, @children) = @_;
3096 3070
3124 } 3098 }
3125 3099
3126 if ($self->{check_size}) { 3100 if ($self->{check_size}) {
3127 my @queue; 3101 my @queue;
3128 3102
3129 for (;;) { 3103 while () {
3130 if ($self->{check_size}) { 3104 if ($self->{check_size}) {
3131 #TODO use array-of-depth approach 3105 #TODO use array-of-depth approach
3132 3106
3133 @queue = sort { $a->{visible} <=> $b->{visible} } 3107 @queue = sort { $a->{visible} <=> $b->{visible} }
3134 @queue, values %{delete $self->{check_size}}; 3108 @queue, values %{delete $self->{check_size}};
3136 3110
3137 my $widget = pop @queue || last; 3111 my $widget = pop @queue || last;
3138 3112
3139 defined $widget->{visible} or last; # do not resize invisible widgets 3113 defined $widget->{visible} or last; # do not resize invisible widgets
3140 3114
3141 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3115 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3142 ? @$widget{qw(user_w user_h)} 3116 ? @$widget{qw(def_w def_h)}
3143 : $widget->size_request; 3117 : $widget->size_request;
3144 3118
3145 if (delete $widget->{force_alloc} 3119 if (delete $widget->{force_alloc}
3146 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3120 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3147 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3121 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
3155 if $widget->{parent}; 3129 if $widget->{parent};
3156 } 3130 }
3157 } 3131 }
3158 } 3132 }
3159 3133
3160 while ($self->{size_alloc}) { 3134 while (my $size_alloc = delete $self->{size_alloc}) {
3161 for (values %{delete $self->{size_alloc}}) { 3135 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3162 my ($widget, $w, $h) = @$_; 3136 values %$size_alloc;
3137
3138 while () {
3139 my ($widget, $w, $h) = @{ pop @queue or last };
3163 3140
3164 $w = 0 if $w < 0; 3141 $w = 0 if $w < 0;
3165 $h = 0 if $h < 0; 3142 $h = 0 if $h < 0;
3166 3143
3167 $widget->{w} = $w; 3144 $widget->{w} = $w;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines