… | |
… | |
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 |
… | |
… | |
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 | } |
265 | } |
271 | |
266 | |
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 | } |
|
|
281 | |
|
|
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 | |
… | |
… | |
397 | } |
380 | } |
398 | |
381 | |
399 | sub set_parent { |
382 | sub set_parent { |
400 | my ($self, $parent) = @_; |
383 | my ($self, $parent) = @_; |
401 | |
384 | |
|
|
385 | $self->{_tree_depth} = $parent->{_tree_depth} + 1; |
402 | Scalar::Util::weaken ($self->{parent} = $parent); |
386 | Scalar::Util::weaken ($self->{parent} = $parent); |
403 | } |
387 | } |
404 | |
388 | |
405 | sub check_size { |
389 | sub check_size { |
406 | my ($self) = @_; |
390 | my ($self) = @_; |
407 | |
391 | |
408 | $self->{parent} |
392 | $CFClient::UI::ROOT->{check_size}{$self} = $self; |
409 | or return 1; |
|
|
410 | |
|
|
411 | my ($w, $h) = $self->{user_w} && $self->{user_h} |
|
|
412 | ? @$self{qw(user_w user_h)} |
|
|
413 | : $self->size_request; |
|
|
414 | |
|
|
415 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
|
|
416 | $self->{req_w} = $w; |
|
|
417 | $self->{req_h} = $h; |
|
|
418 | |
|
|
419 | $self->{parent}->check_size |
|
|
420 | or $self->size_allocate ( |
|
|
421 | (List::Util::max $self->{w}, $w), |
|
|
422 | (List::Util::max $self->{h}, $h), |
|
|
423 | ); |
|
|
424 | |
|
|
425 | 1 |
|
|
426 | } else { |
|
|
427 | 0 |
|
|
428 | } |
|
|
429 | } |
393 | } |
430 | |
394 | |
431 | sub update { |
395 | sub update { |
432 | my ($self) = @_; |
396 | my ($self) = @_; |
433 | |
397 | |
… | |
… | |
530 | |
494 | |
531 | $self |
495 | $self |
532 | } |
496 | } |
533 | |
497 | |
534 | sub add { |
498 | sub add { |
535 | my ($self, $child) = @_; |
499 | my ($self, @widgets) = @_; |
536 | |
500 | |
537 | $child->set_parent ($self); |
501 | $_->set_parent ($self) |
|
|
502 | for @widgets; |
538 | |
503 | |
539 | use sort 'stable'; |
504 | use sort 'stable'; |
540 | |
505 | |
541 | $self->{children} = [ |
506 | $self->{children} = [ |
542 | sort { $a->{z} <=> $b->{z} } |
507 | sort { $a->{z} <=> $b->{z} } |
543 | @{$self->{children}}, $child |
508 | @{$self->{children}}, @widgets |
544 | ]; |
509 | ]; |
545 | |
510 | |
546 | $child->check_size; |
511 | $_->check_size |
|
|
512 | for @widgets; |
|
|
513 | |
547 | $self->update; |
514 | $self->update; |
548 | } |
515 | } |
549 | |
516 | |
550 | sub children { |
517 | sub children { |
551 | @{ $_[0]{children} } |
518 | @{ $_[0]{children} } |
… | |
… | |
741 | $self->{view_y} = int $y; |
708 | $self->{view_y} = int $y; |
742 | |
709 | |
743 | $self->update; |
710 | $self->update; |
744 | } |
711 | } |
745 | |
712 | |
746 | # 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 |
747 | sub _topleft { |
714 | sub coord2local { |
748 | my ($self, $x, $y) = @_; |
715 | my ($self, $x, $y) = @_; |
749 | |
716 | |
750 | $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) |
751 | } |
727 | } |
752 | |
728 | |
753 | sub find_widget { |
729 | sub find_widget { |
754 | my ($self, $x, $y) = @_; |
730 | my ($self, $x, $y) = @_; |
755 | |
731 | |
… | |
… | |
1332 | return if $self->{text} eq "T$text"; |
1308 | return if $self->{text} eq "T$text"; |
1333 | $self->{text} = "T$text"; |
1309 | $self->{text} = "T$text"; |
1334 | |
1310 | |
1335 | $self->{layout}->set_text ($text); |
1311 | $self->{layout}->set_text ($text); |
1336 | |
1312 | |
1337 | delete $self->{texture}; |
|
|
1338 | $self->update; |
1313 | $self->update; |
1339 | $self->check_size; |
1314 | $self->check_size; |
1340 | } |
1315 | } |
1341 | |
1316 | |
1342 | sub set_markup { |
1317 | sub set_markup { |
… | |
… | |
1345 | return if $self->{text} eq "M$markup"; |
1320 | return if $self->{text} eq "M$markup"; |
1346 | $self->{text} = "M$markup"; |
1321 | $self->{text} = "M$markup"; |
1347 | |
1322 | |
1348 | $self->{layout}->set_markup ($markup); |
1323 | $self->{layout}->set_markup ($markup); |
1349 | |
1324 | |
1350 | delete $self->{texture}; |
|
|
1351 | $self->update; |
1325 | $self->update; |
1352 | $self->check_size; |
1326 | $self->check_size; |
1353 | } |
1327 | } |
1354 | |
1328 | |
1355 | sub size_request { |
1329 | sub size_request { |
… | |
… | |
2681 | |
2655 | |
2682 | our @ISA = CFClient::UI::Container::; |
2656 | our @ISA = CFClient::UI::Container::; |
2683 | |
2657 | |
2684 | use CFClient::OpenGL; |
2658 | use CFClient::OpenGL; |
2685 | |
2659 | |
|
|
2660 | sub new { |
|
|
2661 | my $class = shift; |
|
|
2662 | |
|
|
2663 | $class->SUPER::new ( |
|
|
2664 | @_, |
|
|
2665 | ) |
|
|
2666 | } |
|
|
2667 | |
|
|
2668 | sub configure { |
|
|
2669 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2670 | |
|
|
2671 | $self->{w} = $w; |
|
|
2672 | $self->{h} = $h; |
|
|
2673 | } |
|
|
2674 | |
2686 | sub check_size { |
2675 | sub check_size { |
2687 | my ($self) = @_; |
2676 | my ($self) = @_; |
2688 | |
2677 | |
2689 | $self->configure (0, 0, $self->{w}, $self->{h}); |
2678 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
2679 | if $self->{w}; |
2690 | } |
2680 | } |
2691 | |
2681 | |
2692 | sub size_request { |
2682 | sub size_request { |
2693 | my ($self) = @_; |
2683 | my ($self) = @_; |
2694 | |
2684 | |
… | |
… | |
2702 | my $old_h = $self->{old_h}; |
2692 | my $old_h = $self->{old_h}; |
2703 | |
2693 | |
2704 | if ($old_w && $old_h) { |
2694 | if ($old_w && $old_h) { |
2705 | for my $child ($self->children) { |
2695 | for my $child ($self->children) { |
2706 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
2696 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
2707 | $child->{w} = int 0.5 + $child->{req_w} * $w / $old_w; |
2697 | $child->{w} = int 0.5 + $child->{w} * $w / $old_w; |
2708 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
2698 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
2709 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
2699 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
2710 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
2700 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
2711 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
2701 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
2712 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
2702 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
2713 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
2703 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
2714 | } |
2704 | } |
2715 | } |
2705 | } |
2716 | |
2706 | |
2717 | $self->{old_w} = $w; |
|
|
2718 | $self->{old_h} = $h; |
|
|
2719 | } |
|
|
2720 | |
|
|
2721 | sub configure { |
|
|
2722 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2723 | |
|
|
2724 | $self->SUPER::configure ($x, $y, $w, $h); |
|
|
2725 | |
|
|
2726 | for my $child ($self->children) { |
2707 | for my $child ($self->children) { |
2727 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2708 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2728 | |
2709 | |
2729 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2710 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2730 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2711 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2731 | $child->configure ($X, $Y, $W, $H); |
2712 | $child->configure ($X, $Y, $W, $H); |
2732 | } |
2713 | } |
2733 | } |
|
|
2734 | |
2714 | |
2735 | sub _topleft { |
2715 | $self->{old_w} = $w; |
|
|
2716 | $self->{old_h} = $h; |
|
|
2717 | } |
|
|
2718 | |
|
|
2719 | sub coord2local { |
2736 | my ($self, $x, $y) = @_; |
2720 | my ($self, $x, $y) = @_; |
2737 | |
2721 | |
2738 | ($x, $y) |
2722 | ($x, $y) |
2739 | } |
2723 | } |
2740 | |
2724 | |
|
|
2725 | sub coord2global { |
|
|
2726 | my ($self, $x, $y) = @_; |
|
|
2727 | |
|
|
2728 | ($x, $y) |
|
|
2729 | } |
|
|
2730 | |
2741 | sub update { |
2731 | sub update { |
2742 | my ($self) = @_; |
2732 | my ($self) = @_; |
2743 | |
2733 | |
2744 | $self->check_size; |
2734 | $self->check_size; |
2745 | ::refresh (); |
2735 | $::WANT_REFRESH++; |
2746 | } |
2736 | } |
2747 | |
2737 | |
2748 | sub add { |
2738 | sub add { |
2749 | my ($self, $child) = @_; |
2739 | my ($self, $child) = @_; |
2750 | |
2740 | |
2751 | # integerize window positions |
2741 | # integerise window positions |
2752 | $child->{x} = int $child->{x}; |
2742 | $child->{x} = int $child->{x}; |
2753 | $child->{y} = int $child->{y}; |
2743 | $child->{y} = int $child->{y}; |
2754 | |
2744 | |
2755 | $self->SUPER::add ($child); |
2745 | $self->SUPER::add ($child); |
2756 | } |
2746 | } |
… | |
… | |
2759 | my ($self, $id, $cb) = @_; |
2749 | my ($self, $id, $cb) = @_; |
2760 | |
2750 | |
2761 | $self->{refresh_hook}{$id} = $cb; |
2751 | $self->{refresh_hook}{$id} = $cb; |
2762 | } |
2752 | } |
2763 | |
2753 | |
2764 | sub draw { |
2754 | sub draw { |
2765 | my ($self) = @_; |
2755 | my ($self) = @_; |
2766 | |
2756 | |
|
|
2757 | if ($self->{check_size}) { |
|
|
2758 | my @queue = ([], []); |
|
|
2759 | |
|
|
2760 | for (;;) { |
|
|
2761 | if ($self->{check_size}) { |
|
|
2762 | # heuristic: check containers last |
|
|
2763 | push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ |
|
|
2764 | for values %{delete $self->{check_size}} |
|
|
2765 | } |
|
|
2766 | |
|
|
2767 | my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; |
|
|
2768 | |
|
|
2769 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
|
|
2770 | ? @$widget{qw(user_w user_h)} |
|
|
2771 | : $widget->size_request; |
|
|
2772 | |
|
|
2773 | if ($w != $widget->{req_w} || $h != $widget->{req_h}) { |
|
|
2774 | $widget->{req_w} = $w; |
|
|
2775 | $widget->{req_h} = $h; |
|
|
2776 | |
|
|
2777 | $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; |
|
|
2778 | |
|
|
2779 | $widget->{parent}->check_size |
|
|
2780 | if $widget->{parent}; |
|
|
2781 | } |
|
|
2782 | } |
|
|
2783 | } |
|
|
2784 | |
|
|
2785 | while ($self->{size_alloc}) { |
|
|
2786 | for (values %{delete $self->{size_alloc}}) { |
|
|
2787 | my ($widget, $w, $h) = @$_; |
|
|
2788 | |
|
|
2789 | $widget->{w} = $w; |
|
|
2790 | $widget->{h} = $h; |
|
|
2791 | $widget->size_allocate ($w, $h); |
|
|
2792 | $widget->emit (size_allocate => $w, $h); |
|
|
2793 | } |
|
|
2794 | } |
|
|
2795 | |
2767 | while (my $rcb = delete $self->{refresh_hook}) { |
2796 | while ($self->{refresh_hook}) { |
2768 | $_->() for values %$rcb; |
2797 | $_->() |
|
|
2798 | for values %{delete $self->{refresh_hook}}; |
2769 | } |
2799 | } |
2770 | |
2800 | |
2771 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2801 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2772 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2802 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2773 | glClear GL_COLOR_BUFFER_BIT; |
2803 | glClear GL_COLOR_BUFFER_BIT; |