… | |
… | |
232 | $self->{y} = $y; |
232 | $self->{y} = $y; |
233 | $self->update; |
233 | $self->update; |
234 | } |
234 | } |
235 | |
235 | |
236 | if ($self->{w} != $w || $self->{h} != $h) { |
236 | if ($self->{w} != $w || $self->{h} != $h) { |
237 | $self->{w} = $w; |
237 | $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; |
238 | $self->{h} = $h; |
|
|
239 | |
|
|
240 | $self->size_allocate ($w, $h); |
|
|
241 | $self->update; |
|
|
242 | $self->emit (size_allocate => $w, $h); |
|
|
243 | } |
238 | } |
244 | } |
239 | } |
245 | |
240 | |
246 | sub size_allocate { |
241 | sub size_allocate { |
247 | # nothing to be done |
242 | # nothing to be done |
… | |
… | |
256 | |
251 | |
257 | $_->reconfigure |
252 | $_->reconfigure |
258 | for $self->children; |
253 | for $self->children; |
259 | |
254 | |
260 | $self->check_size; |
255 | $self->check_size; |
261 | $self->size_allocate ($self->{w}, $self->{h}); |
256 | $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}]; |
262 | $self->update; |
257 | $self->update; |
263 | } |
258 | } |
264 | |
259 | |
265 | sub set_max_size { |
260 | sub set_max_size { |
266 | my ($self, $w, $h) = @_; |
261 | my ($self, $w, $h) = @_; |
267 | |
262 | |
268 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
263 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
269 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
264 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
270 | } |
|
|
271 | |
|
|
272 | # return top left coordinates |
|
|
273 | sub _topleft { |
|
|
274 | my ($self, $x, $y) = @_; |
|
|
275 | |
|
|
276 | $self->{parent} |
|
|
277 | or Carp::confess "no parent widget in _topleft\n";#d# |
|
|
278 | |
|
|
279 | $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); |
|
|
280 | } |
265 | } |
281 | |
266 | |
282 | # translate global coordinates to local coordinate system |
267 | # translate global coordinates to local coordinate system |
283 | sub coord2local { |
268 | sub coord2local { |
284 | my ($self, $x, $y) = @_; |
269 | my ($self, $x, $y) = @_; |
285 | |
270 | |
286 | my ($X, $Y) = $self->_topleft; |
271 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
287 | ($x - $X, $y - $Y) |
|
|
288 | } |
272 | } |
289 | |
273 | |
290 | # translate local coordinates to global coordinate system |
274 | # translate local coordinates to global coordinate system |
291 | sub coord2global { |
275 | sub coord2global { |
292 | my ($self, $x, $y) = @_; |
276 | my ($self, $x, $y) = @_; |
293 | |
277 | |
294 | my ($X, $Y) = $self->_topleft; |
278 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
295 | ($x + $X, $y + $Y) |
|
|
296 | } |
279 | } |
297 | |
280 | |
298 | sub focus_in { |
281 | sub focus_in { |
299 | my ($self) = @_; |
282 | my ($self) = @_; |
300 | |
283 | |
… | |
… | |
398 | |
381 | |
399 | sub set_parent { |
382 | sub set_parent { |
400 | my ($self, $parent) = @_; |
383 | my ($self, $parent) = @_; |
401 | |
384 | |
402 | Scalar::Util::weaken ($self->{parent} = $parent); |
385 | Scalar::Util::weaken ($self->{parent} = $parent); |
|
|
386 | |
|
|
387 | $self->check_size |
|
|
388 | unless exists $self->{req_w}; |
403 | } |
389 | } |
404 | |
390 | |
405 | sub check_size { |
391 | sub check_size { |
406 | my ($self) = @_; |
392 | my ($self) = @_; |
407 | |
393 | |
408 | $self->_check_size; |
|
|
409 | return; |
|
|
410 | #TODO: but needs a vastly more difficult algorithm |
|
|
411 | my $check_size = $CFClient::UI::ROOT->{_check_size} ||= {}; |
|
|
412 | |
|
|
413 | $check_size->{$self} = $self; |
|
|
414 | |
|
|
415 | $CFClient::UI::ROOT->on_refresh (_check_size => sub { |
|
|
416 | while (%$check_size) { |
|
|
417 | my @widgets = values %$check_size; |
|
|
418 | $_->_check_size |
|
|
419 | for @widgets; |
|
|
420 | } |
|
|
421 | }); |
|
|
422 | } |
|
|
423 | |
|
|
424 | sub _check_size { |
|
|
425 | my ($self) = @_; |
|
|
426 | |
|
|
427 | delete $CFClient::UI::ROOT->{_check_size}{$self}; |
394 | $CFClient::UI::ROOT->{check_size}{$self} = $self; |
428 | |
|
|
429 | $self->{parent} |
|
|
430 | or return 1; |
|
|
431 | |
|
|
432 | my ($w, $h) = $self->{user_w} && $self->{user_h} |
|
|
433 | ? @$self{qw(user_w user_h)} |
|
|
434 | : $self->size_request; |
|
|
435 | |
|
|
436 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
|
|
437 | $self->{req_w} = $w; |
|
|
438 | $self->{req_h} = $h; |
|
|
439 | |
|
|
440 | $self->{parent}->_check_size |
|
|
441 | or $self->size_allocate ( |
|
|
442 | (List::Util::max $self->{w}, $w), |
|
|
443 | (List::Util::max $self->{h}, $h), |
|
|
444 | ); |
|
|
445 | |
|
|
446 | 1 |
|
|
447 | } else { |
|
|
448 | 0 |
|
|
449 | } |
|
|
450 | } |
395 | } |
451 | |
396 | |
452 | sub update { |
397 | sub update { |
453 | my ($self) = @_; |
398 | my ($self) = @_; |
454 | |
399 | |
455 | my $update = $CFClient::UI::ROOT->{_update} ||= {}; |
400 | $self->{parent}->update |
456 | |
401 | if $self->{parent}; |
457 | $update->{$self} = $self; |
|
|
458 | |
|
|
459 | $CFClient::UI::ROOT->on_refresh (_update => sub { |
|
|
460 | while (%$update) { |
|
|
461 | my @widgets = values %$update; |
|
|
462 | %$update = (); |
|
|
463 | |
|
|
464 | $_->{parent} && $_->{parent}->update |
|
|
465 | for @widgets; |
|
|
466 | } |
|
|
467 | }); |
|
|
468 | } |
402 | } |
469 | |
403 | |
470 | sub connect { |
404 | sub connect { |
471 | my ($self, $signal, $cb) = @_; |
405 | my ($self, $signal, $cb) = @_; |
472 | |
406 | |
… | |
… | |
574 | $self->{children} = [ |
508 | $self->{children} = [ |
575 | sort { $a->{z} <=> $b->{z} } |
509 | sort { $a->{z} <=> $b->{z} } |
576 | @{$self->{children}}, @widgets |
510 | @{$self->{children}}, @widgets |
577 | ]; |
511 | ]; |
578 | |
512 | |
579 | $_->check_size |
513 | $self->check_size; |
580 | for @widgets; |
|
|
581 | |
|
|
582 | $self->update; |
514 | $self->update; |
583 | } |
515 | } |
584 | |
516 | |
585 | sub children { |
517 | sub children { |
586 | @{ $_[0]{children} } |
518 | @{ $_[0]{children} } |
… | |
… | |
776 | $self->{view_y} = int $y; |
708 | $self->{view_y} = int $y; |
777 | |
709 | |
778 | $self->update; |
710 | $self->update; |
779 | } |
711 | } |
780 | |
712 | |
781 | # hmm, this does not work for topleft of $self... but we should not aks for that |
713 | # hmm, this does not work for topleft of $self... but we should not ask for that |
782 | sub _topleft { |
714 | sub coord2local { |
783 | my ($self, $x, $y) = @_; |
715 | my ($self, $x, $y) = @_; |
784 | |
716 | |
785 | $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) |
717 | $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) |
|
|
718 | } |
|
|
719 | |
|
|
720 | sub coord2global { |
|
|
721 | my ($self, $x, $y) = @_; |
|
|
722 | |
|
|
723 | $x = List::Util::min $self->{w}, $x - $self->{view_x}; |
|
|
724 | $y = List::Util::min $self->{h}, $y - $self->{view_y}; |
|
|
725 | |
|
|
726 | $self->SUPER::coord2global ($x, $y) |
786 | } |
727 | } |
787 | |
728 | |
788 | sub find_widget { |
729 | sub find_widget { |
789 | my ($self, $x, $y) = @_; |
730 | my ($self, $x, $y) = @_; |
790 | |
731 | |
… | |
… | |
1271 | ) |
1212 | ) |
1272 | } |
1213 | } |
1273 | |
1214 | |
1274 | sub size_allocate { |
1215 | sub size_allocate { |
1275 | my ($self, $w, $h) = @_; |
1216 | my ($self, $w, $h) = @_; |
|
|
1217 | |
|
|
1218 | Carp::confess "negative size" if $w < 0 || $h < 0;#d# |
1276 | |
1219 | |
1277 | my $children = $self->{children}; |
1220 | my $children = $self->{children}; |
1278 | |
1221 | |
1279 | my @h = map $_->{req_h}, @$children; |
1222 | my @h = map $_->{req_h}, @$children; |
1280 | |
1223 | |
… | |
… | |
1367 | return if $self->{text} eq "T$text"; |
1310 | return if $self->{text} eq "T$text"; |
1368 | $self->{text} = "T$text"; |
1311 | $self->{text} = "T$text"; |
1369 | |
1312 | |
1370 | $self->{layout}->set_text ($text); |
1313 | $self->{layout}->set_text ($text); |
1371 | |
1314 | |
1372 | delete $self->{texture}; |
|
|
1373 | $self->update; |
1315 | $self->update; |
1374 | $self->check_size; |
1316 | $self->check_size; |
1375 | } |
1317 | } |
1376 | |
1318 | |
1377 | sub set_markup { |
1319 | sub set_markup { |
… | |
… | |
1380 | return if $self->{text} eq "M$markup"; |
1322 | return if $self->{text} eq "M$markup"; |
1381 | $self->{text} = "M$markup"; |
1323 | $self->{text} = "M$markup"; |
1382 | |
1324 | |
1383 | $self->{layout}->set_markup ($markup); |
1325 | $self->{layout}->set_markup ($markup); |
1384 | |
1326 | |
1385 | delete $self->{texture}; |
|
|
1386 | $self->update; |
1327 | $self->update; |
1387 | $self->check_size; |
1328 | $self->check_size; |
1388 | } |
1329 | } |
1389 | |
1330 | |
1390 | sub size_request { |
1331 | sub size_request { |
… | |
… | |
2542 | sub new { |
2483 | sub new { |
2543 | my $class = shift; |
2484 | my $class = shift; |
2544 | |
2485 | |
2545 | my %args = @_; |
2486 | my %args = @_; |
2546 | |
2487 | |
2547 | my $item = $args{item}; |
2488 | my $item = delete $args{item}; |
2548 | |
2489 | |
2549 | my $desc = $item->{nrof} < 2 |
2490 | my $desc = $item->{nrof} < 2 |
2550 | ? $item->{name} |
2491 | ? $item->{name} |
2551 | : "$item->{nrof} $item->{name_pl}"; |
2492 | : "$item->{nrof} $item->{name_pl}"; |
2552 | |
2493 | |
… | |
… | |
2581 | |
2522 | |
2582 | 1 |
2523 | 1 |
2583 | }, |
2524 | }, |
2584 | %args |
2525 | %args |
2585 | ); |
2526 | ); |
|
|
2527 | |
2586 | $self->add(new CFClient::UI::Face |
2528 | $self->add (new CFClient::UI::Face |
2587 | can_events => 0, |
2529 | can_events => 0, |
2588 | face => $item->{face}, |
2530 | face => $item->{face}, |
2589 | anim => $item->{anim}, |
2531 | anim => $item->{anim}, |
2590 | animspeed => $item->{animspeed}); |
2532 | animspeed => $item->{animspeed}, |
|
|
2533 | ); |
|
|
2534 | |
2591 | $self->add(new CFClient::UI::Label |
2535 | $self->add (new CFClient::UI::Label |
2592 | can_events => 0, |
2536 | can_events => 0, |
2593 | text => $desc); |
2537 | text => $desc, |
|
|
2538 | ); |
2594 | |
2539 | |
2595 | $self |
2540 | $self |
2596 | } |
2541 | } |
2597 | |
2542 | |
2598 | ############################################################################# |
2543 | ############################################################################# |
… | |
… | |
2625 | for my $item (@items) { |
2570 | for my $item (@items) { |
2626 | my $desc = $item->{nrof} < 2 |
2571 | my $desc = $item->{nrof} < 2 |
2627 | ? $item->{name} |
2572 | ? $item->{name} |
2628 | : "$item->{nrof} $item->{name_pl}"; |
2573 | : "$item->{nrof} $item->{name_pl}"; |
2629 | |
2574 | |
2630 | $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); |
2575 | $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); |
2631 | } |
2576 | } |
2632 | |
2577 | |
2633 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2578 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2634 | } |
2579 | } |
2635 | |
2580 | |
… | |
… | |
2716 | |
2661 | |
2717 | our @ISA = CFClient::UI::Container::; |
2662 | our @ISA = CFClient::UI::Container::; |
2718 | |
2663 | |
2719 | use CFClient::OpenGL; |
2664 | use CFClient::OpenGL; |
2720 | |
2665 | |
|
|
2666 | sub new { |
|
|
2667 | my $class = shift; |
|
|
2668 | |
|
|
2669 | $class->SUPER::new ( |
|
|
2670 | @_, |
|
|
2671 | ) |
|
|
2672 | } |
|
|
2673 | |
|
|
2674 | sub configure { |
|
|
2675 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2676 | |
|
|
2677 | $self->{w} = $w; |
|
|
2678 | $self->{h} = $h; |
|
|
2679 | } |
|
|
2680 | |
2721 | sub check_size { |
2681 | sub check_size { |
2722 | my ($self) = @_; |
2682 | my ($self) = @_; |
2723 | |
2683 | |
2724 | $self->configure (0, 0, $self->{w}, $self->{h}); |
2684 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
2685 | if $self->{w}; |
2725 | } |
2686 | } |
2726 | |
2687 | |
2727 | sub size_request { |
2688 | sub size_request { |
2728 | my ($self) = @_; |
2689 | my ($self) = @_; |
2729 | |
2690 | |
… | |
… | |
2747 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
2708 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
2748 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
2709 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
2749 | } |
2710 | } |
2750 | } |
2711 | } |
2751 | |
2712 | |
2752 | $self->{old_w} = $w; |
|
|
2753 | $self->{old_h} = $h; |
|
|
2754 | } |
|
|
2755 | |
|
|
2756 | sub configure { |
|
|
2757 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2758 | |
|
|
2759 | $self->SUPER::configure ($x, $y, $w, $h); |
|
|
2760 | |
|
|
2761 | for my $child ($self->children) { |
2713 | for my $child ($self->children) { |
2762 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2714 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2763 | |
2715 | |
2764 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2716 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2765 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2717 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2766 | $child->configure ($X, $Y, $W, $H); |
2718 | $child->configure ($X, $Y, $W, $H); |
2767 | } |
2719 | } |
2768 | } |
|
|
2769 | |
2720 | |
2770 | sub _topleft { |
2721 | $self->{old_w} = $w; |
|
|
2722 | $self->{old_h} = $h; |
|
|
2723 | } |
|
|
2724 | |
|
|
2725 | sub coord2local { |
2771 | my ($self, $x, $y) = @_; |
2726 | my ($self, $x, $y) = @_; |
2772 | |
2727 | |
2773 | ($x, $y) |
2728 | ($x, $y) |
2774 | } |
2729 | } |
2775 | |
2730 | |
|
|
2731 | sub coord2global { |
|
|
2732 | my ($self, $x, $y) = @_; |
|
|
2733 | |
|
|
2734 | ($x, $y) |
|
|
2735 | } |
|
|
2736 | |
2776 | sub update { |
2737 | sub update { |
2777 | my ($self) = @_; |
2738 | my ($self) = @_; |
2778 | |
2739 | |
2779 | $self->check_size; |
2740 | $self->check_size; |
2780 | ::refresh (); |
2741 | $::WANT_REFRESH++; |
2781 | } |
2742 | } |
2782 | |
2743 | |
2783 | sub add { |
2744 | sub add { |
2784 | my ($self, $child) = @_; |
2745 | my ($self, $child) = @_; |
2785 | |
2746 | |
2786 | # integerize window positions |
2747 | # integerise window positions |
2787 | $child->{x} = int $child->{x}; |
2748 | $child->{x} = int $child->{x}; |
2788 | $child->{y} = int $child->{y}; |
2749 | $child->{y} = int $child->{y}; |
2789 | |
2750 | |
2790 | $self->SUPER::add ($child); |
2751 | $self->SUPER::add ($child); |
2791 | } |
2752 | } |
… | |
… | |
2794 | my ($self, $id, $cb) = @_; |
2755 | my ($self, $id, $cb) = @_; |
2795 | |
2756 | |
2796 | $self->{refresh_hook}{$id} = $cb; |
2757 | $self->{refresh_hook}{$id} = $cb; |
2797 | } |
2758 | } |
2798 | |
2759 | |
2799 | sub draw { |
2760 | sub draw { |
2800 | my ($self) = @_; |
2761 | my ($self) = @_; |
2801 | |
2762 | |
|
|
2763 | if ($self->{check_size}) { |
|
|
2764 | my @queue = ([], []); |
|
|
2765 | |
|
|
2766 | for (;;) { |
|
|
2767 | if ($self->{check_size}) { |
|
|
2768 | # heuristic: check containers last |
|
|
2769 | push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ |
|
|
2770 | for values %{delete $self->{check_size}} |
|
|
2771 | } |
|
|
2772 | |
|
|
2773 | my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; |
|
|
2774 | |
|
|
2775 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
|
|
2776 | ? @$widget{qw(user_w user_h)} |
|
|
2777 | : $widget->size_request; |
|
|
2778 | |
|
|
2779 | if ($w != $widget->{req_w} || $h != $widget->{req_h}) { |
|
|
2780 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
|
|
2781 | |
|
|
2782 | $widget->{req_w} = $w; |
|
|
2783 | $widget->{req_h} = $h; |
|
|
2784 | |
|
|
2785 | $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; |
|
|
2786 | |
|
|
2787 | $widget->{parent}->check_size |
|
|
2788 | if $widget->{parent}; |
|
|
2789 | } |
|
|
2790 | } |
|
|
2791 | } |
|
|
2792 | |
|
|
2793 | while ($self->{size_alloc}) { |
|
|
2794 | for (values %{delete $self->{size_alloc}}) { |
|
|
2795 | my ($widget, $w, $h) = @$_; |
|
|
2796 | |
|
|
2797 | $w = 0 if $w < 0; |
|
|
2798 | $h = 0 if $h < 0; |
|
|
2799 | |
|
|
2800 | $widget->{w} = $w; |
|
|
2801 | $widget->{h} = $h; |
|
|
2802 | $widget->size_allocate ($w, $h); |
|
|
2803 | $widget->emit (size_allocate => $w, $h); |
|
|
2804 | } |
|
|
2805 | } |
|
|
2806 | |
2802 | while (my $rcb = delete $self->{refresh_hook}) { |
2807 | while ($self->{refresh_hook}) { |
2803 | $_->() for values %$rcb; |
2808 | $_->() |
|
|
2809 | for values %{delete $self->{refresh_hook}}; |
2804 | } |
2810 | } |
2805 | |
2811 | |
2806 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2812 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2807 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2813 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2808 | glClear GL_COLOR_BUFFER_BIT; |
2814 | glClear GL_COLOR_BUFFER_BIT; |