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 |
$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 strict; |
125 |
|
126 |
our @ISA = qw/GCE::EditAction::RadioModed/; |
127 |
|
128 |
sub name { 'pick' } |
129 |
|
130 |
sub want_cursor { 0 } |
131 |
|
132 |
sub only_on_click { 1 } |
133 |
|
134 |
sub special_arrow { 'GDK_HAND2' } |
135 |
|
136 |
sub init { |
137 |
my ($self) = @_; |
138 |
|
139 |
my $vb = new Gtk2::VBox; |
140 |
$self->{widget} = $vb; |
141 |
} |
142 |
|
143 |
# Pick does not change the stack |
144 |
sub begin {} |
145 |
sub end {} |
146 |
|
147 |
sub edit { |
148 |
my ($self, $map, $x, $y) = @_; |
149 |
|
150 |
my $cstack = $map->get ($x, $y); |
151 |
|
152 |
return unless @$cstack; |
153 |
|
154 |
my $arch = $cstack->[-1]; |
155 |
|
156 |
# virtual... grmbl.... |
157 |
# FIXME: I have to patch the stack of the real arch??? argl.. how?? |
158 |
my ($ox, $oy) = ($x, $y); |
159 |
if ($arch->{_virtual}) { |
160 |
$x = $arch->{virtual_x}; |
161 |
$y = $arch->{virtual_y}; |
162 |
$arch = $arch->{_virtual}; |
163 |
$cstack = $map->get ($x, $y); |
164 |
# XXX: This heavily blows up if $arch isn't on $cstack now.. and it actually really does :( |
165 |
} |
166 |
|
167 |
my $aref = |
168 |
GCE::ArchRef->new ( |
169 |
arch => $arch, |
170 |
cb => sub { |
171 |
$map->change_begin ('attredit'); |
172 |
$map->change_stack ($x, $y, $cstack); |
173 |
|
174 |
if (my $changeset = $map->change_end) { |
175 |
splice @{ $map->{undo_stack} ||= [] }, |
176 |
$map->{undo_stack_pos}++, 1e6, |
177 |
$changeset; |
178 |
} |
179 |
} |
180 |
); |
181 |
|
182 |
$::MAINWIN->update_attr_editor ($aref); |
183 |
$::MAINWIN->update_stack_view ($map, $ox, $oy); |
184 |
} |
185 |
|
186 |
package GCE::EditAction::Perl; |
187 |
|
188 |
use GCE::Util; |
189 |
use Gtk2; |
190 |
use strict; |
191 |
|
192 |
our @ISA = qw/GCE::EditAction/; |
193 |
|
194 |
sub name { 'perl' } |
195 |
|
196 |
sub special_arrow { 'GDK_HEART' } |
197 |
|
198 |
sub init { |
199 |
my ($self) = @_; |
200 |
|
201 |
my $vb = new Gtk2::VBox; |
202 |
$vb->pack_start (my $combo = Gtk2::ComboBox->new_text, 0, 1, 0); |
203 |
$vb->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0); |
204 |
$sw->add ($self->{txt} = Gtk2::TextView->new); |
205 |
|
206 |
my $code = {}; |
207 |
for (['unique floor' => 'my $i = 0; for (@$os) { $_->{is_floor} and $as->[$i]->{unique} = 1; $i++ }']) { |
208 |
$combo->append_text ($_->[0]); |
209 |
$code->{$_->[0]} = $_->[1]; |
210 |
} |
211 |
|
212 |
$combo->signal_connect (changed => sub { |
213 |
my ($combo) = @_; |
214 |
my $str = $combo->get_active_text; |
215 |
$self->{txt}->get_buffer->set_text ($code->{$str}); |
216 |
}); |
217 |
|
218 |
$self->tool_widget ($vb); |
219 |
} |
220 |
|
221 |
sub want_cursor { 0 } |
222 |
|
223 |
sub edit { |
224 |
my ($self, $map, $x, $y) = @_; |
225 |
|
226 |
my $pick = $::MAINWIN->get_pick; |
227 |
my $as = $map->get ($x, $y); |
228 |
|
229 |
$as = $self->eval ($map, $pick, $as, $x, $y); |
230 |
$map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch)); |
231 |
} |
232 |
|
233 |
sub eval { |
234 |
my ($self, $map, $pick, $as, $x, $y) = @_; |
235 |
my $buf = $self->{txt}->get_buffer; |
236 |
my $code = $buf->get_text ($buf->get_start_iter, $buf->get_end_iter, 0); |
237 |
my $f_idx = stack_find_floor ($as, 'from_top'); |
238 |
my $w_idx = stack_find_wall ($as, 'from_top'); |
239 |
my $os = [ map { $Crossfire::ARCH{$_->{_name}} } @$as ]; |
240 |
|
241 |
unless (arch_is_floor ($as->[$f_idx])) { $f_idx = undef; } |
242 |
unless (arch_is_floor ($as->[$w_idx])) { $w_idx = undef; } |
243 |
|
244 |
eval $code; |
245 |
return $as; |
246 |
} |
247 |
|
248 |
package GCE::EditAction::FollowExit; |
249 |
use Storable qw/dclone/; |
250 |
use File::Spec::Functions; |
251 |
use GCE::Util; |
252 |
use Gtk2; |
253 |
use strict; |
254 |
|
255 |
our @ISA = qw/GCE::EditAction/; |
256 |
|
257 |
sub name { 'place' } |
258 |
|
259 |
sub init { |
260 |
my ($self) = @_; |
261 |
|
262 |
my $vb = new Gtk2::VBox; |
263 |
|
264 |
$self->tool_widget ($vb); |
265 |
} |
266 |
|
267 |
sub want_cursor { 0 } |
268 |
|
269 |
sub edit { |
270 |
my ($self, $map, $x, $y, $mape) = @_; |
271 |
|
272 |
my $as = $map->get ($x, $y); |
273 |
|
274 |
my $exit; |
275 |
for my $arch (@$as) { |
276 |
if ($arch->{_virtual}) { |
277 |
$arch = $arch->{_virtual}; |
278 |
} |
279 |
if (arch_is_exit ($arch)) { |
280 |
$exit = $arch; |
281 |
} |
282 |
} |
283 |
|
284 |
if ($exit and $exit->{slaying} !~ /^!/) { |
285 |
my $dest = map2abs ($exit->{slaying}, $mape); |
286 |
my $file = $dest; |
287 |
# XXX: Replace with statusbar message |
288 |
unless (-f $file) { |
289 |
$file .= ".map"; |
290 |
unless (-f $file) { |
291 |
quick_msg ($mape, "Couldn't find map file at '$dest' or '$dest.map'.", 1); |
292 |
return |
293 |
} |
294 |
} |
295 |
$::MAINWIN->open_map_editor ($file); |
296 |
} |
297 |
} |
298 |
|
299 |
package GCE::EditAction::Place; |
300 |
|
301 |
use Storable qw/dclone/; |
302 |
use GCE::Util; |
303 |
use Gtk2; |
304 |
use strict; |
305 |
|
306 |
our @ISA = qw/GCE::EditAction::RadioModed/; |
307 |
|
308 |
sub name { 'place' } |
309 |
|
310 |
sub init { |
311 |
my ($self) = @_; |
312 |
|
313 |
my $vb = new Gtk2::VBox; |
314 |
|
315 |
$self->add_mode_button ($vb, "auto", "auto"); |
316 |
$self->add_mode_button ($vb, "top", "top"); |
317 |
$self->add_mode_button ($vb, "above floor", "above"); |
318 |
$self->add_mode_button ($vb, "below floor", "below"); |
319 |
$self->add_mode_button ($vb, "bottom", "bottom"); |
320 |
|
321 |
$self->tool_widget ($vb); |
322 |
} |
323 |
|
324 |
sub want_cursor { 0 } |
325 |
|
326 |
# 1 up 2 right 4 down 8 left |
327 |
my @join_ext = ( |
328 |
"0", # 0 |
329 |
"1_2", # 1 |
330 |
"1_4", # 2 |
331 |
"2_2_1", # 3 |
332 |
"1_1", # 4 |
333 |
"2_1_1", # 5 |
334 |
"2_2_2", # 6 |
335 |
"3_2", # 7 |
336 |
"1_3", # 8 |
337 |
"2_2_4", # 9 |
338 |
"2_1_2", # 10 |
339 |
"3_1", # 11 |
340 |
"2_2_3", # 12 |
341 |
"3_4", # 13 |
342 |
"3_3", # 14 |
343 |
"4", # 15 |
344 |
); |
345 |
|
346 |
sub autojoin { |
347 |
my ($map, $pick, $x1, $y1, $x2, $y2) = @_; |
348 |
|
349 |
my $dx = $x2 - $x1; |
350 |
my $dy = $y2 - $y1; |
351 |
|
352 |
my $dir = $dy ? ($dy == -1 ? 1 : $dy == 1 ? 4 : return) |
353 |
: ($dx == -1 ? 8 : $dx == 1 ? 2 : $dx == 0 ? 0 : return); |
354 |
|
355 |
my $as = $map->get ($x1, $y1); |
356 |
|
357 |
(my $base = $pick->{_name}) =~ s/_0$//; |
358 |
|
359 |
for my $idx (0 .. $#$as) { |
360 |
my $arch = $as->[$idx]; |
361 |
for my $dirs (0..15) { |
362 |
my $name = $arch->{_name}; |
363 |
|
364 |
if ($arch->{_name} eq "$base\_$join_ext[$dirs]") { |
365 |
$dirs |= $dir; |
366 |
|
367 |
my $name = "$base\_$join_ext[$dirs]"; |
368 |
|
369 |
if ($Crossfire::ARCH{$name}) { |
370 |
%$arch = ( _name => $name ); |
371 |
$map->change_stack ($x1, $y1, $as); |
372 |
|
373 |
return 1; |
374 |
} |
375 |
} |
376 |
} |
377 |
} |
378 |
|
379 |
return 0; |
380 |
} |
381 |
|
382 |
sub edit { |
383 |
my ($self, $map, $x, $y) = @_; |
384 |
|
385 |
my $pick = $::MAINWIN->get_pick; |
386 |
my $as = $map->get ($x, $y); |
387 |
|
388 |
my $autojoin = $pick->{_name} =~ /_0$/ |
389 |
&& $self->get_mode eq "auto"; |
390 |
|
391 |
autojoin $map, $pick, @{$self->{last_pos}}, $x, $y |
392 |
if $autojoin && $self->{last_pos}; |
393 |
|
394 |
if (!$autojoin |
395 |
|| !($self->{last_pos} ? autojoin $map, $pick, $x, $y, @{$self->{last_pos}}, |
396 |
: autojoin $map, $pick, $x, $y, $x, $y)) { |
397 |
$self->stack_action ($as, dclone $pick); |
398 |
$map->change_stack ($x, $y, $as); |
399 |
autojoin $map, $pick, $x, $y, @{$self->{last_pos}} |
400 |
if $autojoin && $self->{last_pos}; |
401 |
} |
402 |
|
403 |
$self->{last_pos} = [$x, $y]; |
404 |
} |
405 |
|
406 |
sub end { |
407 |
my ($self, $map, $x, $y, $mape) = @_; |
408 |
|
409 |
# now actualize stack and attr editor |
410 |
$::MAINWIN->update_stack_view ($map, $x, $y); |
411 |
|
412 |
my $cstack = $map->get ($x, $y); |
413 |
|
414 |
my $arch = $cstack->[-1]; |
415 |
|
416 |
delete $self->{last_pos}; |
417 |
|
418 |
# virtual... grmbl.... |
419 |
# FIXME: I have to patch the stack of the real arch??? argl.. how?? |
420 |
if ($arch->{_virtual}) { |
421 |
$x = $arch->{virtual_x}; |
422 |
$y = $arch->{virtual_y}; |
423 |
$arch = $arch->{_virtual}; |
424 |
$cstack = $map->get ($x, $y); |
425 |
} |
426 |
|
427 |
$self->SUPER::end ($map, $x, $y, $mape); |
428 |
} |
429 |
|
430 |
sub stack_action { |
431 |
my ($self, $stack, $arch) = @_; |
432 |
|
433 |
my $m = $self->get_mode; |
434 |
|
435 |
if ($m eq 'top') { |
436 |
if (@$stack == 0 or $stack->[-1]->{_name} ne $arch->{_name}) { |
437 |
push @$stack, $arch; |
438 |
} |
439 |
|
440 |
} elsif ($m eq 'bottom') { |
441 |
if (@$stack == 0 or $stack->[0]->{_name} ne $arch->{_name}) { |
442 |
unshift @$stack, $arch; |
443 |
} |
444 |
|
445 |
} elsif ($m eq 'above') { |
446 |
my $fidx = stack_find_floor ($stack, 'from_top'); |
447 |
|
448 |
if (@$stack == 0 |
449 |
or not ($stack->[$fidx + 1]) |
450 |
or $stack->[$fidx + 1]->{_name} ne $arch->{_name}) |
451 |
{ |
452 |
splice (@$stack, $fidx + 1, 0, $arch); |
453 |
} |
454 |
|
455 |
} elsif ($m eq 'below') { |
456 |
my $fidx = stack_find_floor ($stack, 'from_bottom'); |
457 |
|
458 |
if (@$stack == 0 |
459 |
or $fidx == 0 |
460 |
or not ($stack->[$fidx - 1]) |
461 |
or $stack->[$fidx - 1]->{_name} ne $arch->{_name}) |
462 |
{ |
463 |
splice (@$stack, $fidx, 0, $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 above 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 Crossfire; |
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 (arch_is_connector ($_)) { |
840 |
push @$conns, $_; |
841 |
} |
842 |
if (arch_is_exit ($_)) { |
843 |
$exit = $_; |
844 |
} |
845 |
} |
846 |
|
847 |
if ($mode eq 'auto') { |
848 |
$conns = [] if $exit; |
849 |
} elsif ($mode eq 'exit') { |
850 |
$conns = []; |
851 |
} elsif ($mode eq 'connect') { |
852 |
$exit = undef; |
853 |
} |
854 |
|
855 |
my $mappath = $::MAPDIR; |
856 |
|
857 |
if ($exit) { |
858 |
if ($self->{sel_exit}) { |
859 |
|
860 |
$exit->{hp} = $self->{sel_exit}->[3]; |
861 |
$exit->{sp} = $self->{sel_exit}->[4]; |
862 |
#$exit->{slaying} = File::Spec->abs2rel ($self->{sel_exit}->[5], $ent); |
863 |
|
864 |
my $exit2 = $self->{sel_exit}->[0]; |
865 |
|
866 |
$exit2->{hp} = $x; |
867 |
$exit2->{sp} = $y; |
868 |
|
869 |
my ($m1, $m2) = exit_paths ($mappath, $self->{sel_exit}->[5], $mapedit->{path}); |
870 |
|
871 |
$exit2->{slaying} = $m2; |
872 |
$exit->{slaying} = $m1; |
873 |
|
874 |
$self->SUPER::begin ($map, $x, $y, $mapedit); |
875 |
$map->change_stack ($x, $y, $as); |
876 |
$self->SUPER::end ($map); |
877 |
$self->SUPER::begin ($self->{sel_exit}->[1], $exit->{hp}, $exit->{sp}); |
878 |
$self->{sel_exit}->[1]->change_stack ($exit->{hp}, $exit->{sp}, $self->{sel_exit}->[2]); |
879 |
$self->SUPER::end ($self->{sel_exit}->[1]); |
880 |
|
881 |
quick_msg ($mapedit, "$exit->{slaying} ($exit->{hp}:$exit->{sp}) $exit->{_name} <=> $exit2->{slaying} ($exit2->{hp}:$exit2->{sp}) $exit2->{_name}", 0); |
882 |
|
883 |
$::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y); |
884 |
|
885 |
$self->{sel_exit} = undef; |
886 |
$self->{sel_lbl}->set_text (''); |
887 |
} else { |
888 |
$self->{sel_lbl}->set_text ("src: ($x:$y) $exit->{_name}"); |
889 |
$self->{sel_exit} = [$exit, $map, $as, $x, $y, $mapedit->{path}]; |
890 |
} |
891 |
} elsif (@$conns) { |
892 |
for (@$conns) { |
893 |
$_->{connected} = $self->{pcon}->get_value; |
894 |
} |
895 |
$self->SUPER::begin ($map, $x, $y, $mapedit); |
896 |
$map->change_stack ($x, $y, $as); |
897 |
$self->SUPER::end ($map); |
898 |
} elsif ($self->{sel_exit}) { |
899 |
my $exit2 = $self->{sel_exit}->[0]; |
900 |
|
901 |
$exit2->{hp} = $x; |
902 |
$exit2->{sp} = $y; |
903 |
$exit2->{slaying} = undef; |
904 |
|
905 |
$self->SUPER::begin ($self->{sel_exit}->[1], $self->{sel_exit}->[3], $self->{sel_exit}->[4]); |
906 |
$self->{sel_exit}->[1]->change_stack ($self->{sel_exit}->[3], $self->{sel_exit}->[4], $self->{sel_exit}->[2]); |
907 |
$self->SUPER::end ($self->{sel_exit}->[1]); |
908 |
|
909 |
quick_msg ($mapedit, "($self->{sel_exit}->[3]:$self->{sel_exit}->[4]) $self->{sel_exit}->[0]->{_name} => ($x:$y)", 0); |
910 |
|
911 |
$::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y); |
912 |
|
913 |
$self->{sel_exit} = undef; |
914 |
$self->{sel_lbl}->set_text (''); |
915 |
} else { |
916 |
quick_msg ($mapedit, "no exit or connection object found"); |
917 |
} |
918 |
} |
919 |
|
920 |
|
921 |
=head1 AUTHOR |
922 |
|
923 |
Marc Lehmann <schmorp@schmorp.de> |
924 |
http://home.schmorp.de/ |
925 |
|
926 |
Robin Redeker <elmex@ta-sa.org> |
927 |
http://www.ta-sa.org/ |
928 |
|
929 |
=cut |
930 |
|
931 |
1 |
932 |
|