ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/Util.pm
Revision: 1.8
Committed: Fri Mar 17 17:59:43 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.7: +15 -1 lines
Log Message:
fixed some bugs in attr editor and some glitch with place tool which
picked on end. also added a more complete map properties dialog.
also added world map navigation

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
22 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);
23
24 sub map2abs {
25 my ($dest, $mape) = @_;
26
27 my $dir;
28 if (File::Spec->file_name_is_absolute($dest)) {
29 $dir = catdir ($Crossfire::LIB, 'maps', $dest);
30 } else {
31 my ($v, $p, $f) = File::Spec->splitpath ($mape->{path});
32 $dir = File::Spec->rel2abs ($dest, File::Spec->catpath ($v, $p));
33 }
34 return $dir;
35 }
36
37 sub def($$) {
38 return defined ($_[0]) ? $_[0] : $_[1];
39 }
40
41 sub quick_msg {
42 my $wid = shift;
43 my $msg;
44 my $win = $::MAINWIN;
45 if (ref $wid) {
46 $win = $wid;
47 $msg = shift;
48 } else {
49 $msg = $wid;
50 }
51 my $dia = Gtk2::Dialog->new ('Message', $win, 'destroy-with-parent', 'gtk-ok' => 'none');
52
53 my $lbl = Gtk2::Label->new ($msg);
54 $dia->vbox->add ($lbl);
55 $dia->signal_connect (response => sub { $_[0]->destroy });
56
57 unless (defined $_[0]) {
58 Glib::Timeout->add (1000, sub { $dia->destroy; 0 });
59 }
60
61 $dia->show_all;
62 }
63
64 sub new_arch_pb {
65 # this is awful, is this really the best way?
66 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
67 return $pb;
68 }
69
70 sub fill_pb_from_arch {
71 my ($pb, $a) = @_;
72
73 my $o = $Crossfire::ARCH{$a->{_name}};
74 my $face = $Crossfire::FACE{$a->{face} || $o->{face} || "blank.111"}
75 or warn "no gfx found for arch '$a->{_name}' at ($x|$y)\n";
76
77 $face or return;
78
79 $pb->fill (0x00000000);
80 $TILE->composite ($pb,
81 0, 0,
82 TILESIZE, TILESIZE,
83 - ($face->{idx} % 64) * TILESIZE, - TILESIZE * int $face->{idx} / 64,
84 1, 1, 'nearest', 255
85 );
86 }
87
88 sub classify_arch_layer {
89 my ($arch) = @_;
90
91 if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals)
92
93 return 'below';
94
95 } elsif ($arch->{monster}) {
96
97 return 'top';
98
99 } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor
100
101 return 'between';
102 }
103 }
104
105 sub arch_is_exit {
106 my ($a) = @_;
107 my $type = $Crossfire::ARCH{$a->{_name}}->{type};
108 return $type eq '66' || $type eq '41';
109 }
110
111 sub arch_is_floor {
112 my ($a) = @_;
113 return $Crossfire::ARCH{$a->{_name}}->{is_floor};
114 }
115
116 sub arch_is_wall {
117 my ($a) = @_;
118 return $Crossfire::ARCH{$a->{_name}}->{no_pass};
119 }
120
121 sub arch_is_monster {
122 my ($a) = @_;
123 my $arch = $Crossfire::ARCH{$a->{_name}};
124 return $arch->{alive} and ($arch->{monster} or $arch->{generator});
125 }
126
127 sub stack_find {
128 my ($stack, $dir, $pred) = @_;
129
130
131 if ($dir eq 'from_top') {
132 my $i = scalar (@$stack) - 1;
133 if ($i < 0) { $i = 0 }
134
135 for (reverse @$stack) {
136 $pred->($_)
137 and return $i;
138
139 $i--;
140 }
141
142 } else {
143 my $i = 0;
144
145 for (@$stack) {
146 $pred->($_)
147 and return $i;
148
149 $i++;
150 }
151 }
152
153 return 0;
154
155 }
156
157 sub stack_find_floor {
158 my ($stack, $dir) = @_;
159 return stack_find ($stack, $dir, \&arch_is_floor);
160 }
161
162 sub stack_find_wall {
163 my ($stack, $dir) = @_;
164 return stack_find ($stack, $dir, \&arch_is_wall);
165 }
166
167 sub insert_arch_stack_layer {
168 my ($stack, $arch) = @_;
169
170 unless (@$stack) {
171 return [ $arch ];
172 }
173
174 my @outstack;
175
176 my $l = classify_arch_layer ($Crossfire::ARCH{$arch->{_name}});
177
178 if ($l eq 'between') {
179
180 # loop until we reached the first 'between' arch above 'below' arches and the floor
181 while (my $a = shift @$stack) {
182
183 unless ($Crossfire::ARCH{$a->{_name}}->{is_floor}
184 or classify_arch_layer ($Crossfire::ARCH{$a->{_name}}) eq 'below') {
185
186 unshift @$stack, $a;
187 last;
188 }
189
190 push @outstack, $a;
191 }
192
193 # ignore duplicates
194 # FIXME: Broken if non-floor are drawn (too tired to fix)
195 return [ @outstack, @$stack ]
196 if @outstack and $outstack[-1]->{_name} eq $arch->{_name};
197
198 push @outstack, ($arch, @$stack);
199
200 } elsif ($l eq 'top') {
201
202 # ignore duplicates
203 return [ @$stack ]
204 if $stack->[-1]->{_name} eq $arch->{_name};
205
206 @outstack = (@$stack, $arch);
207
208 } else {
209
210 # ignore duplicates
211 return [ @$stack ]
212 if $stack->[0]->{_name} eq $arch->{_name};
213
214 @outstack = ($arch, @$stack);
215 }
216
217 return \@outstack;
218 }
219
220 sub add_table_widget {
221 my ($table, $row, $data, $type, $cb) = @_;
222 my $edwid;
223
224 if ($type eq 'string') {
225 $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
226 $edwid = Gtk2::Entry->new;
227 $edwid->set_text ($data->[1]);
228 $edwid->signal_connect (changed => sub {
229 $data->[1] = $_[0]->get_text;
230 $cb->($data->[1]) if $cb;
231 });
232 $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
233
234 } elsif ($type eq 'button') {
235 $table->attach_defaults (my $b = Gtk2::Button->new_with_label ($data), 0, 2, $row, $row + 1);
236 $b->signal_connect (clicked => ($cb || sub {}));
237
238 } elsif ($type eq 'label') {
239 $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
240 $edwid = Gtk2::Label->new ($data->[1]);
241 $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
242
243 } else {
244 $edwid = Gtk2::Label->new ("FOO");
245 }
246 }
247
248 sub replace_arch_stack_layer {
249 my ($stack, $arch) = @_;
250
251 my @outstack;
252
253 my $l = classify_arch_layer ($Crossfire::ARCH{$arch->{_name}});
254
255 if ($l eq 'between') {
256
257 while (shift @$stack) {
258 last unless $Crossfire::ARCH{$_->{_name}}->{is_floor};
259 push @outstack, $_;
260 }
261
262 if (@outstack and $Crossfire::ARCH{$outstack[-1]->{_name}}->{is_floor}) {
263 pop @outstack;
264 }
265
266 push @outstack, ($arch, @$stack);
267
268 } elsif ($l eq 'top') {
269
270 @outstack = (@$stack, $arch);
271
272 } else {
273
274 @outstack = ($arch, @$stack);
275 }
276
277 return \@outstack;
278 }
279
280 =head1 AUTHOR
281
282 Marc Lehmann <schmorp@schmorp.de>
283 http://home.schmorp.de/
284
285 Robin Redeker <elmex@ta-sa.org>
286 http://www.ta-sa.org/
287
288 =cut
289 1;