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