ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/Util.pm
Revision: 1.16
Committed: Sun Apr 2 18:32:42 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.15: +1 -0 lines
Log Message:
changed the layouf of the documentation widgets

File Contents

# User Rev Content
1 elmex 1.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 elmex 1.2 use Crossfire;
19     use Crossfire::MapWidget;
20 elmex 1.8 use File::Spec::Functions;
21 elmex 1.13 use Cwd 'abs_path';
22 elmex 1.2
23 elmex 1.14 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 elmex 1.16 $html =~ s/<li>/\n* /gi;
31 elmex 1.15 $html =~ s/<\/?\s*li>//gi;
32     $html =~ s/<\/?\s*ul>//gi;
33     $html =~ s/&gt;/>/g;
34     $html =~ s/&lt;/</g;
35 elmex 1.14 $html
36     }
37 elmex 1.13
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 elmex 1.8
67     sub map2abs {
68     my ($dest, $mape) = @_;
69    
70     my $dir;
71     if (File::Spec->file_name_is_absolute($dest)) {
72 elmex 1.11 $dir = catdir ($::CFG->{MAPDIR}, $dest);
73 elmex 1.8 } else {
74     my ($v, $p, $f) = File::Spec->splitpath ($mape->{path});
75     $dir = File::Spec->rel2abs ($dest, File::Spec->catpath ($v, $p));
76     }
77     return $dir;
78     }
79 elmex 1.5
80     sub def($$) {
81     return defined ($_[0]) ? $_[0] : $_[1];
82     }
83    
84     sub quick_msg {
85     my $wid = shift;
86     my $msg;
87     my $win = $::MAINWIN;
88     if (ref $wid) {
89     $win = $wid;
90     $msg = shift;
91     } else {
92     $msg = $wid;
93     }
94     my $dia = Gtk2::Dialog->new ('Message', $win, 'destroy-with-parent', 'gtk-ok' => 'none');
95    
96     my $lbl = Gtk2::Label->new ($msg);
97     $dia->vbox->add ($lbl);
98     $dia->signal_connect (response => sub { $_[0]->destroy });
99    
100     unless (defined $_[0]) {
101     Glib::Timeout->add (1000, sub { $dia->destroy; 0 });
102     }
103    
104     $dia->show_all;
105     }
106 elmex 1.2
107     sub new_arch_pb {
108     # this is awful, is this really the best way?
109     my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
110     return $pb;
111     }
112    
113     sub fill_pb_from_arch {
114 elmex 1.6 my ($pb, $a) = @_;
115    
116     my $o = $Crossfire::ARCH{$a->{_name}};
117     my $face = $Crossfire::FACE{$a->{face} || $o->{face} || "blank.111"}
118     or warn "no gfx found for arch '$a->{_name}' at ($x|$y)\n";
119    
120     $face or return;
121 elmex 1.2
122     $pb->fill (0x00000000);
123     $TILE->composite ($pb,
124     0, 0,
125     TILESIZE, TILESIZE,
126 elmex 1.6 - ($face->{idx} % 64) * TILESIZE, - TILESIZE * int $face->{idx} / 64,
127 elmex 1.2 1, 1, 'nearest', 255
128     );
129     }
130 elmex 1.1
131     sub classify_arch_layer {
132     my ($arch) = @_;
133    
134     if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals)
135    
136     return 'below';
137    
138     } elsif ($arch->{monster}) {
139    
140     return 'top';
141    
142     } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor
143    
144     return 'between';
145     }
146     }
147    
148 elmex 1.7 sub arch_is_exit {
149     my ($a) = @_;
150     my $type = $Crossfire::ARCH{$a->{_name}}->{type};
151     return $type eq '66' || $type eq '41';
152     }
153    
154 elmex 1.2 sub arch_is_floor {
155     my ($a) = @_;
156 elmex 1.10 my $ar = Crossfire::arch_attr $a;
157 elmex 1.12 return (substr $ar->{name}, 0, 5) eq 'Floor';
158 elmex 1.10 #return $Crossfire::ARCH{$a->{_name}}->{is_floor};
159 elmex 1.2 }
160    
161     sub arch_is_wall {
162     my ($a) = @_;
163 elmex 1.10 my $ar = Crossfire::arch_attr $a;
164     return $ar->{name} eq 'Wall';
165     #return $Crossfire::ARCH{$a->{_name}}->{no_pass};
166 elmex 1.2 }
167    
168 elmex 1.3 sub arch_is_monster {
169     my ($a) = @_;
170     my $arch = $Crossfire::ARCH{$a->{_name}};
171     return $arch->{alive} and ($arch->{monster} or $arch->{generator});
172     }
173    
174 elmex 1.2 sub stack_find {
175     my ($stack, $dir, $pred) = @_;
176    
177    
178     if ($dir eq 'from_top') {
179     my $i = scalar (@$stack) - 1;
180     if ($i < 0) { $i = 0 }
181    
182     for (reverse @$stack) {
183     $pred->($_)
184     and return $i;
185    
186     $i--;
187     }
188    
189     } else {
190     my $i = 0;
191    
192     for (@$stack) {
193     $pred->($_)
194     and return $i;
195    
196     $i++;
197     }
198     }
199    
200     return 0;
201    
202     }
203    
204     sub stack_find_floor {
205     my ($stack, $dir) = @_;
206     return stack_find ($stack, $dir, \&arch_is_floor);
207     }
208    
209     sub stack_find_wall {
210     my ($stack, $dir) = @_;
211     return stack_find ($stack, $dir, \&arch_is_wall);
212     }
213    
214 elmex 1.1 sub insert_arch_stack_layer {
215     my ($stack, $arch) = @_;
216    
217     unless (@$stack) {
218     return [ $arch ];
219     }
220    
221     my @outstack;
222    
223     my $l = classify_arch_layer ($Crossfire::ARCH{$arch->{_name}});
224    
225     if ($l eq 'between') {
226    
227     # loop until we reached the first 'between' arch above 'below' arches and the floor
228     while (my $a = shift @$stack) {
229    
230     unless ($Crossfire::ARCH{$a->{_name}}->{is_floor}
231     or classify_arch_layer ($Crossfire::ARCH{$a->{_name}}) eq 'below') {
232    
233     unshift @$stack, $a;
234     last;
235     }
236    
237     push @outstack, $a;
238     }
239    
240     # ignore duplicates
241     # FIXME: Broken if non-floor are drawn (too tired to fix)
242     return [ @outstack, @$stack ]
243     if @outstack and $outstack[-1]->{_name} eq $arch->{_name};
244    
245     push @outstack, ($arch, @$stack);
246    
247     } elsif ($l eq 'top') {
248    
249     # ignore duplicates
250     return [ @$stack ]
251     if $stack->[-1]->{_name} eq $arch->{_name};
252    
253     @outstack = (@$stack, $arch);
254    
255     } else {
256    
257     # ignore duplicates
258     return [ @$stack ]
259     if $stack->[0]->{_name} eq $arch->{_name};
260    
261     @outstack = ($arch, @$stack);
262     }
263    
264     return \@outstack;
265     }
266    
267 elmex 1.4 sub add_table_widget {
268     my ($table, $row, $data, $type, $cb) = @_;
269     my $edwid;
270    
271     if ($type eq 'string') {
272     $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
273     $edwid = Gtk2::Entry->new;
274     $edwid->set_text ($data->[1]);
275     $edwid->signal_connect (changed => sub {
276     $data->[1] = $_[0]->get_text;
277     $cb->($data->[1]) if $cb;
278     });
279     $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
280    
281     } elsif ($type eq 'button') {
282     $table->attach_defaults (my $b = Gtk2::Button->new_with_label ($data), 0, 2, $row, $row + 1);
283     $b->signal_connect (clicked => ($cb || sub {}));
284    
285     } elsif ($type eq 'label') {
286     $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
287     $edwid = Gtk2::Label->new ($data->[1]);
288     $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
289    
290     } else {
291     $edwid = Gtk2::Label->new ("FOO");
292     }
293     }
294    
295 elmex 1.1 sub replace_arch_stack_layer {
296     my ($stack, $arch) = @_;
297    
298     my @outstack;
299    
300     my $l = classify_arch_layer ($Crossfire::ARCH{$arch->{_name}});
301    
302     if ($l eq 'between') {
303    
304     while (shift @$stack) {
305     last unless $Crossfire::ARCH{$_->{_name}}->{is_floor};
306     push @outstack, $_;
307     }
308    
309     if (@outstack and $Crossfire::ARCH{$outstack[-1]->{_name}}->{is_floor}) {
310     pop @outstack;
311     }
312    
313     push @outstack, ($arch, @$stack);
314    
315     } elsif ($l eq 'top') {
316    
317     @outstack = (@$stack, $arch);
318    
319     } else {
320    
321     @outstack = ($arch, @$stack);
322     }
323    
324     return \@outstack;
325     }
326    
327     =head1 AUTHOR
328    
329     Marc Lehmann <schmorp@schmorp.de>
330     http://home.schmorp.de/
331    
332     Robin Redeker <elmex@ta-sa.org>
333     http://www.ta-sa.org/
334    
335     =cut
336     1;