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.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
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
232 %$self = (); 240 %$self = ();
233} 241}
234 242
235sub show { 243sub show {
236 my ($self) = @_; 244 my ($self) = @_;
245
237 return if $self->{parent}; 246 return if $self->{parent};
238 247
239 $CFClient::UI::ROOT->add ($self); 248 $CFClient::UI::ROOT->add ($self);
240} 249}
241 250
242sub show_centered { 251sub center {
243 my ($self) = @_; 252 my ($self) = @_;
244 return if $self->{parent};
245
246 $self->show;
247 253
248 $CFClient::UI::ROOT->on_post_alloc ( 254 $CFClient::UI::ROOT->on_post_alloc (
249 "centered $self" => sub { 255 "center_$self" => sub {
250 $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);
251 }, 257 },
252 ); 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);
253} 272}
254 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; #TODO why breakssssss borked damn if $parent->{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;
499} 517}
500 518
501sub check_size { 519sub check_size {
502 my ($self, $forced) = @_; 520 my ($self, $forced) = @_;
503 521
527sub emit { 545sub emit {
528 my ($self, $signal, @args) = @_; 546 my ($self, $signal, @args) = @_;
529 547
530 $self->_emit ($signal, @args) 548 $self->_emit ($signal, @args)
531 || $self->$signal (@args); 549 || $self->$signal (@args);
550}
551
552sub visibility_change {
553 #my ($self, $visible) = @_;
532} 554}
533 555
534sub DESTROY { 556sub DESTROY {
535 my ($self) = @_; 557 my ($self) = @_;
536 558
812} 834}
813 835
814sub size_request { 836sub size_request {
815 my ($self) = @_; 837 my ($self) = @_;
816 838
817 @$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)};
818 840
819 @$self{qw(child_w child_h)} 841 $w = 10 if $self->{scroll_x};
842 $h = 10 if $self->{scroll_y};
843
844 ($w, $h)
820} 845}
821 846
822sub size_allocate { 847sub size_allocate {
823 my ($self, $w, $h) = @_; 848 my ($self, $w, $h) = @_;
824 849
980 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1005 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); 1006 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
982 1007
983sub new { 1008sub new {
984 my $class = shift; 1009 my $class = shift;
985
986 # TODO: user_x, user_y, overwrite moveto?
987 1010
988 my $self = $class->SUPER::new ( 1011 my $self = $class->SUPER::new (
989 bg => [1, 1, 1, 1], 1012 bg => [1, 1, 1, 1],
990 border_bg => [1, 1, 1, 1], 1013 border_bg => [1, 1, 1, 1],
991 border => 0.6, 1014 border => 0.6,
1053 my $dx = $ev->{x} - $ox; 1076 my $dx = $ev->{x} - $ox;
1054 my $dy = $ev->{y} - $oy; 1077 my $dy = $ev->{y} - $oy;
1055 1078
1056 $self->{user_x} = $wx + $dx * $mx; 1079 $self->{user_x} = $wx + $dx * $mx;
1057 $self->{user_y} = $wy + $dy * $my; 1080 $self->{user_y} = $wy + $dy * $my;
1058 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1081 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
1059 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1082 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
1060 $self->move ($self->{user_x}, $self->{user_y}); 1083 $self->move ($self->{user_x}, $self->{user_y});
1061 $self->check_size; 1084 $self->check_size;
1062 }; 1085 };
1063 1086
1064 } elsif ($lr ^ $td) { 1087 } elsif ($lr ^ $td) {
1070 1093
1071 ($x, $y) = ($ev->{x}, $ev->{y}); 1094 ($x, $y) = ($ev->{x}, $ev->{y});
1072 1095
1073 $self->{user_x} = $bx + $x - $ox; 1096 $self->{user_x} = $bx + $x - $ox;
1074 $self->{user_y} = $by + $y - $oy; 1097 $self->{user_y} = $by + $y - $oy;
1098
1075 $self->move ($self->{user_x}, $self->{user_y}); 1099 $self->move ($self->{user_x}, $self->{user_y});
1076 $self->update; 1100 $self->update;
1077 }; 1101 };
1078 } 1102 }
1079} 1103}
1278 } 1302 }
1279} 1303}
1280 1304
1281############################################################################# 1305#############################################################################
1282 1306
1283package CFClient::UI::HBox; 1307package CFClient::UI::Box;
1284
1285# TODO: wrap into common Box base class
1286 1308
1287our @ISA = CFClient::UI::Container::; 1309our @ISA = CFClient::UI::Container::;
1288 1310
1289sub size_request { 1311sub size_request {
1290 my ($self) = @_; 1312 my ($self) = @_;
1291 1313
1292 my @alloc = map [$_->size_request], @{$self->{children}}; 1314 $self->{vertical}
1293 1315 ? (
1294 ( 1316 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1295 (List::Util::sum map $_->[0], @alloc), 1317 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1296 (List::Util::max map $_->[1], @alloc), 1318 )
1297 ) 1319 : (
1320 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1321 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1322 )
1298} 1323}
1299 1324
1300sub size_allocate { 1325sub size_allocate {
1301 my ($self, $w, $h) = @_; 1326 my ($self, $w, $h) = @_;
1302 1327
1303 ($h, $w) = ($w, $h); 1328 my $space = $self->{vertical} ? $h : $w;
1304
1305 my $children = $self->{children}; 1329 my $children = $self->{children};
1306 1330
1307 my @h = map $_->{req_w}, @$children; 1331 my @req;
1308 1332
1309 my $req_h = List::Util::sum @h; 1333 if ($self->{homogeneous}) {
1310 1334 @req = ($space / (@$children || 1)) x @$children;
1311 if ($req_h > $h) {
1312 # ah well, not enough space
1313 $_ *= $h / $req_h for @h;
1314 } 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 {
1315 my $exp = List::Util::sum map $_->{expand}, @$children; 1343 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1316 $exp ||= 1;
1317 1344
1345 $space = ($space - $req) / $expand; # remaining space to give away
1346
1347 $req[$_] += $space * $children->[$_]{expand}
1318 for (0 .. $#$children) { 1348 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 } 1349 }
1325 } 1350 }
1326 1351
1327 CFClient::UI::harmonize \@h; 1352 CFClient::UI::harmonize \@req;
1328 1353
1329 my $y = 0; 1354 my $pos = 0;
1330 for (0 .. $#$children) { 1355 for (0 .. $#$children) {
1331 my $child = $children->[$_];
1332 my $h = $h[$_]; 1356 my $alloc = $req[$_];
1333 $child->configure ($y, 0, $h, $w); 1357 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1334 1358
1335 $y += $h; 1359 $pos += $alloc;
1336 } 1360 }
1337 1361
1338 1 1362 1
1339} 1363}
1340 1364
1341############################################################################# 1365#############################################################################
1342 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
1343package CFClient::UI::VBox; 1382package CFClient::UI::VBox;
1344 1383
1345# TODO: wrap into common Box base class
1346
1347our @ISA = CFClient::UI::Container::; 1384our @ISA = CFClient::UI::Box::;
1348 1385
1349sub size_request { 1386sub new {
1350 my ($self) = @_; 1387 my $class = shift;
1351 1388
1352 my @alloc = map [$_->size_request], @{$self->{children}}; 1389 $class->SUPER::new (
1353 1390 vertical => 1,
1354 ( 1391 @_,
1355 (List::Util::max map $_->[0], @alloc),
1356 (List::Util::sum map $_->[1], @alloc),
1357 ) 1392 )
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} 1393}
1398 1394
1399############################################################################# 1395#############################################################################
1400 1396
1401package CFClient::UI::Label; 1397package CFClient::UI::Label;
2579 state => 0, 2575 state => 0,
2580 on_activate => \&toggle_flopper, 2576 on_activate => \&toggle_flopper,
2581 @_ 2577 @_
2582 ); 2578 );
2583 2579
2584 if ($self->{state}) {
2585 $self->{state} = 0;
2586 $self->toggle_flopper;
2587 }
2588
2589 $self 2580 $self
2590} 2581}
2591 2582
2592sub toggle_flopper { 2583sub toggle_flopper {
2593 my ($self) = @_; 2584 my ($self) = @_;
2594 2585
2595 # TODO: use animation 2586 $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} 2587}
2607 2588
2608############################################################################# 2589#############################################################################
2609 2590
2610package CFClient::UI::Tooltip; 2591package CFClient::UI::Tooltip;
2794 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 2775 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2795 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 2776 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2796 2777
2797 $row++; 2778 $row++;
2798 } 2779 }
2799}
2800
2801sub size_request {
2802 my ($self) = @_;
2803 ($self->{req_w}, $self->{req_h});
2804} 2780}
2805 2781
2806############################################################################# 2782#############################################################################
2807 2783
2808package CFClient::UI::Menu; 2784package CFClient::UI::Menu;
3032 if exists $child->{req_x}; 3008 if exists $child->{req_x};
3033 3009
3034 $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
3035 if exists $child->{req_y}; 3011 if exists $child->{req_y};
3036 3012
3013 delete @$child{qw(req_x req_y)};
3014
3037 $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;
3038 $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;
3039 3017
3040 $child->configure ($X, $Y, $W, $H); 3018 $child->configure ($X, $Y, $W, $H);
3041 } 3019 }
3060 $::WANT_REFRESH++; 3038 $::WANT_REFRESH++;
3061} 3039}
3062 3040
3063sub add { 3041sub add {
3064 my ($self, @children) = @_; 3042 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 3043
3072 for my $child (@children) { 3044 for my $child (@children) {
3073 $child->{is_toplevel} = 1; 3045 $child->{is_toplevel} = 1;
3074 3046
3075 # integerise window positions 3047 # integerise window positions
3076 $child->{x} = int $child->{x}; 3048 $child->{x} = int $child->{x};
3077 $child->{y} = int $child->{y}; 3049 $child->{y} = int $child->{y};
3078 } 3050 }
3079 3051
3080 $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
3081} 3059}
3082 3060
3083sub remove { 3061sub remove {
3084 my ($self, @children) = @_; 3062 my ($self, @children) = @_;
3085 3063
3125 3103
3126 my $widget = pop @queue || last; 3104 my $widget = pop @queue || last;
3127 3105
3128 defined $widget->{visible} or last; # do not resize invisible widgets 3106 defined $widget->{visible} or last; # do not resize invisible widgets
3129 3107
3130 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3108 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3131 ? @$widget{qw(user_w user_h)} 3109 ? @$widget{qw(def_w def_h)}
3132 : $widget->size_request; 3110 : $widget->size_request;
3133 3111
3134 if (delete $widget->{force_alloc} 3112 if (delete $widget->{force_alloc}
3135 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3113 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3136 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#
3145 } 3123 }
3146 } 3124 }
3147 } 3125 }
3148 3126
3149 while ($self->{size_alloc}) { 3127 while ($self->{size_alloc}) {
3128 for (
3129 sort { $a->[0]{visible} <=> $b->[0]{visible} }
3150 for (values %{delete $self->{size_alloc}}) { 3130 values %{delete $self->{size_alloc}}
3131 ) {
3151 my ($widget, $w, $h) = @$_; 3132 my ($widget, $w, $h) = @$_;
3152 3133
3153 $w = 0 if $w < 0; 3134 $w = 0 if $w < 0;
3154 $h = 0 if $h < 0; 3135 $h = 0 if $h < 0;
3155 3136

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines