1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | CFClient - undocumented utility garbage for our crossfire client |
3 | CFPlus - undocumented utility garbage for our crossfire client |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use CFClient; |
7 | use CFPlus; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
10 | |
10 | |
11 | =over 4 |
11 | =over 4 |
12 | |
12 | |
13 | =cut |
13 | =cut |
14 | |
14 | |
15 | package CFClient; |
15 | package CFPlus; |
|
|
16 | |
|
|
17 | use Carp (); |
16 | |
18 | |
17 | BEGIN { |
19 | BEGIN { |
18 | $VERSION = '0.1'; |
20 | $VERSION = '0.52'; |
19 | |
21 | |
20 | use XSLoader; |
22 | use XSLoader; |
21 | XSLoader::load "CFClient", $VERSION; |
23 | XSLoader::load "CFPlus", $VERSION; |
|
|
24 | } |
|
|
25 | |
|
|
26 | BEGIN { |
|
|
27 | $SIG{__DIE__} = sub { |
|
|
28 | return if CFPlus::in_destruct; |
|
|
29 | #CFPlus::fatal $_[0];#d# |
|
|
30 | CFPlus::error Carp::longmess $_[0];#d# |
|
|
31 | die;#d# |
|
|
32 | }; |
22 | } |
33 | } |
23 | |
34 | |
24 | use utf8; |
35 | use utf8; |
25 | |
36 | |
26 | use Carp (); |
|
|
27 | use AnyEvent (); |
37 | use AnyEvent (); |
28 | use BerkeleyDB; |
38 | use BerkeleyDB; |
29 | use Pod::POM; |
39 | use Pod::POM (); |
|
|
40 | use Scalar::Util (); |
|
|
41 | use Storable (); # finally |
|
|
42 | |
|
|
43 | =item guard { BLOCK } |
|
|
44 | |
|
|
45 | Returns an object that executes the given block as soon as it is destroyed. |
|
|
46 | |
|
|
47 | =cut |
|
|
48 | |
|
|
49 | sub guard(&) { |
|
|
50 | bless \(my $cb = $_[0]), "CFPlus::Guard" |
|
|
51 | } |
|
|
52 | |
|
|
53 | sub CFPlus::Guard::DESTROY { |
|
|
54 | ${$_[0]}->() |
|
|
55 | } |
|
|
56 | |
|
|
57 | sub asxml($) { |
|
|
58 | local $_ = $_[0]; |
|
|
59 | |
|
|
60 | s/&/&/g; |
|
|
61 | s/>/>/g; |
|
|
62 | s/</</g; |
|
|
63 | |
|
|
64 | $_ |
|
|
65 | } |
|
|
66 | |
|
|
67 | sub socketpipe() { |
|
|
68 | socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC |
|
|
69 | or die "cannot establish bidiretcional pipe: $!\n"; |
|
|
70 | |
|
|
71 | ($fh1, $fh2) |
|
|
72 | } |
|
|
73 | |
|
|
74 | sub background(&) { |
|
|
75 | my ($cb) = @_; |
|
|
76 | |
|
|
77 | my ($fh_r, $fh_w) = CFPlus::socketpipe; |
|
|
78 | |
|
|
79 | my $pid = fork; |
|
|
80 | |
|
|
81 | if (defined $pid && !$pid) { |
|
|
82 | $SIG{__DIE__} = sub { |
|
|
83 | my $msg = $_[0]; |
|
|
84 | $msg =~ s/\n+/\n/; |
|
|
85 | warn "FATAL: $msg"; |
|
|
86 | CFPlus::_exit 99; |
|
|
87 | }; |
|
|
88 | |
|
|
89 | open STDOUT, ">&", $fh_w; |
|
|
90 | open STDERR, ">&", $fh_w; |
|
|
91 | close $fh_r; |
|
|
92 | close $fh_w; |
|
|
93 | |
|
|
94 | $| = 1; |
|
|
95 | |
|
|
96 | $cb->(); |
|
|
97 | |
|
|
98 | # win32 is fucked up, of course. exit will clean stuff up, |
|
|
99 | # which destroys our database etc. _exit will exit ALL |
|
|
100 | # forked processes, because of the dreaded fork emulation. |
|
|
101 | CFPlus::_exit 0; |
|
|
102 | } |
|
|
103 | |
|
|
104 | close $fh_w; |
|
|
105 | |
|
|
106 | my $buffer; |
|
|
107 | |
|
|
108 | Event->io (fd => $fh_r, poll => 'r', cb => sub { |
|
|
109 | unless (sysread $fh_r, $buffer, 4096, length $buffer) { |
|
|
110 | $_[0]->w->cancel; |
|
|
111 | $buffer .= "done\n"; |
|
|
112 | } |
|
|
113 | |
|
|
114 | while ($buffer =~ s/^(.*)\n//) { |
|
|
115 | my $line = $1; |
|
|
116 | utf8::decode $line; |
|
|
117 | ::message ({ |
|
|
118 | markup => "editor($pid): " . CFPlus::asxml $line, |
|
|
119 | }); |
|
|
120 | } |
|
|
121 | }); |
|
|
122 | } |
|
|
123 | |
|
|
124 | package CFPlus::Database; |
|
|
125 | |
|
|
126 | our @ISA = BerkeleyDB::Btree::; |
|
|
127 | |
|
|
128 | sub get($$) { |
|
|
129 | my $data; |
|
|
130 | |
|
|
131 | $_[0]->db_get ($_[1], $data) == 0 |
|
|
132 | ? $data |
|
|
133 | : () |
|
|
134 | } |
|
|
135 | |
|
|
136 | my %DB_SYNC; |
|
|
137 | |
|
|
138 | sub put($$$) { |
|
|
139 | my ($db, $key, $data) = @_; |
|
|
140 | |
|
|
141 | my $hkey = $db + 0; |
|
|
142 | Scalar::Util::weaken $db; |
|
|
143 | $DB_SYNC{$hkey} ||= AnyEvent->timer (after => 5, cb => sub { |
|
|
144 | delete $DB_SYNC{$hkey}; |
|
|
145 | $db->db_sync if $db; |
|
|
146 | }); |
|
|
147 | |
|
|
148 | $db->db_put ($key => $data) |
|
|
149 | } |
|
|
150 | |
|
|
151 | package CFPlus; |
30 | |
152 | |
31 | sub find_rcfile($) { |
153 | sub find_rcfile($) { |
32 | my $path; |
154 | my $path; |
33 | |
155 | |
34 | for (grep !ref, @INC) { |
156 | for (grep !ref, @INC) { |
35 | $path = "$_/CFClient/resources/$_[0]"; |
157 | $path = "$_/CFPlus/resources/$_[0]"; |
36 | return $path if -r $path; |
158 | return $path if -r $path; |
37 | } |
159 | } |
38 | |
160 | |
39 | die "FATAL: can't find required file $_[0]\n"; |
161 | die "FATAL: can't find required file $_[0]\n"; |
40 | } |
162 | } |
41 | |
163 | |
|
|
164 | BEGIN { |
|
|
165 | use Crossfire::Protocol::Base (); |
|
|
166 | *to_json = \&Crossfire::Protocol::Base::to_json; |
|
|
167 | *from_json = \&Crossfire::Protocol::Base::from_json; |
|
|
168 | } |
|
|
169 | |
42 | sub read_cfg { |
170 | sub read_cfg { |
43 | my ($file) = @_; |
171 | my ($file) = @_; |
44 | |
172 | |
45 | open CFG, $file |
173 | open my $fh, $file |
46 | or return; |
174 | or return; |
47 | |
175 | |
48 | my $CFG; |
|
|
49 | |
|
|
50 | local $/; |
176 | local $/; |
51 | $CFG = eval <CFG>; |
177 | my $CFG = <$fh>; |
52 | |
178 | |
53 | $::CFG = $CFG; |
179 | if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove |
54 | |
180 | require YAML; |
55 | close CFG; |
181 | utf8::decode $CFG; |
|
|
182 | $::CFG = YAML::Load ($CFG); |
|
|
183 | } elsif ($CFG =~ /^\{/) { |
|
|
184 | $::CFG = from_json $CFG; |
|
|
185 | } else { |
|
|
186 | $::CFG = eval $CFG; ## todo comaptibility cruft |
|
|
187 | } |
56 | } |
188 | } |
57 | |
189 | |
58 | sub write_cfg { |
190 | sub write_cfg { |
59 | my ($file) = @_; |
191 | my ($file) = @_; |
60 | |
192 | |
61 | open CFG, ">$file" |
193 | $::CFG->{VERSION} = $::VERSION; |
|
|
194 | |
|
|
195 | open my $fh, ">:utf8", $file |
62 | or return; |
196 | or return; |
|
|
197 | print $fh to_json $::CFG; |
|
|
198 | } |
63 | |
199 | |
64 | { |
200 | sub http_proxy { |
65 | require Data::Dumper; |
201 | my @proxy = win32_proxy_info; |
66 | local $Data::Dumper::Purity = 1; |
202 | |
67 | $::CFG->{VERSION} = $::VERSION; |
203 | if (@proxy) { |
68 | print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); |
204 | "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0] |
|
|
205 | } elsif (exists $ENV{http_proxy}) { |
|
|
206 | $ENV{http_proxy} |
|
|
207 | } else { |
|
|
208 | () |
69 | } |
209 | } |
70 | |
|
|
71 | close CFG; |
|
|
72 | } |
210 | } |
73 | |
211 | |
74 | sub load_pod($) { |
212 | sub set_proxy { |
75 | my $pod = do { |
213 | my $proxy = http_proxy |
76 | local $/; |
214 | or return; |
77 | open my $pod, "<:utf8", $_[0] |
|
|
78 | or die "$_[0]: $!"; |
|
|
79 | <$pod> |
|
|
80 | }; |
|
|
81 | |
215 | |
82 | Pod::POM->new->parse_text ($pod); |
216 | $ENV{http_proxy} = $proxy; |
83 | } |
217 | } |
84 | |
218 | |
85 | our $DB_ENV; |
219 | our $DB_ENV; |
|
|
220 | our $DB_STATE; |
|
|
221 | |
|
|
222 | sub db_table($) { |
|
|
223 | my ($table) = @_; |
|
|
224 | |
|
|
225 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
|
|
226 | |
|
|
227 | new CFPlus::Database |
|
|
228 | -Env => $DB_ENV, |
|
|
229 | -Filename => $table, |
|
|
230 | # -Filename => "database", |
|
|
231 | # -Subname => $table, |
|
|
232 | -Property => DB_CHKSUM, |
|
|
233 | -Flags => DB_CREATE | DB_UPGRADE, |
|
|
234 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
|
|
235 | } |
86 | |
236 | |
87 | { |
237 | { |
88 | use strict; |
238 | use strict; |
89 | |
239 | |
90 | mkdir "$Crossfire::VARDIR/cfplus", 0777; |
240 | mkdir "$Crossfire::VARDIR/cfplus", 0777; |
… | |
… | |
99 | # -ErrPrefix => "DATABASE", |
249 | # -ErrPrefix => "DATABASE", |
100 | -Verbose => 1, |
250 | -Verbose => 1, |
101 | -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, |
251 | -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, |
102 | -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, |
252 | -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, |
103 | or die "unable to create/open database home $Crossfire::VARDIR/cfplus: $BerkeleyDB::Error"; |
253 | or die "unable to create/open database home $Crossfire::VARDIR/cfplus: $BerkeleyDB::Error"; |
104 | } |
|
|
105 | |
254 | |
106 | sub db_table($) { |
255 | $DB_STATE = db_table "state"; |
107 | my ($table) = @_; |
|
|
108 | |
|
|
109 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
|
|
110 | |
|
|
111 | new CFClient::Database |
|
|
112 | -Env => $DB_ENV, |
|
|
113 | -Filename => $table, |
|
|
114 | # -Filename => "database", |
|
|
115 | # -Subname => $table, |
|
|
116 | -Property => DB_CHKSUM, |
|
|
117 | -Flags => DB_CREATE | DB_UPGRADE, |
|
|
118 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
|
|
119 | } |
256 | } |
120 | |
257 | |
121 | sub pod_to_pango($) { |
258 | package CFPlus::Layout; |
122 | my ($pom) = @_; |
|
|
123 | |
259 | |
124 | $pom->present ("CFClient::PodToPango") |
260 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub { |
125 | } |
261 | reset_glyph_cache; |
126 | |
|
|
127 | sub pod_to_pango_list($) { |
|
|
128 | my ($pom) = @_; |
|
|
129 | |
|
|
130 | [ |
|
|
131 | map s/^(\s*)// && [40 * length $1, length $_ ? $_ : " "], |
|
|
132 | split /\n/, $pom->present ("CFClient::PodToPango") |
|
|
133 | ] |
|
|
134 | } |
|
|
135 | |
|
|
136 | package CFClient::PodToPango; |
|
|
137 | |
|
|
138 | use base Pod::POM::View::Text; |
|
|
139 | |
|
|
140 | our $indent = 0; |
|
|
141 | |
|
|
142 | *view_seq_code = |
|
|
143 | *view_seq_bold = sub { "<b>$_[1]</b>" }; |
|
|
144 | *view_seq_italic = sub { "<i>$_[1]</i>" }; |
|
|
145 | *view_seq_space = |
|
|
146 | *view_seq_link = |
|
|
147 | *view_seq_index = sub { CFClient::UI::Label::escape ($_[1]) }; |
|
|
148 | |
|
|
149 | sub view_seq_text { |
|
|
150 | my $text = $_[1]; |
|
|
151 | $text =~ s/\s+/ /g; |
|
|
152 | CFClient::UI::Label::escape ($text) |
|
|
153 | } |
|
|
154 | |
|
|
155 | sub view_item { |
|
|
156 | ("\t" x ($indent / 4)) |
|
|
157 | . $_[1]->title->present ($_[0]) |
|
|
158 | . "\n" |
|
|
159 | . $_[1]->content->present ($_[0]) |
|
|
160 | } |
|
|
161 | |
|
|
162 | sub view_verbatim { |
|
|
163 | (join "", |
|
|
164 | map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n", |
|
|
165 | split /\n/, CFClient::UI::Label::escape ($_[1])) |
|
|
166 | . "\n" |
|
|
167 | } |
|
|
168 | |
|
|
169 | sub view_textblock { |
|
|
170 | ("\t" x ($indent / 2)) . "$_[1]\n\n" |
|
|
171 | } |
|
|
172 | |
|
|
173 | sub view_head1 { |
|
|
174 | "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" |
|
|
175 | . $_[1]->content->present ($_[0]) |
|
|
176 | }; |
262 | }; |
177 | |
263 | |
178 | sub view_head2 { |
|
|
179 | "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" |
|
|
180 | . $_[1]->content->present ($_[0]) |
|
|
181 | }; |
|
|
182 | |
|
|
183 | sub view_head3 { |
|
|
184 | "\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" |
|
|
185 | . $_[1]->content->present ($_[0]) |
|
|
186 | }; |
|
|
187 | |
|
|
188 | sub view_over { |
|
|
189 | local $indent = $indent + $_[1]->indent; |
|
|
190 | $_[1]->content->present ($_[0]) |
|
|
191 | } |
|
|
192 | |
|
|
193 | package CFClient::Database; |
|
|
194 | |
|
|
195 | our @ISA = BerkeleyDB::Btree::; |
|
|
196 | |
|
|
197 | sub get($$) { |
|
|
198 | my $data; |
|
|
199 | |
|
|
200 | $_[0]->db_get ($_[1], $data) == 0 |
|
|
201 | ? $data |
|
|
202 | : () |
|
|
203 | } |
|
|
204 | |
|
|
205 | my %DB_SYNC; |
|
|
206 | |
|
|
207 | sub put($$$) { |
|
|
208 | my ($db, $key, $data) = @_; |
|
|
209 | |
|
|
210 | $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync }); |
|
|
211 | |
|
|
212 | $db->db_put ($key => $data) |
|
|
213 | } |
|
|
214 | |
|
|
215 | package CFClient::Item; |
264 | package CFPlus::Item; |
216 | |
265 | |
217 | use strict; |
266 | use strict; |
218 | use Crossfire::Protocol::Constants; |
267 | use Crossfire::Protocol::Constants; |
219 | |
268 | |
220 | my $last_enter_count = 1; |
269 | my $last_enter_count = 1; |
… | |
… | |
254 | } |
303 | } |
255 | |
304 | |
256 | sub do_n_dialog { |
305 | sub do_n_dialog { |
257 | my ($cb) = @_; |
306 | my ($cb) = @_; |
258 | |
307 | |
259 | my $w = new CFClient::UI::FancyFrame; |
308 | my $w = new CFPlus::UI::Toplevel |
|
|
309 | on_delete => sub { $_[0]->destroy; 1 }, |
|
|
310 | has_close_button => 1, |
|
|
311 | ; |
|
|
312 | |
260 | $w->add (my $vb = new CFClient::UI::VBox x => "center", y => "center"); |
313 | $w->add (my $vb = new CFPlus::UI::VBox x => "center", y => "center"); |
261 | $vb->add (new CFClient::UI::Label text => "Enter item count:"); |
314 | $vb->add (new CFPlus::UI::Label text => "Enter item count:"); |
262 | $vb->add (my $entry = new CFClient::UI::Entry |
315 | $vb->add (my $entry = new CFPlus::UI::Entry |
263 | text => $last_enter_count, |
316 | text => $last_enter_count, |
264 | on_activate => sub { |
317 | on_activate => sub { |
265 | my ($entry) = @_; |
318 | my ($entry) = @_; |
266 | $last_enter_count = $entry->get_text; |
319 | $last_enter_count = $entry->get_text; |
267 | $cb->($last_enter_count); |
320 | $cb->($last_enter_count); |
268 | $w->hide; |
321 | $w->hide; |
269 | $w = undef; |
322 | $w->destroy; |
|
|
323 | |
|
|
324 | 0 |
270 | } |
325 | }, |
|
|
326 | on_escape => sub { $w->destroy; 1 }, |
271 | ); |
327 | ); |
272 | $entry->focus_in; |
328 | $entry->grab_focus; |
273 | $w->show; |
329 | $w->show; |
274 | |
|
|
275 | } |
330 | } |
276 | |
331 | |
277 | sub update_widgets { |
332 | sub update_widgets { |
278 | my ($self) = @_; |
333 | my ($self) = @_; |
|
|
334 | |
|
|
335 | # necessary to avoid cyclic references |
|
|
336 | Scalar::Util::weaken $self; |
279 | |
337 | |
280 | my $button_cb = sub { |
338 | my $button_cb = sub { |
281 | my (undef, $ev, $x, $y) = @_; |
339 | my (undef, $ev, $x, $y) = @_; |
282 | |
340 | |
283 | my $targ = $::CONN->{player}{tag}; |
341 | my $targ = $::CONN->{player}{tag}; |
284 | |
342 | |
285 | if ($self->{container} == $::CONN->{player}{tag}) { |
343 | if ($self->{container} == $::CONN->{player}{tag}) { |
286 | $targ = $::CONN->{open_container}; |
344 | $targ = $::CONN->{open_container}; |
287 | } |
345 | } |
288 | |
346 | |
289 | if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) { |
347 | if (($ev->{mod} & CFPlus::KMOD_SHIFT) && $ev->{button} == 1) { |
290 | $::CONN->send ("move $targ $self->{tag} 0") |
348 | $::CONN->send ("move $targ $self->{tag} 0") |
291 | if $targ || !($self->{flags} & F_LOCKED); |
349 | if $targ || !($self->{flags} & F_LOCKED); |
292 | } elsif (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 2) { |
350 | } elsif (($ev->{mod} & CFPlus::KMOD_SHIFT) && $ev->{button} == 2) { |
293 | $self->{flags} & F_LOCKED |
351 | $self->{flags} & F_LOCKED |
294 | ? $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) |
352 | ? $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) |
295 | : $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) |
353 | : $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) |
296 | } elsif ($ev->{button} == 1) { |
354 | } elsif ($ev->{button} == 1) { |
297 | $::CONN->send ("examine $self->{tag}"); |
355 | $::CONN->send ("examine $self->{tag}"); |
298 | } elsif ($ev->{button} == 2) { |
356 | } elsif ($ev->{button} == 2) { |
299 | $::CONN->send ("apply $self->{tag}"); |
357 | $::CONN->send ("apply $self->{tag}"); |
300 | } elsif ($ev->{button} == 3) { |
358 | } elsif ($ev->{button} == 3) { |
|
|
359 | my $move_prefix = $::CONN->{open_container} ? 'put' : 'drop'; |
|
|
360 | if ($self->{container} == $::CONN->{open_container}) { |
|
|
361 | $move_prefix = "take"; |
|
|
362 | } |
|
|
363 | |
301 | my @menu_items = ( |
364 | my @menu_items = ( |
302 | ["examine", sub { $::CONN->send ("examine $self->{tag}") }], |
365 | ["examine", sub { $::CONN->send ("examine $self->{tag}") }], |
303 | ["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }], |
366 | ["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }], |
|
|
367 | ["ignite/thaw", # first try of an easier use of flint&steel |
|
|
368 | sub { |
|
|
369 | $::CONN->send ("mark ". pack "N", $self->{tag}); |
|
|
370 | $::CONN->send ("command apply flint and steel"); |
|
|
371 | } |
|
|
372 | ], |
|
|
373 | ["inscribe", # first try of an easier use of flint&steel |
|
|
374 | sub { |
|
|
375 | &::open_string_query ("Text to inscribe", sub { |
|
|
376 | my ($entry, $txt) = @_; |
|
|
377 | $::CONN->send ("mark ". pack "N", $self->{tag}); |
|
|
378 | $::CONN->send ("command use_skill inscription $txt"); |
|
|
379 | }); |
|
|
380 | } |
|
|
381 | ], |
|
|
382 | ["rename", # first try of an easier use of flint&steel |
|
|
383 | sub { |
|
|
384 | &::open_string_query ("Rename item to:", sub { |
|
|
385 | my ($entry, $txt) = @_; |
|
|
386 | $::CONN->send ("mark ". pack "N", $self->{tag}); |
|
|
387 | $::CONN->send ("command rename to <$txt>"); |
|
|
388 | }, $self->{name}, |
|
|
389 | "If you input no name or erase the current custom name, the custom name will be unset"); |
|
|
390 | } |
|
|
391 | ], |
304 | ["apply", sub { $::CONN->send ("apply $self->{tag}") }], |
392 | ["apply", sub { $::CONN->send ("apply $self->{tag}") }], |
305 | ( |
393 | ( |
306 | $self->{flags} & F_LOCKED |
394 | $self->{flags} & F_LOCKED |
307 | ? ( |
395 | ? ( |
308 | ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }], |
396 | ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }], |
309 | ) |
397 | ) |
310 | : ( |
398 | : ( |
311 | ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }], |
399 | ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }], |
312 | ["drop", sub { $::CONN->send ("move $::CONN->{open_container} $self->{tag} 0") }], |
400 | ["$move_prefix all", sub { $::CONN->send ("move $targ $self->{tag} 0") }], |
313 | ["move n", |
401 | ["$move_prefix <n>", |
314 | sub { |
402 | sub { |
315 | do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") }) |
403 | do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") }) |
316 | } |
404 | } |
317 | ] |
405 | ] |
318 | ) |
406 | ) |
319 | ), |
407 | ), |
320 | ); |
408 | ); |
321 | |
409 | |
322 | CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); |
410 | CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); |
323 | } |
411 | } |
324 | |
412 | |
325 | 1 |
413 | 1 |
326 | }; |
414 | }; |
327 | |
415 | |
… | |
… | |
331 | . "Middle click - apply\n" |
419 | . "Middle click - apply\n" |
332 | . "Shift-Middle click - lock/unlock\n" |
420 | . "Shift-Middle click - lock/unlock\n" |
333 | . "Right click - further options" |
421 | . "Right click - further options" |
334 | . "</small>\n"; |
422 | . "</small>\n"; |
335 | |
423 | |
|
|
424 | my $bg = $self->{flags} & F_CURSED ? [1 , 0 , 0, 0.5] |
|
|
425 | : $self->{flags} & F_MAGIC ? [0.2, 0.2, 1, 0.5] |
|
|
426 | : undef; |
|
|
427 | |
336 | $self->{face_widget} ||= new CFClient::UI::Face |
428 | $self->{face_widget} ||= new CFPlus::UI::Face |
337 | can_events => 1, |
429 | can_events => 1, |
338 | can_hover => 1, |
430 | can_hover => 1, |
339 | anim => $self->{anim}, |
431 | anim => $self->{anim}, |
340 | animspeed => $self->{animspeed}, # TODO# must be set at creation time |
432 | animspeed => $self->{animspeed}, # TODO# must be set at creation time |
341 | on_button_down => $button_cb, |
433 | on_button_down => $button_cb, |
342 | ; |
434 | ; |
|
|
435 | $self->{face_widget}{bg} = $bg; |
343 | $self->{face_widget}{face} = $self->{face}; |
436 | $self->{face_widget}{face} = $self->{face}; |
344 | $self->{face_widget}{anim} = $self->{anim}; |
437 | $self->{face_widget}{anim} = $self->{anim}; |
345 | $self->{face_widget}{animspeed} = $self->{animspeed}; |
438 | $self->{face_widget}{animspeed} = $self->{animspeed}; |
346 | $self->{face_widget}->set_tooltip ( |
439 | $self->{face_widget}->set_tooltip ( |
347 | "<b>Face/Animation.</b>\n" |
440 | "<b>Face/Animation.</b>\n" |
348 | . "Item uses face #$self->{face}. " |
441 | . "Item uses face #$self->{face}. " |
349 | . ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ") |
442 | . ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ") |
350 | . "\n\n$tooltip_std" |
443 | . "\n\n$tooltip_std" |
351 | ); |
444 | ); |
352 | |
445 | |
353 | $self->{desc_widget} ||= new CFClient::UI::Label |
446 | $self->{desc_widget} ||= new CFPlus::UI::Label |
354 | can_events => 1, |
447 | can_events => 1, |
355 | can_hover => 1, |
448 | can_hover => 1, |
356 | ellipsise => 2, |
449 | ellipsise => 2, |
357 | align => -1, |
450 | align => -1, |
358 | on_button_down => $button_cb, |
451 | on_button_down => $button_cb, |
359 | ; |
452 | ; |
360 | my $desc = CFClient::Item::desc_string $self; |
453 | my $desc = CFPlus::Item::desc_string $self; |
|
|
454 | $self->{desc_widget}{bg} = $bg; |
361 | $self->{desc_widget}->set_text ($desc); |
455 | $self->{desc_widget}->set_text ($desc); |
362 | $self->{desc_widget}->set_tooltip ("<b>$desc</b>.\n$tooltip_std"); |
456 | $self->{desc_widget}->set_tooltip ("<b>$desc</b>.\n$tooltip_std"); |
363 | |
457 | |
364 | $self->{weight_widget} ||= new CFClient::UI::Label |
458 | $self->{weight_widget} ||= new CFPlus::UI::Label |
365 | can_events => 1, |
459 | can_events => 1, |
366 | can_hover => 1, |
460 | can_hover => 1, |
367 | ellipsise => 0, |
461 | ellipsise => 0, |
368 | align => 0, |
462 | align => 0, |
369 | on_button_down => $button_cb, |
463 | on_button_down => $button_cb, |
370 | ; |
464 | ; |
|
|
465 | $self->{weight_widget}{bg} = $bg; |
371 | $self->{weight_widget}->set_text (CFClient::Item::weight_string $self); |
466 | $self->{weight_widget}->set_text (CFPlus::Item::weight_string $self); |
372 | |
|
|
373 | $self->{weight_widget}->set_tooltip ( |
467 | $self->{weight_widget}->set_tooltip ( |
374 | "<b>Weight</b>.\n" |
468 | "<b>Weight</b>.\n" |
375 | . ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ") |
469 | . ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ") |
376 | . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ") |
470 | . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ") |
377 | . "\n\n$tooltip_std" |
471 | . "\n\n$tooltip_std" |
378 | ); |
472 | ); |
379 | } |
473 | } |
380 | |
474 | |
381 | package CFClient::Binder; |
|
|
382 | |
|
|
383 | my @ALLOWED_MODIFIER_KEYS = ( |
|
|
384 | CFClient::SDLK_LSHIFT, |
|
|
385 | CFClient::SDLK_LCTRL , |
|
|
386 | CFClient::SDLK_LALT , |
|
|
387 | CFClient::SDLK_LMETA , |
|
|
388 | |
|
|
389 | CFClient::SDLK_RSHIFT, |
|
|
390 | CFClient::SDLK_RCTRL , |
|
|
391 | CFClient::SDLK_RALT , |
|
|
392 | CFClient::SDLK_RMETA , |
|
|
393 | ); |
|
|
394 | |
|
|
395 | my %ALLOWED_MODIFIERS = ( |
|
|
396 | CFClient::KMOD_LSHIFT => "LSHIFT", |
|
|
397 | CFClient::KMOD_LCTRL => "LCTRL", |
|
|
398 | CFClient::KMOD_LALT => "LALT", |
|
|
399 | CFClient::KMOD_LMETA => "LMETA", |
|
|
400 | |
|
|
401 | CFClient::KMOD_RSHIFT => "RSHIFT", |
|
|
402 | CFClient::KMOD_RCTRL => "RCTRL", |
|
|
403 | CFClient::KMOD_RALT => "RALT", |
|
|
404 | CFClient::KMOD_RMETA => "RMETA", |
|
|
405 | ); |
|
|
406 | |
|
|
407 | my %DIRECT_BIND_CHARS = map { $_ => 1 } qw/0 1 2 3 4 5 6 7 8 9/; |
|
|
408 | my @DIRECT_BIND_KEYS = ( |
|
|
409 | CFClient::SDLK_F1, |
|
|
410 | CFClient::SDLK_F2, |
|
|
411 | CFClient::SDLK_F3, |
|
|
412 | CFClient::SDLK_F4, |
|
|
413 | CFClient::SDLK_F5, |
|
|
414 | CFClient::SDLK_F6, |
|
|
415 | CFClient::SDLK_F7, |
|
|
416 | CFClient::SDLK_F8, |
|
|
417 | CFClient::SDLK_F9, |
|
|
418 | CFClient::SDLK_F10, |
|
|
419 | CFClient::SDLK_F11, |
|
|
420 | CFClient::SDLK_F12, |
|
|
421 | CFClient::SDLK_F13, |
|
|
422 | CFClient::SDLK_F14, |
|
|
423 | CFClient::SDLK_F15, |
|
|
424 | ); |
|
|
425 | |
|
|
426 | # this binding dialog asks for a key-combo to be pressed |
|
|
427 | # and if successful it calls the $cb with $mod and $sym as args. |
|
|
428 | sub open_binding_dialog { |
|
|
429 | my ($cb) = @_; |
|
|
430 | |
|
|
431 | my $w = new CFClient::UI::FancyFrame |
|
|
432 | title => "Bind Action", |
|
|
433 | x => "center", |
|
|
434 | y => "center"; |
|
|
435 | |
|
|
436 | $w->add (my $vb = new CFClient::UI::VBox); |
|
|
437 | $vb->add (new CFClient::UI::Label |
|
|
438 | text => "Press a modifier (CTRL, ALT and/or SHIFT) and a key." |
|
|
439 | ."You can only bind 0-9 and F1-F15 without modifiers." |
|
|
440 | ); |
|
|
441 | $vb->add (my $entry = new CFClient::UI::Entry |
|
|
442 | text => "", |
|
|
443 | on_key_down => sub { |
|
|
444 | my ($entry, $ev) = @_; |
|
|
445 | |
|
|
446 | my $mod = $ev->{mod}; |
|
|
447 | my $sym = $ev->{sym}; |
|
|
448 | |
|
|
449 | # XXX: This seems a little bit hackisch to me, but i have to ignore them |
|
|
450 | if (grep { $_ == $sym } @ALLOWED_MODIFIER_KEYS) { |
|
|
451 | return; |
|
|
452 | } |
|
|
453 | |
|
|
454 | if ($mod == CFClient::KMOD_NONE |
|
|
455 | and not $DIRECT_BIND_CHARS{chr ($ev->{unicode})} |
|
|
456 | and not grep { $sym == $_ } @DIRECT_BIND_KEYS) |
|
|
457 | { |
|
|
458 | $::STATUSBOX->add ( |
|
|
459 | "Can't bind key ".CFClient::SDL_GetKeyName ($sym) |
|
|
460 | ." directly without modifier! It would damage the completer handling." |
|
|
461 | ); |
|
|
462 | return; |
|
|
463 | } |
|
|
464 | |
|
|
465 | $entry->focus_out; |
|
|
466 | |
|
|
467 | $cb->($mod, $sym); |
|
|
468 | |
|
|
469 | $w->destroy |
|
|
470 | }); |
|
|
471 | |
|
|
472 | $entry->focus_in; |
|
|
473 | $w->show; |
|
|
474 | } |
|
|
475 | |
|
|
476 | sub keycombo_to_name { |
|
|
477 | my ($mod, $sym) = @_; |
|
|
478 | |
|
|
479 | my $mods = join '+', |
|
|
480 | map { $ALLOWED_MODIFIERS{$_} } |
|
|
481 | grep { ($_ + 0) & ($mod + 0) } |
|
|
482 | keys %ALLOWED_MODIFIERS; |
|
|
483 | $mods .= "+" if $mods ne ''; |
|
|
484 | |
|
|
485 | return $mods . CFClient::SDL_GetKeyName ($sym); |
|
|
486 | } |
|
|
487 | |
|
|
488 | package CFClient::Pickup; |
|
|
489 | # some pickup constants |
|
|
490 | sub PU_NOTHING { 0x00000000 } |
|
|
491 | |
|
|
492 | sub PU_DEBUG { 0x10000000 } |
|
|
493 | sub PU_INHIBIT { 0x20000000 } |
|
|
494 | sub PU_STOP { 0x40000000 } |
|
|
495 | sub PU_NEWMODE { 0x80000000 } |
|
|
496 | |
|
|
497 | sub PU_RATIO { 0x0000000F } |
|
|
498 | |
|
|
499 | sub PU_FOOD { 0x00000010 } |
|
|
500 | sub PU_DRINK { 0x00000020 } |
|
|
501 | sub PU_VALUABLES { 0x00000040 } |
|
|
502 | sub PU_BOW { 0x00000080 } |
|
|
503 | |
|
|
504 | sub PU_ARROW { 0x00000100 } |
|
|
505 | sub PU_HELMET { 0x00000200 } |
|
|
506 | sub PU_SHIELD { 0x00000400 } |
|
|
507 | sub PU_ARMOUR { 0x00000800 } |
|
|
508 | |
|
|
509 | sub PU_BOOTS { 0x00001000 } |
|
|
510 | sub PU_GLOVES { 0x00002000 } |
|
|
511 | sub PU_CLOAK { 0x00004000 } |
|
|
512 | sub PU_KEY { 0x00008000 } |
|
|
513 | |
|
|
514 | sub PU_MISSILEWEAPON { 0x00010000 } |
|
|
515 | sub PU_ALLWEAPON { 0x00020000 } |
|
|
516 | sub PU_MAGICAL { 0x00040000 } |
|
|
517 | sub PU_POTION { 0x00080000 } |
|
|
518 | |
|
|
519 | sub PU_SPELLBOOK { 0x00100000 } |
|
|
520 | sub PU_SKILLSCROLL { 0x00200000 } |
|
|
521 | sub PU_READABLES { 0x00400000 } |
|
|
522 | sub PU_MAGIC_DEVICE { 0x00800000 } |
|
|
523 | |
|
|
524 | sub PU_NOT_CURSED { 0x01000000 } |
|
|
525 | |
|
|
526 | sub PU_JEWELS { 0x02000000 } |
|
|
527 | |
|
|
528 | |
|
|
529 | 1; |
475 | 1; |
530 | |
476 | |
531 | =back |
477 | =back |
532 | |
478 | |
533 | =head1 AUTHOR |
479 | =head1 AUTHOR |