ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC.pm (file contents):
Revision 1.83 by root, Wed May 31 07:40:33 2006 UTC vs.
Revision 1.135 by root, Thu Dec 7 15:57:13 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines