1 |
package dc::Item; |
2 |
|
3 |
use strict; |
4 |
use utf8; |
5 |
use Encode; |
6 |
|
7 |
use Deliantra::Protocol::Constants; |
8 |
|
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 |
my $w = new dc::UI::Toplevel |
49 |
on_delete => sub { $_[0]->destroy; 1 }, |
50 |
has_close_button => 1, |
51 |
; |
52 |
|
53 |
$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 |
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 |
sub update_widgets { |
73 |
my ($self) = @_; |
74 |
|
75 |
# necessary to avoid cyclic references |
76 |
dc::weaken $self; |
77 |
|
78 |
my $button_cb = sub { |
79 |
my (undef, $ev, $x, $y) = @_; |
80 |
|
81 |
my $targ = $::CONN->{player}{tag}; |
82 |
|
83 |
if ($self->{container} == $::CONN->{player}{tag}) { |
84 |
$targ = $::CONN->{open_container}; |
85 |
} |
86 |
|
87 |
if (($ev->{mod} & dc::KMOD_SHIFT) && $ev->{button} == 1) { |
88 |
$::CONN->send ("move $targ $self->{tag} 0") |
89 |
if $targ || !($self->{flags} & F_LOCKED); |
90 |
} elsif (($ev->{mod} & dc::KMOD_SHIFT) && $ev->{button} == 2) { |
91 |
$self->{flags} & F_LOCKED |
92 |
? $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) |
93 |
: $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) |
94 |
} elsif ($ev->{button} == 1) { |
95 |
$::CONN->send ("examine $self->{tag}"); |
96 |
} elsif ($ev->{button} == 2) { |
97 |
$::CONN->send ("apply $self->{tag}"); |
98 |
} elsif ($ev->{button} == 3) { |
99 |
my $move_prefix = $::CONN->{open_container} ? 'put' : 'drop'; |
100 |
if ($self->{container} == $::CONN->{open_container}) { |
101 |
$move_prefix = "take"; |
102 |
} |
103 |
|
104 |
my $shortname = dc::shorten $self->{name}, 14; |
105 |
|
106 |
my @menu_items = ( |
107 |
["examine", sub { $::CONN->send ("examine $self->{tag}") }], |
108 |
["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }], |
109 |
["ignite/thaw", # first try of an easier use of flint&steel |
110 |
sub { |
111 |
$::CONN->send ("mark ". pack "N", $self->{tag}); |
112 |
$::CONN->send ("command apply flint and steel"); |
113 |
} |
114 |
], |
115 |
["inscribe", # first try of an easier use of flint&steel |
116 |
sub { |
117 |
&::open_string_query ("Text to inscribe", sub { |
118 |
my ($entry, $txt) = @_; |
119 |
$::CONN->send ("mark ". pack "N", $self->{tag}); |
120 |
$::CONN->send_utf8 ("command use_skill inscription $txt"); |
121 |
}); |
122 |
} |
123 |
], |
124 |
["rename", # first try of an easier use of flint&steel |
125 |
sub { |
126 |
&::open_string_query ("Rename item to:", sub { |
127 |
my ($entry, $txt) = @_; |
128 |
$::CONN->send ("mark ". pack "N", $self->{tag}); |
129 |
$::CONN->send_utf8 ("command rename to <$txt>"); |
130 |
}, $self->{name}, |
131 |
"If you input no name or erase the current custom name, the custom name will be unset"); |
132 |
} |
133 |
], |
134 |
["apply", sub { $::CONN->send ("apply $self->{tag}") }], |
135 |
( |
136 |
$self->{flags} & F_LOCKED |
137 |
? ( |
138 |
["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }], |
139 |
) |
140 |
: ( |
141 |
["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }], |
142 |
["$move_prefix all", sub { $::CONN->send ("move $targ $self->{tag} 0") }], |
143 |
["$move_prefix <n>", |
144 |
sub { |
145 |
do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") }) |
146 |
} |
147 |
] |
148 |
) |
149 |
), |
150 |
["bind <i>apply $shortname</i> to a key" => sub { dc::Macro::quick_macro ["apply $self->{name}"] }], |
151 |
); |
152 |
|
153 |
dc::UI::Menu->new (items => \@menu_items)->popup ($ev); |
154 |
} |
155 |
|
156 |
1 |
157 |
}; |
158 |
|
159 |
my $tooltip_std = |
160 |
"<small>" |
161 |
. "Left click - examine item\n" |
162 |
. "Shift-Left click - " . ($self->{container} ? "move or drop" : "take") . " item\n" |
163 |
. "Middle click - apply\n" |
164 |
. "Shift-Middle click - lock/unlock\n" |
165 |
. "Right click - further options" |
166 |
. "</small>\n"; |
167 |
|
168 |
my $bg = $self->{flags} & F_CURSED ? [1 , 0 , 0, 0.5] |
169 |
: $self->{flags} & F_MAGIC ? [0.2, 0.2, 1, 0.5] |
170 |
: undef; |
171 |
|
172 |
my $desc = dc::Item::desc_string $self; |
173 |
|
174 |
$self->{face_widget} ||= new dc::UI::Face |
175 |
can_events => 1, |
176 |
can_hover => 1, |
177 |
anim => $self->{anim}, |
178 |
animspeed => $self->{animspeed}, # TODO# must be set at creation time |
179 |
on_button_down => $button_cb, |
180 |
; |
181 |
$self->{face_widget}{bg} = $bg; |
182 |
$self->{face_widget}{face} = $self->{face}; |
183 |
$self->{face_widget}{anim} = $self->{anim}; |
184 |
$self->{face_widget}{animspeed} = $self->{animspeed}; |
185 |
# $self->{face_widget}->set_tooltip ( |
186 |
# "<b>Face/Animation.</b>\n" |
187 |
# . "Item uses face #$self->{face}. " |
188 |
# . ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ") |
189 |
# . "\n\n$tooltip_std" |
190 |
# ); |
191 |
$self->{face_widget}->set_tooltip ("<b>$desc</b>\n\n$tooltip_std"); |
192 |
|
193 |
$self->{desc_widget} ||= new dc::UI::Label |
194 |
can_events => 1, |
195 |
can_hover => 1, |
196 |
ellipsise => 2, |
197 |
align => -1, |
198 |
|
199 |
on_button_down => $button_cb, |
200 |
on_tooltip_show => sub { |
201 |
my ($widget) = @_; |
202 |
|
203 |
$::CONN && $::CONN->ex ($self->{tag}, sub { |
204 |
my ($long_desc) = @_; |
205 |
|
206 |
$long_desc =~ s/\s+$//; |
207 |
|
208 |
$self->{long_desc} = $long_desc; |
209 |
$widget->set_tooltip ("<b>$long_desc</b>\n\n$tooltip_std"); |
210 |
}); |
211 |
}, |
212 |
; |
213 |
|
214 |
$self->{desc_widget}{bg} = $bg; |
215 |
$self->{desc_widget}->set_text ($desc); |
216 |
|
217 |
my $long_desc = $self->{long_desc} || $desc; |
218 |
$self->{desc_widget}->set_tooltip ("<b>$long_desc</b>\n\n$tooltip_std"); |
219 |
|
220 |
$self->{weight_widget} ||= new dc::UI::Label |
221 |
can_events => 1, |
222 |
can_hover => 1, |
223 |
ellipsise => 0, |
224 |
align => 0, |
225 |
on_button_down => $button_cb, |
226 |
; |
227 |
$self->{weight_widget}{bg} = $bg; |
228 |
$self->{weight_widget}->set_text (dc::Item::weight_string $self); |
229 |
$self->{weight_widget}->set_tooltip ( |
230 |
"<b>Weight</b>.\n" |
231 |
. ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ") |
232 |
. ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ") |
233 |
. "\n\n$tooltip_std" |
234 |
); |
235 |
} |
236 |
|