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