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