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.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}
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
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
884 my $class = shift; 907 my $class = shift;
885 908
886 my $self; 909 my $self;
887 910
888 my $slider = new CFClient::UI::Slider 911 my $slider = new CFClient::UI::Slider
889 vertical => 1, 912 vertical => 1,
890 range => [0, 0, 1, 0.01], # HACK fix 913 range => [0, 0, 1, 0.01], # HACK fix
891 connect_changed => sub { 914 on_changed => sub {
892 $self->{vp}->set_offset (0, $_[1]); 915 $self->{vp}->set_offset (0, $_[1]);
893 }, 916 },
894 ; 917 ;
895 918
896 $self = $class->SUPER::new ( 919 $self = $class->SUPER::new (
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,
1051 my ($ev, $x, $y) = @_; 1072 my ($ev, $x, $y) = @_;
1052 1073
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
1077 $self->{user_x} = $wx + $dx * $mx;
1078 $self->{user_y} = $wy + $dy * $my;
1056 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1079 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
1057 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1080 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
1058 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1081 $self->move ($self->{user_x}, $self->{user_y});
1059 $self->check_size; 1082 $self->check_size;
1060 }; 1083 };
1061 1084
1062 } elsif ($lr ^ $td) { 1085 } elsif ($lr ^ $td) {
1063 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1086 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1066 $self->{motion} = sub { 1089 $self->{motion} = sub {
1067 my ($ev, $x, $y) = @_; 1090 my ($ev, $x, $y) = @_;
1068 1091
1069 ($x, $y) = ($ev->{x}, $ev->{y}); 1092 ($x, $y) = ($ev->{x}, $ev->{y});
1070 1093
1071 $self->move ($bx + $x - $ox, $by + $y - $oy); 1094 $self->{user_x} = $bx + $x - $ox;
1095 $self->{user_y} = $by + $y - $oy;
1096
1097 $self->move ($self->{user_x}, $self->{user_y});
1072 $self->update; 1098 $self->update;
1073 }; 1099 };
1074 } 1100 }
1075} 1101}
1076 1102
1274 } 1300 }
1275} 1301}
1276 1302
1277############################################################################# 1303#############################################################################
1278 1304
1279package CFClient::UI::HBox; 1305package CFClient::UI::Box;
1280
1281# TODO: wrap into common Box base class
1282 1306
1283our @ISA = CFClient::UI::Container::; 1307our @ISA = CFClient::UI::Container::;
1284 1308
1285sub size_request { 1309sub size_request {
1286 my ($self) = @_; 1310 my ($self) = @_;
1287 1311
1288 my @alloc = map [$_->size_request], @{$self->{children}}; 1312 $self->{vertical}
1289 1313 ? (
1290 ( 1314 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1291 (List::Util::sum map $_->[0], @alloc), 1315 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1292 (List::Util::max map $_->[1], @alloc), 1316 )
1293 ) 1317 : (
1318 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1319 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1320 )
1294} 1321}
1295 1322
1296sub size_allocate { 1323sub size_allocate {
1297 my ($self, $w, $h) = @_; 1324 my ($self, $w, $h) = @_;
1298 1325
1299 ($h, $w) = ($w, $h); 1326 my $space = $self->{vertical} ? $h : $w;
1300
1301 my $children = $self->{children}; 1327 my $children = $self->{children};
1302 1328
1303 my @h = map $_->{req_w}, @$children; 1329 my @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1304
1305 my $req_h = List::Util::sum @h; 1330 my $req = List::Util::sum @req;
1306 1331
1307 if ($req_h > $h) { 1332 if ($req > $space) {
1308 # ah well, not enough space 1333 # ah well, not enough space
1309 $_ *= $h / $req_h for @h; 1334 $_ *= $space / $req for @req;
1310 } else { 1335 } else {
1311 my $exp = List::Util::sum map $_->{expand}, @$children; 1336 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1312 $exp ||= 1;
1313 1337
1338 $req[$_] += ($space - $req) * $children->[$_]{expand} / $expand
1314 for (0 .. $#$children) { 1339 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 }
1321 } 1340 }
1322 1341
1323 CFClient::UI::harmonize \@h; 1342 CFClient::UI::harmonize \@req;
1324 1343
1325 my $y = 0; 1344 my $pos = 0;
1326 for (0 .. $#$children) { 1345 for (0 .. $#$children) {
1327 my $child = $children->[$_];
1328 my $h = $h[$_]; 1346 my $alloc = $req[$_];
1329 $child->configure ($y, 0, $h, $w); 1347 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1330 1348
1331 $y += $h; 1349 $pos += $alloc;
1332 } 1350 }
1333 1351
1334 1 1352 1
1335} 1353}
1336 1354
1337############################################################################# 1355#############################################################################
1338 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
1339package CFClient::UI::VBox; 1372package CFClient::UI::VBox;
1340 1373
1341# TODO: wrap into common Box base class
1342
1343our @ISA = CFClient::UI::Container::; 1374our @ISA = CFClient::UI::Box::;
1344 1375
1345sub size_request { 1376sub new {
1346 my ($self) = @_; 1377 my $class = shift;
1347 1378
1348 my @alloc = map [$_->size_request], @{$self->{children}}; 1379 $class->SUPER::new (
1349 1380 vertical => 1,
1350 ( 1381 @_,
1351 (List::Util::max map $_->[0], @alloc),
1352 (List::Util::sum map $_->[1], @alloc),
1353 ) 1382 )
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} 1383}
1394 1384
1395############################################################################# 1385#############################################################################
1396 1386
1397package CFClient::UI::Label; 1387package CFClient::UI::Label;
2570 2560
2571sub new { 2561sub new {
2572 my $class = shift; 2562 my $class = shift;
2573 2563
2574 my $self = $class->SUPER::new ( 2564 my $self = $class->SUPER::new (
2575 state => 0, 2565 state => 0,
2576 connect_activate => \&toggle_flopper, 2566 on_activate => \&toggle_flopper,
2577 @_ 2567 @_
2578 ); 2568 );
2579 2569
2580 if ($self->{state}) {
2581 $self->{state} = 0;
2582 $self->toggle_flopper;
2583 }
2584
2585 $self 2570 $self
2586} 2571}
2587 2572
2588sub toggle_flopper { 2573sub toggle_flopper {
2589 my ($self) = @_; 2574 my ($self) = @_;
2590 2575
2591 # TODO: use animation 2576 $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} 2577}
2603 2578
2604############################################################################# 2579#############################################################################
2605 2580
2606package CFClient::UI::Tooltip; 2581package CFClient::UI::Tooltip;
2790 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 2765 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2791 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 2766 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2792 2767
2793 $row++; 2768 $row++;
2794 } 2769 }
2795}
2796
2797sub size_request {
2798 my ($self) = @_;
2799 ($self->{req_w}, $self->{req_h});
2800} 2770}
2801 2771
2802############################################################################# 2772#############################################################################
2803 2773
2804package CFClient::UI::Menu; 2774package CFClient::UI::Menu;
3028 if exists $child->{req_x}; 2998 if exists $child->{req_x};
3029 2999
3030 $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
3031 if exists $child->{req_y}; 3001 if exists $child->{req_y};
3032 3002
3003 delete @$child{qw(req_x req_y)};
3004
3033 $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;
3034 $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;
3035 3007
3036 $child->configure ($X, $Y, $W, $H); 3008 $child->configure ($X, $Y, $W, $H);
3037 } 3009 }
3056 $::WANT_REFRESH++; 3028 $::WANT_REFRESH++;
3057} 3029}
3058 3030
3059sub add { 3031sub add {
3060 my ($self, @children) = @_; 3032 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 3033
3068 for my $child (@children) { 3034 for my $child (@children) {
3069 $child->{is_toplevel} = 1; 3035 $child->{is_toplevel} = 1;
3070 3036
3071 # integerise window positions 3037 # integerise window positions
3072 $child->{x} = int $child->{x}; 3038 $child->{x} = int $child->{x};
3073 $child->{y} = int $child->{y}; 3039 $child->{y} = int $child->{y};
3074 } 3040 }
3075 3041
3076 $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
3077} 3049}
3078 3050
3079sub remove { 3051sub remove {
3080 my ($self, @children) = @_; 3052 my ($self, @children) = @_;
3081 3053
3107 $_->() 3079 $_->()
3108 for values %{delete $self->{refresh_hook}}; 3080 for values %{delete $self->{refresh_hook}};
3109 } 3081 }
3110 3082
3111 if ($self->{check_size}) { 3083 if ($self->{check_size}) {
3112 my @queue = ([], []); 3084 my @queue;
3113 3085
3114 for (;;) { 3086 for (;;) {
3115 if ($self->{check_size}) { 3087 if ($self->{check_size}) {
3116 # heuristic: check containers last 3088 #TODO use array-of-depth approach
3117 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3089
3090 @queue = sort { $a->{visible} <=> $b->{visible} }
3118 for values %{delete $self->{check_size}} 3091 @queue, values %{delete $self->{check_size}};
3119 } 3092 }
3120 3093
3121 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3094 my $widget = pop @queue || last;
3122 3095
3096 defined $widget->{visible} or last; # do not resize invisible widgets
3097
3123 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3098 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3124 ? @$widget{qw(user_w user_h)} 3099 ? @$widget{qw(def_w def_h)}
3125 : $widget->size_request; 3100 : $widget->size_request;
3126 3101
3127 if (delete $widget->{force_alloc} 3102 if (delete $widget->{force_alloc}
3128 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3103 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3129 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