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

File Contents

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