package GCE::EditAction; =head1 NAME GCE::EditActions - this is the abstraction of edit actions (placing, deleting, etc.) =cut use Gtk2; use Gtk2::Gdk::Keysyms; use Gtk2::SimpleMenu; use Crossfire; use Crossfire::MapWidget; use strict; sub new { my $class = shift; my $self = { @_ }; bless $self, $class; $self->init; return $self; } sub name { } # a unique name for this tool (for storing it in a hash in the main window) sub tool_widget { if ($_[1]) { $_[0]->{widget} = $_[1] } $_[0]->{widget} } sub init { } sub want_cursor { 1 } sub special_arrow { } # edits one tile of the map sub edit_one { my ($self, $x, $y) = @_; # do one edition } # edits a selection sub edit_selection { } # abstraction for edit_one and edit_selection ? # takes selection if present sub edit { my ($self, $map, $x, $y) = @_; } sub begin { my ($self, $map, $x, $y) = @_; $map->change_begin (ref $self); } sub end { my ($self, $map) = @_; if (my $changeset = $map->change_end) { splice @{ $map->{undo_stack} ||= [] }, $map->{undo_stack_pos}++, 1e6, $changeset; #TODO: limit undo stack size to some preconfigured limit } } package GCE::EditAction::RadioModed; our @ISA = qw/GCE::EditAction/; sub add_mode_button { my ($self, $vb, $lbl, $mode, $default) = @_; $vb->pack_start (my $b = Gtk2::RadioButton->new ($self->{place_radio_grp}, $lbl), 0, 1, 0); unless (defined $self->{place_radio_grp}) { $self->{place_radio_grp} = $b->get_group; unless (defined $default) { $b->set_active (1); $self->set_mode ($mode); } } if ($default) { $b->set_active (1); $self->set_mode ($mode); } $b->signal_connect (clicked => sub { $self->set_mode ($mode); }); } sub set_mode { my ($self, $mode) = @_; $self->{place_mode} = $mode; } sub get_mode { my ($self) = @_; $self->{place_mode} } sub init { my ($self) = @_; die "Implement me!!"; # my $vb = new Gtk2::VBox; # $self->_add_mode_button ($vb, "auto", "auto"); # $self->_add_mode_button ($vb, "top", "top"); # $self->_add_mode_button ($vb, "above floor", "above"); # $self->_add_mode_button ($vb, "below floor", "below"); # $self->_add_mode_button ($vb, "bottom", "bottom"); # # $self->{widget} = $vb; } package GCE::EditAction::Pick; use strict; our @ISA = qw/GCE::EditAction/; sub name { 'pick' } sub want_cursor { 0 } sub special_arrow { 'GDK_HAND2' } sub begin { my ($self, $map, $x, $y) = @_; $self->SUPER::begin ($map, $x, $y); $self->edit ($map, $x, $y); } sub edit { my ($self, $map, $x, $y) = @_; my $cstack = $map->get ($x, $y); my $arch = $cstack->[-1]; # virtual... grmbl.... # FIXME: I have to patch the stack of the real arch??? argl.. how?? if ($arch->{_virtual}) { $x = $arch->{virtual_x}; $y = $arch->{virtual_y}; $arch = $arch->{_virtual}; $cstack = $map->get ($x, $y); } $::MAINWIN->set_pick ($arch) if @$cstack; $::MAINWIN->update_attr_editor ($arch, sub { $map->change_begin (ref $self); $map->change_stack ($x, $y, $cstack); # XXX: Put this into a generic function!!! See also EditTools.pm # FIXME: Fix the automatic update on undo here! if (my $changeset = $map->change_end) { splice @{ $map->{undo_stack} ||= [] }, $map->{undo_stack_pos}++, 1e6, $changeset; } }); $::MAINWIN->update_stack_view ($map, $x, $y); } package GCE::EditAction::Perl; use GCE::Util; use Gtk2; use strict; our @ISA = qw/GCE::EditAction/; sub name { 'perl' } sub special_arrow { 'GDK_HEART' } sub init { my ($self) = @_; my $vb = new Gtk2::VBox; $vb->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0); $sw->add ($self->{txt} = Gtk2::TextView->new); $self->tool_widget ($vb); } sub want_cursor { 0 } sub begin { my ($self, $map, $x, $y) = @_; $self->SUPER::begin ($map, $x, $y); $self->edit ($map, $x, $y); } sub edit { my ($self, $map, $x, $y) = @_; my $pick = $::MAINWIN->get_pick; my $as = $map->get ($x, $y); $as = $self->eval ($map, $pick, $as, $x, $y); $map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch)); } sub eval { my ($self, $map, $pick, $stack, $x, $y) = @_; my $buf = $self->{txt}->get_buffer; my $code = $buf->get_text ($buf->get_start_iter, $buf->get_end_iter, 0); eval $code; return $stack; } package GCE::EditAction::Place; use Storable qw/dclone/; use GCE::Util; use Gtk2; use strict; our @ISA = qw/GCE::EditAction::RadioModed/; sub name { 'place' } sub init { my ($self) = @_; my $vb = new Gtk2::VBox; $self->add_mode_button ($vb, "auto", "auto"); $self->add_mode_button ($vb, "top", "top"); $self->add_mode_button ($vb, "above floor", "above"); $self->add_mode_button ($vb, "below floor", "below"); $self->add_mode_button ($vb, "bottom", "bottom"); $vb->pack_start ($self->{place_clean} = Gtk2::CheckButton->new ('place clean'), 0, 1, 0); $self->tool_widget ($vb); } sub want_cursor { 0 } sub begin { my ($self, $map, $x, $y) = @_; $self->SUPER::begin ($map, $x, $y); $self->edit ($map, $x, $y); } sub edit { my ($self, $map, $x, $y) = @_; my $pick = $::MAINWIN->get_pick; my $as = $map->get ($x, $y); if ($self->{place_clean}->get_active) { $pick = { _name => $pick->{_name} }; } $self->stack_action ($as, dclone ($pick)); $map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch)); } sub end { my ($self, $map, $x, $y, $mape) = @_; $::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y); $self->SUPER::end ($map, $x, $y, $mape); } sub stack_action { my ($self, $stack, $arch) = @_; my $m = $self->get_mode; if ($m eq 'top') { if (@$stack == 0 or $stack->[-1]->{_name} ne $arch->{_name}) { push @$stack, $arch; } } elsif ($m eq 'bottom') { if (@$stack == 0 or $stack->[0]->{_name} ne $arch->{_name}) { unshift @$stack, $arch; } } elsif ($m eq 'above') { my $fidx = stack_find_floor ($stack, 'from_top'); if (@$stack == 0 or not ($stack->[$fidx + 1]) or $stack->[$fidx + 1]->{_name} ne $arch->{_name}) { splice (@$stack, $fidx + 1, 0, $arch); } } elsif ($m eq 'below') { my $fidx = stack_find_floor ($stack, 'from_bottom'); if (@$stack == 0 or $fidx == 0 or not ($stack->[$fidx - 1]) or $stack->[$fidx - 1]->{_name} ne $arch->{_name}) { splice (@$stack, $fidx, 0, $arch); } } elsif ($m eq 'auto') { my $fidx = stack_find_floor ($stack, 'from_top'); my $widx = stack_find_wall ($stack); if (arch_is_floor ($arch)) { # we want to place a floor tile if (arch_is_floor ($stack->[$fidx])) { # replace $stack->[$fidx] = $arch; } else { # insert on bottom unshift @$stack, $arch; } } elsif (arch_is_wall ($arch)) { # we want to place a wall if (arch_is_wall ($stack->[$widx])) { # replace $stack->[$widx] = $arch; } else { # insert above floor splice (@$stack, $fidx + 1, 0, $arch); } } else { if (arch_is_wall ($stack->[$widx])) { # if we have a wall above the floor, replace it with the to place item $stack->[$widx] = $arch; return; } if (@$stack == 0 or not ($stack->[-1]) or $stack->[-1]->{_name} ne $arch->{_name}) { push @$stack, $arch; } } } } package GCE::EditAction::Select; use GCE::Util; use Gtk2; use Crossfire; use Storable qw/dclone/; use strict; our @ISA = qw/GCE::EditAction::RadioModed/; sub name { 'select' } sub special_arrow { 'GDK_CIRCLE' } sub init { my ($self) = @_; my $vb = new Gtk2::VBox; $vb->pack_start (my $bt = Gtk2::Button->new ("copy"), 0, 1, 0); $bt->signal_connect (clicked => sub { $self->copy }); $vb->pack_start ($self->{paste_top} = Gtk2::CheckButton->new ('paste on top'), 0, 1, 0); $vb->pack_start (my $bt = Gtk2::Button->new ("paste"), 0, 1, 0); $bt->signal_connect (clicked => sub { $self->paste }); $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0); $self->add_mode_button ($vb, "place", "place"); $self->add_mode_button ($vb, "erase", "erase"); $self->add_mode_button ($vb, "perl", "perl"); $vb->pack_start (my $bt = Gtk2::Button->new ("invoke"), 0, 1, 0); $bt->signal_connect (clicked => sub { $self->invoke }); $self->tool_widget ($vb); } sub copy { my ($self) = @_; return unless $self->{selection}->{a}; my ($x1, $y1) = @{$self->{selection}->{a}}; my ($x2, $y2) = @{$self->{selection}->{b}}; if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) } if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) } my $map = $self->{selection}->{map}; $self->{copy_coords} = [$x1, $y1, $x2, $y2]; $self->{copy}; for (my $x = $x1; $x <= $x2; $x++) { for (my $y = $y1; $y <= $y2; $y++) { $self->{copy}->[$x - $x1]->[$y - $y1] = $map->get ($x, $y); } } } sub paste { my ($self, $xp, $yp) = @_; return unless $self->{selection}->{a}; my ($x1, $y1); if (defined $xp) { ($x1, $y1) = ($xp, $yp); } else { ($x1, $y1) = @{$self->{selection}->{a}}; } my $map = $self->{selection}->{map}; my $p_o_top = $self->{paste_top}->get_active * 1; my $w = $self->{copy_coords}->[2] - $self->{copy_coords}->[0]; my $h = $self->{copy_coords}->[3] - $self->{copy_coords}->[1]; $self->{copy}; $self->SUPER::begin ($map, $x1, $y1); for (my $x = $x1; $x <= ($x1 + $w); $x++) { for (my $y = $y1; $y <= ($y1 + $h); $y++) { my $cstck = $map->get ($x, $y); if ($p_o_top) { push @$cstck, @{dclone ($self->{copy}->[$x - $x1]->[$y - $y1] || [])}; $map->change_stack ($x, $y, $cstck); } else { $map->change_stack ($x, $y, dclone ($self->{copy}->[$x - $x1]->[$y - $y1] || [])); } } } $self->SUPER::end ($map); $map->invalidate_all; } sub invoke { my ($self) = @_; return unless $self->{selection}->{a}; my ($x1, $y1) = @{$self->{selection}->{a}}; my ($x2, $y2) = @{$self->{selection}->{b}}; if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) } if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) } my $map = $self->{selection}->{map}; my $m = $self->get_mode; $self->SUPER::begin ($map, $x1, $y1); for (my $x = $x1; $x <= $x2; $x++) { for (my $y = $y1; $y <= $y2; $y++) { if ($m eq 'place') { $::MAINWIN->{edit_collection}{place}->edit ($map, $x, $y); } elsif ($m eq 'erase') { $::MAINWIN->{edit_collection}{erase}->edit ($map, $x, $y); } elsif ($m eq 'perl') { $::MAINWIN->{edit_collection}{perl}->edit ($map, $x, $y); } } } $self->SUPER::end ($map); } sub want_cursor { 0 } sub begin { my ($self, $map, $x, $y) = @_; delete $self->{selection}; $self->{selection}->{a} = [$x, $y]; $self->edit ($map, $x, $y); } sub end { } sub edit { my ($self, $map, $x, $y) = @_; $self->{selection}->{b} = [$x, $y]; $self->{selection}->{map} = $map; $self->update_overlay ($map); } sub update_overlay { my ($self, $map) = @_; my ($x1, $y1) = @{$self->{selection}->{a}}; my ($x2, $y2) = @{$self->{selection}->{b}}; if ($x1 > $x2) { ($x2, $x1) = ($x1, $x2) } if ($y1 > $y2) { ($y2, $y1) = ($y1, $y2) } my $w = ($x2 - $x1) + 1; my $h = ($y2 - $y1) + 1; $map->overlay (selection => $x1 * TILESIZE, $y1 * TILESIZE, $w * TILESIZE, $h * TILESIZE, sub { my ($self, $x, $y) = @_; $self->{window}->draw_rectangle ( $_ & 1 ? $self->style->black_gc : $self->style->white_gc, 0, $x + $_, $y + $_, ($w * TILESIZE) - 1 - $_ * 2, ($h * TILESIZE) - 1 - $_ * 2 ) for 0..3; } ); } package GCE::EditAction::Erase; use GCE::Util; use Gtk2; use strict; our @ISA = qw/GCE::EditAction::RadioModed/; sub name { 'erase' } sub want_cursor { 0 } sub special_arrow { 'GDK_DIAMOND_CROSS' } sub init { my ($self) = @_; my $vb = new Gtk2::VBox; $self->add_mode_button ($vb, "top", "top"); $self->add_mode_button ($vb, "walls", "walls"); $self->add_mode_button ($vb, "above floor", "above", 'default'); $self->add_mode_button ($vb, "floor", "floor"); $self->add_mode_button ($vb, "below floor", "below"); $self->add_mode_button ($vb, "bottom", "bottom"); $self->add_mode_button ($vb, "pick match", "match"); $self->tool_widget ($vb); $vb->pack_start ($self->{no_wall_check} = Gtk2::CheckButton->new ("exclude walls"), 0, 1, 0); $vb->pack_start ($self->{no_monsters_check} = Gtk2::CheckButton->new ("exclude monsters"), 0, 1, 0); } sub check_excluded { my ($self, $arch) = @_; my $a1 = $self->{no_monsters_check}->get_active; my $a2 = $self->{no_wall_check}->get_active; my $r = ($self->{no_wall_check}->get_active && arch_is_wall ($arch)) || ($self->{no_monsters_check}->get_active && arch_is_monster ($arch)); return $r; } sub begin { my ($self, $map, $x, $y) = @_; $self->SUPER::begin ($map, $x, $y); $self->edit ($map, $x, $y); } sub edit { my ($self, $map, $x, $y) = @_; my $as = $map->get ($x, $y); $self->stack_action ($as); $map->change_stack ($x, $y, $as); } sub stack_action { my ($self, $stack) = @_; my $m = $self->get_mode; if ($m eq 'top') { pop @$stack; } elsif ($m eq 'bottom') { shift @$stack; } elsif ($m eq 'above') { my $fidx = stack_find_floor ($stack, 'from_top'); if (arch_is_floor ($stack->[$fidx]) and $stack->[$fidx + 1]) { splice (@$stack, $fidx + 1, 1) unless $self->check_excluded ($stack->[$fidx + 1]) } elsif (not arch_is_floor ($stack->[$fidx])) { splice (@$stack, $fidx, 1) unless $self->check_excluded ($stack->[$fidx]) } } elsif ($m eq 'below') { my $fidx = stack_find_floor ($stack, 'from_bottom'); if ($fidx > 0 and not arch_is_floor ($stack->[$fidx - 1])) { splice (@$stack, $fidx - 1, 1) unless $self->check_excluded ($stack->[$fidx - 1]) } elsif (not arch_is_floor ($stack->[$fidx])) { # no floor found splice (@$stack, $fidx, 1) unless $self->check_excluded ($stack->[$fidx]) } } elsif ($m eq 'walls') { my $widx = stack_find_wall ($stack, 'from_top'); while (arch_is_wall ($stack->[$widx])) { splice (@$stack, $widx, 1); $widx = stack_find_wall ($stack, 'from_top') } } elsif ($m eq 'floor') { my $fidx = stack_find_floor ($stack, 'from_top'); while (arch_is_floor ($stack->[$fidx])) { splice (@$stack, $fidx, 1); $fidx = stack_find_floor ($stack, 'from_top') } } elsif ($m eq 'match') { my $pick_name = $::MAINWIN->get_pick ()->{_name}; my $idx = stack_find ($stack, 'from_top', sub { $_[0]->{_name} eq $pick_name }); while ($stack->[$idx] and $stack->[$idx]->{_name} eq $pick_name) { splice (@$stack, $idx, 1); $idx = stack_find ($stack, 'from_top', sub { $_[0]->{_name} eq $pick_name }); } } } package GCE::EditAction::Connect; use Storable qw/dclone/; use GCE::Util; use Gtk2; use File::Spec::Functions; use strict; our @ISA = qw/GCE::EditAction::RadioModed/; sub name { 'connect' } sub init { my ($self) = @_; my $vb = new Gtk2::VBox; $self->add_mode_button ($vb, "exit", "exit"); # $self->add_mode_button ($vb, "triggers", "trig"); $vb->pack_start ($self->{sel_lbl} = Gtk2::Label->new, 0, 0, 0); $self->tool_widget ($vb); } sub want_cursor { 0 } sub begin { my ($self, $map, $x, $y, $mapedit) = @_; $self->edit ($map, $x, $y, $mapedit); } sub edit { my ($self, $map, $x, $y, $mapedit) = @_; my $pick = $::MAINWIN->get_pick; my $as = $map->get ($x, $y); my $exit; for (@$as) { my $a = $Crossfire::ARCH{$_->{_name}}; if ($a->{type} eq '66') { $exit = $_; } } if ($exit) { if ($self->{sel_exit}) { my $ent = catfile ($Crossfire::LIB, 'maps'); $exit->{hp} = $self->{sel_exit}->[3]; $exit->{sp} = $self->{sel_exit}->[4]; $exit->{slaying} = File::Spec->abs2rel ($self->{sel_exit}->[5], $ent); my $exit2 = $self->{sel_exit}->[0]; $exit2->{hp} = $x; $exit2->{sp} = $y; $exit2->{slaying} = File::Spec->abs2rel ($mapedit->{path}, $ent); unless ($exit2->{slaying} =~ m/^\.\./) { $exit2->{slaying} = '/' . $exit2->{slaying}; } unless ($exit->{slaying} =~ m/^\.\./) { $exit->{slaying} = '/' . $exit->{slaying}; } $self->SUPER::begin ($map, $x, $y, $mapedit); $map->change_stack ($x, $y, $as); $self->SUPER::end ($map); $self->SUPER::begin ($self->{sel_exit}->[1], $exit->{hp}, $exit->{sp}); $self->{sel_exit}->[1]->change_stack ($exit->{hp}, $exit->{sp}, $self->{sel_exit}->[2]); $self->SUPER::end ($self->{sel_exit}->[1]); quick_msg ($mapedit, "$exit->{slaying} ($x:$y) $exit->{_name} <=> $exit2->{slaying} ($exit2->{hp}:$exit2->{sp}) $exit2->{_name}", 0); $::MAINWIN->{edit_collection}{pick}->edit ($map, $x, $y); $self->{sel_exit} = undef; $self->{sel_lbl}->set_text (''); } else { $self->{sel_lbl}->set_text ("src: ($x:$y) $exit->{_name}"); $self->{sel_exit} = [$exit, $map, $as, $x, $y, $mapedit->{path}]; } } else { quick_msg ($mapedit, "no exit object found"); } # $self->stack_action ($as, dclone ($pick)); #$map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch)); } sub end {} =head1 AUTHOR Marc Lehmann http://home.schmorp.de/ Robin Redeker http://www.ta-sa.org/ =cut 1