ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/EditAction.pm
Revision: 1.51.2.1
Committed: Tue Jul 7 15:42:18 2009 UTC (14 years, 11 months ago) by elmex
Branch: cursor
Changes since 1.51: +21 -19 lines
Log Message:
*** empty log message ***

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