… | |
… | |
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 | |
… | |
… | |
528 | |
530 | |
529 | $self |
531 | $self |
530 | } |
532 | } |
531 | |
533 | |
532 | sub add { |
534 | sub add { |
533 | my ($self, $child) = @_; |
535 | my ($self, @widgets) = @_; |
534 | |
536 | |
535 | $child->set_parent ($self); |
537 | $_->set_parent ($self) |
|
|
538 | for @widgets; |
536 | |
539 | |
537 | use sort 'stable'; |
540 | use sort 'stable'; |
538 | |
541 | |
539 | $self->{children} = [ |
542 | $self->{children} = [ |
540 | sort { $a->{z} <=> $b->{z} } |
543 | sort { $a->{z} <=> $b->{z} } |
541 | @{$self->{children}}, $child |
544 | @{$self->{children}}, @widgets |
542 | ]; |
545 | ]; |
543 | |
546 | |
544 | $child->check_size; |
547 | $_->check_size |
|
|
548 | for @widgets; |
|
|
549 | |
545 | $self->update; |
550 | $self->update; |
546 | } |
551 | } |
547 | |
552 | |
548 | sub children { |
553 | sub children { |
549 | @{ $_[0]{children} } |
554 | @{ $_[0]{children} } |
… | |
… | |
737 | |
742 | |
738 | $self->{view_x} = int $x; |
743 | $self->{view_x} = int $x; |
739 | $self->{view_y} = int $y; |
744 | $self->{view_y} = int $y; |
740 | |
745 | |
741 | $self->update; |
746 | $self->update; |
|
|
747 | } |
|
|
748 | |
|
|
749 | # hmm, this does not work for topleft of $self... but we should not aks for that |
|
|
750 | sub _topleft { |
|
|
751 | my ($self, $x, $y) = @_; |
|
|
752 | |
|
|
753 | $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) |
|
|
754 | } |
|
|
755 | |
|
|
756 | sub find_widget { |
|
|
757 | my ($self, $x, $y) = @_; |
|
|
758 | |
|
|
759 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
|
|
760 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
|
|
761 | ) { |
|
|
762 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
|
|
763 | } else { |
|
|
764 | $self->CFClient::UI::Base::find_widget ($x, $y) |
|
|
765 | } |
742 | } |
766 | } |
743 | |
767 | |
744 | sub _render { |
768 | sub _render { |
745 | my ($self) = @_; |
769 | my ($self) = @_; |
746 | |
770 | |
… | |
… | |
1303 | |
1327 | |
1304 | delete $self->{texture}; |
1328 | delete $self->{texture}; |
1305 | $self->SUPER::update; |
1329 | $self->SUPER::update; |
1306 | } |
1330 | } |
1307 | |
1331 | |
1308 | sub reconfigure { |
|
|
1309 | my ($self) = @_; |
|
|
1310 | |
|
|
1311 | delete $self->{texture}; |
|
|
1312 | } |
|
|
1313 | |
|
|
1314 | sub set_text { |
1332 | sub set_text { |
1315 | my ($self, $text) = @_; |
1333 | my ($self, $text) = @_; |
1316 | |
1334 | |
1317 | return if $self->{text} eq "T$text"; |
1335 | return if $self->{text} eq "T$text"; |
1318 | $self->{text} = "T$text"; |
1336 | $self->{text} = "T$text"; |
… | |
… | |
1371 | sub set_fontsize { |
1389 | sub set_fontsize { |
1372 | my ($self, $fontsize) = @_; |
1390 | my ($self, $fontsize) = @_; |
1373 | |
1391 | |
1374 | $self->{fontsize} = $fontsize; |
1392 | $self->{fontsize} = $fontsize; |
1375 | delete $self->{texture}; |
1393 | delete $self->{texture}; |
|
|
1394 | |
|
|
1395 | $self->update; |
1376 | $self->check_size; |
1396 | $self->check_size; |
1377 | $self->update; |
|
|
1378 | } |
1397 | } |
1379 | |
1398 | |
1380 | sub _draw { |
1399 | sub _draw { |
1381 | my ($self) = @_; |
1400 | my ($self) = @_; |
1382 | |
1401 | |
… | |
… | |
2464 | } |
2483 | } |
2465 | |
2484 | |
2466 | sub _draw { |
2485 | sub _draw { |
2467 | my ($self) = @_; |
2486 | my ($self) = @_; |
2468 | |
2487 | |
|
|
2488 | return unless $::CONN;#d# manage and cache textures differently |
2469 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2489 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2470 | |
2490 | |
2471 | # TODO animation |
2491 | # TODO animation |
2472 | if ($tex) { |
2492 | if ($tex) { |
2473 | glEnable GL_BLEND; |
2493 | glEnable GL_BLEND; |
… | |
… | |
2481 | } |
2501 | } |
2482 | } |
2502 | } |
2483 | |
2503 | |
2484 | ############################################################################# |
2504 | ############################################################################# |
2485 | |
2505 | |
|
|
2506 | package CFClient::UI::InventoryItem; |
|
|
2507 | |
|
|
2508 | our @ISA = CFClient::UI::HBox::; |
|
|
2509 | |
|
|
2510 | sub new { |
|
|
2511 | my $class = shift; |
|
|
2512 | |
|
|
2513 | my %args = @_; |
|
|
2514 | |
|
|
2515 | my $item = $args{item}; |
|
|
2516 | |
|
|
2517 | my $desc = $item->{nrof} < 2 |
|
|
2518 | ? $item->{name} |
|
|
2519 | : "$item->{nrof} $item->{name_pl}"; |
|
|
2520 | |
|
|
2521 | |
|
|
2522 | my $self = $class->SUPER::new ( |
|
|
2523 | can_hover => 1, |
|
|
2524 | can_events => 1, |
|
|
2525 | tooltip => (CFClient::UI::Label->escape ($desc) |
|
|
2526 | . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), |
|
|
2527 | connect_button_down => sub { |
|
|
2528 | my ($self, $ev, $x, $y) = @_; |
|
|
2529 | |
|
|
2530 | # todo: maybe put examine on 1? but should just be a tooltip :( |
|
|
2531 | if ($ev->{button} == 1) { |
|
|
2532 | $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); |
|
|
2533 | } elsif ($ev->{button} == 2) { |
|
|
2534 | $::CONN->send ("apply $item->{tag}"); |
|
|
2535 | } elsif ($ev->{button} == 3) { |
|
|
2536 | CFClient::UI::Menu->new ( |
|
|
2537 | items => [ |
|
|
2538 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
|
|
2539 | [ |
|
|
2540 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
|
|
2541 | sub { $::CONN->send ("lock $item->{tag}") }, |
|
|
2542 | ], |
|
|
2543 | ["mark", sub { $::CONN->send ("mark $item->{tag}") }], |
|
|
2544 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2545 | ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }], |
|
|
2546 | ], |
|
|
2547 | )->popup ($ev); |
|
|
2548 | } |
|
|
2549 | |
|
|
2550 | 1 |
|
|
2551 | }, |
|
|
2552 | %args |
|
|
2553 | ); |
|
|
2554 | $self->add(new CFClient::UI::Face |
|
|
2555 | can_events => 0, |
|
|
2556 | face => $item->{face}, |
|
|
2557 | anim => $item->{anim}, |
|
|
2558 | animspeed => $item->{animspeed}); |
|
|
2559 | $self->add(new CFClient::UI::Label |
|
|
2560 | can_events => 0, |
|
|
2561 | text => $desc); |
|
|
2562 | |
|
|
2563 | $self |
|
|
2564 | } |
|
|
2565 | |
|
|
2566 | ############################################################################# |
|
|
2567 | |
|
|
2568 | package CFClient::UI::Inventory; |
|
|
2569 | |
|
|
2570 | our @ISA = CFClient::UI::ScrolledWindow::; |
|
|
2571 | |
|
|
2572 | sub new { |
|
|
2573 | my $class = shift; |
|
|
2574 | |
|
|
2575 | my $self = $class->SUPER::new ( |
|
|
2576 | scrolled => (new CFClient::UI::VBox), |
|
|
2577 | @_, |
|
|
2578 | ); |
|
|
2579 | |
|
|
2580 | $self |
|
|
2581 | } |
|
|
2582 | |
|
|
2583 | sub set_items { |
|
|
2584 | my ($self, $items) = @_; |
|
|
2585 | |
|
|
2586 | $self->{scrolled}->clear; |
|
|
2587 | return unless $items; |
|
|
2588 | |
|
|
2589 | my @items = sort { $a->{type} <=> $b->{type} } @$items; |
|
|
2590 | |
|
|
2591 | $self->{real_items} = \@items; |
|
|
2592 | |
|
|
2593 | for my $item (@items) { |
|
|
2594 | my $desc = $item->{nrof} < 2 |
|
|
2595 | ? $item->{name} |
|
|
2596 | : "$item->{nrof} $item->{name_pl}"; |
|
|
2597 | |
|
|
2598 | $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); |
|
|
2599 | } |
|
|
2600 | |
|
|
2601 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
|
|
2602 | } |
|
|
2603 | |
|
|
2604 | sub size_request { |
|
|
2605 | my ($self) = @_; |
|
|
2606 | ($self->{req_w}, $self->{req_h}); |
|
|
2607 | } |
|
|
2608 | |
|
|
2609 | ############################################################################# |
|
|
2610 | |
2486 | package CFClient::UI::Menu; |
2611 | package CFClient::UI::Menu; |
2487 | |
2612 | |
2488 | our @ISA = CFClient::UI::FancyFrame::; |
2613 | our @ISA = CFClient::UI::FancyFrame::; |
2489 | |
2614 | |
2490 | use CFClient::OpenGL; |
2615 | use CFClient::OpenGL; |
… | |
… | |
2562 | use CFClient::OpenGL; |
2687 | use CFClient::OpenGL; |
2563 | |
2688 | |
2564 | sub check_size { |
2689 | sub check_size { |
2565 | my ($self) = @_; |
2690 | my ($self) = @_; |
2566 | |
2691 | |
2567 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
2692 | $self->configure (0, 0, $self->{w}, $self->{h}); |
2568 | } |
2693 | } |
2569 | |
2694 | |
2570 | sub size_request { |
2695 | sub size_request { |
2571 | ($::WIDTH, $::HEIGHT) |
2696 | my ($self) = @_; |
|
|
2697 | |
|
|
2698 | ($self->{w}, $self->{h}) |
|
|
2699 | } |
|
|
2700 | |
|
|
2701 | sub size_allocate { |
|
|
2702 | my ($self, $w, $h) = @_; |
|
|
2703 | |
|
|
2704 | my $old_w = $self->{old_w}; |
|
|
2705 | my $old_h = $self->{old_h}; |
|
|
2706 | |
|
|
2707 | if ($old_w && $old_h) { |
|
|
2708 | for my $child ($self->children) { |
|
|
2709 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
|
|
2710 | $child->{w} = int 0.5 + $child->{w} * $w / $old_w; |
|
|
2711 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
|
|
2712 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
|
|
2713 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
|
|
2714 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
|
|
2715 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
|
|
2716 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
|
|
2717 | } |
|
|
2718 | } |
|
|
2719 | |
|
|
2720 | $self->{old_w} = $w; |
|
|
2721 | $self->{old_h} = $h; |
2572 | } |
2722 | } |
2573 | |
2723 | |
2574 | sub configure { |
2724 | sub configure { |
2575 | my ($self, $x, $y, $w, $h) = @_; |
2725 | my ($self, $x, $y, $w, $h) = @_; |
2576 | |
2726 | |
2577 | $self->SUPER::configure ($x, $y, $w, $h); |
2727 | $self->SUPER::configure ($x, $y, $w, $h); |
2578 | |
2728 | |
2579 | for my $child (@{$self->{children}}) { |
2729 | for my $child ($self->children) { |
2580 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2730 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2581 | |
2731 | |
2582 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2732 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2583 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2733 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2584 | $child->configure ($X, $Y, $W,$H); |
2734 | $child->configure ($X, $Y, $W, $H); |
2585 | } |
2735 | } |
2586 | } |
2736 | } |
2587 | |
2737 | |
2588 | sub _topleft { |
2738 | sub _topleft { |
2589 | my ($self, $x, $y) = @_; |
2739 | my ($self, $x, $y) = @_; |
… | |
… | |
2634 | $self->_draw; |
2784 | $self->_draw; |
2635 | } |
2785 | } |
2636 | |
2786 | |
2637 | ############################################################################# |
2787 | ############################################################################# |
2638 | |
2788 | |
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; |
2789 | package CFClient::UI; |
2693 | |
2790 | |
2694 | $ROOT = new CFClient::UI::Root; |
2791 | $ROOT = new CFClient::UI::Root; |
2695 | $TOOLTIP = new CFClient::UI::Tooltip; |
2792 | $TOOLTIP = new CFClient::UI::Tooltip; |
2696 | |
2793 | |