ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/EditAction.pm
Revision: 1.53
Committed: Thu Nov 5 12:26:04 2009 UTC (14 years, 6 months ago) by elmex
Branch: MAIN
Changes since 1.52: +5 -2 lines
Log Message:
fixed connect tool a bit more.

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