… | |
… | |
255 | my ($self) = @_; |
255 | my ($self) = @_; |
256 | |
256 | |
257 | $_->reconfigure |
257 | $_->reconfigure |
258 | for $self->children; |
258 | for $self->children; |
259 | |
259 | |
260 | $_->check_size; |
260 | $self->check_size; |
|
|
261 | $self->size_allocate ($self->{w}, $self->{h}); |
|
|
262 | $self->update; |
261 | } |
263 | } |
262 | |
264 | |
263 | sub set_max_size { |
265 | sub set_max_size { |
264 | my ($self, $w, $h) = @_; |
266 | my ($self, $w, $h) = @_; |
265 | |
267 | |
… | |
… | |
401 | } |
403 | } |
402 | |
404 | |
403 | sub check_size { |
405 | sub check_size { |
404 | my ($self) = @_; |
406 | my ($self) = @_; |
405 | |
407 | |
|
|
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}; |
|
|
428 | |
406 | $self->{parent} |
429 | $self->{parent} |
407 | or return 1; |
430 | or return 1; |
408 | |
431 | |
409 | my ($w, $h) = $self->{user_w} && $self->{user_h} |
432 | my ($w, $h) = $self->{user_w} && $self->{user_h} |
410 | ? @$self{qw(user_w user_h)} |
433 | ? @$self{qw(user_w user_h)} |
… | |
… | |
412 | |
435 | |
413 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
436 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
414 | $self->{req_w} = $w; |
437 | $self->{req_w} = $w; |
415 | $self->{req_h} = $h; |
438 | $self->{req_h} = $h; |
416 | |
439 | |
417 | $self->{parent}->check_size |
440 | $self->{parent}->_check_size |
418 | or $self->size_allocate ( |
441 | or $self->size_allocate ( |
419 | (List::Util::max $self->{w}, $w), |
442 | (List::Util::max $self->{w}, $w), |
420 | (List::Util::max $self->{h}, $h), |
443 | (List::Util::max $self->{h}, $h), |
421 | ); |
444 | ); |
422 | |
445 | |
… | |
… | |
427 | } |
450 | } |
428 | |
451 | |
429 | sub update { |
452 | sub update { |
430 | my ($self) = @_; |
453 | my ($self) = @_; |
431 | |
454 | |
432 | $self->{parent}->update |
455 | my $update = $CFClient::UI::ROOT->{_update} ||= {}; |
433 | if $self->{parent}; |
456 | |
|
|
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 | }); |
434 | } |
468 | } |
435 | |
469 | |
436 | sub connect { |
470 | sub connect { |
437 | my ($self, $signal, $cb) = @_; |
471 | my ($self, $signal, $cb) = @_; |
438 | |
472 | |
… | |
… | |
528 | |
562 | |
529 | $self |
563 | $self |
530 | } |
564 | } |
531 | |
565 | |
532 | sub add { |
566 | sub add { |
533 | my ($self, $child) = @_; |
567 | my ($self, @widgets) = @_; |
534 | |
568 | |
535 | $child->set_parent ($self); |
569 | $_->set_parent ($self) |
|
|
570 | for @widgets; |
536 | |
571 | |
537 | use sort 'stable'; |
572 | use sort 'stable'; |
538 | |
573 | |
539 | $self->{children} = [ |
574 | $self->{children} = [ |
540 | sort { $a->{z} <=> $b->{z} } |
575 | sort { $a->{z} <=> $b->{z} } |
541 | @{$self->{children}}, $child |
576 | @{$self->{children}}, @widgets |
542 | ]; |
577 | ]; |
543 | |
578 | |
544 | $child->check_size; |
579 | $_->check_size |
|
|
580 | for @widgets; |
|
|
581 | |
545 | $self->update; |
582 | $self->update; |
546 | } |
583 | } |
547 | |
584 | |
548 | sub children { |
585 | sub children { |
549 | @{ $_[0]{children} } |
586 | @{ $_[0]{children} } |
… | |
… | |
737 | |
774 | |
738 | $self->{view_x} = int $x; |
775 | $self->{view_x} = int $x; |
739 | $self->{view_y} = int $y; |
776 | $self->{view_y} = int $y; |
740 | |
777 | |
741 | $self->update; |
778 | $self->update; |
|
|
779 | } |
|
|
780 | |
|
|
781 | # hmm, this does not work for topleft of $self... but we should not aks for that |
|
|
782 | sub _topleft { |
|
|
783 | my ($self, $x, $y) = @_; |
|
|
784 | |
|
|
785 | $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) |
|
|
786 | } |
|
|
787 | |
|
|
788 | sub find_widget { |
|
|
789 | my ($self, $x, $y) = @_; |
|
|
790 | |
|
|
791 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
|
|
792 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
|
|
793 | ) { |
|
|
794 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
|
|
795 | } else { |
|
|
796 | $self->CFClient::UI::Base::find_widget ($x, $y) |
|
|
797 | } |
742 | } |
798 | } |
743 | |
799 | |
744 | sub _render { |
800 | sub _render { |
745 | my ($self) = @_; |
801 | my ($self) = @_; |
746 | |
802 | |
… | |
… | |
1303 | |
1359 | |
1304 | delete $self->{texture}; |
1360 | delete $self->{texture}; |
1305 | $self->SUPER::update; |
1361 | $self->SUPER::update; |
1306 | } |
1362 | } |
1307 | |
1363 | |
1308 | sub reconfigure { |
|
|
1309 | my ($self) = @_; |
|
|
1310 | |
|
|
1311 | delete $self->{texture}; |
|
|
1312 | } |
|
|
1313 | |
|
|
1314 | sub set_text { |
1364 | sub set_text { |
1315 | my ($self, $text) = @_; |
1365 | my ($self, $text) = @_; |
1316 | |
1366 | |
1317 | return if $self->{text} eq "T$text"; |
1367 | return if $self->{text} eq "T$text"; |
1318 | $self->{text} = "T$text"; |
1368 | $self->{text} = "T$text"; |
… | |
… | |
1371 | sub set_fontsize { |
1421 | sub set_fontsize { |
1372 | my ($self, $fontsize) = @_; |
1422 | my ($self, $fontsize) = @_; |
1373 | |
1423 | |
1374 | $self->{fontsize} = $fontsize; |
1424 | $self->{fontsize} = $fontsize; |
1375 | delete $self->{texture}; |
1425 | delete $self->{texture}; |
|
|
1426 | |
|
|
1427 | $self->update; |
1376 | $self->check_size; |
1428 | $self->check_size; |
1377 | $self->update; |
|
|
1378 | } |
1429 | } |
1379 | |
1430 | |
1380 | sub _draw { |
1431 | sub _draw { |
1381 | my ($self) = @_; |
1432 | my ($self) = @_; |
1382 | |
1433 | |
… | |
… | |
2464 | } |
2515 | } |
2465 | |
2516 | |
2466 | sub _draw { |
2517 | sub _draw { |
2467 | my ($self) = @_; |
2518 | my ($self) = @_; |
2468 | |
2519 | |
|
|
2520 | return unless $::CONN;#d# manage and cache textures differently |
2469 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2521 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2470 | |
2522 | |
2471 | # TODO animation |
2523 | # TODO animation |
2472 | if ($tex) { |
2524 | if ($tex) { |
2473 | glEnable GL_BLEND; |
2525 | glEnable GL_BLEND; |
… | |
… | |
2481 | } |
2533 | } |
2482 | } |
2534 | } |
2483 | |
2535 | |
2484 | ############################################################################# |
2536 | ############################################################################# |
2485 | |
2537 | |
|
|
2538 | package CFClient::UI::InventoryItem; |
|
|
2539 | |
|
|
2540 | our @ISA = CFClient::UI::HBox::; |
|
|
2541 | |
|
|
2542 | sub new { |
|
|
2543 | my $class = shift; |
|
|
2544 | |
|
|
2545 | my %args = @_; |
|
|
2546 | |
|
|
2547 | my $item = $args{item}; |
|
|
2548 | |
|
|
2549 | my $desc = $item->{nrof} < 2 |
|
|
2550 | ? $item->{name} |
|
|
2551 | : "$item->{nrof} $item->{name_pl}"; |
|
|
2552 | |
|
|
2553 | |
|
|
2554 | my $self = $class->SUPER::new ( |
|
|
2555 | can_hover => 1, |
|
|
2556 | can_events => 1, |
|
|
2557 | tooltip => (CFClient::UI::Label->escape ($desc) |
|
|
2558 | . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), |
|
|
2559 | connect_button_down => sub { |
|
|
2560 | my ($self, $ev, $x, $y) = @_; |
|
|
2561 | |
|
|
2562 | # todo: maybe put examine on 1? but should just be a tooltip :( |
|
|
2563 | if ($ev->{button} == 1) { |
|
|
2564 | $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); |
|
|
2565 | } elsif ($ev->{button} == 2) { |
|
|
2566 | $::CONN->send ("apply $item->{tag}"); |
|
|
2567 | } elsif ($ev->{button} == 3) { |
|
|
2568 | CFClient::UI::Menu->new ( |
|
|
2569 | items => [ |
|
|
2570 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
|
|
2571 | [ |
|
|
2572 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
|
|
2573 | sub { $::CONN->send ("lock $item->{tag}") }, |
|
|
2574 | ], |
|
|
2575 | ["mark", sub { $::CONN->send ("mark $item->{tag}") }], |
|
|
2576 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2577 | ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }], |
|
|
2578 | ], |
|
|
2579 | )->popup ($ev); |
|
|
2580 | } |
|
|
2581 | |
|
|
2582 | 1 |
|
|
2583 | }, |
|
|
2584 | %args |
|
|
2585 | ); |
|
|
2586 | $self->add(new CFClient::UI::Face |
|
|
2587 | can_events => 0, |
|
|
2588 | face => $item->{face}, |
|
|
2589 | anim => $item->{anim}, |
|
|
2590 | animspeed => $item->{animspeed}); |
|
|
2591 | $self->add(new CFClient::UI::Label |
|
|
2592 | can_events => 0, |
|
|
2593 | text => $desc); |
|
|
2594 | |
|
|
2595 | $self |
|
|
2596 | } |
|
|
2597 | |
|
|
2598 | ############################################################################# |
|
|
2599 | |
|
|
2600 | package CFClient::UI::Inventory; |
|
|
2601 | |
|
|
2602 | our @ISA = CFClient::UI::ScrolledWindow::; |
|
|
2603 | |
|
|
2604 | sub new { |
|
|
2605 | my $class = shift; |
|
|
2606 | |
|
|
2607 | my $self = $class->SUPER::new ( |
|
|
2608 | scrolled => (new CFClient::UI::VBox), |
|
|
2609 | @_, |
|
|
2610 | ); |
|
|
2611 | |
|
|
2612 | $self |
|
|
2613 | } |
|
|
2614 | |
|
|
2615 | sub set_items { |
|
|
2616 | my ($self, $items) = @_; |
|
|
2617 | |
|
|
2618 | $self->{scrolled}->clear; |
|
|
2619 | return unless $items; |
|
|
2620 | |
|
|
2621 | my @items = sort { $a->{type} <=> $b->{type} } @$items; |
|
|
2622 | |
|
|
2623 | $self->{real_items} = \@items; |
|
|
2624 | |
|
|
2625 | for my $item (@items) { |
|
|
2626 | my $desc = $item->{nrof} < 2 |
|
|
2627 | ? $item->{name} |
|
|
2628 | : "$item->{nrof} $item->{name_pl}"; |
|
|
2629 | |
|
|
2630 | $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); |
|
|
2631 | } |
|
|
2632 | |
|
|
2633 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
|
|
2634 | } |
|
|
2635 | |
|
|
2636 | sub size_request { |
|
|
2637 | my ($self) = @_; |
|
|
2638 | ($self->{req_w}, $self->{req_h}); |
|
|
2639 | } |
|
|
2640 | |
|
|
2641 | ############################################################################# |
|
|
2642 | |
2486 | package CFClient::UI::Menu; |
2643 | package CFClient::UI::Menu; |
2487 | |
2644 | |
2488 | our @ISA = CFClient::UI::FancyFrame::; |
2645 | our @ISA = CFClient::UI::FancyFrame::; |
2489 | |
2646 | |
2490 | use CFClient::OpenGL; |
2647 | use CFClient::OpenGL; |
… | |
… | |
2562 | use CFClient::OpenGL; |
2719 | use CFClient::OpenGL; |
2563 | |
2720 | |
2564 | sub check_size { |
2721 | sub check_size { |
2565 | my ($self) = @_; |
2722 | my ($self) = @_; |
2566 | |
2723 | |
2567 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
2724 | $self->configure (0, 0, $self->{w}, $self->{h}); |
2568 | } |
2725 | } |
2569 | |
2726 | |
2570 | sub size_request { |
2727 | sub size_request { |
2571 | ($::WIDTH, $::HEIGHT) |
2728 | my ($self) = @_; |
|
|
2729 | |
|
|
2730 | ($self->{w}, $self->{h}) |
|
|
2731 | } |
|
|
2732 | |
|
|
2733 | sub size_allocate { |
|
|
2734 | my ($self, $w, $h) = @_; |
|
|
2735 | |
|
|
2736 | my $old_w = $self->{old_w}; |
|
|
2737 | my $old_h = $self->{old_h}; |
|
|
2738 | |
|
|
2739 | if ($old_w && $old_h) { |
|
|
2740 | for my $child ($self->children) { |
|
|
2741 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
|
|
2742 | $child->{w} = int 0.5 + $child->{w} * $w / $old_w; |
|
|
2743 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
|
|
2744 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
|
|
2745 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
|
|
2746 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
|
|
2747 | $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}; |
|
|
2749 | } |
|
|
2750 | } |
|
|
2751 | |
|
|
2752 | $self->{old_w} = $w; |
|
|
2753 | $self->{old_h} = $h; |
2572 | } |
2754 | } |
2573 | |
2755 | |
2574 | sub configure { |
2756 | sub configure { |
2575 | my ($self, $x, $y, $w, $h) = @_; |
2757 | my ($self, $x, $y, $w, $h) = @_; |
2576 | |
2758 | |
2577 | $self->SUPER::configure ($x, $y, $w, $h); |
2759 | $self->SUPER::configure ($x, $y, $w, $h); |
2578 | |
2760 | |
2579 | for my $child (@{$self->{children}}) { |
2761 | for my $child ($self->children) { |
2580 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2762 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2581 | |
2763 | |
2582 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2764 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2583 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2765 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2584 | $child->configure ($X, $Y, $W,$H); |
2766 | $child->configure ($X, $Y, $W, $H); |
2585 | } |
2767 | } |
2586 | } |
2768 | } |
2587 | |
2769 | |
2588 | sub _topleft { |
2770 | sub _topleft { |
2589 | my ($self, $x, $y) = @_; |
2771 | my ($self, $x, $y) = @_; |
… | |
… | |
2634 | $self->_draw; |
2816 | $self->_draw; |
2635 | } |
2817 | } |
2636 | |
2818 | |
2637 | ############################################################################# |
2819 | ############################################################################# |
2638 | |
2820 | |
2639 | package CFClient::UI::Inventory; |
|
|
2640 | |
|
|
2641 | our @ISA = CFClient::UI::ScrolledWindow::; |
|
|
2642 | |
|
|
2643 | sub new { |
|
|
2644 | my $class = shift; |
|
|
2645 | |
|
|
2646 | my $self = $class->SUPER::new ( |
|
|
2647 | scrolled => (new CFClient::UI::VBox), |
|
|
2648 | @_, |
|
|
2649 | ); |
|
|
2650 | |
|
|
2651 | $self |
|
|
2652 | } |
|
|
2653 | |
|
|
2654 | sub set_items { |
|
|
2655 | my ($self, $items) = @_; |
|
|
2656 | |
|
|
2657 | $self->{scrolled}->clear; |
|
|
2658 | return unless $items; |
|
|
2659 | |
|
|
2660 | my @items = sort { $a->{type} <=> $b->{type} } @$items; |
|
|
2661 | |
|
|
2662 | $self->{real_items} = \@items; |
|
|
2663 | |
|
|
2664 | for my $item (@items) { |
|
|
2665 | my $desc = $item->{nrof} < 2 |
|
|
2666 | ? $item->{name} |
|
|
2667 | : "$item->{nrof} $item->{name_pl}"; |
|
|
2668 | |
|
|
2669 | my $hb = new CFClient::UI::HBox; |
|
|
2670 | |
|
|
2671 | $hb->add (my $f = new CFClient::UI::Face |
|
|
2672 | can_events => 0, |
|
|
2673 | face => $item->{face}, |
|
|
2674 | anim => $item->{anim}, |
|
|
2675 | animspeed => $item->{animspeed}, |
|
|
2676 | expand => 1, |
|
|
2677 | ); |
|
|
2678 | $hb->add (new CFClient::UI::Label text => $desc, expand => 1); |
|
|
2679 | $self->{scrolled}->add ($hb); |
|
|
2680 | } |
|
|
2681 | |
|
|
2682 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
|
|
2683 | } |
|
|
2684 | |
|
|
2685 | sub size_request { |
|
|
2686 | my ($self) = @_; |
|
|
2687 | (100, 200) |
|
|
2688 | } |
|
|
2689 | |
|
|
2690 | ############################################################################# |
|
|
2691 | |
|
|
2692 | package CFClient::UI; |
2821 | package CFClient::UI; |
2693 | |
2822 | |
2694 | $ROOT = new CFClient::UI::Root; |
2823 | $ROOT = new CFClient::UI::Root; |
2695 | $TOOLTIP = new CFClient::UI::Tooltip; |
2824 | $TOOLTIP = new CFClient::UI::Tooltip; |
2696 | |
2825 | |