… | |
… | |
5 | |
5 | |
6 | use Scalar::Util (); |
6 | use Scalar::Util (); |
7 | use List::Util (); |
7 | use List::Util (); |
8 | |
8 | |
9 | use CFClient; |
9 | use CFClient; |
|
|
10 | use CFClient::Texture; |
10 | |
11 | |
11 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | |
13 | |
13 | our $ROOT; |
14 | our $ROOT; |
14 | our $TOOLTIP; |
15 | our $TOOLTIP; |
… | |
… | |
458 | sub set_parent { |
459 | sub set_parent { |
459 | my ($self, $parent) = @_; |
460 | my ($self, $parent) = @_; |
460 | |
461 | |
461 | Scalar::Util::weaken ($self->{parent} = $parent); |
462 | Scalar::Util::weaken ($self->{parent} = $parent); |
462 | |
463 | |
|
|
464 | $self->{root} = $parent->{root}; |
|
|
465 | $self->{visible} = $parent->{visible}; |
|
|
466 | |
463 | # TODO: req_w _does_change after ->reconfigure |
467 | # TODO: req_w _does_change after ->reconfigure |
464 | $self->check_size |
468 | $self->check_size |
465 | unless exists $self->{req_w}; |
469 | unless exists $self->{req_w}; |
466 | |
470 | |
467 | $self->show; |
471 | $self->show; |
… | |
… | |
618 | delete $child->{parent}; |
622 | delete $child->{parent}; |
619 | $child->hide; |
623 | $child->hide; |
620 | |
624 | |
621 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
625 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
622 | |
626 | |
623 | $self->check_size; |
627 | $self->check_size (1); |
624 | $self->update; |
628 | $self->update; |
625 | } |
629 | } |
626 | |
630 | |
627 | sub clear { |
631 | sub clear { |
628 | my ($self) = @_; |
632 | my ($self) = @_; |
… | |
… | |
871 | $self->{vp}->add ($self->{scrolled}); |
875 | $self->{vp}->add ($self->{scrolled}); |
872 | $self->add ($self->{vp}); |
876 | $self->add ($self->{vp}); |
873 | $self->add ($self->{slider}); |
877 | $self->add ($self->{slider}); |
874 | |
878 | |
875 | $self |
879 | $self |
|
|
880 | } |
|
|
881 | |
|
|
882 | sub update { |
|
|
883 | my ($self) = @_; |
|
|
884 | |
|
|
885 | $self->SUPER::update; |
|
|
886 | |
|
|
887 | # todo: overwrite size_allocate of child |
|
|
888 | my $child = $self->{vp}->child; |
|
|
889 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
876 | } |
890 | } |
877 | |
891 | |
878 | sub size_allocate { |
892 | sub size_allocate { |
879 | my ($self, $w, $h) = @_; |
893 | my ($self, $w, $h) = @_; |
880 | |
894 | |
… | |
… | |
1101 | col_expand => [], |
1115 | col_expand => [], |
1102 | @_, |
1116 | @_, |
1103 | ) |
1117 | ) |
1104 | } |
1118 | } |
1105 | |
1119 | |
|
|
1120 | sub children { |
|
|
1121 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
|
|
1122 | } |
|
|
1123 | |
1106 | sub add { |
1124 | sub add { |
1107 | my ($self, $x, $y, $child) = @_; |
1125 | my ($self, $x, $y, $child) = @_; |
1108 | |
1126 | |
1109 | $child->set_parent ($self); |
1127 | $child->set_parent ($self); |
1110 | $self->{children}[$y][$x] = $child; |
1128 | $self->{children}[$y][$x] = $child; |
1111 | |
1129 | |
1112 | $child->check_size; |
1130 | $self->check_size (1); |
1113 | } |
1131 | } |
1114 | |
1132 | |
1115 | sub children { |
|
|
1116 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
|
|
1117 | } |
|
|
1118 | |
|
|
1119 | # TODO: move to container class maybe? send childs a signal on removal? |
1133 | # TODO: move to container class maybe? send children a signal on removal? |
1120 | sub clear { |
1134 | sub clear { |
1121 | my ($self) = @_; |
1135 | my ($self) = @_; |
1122 | |
1136 | |
1123 | my @children = $self->children; |
1137 | my @children = $self->children; |
1124 | delete $self->{children}; |
1138 | delete $self->{children}; |
… | |
… | |
1126 | for (@children) { |
1140 | for (@children) { |
1127 | delete $_->{parent}; |
1141 | delete $_->{parent}; |
1128 | $_->hide; |
1142 | $_->hide; |
1129 | } |
1143 | } |
1130 | |
1144 | |
|
|
1145 | $self->check_size (1); |
1131 | $self->update; |
1146 | $self->update; |
1132 | } |
1147 | } |
1133 | |
1148 | |
1134 | sub get_wh { |
1149 | sub get_wh { |
1135 | my ($self) = @_; |
1150 | my ($self) = @_; |
… | |
… | |
1167 | sub size_allocate { |
1182 | sub size_allocate { |
1168 | my ($self, $w, $h) = @_; |
1183 | my ($self, $w, $h) = @_; |
1169 | |
1184 | |
1170 | my ($ws, $hs) = $self->get_wh; |
1185 | my ($ws, $hs) = $self->get_wh; |
1171 | |
1186 | |
1172 | my $req_w = sum @$ws; |
1187 | my $req_w = (sum @$ws) || 1; |
1173 | my $req_h = sum @$hs; |
1188 | my $req_h = (sum @$hs) || 1; |
1174 | |
1189 | |
1175 | # TODO: nicer code && do row_expand |
1190 | # TODO: nicer code && do row_expand |
1176 | my @col_expand = @{$self->{col_expand}}; |
1191 | my @col_expand = @{$self->{col_expand}}; |
1177 | @col_expand = (1) x @$ws unless @col_expand; |
1192 | @col_expand = (1) x @$ws unless @col_expand; |
1178 | my $col_expand = (sum @col_expand) || 1; |
1193 | my $col_expand = (sum @col_expand) || 1; |
… | |
… | |
2132 | } |
2147 | } |
2133 | |
2148 | |
2134 | sub set_range { |
2149 | sub set_range { |
2135 | my ($self, $range) = @_; |
2150 | my ($self, $range) = @_; |
2136 | |
2151 | |
2137 | $self->{range} = $range; |
2152 | ($range, $self->{range}) = ($self->{range}, $range); |
2138 | |
2153 | |
2139 | $self->update; |
2154 | $self->update |
|
|
2155 | if "@$range" ne "@{$self->{range}}"; |
2140 | } |
2156 | } |
2141 | |
2157 | |
2142 | sub set_value { |
2158 | sub set_value { |
2143 | my ($self, $value) = @_; |
2159 | my ($self, $value) = @_; |
2144 | |
2160 | |
… | |
… | |
2709 | $self->SUPER::DESTROY; |
2725 | $self->SUPER::DESTROY; |
2710 | } |
2726 | } |
2711 | |
2727 | |
2712 | ############################################################################# |
2728 | ############################################################################# |
2713 | |
2729 | |
2714 | package CFClient::UI::InventoryItem; |
|
|
2715 | |
|
|
2716 | our @ISA = CFClient::UI::HBox::; |
|
|
2717 | |
|
|
2718 | sub new { |
|
|
2719 | my $class = shift; |
|
|
2720 | |
|
|
2721 | my %args = @_; |
|
|
2722 | |
|
|
2723 | my $item = delete $args{item}; |
|
|
2724 | |
|
|
2725 | my $desc = CFClient::Item::desc_string $item; |
|
|
2726 | |
|
|
2727 | my $self = $class->SUPER::new ( |
|
|
2728 | can_hover => 1, |
|
|
2729 | can_events => 1, |
|
|
2730 | tooltip => ((CFClient::UI::Label::escape $desc) |
|
|
2731 | . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"), |
|
|
2732 | connect_button_down => sub { |
|
|
2733 | my ($self, $ev, $x, $y) = @_; |
|
|
2734 | |
|
|
2735 | # todo: maybe put examine on 1? but should just be a tooltip :( |
|
|
2736 | if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) { |
|
|
2737 | my $targ = $::CONN->{player}{tag}; |
|
|
2738 | |
|
|
2739 | if ($item->{container} == $::CONN->{player}{tag}) { |
|
|
2740 | $targ = $main::OPENCONT; |
|
|
2741 | } |
|
|
2742 | |
|
|
2743 | $::CONN->send ("move $targ $item->{tag} 0"); |
|
|
2744 | } elsif ($ev->{button} == 1) { |
|
|
2745 | $::CONN->send ("examine $item->{tag}"); |
|
|
2746 | } elsif ($ev->{button} == 2) { |
|
|
2747 | $::CONN->send ("apply $item->{tag}"); |
|
|
2748 | } elsif ($ev->{button} == 3) { |
|
|
2749 | my @menu_items = ( |
|
|
2750 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
|
|
2751 | ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }], |
|
|
2752 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2753 | ( |
|
|
2754 | $item->{flags} & Crossfire::Protocol::F_LOCKED |
|
|
2755 | ? ( |
|
|
2756 | ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }], |
|
|
2757 | ) |
|
|
2758 | : ( |
|
|
2759 | ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }], |
|
|
2760 | ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], |
|
|
2761 | ) |
|
|
2762 | ), |
|
|
2763 | ); |
|
|
2764 | |
|
|
2765 | CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); |
|
|
2766 | } |
|
|
2767 | |
|
|
2768 | 1 |
|
|
2769 | }, |
|
|
2770 | %args |
|
|
2771 | ); |
|
|
2772 | |
|
|
2773 | |
|
|
2774 | $self->add (new CFClient::UI::Face |
|
|
2775 | can_events => 0, |
|
|
2776 | face => $item->{face}, |
|
|
2777 | anim => $item->{anim}, |
|
|
2778 | animspeed => $item->{animspeed}, |
|
|
2779 | ); |
|
|
2780 | |
|
|
2781 | $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0); |
|
|
2782 | |
|
|
2783 | $self->{item} = $item; |
|
|
2784 | |
|
|
2785 | $self->update_item; |
|
|
2786 | |
|
|
2787 | $self |
|
|
2788 | } |
|
|
2789 | |
|
|
2790 | sub update_item { |
|
|
2791 | my ($self) = @_; |
|
|
2792 | |
|
|
2793 | my $desc = CFClient::Item::desc_string ($self->{item}); |
|
|
2794 | |
|
|
2795 | $self->{name_lbl}->set_text ($desc); |
|
|
2796 | } |
|
|
2797 | |
|
|
2798 | ############################################################################# |
|
|
2799 | |
|
|
2800 | package CFClient::UI::Inventory; |
2730 | package CFClient::UI::Inventory; |
2801 | |
2731 | |
2802 | our @ISA = CFClient::UI::ScrolledWindow::; |
2732 | our @ISA = CFClient::UI::ScrolledWindow::; |
2803 | |
2733 | |
2804 | sub new { |
2734 | sub new { |
… | |
… | |
2833 | $self->{scrolled}->add (1, $row, $item->{desc_widget}); |
2763 | $self->{scrolled}->add (1, $row, $item->{desc_widget}); |
2834 | $self->{scrolled}->add (2, $row, $item->{weight_widget}); |
2764 | $self->{scrolled}->add (2, $row, $item->{weight_widget}); |
2835 | |
2765 | |
2836 | $row++; |
2766 | $row++; |
2837 | } |
2767 | } |
2838 | |
|
|
2839 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
|
|
2840 | } |
2768 | } |
2841 | |
2769 | |
2842 | sub size_request { |
2770 | sub size_request { |
2843 | my ($self) = @_; |
2771 | my ($self) = @_; |
2844 | ($self->{req_w}, $self->{req_h}); |
2772 | ($self->{req_w}, $self->{req_h}); |