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.5 by elmex, Thu Mar 16 11:59:34 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);
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, $arch) = @_;
58
59 $pb->fill (0x00000000);
60 $TILE->composite ($pb,
61 0, 0,
62 TILESIZE, TILESIZE,
63 - ($arch->{_face} % 64) * TILESIZE, - TILESIZE * int $arch->{_face} / 64,
64 1, 1, 'nearest', 255
65 );
66}
19 67
20sub classify_arch_layer { 68sub classify_arch_layer {
21 my ($arch) = @_; 69 my ($arch) = @_;
22 70
23 if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals) 71 if ($arch->{invisible}) { # just a heuristic for 'special' tiles (er. pedestals)
30 78
31 } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor 79 } else { # $arch->{is_floor} and all other arches are 'between' monsters and floor
32 80
33 return 'between'; 81 return 'between';
34 } 82 }
83}
84
85sub arch_is_floor {
86 my ($a) = @_;
87 return $Crossfire::ARCH{$a->{_name}}->{is_floor};
88}
89
90sub arch_is_wall {
91 my ($a) = @_;
92 return $Crossfire::ARCH{$a->{_name}}->{no_pass};
93}
94
95sub arch_is_monster {
96 my ($a) = @_;
97 my $arch = $Crossfire::ARCH{$a->{_name}};
98 return $arch->{alive} and ($arch->{monster} or $arch->{generator});
99}
100
101sub stack_find {
102 my ($stack, $dir, $pred) = @_;
103
104
105 if ($dir eq 'from_top') {
106 my $i = scalar (@$stack) - 1;
107 if ($i < 0) { $i = 0 }
108
109 for (reverse @$stack) {
110 $pred->($_)
111 and return $i;
112
113 $i--;
114 }
115
116 } else {
117 my $i = 0;
118
119 for (@$stack) {
120 $pred->($_)
121 and return $i;
122
123 $i++;
124 }
125 }
126
127 return 0;
128
129}
130
131sub stack_find_floor {
132 my ($stack, $dir) = @_;
133 return stack_find ($stack, $dir, \&arch_is_floor);
134}
135
136sub stack_find_wall {
137 my ($stack, $dir) = @_;
138 return stack_find ($stack, $dir, \&arch_is_wall);
35} 139}
36 140
37sub insert_arch_stack_layer { 141sub insert_arch_stack_layer {
38 my ($stack, $arch) = @_; 142 my ($stack, $arch) = @_;
39 143
85 } 189 }
86 190
87 return \@outstack; 191 return \@outstack;
88} 192}
89 193
194sub add_table_widget {
195 my ($table, $row, $data, $type, $cb) = @_;
196 my $edwid;
197
198 if ($type eq 'string') {
199 $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
200 $edwid = Gtk2::Entry->new;
201 $edwid->set_text ($data->[1]);
202 $edwid->signal_connect (changed => sub {
203 $data->[1] = $_[0]->get_text;
204 $cb->($data->[1]) if $cb;
205 });
206 $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
207
208 } elsif ($type eq 'button') {
209 $table->attach_defaults (my $b = Gtk2::Button->new_with_label ($data), 0, 2, $row, $row + 1);
210 $b->signal_connect (clicked => ($cb || sub {}));
211
212 } elsif ($type eq 'label') {
213 $table->attach_defaults (my $lbl = Gtk2::Label->new ($data->[0]), 0, 1, $row, $row + 1);
214 $edwid = Gtk2::Label->new ($data->[1]);
215 $table->attach_defaults ($edwid, 1, 2, $row, $row + 1);
216
217 } else {
218 $edwid = Gtk2::Label->new ("FOO");
219 }
220}
221
90sub replace_arch_stack_layer { 222sub replace_arch_stack_layer {
91 my ($stack, $arch) = @_; 223 my ($stack, $arch) = @_;
92 224
93 my @outstack; 225 my @outstack;
94 226

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines