… | |
… | |
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; |
… | |
… | |
322 | } |
323 | } |
323 | |
324 | |
324 | sub set_tooltip { |
325 | sub set_tooltip { |
325 | my ($self, $tooltip) = @_; |
326 | my ($self, $tooltip) = @_; |
326 | |
327 | |
|
|
328 | $tooltip =~ s/^\s+//; |
|
|
329 | $tooltip =~ s/\s+$//; |
|
|
330 | |
|
|
331 | return if $self->{tooltip} eq $tooltip; |
|
|
332 | |
327 | $self->{tooltip} = $tooltip; |
333 | $self->{tooltip} = $tooltip; |
328 | |
334 | |
329 | if ($CFClient::UI::TOOLTIP->{owner} == $self) { |
335 | if ($CFClient::UI::TOOLTIP->{owner} == $self) { |
330 | delete $CFClient::UI::TOOLTIP->{owner}; |
336 | delete $CFClient::UI::TOOLTIP->{owner}; |
331 | CFClient::UI::check_tooltip; |
337 | CFClient::UI::check_tooltip; |
… | |
… | |
420 | if ($ENV{PCLIENT_DEBUG}) { |
426 | if ($ENV{PCLIENT_DEBUG}) { |
421 | glPushMatrix; |
427 | glPushMatrix; |
422 | glColor 1, 1, 0, 1; |
428 | glColor 1, 1, 0, 1; |
423 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
429 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
424 | glBegin GL_LINE_LOOP; |
430 | glBegin GL_LINE_LOOP; |
425 | glVertex 0 , 0; |
431 | glVertex 0 , 0; |
426 | glVertex $self->{w}, 0; |
432 | glVertex $self->{w} - 1, 0; |
427 | glVertex $self->{w}, $self->{h}; |
433 | glVertex $self->{w} - 1, $self->{h} - 1; |
428 | glVertex 0 , $self->{h}; |
434 | glVertex 0 , $self->{h} - 1; |
429 | glEnd; |
435 | glEnd; |
430 | glPopMatrix; |
436 | glPopMatrix; |
431 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
437 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
432 | } |
438 | } |
433 | } |
439 | } |
… | |
… | |
452 | |
458 | |
453 | sub set_parent { |
459 | sub set_parent { |
454 | my ($self, $parent) = @_; |
460 | my ($self, $parent) = @_; |
455 | |
461 | |
456 | Scalar::Util::weaken ($self->{parent} = $parent); |
462 | Scalar::Util::weaken ($self->{parent} = $parent); |
|
|
463 | |
|
|
464 | $self->{root} = $parent->{root}; |
|
|
465 | $self->{visible} = $parent->{visible}; |
457 | |
466 | |
458 | # TODO: req_w _does_change after ->reconfigure |
467 | # TODO: req_w _does_change after ->reconfigure |
459 | $self->check_size |
468 | $self->check_size |
460 | unless exists $self->{req_w}; |
469 | unless exists $self->{req_w}; |
461 | |
470 | |
… | |
… | |
613 | delete $child->{parent}; |
622 | delete $child->{parent}; |
614 | $child->hide; |
623 | $child->hide; |
615 | |
624 | |
616 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
625 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
617 | |
626 | |
618 | $self->check_size; |
627 | $self->check_size (1); |
619 | $self->update; |
628 | $self->update; |
620 | } |
629 | } |
621 | |
630 | |
622 | sub clear { |
631 | sub clear { |
623 | my ($self) = @_; |
632 | my ($self) = @_; |
… | |
… | |
763 | |
772 | |
764 | package CFClient::UI::ViewPort; |
773 | package CFClient::UI::ViewPort; |
765 | |
774 | |
766 | our @ISA = CFClient::UI::Window::; |
775 | our @ISA = CFClient::UI::Window::; |
767 | |
776 | |
|
|
777 | sub new { |
|
|
778 | my $class = shift; |
|
|
779 | |
|
|
780 | $class->SUPER::new ( |
|
|
781 | scroll_x => 0, |
|
|
782 | scroll_y => 1, |
|
|
783 | @_, |
|
|
784 | ) |
|
|
785 | } |
|
|
786 | |
768 | sub size_request { |
787 | sub size_request { |
769 | my ($self) = @_; |
788 | my ($self) = @_; |
770 | |
789 | |
771 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
790 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
772 | |
791 | |
… | |
… | |
774 | } |
793 | } |
775 | |
794 | |
776 | sub size_allocate { |
795 | sub size_allocate { |
777 | my ($self, $w, $h) = @_; |
796 | my ($self, $w, $h) = @_; |
778 | |
797 | |
779 | my ($cw, $ch) = @$self{qw(child_w child_h)}; |
798 | $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; |
780 | # $w = $self->{w}; |
799 | $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; |
|
|
800 | |
781 | $self->child->configure (0, 0, $cw, $ch); |
801 | $self->child->configure (0, 0, $w, $h); |
782 | $self->update; |
802 | $self->update; |
783 | } |
803 | } |
784 | |
804 | |
785 | sub set_offset { |
805 | sub set_offset { |
786 | my ($self, $x, $y) = @_; |
806 | my ($self, $x, $y) = @_; |
… | |
… | |
855 | $self->{vp}->add ($self->{scrolled}); |
875 | $self->{vp}->add ($self->{scrolled}); |
856 | $self->add ($self->{vp}); |
876 | $self->add ($self->{vp}); |
857 | $self->add ($self->{slider}); |
877 | $self->add ($self->{slider}); |
858 | |
878 | |
859 | $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]); |
860 | } |
890 | } |
861 | |
891 | |
862 | sub size_allocate { |
892 | sub size_allocate { |
863 | my ($self, $w, $h) = @_; |
893 | my ($self, $w, $h) = @_; |
864 | |
894 | |
… | |
… | |
1081 | sub new { |
1111 | sub new { |
1082 | my $class = shift; |
1112 | my $class = shift; |
1083 | |
1113 | |
1084 | $class->SUPER::new ( |
1114 | $class->SUPER::new ( |
1085 | col_expand => [], |
1115 | col_expand => [], |
1086 | @_ |
1116 | @_, |
1087 | ) |
1117 | ) |
|
|
1118 | } |
|
|
1119 | |
|
|
1120 | sub children { |
|
|
1121 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
1088 | } |
1122 | } |
1089 | |
1123 | |
1090 | sub add { |
1124 | sub add { |
1091 | my ($self, $x, $y, $child) = @_; |
1125 | my ($self, $x, $y, $child) = @_; |
1092 | |
1126 | |
1093 | $child->set_parent ($self); |
1127 | $child->set_parent ($self); |
1094 | $self->{children}[$y][$x] = $child; |
1128 | $self->{children}[$y][$x] = $child; |
1095 | |
1129 | |
1096 | $child->check_size; |
1130 | $self->check_size (1); |
1097 | } |
1131 | } |
1098 | |
1132 | |
1099 | sub children { |
|
|
1100 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
|
|
1101 | } |
|
|
1102 | |
|
|
1103 | # 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? |
1104 | sub clear { |
1134 | sub clear { |
1105 | my ($self) = @_; |
1135 | my ($self) = @_; |
1106 | |
1136 | |
1107 | my @children = $self->children; |
1137 | my @children = $self->children; |
1108 | delete $self->{children}; |
1138 | delete $self->{children}; |
… | |
… | |
1110 | for (@children) { |
1140 | for (@children) { |
1111 | delete $_->{parent}; |
1141 | delete $_->{parent}; |
1112 | $_->hide; |
1142 | $_->hide; |
1113 | } |
1143 | } |
1114 | |
1144 | |
|
|
1145 | $self->check_size (1); |
1115 | $self->update; |
1146 | $self->update; |
1116 | } |
1147 | } |
1117 | |
1148 | |
1118 | sub get_wh { |
1149 | sub get_wh { |
1119 | my ($self) = @_; |
1150 | my ($self) = @_; |
… | |
… | |
1151 | sub size_allocate { |
1182 | sub size_allocate { |
1152 | my ($self, $w, $h) = @_; |
1183 | my ($self, $w, $h) = @_; |
1153 | |
1184 | |
1154 | my ($ws, $hs) = $self->get_wh; |
1185 | my ($ws, $hs) = $self->get_wh; |
1155 | |
1186 | |
1156 | my $req_w = sum @$ws; |
1187 | my $req_w = (sum @$ws) || 1; |
1157 | my $req_h = sum @$hs; |
1188 | my $req_h = (sum @$hs) || 1; |
1158 | |
1189 | |
1159 | # TODO: nicer code && do row_expand |
1190 | # TODO: nicer code && do row_expand |
1160 | my @col_expand = @{$self->{col_expand}}; |
1191 | my @col_expand = @{$self->{col_expand}}; |
1161 | @col_expand = (1) x @$ws unless @col_expand; |
1192 | @col_expand = (1) x @$ws unless @col_expand; |
1162 | my $col_expand = (sum @col_expand) || 1; |
1193 | my $col_expand = (sum @col_expand) || 1; |
… | |
… | |
2116 | } |
2147 | } |
2117 | |
2148 | |
2118 | sub set_range { |
2149 | sub set_range { |
2119 | my ($self, $range) = @_; |
2150 | my ($self, $range) = @_; |
2120 | |
2151 | |
2121 | $self->{range} = $range; |
2152 | ($range, $self->{range}) = ($self->{range}, $range); |
2122 | |
2153 | |
2123 | $self->update; |
2154 | $self->update |
|
|
2155 | if "@$range" ne "@{$self->{range}}"; |
2124 | } |
2156 | } |
2125 | |
2157 | |
2126 | sub set_value { |
2158 | sub set_value { |
2127 | my ($self, $value) = @_; |
2159 | my ($self, $value) = @_; |
2128 | |
2160 | |
… | |
… | |
2623 | |
2655 | |
2624 | sub new { |
2656 | sub new { |
2625 | my $class = shift; |
2657 | my $class = shift; |
2626 | |
2658 | |
2627 | my $self = $class->SUPER::new ( |
2659 | my $self = $class->SUPER::new ( |
2628 | aspect => 1, |
2660 | aspect => 1, |
|
|
2661 | can_events => 0, |
2629 | @_, |
2662 | @_, |
2630 | ); |
2663 | ); |
2631 | |
2664 | |
2632 | if ($self->{anim} && $self->{animspeed}) { |
2665 | if ($self->{anim} && $self->{animspeed}) { |
2633 | Scalar::Util::weaken (my $widget = $self); |
2666 | Scalar::Util::weaken (my $widget = $self); |
… | |
… | |
2692 | $self->SUPER::DESTROY; |
2725 | $self->SUPER::DESTROY; |
2693 | } |
2726 | } |
2694 | |
2727 | |
2695 | ############################################################################# |
2728 | ############################################################################# |
2696 | |
2729 | |
2697 | package CFClient::UI::InventoryItem; |
2730 | package CFClient::UI::Inventory; |
2698 | |
2731 | |
2699 | our @ISA = CFClient::UI::HBox::; |
2732 | our @ISA = CFClient::UI::ScrolledWindow::; |
2700 | |
|
|
2701 | sub _item_to_desc { |
|
|
2702 | my ($item) = @_; |
|
|
2703 | |
|
|
2704 | my $desc = |
|
|
2705 | $item->{nrof} < 2 |
|
|
2706 | ? $item->{name} |
|
|
2707 | : "$item->{nrof} × $item->{name_pl}"; |
|
|
2708 | |
|
|
2709 | $item->{flags} & Crossfire::Protocol::F_OPEN |
|
|
2710 | and $desc .= " (open)"; |
|
|
2711 | $item->{flags} & Crossfire::Protocol::F_APPLIED |
|
|
2712 | and $desc .= " (applied)"; |
|
|
2713 | $item->{flags} & Crossfire::Protocol::F_UNPAID |
|
|
2714 | and $desc .= " (unpaid)"; |
|
|
2715 | $item->{flags} & Crossfire::Protocol::F_MAGIC |
|
|
2716 | and $desc .= " (magic)"; |
|
|
2717 | $item->{flags} & Crossfire::Protocol::F_CURSED |
|
|
2718 | and $desc .= " (cursed)"; |
|
|
2719 | $item->{flags} & Crossfire::Protocol::F_DAMNED |
|
|
2720 | and $desc .= " (damned)"; |
|
|
2721 | $item->{flags} & Crossfire::Protocol::F_LOCKED |
|
|
2722 | and $desc .= " *"; |
|
|
2723 | |
|
|
2724 | $desc |
|
|
2725 | } |
|
|
2726 | |
2733 | |
2727 | sub new { |
2734 | sub new { |
2728 | my $class = shift; |
2735 | my $class = shift; |
2729 | |
2736 | |
2730 | my %args = @_; |
|
|
2731 | |
|
|
2732 | my $item = delete $args{item}; |
|
|
2733 | |
|
|
2734 | my $desc = _item_to_desc ($item); |
|
|
2735 | |
|
|
2736 | my $self = $class->SUPER::new ( |
2737 | my $self = $class->SUPER::new ( |
2737 | can_hover => 1, |
|
|
2738 | can_events => 1, |
|
|
2739 | tooltip => ((CFClient::UI::Label::escape $desc) |
|
|
2740 | . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"), |
|
|
2741 | connect_button_down => sub { |
|
|
2742 | my ($self, $ev, $x, $y) = @_; |
|
|
2743 | |
|
|
2744 | # todo: maybe put examine on 1? but should just be a tooltip :( |
|
|
2745 | if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) { |
|
|
2746 | my $targ = $::CONN->{player}{tag}; |
|
|
2747 | |
|
|
2748 | if ($item->{container} == $::CONN->{player}{tag}) { |
|
|
2749 | $targ = $main::OPENCONT; |
|
|
2750 | } |
|
|
2751 | |
|
|
2752 | $::CONN->send ("move $targ $item->{tag} 0"); |
|
|
2753 | } elsif ($ev->{button} == 1) { |
|
|
2754 | $::CONN->send ("examine $item->{tag}"); |
|
|
2755 | } elsif ($ev->{button} == 2) { |
|
|
2756 | $::CONN->send ("apply $item->{tag}"); |
|
|
2757 | } elsif ($ev->{button} == 3) { |
|
|
2758 | my @menu_items = ( |
|
|
2759 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
|
|
2760 | ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }], |
|
|
2761 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2762 | ( |
|
|
2763 | $item->{flags} & Crossfire::Protocol::F_LOCKED |
|
|
2764 | ? ( |
|
|
2765 | ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }], |
|
|
2766 | ) |
|
|
2767 | : ( |
|
|
2768 | ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }], |
|
|
2769 | ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], |
|
|
2770 | ) |
|
|
2771 | ), |
|
|
2772 | ); |
|
|
2773 | |
|
|
2774 | CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); |
|
|
2775 | } |
|
|
2776 | |
|
|
2777 | 1 |
|
|
2778 | }, |
|
|
2779 | %args |
|
|
2780 | ); |
|
|
2781 | |
|
|
2782 | |
|
|
2783 | $self->add (new CFClient::UI::Face |
|
|
2784 | can_events => 0, |
|
|
2785 | face => $item->{face}, |
|
|
2786 | anim => $item->{anim}, |
|
|
2787 | animspeed => $item->{animspeed}, |
|
|
2788 | ); |
|
|
2789 | |
|
|
2790 | $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0); |
|
|
2791 | |
|
|
2792 | $self->{item} = $item; |
|
|
2793 | |
|
|
2794 | $self->update_item; |
|
|
2795 | |
|
|
2796 | $self |
|
|
2797 | } |
|
|
2798 | |
|
|
2799 | sub update_item { |
|
|
2800 | my ($self) = @_; |
|
|
2801 | |
|
|
2802 | my $desc = _item_to_desc ($self->{item}); |
|
|
2803 | |
|
|
2804 | $self->{name_lbl}->set_text ($desc); |
|
|
2805 | } |
|
|
2806 | |
|
|
2807 | ############################################################################# |
|
|
2808 | |
|
|
2809 | package CFClient::UI::Inventory; |
|
|
2810 | |
|
|
2811 | our @ISA = CFClient::UI::ScrolledWindow::; |
|
|
2812 | |
|
|
2813 | sub new { |
|
|
2814 | my $class = shift; |
|
|
2815 | |
|
|
2816 | my $self = $class->SUPER::new ( |
|
|
2817 | scrolled => (new CFClient::UI::Table), |
2738 | scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]), |
2818 | @_, |
2739 | @_, |
2819 | ); |
2740 | ); |
2820 | |
2741 | |
2821 | $self |
2742 | $self |
2822 | } |
2743 | } |
… | |
… | |
2832 | or ($a->{name} cmp $b->{name}) |
2753 | or ($a->{name} cmp $b->{name}) |
2833 | } @$items; |
2754 | } @$items; |
2834 | |
2755 | |
2835 | $self->{real_items} = \@items; |
2756 | $self->{real_items} = \@items; |
2836 | |
2757 | |
|
|
2758 | my $row = 0; |
2837 | for my $item (@items) { |
2759 | for my $item (@items) { |
2838 | $item->{item} = $item; |
2760 | CFClient::Item::update_widgets $item; |
2839 | $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; |
|
|
2840 | $item->update_item (); |
|
|
2841 | } |
|
|
2842 | |
2761 | |
2843 | my $i = 0; |
2762 | $self->{scrolled}->add (0, $row, $item->{face_widget}); |
2844 | for (@items) { |
2763 | $self->{scrolled}->add (1, $row, $item->{desc_widget}); |
2845 | $self->{scrolled}->add (0, $i, $_); |
2764 | $self->{scrolled}->add (2, $row, $item->{weight_widget}); |
2846 | my $nrof = $_->{item}->{nrof} || 1; |
|
|
2847 | $self->{scrolled}->add (1, $i++, new CFClient::UI::Label text => ($_->{item}->{weight} * $nrof) / 1000); |
|
|
2848 | } |
|
|
2849 | |
2765 | |
2850 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2766 | $row++; |
|
|
2767 | } |
2851 | } |
2768 | } |
2852 | |
2769 | |
2853 | sub size_request { |
2770 | sub size_request { |
2854 | my ($self) = @_; |
2771 | my ($self) = @_; |
2855 | ($self->{req_w}, $self->{req_h}); |
2772 | ($self->{req_w}, $self->{req_h}); |