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.1 by elmex, Mon Feb 20 18:21:04 2006 UTC vs.
Revision 1.10 by elmex, Tue Mar 21 01:02:14 2006 UTC

13 13
14use Carp (); 14use Carp ();
15use Storable; 15use Storable;
16use List::Util qw(min max); 16use List::Util qw(min max);
17 17
18our @EXPORT = qw(insert_arch_stack_layer replace_arch_stack_layer); 18use Crossfire;
19use Crossfire::MapWidget;
20use File::Spec::Functions;
21
22our @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
24sub map2abs {
25 my ($dest, $mape) = @_;
26
27 my $dir;
28 if (File::Spec->file_name_is_absolute($dest)) {
29 $dir = catdir ($CFG->{MAPDIR}, $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
37sub def($$) {
38 return defined ($_[0]) ? $_[0] : $_[1];
39}
40
41sub 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
64sub 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
70sub 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}
19 87
20sub classify_arch_layer { 88sub classify_arch_layer {
21 my ($arch) = @_; 89 my ($arch) = @_;
22 90
23 if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals) 91 if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals)
30 98
31 } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor 99 } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor
32 100
33 return 'between'; 101 return 'between';
34 } 102 }
103}
104
105sub arch_is_exit {
106 my ($a) = @_;
107 my $type = $Crossfire::ARCH{$a->{_name}}->{type};
108 return $type eq '66' || $type eq '41';
109}
110
111sub arch_is_floor {
112 my ($a) = @_;
113 my $ar = Crossfire::arch_attr $a;
114 return $ar->{name} eq 'Floor';
115#return $Crossfire::ARCH{$a->{_name}}->{is_floor};
116}
117
118sub arch_is_wall {
119 my ($a) = @_;
120 my $ar = Crossfire::arch_attr $a;
121 return $ar->{name} eq 'Wall';
122#return $Crossfire::ARCH{$a->{_name}}->{no_pass};
123}
124
125sub arch_is_monster {
126 my ($a) = @_;
127 my $arch = $Crossfire::ARCH{$a->{_name}};
128 return $arch->{alive} and ($arch->{monster} or $arch->{generator});
129}
130
131sub stack_find {
132 my ($stack, $dir, $pred) = @_;
133
134
135 if ($dir eq 'from_top') {
136 my $i = scalar (@$stack) - 1;
137 if ($i < 0) { $i = 0 }
138
139 for (reverse @$stack) {
140 $pred->($_)
141 and return $i;
142
143 $i--;
144 }
145
146 } else {
147 my $i = 0;
148
149 for (@$stack) {
150 $pred->($_)
151 and return $i;
152
153 $i++;
154 }
155 }
156
157 return 0;
158
159}
160
161sub stack_find_floor {
162 my ($stack, $dir) = @_;
163 return stack_find ($stack, $dir, \&arch_is_floor);
164}
165
166sub stack_find_wall {
167 my ($stack, $dir) = @_;
168 return stack_find ($stack, $dir, \&arch_is_wall);
35} 169}
36 170
37sub insert_arch_stack_layer { 171sub insert_arch_stack_layer {
38 my ($stack, $arch) = @_; 172 my ($stack, $arch) = @_;
39 173
85 } 219 }
86 220
87 return \@outstack; 221 return \@outstack;
88} 222}
89 223
224sub add_table_widget {
225 my ($table, $row, $data, $type, $cb) = @_;
226 my $edwid;
227
228 if ($type eq 'string') {
229 $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
230 $edwid = Gtk2::Entry->new;
231 $edwid->set_text ($data->[1]);
232 $edwid->signal_connect (changed => sub {
233 $data->[1] = $_[0]->get_text;
234 $cb->($data->[1]) if $cb;
235 });
236 $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
237
238 } elsif ($type eq 'button') {
239 $table->attach_defaults (my $b = Gtk2::Button->new_with_label ($data), 0, 2, $row, $row + 1);
240 $b->signal_connect (clicked => ($cb || sub {}));
241
242 } elsif ($type eq 'label') {
243 $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
244 $edwid = Gtk2::Label->new ($data->[1]);
245 $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
246
247 } else {
248 $edwid = Gtk2::Label->new ("FOO");
249 }
250}
251
90sub replace_arch_stack_layer { 252sub replace_arch_stack_layer {
91 my ($stack, $arch) = @_; 253 my ($stack, $arch) = @_;
92 254
93 my @outstack; 255 my @outstack;
94 256

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines