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

# User Rev Content
1 elmex 1.1 package GCE::EditAction;
2    
3     =head1 NAME
4    
5     GCE::EditActions - this is the abstraction of edit actions (placing, deleting, etc.)
6    
7     =cut
8    
9     use Gtk2;
10     use Gtk2::Gdk::Keysyms;
11     use Gtk2::SimpleMenu;
12    
13 root 1.51 use Deliantra;
14     use Deliantra::MapWidget;
15 elmex 1.1
16     use strict;
17    
18     sub new {
19     my $class = shift;
20     my $self = { @_ };
21     bless $self, $class;
22 elmex 1.6 $self->init;
23 elmex 1.1 return $self;
24     }
25    
26 elmex 1.10 sub name { } # a unique name for this tool (for storing it in a hash in the main window)
27    
28 elmex 1.7 sub tool_widget { if ($_[1]) { $_[0]->{widget} = $_[1] } $_[0]->{widget} }
29 elmex 1.6 sub init { }
30    
31 elmex 1.1 sub want_cursor { 1 }
32    
33 elmex 1.42 sub only_on_click { 0 }
34    
35 elmex 1.1 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 elmex 1.10 my ($self, $map, $x, $y) = @_;
51 elmex 1.1 }
52    
53     sub begin {
54 elmex 1.10 my ($self, $map, $x, $y) = @_;
55 elmex 1.43 #d# warn "CHANGE BEGIN ".(ref $self)."\n";
56 root 1.3 $map->change_begin (ref $self);
57 elmex 1.1 }
58 root 1.3
59 elmex 1.1 sub end {
60 root 1.3 my ($self, $map) = @_;
61    
62 elmex 1.43 #d# warn "CHANGE END ".(ref $self)."\n";
63 root 1.3 if (my $changeset = $map->change_end) {
64     splice @{ $map->{undo_stack} ||= [] },
65 root 1.4 $map->{undo_stack_pos}++, 1e6,
66 root 1.3 $changeset;
67    
68     #TODO: limit undo stack size to some preconfigured limit
69     }
70 elmex 1.1 }
71    
72 elmex 1.7 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 elmex 1.1
122     package GCE::EditAction::Pick;
123 elmex 1.34 use GCE::ArchRef;
124 elmex 1.49 use GCE::Util;
125 elmex 1.7 use strict;
126 elmex 1.1
127 elmex 1.23 our @ISA = qw/GCE::EditAction::RadioModed/;
128 elmex 1.1
129 elmex 1.10 sub name { 'pick' }
130    
131 elmex 1.7 sub want_cursor { 0 }
132    
133 elmex 1.42 sub only_on_click { 1 }
134    
135 elmex 1.1 sub special_arrow { 'GDK_HAND2' }
136    
137 elmex 1.23 sub init {
138     my ($self) = @_;
139    
140     my $vb = new Gtk2::VBox;
141     $self->{widget} = $vb;
142     }
143    
144 elmex 1.40 # Pick does not change the stack
145     sub begin {}
146     sub end {}
147    
148 elmex 1.1 sub edit {
149 elmex 1.10 my ($self, $map, $x, $y) = @_;
150 elmex 1.1
151 elmex 1.51.2.1 my $cstack = $map->get_ro ($x, $y);
152 elmex 1.49 my ($ox, $oy) = ($x, $y);
153 elmex 1.1
154 elmex 1.34 return unless @$cstack;
155    
156 elmex 1.1 my $arch = $cstack->[-1];
157 elmex 1.51.2.1 my $z;
158     ($x, $y, $arch, $cstack, $z) = devirtualize ($map, $x, $y, $arch, $cstack);
159 elmex 1.1
160 elmex 1.51.2.1 $::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 elmex 1.34
179 elmex 1.23 }
180 elmex 1.1
181 elmex 1.13 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 elmex 1.15 sub special_arrow { 'GDK_HEART' }
192    
193 elmex 1.13 sub init {
194     my ($self) = @_;
195    
196     my $vb = new Gtk2::VBox;
197 elmex 1.20 $vb->pack_start (my $combo = Gtk2::ComboBox->new_text, 0, 1, 0);
198 elmex 1.13 $vb->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
199     $sw->add ($self->{txt} = Gtk2::TextView->new);
200    
201 elmex 1.20 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 elmex 1.13 $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 elmex 1.20 my ($self, $map, $pick, $as, $x, $y) = @_;
230 elmex 1.13 my $buf = $self->{txt}->get_buffer;
231     my $code = $buf->get_text ($buf->get_start_iter, $buf->get_end_iter, 0);
232 elmex 1.20 my $f_idx = stack_find_floor ($as, 'from_top');
233     my $w_idx = stack_find_wall ($as, 'from_top');
234 root 1.51 my $os = [ map { $Deliantra::ARCH{$_->{_name}} } @$as ];
235 elmex 1.20
236     unless (arch_is_floor ($as->[$f_idx])) { $f_idx = undef; }
237     unless (arch_is_floor ($as->[$w_idx])) { $w_idx = undef; }
238 elmex 1.13
239     eval $code;
240 elmex 1.20 return $as;
241 elmex 1.13 }
242    
243 elmex 1.18 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 elmex 1.21 my ($self, $map, $x, $y, $mape) = @_;
266 elmex 1.18
267     my $as = $map->get ($x, $y);
268    
269     my $exit;
270 elmex 1.27 for my $arch (@$as) {
271 elmex 1.50 my ($rx, $ry, $rarch) = devirtualize ($map, $x, $y, $arch, $as);
272     if (arch_is_exit ($rarch)) {
273     $exit = $rarch;
274 elmex 1.18 }
275     }
276    
277 elmex 1.32 if ($exit and $exit->{slaying} !~ /^!/) {
278 elmex 1.22 my $dest = map2abs ($exit->{slaying}, $mape);
279 elmex 1.45 my $file = $dest;
280 elmex 1.32 # XXX: Replace with statusbar message
281 elmex 1.45 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 elmex 1.32 }
288 elmex 1.45 $::MAINWIN->open_map_editor ($file);
289 elmex 1.18 }
290     }
291    
292 elmex 1.1 package GCE::EditAction::Place;
293 root 1.28
294 elmex 1.14 use Storable qw/dclone/;
295 elmex 1.2 use GCE::Util;
296 elmex 1.6 use Gtk2;
297     use strict;
298 elmex 1.2
299 elmex 1.7 our @ISA = qw/GCE::EditAction::RadioModed/;
300 elmex 1.6
301 elmex 1.10 sub name { 'place' }
302    
303 elmex 1.6 sub init {
304     my ($self) = @_;
305    
306     my $vb = new Gtk2::VBox;
307 root 1.28
308 elmex 1.7 $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 elmex 1.6
314 elmex 1.7 $self->tool_widget ($vb);
315 elmex 1.6 }
316    
317 elmex 1.1 sub want_cursor { 0 }
318    
319 root 1.28 # 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 root 1.51 if ($Deliantra::ARCH{$name}) {
363 root 1.28 %$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 elmex 1.1 sub edit {
376 elmex 1.10 my ($self, $map, $x, $y) = @_;
377 elmex 1.1
378 elmex 1.10 my $pick = $::MAINWIN->get_pick;
379     my $as = $map->get ($x, $y);
380 elmex 1.1
381 root 1.28 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 elmex 1.1 }
398    
399 elmex 1.17 sub end {
400     my ($self, $map, $x, $y, $mape) = @_;
401 elmex 1.37
402 elmex 1.22 # 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 root 1.28 delete $self->{last_pos};
410    
411 elmex 1.22 # 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 elmex 1.17 $self->SUPER::end ($map, $x, $y, $mape);
421     }
422    
423 elmex 1.6 sub stack_action {
424     my ($self, $stack, $arch) = @_;
425    
426 elmex 1.7 my $m = $self->get_mode;
427 elmex 1.6
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 elmex 1.9 # if we have a wall above the floor, replace it with the to place item
485     $stack->[$widx] = $arch;
486 elmex 1.6 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 elmex 1.36
495     } elsif ($stack->[-1]->{_name} eq $arch->{_name}) {
496     $stack->[-1] = $arch;
497 elmex 1.6 }
498     }
499     }
500     }
501    
502 elmex 1.12 package GCE::EditAction::Select;
503 elmex 1.11 use GCE::Util;
504     use Gtk2;
505 root 1.51 use Deliantra;
506 elmex 1.12 use Storable qw/dclone/;
507 elmex 1.11 use strict;
508    
509     our @ISA = qw/GCE::EditAction::RadioModed/;
510    
511 elmex 1.12 sub name { 'select' }
512 elmex 1.11
513 elmex 1.15 sub special_arrow { 'GDK_CIRCLE' }
514    
515 elmex 1.11 sub init {
516     my ($self) = @_;
517    
518     my $vb = new Gtk2::VBox;
519    
520 elmex 1.19 $vb->pack_start (my $bt = Gtk2::Button->new_with_mnemonic ("_copy"), 0, 1, 0);
521 elmex 1.12 $bt->signal_connect (clicked => sub { $self->copy });
522     $vb->pack_start ($self->{paste_top} = Gtk2::CheckButton->new ('paste on top'), 0, 1, 0);
523 elmex 1.19 $vb->pack_start (my $bt = Gtk2::Button->new_with_mnemonic ("paste (_v)"), 0, 1, 0);
524 elmex 1.12 $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 elmex 1.15 $self->add_mode_button ($vb, "perl", "perl");
529 elmex 1.19 $vb->pack_start (my $bt = Gtk2::Button->new_with_mnemonic ("i_nvoke"), 0, 1, 0);
530 elmex 1.12 $bt->signal_connect (clicked => sub { $self->invoke });
531 elmex 1.11
532     $self->tool_widget ($vb);
533     }
534    
535 elmex 1.12 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 elmex 1.23 my ($self, $map, $xp, $yp) = @_;
558 elmex 1.12
559     return unless $self->{selection}->{a};
560    
561     my ($x1, $y1);
562    
563 elmex 1.15 if (defined $xp) {
564 elmex 1.12 ($x1, $y1) = ($xp, $yp);
565     } else {
566     ($x1, $y1) = @{$self->{selection}->{a}};
567     }
568    
569 elmex 1.23 $map ||= $self->{selection}->{map};
570 elmex 1.12
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 elmex 1.15 } elsif ($m eq 'perl') {
614     $::MAINWIN->{edit_collection}{perl}->edit ($map, $x, $y);
615 elmex 1.12 }
616     }
617     }
618     $self->SUPER::end ($map);
619     }
620    
621 elmex 1.11 sub want_cursor { 0 }
622    
623     sub begin {
624     my ($self, $map, $x, $y) = @_;
625    
626 elmex 1.29 if ($self->{selection}->{map}) {
627     $self->{selection}->{map}->overlay ('selection');
628     }
629 elmex 1.11 delete $self->{selection};
630    
631 elmex 1.12 $self->{selection}->{a} = [$x, $y];
632     }
633 elmex 1.11
634 elmex 1.12 sub end {
635 elmex 1.11 }
636    
637     sub edit {
638     my ($self, $map, $x, $y) = @_;
639    
640     $self->{selection}->{b} = [$x, $y];
641 elmex 1.12 $self->{selection}->{map} = $map;
642 elmex 1.29 $self->update_overlay ();
643 elmex 1.11 }
644    
645     sub update_overlay {
646 elmex 1.29 my ($self) = @_;
647    
648     my $map = $self->{selection}->{map};
649    
650     return unless (defined $self->{selection}->{a} and defined $self->{selection}->{b});
651 elmex 1.11
652 elmex 1.12 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 elmex 1.11
658 elmex 1.12 my $w = ($x2 - $x1) + 1;
659     my $h = ($y2 - $y1) + 1;
660    
661     $map->overlay (selection =>
662     $x1 * TILESIZE, $y1 * TILESIZE,
663 elmex 1.48 $w * TILESIZE, $h * TILESIZE,
664 elmex 1.12 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 elmex 1.11 }
676    
677 elmex 1.1 package GCE::EditAction::Erase;
678 elmex 1.7 use GCE::Util;
679     use Gtk2;
680     use strict;
681 elmex 1.1
682 elmex 1.7 our @ISA = qw/GCE::EditAction::RadioModed/;
683 elmex 1.1
684 elmex 1.10 sub name { 'erase' }
685    
686 elmex 1.1 sub want_cursor { 0 }
687    
688 elmex 1.15 sub special_arrow { 'GDK_DIAMOND_CROSS' }
689 elmex 1.10
690 elmex 1.7 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 root 1.25 $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 elmex 1.7 }
707    
708     sub check_excluded {
709     my ($self, $arch) = @_;
710    
711 elmex 1.37 my $r = ($self->{no_wall_check}->get_active && arch_is_wall ($arch))
712     || ($self->{no_monsters_check}->get_active && arch_is_monster ($arch));
713 elmex 1.7
714 elmex 1.37 return $r;
715 elmex 1.1 }
716    
717     sub edit {
718 elmex 1.10 my ($self, $map, $x, $y) = @_;
719 elmex 1.1
720     my $as = $map->get ($x, $y);
721 elmex 1.7 $self->stack_action ($as);
722 root 1.3 $map->change_stack ($x, $y, $as);
723 elmex 1.1 }
724    
725 elmex 1.7 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 elmex 1.38 package GCE::EditAction::Connect;
790 elmex 1.16 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 elmex 1.38 sub name { 'connect' }
799 elmex 1.16
800     sub init {
801     my ($self) = @_;
802    
803     my $vb = new Gtk2::VBox;
804 elmex 1.39 $self->add_mode_button ($vb, "auto", "auto", 1);
805 elmex 1.16 $self->add_mode_button ($vb, "exit", "exit");
806 elmex 1.39 $self->add_mode_button ($vb, "connect", "connect");
807 elmex 1.37
808 elmex 1.38 $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 elmex 1.16 $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 elmex 1.37 #XXX: change_begin/end is handled in edit
818     sub begin { }
819     sub end { }
820 elmex 1.16
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 elmex 1.39 my $mode = $self->get_mode;
828    
829 elmex 1.16 my $exit;
830 elmex 1.38 my $conns = [];
831 elmex 1.16 for (@$as) {
832 elmex 1.50 if ($_->{_virtual}) { #FIXME: implement virtual handling for connect
833     return;
834     }
835 elmex 1.38 if (arch_is_connector ($_)) {
836     push @$conns, $_;
837 elmex 1.39 }
838     if (arch_is_exit ($_)) {
839 elmex 1.16 $exit = $_;
840     }
841     }
842    
843 elmex 1.39 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 elmex 1.46 my $mappath = $::MAPDIR;
852 elmex 1.26
853 elmex 1.16 if ($exit) {
854     if ($self->{sel_exit}) {
855    
856     $exit->{hp} = $self->{sel_exit}->[3];
857     $exit->{sp} = $self->{sel_exit}->[4];
858 elmex 1.26 #$exit->{slaying} = File::Spec->abs2rel ($self->{sel_exit}->[5], $ent);
859 elmex 1.16
860     my $exit2 = $self->{sel_exit}->[0];
861    
862     $exit2->{hp} = $x;
863     $exit2->{sp} = $y;
864    
865 elmex 1.26 my ($m1, $m2) = exit_paths ($mappath, $self->{sel_exit}->[5], $mapedit->{path});
866    
867     $exit2->{slaying} = $m2;
868     $exit->{slaying} = $m1;
869 elmex 1.16
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 elmex 1.26 quick_msg ($mapedit, "$exit->{slaying} ($exit->{hp}:$exit->{sp}) $exit->{_name} <=> $exit2->{slaying} ($exit2->{hp}:$exit2->{sp}) $exit2->{_name}", 0);
878 elmex 1.16
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 elmex 1.39 } elsif (@$conns) {
888     for (@$conns) {
889 elmex 1.38 $_->{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 elmex 1.39 } elsif ($self->{sel_exit}) {
895     my $exit2 = $self->{sel_exit}->[0];
896 elmex 1.18
897 elmex 1.39 $exit2->{hp} = $x;
898     $exit2->{sp} = $y;
899     $exit2->{slaying} = undef;
900 elmex 1.18
901 elmex 1.39 $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 elmex 1.18
905 elmex 1.41 quick_msg ($mapedit, "($self->{sel_exit}->[3]:$self->{sel_exit}->[4]) $self->{sel_exit}->[0]->{_name} => ($x:$y)", 0);
906 elmex 1.18
907 elmex 1.39 $::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y);
908 elmex 1.18
909 elmex 1.39 $self->{sel_exit} = undef;
910     $self->{sel_lbl}->set_text ('');
911     } else {
912     quick_msg ($mapedit, "no exit or connection object found");
913 elmex 1.16 }
914     }
915    
916 elmex 1.7
917 elmex 1.1 =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 root 1.5
927     1
928 elmex 1.1