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.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} 224}
227 225
232 %$self = (); 230 %$self = ();
233} 231}
234 232
235sub show { 233sub show {
236 my ($self) = @_; 234 my ($self) = @_;
235
237 return if $self->{parent}; 236 return if $self->{parent};
238 237
239 $CFClient::UI::ROOT->add ($self); 238 $CFClient::UI::ROOT->add ($self);
240} 239}
241 240
242sub show_centered { 241sub center {
243 my ($self) = @_; 242 my ($self) = @_;
244 return if $self->{parent};
245
246 $self->show;
247 243
248 $CFClient::UI::ROOT->on_post_alloc ( 244 $CFClient::UI::ROOT->on_post_alloc (
249 "centered $self" => sub { 245 "center_$self" => sub {
250 $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);
251 }, 247 },
252 ); 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);
253} 262}
254 263
255sub set_invisible { 264sub set_invisible {
256 my ($self) = @_; 265 my ($self) = @_;
266
267 return unless $self->{visible};
257 268
258 # broken show/hide model 269 # broken show/hide model
259 270
260 delete $self->{root}; 271 delete $self->{root};
261 delete $self->{visible}; 272 delete $self->{visible};
265 276
266 CFClient::UI::check_tooltip 277 CFClient::UI::check_tooltip
267 if $CFClient::UI::TOOLTIP->{owner} == $self; 278 if $CFClient::UI::TOOLTIP->{owner} == $self;
268 279
269 $self->focus_out; 280 $self->focus_out;
281
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;
270} 300}
271 301
272sub hide { 302sub hide {
273 my ($self) = @_; 303 my ($self) = @_;
274 304
289} 319}
290 320
291sub set_size { 321sub set_size {
292 my ($self, $w, $h) = @_; 322 my ($self, $w, $h) = @_;
293 323
294 $self->{user_w} = $w; 324 $self->{def_w} = $w;
295 $self->{user_h} = $h; 325 $self->{def_h} = $h;
296 326
297 $self->check_size; 327 $self->check_size;
298} 328}
299 329
300sub size_request { 330sub size_request {
322 $self->{y} = $y; 352 $self->{y} = $y;
323 $self->update; 353 $self->update;
324 } 354 }
325 355
326 if ($self->{w} != $w || $self->{h} != $h) { 356 if ($self->{w} != $w || $self->{h} != $h) {
327 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 357 $CFClient::UI::ROOT->{size_alloc}{$self+0} = [$self, $w, $h];
328 } 358 }
329} 359}
330 360
331sub size_allocate { 361sub size_allocate {
332 # nothing to be done 362 # nothing to be done
486sub set_parent { 516sub set_parent {
487 my ($self, $parent) = @_; 517 my ($self, $parent) = @_;
488 518
489 Scalar::Util::weaken ($self->{parent} = $parent); 519 Scalar::Util::weaken ($self->{parent} = $parent);
490 520
491 $self->{root} = $parent->{root}; 521 $self->set_visible; #TODO why breakssssss borked damn if $parent->{visible};
492 $self->{visible} = $parent->{visible} + 1;
493 522
494 # TODO: req_w _does_change after ->reconfigure
495 $self->check_size 523 $self->check_size;
496 unless exists $self->{req_w};
497
498 $self->show;
499} 524}
500 525
501sub check_size { 526sub check_size {
502 my ($self, $forced) = @_; 527 my ($self, $forced) = @_;
503 528
527sub emit { 552sub emit {
528 my ($self, $signal, @args) = @_; 553 my ($self, $signal, @args) = @_;
529 554
530 $self->_emit ($signal, @args) 555 $self->_emit ($signal, @args)
531 || $self->$signal (@args); 556 || $self->$signal (@args);
557}
558
559sub visibility_change {
560 #my ($self, $visible) = @_;
532} 561}
533 562
534sub DESTROY { 563sub DESTROY {
535 my ($self) = @_; 564 my ($self) = @_;
536 565
812} 841}
813 842
814sub size_request { 843sub size_request {
815 my ($self) = @_; 844 my ($self) = @_;
816 845
817 @$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)};
818 847
819 @$self{qw(child_w child_h)} 848 $w = 10 if $self->{scroll_x};
849 $h = 10 if $self->{scroll_y};
850
851 ($w, $h)
820} 852}
821 853
822sub size_allocate { 854sub size_allocate {
823 my ($self, $w, $h) = @_; 855 my ($self, $w, $h) = @_;
824 856
980 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1012 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); 1013 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
982 1014
983sub new { 1015sub new {
984 my $class = shift; 1016 my $class = shift;
985
986 # TODO: user_x, user_y, overwrite moveto?
987 1017
988 my $self = $class->SUPER::new ( 1018 my $self = $class->SUPER::new (
989 bg => [1, 1, 1, 1], 1019 bg => [1, 1, 1, 1],
990 border_bg => [1, 1, 1, 1], 1020 border_bg => [1, 1, 1, 1],
991 border => 0.6, 1021 border => 0.6,
1053 my $dx = $ev->{x} - $ox; 1083 my $dx = $ev->{x} - $ox;
1054 my $dy = $ev->{y} - $oy; 1084 my $dy = $ev->{y} - $oy;
1055 1085
1056 $self->{user_x} = $wx + $dx * $mx; 1086 $self->{user_x} = $wx + $dx * $mx;
1057 $self->{user_y} = $wy + $dy * $my; 1087 $self->{user_y} = $wy + $dy * $my;
1058 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1088 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
1059 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1089 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
1060 $self->move ($self->{user_x}, $self->{user_y}); 1090 $self->move ($self->{user_x}, $self->{user_y});
1061 $self->check_size; 1091 $self->check_size;
1062 }; 1092 };
1063 1093
1064 } elsif ($lr ^ $td) { 1094 } elsif ($lr ^ $td) {
1070 1100
1071 ($x, $y) = ($ev->{x}, $ev->{y}); 1101 ($x, $y) = ($ev->{x}, $ev->{y});
1072 1102
1073 $self->{user_x} = $bx + $x - $ox; 1103 $self->{user_x} = $bx + $x - $ox;
1074 $self->{user_y} = $by + $y - $oy; 1104 $self->{user_y} = $by + $y - $oy;
1105
1075 $self->move ($self->{user_x}, $self->{user_y}); 1106 $self->move ($self->{user_x}, $self->{user_y});
1076 $self->update; 1107 $self->update;
1077 }; 1108 };
1078 } 1109 }
1079} 1110}
1278 } 1309 }
1279} 1310}
1280 1311
1281############################################################################# 1312#############################################################################
1282 1313
1283package CFClient::UI::HBox; 1314package CFClient::UI::Box;
1284
1285# TODO: wrap into common Box base class
1286 1315
1287our @ISA = CFClient::UI::Container::; 1316our @ISA = CFClient::UI::Container::;
1288 1317
1289sub size_request { 1318sub size_request {
1290 my ($self) = @_; 1319 my ($self) = @_;
1291 1320
1292 my @alloc = map [$_->size_request], @{$self->{children}}; 1321 $self->{vertical}
1293 1322 ? (
1294 ( 1323 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1295 (List::Util::sum map $_->[0], @alloc), 1324 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1296 (List::Util::max map $_->[1], @alloc), 1325 )
1297 ) 1326 : (
1327 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1328 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1329 )
1298} 1330}
1299 1331
1300sub size_allocate { 1332sub size_allocate {
1301 my ($self, $w, $h) = @_; 1333 my ($self, $w, $h) = @_;
1302 1334
1303 ($h, $w) = ($w, $h); 1335 my $space = $self->{vertical} ? $h : $w;
1304
1305 my $children = $self->{children}; 1336 my $children = $self->{children};
1306 1337
1307 my @h = map $_->{req_w}, @$children; 1338 my @req;
1308 1339
1309 my $req_h = List::Util::sum @h; 1340 if ($self->{homogeneous}) {
1310 1341 @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 { 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 {
1315 my $exp = List::Util::sum map $_->{expand}, @$children; 1350 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1316 $exp ||= 1;
1317 1351
1352 $space = ($space - $req) / $expand; # remaining space to give away
1353
1354 $req[$_] += $space * $children->[$_]{expand}
1318 for (0 .. $#$children) { 1355 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 } 1356 }
1325 } 1357 }
1326 1358
1327 CFClient::UI::harmonize \@h; 1359 CFClient::UI::harmonize \@req;
1328 1360
1329 my $y = 0; 1361 my $pos = 0;
1330 for (0 .. $#$children) { 1362 for (0 .. $#$children) {
1331 my $child = $children->[$_];
1332 my $h = $h[$_]; 1363 my $alloc = $req[$_];
1333 $child->configure ($y, 0, $h, $w); 1364 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1334 1365
1335 $y += $h; 1366 $pos += $alloc;
1336 } 1367 }
1337 1368
1338 1 1369 1
1339} 1370}
1340 1371
1341############################################################################# 1372#############################################################################
1342 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
1343package CFClient::UI::VBox; 1389package CFClient::UI::VBox;
1344 1390
1345# TODO: wrap into common Box base class
1346
1347our @ISA = CFClient::UI::Container::; 1391our @ISA = CFClient::UI::Box::;
1348 1392
1349sub size_request { 1393sub new {
1350 my ($self) = @_; 1394 my $class = shift;
1351 1395
1352 my @alloc = map [$_->size_request], @{$self->{children}}; 1396 $class->SUPER::new (
1353 1397 vertical => 1,
1354 ( 1398 @_,
1355 (List::Util::max map $_->[0], @alloc),
1356 (List::Util::sum map $_->[1], @alloc),
1357 ) 1399 )
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} 1400}
1398 1401
1399############################################################################# 1402#############################################################################
1400 1403
1401package CFClient::UI::Label; 1404package CFClient::UI::Label;
2579 state => 0, 2582 state => 0,
2580 on_activate => \&toggle_flopper, 2583 on_activate => \&toggle_flopper,
2581 @_ 2584 @_
2582 ); 2585 );
2583 2586
2584 if ($self->{state}) {
2585 $self->{state} = 0;
2586 $self->toggle_flopper;
2587 }
2588
2589 $self 2587 $self
2590} 2588}
2591 2589
2592sub toggle_flopper { 2590sub toggle_flopper {
2593 my ($self) = @_; 2591 my ($self) = @_;
2594 2592
2595 # TODO: use animation 2593 $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} 2594}
2607 2595
2608############################################################################# 2596#############################################################################
2609 2597
2610package CFClient::UI::Tooltip; 2598package CFClient::UI::Tooltip;
2794 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 2782 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2795 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 2783 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2796 2784
2797 $row++; 2785 $row++;
2798 } 2786 }
2799}
2800
2801sub size_request {
2802 my ($self) = @_;
2803 ($self->{req_w}, $self->{req_h});
2804} 2787}
2805 2788
2806############################################################################# 2789#############################################################################
2807 2790
2808package CFClient::UI::Menu; 2791package CFClient::UI::Menu;
3032 if exists $child->{req_x}; 3015 if exists $child->{req_x};
3033 3016
3034 $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
3035 if exists $child->{req_y}; 3018 if exists $child->{req_y};
3036 3019
3020 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3021
3037 $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;
3038 $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;
3039 3024
3040 $child->configure ($X, $Y, $W, $H); 3025 $child->configure ($X, $Y, $W, $H);
3041 } 3026 }
3060 $::WANT_REFRESH++; 3045 $::WANT_REFRESH++;
3061} 3046}
3062 3047
3063sub add { 3048sub add {
3064 my ($self, @children) = @_; 3049 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 3050
3072 for my $child (@children) { 3051 for my $child (@children) {
3073 $child->{is_toplevel} = 1; 3052 $child->{is_toplevel} = 1;
3074 3053
3075 # integerise window positions 3054 # integerise window positions
3076 $child->{x} = int $child->{x}; 3055 $child->{x} = int $child->{x};
3077 $child->{y} = int $child->{y}; 3056 $child->{y} = int $child->{y};
3078 } 3057 }
3079 3058
3080 $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
3081} 3066}
3082 3067
3083sub remove { 3068sub remove {
3084 my ($self, @children) = @_; 3069 my ($self, @children) = @_;
3085 3070
3113 } 3098 }
3114 3099
3115 if ($self->{check_size}) { 3100 if ($self->{check_size}) {
3116 my @queue; 3101 my @queue;
3117 3102
3118 for (;;) { 3103 while () {
3119 if ($self->{check_size}) { 3104 if ($self->{check_size}) {
3120 #TODO use array-of-depth approach 3105 #TODO use array-of-depth approach
3121 3106
3122 @queue = sort { $a->{visible} <=> $b->{visible} } 3107 @queue = sort { $a->{visible} <=> $b->{visible} }
3123 @queue, values %{delete $self->{check_size}}; 3108 @queue, values %{delete $self->{check_size}};
3125 3110
3126 my $widget = pop @queue || last; 3111 my $widget = pop @queue || last;
3127 3112
3128 defined $widget->{visible} or last; # do not resize invisible widgets 3113 defined $widget->{visible} or last; # do not resize invisible widgets
3129 3114
3130 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3115 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3131 ? @$widget{qw(user_w user_h)} 3116 ? @$widget{qw(def_w def_h)}
3132 : $widget->size_request; 3117 : $widget->size_request;
3133 3118
3134 if (delete $widget->{force_alloc} 3119 if (delete $widget->{force_alloc}
3135 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3120 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3136 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#
3144 if $widget->{parent}; 3129 if $widget->{parent};
3145 } 3130 }
3146 } 3131 }
3147 } 3132 }
3148 3133
3149 while ($self->{size_alloc}) { 3134 while (my $size_alloc = delete $self->{size_alloc}) {
3150 for (values %{delete $self->{size_alloc}}) { 3135 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3151 my ($widget, $w, $h) = @$_; 3136 values %$size_alloc;
3137
3138 while () {
3139 my ($widget, $w, $h) = @{ pop @queue or last };
3152 3140
3153 $w = 0 if $w < 0; 3141 $w = 0 if $w < 0;
3154 $h = 0 if $h < 0; 3142 $h = 0 if $h < 0;
3155 3143
3156 $widget->{w} = $w; 3144 $widget->{w} = $w;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines