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.29 by root, Wed Apr 12 20:42:52 2006 UTC vs.
Revision 1.99 by elmex, Fri Jul 14 17:35:34 2006 UTC

19 19
20 use XSLoader; 20 use XSLoader;
21 XSLoader::load "CFClient", $VERSION; 21 XSLoader::load "CFClient", $VERSION;
22} 22}
23 23
24use SDL::OpenGL; 24use utf8;
25 25
26our %GL_EXT; 26use Carp ();
27our $GL_VERSION; 27use AnyEvent ();
28use BerkeleyDB;
29use Pod::POM ();
30use Scalar::Util ();
31use Storable (); # finally
28 32
29our $GL_NPOT; 33package CFClient::PodToPango;
30 34
31sub gl_init { 35use base Pod::POM::View::Text;
32 $GL_VERSION = gl_version * 1;
33 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions;
34 36
35 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; 37our $VERSION = 1; # bump if resultant formatting changes
36 38
37 glClearColor 0.45, 0.45, 0.45, 1; 39our $indent = 0;
38 40
39 glEnable GL_TEXTURE_2D; 41*view_seq_code =
40 glEnable GL_COLOR_MATERIAL; 42*view_seq_bold = sub { "<b>$_[1]</b>" };
41 glShadeModel GL_FLAT; 43*view_seq_italic = sub { "<i>$_[1]</i>" };
42 glDisable GL_DEPTH_TEST; 44*view_seq_space =
43 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 45*view_seq_link =
46*view_seq_index = sub { CFClient::UI::Label::escape ($_[1]) };
44 47
45 glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST; 48sub view_seq_text {
46 49 my $text = $_[1];
47 CFClient::Texture::restore_state (); 50 $text =~ s/\s+/ /g;
51 CFClient::UI::Label::escape ($text)
48} 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;
49 115
50sub find_rcfile($) { 116sub find_rcfile($) {
51 my $path; 117 my $path;
52 118
53 for (@INC) { 119 for (grep !ref, @INC) {
54 $path = "$_/CFClient/resources/$_[0]"; 120 $path = "$_/CFClient/resources/$_[0]";
55 return $path if -r $path; 121 return $path if -r $path;
56 } 122 }
57 123
58 die "FATAL: can't find required file $_[0]\n"; 124 die "FATAL: can't find required file $_[0]\n";
88 } 154 }
89 155
90 close CFG; 156 close CFG;
91} 157}
92 158
159our $DB_ENV;
160
161{
162 use strict;
163
164 mkdir "$Crossfire::VARDIR/cfplus", 0777;
165 my $recover = $BerkeleyDB::db_version >= 4.4
166 ? eval "DB_REGISTER | DB_RECOVER"
167 : 0;
168
169 $DB_ENV = new BerkeleyDB::Env
170 -Home => "$Crossfire::VARDIR/cfplus",
171 -Cachesize => 1_000_000,
172 -ErrFile => "$Crossfire::VARDIR/cfplus/errorlog.txt",
173# -ErrPrefix => "DATABASE",
174 -Verbose => 1,
175 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover,
176 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE,
177 or die "unable to create/open database home $Crossfire::VARDIR/cfplus: $BerkeleyDB::Error";
178}
179
180sub db_table($) {
181 my ($table) = @_;
182
183 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
184
185 new CFClient::Database
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}
194
195my $pod_cache = db_table "pod_cache";
196
197sub load_pod($$$$) {
198 my ($path, $filtertype, $filterversion, $filtercb) = @_;
199
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::Layout;
241
242$CFClient::OpenGL::SHUTDOWN_HOOK{"CFClient::Layout"} = sub {
243 reset_glyph_cache;
244};
245
93package CFClient::Texture; 246package CFClient::Item;
94 247
95use strict; 248use strict;
249use Crossfire::Protocol::Constants;
96 250
97use Scalar::Util; 251my $last_enter_count = 1;
98 252
99use SDL::OpenGL; 253sub desc_string {
100
101my @textures;
102
103sub new {
104 my ($class, %data) = @_;
105
106 my $self = bless {
107 internalformat => GL_RGBA,
108 format => GL_RGBA,
109 type => GL_UNSIGNED_BYTE,
110 %data,
111 }, $class;
112
113 push @textures, $self;
114 Scalar::Util::weaken $textures[-1];
115
116 $self->upload;
117
118 $self
119}
120
121sub new_from_image {
122 my ($class, $image) = @_;
123
124 $class->new (image => $image)
125}
126
127sub new_from_file {
128 my ($class, $path) = @_;
129
130 open my $fh, "<:raw", $path
131 or die "$path: $!";
132
133 local $/;
134 $class->new_from_image (<$fh>)
135}
136
137#sub new_from_surface {
138# my ($class, $surface) = @_;
139#
140# $surface->rgba;
141#
142# $class->new (
143# data => $surface->pixels,
144# w => $surface->width,
145# h => $surface->height,
146# )
147#}
148
149sub new_from_layout {
150 my ($class, $layout) = @_;
151
152 my ($w, $h, $data) = $layout->render;
153
154 $class->new (
155 w => $w,
156 h => $h,
157 data => $data,
158 internalformat => GL_ALPHA4,
159 format => GL_ALPHA,
160 type => GL_UNSIGNED_BYTE,
161 )
162}
163
164sub new_from_opengl {
165 my ($class, $w, $h, $cb) = @_;
166
167 $class->new (w => $w, h => $h, render_cb => $cb)
168}
169
170sub topot {
171 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
172}
173
174sub upload {
175 my ($self) = @_; 254 my ($self) = @_;
176 255
177 return unless $GL_VERSION; 256 my $desc =
257 $self->{nrof} < 2
258 ? $self->{name}
259 : "$self->{nrof} × $self->{name_pl}";
178 260
179 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 .= " *";
180 275
181 if (exists $self->{data}) { 276 $desc
182 $data = $self->{data}; 277}
183 278
184 } elsif (exists $self->{render_cb}) { 279sub weight_string {
185 glViewport 0, 0, $self->{w}, $self->{h}; 280 my ($self) = @_;
186 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
187 glMatrixMode GL_PROJECTION;
188 glLoadIdentity;
189 glMatrixMode GL_MODELVIEW;
190 glLoadIdentity;
191 glClear GL_COLOR_BUFFER_BIT;
192 $self->{render_cb}->($self, $self->{w}, $self->{h});
193 281
194 } else { 282 my $weight = ($self->{nrof} || 1) * $self->{weight};
195 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
196 = CFClient::load_image_inline $self->{image};
197 }
198 283
199 my ($tw, $th) = @$self{qw(w h)}; 284 $weight < 0 ? "?" : $weight * 0.001
285}
200 286
201 unless ($tw && $th) { 287sub do_n_dialog {
202 $tw = $th = 1; 288 my ($cb) = @_;
203 $data = "\x00" x 64;
204 }
205 289
206 unless ($GL_NPOT) { 290 my $w = new CFClient::UI::FancyFrame;
207 # TODO: does not work for zero-sized textures 291 $w->add (my $vb = new CFClient::UI::VBox x => "center", y => "center");
208 $tw = topot $tw; 292 $vb->add (new CFClient::UI::Label text => "Enter item count:");
209 $th = topot $th; 293 $vb->add (my $entry = new CFClient::UI::Entry
210 294 text => $last_enter_count,
211 if ($tw != $self->{w} || $th != $self->{h} && defined $data) { 295 on_activate => sub {
212 my $bpp = (length $data) / ($self->{w} * $self->{h}); 296 my ($entry) = @_;
213 $data = pack "(a" . ($tw * $bpp) . ")*", 297 $last_enter_count = $entry->get_text;
214 unpack "(a" . ($self->{w} * $bpp) . ")*", $data; 298 $cb->($last_enter_count);
215 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); 299 $w->hide;
300 $w = undef;
216 } 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
217 } 366 };
218 367
219 $self->{s} = $self->{w} / $tw; 368 my $tooltip_std = "<small>"
220 $self->{t} = $self->{h} / $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";
221 375
222 $self->{name} ||= (glGenTextures 1)->[0]; 376 $self->{face_widget} ||= new CFClient::UI::Face
223 377 can_events => 1,
224 glBindTexture GL_TEXTURE_2D, $self->{name}; 378 can_hover => 1,
225 379 anim => $self->{anim},
226 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; 380 animspeed => $self->{animspeed}, # TODO# must be set at creation time
227 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; 381 on_button_down => $button_cb,
228 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 382 ;
229 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 );
230 392
231 if (defined $data) { 393 $self->{desc_widget} ||= new CFClient::UI::Label
232 glTexImage2D GL_TEXTURE_2D, 0, 394 can_events => 1,
233 $self->{internalformat}, 395 can_hover => 1,
234 $tw, $th, # need to pad texture first 396 ellipsise => 2,
235 0, 397 align => -1,
236 $self->{format}, 398 on_button_down => $button_cb,
237 $self->{type},
238 $data;
239 glGetError and die;
240 } else {
241 glCopyTexImage2D GL_TEXTURE_2D, 0,
242 $self->{internalformat},
243 0, 0,
244 $tw, $th,
245 0;
246 glGetError and die;
247 } 399 ;
248} 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");
249 403
250sub DESTROY { 404 $self->{weight_widget} ||= new CFClient::UI::Label
251 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);
252 412
253 return unless exists $self->{name}; 413 $self->{weight_widget}->set_tooltip (
254 414 "<b>Weight</b>.\n"
255 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 );
256} 419}
257
258sub restore_state{
259 $_->upload
260 for grep $_, @textures;
261};
262 420
2631; 4211;
264 422
265=back 423=back
266 424

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines