ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/EditAction.pm
Revision: 1.31
Committed: Sat Apr 1 20:16:18 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.30: +0 -4 lines
Log Message:
place_clean removed

File Contents

# Content
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 $self->init;
23 return $self;
24 }
25
26 sub name { } # a unique name for this tool (for storing it in a hash in the main window)
27
28 sub tool_widget { if ($_[1]) { $_[0]->{widget} = $_[1] } $_[0]->{widget} }
29 sub init { }
30
31 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 my ($self, $map, $x, $y) = @_;
49 }
50
51 sub begin {
52 my ($self, $map, $x, $y) = @_;
53
54 $map->change_begin (ref $self);
55 }
56
57 sub end {
58 my ($self, $map) = @_;
59
60 if (my $changeset = $map->change_end) {
61 splice @{ $map->{undo_stack} ||= [] },
62 $map->{undo_stack_pos}++, 1e6,
63 $changeset;
64
65 #TODO: limit undo stack size to some preconfigured limit
66 }
67 }
68
69 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
119 package GCE::EditAction::Pick;
120 use strict;
121
122 our @ISA = qw/GCE::EditAction::RadioModed/;
123
124 sub name { 'pick' }
125
126 sub want_cursor { 0 }
127
128 sub special_arrow { 'GDK_HAND2' }
129
130 sub init {
131 my ($self) = @_;
132
133 my $vb = new Gtk2::VBox;
134 $vb->pack_start ($self->{only_attr} = Gtk2::CheckButton->new ('pick attr'), 0, 1, 0);
135 $self->{widget} = $vb;
136 }
137
138 sub begin {
139 my ($self, $map, $x, $y) = @_;
140 $self->edit ($map, $x, $y);
141 }
142
143 sub edit {
144 my ($self, $map, $x, $y) = @_;
145
146 $::MAINWIN->update_stack_view ($map, $x, $y);
147
148 my $cstack = $map->get ($x, $y);
149
150 my $arch = $cstack->[-1];
151
152 # virtual... grmbl....
153 # FIXME: I have to patch the stack of the real arch??? argl.. how??
154 if ($arch->{_virtual}) {
155 $x = $arch->{virtual_x};
156 $y = $arch->{virtual_y};
157 $arch = $arch->{_virtual};
158 $cstack = $map->get ($x, $y);
159 }
160
161 unless ($self->{only_attr}->get_active) {
162 $::MAINWIN->set_pick ($arch)
163 if @$cstack;
164 }
165
166 $::MAINWIN->update_attr_editor ($arch, sub {
167 $map->change_begin (ref $self);
168 $map->change_stack ($x, $y, $cstack);
169 # XXX: Put this into a generic function!!! See also EditTools.pm
170 # FIXME: Fix the automatic update on undo here!
171 if (my $changeset = $map->change_end) {
172 splice @{ $map->{undo_stack} ||= [] },
173 $map->{undo_stack_pos}++, 1e6,
174 $changeset;
175 }
176 });
177 }
178
179 sub end {}
180
181 package GCE::EditAction::Perl;
182
183 use GCE::Util;
184 use Gtk2;
185 use strict;
186
187 our @ISA = qw/GCE::EditAction/;
188
189 sub name { 'perl' }
190
191 sub special_arrow { 'GDK_HEART' }
192
193 sub init {
194 my ($self) = @_;
195
196 my $vb = new Gtk2::VBox;
197 $vb->pack_start (my $combo = Gtk2::ComboBox->new_text, 0, 1, 0);
198 $vb->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
199 $sw->add ($self->{txt} = Gtk2::TextView->new);
200
201 my $code = {};
202 for (['unique floor' => 'my $i = 0; for (@$os) { $_->{is_floor} and $as->[$i]->{unique} = 1; $i++ }']) {
203 $combo->append_text ($_->[0]);
204 $code->{$_->[0]} = $_->[1];
205 }
206
207 $combo->signal_connect (changed => sub {
208 my ($combo) = @_;
209 my $str = $combo->get_active_text;
210 $self->{txt}->get_buffer->set_text ($code->{$str});
211 });
212
213 $self->tool_widget ($vb);
214 }
215
216 sub want_cursor { 0 }
217
218 sub begin {
219 my ($self, $map, $x, $y) = @_;
220
221 $self->SUPER::begin ($map, $x, $y);
222 $self->edit ($map, $x, $y);
223 }
224
225 sub edit {
226 my ($self, $map, $x, $y) = @_;
227
228 my $pick = $::MAINWIN->get_pick;
229 my $as = $map->get ($x, $y);
230
231 $as = $self->eval ($map, $pick, $as, $x, $y);
232 $map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch));
233 }
234
235 sub eval {
236 my ($self, $map, $pick, $as, $x, $y) = @_;
237 my $buf = $self->{txt}->get_buffer;
238 my $code = $buf->get_text ($buf->get_start_iter, $buf->get_end_iter, 0);
239 my $f_idx = stack_find_floor ($as, 'from_top');
240 my $w_idx = stack_find_wall ($as, 'from_top');
241 my $os = [ map { $Crossfire::ARCH{$_->{_name}} } @$as ];
242
243 unless (arch_is_floor ($as->[$f_idx])) { $f_idx = undef; }
244 unless (arch_is_floor ($as->[$w_idx])) { $w_idx = undef; }
245
246 eval $code;
247 return $as;
248 }
249
250 package GCE::EditAction::FollowExit;
251 use Storable qw/dclone/;
252 use File::Spec::Functions;
253 use GCE::Util;
254 use Gtk2;
255 use strict;
256
257 our @ISA = qw/GCE::EditAction/;
258
259 sub name { 'place' }
260
261 sub init {
262 my ($self) = @_;
263
264 my $vb = new Gtk2::VBox;
265
266 $self->tool_widget ($vb);
267 }
268
269 sub want_cursor { 0 }
270
271 sub begin {
272 my ($self, $map, $x, $y, $mape) = @_;
273
274 # $self->SUPER::begin ($map, $x, $y);
275 $self->edit ($map, $x, $y, $mape);
276 }
277
278 sub edit {
279 my ($self, $map, $x, $y, $mape) = @_;
280
281 my $as = $map->get ($x, $y);
282
283 my $exit;
284 for my $arch (@$as) {
285 if ($arch->{_virtual}) {
286 $arch = $arch->{_virtual};
287 }
288 if (arch_is_exit ($arch)) {
289 $exit = $arch;
290 }
291 }
292
293 if ($exit) {
294 my $dest = map2abs ($exit->{slaying}, $mape);
295 $::MAINWIN->open_map_editor ($dest);
296 }
297 }
298
299 sub end {
300 my ($self, $map, $x, $y, $mape) = @_;
301 # $::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y);
302 #$self->SUPER::end ($map, $x, $y, $mape);
303 }
304
305 package GCE::EditAction::Place;
306
307 use Storable qw/dclone/;
308 use GCE::Util;
309 use Gtk2;
310 use strict;
311
312 our @ISA = qw/GCE::EditAction::RadioModed/;
313
314 sub name { 'place' }
315
316 sub init {
317 my ($self) = @_;
318
319 my $vb = new Gtk2::VBox;
320
321 $self->add_mode_button ($vb, "auto", "auto");
322 $self->add_mode_button ($vb, "top", "top");
323 $self->add_mode_button ($vb, "above floor", "above");
324 $self->add_mode_button ($vb, "below floor", "below");
325 $self->add_mode_button ($vb, "bottom", "bottom");
326
327 $self->tool_widget ($vb);
328 }
329
330 sub want_cursor { 0 }
331
332 sub begin {
333 my ($self, $map, $x, $y) = @_;
334
335 $self->SUPER::begin ($map, $x, $y);
336 $self->edit ($map, $x, $y);
337 }
338
339 # 1 up 2 right 4 down 8 left
340 my @join_ext = (
341 "0", # 0
342 "1_2", # 1
343 "1_4", # 2
344 "2_2_1", # 3
345 "1_1", # 4
346 "2_1_1", # 5
347 "2_2_2", # 6
348 "3_2", # 7
349 "1_3", # 8
350 "2_2_4", # 9
351 "2_1_2", # 10
352 "3_1", # 11
353 "2_2_3", # 12
354 "3_4", # 13
355 "3_3", # 14
356 "4", # 15
357 );
358
359 sub autojoin {
360 my ($map, $pick, $x1, $y1, $x2, $y2) = @_;
361
362 my $dx = $x2 - $x1;
363 my $dy = $y2 - $y1;
364
365 my $dir = $dy ? ($dy == -1 ? 1 : $dy == 1 ? 4 : return)
366 : ($dx == -1 ? 8 : $dx == 1 ? 2 : $dx == 0 ? 0 : return);
367
368 my $as = $map->get ($x1, $y1);
369
370 (my $base = $pick->{_name}) =~ s/_0$//;
371
372 for my $idx (0 .. $#$as) {
373 my $arch = $as->[$idx];
374 for my $dirs (0..15) {
375 my $name = $arch->{_name};
376
377 if ($arch->{_name} eq "$base\_$join_ext[$dirs]") {
378 $dirs |= $dir;
379
380 my $name = "$base\_$join_ext[$dirs]";
381
382 if ($Crossfire::ARCH{$name}) {
383 %$arch = ( _name => $name );
384 $map->change_stack ($x1, $y1, $as);
385
386 return 1;
387 }
388 }
389 }
390 }
391
392 return 0;
393 }
394
395 sub edit {
396 my ($self, $map, $x, $y) = @_;
397
398 my $pick = $::MAINWIN->get_pick;
399 my $as = $map->get ($x, $y);
400
401 my $autojoin = $pick->{_name} =~ /_0$/
402 && $self->get_mode eq "auto";
403
404 autojoin $map, $pick, @{$self->{last_pos}}, $x, $y
405 if $autojoin && $self->{last_pos};
406
407 if (!$autojoin
408 || !($self->{last_pos} ? autojoin $map, $pick, $x, $y, @{$self->{last_pos}},
409 : autojoin $map, $pick, $x, $y, $x, $y)) {
410 $self->stack_action ($as, dclone $pick);
411 $map->change_stack ($x, $y, $as);
412 autojoin $map, $pick, $x, $y, @{$self->{last_pos}}
413 if $autojoin && $self->{last_pos};
414 }
415
416 $self->{last_pos} = [$x, $y];
417 }
418
419 sub end {
420 my ($self, $map, $x, $y, $mape) = @_;
421 # $::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y);
422 # now actualize stack and attr editor
423 $::MAINWIN->update_stack_view ($map, $x, $y);
424
425 my $cstack = $map->get ($x, $y);
426
427 my $arch = $cstack->[-1];
428
429 delete $self->{last_pos};
430
431 # virtual... grmbl....
432 # FIXME: I have to patch the stack of the real arch??? argl.. how??
433 if ($arch->{_virtual}) {
434 $x = $arch->{virtual_x};
435 $y = $arch->{virtual_y};
436 $arch = $arch->{_virtual};
437 $cstack = $map->get ($x, $y);
438 }
439
440
441 $::MAINWIN->update_attr_editor ($arch, sub {
442 $map->change_begin (ref $self);
443 $map->change_stack ($x, $y, $cstack);
444 # XXX: Put this into a generic function!!! See also EditTools.pm
445 # FIXME: Fix the automatic update on undo here!
446 if (my $changeset = $map->change_end) {
447 splice @{ $map->{undo_stack} ||= [] },
448 $map->{undo_stack_pos}++, 1e6,
449 $changeset;
450 }
451 });
452
453
454 $self->SUPER::end ($map, $x, $y, $mape);
455 }
456
457 sub stack_action {
458 my ($self, $stack, $arch) = @_;
459
460 my $m = $self->get_mode;
461
462 if ($m eq 'top') {
463 if (@$stack == 0 or $stack->[-1]->{_name} ne $arch->{_name}) {
464 push @$stack, $arch;
465 }
466
467 } elsif ($m eq 'bottom') {
468 if (@$stack == 0 or $stack->[0]->{_name} ne $arch->{_name}) {
469 unshift @$stack, $arch;
470 }
471
472 } elsif ($m eq 'above') {
473 my $fidx = stack_find_floor ($stack, 'from_top');
474
475 if (@$stack == 0
476 or not ($stack->[$fidx + 1])
477 or $stack->[$fidx + 1]->{_name} ne $arch->{_name})
478 {
479 splice (@$stack, $fidx + 1, 0, $arch);
480 }
481
482 } elsif ($m eq 'below') {
483 my $fidx = stack_find_floor ($stack, 'from_bottom');
484
485 if (@$stack == 0
486 or $fidx == 0
487 or not ($stack->[$fidx - 1])
488 or $stack->[$fidx - 1]->{_name} ne $arch->{_name})
489 {
490 splice (@$stack, $fidx, 0, $arch);
491 }
492
493 } elsif ($m eq 'auto') {
494 my $fidx = stack_find_floor ($stack, 'from_top');
495 my $widx = stack_find_wall ($stack);
496
497 if (arch_is_floor ($arch)) { # we want to place a floor tile
498
499 if (arch_is_floor ($stack->[$fidx])) { # replace
500 $stack->[$fidx] = $arch;
501
502 } else { # insert on bottom
503 unshift @$stack, $arch;
504 }
505
506 } elsif (arch_is_wall ($arch)) { # we want to place a wall
507
508 if (arch_is_wall ($stack->[$widx])) { # replace
509 $stack->[$widx] = $arch;
510
511 } else { # insert above floor
512 splice (@$stack, $fidx + 1, 0, $arch);
513 }
514
515 } else {
516
517 if (arch_is_wall ($stack->[$widx])) {
518 # if we have a wall above the floor, replace it with the to place item
519 $stack->[$widx] = $arch;
520 return;
521 }
522
523 if (@$stack == 0
524 or not ($stack->[-1])
525 or $stack->[-1]->{_name} ne $arch->{_name})
526 {
527 push @$stack, $arch;
528 }
529 }
530 }
531 }
532
533 package GCE::EditAction::Select;
534 use GCE::Util;
535 use Gtk2;
536 use Crossfire;
537 use Storable qw/dclone/;
538 use strict;
539
540 our @ISA = qw/GCE::EditAction::RadioModed/;
541
542 sub name { 'select' }
543
544 sub special_arrow { 'GDK_CIRCLE' }
545
546 sub init {
547 my ($self) = @_;
548
549 my $vb = new Gtk2::VBox;
550
551 $vb->pack_start (my $bt = Gtk2::Button->new_with_mnemonic ("_copy"), 0, 1, 0);
552 $bt->signal_connect (clicked => sub { $self->copy });
553 $vb->pack_start ($self->{paste_top} = Gtk2::CheckButton->new ('paste on top'), 0, 1, 0);
554 $vb->pack_start (my $bt = Gtk2::Button->new_with_mnemonic ("paste (_v)"), 0, 1, 0);
555 $bt->signal_connect (clicked => sub { $self->paste });
556 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
557 $self->add_mode_button ($vb, "place", "place");
558 $self->add_mode_button ($vb, "erase", "erase");
559 $self->add_mode_button ($vb, "perl", "perl");
560 $vb->pack_start (my $bt = Gtk2::Button->new_with_mnemonic ("i_nvoke"), 0, 1, 0);
561 $bt->signal_connect (clicked => sub { $self->invoke });
562
563 $self->tool_widget ($vb);
564 }
565
566 sub copy {
567 my ($self) = @_;
568
569 return unless $self->{selection}->{a};
570 my ($x1, $y1) = @{$self->{selection}->{a}};
571 my ($x2, $y2) = @{$self->{selection}->{b}};
572
573 if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) }
574 if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) }
575
576 my $map = $self->{selection}->{map};
577
578 $self->{copy_coords} = [$x1, $y1, $x2, $y2];
579 $self->{copy};
580 for (my $x = $x1; $x <= $x2; $x++) {
581 for (my $y = $y1; $y <= $y2; $y++) {
582 $self->{copy}->[$x - $x1]->[$y - $y1] = $map->get ($x, $y);
583 }
584 }
585 }
586
587 sub paste {
588 my ($self, $map, $xp, $yp) = @_;
589
590 return unless $self->{selection}->{a};
591
592 my ($x1, $y1);
593
594 if (defined $xp) {
595 ($x1, $y1) = ($xp, $yp);
596 } else {
597 ($x1, $y1) = @{$self->{selection}->{a}};
598 }
599
600 $map ||= $self->{selection}->{map};
601
602 my $p_o_top = $self->{paste_top}->get_active * 1;
603
604 my $w = $self->{copy_coords}->[2] - $self->{copy_coords}->[0];
605 my $h = $self->{copy_coords}->[3] - $self->{copy_coords}->[1];
606 $self->{copy};
607 $self->SUPER::begin ($map, $x1, $y1);
608 for (my $x = $x1; $x <= ($x1 + $w); $x++) {
609 for (my $y = $y1; $y <= ($y1 + $h); $y++) {
610 my $cstck = $map->get ($x, $y);
611
612 if ($p_o_top) {
613 push @$cstck, @{dclone ($self->{copy}->[$x - $x1]->[$y - $y1] || [])};
614 $map->change_stack ($x, $y, $cstck);
615 } else {
616 $map->change_stack ($x, $y, dclone ($self->{copy}->[$x - $x1]->[$y - $y1] || []));
617 }
618 }
619 }
620 $self->SUPER::end ($map);
621 $map->invalidate_all;
622 }
623
624 sub invoke {
625 my ($self) = @_;
626
627 return unless $self->{selection}->{a};
628 my ($x1, $y1) = @{$self->{selection}->{a}};
629 my ($x2, $y2) = @{$self->{selection}->{b}};
630
631 if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) }
632 if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) }
633
634 my $map = $self->{selection}->{map};
635
636 my $m = $self->get_mode;
637 $self->SUPER::begin ($map, $x1, $y1);
638 for (my $x = $x1; $x <= $x2; $x++) {
639 for (my $y = $y1; $y <= $y2; $y++) {
640 if ($m eq 'place') {
641 $::MAINWIN->{edit_collection}{place}->edit ($map, $x, $y);
642 } elsif ($m eq 'erase') {
643 $::MAINWIN->{edit_collection}{erase}->edit ($map, $x, $y);
644 } elsif ($m eq 'perl') {
645 $::MAINWIN->{edit_collection}{perl}->edit ($map, $x, $y);
646 }
647 }
648 }
649 $self->SUPER::end ($map);
650 }
651
652 sub want_cursor { 0 }
653
654 sub begin {
655 my ($self, $map, $x, $y) = @_;
656
657 if ($self->{selection}->{map}) {
658 $self->{selection}->{map}->overlay ('selection');
659 }
660 delete $self->{selection};
661
662 $self->{selection}->{a} = [$x, $y];
663 $self->edit ($map, $x, $y);
664 }
665
666 sub end {
667 }
668
669 sub edit {
670 my ($self, $map, $x, $y) = @_;
671
672 $self->{selection}->{b} = [$x, $y];
673 $self->{selection}->{map} = $map;
674 $self->update_overlay ();
675 }
676
677 sub update_overlay {
678 my ($self) = @_;
679
680 my $map = $self->{selection}->{map};
681
682 return unless (defined $self->{selection}->{a} and defined $self->{selection}->{b});
683
684 my ($x1, $y1) = @{$self->{selection}->{a}};
685 my ($x2, $y2) = @{$self->{selection}->{b}};
686
687 if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) }
688 if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) }
689
690 my $w = ($x2 - $x1) + 1;
691 my $h = ($y2 - $y1) + 1;
692
693 $map->overlay (selection =>
694 $x1 * TILESIZE, $y1 * TILESIZE,
695 $w * TILESIZE, $h * TILESIZE,
696 sub {
697 my ($self, $x, $y) = @_;
698 $self->{window}->draw_rectangle (
699 $_ & 1 ? $self->style->black_gc : $self->style->white_gc,
700 0,
701 $x + $_, $y + $_,
702 ($w * TILESIZE) - 1 - $_ * 2,
703 ($h * TILESIZE) - 1 - $_ * 2
704 ) for 0..3;
705 }
706 );
707 }
708
709 package GCE::EditAction::Erase;
710 use GCE::Util;
711 use Gtk2;
712 use strict;
713
714 our @ISA = qw/GCE::EditAction::RadioModed/;
715
716 sub name { 'erase' }
717
718 sub want_cursor { 0 }
719
720 sub special_arrow { 'GDK_DIAMOND_CROSS' }
721
722 sub init {
723 my ($self) = @_;
724
725 my $vb = new Gtk2::VBox;
726 $self->add_mode_button ($vb, "top", "top");
727 $self->add_mode_button ($vb, "walls", "walls");
728 $self->add_mode_button ($vb, "above floor", "above", 'default');
729 $self->add_mode_button ($vb, "floor", "floor");
730 $self->add_mode_button ($vb, "below floor", "below");
731 $self->add_mode_button ($vb, "bottom", "bottom");
732 $self->add_mode_button ($vb, "pick match", "match");
733
734 $self->tool_widget ($vb);
735
736 $vb->pack_start ($self->{no_wall_check} = Gtk2::CheckButton->new ("protect walls"), 0, 1, 0);
737 $vb->pack_start ($self->{no_monsters_check} = Gtk2::CheckButton->new ("protect monsters"), 0, 1, 0);
738 }
739
740 sub check_excluded {
741 my ($self, $arch) = @_;
742
743 my $a1 = $self->{no_monsters_check}->get_active;
744 my $a2 = $self->{no_wall_check}->get_active;
745
746 my $r = ($self->{no_wall_check}->get_active && arch_is_wall ($arch))
747 || ($self->{no_monsters_check}->get_active && arch_is_monster ($arch));
748 return $r;
749 }
750
751 sub begin {
752 my ($self, $map, $x, $y) = @_;
753
754 $self->SUPER::begin ($map, $x, $y);
755 $self->edit ($map, $x, $y);
756 }
757
758 sub edit {
759 my ($self, $map, $x, $y) = @_;
760
761 my $as = $map->get ($x, $y);
762 $self->stack_action ($as);
763 $map->change_stack ($x, $y, $as);
764 }
765
766 sub stack_action {
767 my ($self, $stack) = @_;
768
769 my $m = $self->get_mode;
770
771 if ($m eq 'top') {
772 pop @$stack;
773
774 } elsif ($m eq 'bottom') {
775 shift @$stack;
776
777 } elsif ($m eq 'above') {
778 my $fidx = stack_find_floor ($stack, 'from_top');
779
780 if (arch_is_floor ($stack->[$fidx]) and $stack->[$fidx + 1]) {
781 splice (@$stack, $fidx + 1, 1)
782 unless $self->check_excluded ($stack->[$fidx + 1])
783
784 } elsif (not arch_is_floor ($stack->[$fidx])) {
785 splice (@$stack, $fidx, 1)
786 unless $self->check_excluded ($stack->[$fidx])
787
788 }
789
790 } elsif ($m eq 'below') {
791 my $fidx = stack_find_floor ($stack, 'from_bottom');
792
793 if ($fidx > 0 and not arch_is_floor ($stack->[$fidx - 1])) {
794 splice (@$stack, $fidx - 1, 1)
795 unless $self->check_excluded ($stack->[$fidx - 1])
796
797 } elsif (not arch_is_floor ($stack->[$fidx])) { # no floor found
798 splice (@$stack, $fidx, 1)
799 unless $self->check_excluded ($stack->[$fidx])
800
801 }
802
803 } elsif ($m eq 'walls') {
804 my $widx = stack_find_wall ($stack, 'from_top');
805
806 while (arch_is_wall ($stack->[$widx])) {
807 splice (@$stack, $widx, 1);
808 $widx = stack_find_wall ($stack, 'from_top')
809 }
810
811 } elsif ($m eq 'floor') {
812 my $fidx = stack_find_floor ($stack, 'from_top');
813
814 while (arch_is_floor ($stack->[$fidx])) {
815 splice (@$stack, $fidx, 1);
816 $fidx = stack_find_floor ($stack, 'from_top')
817 }
818
819 } elsif ($m eq 'match') {
820 my $pick_name = $::MAINWIN->get_pick ()->{_name};
821 my $idx = stack_find ($stack, 'from_top', sub { $_[0]->{_name} eq $pick_name });
822
823 while ($stack->[$idx] and $stack->[$idx]->{_name} eq $pick_name) {
824 splice (@$stack, $idx, 1);
825 $idx = stack_find ($stack, 'from_top', sub { $_[0]->{_name} eq $pick_name });
826 }
827 }
828 }
829
830 package GCE::EditAction::ConnectExit;
831 use Storable qw/dclone/;
832 use GCE::Util;
833 use Gtk2;
834 use File::Spec::Functions;
835 use strict;
836
837 our @ISA = qw/GCE::EditAction::RadioModed/;
838
839 sub name { 'connectexit' }
840
841 sub init {
842 my ($self) = @_;
843
844 my $vb = new Gtk2::VBox;
845 $self->add_mode_button ($vb, "exit", "exit");
846 # $self->add_mode_button ($vb, "triggers", "trig");
847 $vb->pack_start ($self->{sel_edt} = Gtk2::Entry->new, 0, 1, 0);
848 $self->{sel_edt}->set_text (catfile ($::CFG->{MAPDIR}));
849
850 $vb->pack_start ($self->{sel_lbl} = Gtk2::Label->new, 0, 0, 0);
851 $self->tool_widget ($vb);
852 }
853
854 sub want_cursor { 0 }
855
856 sub begin {
857 my ($self, $map, $x, $y, $mapedit) = @_;
858
859 $self->edit ($map, $x, $y, $mapedit);
860 }
861
862 sub edit {
863 my ($self, $map, $x, $y, $mapedit) = @_;
864
865 my $pick = $::MAINWIN->get_pick;
866 my $as = $map->get ($x, $y);
867
868 my $exit;
869 for (@$as) {
870 if (arch_is_exit ($_)) {
871 $exit = $_;
872 }
873 }
874
875 my $mappath = $::CFG->{MAPDIR};
876
877 if ($exit) {
878 if ($self->{sel_exit}) {
879
880 $exit->{hp} = $self->{sel_exit}->[3];
881 $exit->{sp} = $self->{sel_exit}->[4];
882 #$exit->{slaying} = File::Spec->abs2rel ($self->{sel_exit}->[5], $ent);
883
884 my $exit2 = $self->{sel_exit}->[0];
885
886 $exit2->{hp} = $x;
887 $exit2->{sp} = $y;
888
889 my ($m1, $m2) = exit_paths ($mappath, $self->{sel_exit}->[5], $mapedit->{path});
890
891 $exit2->{slaying} = $m2;
892 $exit->{slaying} = $m1;
893
894 $self->SUPER::begin ($map, $x, $y, $mapedit);
895 $map->change_stack ($x, $y, $as);
896 $self->SUPER::end ($map);
897 $self->SUPER::begin ($self->{sel_exit}->[1], $exit->{hp}, $exit->{sp});
898 $self->{sel_exit}->[1]->change_stack ($exit->{hp}, $exit->{sp}, $self->{sel_exit}->[2]);
899 $self->SUPER::end ($self->{sel_exit}->[1]);
900
901 quick_msg ($mapedit, "$exit->{slaying} ($exit->{hp}:$exit->{sp}) $exit->{_name} <=> $exit2->{slaying} ($exit2->{hp}:$exit2->{sp}) $exit2->{_name}", 0);
902
903 $::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y);
904
905 $self->{sel_exit} = undef;
906 $self->{sel_lbl}->set_text ('');
907 } else {
908 $self->{sel_lbl}->set_text ("src: ($x:$y) $exit->{_name}");
909 $self->{sel_exit} = [$exit, $map, $as, $x, $y, $mapedit->{path}];
910 }
911 } else {
912 if ($self->{sel_exit}) {
913
914 my $exit2 = $self->{sel_exit}->[0];
915
916 $exit2->{hp} = $x;
917 $exit2->{sp} = $y;
918 $exit2->{slaying} = undef;
919
920 unless ($exit2->{slaying} =~ m/^\.\./) {
921 $exit2->{slaying} = '/' . $exit2->{slaying};
922 }
923
924 $self->SUPER::begin ($self->{sel_exit}->[1], $exit->{hp}, $exit->{sp});
925 $self->{sel_exit}->[1]->change_stack ($self->{sel_exit}->[3], $self->{sel_exit}->[4], $self->{sel_exit}->[2]);
926 $self->SUPER::end ($self->{sel_exit}->[1]);
927
928 quick_msg ($mapedit, "($self->{sel_exit}->[3]:$self->{sel_exit}->[4]) $self->{sel_exit}->[0]->{_name} => ($x:$y) $exit2->{_name}", 0);
929
930 $::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y);
931
932 $self->{sel_exit} = undef;
933 $self->{sel_lbl}->set_text ('');
934 } else {
935 quick_msg ($mapedit, "no exit object found");
936 }
937 }
938
939 # $self->stack_action ($as, dclone ($pick));
940 #$map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch));
941 }
942
943 sub end {}
944
945
946 =head1 AUTHOR
947
948 Marc Lehmann <schmorp@schmorp.de>
949 http://home.schmorp.de/
950
951 Robin Redeker <elmex@ta-sa.org>
952 http://www.ta-sa.org/
953
954 =cut
955
956 1
957