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