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.243 by root, Sat May 27 20:46:54 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) = @_;
276
277 return unless $self->{visible};
257 278
258 # broken show/hide model 279 # broken show/hide model
259 280
260 delete $self->{root}; 281 delete $self->{root};
261 delete $self->{visible}; 282 delete $self->{visible};
265 286
266 CFClient::UI::check_tooltip 287 CFClient::UI::check_tooltip
267 if $CFClient::UI::TOOLTIP->{owner} == $self; 288 if $CFClient::UI::TOOLTIP->{owner} == $self;
268 289
269 $self->focus_out; 290 $self->focus_out;
291
292 $self->emit (visibility_change => 0);
270} 293}
271 294
272sub hide { 295sub hide {
273 my ($self) = @_; 296 my ($self) = @_;
274 297
289} 312}
290 313
291sub set_size { 314sub set_size {
292 my ($self, $w, $h) = @_; 315 my ($self, $w, $h) = @_;
293 316
294 $self->{user_w} = $w; 317 $self->{def_w} = $w;
295 $self->{user_h} = $h; 318 $self->{def_h} = $h;
296 319
297 $self->check_size; 320 $self->check_size;
298} 321}
299 322
300sub size_request { 323sub size_request {
486sub set_parent { 509sub set_parent {
487 my ($self, $parent) = @_; 510 my ($self, $parent) = @_;
488 511
489 Scalar::Util::weaken ($self->{parent} = $parent); 512 Scalar::Util::weaken ($self->{parent} = $parent);
490 513
491 $self->{root} = $parent->{root}; 514 $self->set_visible;
492 $self->{visible} = $parent->{visible} + 1;
493 515
494 # TODO: req_w _does_change after ->reconfigure
495 $self->check_size 516 $self->check_size;
496 unless exists $self->{req_w};
497
498 $self->show; 517 $self->show;
499} 518}
500 519
501sub check_size { 520sub check_size {
502 my ($self, $forced) = @_; 521 my ($self, $forced) = @_;
527sub emit { 546sub emit {
528 my ($self, $signal, @args) = @_; 547 my ($self, $signal, @args) = @_;
529 548
530 $self->_emit ($signal, @args) 549 $self->_emit ($signal, @args)
531 || $self->$signal (@args); 550 || $self->$signal (@args);
551}
552
553sub visibility_change {
554 #my ($self, $visible) = @_;
532} 555}
533 556
534sub DESTROY { 557sub DESTROY {
535 my ($self) = @_; 558 my ($self) = @_;
536 559
980 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1003 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
981 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);
982 1005
983sub new { 1006sub new {
984 my $class = shift; 1007 my $class = shift;
985
986 # TODO: user_x, user_y, overwrite moveto?
987 1008
988 my $self = $class->SUPER::new ( 1009 my $self = $class->SUPER::new (
989 bg => [1, 1, 1, 1], 1010 bg => [1, 1, 1, 1],
990 border_bg => [1, 1, 1, 1], 1011 border_bg => [1, 1, 1, 1],
991 border => 0.6, 1012 border => 0.6,
1053 my $dx = $ev->{x} - $ox; 1074 my $dx = $ev->{x} - $ox;
1054 my $dy = $ev->{y} - $oy; 1075 my $dy = $ev->{y} - $oy;
1055 1076
1056 $self->{user_x} = $wx + $dx * $mx; 1077 $self->{user_x} = $wx + $dx * $mx;
1057 $self->{user_y} = $wy + $dy * $my; 1078 $self->{user_y} = $wy + $dy * $my;
1058 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1079 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
1059 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1080 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
1060 $self->move ($self->{user_x}, $self->{user_y}); 1081 $self->move ($self->{user_x}, $self->{user_y});
1061 $self->check_size; 1082 $self->check_size;
1062 }; 1083 };
1063 1084
1064 } elsif ($lr ^ $td) { 1085 } elsif ($lr ^ $td) {
1070 1091
1071 ($x, $y) = ($ev->{x}, $ev->{y}); 1092 ($x, $y) = ($ev->{x}, $ev->{y});
1072 1093
1073 $self->{user_x} = $bx + $x - $ox; 1094 $self->{user_x} = $bx + $x - $ox;
1074 $self->{user_y} = $by + $y - $oy; 1095 $self->{user_y} = $by + $y - $oy;
1096
1075 $self->move ($self->{user_x}, $self->{user_y}); 1097 $self->move ($self->{user_x}, $self->{user_y});
1076 $self->update; 1098 $self->update;
1077 }; 1099 };
1078 } 1100 }
1079} 1101}
1278 } 1300 }
1279} 1301}
1280 1302
1281############################################################################# 1303#############################################################################
1282 1304
1283package CFClient::UI::HBox; 1305package CFClient::UI::Box;
1284
1285# TODO: wrap into common Box base class
1286 1306
1287our @ISA = CFClient::UI::Container::; 1307our @ISA = CFClient::UI::Container::;
1288 1308
1289sub size_request { 1309sub size_request {
1290 my ($self) = @_; 1310 my ($self) = @_;
1291 1311
1292 my @alloc = map [$_->size_request], @{$self->{children}}; 1312 $self->{vertical}
1293 1313 ? (
1294 ( 1314 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1295 (List::Util::sum map $_->[0], @alloc), 1315 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1296 (List::Util::max map $_->[1], @alloc), 1316 )
1297 ) 1317 : (
1318 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1319 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1320 )
1298} 1321}
1299 1322
1300sub size_allocate { 1323sub size_allocate {
1301 my ($self, $w, $h) = @_; 1324 my ($self, $w, $h) = @_;
1302 1325
1303 ($h, $w) = ($w, $h); 1326 my $space = $self->{vertical} ? $h : $w;
1304
1305 my $children = $self->{children}; 1327 my $children = $self->{children};
1306 1328
1307 my @h = map $_->{req_w}, @$children; 1329 my @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1308
1309 my $req_h = List::Util::sum @h; 1330 my $req = List::Util::sum @req;
1310 1331
1311 if ($req_h > $h) { 1332 if ($req > $space) {
1312 # ah well, not enough space 1333 # ah well, not enough space
1313 $_ *= $h / $req_h for @h; 1334 $_ *= $space / $req for @req;
1314 } else { 1335 } else {
1315 my $exp = List::Util::sum map $_->{expand}, @$children; 1336 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1316 $exp ||= 1;
1317 1337
1338 $req[$_] += ($space - $req) * $children->[$_]{expand} / $expand
1318 for (0 .. $#$children) { 1339 for 0 .. $#$children;
1319 my $child = $children->[$_];
1320
1321 my $alloc_h = $h[$_];
1322 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1323 $h[$_] = $alloc_h;
1324 }
1325 } 1340 }
1326 1341
1327 CFClient::UI::harmonize \@h; 1342 CFClient::UI::harmonize \@req;
1328 1343
1329 my $y = 0; 1344 my $pos = 0;
1330 for (0 .. $#$children) { 1345 for (0 .. $#$children) {
1331 my $child = $children->[$_];
1332 my $h = $h[$_]; 1346 my $alloc = $req[$_];
1333 $child->configure ($y, 0, $h, $w); 1347 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1334 1348
1335 $y += $h; 1349 $pos += $alloc;
1336 } 1350 }
1337 1351
1338 1 1352 1
1339} 1353}
1340 1354
1341############################################################################# 1355#############################################################################
1342 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
1343package CFClient::UI::VBox; 1372package CFClient::UI::VBox;
1344 1373
1345# TODO: wrap into common Box base class
1346
1347our @ISA = CFClient::UI::Container::; 1374our @ISA = CFClient::UI::Box::;
1348 1375
1349sub size_request { 1376sub new {
1350 my ($self) = @_; 1377 my $class = shift;
1351 1378
1352 my @alloc = map [$_->size_request], @{$self->{children}}; 1379 $class->SUPER::new (
1353 1380 vertical => 1,
1354 ( 1381 @_,
1355 (List::Util::max map $_->[0], @alloc),
1356 (List::Util::sum map $_->[1], @alloc),
1357 ) 1382 )
1358}
1359
1360sub size_allocate {
1361 my ($self, $w, $h) = @_;
1362
1363 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1364
1365 my $children = $self->{children};
1366
1367 my @h = map $_->{req_h}, @$children;
1368
1369 my $req_h = List::Util::sum @h;
1370
1371 if ($req_h > $h) {
1372 # ah well, not enough space
1373 $_ *= $h / $req_h for @h;
1374 } else {
1375 my $exp = List::Util::sum map $_->{expand}, @$children;
1376 $exp ||= 1;
1377
1378 for (0 .. $#$children) {
1379 my $child = $children->[$_];
1380
1381 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1382 }
1383 }
1384
1385 CFClient::UI::harmonize \@h;
1386
1387 my $y = 0;
1388 for (0 .. $#$children) {
1389 my $child = $children->[$_];
1390 my $h = $h[$_];
1391 $child->configure (0, $y, $w, $h);
1392
1393 $y += $h;
1394 }
1395
1396 1
1397} 1383}
1398 1384
1399############################################################################# 1385#############################################################################
1400 1386
1401package CFClient::UI::Label; 1387package CFClient::UI::Label;
2579 state => 0, 2565 state => 0,
2580 on_activate => \&toggle_flopper, 2566 on_activate => \&toggle_flopper,
2581 @_ 2567 @_
2582 ); 2568 );
2583 2569
2584 if ($self->{state}) {
2585 $self->{state} = 0;
2586 $self->toggle_flopper;
2587 }
2588
2589 $self 2570 $self
2590} 2571}
2591 2572
2592sub toggle_flopper { 2573sub toggle_flopper {
2593 my ($self) = @_; 2574 my ($self) = @_;
2594 2575
2595 # TODO: use animation 2576 $self->{other}->toggle_visibility;
2596 if ($self->{state} = !$self->{state}) {
2597 $CFClient::UI::ROOT->add ($self->{other});
2598 $self->{other}->move ($self->coord2global (0, $self->{h}));
2599 $self->_emit ("open");
2600 } else {
2601 $CFClient::UI::ROOT->remove ($self->{other});
2602 $self->_emit ("close");
2603 }
2604
2605 $self->_emit (changed => $self->{state});
2606} 2577}
2607 2578
2608############################################################################# 2579#############################################################################
2609 2580
2610package CFClient::UI::Tooltip; 2581package CFClient::UI::Tooltip;
2794 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 2765 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2795 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 2766 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2796 2767
2797 $row++; 2768 $row++;
2798 } 2769 }
2799}
2800
2801sub size_request {
2802 my ($self) = @_;
2803 ($self->{req_w}, $self->{req_h});
2804} 2770}
2805 2771
2806############################################################################# 2772#############################################################################
2807 2773
2808package CFClient::UI::Menu; 2774package CFClient::UI::Menu;
3032 if exists $child->{req_x}; 2998 if exists $child->{req_x};
3033 2999
3034 $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
3035 if exists $child->{req_y}; 3001 if exists $child->{req_y};
3036 3002
3003 delete @$child{qw(req_x req_y)};
3004
3037 $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;
3038 $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;
3039 3007
3040 $child->configure ($X, $Y, $W, $H); 3008 $child->configure ($X, $Y, $W, $H);
3041 } 3009 }
3060 $::WANT_REFRESH++; 3028 $::WANT_REFRESH++;
3061} 3029}
3062 3030
3063sub add { 3031sub add {
3064 my ($self, @children) = @_; 3032 my ($self, @children) = @_;
3065
3066 for (my @widgets = @children; my $w = pop @widgets; ) {
3067 push @widgets, $w->children;
3068 $w->{root} = $self;
3069 $w->{visible} = $self->{visible} + 1;
3070 }
3071 3033
3072 for my $child (@children) { 3034 for my $child (@children) {
3073 $child->{is_toplevel} = 1; 3035 $child->{is_toplevel} = 1;
3074 3036
3075 # integerise window positions 3037 # integerise window positions
3076 $child->{x} = int $child->{x}; 3038 $child->{x} = int $child->{x};
3077 $child->{y} = int $child->{y}; 3039 $child->{y} = int $child->{y};
3078 } 3040 }
3079 3041
3080 $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
3081} 3049}
3082 3050
3083sub remove { 3051sub remove {
3084 my ($self, @children) = @_; 3052 my ($self, @children) = @_;
3085 3053
3125 3093
3126 my $widget = pop @queue || last; 3094 my $widget = pop @queue || last;
3127 3095
3128 defined $widget->{visible} or last; # do not resize invisible widgets 3096 defined $widget->{visible} or last; # do not resize invisible widgets
3129 3097
3130 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3098 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3131 ? @$widget{qw(user_w user_h)} 3099 ? @$widget{qw(def_w def_h)}
3132 : $widget->size_request; 3100 : $widget->size_request;
3133 3101
3134 if (delete $widget->{force_alloc} 3102 if (delete $widget->{force_alloc}
3135 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3103 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3136 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