1 |
root |
1.1 |
package CFPlus::Item; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
root |
1.3 |
use utf8; |
5 |
|
|
|
6 |
root |
1.1 |
use Crossfire::Protocol::Constants; |
7 |
|
|
|
8 |
|
|
my $last_enter_count = 1; |
9 |
|
|
|
10 |
|
|
sub desc_string { |
11 |
|
|
my ($self) = @_; |
12 |
|
|
|
13 |
|
|
my $desc = |
14 |
|
|
$self->{nrof} < 2 |
15 |
|
|
? $self->{name} |
16 |
|
|
: "$self->{nrof} × $self->{name_pl}"; |
17 |
|
|
|
18 |
|
|
$self->{flags} & F_OPEN |
19 |
|
|
and $desc .= " (open)"; |
20 |
|
|
$self->{flags} & F_APPLIED |
21 |
|
|
and $desc .= " (applied)"; |
22 |
|
|
$self->{flags} & F_UNPAID |
23 |
|
|
and $desc .= " (unpaid)"; |
24 |
|
|
$self->{flags} & F_MAGIC |
25 |
|
|
and $desc .= " (magic)"; |
26 |
|
|
$self->{flags} & F_CURSED |
27 |
|
|
and $desc .= " (cursed)"; |
28 |
|
|
$self->{flags} & F_DAMNED |
29 |
|
|
and $desc .= " (damned)"; |
30 |
|
|
$self->{flags} & F_LOCKED |
31 |
|
|
and $desc .= " *"; |
32 |
|
|
|
33 |
|
|
$desc |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
sub weight_string { |
37 |
|
|
my ($self) = @_; |
38 |
|
|
|
39 |
|
|
my $weight = ($self->{nrof} || 1) * $self->{weight}; |
40 |
|
|
|
41 |
|
|
$weight < 0 ? "?" : $weight * 0.001 |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
sub do_n_dialog { |
45 |
|
|
my ($cb) = @_; |
46 |
|
|
|
47 |
|
|
my $w = new CFPlus::UI::Toplevel |
48 |
|
|
on_delete => sub { $_[0]->destroy; 1 }, |
49 |
|
|
has_close_button => 1, |
50 |
|
|
; |
51 |
|
|
|
52 |
|
|
$w->add (my $vb = new CFPlus::UI::VBox x => "center", y => "center"); |
53 |
|
|
$vb->add (new CFPlus::UI::Label text => "Enter item count:"); |
54 |
|
|
$vb->add (my $entry = new CFPlus::UI::Entry |
55 |
|
|
text => $last_enter_count, |
56 |
|
|
on_activate => sub { |
57 |
|
|
my ($entry) = @_; |
58 |
|
|
$last_enter_count = $entry->get_text; |
59 |
|
|
$cb->($last_enter_count); |
60 |
|
|
$w->hide; |
61 |
|
|
$w->destroy; |
62 |
|
|
|
63 |
|
|
0 |
64 |
|
|
}, |
65 |
|
|
on_escape => sub { $w->destroy; 1 }, |
66 |
|
|
); |
67 |
|
|
$entry->grab_focus; |
68 |
|
|
$w->show; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
sub update_widgets { |
72 |
|
|
my ($self) = @_; |
73 |
|
|
|
74 |
|
|
# necessary to avoid cyclic references |
75 |
root |
1.2 |
CFPlus::weaken $self; |
76 |
root |
1.1 |
|
77 |
|
|
my $button_cb = sub { |
78 |
|
|
my (undef, $ev, $x, $y) = @_; |
79 |
|
|
|
80 |
|
|
my $targ = $::CONN->{player}{tag}; |
81 |
|
|
|
82 |
|
|
if ($self->{container} == $::CONN->{player}{tag}) { |
83 |
|
|
$targ = $::CONN->{open_container}; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
if (($ev->{mod} & CFPlus::KMOD_SHIFT) && $ev->{button} == 1) { |
87 |
|
|
$::CONN->send ("move $targ $self->{tag} 0") |
88 |
|
|
if $targ || !($self->{flags} & F_LOCKED); |
89 |
|
|
} elsif (($ev->{mod} & CFPlus::KMOD_SHIFT) && $ev->{button} == 2) { |
90 |
|
|
$self->{flags} & F_LOCKED |
91 |
|
|
? $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) |
92 |
|
|
: $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) |
93 |
|
|
} elsif ($ev->{button} == 1) { |
94 |
|
|
$::CONN->send ("examine $self->{tag}"); |
95 |
|
|
} elsif ($ev->{button} == 2) { |
96 |
|
|
$::CONN->send ("apply $self->{tag}"); |
97 |
|
|
} elsif ($ev->{button} == 3) { |
98 |
|
|
my $move_prefix = $::CONN->{open_container} ? 'put' : 'drop'; |
99 |
|
|
if ($self->{container} == $::CONN->{open_container}) { |
100 |
|
|
$move_prefix = "take"; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
my $shortname = CFPlus::shorten $self->{name}, 14; |
104 |
|
|
|
105 |
|
|
my @menu_items = ( |
106 |
|
|
["examine", sub { $::CONN->send ("examine $self->{tag}") }], |
107 |
|
|
["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }], |
108 |
|
|
["ignite/thaw", # first try of an easier use of flint&steel |
109 |
|
|
sub { |
110 |
|
|
$::CONN->send ("mark ". pack "N", $self->{tag}); |
111 |
|
|
$::CONN->send ("command apply flint and steel"); |
112 |
|
|
} |
113 |
|
|
], |
114 |
|
|
["inscribe", # first try of an easier use of flint&steel |
115 |
|
|
sub { |
116 |
|
|
&::open_string_query ("Text to inscribe", sub { |
117 |
|
|
my ($entry, $txt) = @_; |
118 |
|
|
$::CONN->send ("mark ". pack "N", $self->{tag}); |
119 |
|
|
$::CONN->send ("command use_skill inscription $txt"); |
120 |
|
|
}); |
121 |
|
|
} |
122 |
|
|
], |
123 |
|
|
["rename", # first try of an easier use of flint&steel |
124 |
|
|
sub { |
125 |
|
|
&::open_string_query ("Rename item to:", sub { |
126 |
|
|
my ($entry, $txt) = @_; |
127 |
|
|
$::CONN->send ("mark ". pack "N", $self->{tag}); |
128 |
|
|
$::CONN->send ("command rename to <$txt>"); |
129 |
|
|
}, $self->{name}, |
130 |
|
|
"If you input no name or erase the current custom name, the custom name will be unset"); |
131 |
|
|
} |
132 |
|
|
], |
133 |
|
|
["apply", sub { $::CONN->send ("apply $self->{tag}") }], |
134 |
|
|
( |
135 |
|
|
$self->{flags} & F_LOCKED |
136 |
|
|
? ( |
137 |
|
|
["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }], |
138 |
|
|
) |
139 |
|
|
: ( |
140 |
|
|
["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }], |
141 |
|
|
["$move_prefix all", sub { $::CONN->send ("move $targ $self->{tag} 0") }], |
142 |
|
|
["$move_prefix <n>", |
143 |
|
|
sub { |
144 |
|
|
do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") }) |
145 |
|
|
} |
146 |
|
|
] |
147 |
|
|
) |
148 |
|
|
), |
149 |
|
|
["bind <i>apply $shortname</i> to a key" => sub { CFPlus::Macro::quick_macro ["apply $self->{name}"] }], |
150 |
|
|
); |
151 |
|
|
|
152 |
|
|
CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
1 |
156 |
|
|
}; |
157 |
|
|
|
158 |
root |
1.4 |
my $tooltip_std = |
159 |
|
|
"<small>" |
160 |
|
|
. "Left click - examine item\n" |
161 |
|
|
. "Shift-Left click - " . ($self->{container} ? "move or drop" : "take") . " item\n" |
162 |
|
|
. "Middle click - apply\n" |
163 |
|
|
. "Shift-Middle click - lock/unlock\n" |
164 |
|
|
. "Right click - further options" |
165 |
|
|
. "</small>\n"; |
166 |
root |
1.1 |
|
167 |
|
|
my $bg = $self->{flags} & F_CURSED ? [1 , 0 , 0, 0.5] |
168 |
|
|
: $self->{flags} & F_MAGIC ? [0.2, 0.2, 1, 0.5] |
169 |
|
|
: undef; |
170 |
|
|
|
171 |
|
|
$self->{face_widget} ||= new CFPlus::UI::Face |
172 |
|
|
can_events => 1, |
173 |
|
|
can_hover => 1, |
174 |
|
|
anim => $self->{anim}, |
175 |
|
|
animspeed => $self->{animspeed}, # TODO# must be set at creation time |
176 |
|
|
on_button_down => $button_cb, |
177 |
|
|
; |
178 |
|
|
$self->{face_widget}{bg} = $bg; |
179 |
|
|
$self->{face_widget}{face} = $self->{face}; |
180 |
|
|
$self->{face_widget}{anim} = $self->{anim}; |
181 |
|
|
$self->{face_widget}{animspeed} = $self->{animspeed}; |
182 |
|
|
$self->{face_widget}->set_tooltip ( |
183 |
|
|
"<b>Face/Animation.</b>\n" |
184 |
|
|
. "Item uses face #$self->{face}. " |
185 |
|
|
. ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ") |
186 |
|
|
. "\n\n$tooltip_std" |
187 |
|
|
); |
188 |
|
|
|
189 |
|
|
$self->{desc_widget} ||= new CFPlus::UI::Label |
190 |
|
|
can_events => 1, |
191 |
|
|
can_hover => 1, |
192 |
|
|
ellipsise => 2, |
193 |
|
|
align => -1, |
194 |
root |
1.4 |
|
195 |
|
|
on_button_down => $button_cb, |
196 |
|
|
on_tooltip_show => sub { |
197 |
|
|
my ($widget) = @_; |
198 |
|
|
|
199 |
|
|
$::CONN->ex ($self->{tag}, sub { |
200 |
|
|
my ($long_desc) = @_; |
201 |
|
|
|
202 |
|
|
$long_desc =~ s/\s+$//; |
203 |
|
|
|
204 |
|
|
$self->{long_desc} = $long_desc; |
205 |
|
|
$widget->set_tooltip ("<b>$long_desc</b>\n\n$tooltip_std"); |
206 |
|
|
}); |
207 |
|
|
}, |
208 |
root |
1.1 |
; |
209 |
root |
1.4 |
|
210 |
root |
1.1 |
my $desc = CFPlus::Item::desc_string $self; |
211 |
|
|
$self->{desc_widget}{bg} = $bg; |
212 |
|
|
$self->{desc_widget}->set_text ($desc); |
213 |
root |
1.4 |
|
214 |
|
|
my $long_desc = $self->{long_desc} || "<b>$desc</b>."; |
215 |
|
|
$self->{desc_widget}->set_tooltip ("$long_desc\n$tooltip_std"); |
216 |
root |
1.1 |
|
217 |
|
|
$self->{weight_widget} ||= new CFPlus::UI::Label |
218 |
|
|
can_events => 1, |
219 |
|
|
can_hover => 1, |
220 |
|
|
ellipsise => 0, |
221 |
|
|
align => 0, |
222 |
|
|
on_button_down => $button_cb, |
223 |
|
|
; |
224 |
|
|
$self->{weight_widget}{bg} = $bg; |
225 |
|
|
$self->{weight_widget}->set_text (CFPlus::Item::weight_string $self); |
226 |
|
|
$self->{weight_widget}->set_tooltip ( |
227 |
|
|
"<b>Weight</b>.\n" |
228 |
|
|
. ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ") |
229 |
|
|
. ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ") |
230 |
|
|
. "\n\n$tooltip_std" |
231 |
|
|
); |
232 |
|
|
} |