ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/Util.pm
(Generate patch)

Comparing deliantra/gde/GCE/Util.pm (file contents):
Revision 1.2 by elmex, Fri Mar 10 20:32:47 2006 UTC vs.
Revision 1.17 by elmex, Tue Apr 4 11:29:54 2006 UTC

15use Storable; 15use Storable;
16use List::Util qw(min max); 16use List::Util qw(min max);
17 17
18use Crossfire; 18use Crossfire;
19use Crossfire::MapWidget; 19use Crossfire::MapWidget;
20use File::Spec::Functions;
21use Cwd 'abs_path';
20 22
21our @EXPORT = qw(insert_arch_stack_layer replace_arch_stack_layer new_arch_pb fill_pb_from_arch arch_is_floor stack_find_floor stack_find_wall stack_find arch_is_wall); 23our @EXPORT = qw(insert_arch_stack_layer replace_arch_stack_layer new_arch_pb fill_pb_from_arch arch_is_floor stack_find_floor stack_find_wall stack_find arch_is_wall arch_is_monster add_table_widget quick_msg def arch_is_exit map2abs exit_paths pseudohtml2txt);
24
25sub pseudohtml2txt {
26 my ($html) = @_;
27
28 $html =~ s/<br\s*?\/?>/\n/gsi;
29 $html =~ s/<b>(.*?)<\/b>/_\1_/gsi;
30 $html =~ s/<li>/\n* /gi;
31 $html =~ s/<\/?\s*li>//gi;
32 $html =~ s/<\/?\s*ul>//gi;
33 $html =~ s/&gt;/>/g;
34 $html =~ s/&lt;/</g;
35 $html
36}
37
38sub exit_paths {
39 my ($mappath, $map1path, $map2path) = @_;
40 $mappath = abs_path $mappath;
41 $map1path = abs_path $map1path;
42 $map2path = abs_path $map2path;
43
44 if ( (substr $map1path, 0, length $mappath) eq $mappath
45 and (substr $map2path, 0, length $mappath) eq $mappath) {
46 substr $map1path, 0, length $mappath, '';
47 substr $map2path, 0, length $mappath, '';
48
49 my ($v1, $d1, $f1) = File::Spec->splitpath ($map1path);
50 my ($v2, $d2, $f2) = File::Spec->splitpath ($map2path);
51
52 my @di1 = File::Spec->splitdir ($d1);
53 my @di2 = File::Spec->splitdir ($d2);
54
55 if ((defined $di1[1]) and (defined $di2[1]) and $di1[1] eq $di2[1]) {
56 my $m1 = File::Spec->abs2rel ($map1path, File::Spec->catdir (@di2));
57 my $m2 = File::Spec->abs2rel ($map2path, File::Spec->catdir (@di1));
58 return ($m1, $m2);
59 } else {
60 return ($map1path, $map2path);
61 }
62 } else {
63 return ('', '');
64 }
65}
66
67sub map2abs {
68 my ($dest, $mape) = @_;
69
70 $mappath = abs_path $mappath;
71 my $dir;
72 if (File::Spec->file_name_is_absolute($dest)) {
73 $dir = catdir ($::CFG->{MAPDIR}, $dest);
74 } else {
75 my ($v, $p, $f) = File::Spec->splitpath ($mape->{path});
76 $dir = File::Spec->rel2abs ($dest, File::Spec->catpath ($v, $p));
77 }
78 return $dir;
79}
80
81sub def($$) {
82 return defined ($_[0]) ? $_[0] : $_[1];
83}
84
85sub quick_msg {
86 my $wid = shift;
87 my $msg;
88 my $win = $::MAINWIN;
89 if (ref $wid) {
90 $win = $wid;
91 $msg = shift;
92 } else {
93 $msg = $wid;
94 }
95 my $dia = Gtk2::Dialog->new ('Message', $win, 'destroy-with-parent', 'gtk-ok' => 'none');
96
97 my $lbl = Gtk2::Label->new ($msg);
98 $dia->vbox->add ($lbl);
99 $dia->signal_connect (response => sub { $_[0]->destroy });
100
101 unless (defined $_[0]) {
102 Glib::Timeout->add (1000, sub { $dia->destroy; 0 });
103 }
104
105 $dia->show_all;
106}
22 107
23sub new_arch_pb { 108sub new_arch_pb {
24 # this is awful, is this really the best way? 109 # this is awful, is this really the best way?
25 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE; 110 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
26 return $pb; 111 return $pb;
27} 112}
28 113
29sub fill_pb_from_arch { 114sub fill_pb_from_arch {
30 my ($pb, $arch) = @_; 115 my ($pb, $a) = @_;
116
117 my $o = $Crossfire::ARCH{$a->{_name}};
118 my $face = $Crossfire::FACE{$a->{face} || $o->{face} || "blank.111"}
119 or warn "no gfx found for arch '$a->{_name}' at ($x|$y)\n";
120
121 $face or return;
31 122
32 $pb->fill (0x00000000); 123 $pb->fill (0x00000000);
33 $TILE->composite ($pb, 124 $TILE->composite ($pb,
34 0, 0, 125 0, 0,
35 TILESIZE, TILESIZE, 126 TILESIZE, TILESIZE,
36 - ($arch->{_face} % 64) * TILESIZE, - TILESIZE * int $arch->{_face} / 64, 127 - ($face->{idx} % 64) * TILESIZE, - TILESIZE * int $face->{idx} / 64,
37 1, 1, 'nearest', 255 128 1, 1, 'nearest', 255
38 ); 129 );
39} 130}
40 131
41sub classify_arch_layer { 132sub classify_arch_layer {
53 144
54 return 'between'; 145 return 'between';
55 } 146 }
56} 147}
57 148
149sub arch_is_exit {
150 my ($a) = @_;
151 my $type = $Crossfire::ARCH{$a->{_name}}->{type};
152 return $type eq '66' || $type eq '41';
153}
154
58sub arch_is_floor { 155sub arch_is_floor {
59 my ($a) = @_; 156 my ($a) = @_;
157 my $ar = Crossfire::arch_attr $a;
158 return (substr $ar->{name}, 0, 5) eq 'Floor';
60 return $Crossfire::ARCH{$a->{_name}}->{is_floor}; 159#return $Crossfire::ARCH{$a->{_name}}->{is_floor};
61} 160}
62 161
63sub arch_is_wall { 162sub arch_is_wall {
64 my ($a) = @_; 163 my ($a) = @_;
164 my $ar = Crossfire::arch_attr $a;
165 return $ar->{name} eq 'Wall';
65 return $Crossfire::ARCH{$a->{_name}}->{no_pass}; 166#return $Crossfire::ARCH{$a->{_name}}->{no_pass};
167}
168
169sub arch_is_monster {
170 my ($a) = @_;
171 my $arch = $Crossfire::ARCH{$a->{_name}};
172 return $arch->{alive} and ($arch->{monster} or $arch->{generator});
66} 173}
67 174
68sub stack_find { 175sub stack_find {
69 my ($stack, $dir, $pred) = @_; 176 my ($stack, $dir, $pred) = @_;
70 177
156 } 263 }
157 264
158 return \@outstack; 265 return \@outstack;
159} 266}
160 267
268sub add_table_widget {
269 my ($table, $row, $data, $type, $cb) = @_;
270 my $edwid;
271
272 if ($type eq 'string') {
273 $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
274 $edwid = Gtk2::Entry->new;
275 $edwid->set_text ($data->[1]);
276 $edwid->signal_connect (changed => sub {
277 $data->[1] = $_[0]->get_text;
278 $cb->($data->[1]) if $cb;
279 });
280 $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
281
282 } elsif ($type eq 'button') {
283 $table->attach_defaults (my $b = Gtk2::Button->new_with_label ($data), 0, 2, $row, $row + 1);
284 $b->signal_connect (clicked => ($cb || sub {}));
285
286 } elsif ($type eq 'label') {
287 $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
288 $edwid = Gtk2::Label->new ($data->[1]);
289 $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
290
291 } else {
292 $edwid = Gtk2::Label->new ("FOO");
293 }
294}
295
161sub replace_arch_stack_layer { 296sub replace_arch_stack_layer {
162 my ($stack, $arch) = @_; 297 my ($stack, $arch) = @_;
163 298
164 my @outstack; 299 my @outstack;
165 300

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines