ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/Util.pm
Revision: 1.17
Committed: Tue Apr 4 11:29:54 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.16: +1 -0 lines
Log Message:
Removed pick and made the attribut editor 'the current pick'.
Added context menu to map view with an follow item in it.

File Contents

# Content
1 package GCE::Util;
2 =head1 NAME
3
4 GCE::Util - some utility functions
5
6 =over 4
7
8 =cut
9
10 use base 'Exporter';
11
12 use Crossfire;
13
14 use Carp ();
15 use Storable;
16 use List::Util qw(min max);
17
18 use Crossfire;
19 use Crossfire::MapWidget;
20 use File::Spec::Functions;
21 use Cwd 'abs_path';
22
23 our @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
25 sub 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
38 sub 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
67 sub 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
81 sub def($$) {
82 return defined ($_[0]) ? $_[0] : $_[1];
83 }
84
85 sub 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 }
107
108 sub new_arch_pb {
109 # this is awful, is this really the best way?
110 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
111 return $pb;
112 }
113
114 sub fill_pb_from_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;
122
123 $pb->fill (0x00000000);
124 $TILE->composite ($pb,
125 0, 0,
126 TILESIZE, TILESIZE,
127 - ($face->{idx} % 64) * TILESIZE, - TILESIZE * int $face->{idx} / 64,
128 1, 1, 'nearest', 255
129 );
130 }
131
132 sub classify_arch_layer {
133 my ($arch) = @_;
134
135 if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals)
136
137 return 'below';
138
139 } elsif ($arch->{monster}) {
140
141 return 'top';
142
143 } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor
144
145 return 'between';
146 }
147 }
148
149 sub arch_is_exit {
150 my ($a) = @_;
151 my $type = $Crossfire::ARCH{$a->{_name}}->{type};
152 return $type eq '66' || $type eq '41';
153 }
154
155 sub arch_is_floor {
156 my ($a) = @_;
157 my $ar = Crossfire::arch_attr $a;
158 return (substr $ar->{name}, 0, 5) eq 'Floor';
159 #return $Crossfire::ARCH{$a->{_name}}->{is_floor};
160 }
161
162 sub arch_is_wall {
163 my ($a) = @_;
164 my $ar = Crossfire::arch_attr $a;
165 return $ar->{name} eq 'Wall';
166 #return $Crossfire::ARCH{$a->{_name}}->{no_pass};
167 }
168
169 sub arch_is_monster {
170 my ($a) = @_;
171 my $arch = $Crossfire::ARCH{$a->{_name}};
172 return $arch->{alive} and ($arch->{monster} or $arch->{generator});
173 }
174
175 sub stack_find {
176 my ($stack, $dir, $pred) = @_;
177
178
179 if ($dir eq 'from_top') {
180 my $i = scalar (@$stack) - 1;
181 if ($i < 0) { $i = 0 }
182
183 for (reverse @$stack) {
184 $pred->($_)
185 and return $i;
186
187 $i--;
188 }
189
190 } else {
191 my $i = 0;
192
193 for (@$stack) {
194 $pred->($_)
195 and return $i;
196
197 $i++;
198 }
199 }
200
201 return 0;
202
203 }
204
205 sub stack_find_floor {
206 my ($stack, $dir) = @_;
207 return stack_find ($stack, $dir, \&arch_is_floor);
208 }
209
210 sub stack_find_wall {
211 my ($stack, $dir) = @_;
212 return stack_find ($stack, $dir, \&arch_is_wall);
213 }
214
215 sub insert_arch_stack_layer {
216 my ($stack, $arch) = @_;
217
218 unless (@$stack) {
219 return [ $arch ];
220 }
221
222 my @outstack;
223
224 my $l = classify_arch_layer ($Crossfire::ARCH{$arch->{_name}});
225
226 if ($l eq 'between') {
227
228 # loop until we reached the first 'between' arch above 'below' arches and the floor
229 while (my $a = shift @$stack) {
230
231 unless ($Crossfire::ARCH{$a->{_name}}->{is_floor}
232 or classify_arch_layer ($Crossfire::ARCH{$a->{_name}}) eq 'below') {
233
234 unshift @$stack, $a;
235 last;
236 }
237
238 push @outstack, $a;
239 }
240
241 # ignore duplicates
242 # FIXME: Broken if non-floor are drawn (too tired to fix)
243 return [ @outstack, @$stack ]
244 if @outstack and $outstack[-1]->{_name} eq $arch->{_name};
245
246 push @outstack, ($arch, @$stack);
247
248 } elsif ($l eq 'top') {
249
250 # ignore duplicates
251 return [ @$stack ]
252 if $stack->[-1]->{_name} eq $arch->{_name};
253
254 @outstack = (@$stack, $arch);
255
256 } else {
257
258 # ignore duplicates
259 return [ @$stack ]
260 if $stack->[0]->{_name} eq $arch->{_name};
261
262 @outstack = ($arch, @$stack);
263 }
264
265 return \@outstack;
266 }
267
268 sub 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
296 sub replace_arch_stack_layer {
297 my ($stack, $arch) = @_;
298
299 my @outstack;
300
301 my $l = classify_arch_layer ($Crossfire::ARCH{$arch->{_name}});
302
303 if ($l eq 'between') {
304
305 while (shift @$stack) {
306 last unless $Crossfire::ARCH{$_->{_name}}->{is_floor};
307 push @outstack, $_;
308 }
309
310 if (@outstack and $Crossfire::ARCH{$outstack[-1]->{_name}}->{is_floor}) {
311 pop @outstack;
312 }
313
314 push @outstack, ($arch, @$stack);
315
316 } elsif ($l eq 'top') {
317
318 @outstack = (@$stack, $arch);
319
320 } else {
321
322 @outstack = ($arch, @$stack);
323 }
324
325 return \@outstack;
326 }
327
328 =head1 AUTHOR
329
330 Marc Lehmann <schmorp@schmorp.de>
331 http://home.schmorp.de/
332
333 Robin Redeker <elmex@ta-sa.org>
334 http://www.ta-sa.org/
335
336 =cut
337 1;