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.242 by elmex, Sat May 27 20:00:40 2006 UTC vs.
Revision 1.247 by root, Sun May 28 01:16:04 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}
206 can_events => 1, 204 can_events => 1,
207 @_ 205 @_
208 }, $class; 206 }, $class;
209 207
210 for (keys %$self) { 208 for (keys %$self) {
211 if (/^connect_(.*)$/) { 209 if (/^on_(.*)$/) {
212 $self->connect ($1 => delete $self->{$_}); 210 $self->connect ($1 => delete $self->{$_});
213 } 211 }
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
237 return if $self->{parent}; 245 return if $self->{parent};
238 246
239 $CFClient::UI::ROOT->add ($self); 247 $CFClient::UI::ROOT->add ($self);
240} 248}
241 249
242sub show_centered { 250sub center {
243 my ($self) = @_; 251 my ($self) = @_;
244 return if $self->{parent};
245
246 $self->show;
247 252
248 $CFClient::UI::ROOT->on_post_alloc ( 253 $CFClient::UI::ROOT->on_post_alloc (
249 "centered $self" => sub { 254 "center_$self" => sub {
250 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 255 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5);
251 }, 256 },
252 ); 257 );
258
259 $self->update;
260}
261
262sub set_visible {
263 my ($self) = @_;
264
265 return if $self->{visible};
266
267 $self->{root} = $self->{parent}{root};
268 $self->{visible} = $self->{parent}{visible} + 1;
269
270 $self->emit (visibility_change => 1)
253} 271}
254 272
255sub set_invisible { 273sub set_invisible {
256 my ($self) = @_; 274 my ($self) = @_;
275
276 return unless $self->{visible};
257 277
258 # broken show/hide model 278 # broken show/hide model
259 279
260 delete $self->{root}; 280 delete $self->{root};
261 delete $self->{visible}; 281 delete $self->{visible};
265 285
266 CFClient::UI::check_tooltip 286 CFClient::UI::check_tooltip
267 if $CFClient::UI::TOOLTIP->{owner} == $self; 287 if $CFClient::UI::TOOLTIP->{owner} == $self;
268 288
269 $self->focus_out; 289 $self->focus_out;
290
291 $self->emit (visibility_change => 0);
270} 292}
271 293
272sub hide { 294sub hide {
273 my ($self) = @_; 295 my ($self) = @_;
274 296
289} 311}
290 312
291sub set_size { 313sub set_size {
292 my ($self, $w, $h) = @_; 314 my ($self, $w, $h) = @_;
293 315
294 $self->{user_w} = $w; 316 $self->{def_w} = $w;
295 $self->{user_h} = $h; 317 $self->{def_h} = $h;
296 318
297 $self->check_size; 319 $self->check_size;
298} 320}
299 321
300sub size_request { 322sub size_request {
486sub set_parent { 508sub set_parent {
487 my ($self, $parent) = @_; 509 my ($self, $parent) = @_;
488 510
489 Scalar::Util::weaken ($self->{parent} = $parent); 511 Scalar::Util::weaken ($self->{parent} = $parent);
490 512
491 $self->{root} = $parent->{root}; 513 $self->set_visible; #TODO why breakssssss borked damn if $parent->{visible};
492 $self->{visible} = $parent->{visible} + 1;
493 514
494 # TODO: req_w _does_change after ->reconfigure
495 $self->check_size 515 $self->check_size;
496 unless exists $self->{req_w};
497
498 $self->show;
499} 516}
500 517
501sub check_size { 518sub check_size {
502 my ($self, $forced) = @_; 519 my ($self, $forced) = @_;
503 520
527sub emit { 544sub emit {
528 my ($self, $signal, @args) = @_; 545 my ($self, $signal, @args) = @_;
529 546
530 $self->_emit ($signal, @args) 547 $self->_emit ($signal, @args)
531 || $self->$signal (@args); 548 || $self->$signal (@args);
549}
550
551sub visibility_change {
552 #my ($self, $visible) = @_;
532} 553}
533 554
534sub DESTROY { 555sub DESTROY {
535 my ($self) = @_; 556 my ($self) = @_;
536 557
812} 833}
813 834
814sub size_request { 835sub size_request {
815 my ($self) = @_; 836 my ($self) = @_;
816 837
817 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 838 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
818 839
819 @$self{qw(child_w child_h)} 840 $w = 10 if $self->{scroll_x};
841 $h = 10 if $self->{scroll_y};
842
843 ($w, $h)
820} 844}
821 845
822sub size_allocate { 846sub size_allocate {
823 my ($self, $w, $h) = @_; 847 my ($self, $w, $h) = @_;
824 848
884 my $class = shift; 908 my $class = shift;
885 909
886 my $self; 910 my $self;
887 911
888 my $slider = new CFClient::UI::Slider 912 my $slider = new CFClient::UI::Slider
889 vertical => 1, 913 vertical => 1,
890 range => [0, 0, 1, 0.01], # HACK fix 914 range => [0, 0, 1, 0.01], # HACK fix
891 connect_changed => sub { 915 on_changed => sub {
892 $self->{vp}->set_offset (0, $_[1]); 916 $self->{vp}->set_offset (0, $_[1]);
893 }, 917 },
894 ; 918 ;
895 919
896 $self = $class->SUPER::new ( 920 $self = $class->SUPER::new (
980 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1004 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); 1005 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
982 1006
983sub new { 1007sub new {
984 my $class = shift; 1008 my $class = shift;
985
986 # TODO: user_x, user_y, overwrite moveto?
987 1009
988 my $self = $class->SUPER::new ( 1010 my $self = $class->SUPER::new (
989 bg => [1, 1, 1, 1], 1011 bg => [1, 1, 1, 1],
990 border_bg => [1, 1, 1, 1], 1012 border_bg => [1, 1, 1, 1],
991 border => 0.6, 1013 border => 0.6,
1051 my ($ev, $x, $y) = @_; 1073 my ($ev, $x, $y) = @_;
1052 1074
1053 my $dx = $ev->{x} - $ox; 1075 my $dx = $ev->{x} - $ox;
1054 my $dy = $ev->{y} - $oy; 1076 my $dy = $ev->{y} - $oy;
1055 1077
1078 $self->{user_x} = $wx + $dx * $mx;
1079 $self->{user_y} = $wy + $dy * $my;
1056 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1080 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
1057 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1081 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
1058 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1082 $self->move ($self->{user_x}, $self->{user_y});
1059 $self->check_size; 1083 $self->check_size;
1060 }; 1084 };
1061 1085
1062 } elsif ($lr ^ $td) { 1086 } elsif ($lr ^ $td) {
1063 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1087 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1066 $self->{motion} = sub { 1090 $self->{motion} = sub {
1067 my ($ev, $x, $y) = @_; 1091 my ($ev, $x, $y) = @_;
1068 1092
1069 ($x, $y) = ($ev->{x}, $ev->{y}); 1093 ($x, $y) = ($ev->{x}, $ev->{y});
1070 1094
1071 $self->move ($bx + $x - $ox, $by + $y - $oy); 1095 $self->{user_x} = $bx + $x - $ox;
1096 $self->{user_y} = $by + $y - $oy;
1097
1098 $self->move ($self->{user_x}, $self->{user_y});
1072 $self->update; 1099 $self->update;
1073 }; 1100 };
1074 } 1101 }
1075} 1102}
1076 1103
1274 } 1301 }
1275} 1302}
1276 1303
1277############################################################################# 1304#############################################################################
1278 1305
1279package CFClient::UI::HBox; 1306package CFClient::UI::Box;
1280
1281# TODO: wrap into common Box base class
1282 1307
1283our @ISA = CFClient::UI::Container::; 1308our @ISA = CFClient::UI::Container::;
1284 1309
1285sub size_request { 1310sub size_request {
1286 my ($self) = @_; 1311 my ($self) = @_;
1287 1312
1288 my @alloc = map [$_->size_request], @{$self->{children}}; 1313 $self->{vertical}
1289 1314 ? (
1290 ( 1315 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1291 (List::Util::sum map $_->[0], @alloc), 1316 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1292 (List::Util::max map $_->[1], @alloc), 1317 )
1293 ) 1318 : (
1319 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1320 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1321 )
1294} 1322}
1295 1323
1296sub size_allocate { 1324sub size_allocate {
1297 my ($self, $w, $h) = @_; 1325 my ($self, $w, $h) = @_;
1298 1326
1299 ($h, $w) = ($w, $h); 1327 my $space = $self->{vertical} ? $h : $w;
1300
1301 my $children = $self->{children}; 1328 my $children = $self->{children};
1302 1329
1303 my @h = map $_->{req_w}, @$children; 1330 my @req;
1304 1331
1305 my $req_h = List::Util::sum @h; 1332 if ($self->{homogeneous}) {
1306 1333 @req = ($space / (@$children || 1)) x @$children;
1307 if ($req_h > $h) {
1308 # ah well, not enough space
1309 $_ *= $h / $req_h for @h;
1310 } else { 1334 } else {
1335 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1336 my $req = List::Util::sum @req;
1337
1338 if ($req > $space) {
1339 # ah well, not enough space
1340 $_ *= $space / $req for @req;
1341 } else {
1311 my $exp = List::Util::sum map $_->{expand}, @$children; 1342 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1312 $exp ||= 1;
1313 1343
1344 $space = ($space - $req) / $expand; # remaining space to give away
1345
1346 $req[$_] += $space * $children->[$_]{expand}
1314 for (0 .. $#$children) { 1347 for 0 .. $#$children;
1315 my $child = $children->[$_];
1316
1317 my $alloc_h = $h[$_];
1318 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1319 $h[$_] = $alloc_h;
1320 } 1348 }
1321 } 1349 }
1322 1350
1323 CFClient::UI::harmonize \@h; 1351 CFClient::UI::harmonize \@req;
1324 1352
1325 my $y = 0; 1353 my $pos = 0;
1326 for (0 .. $#$children) { 1354 for (0 .. $#$children) {
1327 my $child = $children->[$_];
1328 my $h = $h[$_]; 1355 my $alloc = $req[$_];
1329 $child->configure ($y, 0, $h, $w); 1356 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1330 1357
1331 $y += $h; 1358 $pos += $alloc;
1332 } 1359 }
1333 1360
1334 1 1361 1
1335} 1362}
1336 1363
1337############################################################################# 1364#############################################################################
1338 1365
1366package CFClient::UI::HBox;
1367
1368our @ISA = CFClient::UI::Box::;
1369
1370sub new {
1371 my $class = shift;
1372
1373 $class->SUPER::new (
1374 vertical => 0,
1375 @_,
1376 )
1377}
1378
1379#############################################################################
1380
1339package CFClient::UI::VBox; 1381package CFClient::UI::VBox;
1340 1382
1341# TODO: wrap into common Box base class
1342
1343our @ISA = CFClient::UI::Container::; 1383our @ISA = CFClient::UI::Box::;
1344 1384
1345sub size_request { 1385sub new {
1346 my ($self) = @_; 1386 my $class = shift;
1347 1387
1348 my @alloc = map [$_->size_request], @{$self->{children}}; 1388 $class->SUPER::new (
1349 1389 vertical => 1,
1350 ( 1390 @_,
1351 (List::Util::max map $_->[0], @alloc),
1352 (List::Util::sum map $_->[1], @alloc),
1353 ) 1391 )
1354}
1355
1356sub size_allocate {
1357 my ($self, $w, $h) = @_;
1358
1359 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1360
1361 my $children = $self->{children};
1362
1363 my @h = map $_->{req_h}, @$children;
1364
1365 my $req_h = List::Util::sum @h;
1366
1367 if ($req_h > $h) {
1368 # ah well, not enough space
1369 $_ *= $h / $req_h for @h;
1370 } else {
1371 my $exp = List::Util::sum map $_->{expand}, @$children;
1372 $exp ||= 1;
1373
1374 for (0 .. $#$children) {
1375 my $child = $children->[$_];
1376
1377 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1378 }
1379 }
1380
1381 CFClient::UI::harmonize \@h;
1382
1383 my $y = 0;
1384 for (0 .. $#$children) {
1385 my $child = $children->[$_];
1386 my $h = $h[$_];
1387 $child->configure (0, $y, $w, $h);
1388
1389 $y += $h;
1390 }
1391
1392 1
1393} 1392}
1394 1393
1395############################################################################# 1394#############################################################################
1396 1395
1397package CFClient::UI::Label; 1396package CFClient::UI::Label;
2570 2569
2571sub new { 2570sub new {
2572 my $class = shift; 2571 my $class = shift;
2573 2572
2574 my $self = $class->SUPER::new ( 2573 my $self = $class->SUPER::new (
2575 state => 0, 2574 state => 0,
2576 connect_activate => \&toggle_flopper, 2575 on_activate => \&toggle_flopper,
2577 @_ 2576 @_
2578 ); 2577 );
2579 2578
2580 if ($self->{state}) {
2581 $self->{state} = 0;
2582 $self->toggle_flopper;
2583 }
2584
2585 $self 2579 $self
2586} 2580}
2587 2581
2588sub toggle_flopper { 2582sub toggle_flopper {
2589 my ($self) = @_; 2583 my ($self) = @_;
2590 2584
2591 # TODO: use animation 2585 $self->{other}->toggle_visibility;
2592 if ($self->{state} = !$self->{state}) {
2593 $CFClient::UI::ROOT->add ($self->{other});
2594 $self->{other}->move ($self->coord2global (0, $self->{h}));
2595 $self->_emit ("open");
2596 } else {
2597 $CFClient::UI::ROOT->remove ($self->{other});
2598 $self->_emit ("close");
2599 }
2600
2601 $self->_emit (changed => $self->{state});
2602} 2586}
2603 2587
2604############################################################################# 2588#############################################################################
2605 2589
2606package CFClient::UI::Tooltip; 2590package CFClient::UI::Tooltip;
2790 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 2774 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2791 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 2775 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2792 2776
2793 $row++; 2777 $row++;
2794 } 2778 }
2795}
2796
2797sub size_request {
2798 my ($self) = @_;
2799 ($self->{req_w}, $self->{req_h});
2800} 2779}
2801 2780
2802############################################################################# 2781#############################################################################
2803 2782
2804package CFClient::UI::Menu; 2783package CFClient::UI::Menu;
3028 if exists $child->{req_x}; 3007 if exists $child->{req_x};
3029 3008
3030 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3009 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3031 if exists $child->{req_y}; 3010 if exists $child->{req_y};
3032 3011
3012 delete @$child{qw(req_x req_y)};
3013
3033 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3014 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3034 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; 3015 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3035 3016
3036 $child->configure ($X, $Y, $W, $H); 3017 $child->configure ($X, $Y, $W, $H);
3037 } 3018 }
3056 $::WANT_REFRESH++; 3037 $::WANT_REFRESH++;
3057} 3038}
3058 3039
3059sub add { 3040sub add {
3060 my ($self, @children) = @_; 3041 my ($self, @children) = @_;
3061
3062 for (my @widgets = @children; my $w = pop @widgets; ) {
3063 push @widgets, $w->children;
3064 $w->{root} = $self;
3065 $w->{visible} = $self->{visible} + 1;
3066 }
3067 3042
3068 for my $child (@children) { 3043 for my $child (@children) {
3069 $child->{is_toplevel} = 1; 3044 $child->{is_toplevel} = 1;
3070 3045
3071 # integerise window positions 3046 # integerise window positions
3072 $child->{x} = int $child->{x}; 3047 $child->{x} = int $child->{x};
3073 $child->{y} = int $child->{y}; 3048 $child->{y} = int $child->{y};
3074 } 3049 }
3075 3050
3076 $self->SUPER::add (@children); 3051 $self->SUPER::add (@children);
3052
3053 for (my @widgets = @children; my $w = pop @widgets; ) {
3054 push @widgets, $w->children;
3055 $w->set_visible;
3056 }
3057
3077} 3058}
3078 3059
3079sub remove { 3060sub remove {
3080 my ($self, @children) = @_; 3061 my ($self, @children) = @_;
3081 3062
3107 $_->() 3088 $_->()
3108 for values %{delete $self->{refresh_hook}}; 3089 for values %{delete $self->{refresh_hook}};
3109 } 3090 }
3110 3091
3111 if ($self->{check_size}) { 3092 if ($self->{check_size}) {
3112 my @queue = ([], []); 3093 my @queue;
3113 3094
3114 for (;;) { 3095 for (;;) {
3115 if ($self->{check_size}) { 3096 if ($self->{check_size}) {
3116 # heuristic: check containers last 3097 #TODO use array-of-depth approach
3117 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3098
3099 @queue = sort { $a->{visible} <=> $b->{visible} }
3118 for values %{delete $self->{check_size}} 3100 @queue, values %{delete $self->{check_size}};
3119 } 3101 }
3120 3102
3121 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3103 my $widget = pop @queue || last;
3122 3104
3105 defined $widget->{visible} or last; # do not resize invisible widgets
3106
3123 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3107 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3124 ? @$widget{qw(user_w user_h)} 3108 ? @$widget{qw(def_w def_h)}
3125 : $widget->size_request; 3109 : $widget->size_request;
3126 3110
3127 if (delete $widget->{force_alloc} 3111 if (delete $widget->{force_alloc}
3128 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3112 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3129 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3113 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines