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 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 |
my ($self, $map, $x, $y) = @_; |
49 |
} |
50 |
|
51 |
sub begin { |
52 |
my ($self, $map, $x, $y) = @_; |
53 |
|
54 |
$map->change_begin (ref $self); |
55 |
} |
56 |
|
57 |
sub end { |
58 |
my ($self, $map) = @_; |
59 |
|
60 |
if (my $changeset = $map->change_end) { |
61 |
splice @{ $map->{undo_stack} ||= [] }, |
62 |
$map->{undo_stack_pos}++, 1e6, |
63 |
$changeset; |
64 |
|
65 |
#TODO: limit undo stack size to some preconfigured limit |
66 |
} |
67 |
} |
68 |
|
69 |
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 |
|
119 |
package GCE::EditAction::Pick; |
120 |
use strict; |
121 |
|
122 |
our @ISA = qw/GCE::EditAction::RadioModed/; |
123 |
|
124 |
sub name { 'pick' } |
125 |
|
126 |
sub want_cursor { 0 } |
127 |
|
128 |
sub special_arrow { 'GDK_HAND2' } |
129 |
|
130 |
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 |
sub begin { |
139 |
my ($self, $map, $x, $y) = @_; |
140 |
$self->edit ($map, $x, $y); |
141 |
} |
142 |
|
143 |
sub edit { |
144 |
my ($self, $map, $x, $y) = @_; |
145 |
|
146 |
$::MAINWIN->update_stack_view ($map, $x, $y); |
147 |
|
148 |
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 |
$x = $arch->{virtual_x}; |
156 |
$y = $arch->{virtual_y}; |
157 |
$arch = $arch->{_virtual}; |
158 |
$cstack = $map->get ($x, $y); |
159 |
} |
160 |
|
161 |
unless ($self->{only_attr}->get_active) { |
162 |
$::MAINWIN->set_pick ($arch) |
163 |
if @$cstack; |
164 |
} |
165 |
|
166 |
$::MAINWIN->update_attr_editor ($arch, sub { |
167 |
$map->change_begin (ref $self); |
168 |
$map->change_stack ($x, $y, $cstack); |
169 |
# 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 |
}); |
177 |
} |
178 |
|
179 |
sub end {} |
180 |
|
181 |
package GCE::EditAction::Perl; |
182 |
|
183 |
use GCE::Util; |
184 |
use Gtk2; |
185 |
use strict; |
186 |
|
187 |
our @ISA = qw/GCE::EditAction/; |
188 |
|
189 |
sub name { 'perl' } |
190 |
|
191 |
sub special_arrow { 'GDK_HEART' } |
192 |
|
193 |
sub init { |
194 |
my ($self) = @_; |
195 |
|
196 |
my $vb = new Gtk2::VBox; |
197 |
$vb->pack_start (my $combo = Gtk2::ComboBox->new_text, 0, 1, 0); |
198 |
$vb->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0); |
199 |
$sw->add ($self->{txt} = Gtk2::TextView->new); |
200 |
|
201 |
my $code = {}; |
202 |
for (['unique floor' => 'my $i = 0; for (@$os) { $_->{is_floor} and $as->[$i]->{unique} = 1; $i++ }']) { |
203 |
$combo->append_text ($_->[0]); |
204 |
$code->{$_->[0]} = $_->[1]; |
205 |
} |
206 |
|
207 |
$combo->signal_connect (changed => sub { |
208 |
my ($combo) = @_; |
209 |
my $str = $combo->get_active_text; |
210 |
$self->{txt}->get_buffer->set_text ($code->{$str}); |
211 |
}); |
212 |
|
213 |
$self->tool_widget ($vb); |
214 |
} |
215 |
|
216 |
sub want_cursor { 0 } |
217 |
|
218 |
sub 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 |
my ($self, $map, $pick, $as, $x, $y) = @_; |
237 |
my $buf = $self->{txt}->get_buffer; |
238 |
my $code = $buf->get_text ($buf->get_start_iter, $buf->get_end_iter, 0); |
239 |
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 |
|
246 |
eval $code; |
247 |
return $as; |
248 |
} |
249 |
|
250 |
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 |
my ($self, $map, $x, $y, $mape) = @_; |
273 |
|
274 |
# $self->SUPER::begin ($map, $x, $y); |
275 |
$self->edit ($map, $x, $y, $mape); |
276 |
} |
277 |
|
278 |
sub edit { |
279 |
my ($self, $map, $x, $y, $mape) = @_; |
280 |
|
281 |
my $as = $map->get ($x, $y); |
282 |
|
283 |
my $exit; |
284 |
for (@$as) { |
285 |
my $a = $Crossfire::ARCH{$_->{_name}}; |
286 |
if (arch_is_exit ($_)) { |
287 |
$exit = $_; |
288 |
} |
289 |
} |
290 |
|
291 |
if ($exit) { |
292 |
my $dest = map2abs ($exit->{slaying}, $mape); |
293 |
$::MAINWIN->open_map_editor ($dest); |
294 |
} |
295 |
} |
296 |
|
297 |
sub end { |
298 |
my ($self, $map, $x, $y, $mape) = @_; |
299 |
# $::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y); |
300 |
#$self->SUPER::end ($map, $x, $y, $mape); |
301 |
} |
302 |
|
303 |
package GCE::EditAction::Place; |
304 |
use Storable qw/dclone/; |
305 |
use GCE::Util; |
306 |
use Gtk2; |
307 |
use strict; |
308 |
|
309 |
our @ISA = qw/GCE::EditAction::RadioModed/; |
310 |
|
311 |
sub name { 'place' } |
312 |
|
313 |
sub init { |
314 |
my ($self) = @_; |
315 |
|
316 |
my $vb = new Gtk2::VBox; |
317 |
$self->add_mode_button ($vb, "auto", "auto"); |
318 |
$self->add_mode_button ($vb, "top", "top"); |
319 |
$self->add_mode_button ($vb, "above floor", "above"); |
320 |
$self->add_mode_button ($vb, "below floor", "below"); |
321 |
$self->add_mode_button ($vb, "bottom", "bottom"); |
322 |
|
323 |
$vb->pack_start ($self->{place_clean} = Gtk2::CheckButton->new ('place clean'), 0, 1, 0); |
324 |
|
325 |
$self->tool_widget ($vb); |
326 |
} |
327 |
|
328 |
sub want_cursor { 0 } |
329 |
|
330 |
sub begin { |
331 |
my ($self, $map, $x, $y) = @_; |
332 |
|
333 |
$self->SUPER::begin ($map, $x, $y); |
334 |
$self->edit ($map, $x, $y); |
335 |
} |
336 |
|
337 |
sub edit { |
338 |
my ($self, $map, $x, $y) = @_; |
339 |
|
340 |
my $pick = $::MAINWIN->get_pick; |
341 |
my $as = $map->get ($x, $y); |
342 |
|
343 |
if ($self->{place_clean}->get_active) { |
344 |
$pick = { _name => $pick->{_name} }; |
345 |
} |
346 |
|
347 |
$self->stack_action ($as, dclone ($pick)); |
348 |
$map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch)); |
349 |
} |
350 |
|
351 |
sub end { |
352 |
my ($self, $map, $x, $y, $mape) = @_; |
353 |
# $::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y); |
354 |
# now actualize stack and attr editor |
355 |
$::MAINWIN->update_stack_view ($map, $x, $y); |
356 |
|
357 |
my $cstack = $map->get ($x, $y); |
358 |
|
359 |
my $arch = $cstack->[-1]; |
360 |
|
361 |
# virtual... grmbl.... |
362 |
# FIXME: I have to patch the stack of the real arch??? argl.. how?? |
363 |
if ($arch->{_virtual}) { |
364 |
$x = $arch->{virtual_x}; |
365 |
$y = $arch->{virtual_y}; |
366 |
$arch = $arch->{_virtual}; |
367 |
$cstack = $map->get ($x, $y); |
368 |
} |
369 |
|
370 |
|
371 |
$::MAINWIN->update_attr_editor ($arch, sub { |
372 |
$map->change_begin (ref $self); |
373 |
$map->change_stack ($x, $y, $cstack); |
374 |
# XXX: Put this into a generic function!!! See also EditTools.pm |
375 |
# FIXME: Fix the automatic update on undo here! |
376 |
if (my $changeset = $map->change_end) { |
377 |
splice @{ $map->{undo_stack} ||= [] }, |
378 |
$map->{undo_stack_pos}++, 1e6, |
379 |
$changeset; |
380 |
} |
381 |
}); |
382 |
|
383 |
|
384 |
$self->SUPER::end ($map, $x, $y, $mape); |
385 |
} |
386 |
|
387 |
sub stack_action { |
388 |
my ($self, $stack, $arch) = @_; |
389 |
|
390 |
my $m = $self->get_mode; |
391 |
|
392 |
if ($m eq 'top') { |
393 |
if (@$stack == 0 or $stack->[-1]->{_name} ne $arch->{_name}) { |
394 |
push @$stack, $arch; |
395 |
} |
396 |
|
397 |
} elsif ($m eq 'bottom') { |
398 |
if (@$stack == 0 or $stack->[0]->{_name} ne $arch->{_name}) { |
399 |
unshift @$stack, $arch; |
400 |
} |
401 |
|
402 |
} elsif ($m eq 'above') { |
403 |
my $fidx = stack_find_floor ($stack, 'from_top'); |
404 |
|
405 |
if (@$stack == 0 |
406 |
or not ($stack->[$fidx + 1]) |
407 |
or $stack->[$fidx + 1]->{_name} ne $arch->{_name}) |
408 |
{ |
409 |
splice (@$stack, $fidx + 1, 0, $arch); |
410 |
} |
411 |
|
412 |
} elsif ($m eq 'below') { |
413 |
my $fidx = stack_find_floor ($stack, 'from_bottom'); |
414 |
|
415 |
if (@$stack == 0 |
416 |
or $fidx == 0 |
417 |
or not ($stack->[$fidx - 1]) |
418 |
or $stack->[$fidx - 1]->{_name} ne $arch->{_name}) |
419 |
{ |
420 |
splice (@$stack, $fidx, 0, $arch); |
421 |
} |
422 |
|
423 |
} elsif ($m eq 'auto') { |
424 |
my $fidx = stack_find_floor ($stack, 'from_top'); |
425 |
my $widx = stack_find_wall ($stack); |
426 |
|
427 |
if (arch_is_floor ($arch)) { # we want to place a floor tile |
428 |
|
429 |
if (arch_is_floor ($stack->[$fidx])) { # replace |
430 |
$stack->[$fidx] = $arch; |
431 |
|
432 |
} else { # insert on bottom |
433 |
unshift @$stack, $arch; |
434 |
} |
435 |
|
436 |
} elsif (arch_is_wall ($arch)) { # we want to place a wall |
437 |
|
438 |
if (arch_is_wall ($stack->[$widx])) { # replace |
439 |
$stack->[$widx] = $arch; |
440 |
|
441 |
} else { # insert above floor |
442 |
splice (@$stack, $fidx + 1, 0, $arch); |
443 |
} |
444 |
|
445 |
} else { |
446 |
|
447 |
if (arch_is_wall ($stack->[$widx])) { |
448 |
# if we have a wall above the floor, replace it with the to place item |
449 |
$stack->[$widx] = $arch; |
450 |
return; |
451 |
} |
452 |
|
453 |
if (@$stack == 0 |
454 |
or not ($stack->[-1]) |
455 |
or $stack->[-1]->{_name} ne $arch->{_name}) |
456 |
{ |
457 |
push @$stack, $arch; |
458 |
} |
459 |
} |
460 |
} |
461 |
} |
462 |
|
463 |
package GCE::EditAction::Select; |
464 |
use GCE::Util; |
465 |
use Gtk2; |
466 |
use Crossfire; |
467 |
use Storable qw/dclone/; |
468 |
use strict; |
469 |
|
470 |
our @ISA = qw/GCE::EditAction::RadioModed/; |
471 |
|
472 |
sub name { 'select' } |
473 |
|
474 |
sub special_arrow { 'GDK_CIRCLE' } |
475 |
|
476 |
sub init { |
477 |
my ($self) = @_; |
478 |
|
479 |
my $vb = new Gtk2::VBox; |
480 |
|
481 |
$vb->pack_start (my $bt = Gtk2::Button->new_with_mnemonic ("_copy"), 0, 1, 0); |
482 |
$bt->signal_connect (clicked => sub { $self->copy }); |
483 |
$vb->pack_start ($self->{paste_top} = Gtk2::CheckButton->new ('paste on top'), 0, 1, 0); |
484 |
$vb->pack_start (my $bt = Gtk2::Button->new_with_mnemonic ("paste (_v)"), 0, 1, 0); |
485 |
$bt->signal_connect (clicked => sub { $self->paste }); |
486 |
$vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0); |
487 |
$self->add_mode_button ($vb, "place", "place"); |
488 |
$self->add_mode_button ($vb, "erase", "erase"); |
489 |
$self->add_mode_button ($vb, "perl", "perl"); |
490 |
$vb->pack_start (my $bt = Gtk2::Button->new_with_mnemonic ("i_nvoke"), 0, 1, 0); |
491 |
$bt->signal_connect (clicked => sub { $self->invoke }); |
492 |
|
493 |
$self->tool_widget ($vb); |
494 |
} |
495 |
|
496 |
sub copy { |
497 |
my ($self) = @_; |
498 |
|
499 |
return unless $self->{selection}->{a}; |
500 |
my ($x1, $y1) = @{$self->{selection}->{a}}; |
501 |
my ($x2, $y2) = @{$self->{selection}->{b}}; |
502 |
|
503 |
if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) } |
504 |
if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) } |
505 |
|
506 |
my $map = $self->{selection}->{map}; |
507 |
|
508 |
$self->{copy_coords} = [$x1, $y1, $x2, $y2]; |
509 |
$self->{copy}; |
510 |
for (my $x = $x1; $x <= $x2; $x++) { |
511 |
for (my $y = $y1; $y <= $y2; $y++) { |
512 |
$self->{copy}->[$x - $x1]->[$y - $y1] = $map->get ($x, $y); |
513 |
} |
514 |
} |
515 |
} |
516 |
|
517 |
sub paste { |
518 |
my ($self, $map, $xp, $yp) = @_; |
519 |
|
520 |
return unless $self->{selection}->{a}; |
521 |
|
522 |
my ($x1, $y1); |
523 |
|
524 |
if (defined $xp) { |
525 |
($x1, $y1) = ($xp, $yp); |
526 |
} else { |
527 |
($x1, $y1) = @{$self->{selection}->{a}}; |
528 |
} |
529 |
|
530 |
$map ||= $self->{selection}->{map}; |
531 |
|
532 |
my $p_o_top = $self->{paste_top}->get_active * 1; |
533 |
|
534 |
my $w = $self->{copy_coords}->[2] - $self->{copy_coords}->[0]; |
535 |
my $h = $self->{copy_coords}->[3] - $self->{copy_coords}->[1]; |
536 |
$self->{copy}; |
537 |
$self->SUPER::begin ($map, $x1, $y1); |
538 |
for (my $x = $x1; $x <= ($x1 + $w); $x++) { |
539 |
for (my $y = $y1; $y <= ($y1 + $h); $y++) { |
540 |
my $cstck = $map->get ($x, $y); |
541 |
|
542 |
if ($p_o_top) { |
543 |
push @$cstck, @{dclone ($self->{copy}->[$x - $x1]->[$y - $y1] || [])}; |
544 |
$map->change_stack ($x, $y, $cstck); |
545 |
} else { |
546 |
$map->change_stack ($x, $y, dclone ($self->{copy}->[$x - $x1]->[$y - $y1] || [])); |
547 |
} |
548 |
} |
549 |
} |
550 |
$self->SUPER::end ($map); |
551 |
$map->invalidate_all; |
552 |
} |
553 |
|
554 |
sub invoke { |
555 |
my ($self) = @_; |
556 |
|
557 |
return unless $self->{selection}->{a}; |
558 |
my ($x1, $y1) = @{$self->{selection}->{a}}; |
559 |
my ($x2, $y2) = @{$self->{selection}->{b}}; |
560 |
|
561 |
if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) } |
562 |
if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) } |
563 |
|
564 |
my $map = $self->{selection}->{map}; |
565 |
|
566 |
my $m = $self->get_mode; |
567 |
$self->SUPER::begin ($map, $x1, $y1); |
568 |
for (my $x = $x1; $x <= $x2; $x++) { |
569 |
for (my $y = $y1; $y <= $y2; $y++) { |
570 |
if ($m eq 'place') { |
571 |
$::MAINWIN->{edit_collection}{place}->edit ($map, $x, $y); |
572 |
} elsif ($m eq 'erase') { |
573 |
$::MAINWIN->{edit_collection}{erase}->edit ($map, $x, $y); |
574 |
} elsif ($m eq 'perl') { |
575 |
$::MAINWIN->{edit_collection}{perl}->edit ($map, $x, $y); |
576 |
} |
577 |
} |
578 |
} |
579 |
$self->SUPER::end ($map); |
580 |
} |
581 |
|
582 |
sub want_cursor { 0 } |
583 |
|
584 |
sub begin { |
585 |
my ($self, $map, $x, $y) = @_; |
586 |
|
587 |
delete $self->{selection}; |
588 |
|
589 |
$self->{selection}->{a} = [$x, $y]; |
590 |
$self->edit ($map, $x, $y); |
591 |
} |
592 |
|
593 |
sub end { |
594 |
} |
595 |
|
596 |
sub edit { |
597 |
my ($self, $map, $x, $y) = @_; |
598 |
|
599 |
$self->{selection}->{b} = [$x, $y]; |
600 |
$self->{selection}->{map} = $map; |
601 |
$self->update_overlay ($map); |
602 |
} |
603 |
|
604 |
sub update_overlay { |
605 |
my ($self, $map) = @_; |
606 |
|
607 |
my ($x1, $y1) = @{$self->{selection}->{a}}; |
608 |
my ($x2, $y2) = @{$self->{selection}->{b}}; |
609 |
|
610 |
if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) } |
611 |
if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) } |
612 |
|
613 |
my $w = ($x2 - $x1) + 1; |
614 |
my $h = ($y2 - $y1) + 1; |
615 |
|
616 |
$map->overlay (selection => |
617 |
$x1 * TILESIZE, $y1 * TILESIZE, |
618 |
$w * TILESIZE, $h * TILESIZE, |
619 |
sub { |
620 |
my ($self, $x, $y) = @_; |
621 |
$self->{window}->draw_rectangle ( |
622 |
$_ & 1 ? $self->style->black_gc : $self->style->white_gc, |
623 |
0, |
624 |
$x + $_, $y + $_, |
625 |
($w * TILESIZE) - 1 - $_ * 2, |
626 |
($h * TILESIZE) - 1 - $_ * 2 |
627 |
) for 0..3; |
628 |
} |
629 |
); |
630 |
} |
631 |
|
632 |
package GCE::EditAction::Erase; |
633 |
use GCE::Util; |
634 |
use Gtk2; |
635 |
use strict; |
636 |
|
637 |
our @ISA = qw/GCE::EditAction::RadioModed/; |
638 |
|
639 |
sub name { 'erase' } |
640 |
|
641 |
sub want_cursor { 0 } |
642 |
|
643 |
sub special_arrow { 'GDK_DIAMOND_CROSS' } |
644 |
|
645 |
sub init { |
646 |
my ($self) = @_; |
647 |
|
648 |
my $vb = new Gtk2::VBox; |
649 |
$self->add_mode_button ($vb, "top", "top"); |
650 |
$self->add_mode_button ($vb, "walls", "walls"); |
651 |
$self->add_mode_button ($vb, "above floor", "above", 'default'); |
652 |
$self->add_mode_button ($vb, "floor", "floor"); |
653 |
$self->add_mode_button ($vb, "below floor", "below"); |
654 |
$self->add_mode_button ($vb, "bottom", "bottom"); |
655 |
$self->add_mode_button ($vb, "pick match", "match"); |
656 |
|
657 |
$self->tool_widget ($vb); |
658 |
|
659 |
$vb->pack_start ($self->{no_wall_check} = Gtk2::CheckButton->new ("protect walls"), 0, 1, 0); |
660 |
$vb->pack_start ($self->{no_monsters_check} = Gtk2::CheckButton->new ("protect monsters"), 0, 1, 0); |
661 |
} |
662 |
|
663 |
sub check_excluded { |
664 |
my ($self, $arch) = @_; |
665 |
|
666 |
my $a1 = $self->{no_monsters_check}->get_active; |
667 |
my $a2 = $self->{no_wall_check}->get_active; |
668 |
|
669 |
my $r = ($self->{no_wall_check}->get_active && arch_is_wall ($arch)) |
670 |
|| ($self->{no_monsters_check}->get_active && arch_is_monster ($arch)); |
671 |
return $r; |
672 |
} |
673 |
|
674 |
sub begin { |
675 |
my ($self, $map, $x, $y) = @_; |
676 |
|
677 |
$self->SUPER::begin ($map, $x, $y); |
678 |
$self->edit ($map, $x, $y); |
679 |
} |
680 |
|
681 |
sub edit { |
682 |
my ($self, $map, $x, $y) = @_; |
683 |
|
684 |
my $as = $map->get ($x, $y); |
685 |
$self->stack_action ($as); |
686 |
$map->change_stack ($x, $y, $as); |
687 |
} |
688 |
|
689 |
sub stack_action { |
690 |
my ($self, $stack) = @_; |
691 |
|
692 |
my $m = $self->get_mode; |
693 |
|
694 |
if ($m eq 'top') { |
695 |
pop @$stack; |
696 |
|
697 |
} elsif ($m eq 'bottom') { |
698 |
shift @$stack; |
699 |
|
700 |
} elsif ($m eq 'above') { |
701 |
my $fidx = stack_find_floor ($stack, 'from_top'); |
702 |
|
703 |
if (arch_is_floor ($stack->[$fidx]) and $stack->[$fidx + 1]) { |
704 |
splice (@$stack, $fidx + 1, 1) |
705 |
unless $self->check_excluded ($stack->[$fidx + 1]) |
706 |
|
707 |
} elsif (not arch_is_floor ($stack->[$fidx])) { |
708 |
splice (@$stack, $fidx, 1) |
709 |
unless $self->check_excluded ($stack->[$fidx]) |
710 |
|
711 |
} |
712 |
|
713 |
} elsif ($m eq 'below') { |
714 |
my $fidx = stack_find_floor ($stack, 'from_bottom'); |
715 |
|
716 |
if ($fidx > 0 and not arch_is_floor ($stack->[$fidx - 1])) { |
717 |
splice (@$stack, $fidx - 1, 1) |
718 |
unless $self->check_excluded ($stack->[$fidx - 1]) |
719 |
|
720 |
} elsif (not arch_is_floor ($stack->[$fidx])) { # no floor found |
721 |
splice (@$stack, $fidx, 1) |
722 |
unless $self->check_excluded ($stack->[$fidx]) |
723 |
|
724 |
} |
725 |
|
726 |
} elsif ($m eq 'walls') { |
727 |
my $widx = stack_find_wall ($stack, 'from_top'); |
728 |
|
729 |
while (arch_is_wall ($stack->[$widx])) { |
730 |
splice (@$stack, $widx, 1); |
731 |
$widx = stack_find_wall ($stack, 'from_top') |
732 |
} |
733 |
|
734 |
} elsif ($m eq 'floor') { |
735 |
my $fidx = stack_find_floor ($stack, 'from_top'); |
736 |
|
737 |
while (arch_is_floor ($stack->[$fidx])) { |
738 |
splice (@$stack, $fidx, 1); |
739 |
$fidx = stack_find_floor ($stack, 'from_top') |
740 |
} |
741 |
|
742 |
} elsif ($m eq 'match') { |
743 |
my $pick_name = $::MAINWIN->get_pick ()->{_name}; |
744 |
my $idx = stack_find ($stack, 'from_top', sub { $_[0]->{_name} eq $pick_name }); |
745 |
|
746 |
while ($stack->[$idx] and $stack->[$idx]->{_name} eq $pick_name) { |
747 |
splice (@$stack, $idx, 1); |
748 |
$idx = stack_find ($stack, 'from_top', sub { $_[0]->{_name} eq $pick_name }); |
749 |
} |
750 |
} |
751 |
} |
752 |
|
753 |
package GCE::EditAction::ConnectExit; |
754 |
use Storable qw/dclone/; |
755 |
use GCE::Util; |
756 |
use Gtk2; |
757 |
use File::Spec::Functions; |
758 |
use strict; |
759 |
|
760 |
our @ISA = qw/GCE::EditAction::RadioModed/; |
761 |
|
762 |
sub name { 'connectexit' } |
763 |
|
764 |
sub init { |
765 |
my ($self) = @_; |
766 |
|
767 |
my $vb = new Gtk2::VBox; |
768 |
$self->add_mode_button ($vb, "exit", "exit"); |
769 |
# $self->add_mode_button ($vb, "triggers", "trig"); |
770 |
$vb->pack_start ($self->{sel_edt} = Gtk2::Entry->new, 0, 1, 0); |
771 |
$self->{sel_edt}->set_text (catfile ($::CFG->{MAPDIR})); |
772 |
|
773 |
$vb->pack_start ($self->{sel_lbl} = Gtk2::Label->new, 0, 0, 0); |
774 |
$self->tool_widget ($vb); |
775 |
} |
776 |
|
777 |
sub want_cursor { 0 } |
778 |
|
779 |
sub begin { |
780 |
my ($self, $map, $x, $y, $mapedit) = @_; |
781 |
|
782 |
$self->edit ($map, $x, $y, $mapedit); |
783 |
} |
784 |
|
785 |
sub edit { |
786 |
my ($self, $map, $x, $y, $mapedit) = @_; |
787 |
|
788 |
my $pick = $::MAINWIN->get_pick; |
789 |
my $as = $map->get ($x, $y); |
790 |
|
791 |
my $exit; |
792 |
for (@$as) { |
793 |
if (arch_is_exit ($_)) { |
794 |
$exit = $_; |
795 |
} |
796 |
} |
797 |
|
798 |
my $ent = $self->{sel_edt}->get_text; |
799 |
if ($exit) { |
800 |
if ($self->{sel_exit}) { |
801 |
|
802 |
$exit->{hp} = $self->{sel_exit}->[3]; |
803 |
$exit->{sp} = $self->{sel_exit}->[4]; |
804 |
$exit->{slaying} = File::Spec->abs2rel ($self->{sel_exit}->[5], $ent); |
805 |
|
806 |
my $exit2 = $self->{sel_exit}->[0]; |
807 |
|
808 |
$exit2->{hp} = $x; |
809 |
$exit2->{sp} = $y; |
810 |
$exit2->{slaying} = File::Spec->abs2rel ($mapedit->{path}, $ent); |
811 |
|
812 |
unless ($exit2->{slaying} =~ m/^\.\./) { |
813 |
$exit2->{slaying} = '/' . $exit2->{slaying}; |
814 |
} |
815 |
unless ($exit->{slaying} =~ m/^\.\./) { |
816 |
$exit->{slaying} = '/' . $exit->{slaying}; |
817 |
} |
818 |
|
819 |
$self->SUPER::begin ($map, $x, $y, $mapedit); |
820 |
$map->change_stack ($x, $y, $as); |
821 |
$self->SUPER::end ($map); |
822 |
$self->SUPER::begin ($self->{sel_exit}->[1], $exit->{hp}, $exit->{sp}); |
823 |
$self->{sel_exit}->[1]->change_stack ($exit->{hp}, $exit->{sp}, $self->{sel_exit}->[2]); |
824 |
$self->SUPER::end ($self->{sel_exit}->[1]); |
825 |
|
826 |
quick_msg ($mapedit, "$exit->{slaying} ($x:$y) $exit->{_name} <=> $exit2->{slaying} ($exit2->{hp}:$exit2->{sp}) $exit2->{_name}", 0); |
827 |
|
828 |
$::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y); |
829 |
|
830 |
$self->{sel_exit} = undef; |
831 |
$self->{sel_lbl}->set_text (''); |
832 |
} else { |
833 |
$self->{sel_lbl}->set_text ("src: ($x:$y) $exit->{_name}"); |
834 |
$self->{sel_exit} = [$exit, $map, $as, $x, $y, $mapedit->{path}]; |
835 |
} |
836 |
} else { |
837 |
if ($self->{sel_exit}) { |
838 |
|
839 |
my $exit2 = $self->{sel_exit}->[0]; |
840 |
|
841 |
$exit2->{hp} = $x; |
842 |
$exit2->{sp} = $y; |
843 |
$exit2->{slaying} = File::Spec->abs2rel ($mapedit->{path}, $ent); |
844 |
|
845 |
unless ($exit2->{slaying} =~ m/^\.\./) { |
846 |
$exit2->{slaying} = '/' . $exit2->{slaying}; |
847 |
} |
848 |
|
849 |
$self->SUPER::begin ($self->{sel_exit}->[1], $exit->{hp}, $exit->{sp}); |
850 |
$self->{sel_exit}->[1]->change_stack ($self->{sel_exit}->[3], $self->{sel_exit}->[4], $self->{sel_exit}->[2]); |
851 |
$self->SUPER::end ($self->{sel_exit}->[1]); |
852 |
|
853 |
quick_msg ($mapedit, "$self->{sel_exit}->[5] ($self->{sel_exit}->[3]:$self->{sel_exit}->[4]) $self->{sel_exit}->[0]->{_name} => $exit2->{slaying} ($x:$y) $exit2->{_name}", 0); |
854 |
|
855 |
$::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y); |
856 |
|
857 |
$self->{sel_exit} = undef; |
858 |
$self->{sel_lbl}->set_text (''); |
859 |
} else { |
860 |
quick_msg ($mapedit, "no exit object found"); |
861 |
} |
862 |
} |
863 |
|
864 |
# $self->stack_action ($as, dclone ($pick)); |
865 |
#$map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch)); |
866 |
} |
867 |
|
868 |
sub end {} |
869 |
|
870 |
|
871 |
=head1 AUTHOR |
872 |
|
873 |
Marc Lehmann <schmorp@schmorp.de> |
874 |
http://home.schmorp.de/ |
875 |
|
876 |
Robin Redeker <elmex@ta-sa.org> |
877 |
http://www.ta-sa.org/ |
878 |
|
879 |
=cut |
880 |
|
881 |
1 |
882 |
|