ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/EditAction.pm
Revision: 1.55
Committed: Sat Mar 20 12:12:56 2010 UTC (14 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.54: +3 -0 lines
Log Message:
added error reporting to the perl eval tool.

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