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.7 by elmex, Fri Mar 17 01:18:01 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;
20
21our @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);
22
23sub def($$) {
24 return defined ($_[0]) ? $_[0] : $_[1];
25}
26
27sub 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
50sub 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
56sub 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}
19 73
20sub classify_arch_layer { 74sub classify_arch_layer {
21 my ($arch) = @_; 75 my ($arch) = @_;
22 76
23 if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals) 77 if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals)
30 84
31 } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor 85 } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor
32 86
33 return 'between'; 87 return 'between';
34 } 88 }
89}
90
91sub arch_is_exit {
92 my ($a) = @_;
93 my $type = $Crossfire::ARCH{$a->{_name}}->{type};
94 return $type eq '66' || $type eq '41';
95}
96
97sub arch_is_floor {
98 my ($a) = @_;
99 return $Crossfire::ARCH{$a->{_name}}->{is_floor};
100}
101
102sub arch_is_wall {
103 my ($a) = @_;
104 return $Crossfire::ARCH{$a->{_name}}->{no_pass};
105}
106
107sub arch_is_monster {
108 my ($a) = @_;
109 my $arch = $Crossfire::ARCH{$a->{_name}};
110 return $arch->{alive} and ($arch->{monster} or $arch->{generator});
111}
112
113sub stack_find {
114 my ($stack, $dir, $pred) = @_;
115
116
117 if ($dir eq 'from_top') {
118 my $i = scalar (@$stack) - 1;
119 if ($i < 0) { $i = 0 }
120
121 for (reverse @$stack) {
122 $pred->($_)
123 and return $i;
124
125 $i--;
126 }
127
128 } else {
129 my $i = 0;
130
131 for (@$stack) {
132 $pred->($_)
133 and return $i;
134
135 $i++;
136 }
137 }
138
139 return 0;
140
141}
142
143sub stack_find_floor {
144 my ($stack, $dir) = @_;
145 return stack_find ($stack, $dir, \&arch_is_floor);
146}
147
148sub stack_find_wall {
149 my ($stack, $dir) = @_;
150 return stack_find ($stack, $dir, \&arch_is_wall);
35} 151}
36 152
37sub insert_arch_stack_layer { 153sub insert_arch_stack_layer {
38 my ($stack, $arch) = @_; 154 my ($stack, $arch) = @_;
39 155
85 } 201 }
86 202
87 return \@outstack; 203 return \@outstack;
88} 204}
89 205
206sub add_table_widget {
207 my ($table, $row, $data, $type, $cb) = @_;
208 my $edwid;
209
210 if ($type eq 'string') {
211 $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
212 $edwid = Gtk2::Entry->new;
213 $edwid->set_text ($data->[1]);
214 $edwid->signal_connect (changed => sub {
215 $data->[1] = $_[0]->get_text;
216 $cb->($data->[1]) if $cb;
217 });
218 $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
219
220 } elsif ($type eq 'button') {
221 $table->attach_defaults (my $b = Gtk2::Button->new_with_label ($data), 0, 2, $row, $row + 1);
222 $b->signal_connect (clicked => ($cb || sub {}));
223
224 } elsif ($type eq 'label') {
225 $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
226 $edwid = Gtk2::Label->new ($data->[1]);
227 $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
228
229 } else {
230 $edwid = Gtk2::Label->new ("FOO");
231 }
232}
233
90sub replace_arch_stack_layer { 234sub replace_arch_stack_layer {
91 my ($stack, $arch) = @_; 235 my ($stack, $arch) = @_;
92 236
93 my @outstack; 237 my @outstack;
94 238

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines