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