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.19 by root, Mon Apr 10 19:34:03 2006 UTC vs.
Revision 1.99 by elmex, Fri Jul 14 17:35:34 2006 UTC

1=head1 NAME 1=head1 NAME
2 2
3Crossfire::Client - undocumented utility garbage for our crossfire client 3CFClient - undocumented utility garbage for our crossfire client
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use Crossfire::Client; 7 use CFClient;
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 Crossfire::Client; 15package CFClient;
16 16
17BEGIN { 17BEGIN {
18 $VERSION = '0.1'; 18 $VERSION = '0.1';
19 19
20 use XSLoader; 20 use XSLoader;
21 XSLoader::load "Crossfire::Client", $VERSION; 21 XSLoader::load "CFClient", $VERSION;
22} 22}
23 23
24our %GL_EXT; 24use utf8;
25our $GL_VERSION;
26 25
27our $GL_NPOT; 26use Carp ();
27use AnyEvent ();
28use BerkeleyDB;
29use Pod::POM ();
30use Scalar::Util ();
31use Storable (); # finally
28 32
29sub gl_init { 33package CFClient::PodToPango;
30 $GL_VERSION = gl_version * 1;
31 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions;
32 34
33 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; 35use base Pod::POM::View::Text;
34 36
35 Crossfire::Client::Texture::restore_state (); 37our $VERSION = 1; # bump if resultant formatting changes
36 38
37# $GL_NPOT=0;#d# 39our $indent = 0;
40
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)
38} 52}
53
54sub view_item {
55 ("\t" x ($indent / 4))
56 . $_[1]->title->present ($_[0])
57 . "\n\n"
58 . $_[1]->content->present ($_[0])
59}
60
61sub view_verbatim {
62 (join "",
63 map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n",
64 split /\n/, CFClient::UI::Label::escape ($_[1]))
65 . "\n"
66}
67
68sub view_textblock {
69 ("\t" x ($indent / 2)) . "$_[1]\n\n"
70}
71
72sub view_head1 {
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}
91
92package CFClient::Database;
93
94our @ISA = BerkeleyDB::Btree::;
95
96sub get($$) {
97 my $data;
98
99 $_[0]->db_get ($_[1], $data) == 0
100 ? $data
101 : ()
102}
103
104my %DB_SYNC;
105
106sub put($$$) {
107 my ($db, $key, $data) = @_;
108
109 $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync });
110
111 $db->db_put ($key => $data)
112}
113
114package CFClient;
39 115
40sub find_rcfile($) { 116sub find_rcfile($) {
41 my $path; 117 my $path;
42 118
43 for (@INC) { 119 for (grep !ref, @INC) {
44 $path = "$_/Crossfire/resources/$_[0]"; 120 $path = "$_/CFClient/resources/$_[0]";
45 return $path if -r $path; 121 return $path if -r $path;
46 } 122 }
47 123
48 die "FATAL: can't find required file $_[0]\n"; 124 die "FATAL: can't find required file $_[0]\n";
49} 125}
78 } 154 }
79 155
80 close CFG; 156 close CFG;
81} 157}
82 158
83package Crossfire::Client::Texture; 159our $DB_ENV;
84 160
85use Scalar::Util; 161{
162 use strict;
86 163
87use SDL::OpenGL; 164 mkdir "$Crossfire::VARDIR/cfplus", 0777;
165 my $recover = $BerkeleyDB::db_version >= 4.4
166 ? eval "DB_REGISTER | DB_RECOVER"
167 : 0;
88 168
89my @textures; 169 $DB_ENV = new BerkeleyDB::Env
90 170 -Home => "$Crossfire::VARDIR/cfplus",
91sub new { 171 -Cachesize => 1_000_000,
92 my ($class, %data) = @_; 172 -ErrFile => "$Crossfire::VARDIR/cfplus/errorlog.txt",
93 173# -ErrPrefix => "DATABASE",
94 my $self = bless { 174 -Verbose => 1,
95 internalformat => GL_RGBA, 175 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover,
96 format => GL_RGBA, 176 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE,
97 %data, 177 or die "unable to create/open database home $Crossfire::VARDIR/cfplus: $BerkeleyDB::Error";
98 }, $class;
99
100 push @textures, $self;
101 Scalar::Util::weaken $textures[-1];
102
103 $self->upload;
104
105 $self
106} 178}
107 179
108sub new_from_image { 180sub db_table($) {
109 my ($class, $image) = @_; 181 my ($table) = @_;
110 182
111 $class->new (image => $image) 183 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
112}
113 184
114sub new_from_file { 185 new CFClient::Database
115 my ($class, $path) = @_; 186 -Env => $DB_ENV,
187 -Filename => $table,
188# -Filename => "database",
189# -Subname => $table,
190 -Property => DB_CHKSUM,
191 -Flags => DB_CREATE | DB_UPGRADE,
192 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
193}
116 194
117 open my $fh, "<:raw", $path 195my $pod_cache = db_table "pod_cache";
196
197sub load_pod($$$$) {
198 my ($path, $filtertype, $filterversion, $filtercb) = @_;
199
200 stat $path
118 or die "$path: $!"; 201 or die "$path: $!";
119 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 {
120 local $/; 210 local $/;
121 $class->new_from_image (<$fh>) 211 open my $pod, "<:utf8", $_[0]
122} 212 or die "$_[0]: $!";
213 <$pod>
214 };
123 215
124#sub new_from_surface { 216 #utf8::downgrade $pod;
125# my ($class, $surface) = @_;
126#
127# $surface->rgba;
128#
129# $class->new (
130# data => $surface->pixels,
131# width => $surface->width,
132# height => $surface->height,
133# )
134#}
135 217
136sub new_from_text { 218 $pom = $filtercb-> (Pod::POM->new->parse_text ($pod));
137 my ($class, $text, $height) = @_;
138 219
139 my ($w, $h, $data) = Crossfire::Client::font_render $text, $height; 220 $pod_cache->put ("$path/$filtertype" => Storable::nfreeze [$phash, $pom]);
140 221
141 $class->new ( 222 $pom
142 width => $w, 223}
143 height => $h, 224
144 data => $data, 225sub pod_to_pango($) {
145 internalformat => GL_ALPHA4, 226 my ($pom) = @_;
146 format => GL_ALPHA, 227
228 $pom->present ("CFClient::PodToPango")
229}
230
231sub pod_to_pango_list($) {
232 my ($pom) = @_;
233
147 ) 234 [
235 map s/^(\s*)// && [40 * length $1, length $_ ? $_ : " "],
236 split /\n/, $pom->present ("CFClient::PodToPango")
237 ]
148} 238}
149 239
150sub new_from_opengl { 240package CFClient::Layout;
151 my ($class, $w, $h, $cb) = @_;
152 241
153 $class->new (width => $w, height => $h, rendercb => $cb) 242$CFClient::OpenGL::SHUTDOWN_HOOK{"CFClient::Layout"} = sub {
154} 243 reset_glyph_cache;
244};
155 245
156sub topot { 246package CFClient::Item;
157 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
158}
159 247
160sub upload { 248use strict;
249use Crossfire::Protocol::Constants;
250
251my $last_enter_count = 1;
252
253sub desc_string {
161 my ($self) = @_; 254 my ($self) = @_;
162 255
163 return unless $SDL::App::USING_OPENGL; 256 my $desc =
257 $self->{nrof} < 2
258 ? $self->{name}
259 : "$self->{nrof} × $self->{name_pl}";
164 260
165 my $data; 261 $self->{flags} & F_OPEN
262 and $desc .= " (open)";
263 $self->{flags} & F_APPLIED
264 and $desc .= " (applied)";
265 $self->{flags} & F_UNPAID
266 and $desc .= " (unpaid)";
267 $self->{flags} & F_MAGIC
268 and $desc .= " (magic)";
269 $self->{flags} & F_CURSED
270 and $desc .= " (cursed)";
271 $self->{flags} & F_DAMNED
272 and $desc .= " (damned)";
273 $self->{flags} & F_LOCKED
274 and $desc .= " *";
166 275
167 if (exists $self->{data}) { 276 $desc
168 $data = $self->{data}; 277}
169 } elsif (exists $self->{rendercb}) {
170 glViewport 0, 0, $self->{width}, $self->{height};
171 glMatrixMode GL_PROJECTION;
172 glLoadIdentity;
173 glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000;
174 glMatrixMode GL_MODELVIEW;
175 glPushmatrix;
176 glLoadIdentity;
177 glClear GL_COLOR_BUFFER_BIT;
178 278
179 $self->{rendercb}->($self, $self->{width}, $self->{height}); 279sub weight_string {
180 } else { 280 my ($self) = @_;
181 my $pb = new Gtk2::Gdk::PixbufLoader;
182 $pb->write ($self->{image});
183 $pb->close;
184 281
185 $pb = $pb->get_pixbuf; 282 my $weight = ($self->{nrof} || 1) * $self->{weight};
186 $pb = $pb->add_alpha (0, 0, 0, 0);
187 283
188 $self->{width} = $pb->get_width; 284 $weight < 0 ? "?" : $weight * 0.001
189 $self->{height} = $pb->get_height; 285}
190 286
191 $data = $pb->get_pixels; 287sub do_n_dialog {
192 } 288 my ($cb) = @_;
193 289
194 my ($tw, $th) = @$self{qw(width height)}; 290 my $w = new CFClient::UI::FancyFrame;
195 291 $w->add (my $vb = new CFClient::UI::VBox x => "center", y => "center");
196 unless ($NPOT) { 292 $vb->add (new CFClient::UI::Label text => "Enter item count:");
197 $tw = topot $tw; 293 $vb->add (my $entry = new CFClient::UI::Entry
198 $th = topot $th; 294 text => $last_enter_count,
199 295 on_activate => sub {
200 if (defined $data) { 296 my ($entry) = @_;
201 $data = pack "(a$tw)*", (unpack "(a$self->{width})*", $data); 297 $last_enter_count = $entry->get_text;
202 $data .= ("\x00" x $tw) x ($th - $self->{height}); 298 $cb->($last_enter_count);
299 $w->hide;
300 $w = undef;
203 } 301 }
302 );
303 $entry->grab_focus;
304 $w->show;
305
306}
307
308sub update_widgets {
309 my ($self) = @_;
310
311 # necessary to avoid cyclic references
312 Scalar::Util::weaken $self;
313
314 my $button_cb = sub {
315 my (undef, $ev, $x, $y) = @_;
316
317 my $targ = $::CONN->{player}{tag};
318
319 if ($self->{container} == $::CONN->{player}{tag}) {
320 $targ = $::CONN->{open_container};
321 }
322
323 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
324 $::CONN->send ("move $targ $self->{tag} 0")
325 if $targ || !($self->{flags} & F_LOCKED);
326 } elsif (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 2) {
327 $self->{flags} & F_LOCKED
328 ? $::CONN->send ("lock " . pack "CN", 0, $self->{tag})
329 : $::CONN->send ("lock " . pack "CN", 1, $self->{tag})
330 } elsif ($ev->{button} == 1) {
331 $::CONN->send ("examine $self->{tag}");
332 } elsif ($ev->{button} == 2) {
333 $::CONN->send ("apply $self->{tag}");
334 } elsif ($ev->{button} == 3) {
335 my @menu_items = (
336 ["examine", sub { $::CONN->send ("examine $self->{tag}") }],
337 ["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }],
338 ["ignite/thaw", # first try of an easier use of flint&steel
339 sub {
340 $::CONN->send ("mark ". pack "N", $self->{tag});
341 $::CONN->send ("command apply flint and steel");
342 }
343 ],
344 ["apply", sub { $::CONN->send ("apply $self->{tag}") }],
345 (
346 $self->{flags} & F_LOCKED
347 ? (
348 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }],
349 )
350 : (
351 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }],
352 ["drop", sub { $::CONN->send ("move $::CONN->{open_container} $self->{tag} 0") }],
353 ["move n",
354 sub {
355 do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") })
356 }
357 ]
358 )
359 ),
360 );
361
362 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
363 }
364
365 1
204 } 366 };
205 367
206 $self->{u} = $self->{width} / $tw; 368 my $tooltip_std = "<small>"
207 $self->{v} = $self->{height} / $th; 369 . "Left click - examine item\n"
370 . "Shift-Left click - " . ($self->{container} ? "move or drop" : "take") . " item\n"
371 . "Middle click - apply\n"
372 . "Shift-Middle click - lock/unlock\n"
373 . "Right click - further options"
374 . "</small>\n";
208 375
209 $self->{name} ||= (glGenTextures 1)->[0]; 376 $self->{face_widget} ||= new CFClient::UI::Face
210 377 can_events => 1,
211 glBindTexture GL_TEXTURE_2D, $self->{name}; 378 can_hover => 1,
212 379 anim => $self->{anim},
213 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; 380 animspeed => $self->{animspeed}, # TODO# must be set at creation time
214 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR; 381 on_button_down => $button_cb,
215 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 382 ;
216 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 383 $self->{face_widget}{face} = $self->{face};
384 $self->{face_widget}{anim} = $self->{anim};
385 $self->{face_widget}{animspeed} = $self->{animspeed};
386 $self->{face_widget}->set_tooltip (
387 "<b>Face/Animation.</b>\n"
388 . "Item uses face #$self->{face}. "
389 . ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ")
390 . "\n\n$tooltip_std"
391 );
217 392
218 glGetError; 393 $self->{desc_widget} ||= new CFClient::UI::Label
219 if (defined $data) { 394 can_events => 1,
220 glTexImage2D GL_TEXTURE_2D, 0, 395 can_hover => 1,
221 $self->{internalformat}, 396 ellipsise => 2,
222 $tw, $th, # need to pad texture first 397 align => -1,
223 0, 398 on_button_down => $button_cb,
224 $self->{format},
225 GL_UNSIGNED_BYTE,
226 $data;
227 glGetError and die;
228 } else {
229 glCopyTexImage2D GL_TEXTURE_2D, 0,
230 $self->{internalformat},
231 0, 0,
232 $tw, $th,
233 0;
234 glPopmatrix;
235 } 399 ;
236} 400 my $desc = CFClient::Item::desc_string $self;
401 $self->{desc_widget}->set_text ($desc);
402 $self->{desc_widget}->set_tooltip ("<b>$desc</b>.\n$tooltip_std");
237 403
238sub DESTROY { 404 $self->{weight_widget} ||= new CFClient::UI::Label
239 my ($self) = @_; 405 can_events => 1,
406 can_hover => 1,
407 ellipsise => 0,
408 align => 0,
409 on_button_down => $button_cb,
410 ;
411 $self->{weight_widget}->set_text (CFClient::Item::weight_string $self);
240 412
241 return unless exists $self->{name}; 413 $self->{weight_widget}->set_tooltip (
242 414 "<b>Weight</b>.\n"
243 glDeleteTextures delete $self->{name}; 415 . ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ")
416 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ")
417 . "\n\n$tooltip_std"
418 );
244} 419}
245
246sub restore_state{
247 $_->upload
248 for grep $_, @textures;
249};
250 420
2511; 4211;
252 422
253=back 423=back
254 424

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines