ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/EditAction.pm
Revision: 1.16
Committed: Thu Mar 16 11:59:34 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.15: +90 -0 lines
Log Message:
implemented connect tool for exits and some minor improvements in saving

File Contents

# Content
1 package GCE::EditAction;
2
3 =head1 NAME
4
5 GCE::EditActions - this is the abstraction of edit actions (placing, deleting, etc.)
6
7 =cut
8
9 use Gtk2;
10 use Gtk2::Gdk::Keysyms;
11 use Gtk2::SimpleMenu;
12
13 use 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/;
123
124 sub name { 'pick' }
125
126 sub want_cursor { 0 }
127
128 sub special_arrow { 'GDK_HAND2' }
129
130 sub begin {
131 my ($self, $map, $x, $y) = @_;
132
133 $self->SUPER::begin ($map, $x, $y);
134 $self->edit ($map, $x, $y);
135 }
136
137 sub edit {
138 my ($self, $map, $x, $y) = @_;
139
140 my $cstack = $map->get ($x, $y);
141
142 my $arch = $cstack->[-1];
143
144 # virtual... grmbl....
145 # FIXME: I have to patch the stack of the real arch??? argl.. how??
146 if ($arch->{_virtual}) {
147 $x = $arch->{virtual_x};
148 $y = $arch->{virtual_y};
149 $arch = $arch->{_virtual};
150 $cstack = $map->get ($x, $y);
151 }
152
153 $::MAINWIN->set_pick ($arch)
154 if @$cstack;
155
156 $::MAINWIN->update_attr_editor ($arch, sub {
157 $map->change_begin (ref $self);
158 $map->change_stack ($x, $y, $cstack);
159 # XXX: Put this into a generic function!!! See also EditTools.pm
160 # FIXME: Fix the automatic update on undo here!
161 if (my $changeset = $map->change_end) {
162 splice @{ $map->{undo_stack} ||= [] },
163 $map->{undo_stack_pos}++, 1e6,
164 $changeset;
165 }
166 });
167
168 $::MAINWIN->update_stack_view ($map, $x, $y);
169 }
170
171 package GCE::EditAction::Perl;
172
173 use GCE::Util;
174 use Gtk2;
175 use strict;
176
177 our @ISA = qw/GCE::EditAction/;
178
179 sub name { 'perl' }
180
181 sub special_arrow { 'GDK_HEART' }
182
183 sub init {
184 my ($self) = @_;
185
186 my $vb = new Gtk2::VBox;
187 $vb->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
188 $sw->add ($self->{txt} = Gtk2::TextView->new);
189
190 $self->tool_widget ($vb);
191 }
192
193 sub want_cursor { 0 }
194
195 sub begin {
196 my ($self, $map, $x, $y) = @_;
197
198 $self->SUPER::begin ($map, $x, $y);
199 $self->edit ($map, $x, $y);
200 }
201
202 sub edit {
203 my ($self, $map, $x, $y) = @_;
204
205 my $pick = $::MAINWIN->get_pick;
206 my $as = $map->get ($x, $y);
207
208 $as = $self->eval ($map, $pick, $as, $x, $y);
209 $map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch));
210 }
211
212 sub eval {
213 my ($self, $map, $pick, $stack, $x, $y) = @_;
214 my $buf = $self->{txt}->get_buffer;
215 my $code = $buf->get_text ($buf->get_start_iter, $buf->get_end_iter, 0);
216
217 eval $code;
218 return $stack;
219 }
220
221 package GCE::EditAction::Place;
222 use Storable qw/dclone/;
223 use GCE::Util;
224 use Gtk2;
225 use strict;
226
227 our @ISA = qw/GCE::EditAction::RadioModed/;
228
229 sub name { 'place' }
230
231 sub init {
232 my ($self) = @_;
233
234 my $vb = new Gtk2::VBox;
235 $self->add_mode_button ($vb, "auto", "auto");
236 $self->add_mode_button ($vb, "top", "top");
237 $self->add_mode_button ($vb, "above floor", "above");
238 $self->add_mode_button ($vb, "below floor", "below");
239 $self->add_mode_button ($vb, "bottom", "bottom");
240
241 $self->tool_widget ($vb);
242 }
243
244 sub want_cursor { 0 }
245
246 sub begin {
247 my ($self, $map, $x, $y) = @_;
248
249 $self->SUPER::begin ($map, $x, $y);
250 $self->edit ($map, $x, $y);
251 }
252
253 sub edit {
254 my ($self, $map, $x, $y) = @_;
255
256 my $pick = $::MAINWIN->get_pick;
257 my $as = $map->get ($x, $y);
258
259 $self->stack_action ($as, dclone ($pick));
260 $map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch));
261 }
262
263 sub stack_action {
264 my ($self, $stack, $arch) = @_;
265
266 my $m = $self->get_mode;
267
268 if ($m eq 'top') {
269 if (@$stack == 0 or $stack->[-1]->{_name} ne $arch->{_name}) {
270 push @$stack, $arch;
271 }
272
273 } elsif ($m eq 'bottom') {
274 if (@$stack == 0 or $stack->[0]->{_name} ne $arch->{_name}) {
275 unshift @$stack, $arch;
276 }
277
278 } elsif ($m eq 'above') {
279 my $fidx = stack_find_floor ($stack, 'from_top');
280
281 if (@$stack == 0
282 or not ($stack->[$fidx + 1])
283 or $stack->[$fidx + 1]->{_name} ne $arch->{_name})
284 {
285 splice (@$stack, $fidx + 1, 0, $arch);
286 }
287
288 } elsif ($m eq 'below') {
289 my $fidx = stack_find_floor ($stack, 'from_bottom');
290
291 if (@$stack == 0
292 or $fidx == 0
293 or not ($stack->[$fidx - 1])
294 or $stack->[$fidx - 1]->{_name} ne $arch->{_name})
295 {
296 splice (@$stack, $fidx, 0, $arch);
297 }
298
299 } elsif ($m eq 'auto') {
300 my $fidx = stack_find_floor ($stack, 'from_top');
301 my $widx = stack_find_wall ($stack);
302
303 if (arch_is_floor ($arch)) { # we want to place a floor tile
304
305 if (arch_is_floor ($stack->[$fidx])) { # replace
306 $stack->[$fidx] = $arch;
307
308 } else { # insert on bottom
309 unshift @$stack, $arch;
310 }
311
312 } elsif (arch_is_wall ($arch)) { # we want to place a wall
313
314 if (arch_is_wall ($stack->[$widx])) { # replace
315 $stack->[$widx] = $arch;
316
317 } else { # insert above floor
318 splice (@$stack, $fidx + 1, 0, $arch);
319 }
320
321 } else {
322
323 if (arch_is_wall ($stack->[$widx])) {
324 # if we have a wall above the floor, replace it with the to place item
325 $stack->[$widx] = $arch;
326 return;
327 }
328
329 if (@$stack == 0
330 or not ($stack->[-1])
331 or $stack->[-1]->{_name} ne $arch->{_name})
332 {
333 push @$stack, $arch;
334 }
335 }
336 }
337 }
338
339 package GCE::EditAction::Select;
340 use GCE::Util;
341 use Gtk2;
342 use Crossfire;
343 use Storable qw/dclone/;
344 use strict;
345
346 our @ISA = qw/GCE::EditAction::RadioModed/;
347
348 sub name { 'select' }
349
350 sub special_arrow { 'GDK_CIRCLE' }
351
352 sub init {
353 my ($self) = @_;
354
355 my $vb = new Gtk2::VBox;
356
357 $vb->pack_start (my $bt = Gtk2::Button->new ("copy"), 0, 1, 0);
358 $bt->signal_connect (clicked => sub { $self->copy });
359 $vb->pack_start ($self->{paste_top} = Gtk2::CheckButton->new ('paste on top'), 0, 1, 0);
360 $vb->pack_start (my $bt = Gtk2::Button->new ("paste"), 0, 1, 0);
361 $bt->signal_connect (clicked => sub { $self->paste });
362 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
363 $self->add_mode_button ($vb, "place", "place");
364 $self->add_mode_button ($vb, "erase", "erase");
365 $self->add_mode_button ($vb, "perl", "perl");
366 $vb->pack_start (my $bt = Gtk2::Button->new ("invoke"), 0, 1, 0);
367 $bt->signal_connect (clicked => sub { $self->invoke });
368
369 $self->tool_widget ($vb);
370 }
371
372 sub copy {
373 my ($self) = @_;
374
375 return unless $self->{selection}->{a};
376 my ($x1, $y1) = @{$self->{selection}->{a}};
377 my ($x2, $y2) = @{$self->{selection}->{b}};
378
379 if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) }
380 if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) }
381
382 my $map = $self->{selection}->{map};
383
384 $self->{copy_coords} = [$x1, $y1, $x2, $y2];
385 $self->{copy};
386 for (my $x = $x1; $x <= $x2; $x++) {
387 for (my $y = $y1; $y <= $y2; $y++) {
388 $self->{copy}->[$x - $x1]->[$y - $y1] = $map->get ($x, $y);
389 }
390 }
391 }
392
393 sub paste {
394 my ($self, $xp, $yp) = @_;
395
396 return unless $self->{selection}->{a};
397
398 my ($x1, $y1);
399
400 if (defined $xp) {
401 ($x1, $y1) = ($xp, $yp);
402 } else {
403 ($x1, $y1) = @{$self->{selection}->{a}};
404 }
405
406 my $map = $self->{selection}->{map};
407
408 my $p_o_top = $self->{paste_top}->get_active * 1;
409
410 my $w = $self->{copy_coords}->[2] - $self->{copy_coords}->[0];
411 my $h = $self->{copy_coords}->[3] - $self->{copy_coords}->[1];
412 $self->{copy};
413 $self->SUPER::begin ($map, $x1, $y1);
414 for (my $x = $x1; $x <= ($x1 + $w); $x++) {
415 for (my $y = $y1; $y <= ($y1 + $h); $y++) {
416 my $cstck = $map->get ($x, $y);
417
418 if ($p_o_top) {
419 push @$cstck, @{dclone ($self->{copy}->[$x - $x1]->[$y - $y1] || [])};
420 $map->change_stack ($x, $y, $cstck);
421 } else {
422 $map->change_stack ($x, $y, dclone ($self->{copy}->[$x - $x1]->[$y - $y1] || []));
423 }
424 }
425 }
426 $self->SUPER::end ($map);
427 $map->invalidate_all;
428 }
429
430 sub invoke {
431 my ($self) = @_;
432
433 return unless $self->{selection}->{a};
434 my ($x1, $y1) = @{$self->{selection}->{a}};
435 my ($x2, $y2) = @{$self->{selection}->{b}};
436
437 if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) }
438 if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) }
439
440 my $map = $self->{selection}->{map};
441
442 my $m = $self->get_mode;
443 $self->SUPER::begin ($map, $x1, $y1);
444 for (my $x = $x1; $x <= $x2; $x++) {
445 for (my $y = $y1; $y <= $y2; $y++) {
446 if ($m eq 'place') {
447 $::MAINWIN->{edit_collection}{place}->edit ($map, $x, $y);
448 } elsif ($m eq 'erase') {
449 $::MAINWIN->{edit_collection}{erase}->edit ($map, $x, $y);
450 } elsif ($m eq 'perl') {
451 $::MAINWIN->{edit_collection}{perl}->edit ($map, $x, $y);
452 }
453 }
454 }
455 $self->SUPER::end ($map);
456 }
457
458 sub want_cursor { 0 }
459
460 sub begin {
461 my ($self, $map, $x, $y) = @_;
462
463 delete $self->{selection};
464
465 $self->{selection}->{a} = [$x, $y];
466 $self->edit ($map, $x, $y);
467 }
468
469 sub end {
470 }
471
472 sub edit {
473 my ($self, $map, $x, $y) = @_;
474
475 $self->{selection}->{b} = [$x, $y];
476 $self->{selection}->{map} = $map;
477 $self->update_overlay ($map);
478 }
479
480 sub update_overlay {
481 my ($self, $map) = @_;
482
483 my ($x1, $y1) = @{$self->{selection}->{a}};
484 my ($x2, $y2) = @{$self->{selection}->{b}};
485
486 if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) }
487 if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) }
488
489 my $w = ($x2 - $x1) + 1;
490 my $h = ($y2 - $y1) + 1;
491
492 $map->overlay (selection =>
493 $x1 * TILESIZE, $y1 * TILESIZE,
494 $w * TILESIZE, $h * TILESIZE,
495 sub {
496 my ($self, $x, $y) = @_;
497 $self->{window}->draw_rectangle (
498 $_ & 1 ? $self->style->black_gc : $self->style->white_gc,
499 0,
500 $x + $_, $y + $_,
501 ($w * TILESIZE) - 1 - $_ * 2,
502 ($h * TILESIZE) - 1 - $_ * 2
503 ) for 0..3;
504 }
505 );
506 }
507
508 package GCE::EditAction::Erase;
509 use GCE::Util;
510 use Gtk2;
511 use strict;
512
513 our @ISA = qw/GCE::EditAction::RadioModed/;
514
515 sub name { 'erase' }
516
517 sub want_cursor { 0 }
518
519 sub special_arrow { 'GDK_DIAMOND_CROSS' }
520
521 sub init {
522 my ($self) = @_;
523
524 my $vb = new Gtk2::VBox;
525 $self->add_mode_button ($vb, "top", "top");
526 $self->add_mode_button ($vb, "walls", "walls");
527 $self->add_mode_button ($vb, "above floor", "above", 'default');
528 $self->add_mode_button ($vb, "floor", "floor");
529 $self->add_mode_button ($vb, "below floor", "below");
530 $self->add_mode_button ($vb, "bottom", "bottom");
531 $self->add_mode_button ($vb, "pick match", "match");
532
533 $self->tool_widget ($vb);
534
535 $vb->pack_start ($self->{no_wall_check} = Gtk2::CheckButton->new ("exclude walls"), 0, 1, 0);
536 $vb->pack_start ($self->{no_monsters_check} = Gtk2::CheckButton->new ("exclude monsters"), 0, 1, 0);
537 }
538
539 sub check_excluded {
540 my ($self, $arch) = @_;
541
542 my $a1 = $self->{no_monsters_check}->get_active;
543 my $a2 = $self->{no_wall_check}->get_active;
544
545 my $r = ($self->{no_wall_check}->get_active && arch_is_wall ($arch))
546 || ($self->{no_monsters_check}->get_active && arch_is_monster ($arch));
547 return $r;
548 }
549
550 sub begin {
551 my ($self, $map, $x, $y) = @_;
552
553 $self->SUPER::begin ($map, $x, $y);
554 $self->edit ($map, $x, $y);
555 }
556
557 sub edit {
558 my ($self, $map, $x, $y) = @_;
559
560 my $as = $map->get ($x, $y);
561 $self->stack_action ($as);
562 $map->change_stack ($x, $y, $as);
563 }
564
565 sub stack_action {
566 my ($self, $stack) = @_;
567
568 my $m = $self->get_mode;
569
570 if ($m eq 'top') {
571 pop @$stack;
572
573 } elsif ($m eq 'bottom') {
574 shift @$stack;
575
576 } elsif ($m eq 'above') {
577 my $fidx = stack_find_floor ($stack, 'from_top');
578
579 if (arch_is_floor ($stack->[$fidx]) and $stack->[$fidx + 1]) {
580 splice (@$stack, $fidx + 1, 1)
581 unless $self->check_excluded ($stack->[$fidx + 1])
582
583 } elsif (not arch_is_floor ($stack->[$fidx])) {
584 splice (@$stack, $fidx, 1)
585 unless $self->check_excluded ($stack->[$fidx])
586
587 }
588
589 } elsif ($m eq 'below') {
590 my $fidx = stack_find_floor ($stack, 'from_bottom');
591
592 if ($fidx > 0 and not arch_is_floor ($stack->[$fidx - 1])) {
593 splice (@$stack, $fidx - 1, 1)
594 unless $self->check_excluded ($stack->[$fidx - 1])
595
596 } elsif (not arch_is_floor ($stack->[$fidx])) { # no floor found
597 splice (@$stack, $fidx, 1)
598 unless $self->check_excluded ($stack->[$fidx])
599
600 }
601
602 } elsif ($m eq 'walls') {
603 my $widx = stack_find_wall ($stack, 'from_top');
604
605 while (arch_is_wall ($stack->[$widx])) {
606 splice (@$stack, $widx, 1);
607 $widx = stack_find_wall ($stack, 'from_top')
608 }
609
610 } elsif ($m eq 'floor') {
611 my $fidx = stack_find_floor ($stack, 'from_top');
612
613 while (arch_is_floor ($stack->[$fidx])) {
614 splice (@$stack, $fidx, 1);
615 $fidx = stack_find_floor ($stack, 'from_top')
616 }
617
618 } elsif ($m eq 'match') {
619 my $pick_name = $::MAINWIN->get_pick ()->{_name};
620 my $idx = stack_find ($stack, 'from_top', sub { $_[0]->{_name} eq $pick_name });
621
622 while ($stack->[$idx] and $stack->[$idx]->{_name} eq $pick_name) {
623 splice (@$stack, $idx, 1);
624 $idx = stack_find ($stack, 'from_top', sub { $_[0]->{_name} eq $pick_name });
625 }
626 }
627 }
628
629 package GCE::EditAction::Connect;
630 use Storable qw/dclone/;
631 use GCE::Util;
632 use Gtk2;
633 use File::Spec::Functions;
634 use strict;
635
636 our @ISA = qw/GCE::EditAction::RadioModed/;
637
638 sub name { 'connect' }
639
640 sub init {
641 my ($self) = @_;
642
643 my $vb = new Gtk2::VBox;
644 $self->add_mode_button ($vb, "exit", "exit");
645 # $self->add_mode_button ($vb, "triggers", "trig");
646 $vb->pack_start ($self->{sel_lbl} = Gtk2::Label->new, 0, 0, 0);
647 $self->tool_widget ($vb);
648 }
649
650 sub want_cursor { 0 }
651
652 sub begin {
653 my ($self, $map, $x, $y, $mapedit) = @_;
654
655 $self->edit ($map, $x, $y, $mapedit);
656 }
657
658 sub edit {
659 my ($self, $map, $x, $y, $mapedit) = @_;
660
661 my $pick = $::MAINWIN->get_pick;
662 my $as = $map->get ($x, $y);
663
664 my $exit;
665 for (@$as) {
666 my $a = $Crossfire::ARCH{$_->{_name}};
667 if ($a->{type} eq '66') {
668 $exit = $_;
669 }
670 }
671
672 if ($exit) {
673 if ($self->{sel_exit}) {
674 my $ent = catfile ($Crossfire::LIB, 'maps');
675
676 $exit->{hp} = $self->{sel_exit}->[3];
677 $exit->{sp} = $self->{sel_exit}->[4];
678 $exit->{slaying} = File::Spec->abs2rel ($self->{sel_exit}->[5], $ent);
679
680 my $exit2 = $self->{sel_exit}->[0];
681
682 $exit2->{hp} = $x;
683 $exit2->{sp} = $y;
684 $exit2->{slaying} = File::Spec->abs2rel ($mapedit->{path}, $ent);
685
686 unless ($exit2->{slaying} =~ m/^\.\./) {
687 $exit2->{slaying} = '/' . $exit2->{slaying};
688 }
689 unless ($exit->{slaying} =~ m/^\.\./) {
690 $exit->{slaying} = '/' . $exit->{slaying};
691 }
692
693 $self->SUPER::begin ($map, $x, $y, $mapedit);
694 $map->change_stack ($x, $y, $as);
695 $self->SUPER::end ($map);
696 $self->SUPER::begin ($self->{sel_exit}->[1], $exit->{hp}, $exit->{sp});
697 $self->{sel_exit}->[1]->change_stack ($exit->{hp}, $exit->{sp}, $self->{sel_exit}->[2]);
698 $self->SUPER::end ($self->{sel_exit}->[1]);
699
700 quick_msg ($mapedit, "$exit->{slaying} ($x:$y) $exit->{_name} <=> $exit2->{slaying} ($exit2->{hp}:$exit2->{sp}) $exit2->{_name}", 0);
701
702 $::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y);
703
704 $self->{sel_exit} = undef;
705 $self->{sel_lbl}->set_text ('');
706 } else {
707 $self->{sel_lbl}->set_text ("src: ($x:$y) $exit->{_name}");
708 $self->{sel_exit} = [$exit, $map, $as, $x, $y, $mapedit->{path}];
709 }
710 } else {
711 quick_msg ($mapedit, "no exit object found");
712 }
713
714 # $self->stack_action ($as, dclone ($pick));
715 #$map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch));
716 }
717
718 sub end {}
719
720
721 =head1 AUTHOR
722
723 Marc Lehmann <schmorp@schmorp.de>
724 http://home.schmorp.de/
725
726 Robin Redeker <elmex@ta-sa.org>
727 http://www.ta-sa.org/
728
729 =cut
730
731 1
732