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.244 by root, Sat May 27 21:06:09 2006 UTC vs.
Revision 1.246 by root, Sun May 28 00:40:51 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
224}
225
226sub toggle_visibility {
227 my ($self) = @_;
228
229 if ($self->{visible}) {
230 $self->hide;
231 } else {
232 $self->show;
233 }
226} 234}
227 235
228sub destroy { 236sub destroy {
229 my ($self) = @_; 237 my ($self) = @_;
230 238
250 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 258 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5);
251 }, 259 },
252 ); 260 );
253} 261}
254 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)
272}
273
255sub set_invisible { 274sub set_invisible {
256 my ($self) = @_; 275 my ($self) = @_;
257 276
258 return unless $self->{visible}; 277 return unless $self->{visible};
259 278
293} 312}
294 313
295sub set_size { 314sub set_size {
296 my ($self, $w, $h) = @_; 315 my ($self, $w, $h) = @_;
297 316
298 $self->{user_w} = $w; 317 $self->{def_w} = $w;
299 $self->{user_h} = $h; 318 $self->{def_h} = $h;
300 319
301 $self->check_size; 320 $self->check_size;
302} 321}
303 322
304sub size_request { 323sub size_request {
490sub set_parent { 509sub set_parent {
491 my ($self, $parent) = @_; 510 my ($self, $parent) = @_;
492 511
493 Scalar::Util::weaken ($self->{parent} = $parent); 512 Scalar::Util::weaken ($self->{parent} = $parent);
494 513
495 if ($parent->{visible} || 1) { 514 $self->set_visible;
496 $self->{root} = $parent->{root};
497 $self->{visible} = $parent->{visible} + 1;
498 515
499 $self->emit (visibility_change => 1)
500 unless $self->{parent}{visible};
501 }
502
503 # TODO: req_w _does_change after ->reconfigure
504 $self->check_size 516 $self->check_size;
505 unless exists $self->{req_w};
506
507 $self->show; 517 $self->show;
508} 518}
509 519
510sub check_size { 520sub check_size {
511 my ($self, $forced) = @_; 521 my ($self, $forced) = @_;
993 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1003 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
994 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1004 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
995 1005
996sub new { 1006sub new {
997 my $class = shift; 1007 my $class = shift;
998
999 # TODO: user_x, user_y, overwrite moveto?
1000 1008
1001 my $self = $class->SUPER::new ( 1009 my $self = $class->SUPER::new (
1002 bg => [1, 1, 1, 1], 1010 bg => [1, 1, 1, 1],
1003 border_bg => [1, 1, 1, 1], 1011 border_bg => [1, 1, 1, 1],
1004 border => 0.6, 1012 border => 0.6,
1066 my $dx = $ev->{x} - $ox; 1074 my $dx = $ev->{x} - $ox;
1067 my $dy = $ev->{y} - $oy; 1075 my $dy = $ev->{y} - $oy;
1068 1076
1069 $self->{user_x} = $wx + $dx * $mx; 1077 $self->{user_x} = $wx + $dx * $mx;
1070 $self->{user_y} = $wy + $dy * $my; 1078 $self->{user_y} = $wy + $dy * $my;
1071 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1079 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
1072 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1080 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
1073 $self->move ($self->{user_x}, $self->{user_y}); 1081 $self->move ($self->{user_x}, $self->{user_y});
1074 $self->check_size; 1082 $self->check_size;
1075 }; 1083 };
1076 1084
1077 } elsif ($lr ^ $td) { 1085 } elsif ($lr ^ $td) {
1083 1091
1084 ($x, $y) = ($ev->{x}, $ev->{y}); 1092 ($x, $y) = ($ev->{x}, $ev->{y});
1085 1093
1086 $self->{user_x} = $bx + $x - $ox; 1094 $self->{user_x} = $bx + $x - $ox;
1087 $self->{user_y} = $by + $y - $oy; 1095 $self->{user_y} = $by + $y - $oy;
1096
1088 $self->move ($self->{user_x}, $self->{user_y}); 1097 $self->move ($self->{user_x}, $self->{user_y});
1089 $self->update; 1098 $self->update;
1090 }; 1099 };
1091 } 1100 }
1092} 1101}
1291 } 1300 }
1292} 1301}
1293 1302
1294############################################################################# 1303#############################################################################
1295 1304
1296package CFClient::UI::HBox; 1305package CFClient::UI::Box;
1297
1298# TODO: wrap into common Box base class
1299 1306
1300our @ISA = CFClient::UI::Container::; 1307our @ISA = CFClient::UI::Container::;
1301 1308
1302sub size_request { 1309sub size_request {
1303 my ($self) = @_; 1310 my ($self) = @_;
1304 1311
1305 my @alloc = map [$_->size_request], @{$self->{children}}; 1312 $self->{vertical}
1306 1313 ? (
1307 ( 1314 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1308 (List::Util::sum map $_->[0], @alloc), 1315 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1309 (List::Util::max map $_->[1], @alloc), 1316 )
1310 ) 1317 : (
1318 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1319 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1320 )
1311} 1321}
1312 1322
1313sub size_allocate { 1323sub size_allocate {
1314 my ($self, $w, $h) = @_; 1324 my ($self, $w, $h) = @_;
1315 1325
1316 ($h, $w) = ($w, $h); 1326 my $space = $self->{vertical} ? $h : $w;
1317
1318 my $children = $self->{children}; 1327 my $children = $self->{children};
1319 1328
1320 my @h = map $_->{req_w}, @$children; 1329 my @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1321
1322 my $req_h = List::Util::sum @h; 1330 my $req = List::Util::sum @req;
1323 1331
1324 if ($req_h > $h) { 1332 if ($req > $space) {
1325 # ah well, not enough space 1333 # ah well, not enough space
1326 $_ *= $h / $req_h for @h; 1334 $_ *= $space / $req for @req;
1327 } else { 1335 } else {
1328 my $exp = List::Util::sum map $_->{expand}, @$children; 1336 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1329 $exp ||= 1;
1330 1337
1338 $req[$_] += ($space - $req) * $children->[$_]{expand} / $expand
1331 for (0 .. $#$children) { 1339 for 0 .. $#$children;
1332 my $child = $children->[$_];
1333
1334 my $alloc_h = $h[$_];
1335 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1336 $h[$_] = $alloc_h;
1337 }
1338 } 1340 }
1339 1341
1340 CFClient::UI::harmonize \@h; 1342 CFClient::UI::harmonize \@req;
1341 1343
1342 my $y = 0; 1344 my $pos = 0;
1343 for (0 .. $#$children) { 1345 for (0 .. $#$children) {
1344 my $child = $children->[$_];
1345 my $h = $h[$_]; 1346 my $alloc = $req[$_];
1346 $child->configure ($y, 0, $h, $w); 1347 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1347 1348
1348 $y += $h; 1349 $pos += $alloc;
1349 } 1350 }
1350 1351
1351 1 1352 1
1352} 1353}
1353 1354
1354############################################################################# 1355#############################################################################
1355 1356
1357package CFClient::UI::HBox;
1358
1359our @ISA = CFClient::UI::Box::;
1360
1361sub new {
1362 my $class = shift;
1363
1364 $class->SUPER::new (
1365 vertical => 0,
1366 @_,
1367 )
1368}
1369
1370#############################################################################
1371
1356package CFClient::UI::VBox; 1372package CFClient::UI::VBox;
1357 1373
1358# TODO: wrap into common Box base class
1359
1360our @ISA = CFClient::UI::Container::; 1374our @ISA = CFClient::UI::Box::;
1361 1375
1362sub size_request { 1376sub new {
1363 my ($self) = @_; 1377 my $class = shift;
1364 1378
1365 my @alloc = map [$_->size_request], @{$self->{children}}; 1379 $class->SUPER::new (
1366 1380 vertical => 1,
1367 ( 1381 @_,
1368 (List::Util::max map $_->[0], @alloc),
1369 (List::Util::sum map $_->[1], @alloc),
1370 ) 1382 )
1371}
1372
1373sub size_allocate {
1374 my ($self, $w, $h) = @_;
1375
1376 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1377
1378 my $children = $self->{children};
1379
1380 my @h = map $_->{req_h}, @$children;
1381
1382 my $req_h = List::Util::sum @h;
1383
1384 if ($req_h > $h) {
1385 # ah well, not enough space
1386 $_ *= $h / $req_h for @h;
1387 } else {
1388 my $exp = List::Util::sum map $_->{expand}, @$children;
1389 $exp ||= 1;
1390
1391 for (0 .. $#$children) {
1392 my $child = $children->[$_];
1393
1394 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1395 }
1396 }
1397
1398 CFClient::UI::harmonize \@h;
1399
1400 my $y = 0;
1401 for (0 .. $#$children) {
1402 my $child = $children->[$_];
1403 my $h = $h[$_];
1404 $child->configure (0, $y, $w, $h);
1405
1406 $y += $h;
1407 }
1408
1409 1
1410} 1383}
1411 1384
1412############################################################################# 1385#############################################################################
1413 1386
1414package CFClient::UI::Label; 1387package CFClient::UI::Label;
2592 state => 0, 2565 state => 0,
2593 on_activate => \&toggle_flopper, 2566 on_activate => \&toggle_flopper,
2594 @_ 2567 @_
2595 ); 2568 );
2596 2569
2597 if ($self->{state}) {
2598 $self->{state} = 0;
2599 $self->toggle_flopper;
2600 }
2601
2602 $self 2570 $self
2603} 2571}
2604 2572
2605sub toggle_flopper { 2573sub toggle_flopper {
2606 my ($self) = @_; 2574 my ($self) = @_;
2607 2575
2608 # TODO: use animation 2576 $self->{other}->toggle_visibility;
2609 if ($self->{state} = !$self->{state}) {
2610 $CFClient::UI::ROOT->add ($self->{other});
2611 $self->{other}->move ($self->coord2global (0, $self->{h}));
2612 $self->_emit ("open");
2613 } else {
2614 $CFClient::UI::ROOT->remove ($self->{other});
2615 $self->_emit ("close");
2616 }
2617
2618 $self->_emit (changed => $self->{state});
2619} 2577}
2620 2578
2621############################################################################# 2579#############################################################################
2622 2580
2623package CFClient::UI::Tooltip; 2581package CFClient::UI::Tooltip;
2807 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 2765 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2808 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 2766 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2809 2767
2810 $row++; 2768 $row++;
2811 } 2769 }
2812}
2813
2814sub size_request {
2815 my ($self) = @_;
2816 ($self->{req_w}, $self->{req_h});
2817} 2770}
2818 2771
2819############################################################################# 2772#############################################################################
2820 2773
2821package CFClient::UI::Menu; 2774package CFClient::UI::Menu;
3045 if exists $child->{req_x}; 2998 if exists $child->{req_x};
3046 2999
3047 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3000 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3048 if exists $child->{req_y}; 3001 if exists $child->{req_y};
3049 3002
3050 $X = $self->{user_x} if exists $self->{user_x}; 3003 delete @$child{qw(req_x req_y)};
3051 $Y = $self->{user_y} if exists $self->{user_y};
3052 3004
3053 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3005 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3054 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; 3006 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3055 3007
3056 $child->configure ($X, $Y, $W, $H); 3008 $child->configure ($X, $Y, $W, $H);
3076 $::WANT_REFRESH++; 3028 $::WANT_REFRESH++;
3077} 3029}
3078 3030
3079sub add { 3031sub add {
3080 my ($self, @children) = @_; 3032 my ($self, @children) = @_;
3081
3082 for (my @widgets = @children; my $w = pop @widgets; ) {
3083 push @widgets, $w->children;
3084 $w->{root} = $self;
3085 $w->{visible} = $self->{visible} + 1;
3086 }
3087 3033
3088 for my $child (@children) { 3034 for my $child (@children) {
3089 $child->{is_toplevel} = 1; 3035 $child->{is_toplevel} = 1;
3090 3036
3091 # integerise window positions 3037 # integerise window positions
3092 $child->{x} = int $child->{x}; 3038 $child->{x} = int $child->{x};
3093 $child->{y} = int $child->{y}; 3039 $child->{y} = int $child->{y};
3094 } 3040 }
3095 3041
3096 $self->SUPER::add (@children); 3042 $self->SUPER::add (@children);
3043
3044 for (my @widgets = @children; my $w = pop @widgets; ) {
3045 push @widgets, $w->children;
3046 $w->set_visible;
3047 }
3048
3097} 3049}
3098 3050
3099sub remove { 3051sub remove {
3100 my ($self, @children) = @_; 3052 my ($self, @children) = @_;
3101 3053
3141 3093
3142 my $widget = pop @queue || last; 3094 my $widget = pop @queue || last;
3143 3095
3144 defined $widget->{visible} or last; # do not resize invisible widgets 3096 defined $widget->{visible} or last; # do not resize invisible widgets
3145 3097
3146 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3098 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3147 ? @$widget{qw(user_w user_h)} 3099 ? @$widget{qw(def_w def_h)}
3148 : $widget->size_request; 3100 : $widget->size_request;
3149 3101
3150 if (delete $widget->{force_alloc} 3102 if (delete $widget->{force_alloc}
3151 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3103 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3152 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3104 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines