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.96 by root, Sun Jul 2 18:52:04 2006 UTC vs.
Revision 1.113 by root, Mon Aug 14 01:21:01 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 16
17BEGIN { 17BEGIN {
18 $VERSION = '0.1'; 18 $VERSION = '0.2';
19 19
20 use XSLoader; 20 use XSLoader;
21 XSLoader::load "CFClient", $VERSION; 21 XSLoader::load "CFPlus", $VERSION;
22} 22}
23 23
24use utf8; 24use utf8;
25 25
26use Carp (); 26use Carp ();
28use BerkeleyDB; 28use BerkeleyDB;
29use Pod::POM (); 29use Pod::POM ();
30use Scalar::Util (); 30use Scalar::Util ();
31use Storable (); # finally 31use Storable (); # finally
32 32
33package CFClient::PodToPango; 33=item guard { BLOCK }
34 34
35use base Pod::POM::View::Text; 35Returns an object that executes the given block as soon as it is destroyed.
36 36
37our $VERSION = 1; # bump if resultant formatting changes 37=cut
38 38
39our $indent = 0; 39sub guard(&) {
40 40 bless \(my $cb = $_[0]), "CFPlus::Guard"
41*view_seq_code =
42*view_seq_bold = sub { "<b>$_[1]</b>" };
43*view_seq_italic = sub { "<i>$_[1]</i>" };
44*view_seq_space =
45*view_seq_link =
46*view_seq_index = sub { CFClient::UI::Label::escape ($_[1]) };
47
48sub view_seq_text {
49 my $text = $_[1];
50 $text =~ s/\s+/ /g;
51 CFClient::UI::Label::escape ($text)
52} 41}
53 42
54sub view_item { 43sub CFPlus::Guard::DESTROY {
55 ("\t" x ($indent / 4)) 44 ${$_[0]}->()
56 . $_[1]->title->present ($_[0])
57 . "\n\n"
58 . $_[1]->content->present ($_[0])
59} 45}
60 46
61sub view_verbatim { 47sub asxml($) {
62 (join "", 48 local $_ = $_[0];
63 map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n",
64 split /\n/, CFClient::UI::Label::escape ($_[1]))
65 . "\n"
66}
67 49
68sub view_textblock { 50 s/&/&amp;/g;
69 ("\t" x ($indent / 2)) . "$_[1]\n\n" 51 s/>/&gt;/g;
70} 52 s/</&lt;/g;
71 53
72sub view_head1 { 54 $_
73 "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
74 . $_[1]->content->present ($_[0])
75};
76
77sub view_head2 {
78 "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
79 . $_[1]->content->present ($_[0])
80};
81
82sub view_head3 {
83 "\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
84 . $_[1]->content->present ($_[0])
85};
86
87sub view_over {
88 local $indent = $indent + $_[1]->indent;
89 $_[1]->content->present ($_[0])
90} 55}
91 56
92package CFClient::Database; 57package CFPlus::Database;
93 58
94our @ISA = BerkeleyDB::Btree::; 59our @ISA = BerkeleyDB::Btree::;
95 60
96sub get($$) { 61sub get($$) {
97 my $data; 62 my $data;
109 $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync }); 74 $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync });
110 75
111 $db->db_put ($key => $data) 76 $db->db_put ($key => $data)
112} 77}
113 78
114package CFClient; 79package CFPlus;
115 80
116sub find_rcfile($) { 81sub find_rcfile($) {
117 my $path; 82 my $path;
118 83
119 for (grep !ref, @INC) { 84 for (grep !ref, @INC) {
120 $path = "$_/CFClient/resources/$_[0]"; 85 $path = "$_/CFPlus/resources/$_[0]";
121 return $path if -r $path; 86 return $path if -r $path;
122 } 87 }
123 88
124 die "FATAL: can't find required file $_[0]\n"; 89 die "FATAL: can't find required file $_[0]\n";
125} 90}
126 91
92BEGIN {
93 use Crossfire::Protocol::Base ();
94 *to_json = \&Crossfire::Protocol::Base::to_json;
95 *from_json = \&Crossfire::Protocol::Base::from_json;
96}
97
127sub read_cfg { 98sub read_cfg {
128 my ($file) = @_; 99 my ($file) = @_;
129 100
130 open CFG, $file 101 open my $fh, $file
131 or return; 102 or return;
132 103
133 my $CFG;
134
135 local $/; 104 local $/;
136 $CFG = eval <CFG>; 105 my $CFG = <$fh>;
137 106
138 $::CFG = $CFG; 107 if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove
139 108 require YAML;
140 close CFG; 109 utf8::decode $CFG;
110 $::CFG = YAML::Load ($CFG);
111 } elsif ($CFG =~ /^\{/) {
112 $::CFG = from_json $CFG;
113 } else {
114 $::CFG = eval $CFG; ## todo comaptibility cruft
115 }
141} 116}
142 117
143sub write_cfg { 118sub write_cfg {
144 my ($file) = @_; 119 my ($file) = @_;
145 120
146 open CFG, ">$file" 121 $::CFG->{VERSION} = $::VERSION;
122
123 open my $fh, ">:utf8", $file
147 or return; 124 or return;
148 125 print $fh to_json $::CFG;
149 {
150 require Data::Dumper;
151 local $Data::Dumper::Purity = 1;
152 $::CFG->{VERSION} = $::VERSION;
153 print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]);
154 }
155
156 close CFG;
157} 126}
158 127
159our $DB_ENV; 128our $DB_ENV;
160 129
161{ 130{
180sub db_table($) { 149sub db_table($) {
181 my ($table) = @_; 150 my ($table) = @_;
182 151
183 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; 152 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
184 153
185 new CFClient::Database 154 new CFPlus::Database
186 -Env => $DB_ENV, 155 -Env => $DB_ENV,
187 -Filename => $table, 156 -Filename => $table,
188# -Filename => "database", 157# -Filename => "database",
189# -Subname => $table, 158# -Subname => $table,
190 -Property => DB_CHKSUM, 159 -Property => DB_CHKSUM,
191 -Flags => DB_CREATE | DB_UPGRADE, 160 -Flags => DB_CREATE | DB_UPGRADE,
192 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" 161 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
193} 162}
194 163
195my $pod_cache = db_table "pod_cache"; 164package CFPlus::Layout;
196 165
197sub load_pod($$$$) { 166$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub {
198 my ($path, $filtertype, $filterversion, $filtercb) = @_; 167 reset_glyph_cache;
168};
199 169
200 stat $path
201 or die "$path: $!";
202
203 my $phash = join ",", $filterversion, $CFClient::PodToPango::VERSION, (stat _)[7,9];
204
205 my ($chash, $pom) = eval { @{ Storable::thaw $pod_cache->get ("$path/$filtertype") } };
206
207 return $pom if $chash eq $phash;
208
209 my $pod = do {
210 local $/;
211 open my $pod, "<:utf8", $_[0]
212 or die "$_[0]: $!";
213 <$pod>
214 };
215
216 #utf8::downgrade $pod;
217
218 $pom = $filtercb-> (Pod::POM->new->parse_text ($pod));
219
220 $pod_cache->put ("$path/$filtertype" => Storable::nfreeze [$phash, $pom]);
221
222 $pom
223}
224
225sub pod_to_pango($) {
226 my ($pom) = @_;
227
228 $pom->present ("CFClient::PodToPango")
229}
230
231sub pod_to_pango_list($) {
232 my ($pom) = @_;
233
234 [
235 map s/^(\s*)// && [40 * length $1, length $_ ? $_ : " "],
236 split /\n/, $pom->present ("CFClient::PodToPango")
237 ]
238}
239
240package CFClient::Item; 170package CFPlus::Item;
241 171
242use strict; 172use strict;
243use Crossfire::Protocol::Constants; 173use Crossfire::Protocol::Constants;
244 174
245my $last_enter_count = 1; 175my $last_enter_count = 1;
279} 209}
280 210
281sub do_n_dialog { 211sub do_n_dialog {
282 my ($cb) = @_; 212 my ($cb) = @_;
283 213
284 my $w = new CFClient::UI::FancyFrame; 214 my $w = new CFPlus::UI::Toplevel
215 on_delete => sub { $_[0]->destroy; 1 },
216 has_close_button => 1,
217 ;
218
285 $w->add (my $vb = new CFClient::UI::VBox x => "center", y => "center"); 219 $w->add (my $vb = new CFPlus::UI::VBox x => "center", y => "center");
286 $vb->add (new CFClient::UI::Label text => "Enter item count:"); 220 $vb->add (new CFPlus::UI::Label text => "Enter item count:");
287 $vb->add (my $entry = new CFClient::UI::Entry 221 $vb->add (my $entry = new CFPlus::UI::Entry
288 text => $last_enter_count, 222 text => $last_enter_count,
289 on_activate => sub { 223 on_activate => sub {
290 my ($entry) = @_; 224 my ($entry) = @_;
291 $last_enter_count = $entry->get_text; 225 $last_enter_count = $entry->get_text;
292 $cb->($last_enter_count); 226 $cb->($last_enter_count);
293 $w->hide; 227 $w->hide;
294 $w = undef; 228 $w->destroy;
229
230 0
295 } 231 },
232 on_escape => sub { $w->destroy; 1 },
296 ); 233 );
297 $entry->grab_focus; 234 $entry->grab_focus;
298 $w->show; 235 $w->show;
299
300} 236}
301 237
302sub update_widgets { 238sub update_widgets {
303 my ($self) = @_; 239 my ($self) = @_;
304 240
312 248
313 if ($self->{container} == $::CONN->{player}{tag}) { 249 if ($self->{container} == $::CONN->{player}{tag}) {
314 $targ = $::CONN->{open_container}; 250 $targ = $::CONN->{open_container};
315 } 251 }
316 252
317 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) { 253 if (($ev->{mod} & CFPlus::KMOD_SHIFT) && $ev->{button} == 1) {
318 $::CONN->send ("move $targ $self->{tag} 0") 254 $::CONN->send ("move $targ $self->{tag} 0")
319 if $targ || !($self->{flags} & F_LOCKED); 255 if $targ || !($self->{flags} & F_LOCKED);
320 } elsif (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 2) { 256 } elsif (($ev->{mod} & CFPlus::KMOD_SHIFT) && $ev->{button} == 2) {
321 $self->{flags} & F_LOCKED 257 $self->{flags} & F_LOCKED
322 ? $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) 258 ? $::CONN->send ("lock " . pack "CN", 0, $self->{tag})
323 : $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) 259 : $::CONN->send ("lock " . pack "CN", 1, $self->{tag})
324 } elsif ($ev->{button} == 1) { 260 } elsif ($ev->{button} == 1) {
325 $::CONN->send ("examine $self->{tag}"); 261 $::CONN->send ("examine $self->{tag}");
326 } elsif ($ev->{button} == 2) { 262 } elsif ($ev->{button} == 2) {
327 $::CONN->send ("apply $self->{tag}"); 263 $::CONN->send ("apply $self->{tag}");
328 } elsif ($ev->{button} == 3) { 264 } elsif ($ev->{button} == 3) {
265 my $move_prefix = $::CONN->{open_container} ? 'put' : 'drop';
266 if ($self->{container} == $::CONN->{open_container}) {
267 $move_prefix = "take";
268 }
269
329 my @menu_items = ( 270 my @menu_items = (
330 ["examine", sub { $::CONN->send ("examine $self->{tag}") }], 271 ["examine", sub { $::CONN->send ("examine $self->{tag}") }],
331 ["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }], 272 ["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }],
273 ["ignite/thaw", # first try of an easier use of flint&steel
274 sub {
275 $::CONN->send ("mark ". pack "N", $self->{tag});
276 $::CONN->send ("command apply flint and steel");
277 }
278 ],
279 ["inscribe", # first try of an easier use of flint&steel
280 sub {
281 &::open_string_query ("Text to inscribe", sub {
282 my ($entry, $txt) = @_;
283 $::CONN->send ("mark ". pack "N", $self->{tag});
284 $::CONN->send ("command use_skill inscription $txt");
285 });
286 }
287 ],
332 ["apply", sub { $::CONN->send ("apply $self->{tag}") }], 288 ["apply", sub { $::CONN->send ("apply $self->{tag}") }],
333 ( 289 (
334 $self->{flags} & F_LOCKED 290 $self->{flags} & F_LOCKED
335 ? ( 291 ? (
336 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }], 292 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }],
337 ) 293 )
338 : ( 294 : (
339 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }], 295 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }],
340 ["drop", sub { $::CONN->send ("move $::CONN->{open_container} $self->{tag} 0") }], 296 ["$move_prefix all", sub { $::CONN->send ("move $targ $self->{tag} 0") }],
341 ["move n", 297 ["$move_prefix &lt;n&gt;",
342 sub { 298 sub {
343 do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") }) 299 do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") })
344 } 300 }
345 ] 301 ]
346 ) 302 )
347 ), 303 ),
348 ); 304 );
349 305
350 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); 306 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev);
351 } 307 }
352 308
353 1 309 1
354 }; 310 };
355 311
359 . "Middle click - apply\n" 315 . "Middle click - apply\n"
360 . "Shift-Middle click - lock/unlock\n" 316 . "Shift-Middle click - lock/unlock\n"
361 . "Right click - further options" 317 . "Right click - further options"
362 . "</small>\n"; 318 . "</small>\n";
363 319
320 my $bg = $self->{flags} & F_CURSED ? [1 , 0 , 0, 0.5]
321 : $self->{flags} & F_MAGIC ? [0.2, 0.2, 1, 0.5]
322 : undef;
323
364 $self->{face_widget} ||= new CFClient::UI::Face 324 $self->{face_widget} ||= new CFPlus::UI::Face
365 can_events => 1, 325 can_events => 1,
366 can_hover => 1, 326 can_hover => 1,
367 anim => $self->{anim}, 327 anim => $self->{anim},
368 animspeed => $self->{animspeed}, # TODO# must be set at creation time 328 animspeed => $self->{animspeed}, # TODO# must be set at creation time
369 on_button_down => $button_cb, 329 on_button_down => $button_cb,
370 ; 330 ;
331 $self->{face_widget}{bg} = $bg;
371 $self->{face_widget}{face} = $self->{face}; 332 $self->{face_widget}{face} = $self->{face};
372 $self->{face_widget}{anim} = $self->{anim}; 333 $self->{face_widget}{anim} = $self->{anim};
373 $self->{face_widget}{animspeed} = $self->{animspeed}; 334 $self->{face_widget}{animspeed} = $self->{animspeed};
374 $self->{face_widget}->set_tooltip ( 335 $self->{face_widget}->set_tooltip (
375 "<b>Face/Animation.</b>\n" 336 "<b>Face/Animation.</b>\n"
376 . "Item uses face #$self->{face}. " 337 . "Item uses face #$self->{face}. "
377 . ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ") 338 . ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ")
378 . "\n\n$tooltip_std" 339 . "\n\n$tooltip_std"
379 ); 340 );
380 341
381 $self->{desc_widget} ||= new CFClient::UI::Label 342 $self->{desc_widget} ||= new CFPlus::UI::Label
382 can_events => 1, 343 can_events => 1,
383 can_hover => 1, 344 can_hover => 1,
384 ellipsise => 2, 345 ellipsise => 2,
385 align => -1, 346 align => -1,
386 on_button_down => $button_cb, 347 on_button_down => $button_cb,
387 ; 348 ;
388 my $desc = CFClient::Item::desc_string $self; 349 my $desc = CFPlus::Item::desc_string $self;
350 $self->{desc_widget}{bg} = $bg;
389 $self->{desc_widget}->set_text ($desc); 351 $self->{desc_widget}->set_text ($desc);
390 $self->{desc_widget}->set_tooltip ("<b>$desc</b>.\n$tooltip_std"); 352 $self->{desc_widget}->set_tooltip ("<b>$desc</b>.\n$tooltip_std");
391 353
392 $self->{weight_widget} ||= new CFClient::UI::Label 354 $self->{weight_widget} ||= new CFPlus::UI::Label
393 can_events => 1, 355 can_events => 1,
394 can_hover => 1, 356 can_hover => 1,
395 ellipsise => 0, 357 ellipsise => 0,
396 align => 0, 358 align => 0,
397 on_button_down => $button_cb, 359 on_button_down => $button_cb,
398 ; 360 ;
361 $self->{weight_widget}{bg} = $bg;
399 $self->{weight_widget}->set_text (CFClient::Item::weight_string $self); 362 $self->{weight_widget}->set_text (CFPlus::Item::weight_string $self);
400
401 $self->{weight_widget}->set_tooltip ( 363 $self->{weight_widget}->set_tooltip (
402 "<b>Weight</b>.\n" 364 "<b>Weight</b>.\n"
403 . ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ") 365 . ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ")
404 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ") 366 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ")
405 . "\n\n$tooltip_std" 367 . "\n\n$tooltip_std"

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines