ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/EditAction.pm
Revision: 1.20
Committed: Thu Mar 16 23:58:10 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.19: +21 -2 lines
Log Message:
some minor improvements (also in the eval tool)

File Contents

# User Rev Content
1 elmex 1.1 package GCE::EditAction;
2    
3     =head1 NAME
4    
5     GCE::EditActions - this is the abstraction of edit actions (placing, deleting, etc.)
6    
7     =cut
8    
9     use Gtk2;
10     use Gtk2::Gdk::Keysyms;
11     use Gtk2::SimpleMenu;
12    
13     use Crossfire;
14     use Crossfire::MapWidget;
15    
16     use strict;
17    
18     sub new {
19     my $class = shift;
20     my $self = { @_ };
21     bless $self, $class;
22 elmex 1.6 $self->init;
23 elmex 1.1 return $self;
24     }
25    
26 elmex 1.10 sub name { } # a unique name for this tool (for storing it in a hash in the main window)
27    
28 elmex 1.7 sub tool_widget { if ($_[1]) { $_[0]->{widget} = $_[1] } $_[0]->{widget} }
29 elmex 1.6 sub init { }
30    
31 elmex 1.1 sub want_cursor { 1 }
32    
33     sub special_arrow { }
34    
35     # edits one tile of the map
36     sub edit_one {
37     my ($self, $x, $y) = @_;
38     # do one edition
39     }
40    
41     # edits a selection
42     sub edit_selection {
43     }
44    
45     # abstraction for edit_one and edit_selection ?
46     # takes selection if present
47     sub edit {
48 elmex 1.10 my ($self, $map, $x, $y) = @_;
49 elmex 1.1 }
50    
51     sub begin {
52 elmex 1.10 my ($self, $map, $x, $y) = @_;
53 root 1.3
54     $map->change_begin (ref $self);
55 elmex 1.1 }
56 root 1.3
57 elmex 1.1 sub end {
58 root 1.3 my ($self, $map) = @_;
59    
60     if (my $changeset = $map->change_end) {
61     splice @{ $map->{undo_stack} ||= [] },
62 root 1.4 $map->{undo_stack_pos}++, 1e6,
63 root 1.3 $changeset;
64    
65     #TODO: limit undo stack size to some preconfigured limit
66     }
67 elmex 1.1 }
68    
69 elmex 1.7 package GCE::EditAction::RadioModed;
70    
71     our @ISA = qw/GCE::EditAction/;
72    
73     sub add_mode_button {
74     my ($self, $vb, $lbl, $mode, $default) = @_;
75    
76     $vb->pack_start (my $b = Gtk2::RadioButton->new ($self->{place_radio_grp}, $lbl), 0, 1, 0);
77     unless (defined $self->{place_radio_grp}) {
78     $self->{place_radio_grp} = $b->get_group;
79    
80     unless (defined $default) {
81     $b->set_active (1);
82     $self->set_mode ($mode);
83     }
84     }
85     if ($default) {
86     $b->set_active (1);
87     $self->set_mode ($mode);
88     }
89     $b->signal_connect (clicked => sub {
90     $self->set_mode ($mode);
91     });
92     }
93    
94     sub set_mode {
95     my ($self, $mode) = @_;
96     $self->{place_mode} = $mode;
97     }
98    
99     sub get_mode {
100     my ($self) = @_;
101     $self->{place_mode}
102     }
103    
104     sub init {
105     my ($self) = @_;
106    
107     die "Implement me!!";
108    
109     # my $vb = new Gtk2::VBox;
110     # $self->_add_mode_button ($vb, "auto", "auto");
111     # $self->_add_mode_button ($vb, "top", "top");
112     # $self->_add_mode_button ($vb, "above floor", "above");
113     # $self->_add_mode_button ($vb, "below floor", "below");
114     # $self->_add_mode_button ($vb, "bottom", "bottom");
115     #
116     # $self->{widget} = $vb;
117     }
118 elmex 1.1
119     package GCE::EditAction::Pick;
120 elmex 1.7 use strict;
121 elmex 1.1
122     our @ISA = qw/GCE::EditAction/;
123    
124 elmex 1.10 sub name { 'pick' }
125    
126 elmex 1.7 sub want_cursor { 0 }
127    
128 elmex 1.1 sub special_arrow { 'GDK_HAND2' }
129    
130     sub begin {
131 elmex 1.10 my ($self, $map, $x, $y) = @_;
132 elmex 1.1
133 elmex 1.10 $self->SUPER::begin ($map, $x, $y);
134     $self->edit ($map, $x, $y);
135 elmex 1.1 }
136    
137     sub edit {
138 elmex 1.10 my ($self, $map, $x, $y) = @_;
139 elmex 1.1
140     my $cstack = $map->get ($x, $y);
141    
142     my $arch = $cstack->[-1];
143    
144     # virtual... grmbl....
145     # FIXME: I have to patch the stack of the real arch??? argl.. how??
146     if ($arch->{_virtual}) {
147 root 1.3 $x = $arch->{virtual_x};
148     $y = $arch->{virtual_y};
149 elmex 1.1 $arch = $arch->{_virtual};
150 elmex 1.6 $cstack = $map->get ($x, $y);
151 elmex 1.1 }
152    
153 root 1.5 $::MAINWIN->set_pick ($arch)
154 elmex 1.10 if @$cstack;
155 elmex 1.1
156 root 1.5 $::MAINWIN->update_attr_editor ($arch, sub {
157 elmex 1.8 $map->change_begin (ref $self);
158 root 1.3 $map->change_stack ($x, $y, $cstack);
159 elmex 1.8 # XXX: Put this into a generic function!!! See also EditTools.pm
160     # FIXME: Fix the automatic update on undo here!
161     if (my $changeset = $map->change_end) {
162     splice @{ $map->{undo_stack} ||= [] },
163     $map->{undo_stack_pos}++, 1e6,
164     $changeset;
165     }
166 elmex 1.1 });
167    
168 root 1.5 $::MAINWIN->update_stack_view ($map, $x, $y);
169 elmex 1.1 }
170    
171 elmex 1.13 package GCE::EditAction::Perl;
172    
173     use GCE::Util;
174     use Gtk2;
175     use strict;
176    
177     our @ISA = qw/GCE::EditAction/;
178    
179     sub name { 'perl' }
180    
181 elmex 1.15 sub special_arrow { 'GDK_HEART' }
182    
183 elmex 1.13 sub init {
184     my ($self) = @_;
185    
186     my $vb = new Gtk2::VBox;
187 elmex 1.20 $vb->pack_start (my $combo = Gtk2::ComboBox->new_text, 0, 1, 0);
188 elmex 1.13 $vb->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
189     $sw->add ($self->{txt} = Gtk2::TextView->new);
190    
191 elmex 1.20 my $code = {};
192     for (['unique floor' => 'my $i = 0; for (@$os) { $_->{is_floor} and $as->[$i]->{unique} = 1; $i++ }']) {
193     $combo->append_text ($_->[0]);
194     $code->{$_->[0]} = $_->[1];
195     }
196    
197     $combo->signal_connect (changed => sub {
198     my ($combo) = @_;
199     my $str = $combo->get_active_text;
200     $self->{txt}->get_buffer->set_text ($code->{$str});
201     });
202    
203 elmex 1.13 $self->tool_widget ($vb);
204     }
205    
206     sub want_cursor { 0 }
207    
208     sub begin {
209     my ($self, $map, $x, $y) = @_;
210    
211     $self->SUPER::begin ($map, $x, $y);
212     $self->edit ($map, $x, $y);
213     }
214    
215     sub edit {
216     my ($self, $map, $x, $y) = @_;
217    
218     my $pick = $::MAINWIN->get_pick;
219     my $as = $map->get ($x, $y);
220    
221     $as = $self->eval ($map, $pick, $as, $x, $y);
222     $map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch));
223     }
224    
225     sub eval {
226 elmex 1.20 my ($self, $map, $pick, $as, $x, $y) = @_;
227 elmex 1.13 my $buf = $self->{txt}->get_buffer;
228     my $code = $buf->get_text ($buf->get_start_iter, $buf->get_end_iter, 0);
229 elmex 1.20 my $f_idx = stack_find_floor ($as, 'from_top');
230     my $w_idx = stack_find_wall ($as, 'from_top');
231     my $os = [ map { $Crossfire::ARCH{$_->{_name}} } @$as ];
232    
233     unless (arch_is_floor ($as->[$f_idx])) { $f_idx = undef; }
234     unless (arch_is_floor ($as->[$w_idx])) { $w_idx = undef; }
235 elmex 1.13
236     eval $code;
237 elmex 1.20 return $as;
238 elmex 1.13 }
239    
240 elmex 1.18 package GCE::EditAction::FollowExit;
241     use Storable qw/dclone/;
242     use File::Spec::Functions;
243     use GCE::Util;
244     use Gtk2;
245     use strict;
246    
247     our @ISA = qw/GCE::EditAction/;
248    
249     sub name { 'place' }
250    
251     sub init {
252     my ($self) = @_;
253    
254     my $vb = new Gtk2::VBox;
255    
256     $self->tool_widget ($vb);
257     }
258    
259     sub want_cursor { 0 }
260    
261     sub begin {
262     my ($self, $map, $x, $y) = @_;
263    
264     # $self->SUPER::begin ($map, $x, $y);
265     $self->edit ($map, $x, $y);
266     }
267    
268     sub edit {
269     my ($self, $map, $x, $y) = @_;
270    
271     my $as = $map->get ($x, $y);
272    
273     my $exit;
274     for (@$as) {
275     my $a = $Crossfire::ARCH{$_->{_name}};
276     if ($a->{type} eq '66') {
277     $exit = $_;
278     }
279     }
280    
281     if ($exit) {
282     my $dest = $exit->{slaying};
283     my $dir;
284     if (File::Spec->file_name_is_absolute($dest)) {
285     $dir = catdir ($Crossfire::LIB, 'maps', $dest);
286     } else {
287     $dir = File::Spec->rel2abs ($dest, catdir ($Crossfire::LIB, 'maps'));
288     }
289     $::MAINWIN->open_map_editor ($dir);
290     }
291     }
292    
293     sub end {
294     my ($self, $map, $x, $y, $mape) = @_;
295     # $::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y);
296     #$self->SUPER::end ($map, $x, $y, $mape);
297     }
298    
299 elmex 1.1 package GCE::EditAction::Place;
300 elmex 1.14 use Storable qw/dclone/;
301 elmex 1.2 use GCE::Util;
302 elmex 1.6 use Gtk2;
303     use strict;
304 elmex 1.2
305 elmex 1.7 our @ISA = qw/GCE::EditAction::RadioModed/;
306 elmex 1.6
307 elmex 1.10 sub name { 'place' }
308    
309 elmex 1.6 sub init {
310     my ($self) = @_;
311    
312     my $vb = new Gtk2::VBox;
313 elmex 1.7 $self->add_mode_button ($vb, "auto", "auto");
314     $self->add_mode_button ($vb, "top", "top");
315     $self->add_mode_button ($vb, "above floor", "above");
316     $self->add_mode_button ($vb, "below floor", "below");
317     $self->add_mode_button ($vb, "bottom", "bottom");
318 elmex 1.6
319 elmex 1.17 $vb->pack_start ($self->{place_clean} = Gtk2::CheckButton->new ('place clean'), 0, 1, 0);
320    
321 elmex 1.7 $self->tool_widget ($vb);
322 elmex 1.6 }
323    
324 elmex 1.1 sub want_cursor { 0 }
325    
326     sub begin {
327 elmex 1.10 my ($self, $map, $x, $y) = @_;
328 elmex 1.1
329 elmex 1.10 $self->SUPER::begin ($map, $x, $y);
330     $self->edit ($map, $x, $y);
331 elmex 1.1 }
332    
333     sub edit {
334 elmex 1.10 my ($self, $map, $x, $y) = @_;
335 elmex 1.1
336 elmex 1.10 my $pick = $::MAINWIN->get_pick;
337     my $as = $map->get ($x, $y);
338 elmex 1.1
339 elmex 1.17 if ($self->{place_clean}->get_active) {
340     $pick = { _name => $pick->{_name} };
341     }
342    
343 elmex 1.14 $self->stack_action ($as, dclone ($pick));
344 elmex 1.10 $map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch));
345 elmex 1.1 }
346    
347 elmex 1.17 sub end {
348     my ($self, $map, $x, $y, $mape) = @_;
349     $::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y);
350     $self->SUPER::end ($map, $x, $y, $mape);
351     }
352    
353 elmex 1.6 sub stack_action {
354     my ($self, $stack, $arch) = @_;
355    
356 elmex 1.7 my $m = $self->get_mode;
357 elmex 1.6
358     if ($m eq 'top') {
359     if (@$stack == 0 or $stack->[-1]->{_name} ne $arch->{_name}) {
360     push @$stack, $arch;
361     }
362    
363     } elsif ($m eq 'bottom') {
364     if (@$stack == 0 or $stack->[0]->{_name} ne $arch->{_name}) {
365     unshift @$stack, $arch;
366     }
367    
368     } elsif ($m eq 'above') {
369     my $fidx = stack_find_floor ($stack, 'from_top');
370    
371     if (@$stack == 0
372     or not ($stack->[$fidx + 1])
373     or $stack->[$fidx + 1]->{_name} ne $arch->{_name})
374     {
375     splice (@$stack, $fidx + 1, 0, $arch);
376     }
377    
378     } elsif ($m eq 'below') {
379     my $fidx = stack_find_floor ($stack, 'from_bottom');
380    
381     if (@$stack == 0
382     or $fidx == 0
383     or not ($stack->[$fidx - 1])
384     or $stack->[$fidx - 1]->{_name} ne $arch->{_name})
385     {
386     splice (@$stack, $fidx, 0, $arch);
387     }
388    
389     } elsif ($m eq 'auto') {
390     my $fidx = stack_find_floor ($stack, 'from_top');
391     my $widx = stack_find_wall ($stack);
392    
393     if (arch_is_floor ($arch)) { # we want to place a floor tile
394    
395     if (arch_is_floor ($stack->[$fidx])) { # replace
396     $stack->[$fidx] = $arch;
397    
398     } else { # insert on bottom
399     unshift @$stack, $arch;
400     }
401    
402     } elsif (arch_is_wall ($arch)) { # we want to place a wall
403    
404     if (arch_is_wall ($stack->[$widx])) { # replace
405     $stack->[$widx] = $arch;
406    
407     } else { # insert above floor
408     splice (@$stack, $fidx + 1, 0, $arch);
409     }
410    
411     } else {
412    
413     if (arch_is_wall ($stack->[$widx])) {
414 elmex 1.9 # if we have a wall above the floor, replace it with the to place item
415     $stack->[$widx] = $arch;
416 elmex 1.6 return;
417     }
418    
419     if (@$stack == 0
420     or not ($stack->[-1])
421     or $stack->[-1]->{_name} ne $arch->{_name})
422     {
423     push @$stack, $arch;
424     }
425     }
426     }
427     }
428    
429 elmex 1.12 package GCE::EditAction::Select;
430 elmex 1.11 use GCE::Util;
431     use Gtk2;
432 elmex 1.12 use Crossfire;
433     use Storable qw/dclone/;
434 elmex 1.11 use strict;
435    
436     our @ISA = qw/GCE::EditAction::RadioModed/;
437    
438 elmex 1.12 sub name { 'select' }
439 elmex 1.11
440 elmex 1.15 sub special_arrow { 'GDK_CIRCLE' }
441    
442 elmex 1.11 sub init {
443     my ($self) = @_;
444    
445     my $vb = new Gtk2::VBox;
446    
447 elmex 1.19 $vb->pack_start (my $bt = Gtk2::Button->new_with_mnemonic ("_copy"), 0, 1, 0);
448 elmex 1.12 $bt->signal_connect (clicked => sub { $self->copy });
449     $vb->pack_start ($self->{paste_top} = Gtk2::CheckButton->new ('paste on top'), 0, 1, 0);
450 elmex 1.19 $vb->pack_start (my $bt = Gtk2::Button->new_with_mnemonic ("paste (_v)"), 0, 1, 0);
451 elmex 1.12 $bt->signal_connect (clicked => sub { $self->paste });
452     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
453     $self->add_mode_button ($vb, "place", "place");
454     $self->add_mode_button ($vb, "erase", "erase");
455 elmex 1.15 $self->add_mode_button ($vb, "perl", "perl");
456 elmex 1.19 $vb->pack_start (my $bt = Gtk2::Button->new_with_mnemonic ("i_nvoke"), 0, 1, 0);
457 elmex 1.12 $bt->signal_connect (clicked => sub { $self->invoke });
458 elmex 1.11
459     $self->tool_widget ($vb);
460     }
461    
462 elmex 1.12 sub copy {
463     my ($self) = @_;
464    
465     return unless $self->{selection}->{a};
466     my ($x1, $y1) = @{$self->{selection}->{a}};
467     my ($x2, $y2) = @{$self->{selection}->{b}};
468    
469     if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) }
470     if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) }
471    
472     my $map = $self->{selection}->{map};
473    
474     $self->{copy_coords} = [$x1, $y1, $x2, $y2];
475     $self->{copy};
476     for (my $x = $x1; $x <= $x2; $x++) {
477     for (my $y = $y1; $y <= $y2; $y++) {
478     $self->{copy}->[$x - $x1]->[$y - $y1] = $map->get ($x, $y);
479     }
480     }
481     }
482    
483     sub paste {
484     my ($self, $xp, $yp) = @_;
485    
486     return unless $self->{selection}->{a};
487    
488     my ($x1, $y1);
489    
490 elmex 1.15 if (defined $xp) {
491 elmex 1.12 ($x1, $y1) = ($xp, $yp);
492     } else {
493     ($x1, $y1) = @{$self->{selection}->{a}};
494     }
495    
496     my $map = $self->{selection}->{map};
497    
498     my $p_o_top = $self->{paste_top}->get_active * 1;
499    
500     my $w = $self->{copy_coords}->[2] - $self->{copy_coords}->[0];
501     my $h = $self->{copy_coords}->[3] - $self->{copy_coords}->[1];
502     $self->{copy};
503     $self->SUPER::begin ($map, $x1, $y1);
504     for (my $x = $x1; $x <= ($x1 + $w); $x++) {
505     for (my $y = $y1; $y <= ($y1 + $h); $y++) {
506     my $cstck = $map->get ($x, $y);
507    
508     if ($p_o_top) {
509     push @$cstck, @{dclone ($self->{copy}->[$x - $x1]->[$y - $y1] || [])};
510     $map->change_stack ($x, $y, $cstck);
511     } else {
512     $map->change_stack ($x, $y, dclone ($self->{copy}->[$x - $x1]->[$y - $y1] || []));
513     }
514     }
515     }
516     $self->SUPER::end ($map);
517     $map->invalidate_all;
518     }
519    
520     sub invoke {
521     my ($self) = @_;
522    
523     return unless $self->{selection}->{a};
524     my ($x1, $y1) = @{$self->{selection}->{a}};
525     my ($x2, $y2) = @{$self->{selection}->{b}};
526    
527     if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) }
528     if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) }
529    
530     my $map = $self->{selection}->{map};
531    
532     my $m = $self->get_mode;
533     $self->SUPER::begin ($map, $x1, $y1);
534     for (my $x = $x1; $x <= $x2; $x++) {
535     for (my $y = $y1; $y <= $y2; $y++) {
536     if ($m eq 'place') {
537     $::MAINWIN->{edit_collection}{place}->edit ($map, $x, $y);
538     } elsif ($m eq 'erase') {
539     $::MAINWIN->{edit_collection}{erase}->edit ($map, $x, $y);
540 elmex 1.15 } elsif ($m eq 'perl') {
541     $::MAINWIN->{edit_collection}{perl}->edit ($map, $x, $y);
542 elmex 1.12 }
543     }
544     }
545     $self->SUPER::end ($map);
546     }
547    
548 elmex 1.11 sub want_cursor { 0 }
549    
550     sub begin {
551     my ($self, $map, $x, $y) = @_;
552    
553     delete $self->{selection};
554    
555 elmex 1.12 $self->{selection}->{a} = [$x, $y];
556 elmex 1.11 $self->edit ($map, $x, $y);
557 elmex 1.12 }
558 elmex 1.11
559 elmex 1.12 sub end {
560 elmex 1.11 }
561    
562     sub edit {
563     my ($self, $map, $x, $y) = @_;
564    
565     $self->{selection}->{b} = [$x, $y];
566 elmex 1.12 $self->{selection}->{map} = $map;
567 elmex 1.11 $self->update_overlay ($map);
568     }
569    
570     sub update_overlay {
571     my ($self, $map) = @_;
572    
573 elmex 1.12 my ($x1, $y1) = @{$self->{selection}->{a}};
574     my ($x2, $y2) = @{$self->{selection}->{b}};
575    
576     if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) }
577     if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) }
578 elmex 1.11
579 elmex 1.12 my $w = ($x2 - $x1) + 1;
580     my $h = ($y2 - $y1) + 1;
581    
582     $map->overlay (selection =>
583     $x1 * TILESIZE, $y1 * TILESIZE,
584     $w * TILESIZE, $h * TILESIZE,
585     sub {
586     my ($self, $x, $y) = @_;
587     $self->{window}->draw_rectangle (
588     $_ & 1 ? $self->style->black_gc : $self->style->white_gc,
589     0,
590     $x + $_, $y + $_,
591     ($w * TILESIZE) - 1 - $_ * 2,
592     ($h * TILESIZE) - 1 - $_ * 2
593     ) for 0..3;
594     }
595     );
596 elmex 1.11 }
597    
598 elmex 1.1 package GCE::EditAction::Erase;
599 elmex 1.7 use GCE::Util;
600     use Gtk2;
601     use strict;
602 elmex 1.1
603 elmex 1.7 our @ISA = qw/GCE::EditAction::RadioModed/;
604 elmex 1.1
605 elmex 1.10 sub name { 'erase' }
606    
607 elmex 1.1 sub want_cursor { 0 }
608    
609 elmex 1.15 sub special_arrow { 'GDK_DIAMOND_CROSS' }
610 elmex 1.10
611 elmex 1.7 sub init {
612     my ($self) = @_;
613    
614     my $vb = new Gtk2::VBox;
615     $self->add_mode_button ($vb, "top", "top");
616     $self->add_mode_button ($vb, "walls", "walls");
617     $self->add_mode_button ($vb, "above floor", "above", 'default');
618     $self->add_mode_button ($vb, "floor", "floor");
619     $self->add_mode_button ($vb, "below floor", "below");
620     $self->add_mode_button ($vb, "bottom", "bottom");
621     $self->add_mode_button ($vb, "pick match", "match");
622    
623     $self->tool_widget ($vb);
624    
625     $vb->pack_start ($self->{no_wall_check} = Gtk2::CheckButton->new ("exclude walls"), 0, 1, 0);
626     $vb->pack_start ($self->{no_monsters_check} = Gtk2::CheckButton->new ("exclude monsters"), 0, 1, 0);
627     }
628    
629     sub check_excluded {
630     my ($self, $arch) = @_;
631    
632     my $a1 = $self->{no_monsters_check}->get_active;
633     my $a2 = $self->{no_wall_check}->get_active;
634    
635     my $r = ($self->{no_wall_check}->get_active && arch_is_wall ($arch))
636     || ($self->{no_monsters_check}->get_active && arch_is_monster ($arch));
637     return $r;
638     }
639    
640 elmex 1.1 sub begin {
641 elmex 1.10 my ($self, $map, $x, $y) = @_;
642 elmex 1.1
643 elmex 1.10 $self->SUPER::begin ($map, $x, $y);
644     $self->edit ($map, $x, $y);
645 elmex 1.1 }
646    
647     sub edit {
648 elmex 1.10 my ($self, $map, $x, $y) = @_;
649 elmex 1.1
650     my $as = $map->get ($x, $y);
651 elmex 1.7 $self->stack_action ($as);
652 root 1.3 $map->change_stack ($x, $y, $as);
653 elmex 1.1 }
654    
655 elmex 1.7 sub stack_action {
656     my ($self, $stack) = @_;
657    
658     my $m = $self->get_mode;
659    
660     if ($m eq 'top') {
661     pop @$stack;
662    
663     } elsif ($m eq 'bottom') {
664     shift @$stack;
665    
666     } elsif ($m eq 'above') {
667     my $fidx = stack_find_floor ($stack, 'from_top');
668    
669     if (arch_is_floor ($stack->[$fidx]) and $stack->[$fidx + 1]) {
670     splice (@$stack, $fidx + 1, 1)
671     unless $self->check_excluded ($stack->[$fidx + 1])
672    
673     } elsif (not arch_is_floor ($stack->[$fidx])) {
674     splice (@$stack, $fidx, 1)
675     unless $self->check_excluded ($stack->[$fidx])
676    
677     }
678    
679     } elsif ($m eq 'below') {
680     my $fidx = stack_find_floor ($stack, 'from_bottom');
681    
682     if ($fidx > 0 and not arch_is_floor ($stack->[$fidx - 1])) {
683     splice (@$stack, $fidx - 1, 1)
684     unless $self->check_excluded ($stack->[$fidx - 1])
685    
686     } elsif (not arch_is_floor ($stack->[$fidx])) { # no floor found
687     splice (@$stack, $fidx, 1)
688     unless $self->check_excluded ($stack->[$fidx])
689    
690     }
691    
692     } elsif ($m eq 'walls') {
693     my $widx = stack_find_wall ($stack, 'from_top');
694    
695     while (arch_is_wall ($stack->[$widx])) {
696     splice (@$stack, $widx, 1);
697     $widx = stack_find_wall ($stack, 'from_top')
698     }
699    
700     } elsif ($m eq 'floor') {
701     my $fidx = stack_find_floor ($stack, 'from_top');
702    
703     while (arch_is_floor ($stack->[$fidx])) {
704     splice (@$stack, $fidx, 1);
705     $fidx = stack_find_floor ($stack, 'from_top')
706     }
707    
708     } elsif ($m eq 'match') {
709     my $pick_name = $::MAINWIN->get_pick ()->{_name};
710     my $idx = stack_find ($stack, 'from_top', sub { $_[0]->{_name} eq $pick_name });
711    
712     while ($stack->[$idx] and $stack->[$idx]->{_name} eq $pick_name) {
713     splice (@$stack, $idx, 1);
714     $idx = stack_find ($stack, 'from_top', sub { $_[0]->{_name} eq $pick_name });
715     }
716     }
717     }
718    
719 elmex 1.18 package GCE::EditAction::ConnectExit;
720 elmex 1.16 use Storable qw/dclone/;
721     use GCE::Util;
722     use Gtk2;
723     use File::Spec::Functions;
724     use strict;
725    
726     our @ISA = qw/GCE::EditAction::RadioModed/;
727    
728 elmex 1.18 sub name { 'connectexit' }
729 elmex 1.16
730     sub init {
731     my ($self) = @_;
732    
733     my $vb = new Gtk2::VBox;
734     $self->add_mode_button ($vb, "exit", "exit");
735     # $self->add_mode_button ($vb, "triggers", "trig");
736 elmex 1.18 $vb->pack_start ($self->{sel_edt} = Gtk2::Entry->new, 0, 1, 0);
737     $self->{sel_edt}->set_text (catfile ($Crossfire::LIB, 'maps'));
738    
739 elmex 1.16 $vb->pack_start ($self->{sel_lbl} = Gtk2::Label->new, 0, 0, 0);
740     $self->tool_widget ($vb);
741     }
742    
743     sub want_cursor { 0 }
744    
745     sub begin {
746     my ($self, $map, $x, $y, $mapedit) = @_;
747    
748     $self->edit ($map, $x, $y, $mapedit);
749     }
750    
751     sub edit {
752     my ($self, $map, $x, $y, $mapedit) = @_;
753    
754     my $pick = $::MAINWIN->get_pick;
755     my $as = $map->get ($x, $y);
756    
757     my $exit;
758     for (@$as) {
759     my $a = $Crossfire::ARCH{$_->{_name}};
760     if ($a->{type} eq '66') {
761     $exit = $_;
762     }
763     }
764    
765 elmex 1.18 my $ent = $self->{sel_edt}->get_text;
766 elmex 1.16 if ($exit) {
767     if ($self->{sel_exit}) {
768    
769     $exit->{hp} = $self->{sel_exit}->[3];
770     $exit->{sp} = $self->{sel_exit}->[4];
771     $exit->{slaying} = File::Spec->abs2rel ($self->{sel_exit}->[5], $ent);
772    
773     my $exit2 = $self->{sel_exit}->[0];
774    
775     $exit2->{hp} = $x;
776     $exit2->{sp} = $y;
777     $exit2->{slaying} = File::Spec->abs2rel ($mapedit->{path}, $ent);
778    
779     unless ($exit2->{slaying} =~ m/^\.\./) {
780     $exit2->{slaying} = '/' . $exit2->{slaying};
781     }
782     unless ($exit->{slaying} =~ m/^\.\./) {
783     $exit->{slaying} = '/' . $exit->{slaying};
784     }
785    
786     $self->SUPER::begin ($map, $x, $y, $mapedit);
787     $map->change_stack ($x, $y, $as);
788     $self->SUPER::end ($map);
789     $self->SUPER::begin ($self->{sel_exit}->[1], $exit->{hp}, $exit->{sp});
790     $self->{sel_exit}->[1]->change_stack ($exit->{hp}, $exit->{sp}, $self->{sel_exit}->[2]);
791     $self->SUPER::end ($self->{sel_exit}->[1]);
792    
793     quick_msg ($mapedit, "$exit->{slaying} ($x:$y) $exit->{_name} <=> $exit2->{slaying} ($exit2->{hp}:$exit2->{sp}) $exit2->{_name}", 0);
794    
795     $::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y);
796    
797     $self->{sel_exit} = undef;
798     $self->{sel_lbl}->set_text ('');
799     } else {
800     $self->{sel_lbl}->set_text ("src: ($x:$y) $exit->{_name}");
801     $self->{sel_exit} = [$exit, $map, $as, $x, $y, $mapedit->{path}];
802     }
803     } else {
804 elmex 1.18 if ($self->{sel_exit}) {
805    
806     my $exit2 = $self->{sel_exit}->[0];
807    
808     $exit2->{hp} = $x;
809     $exit2->{sp} = $y;
810     $exit2->{slaying} = File::Spec->abs2rel ($mapedit->{path}, $ent);
811     print "TO: $x:$y => $exit2->{slaying}\n";
812    
813     unless ($exit2->{slaying} =~ m/^\.\./) {
814     $exit2->{slaying} = '/' . $exit2->{slaying};
815     }
816    
817     $self->SUPER::begin ($self->{sel_exit}->[1], $exit->{hp}, $exit->{sp});
818     $self->{sel_exit}->[1]->change_stack ($self->{sel_exit}->[3], $self->{sel_exit}->[4], $self->{sel_exit}->[2]);
819     $self->SUPER::end ($self->{sel_exit}->[1]);
820    
821     quick_msg ($mapedit, "$self->{sel_exit}->[5] ($self->{sel_exit}->[3]:$self->{sel_exit}->[4]) $self->{sel_exit}->[0]->{_name} => $exit2->{slaying} ($x:$y) $exit2->{_name}", 0);
822    
823     $::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y);
824    
825     $self->{sel_exit} = undef;
826     $self->{sel_lbl}->set_text ('');
827     } else {
828     quick_msg ($mapedit, "no exit object found");
829     }
830 elmex 1.16 }
831    
832     # $self->stack_action ($as, dclone ($pick));
833     #$map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch));
834     }
835    
836     sub end {}
837 elmex 1.7
838    
839 elmex 1.1 =head1 AUTHOR
840    
841     Marc Lehmann <schmorp@schmorp.de>
842     http://home.schmorp.de/
843    
844     Robin Redeker <elmex@ta-sa.org>
845     http://www.ta-sa.org/
846    
847     =cut
848 root 1.5
849     1
850 elmex 1.1