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 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, $btn) = @_; } sub begin { my ($self, $map, $x, $y, $btn) = @_; $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 want_cursor { 0 } sub special_arrow { 'GDK_HAND2' } sub begin { my ($self, $map, $x, $y, $btn) = @_; $self->SUPER::begin ($map, $x, $y, $btn); $self->edit ($map, $x, $y, $btn); } sub edit { my ($self, $map, $x, $y, $btn) = @_; 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 && $btn == 1; $::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::Place; use GCE::Util; use Gtk2; use strict; our @ISA = qw/GCE::EditAction::RadioModed/; 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"); $self->tool_widget ($vb); } sub want_cursor { 0 } sub begin { my ($self, $map, $x, $y, $btn) = @_; $self->SUPER::begin ($map, $x, $y, $btn); $self->edit ($map, $x, $y, $btn); } sub edit { my ($self, $map, $x, $y, $btn) = @_; if ($btn == 3) { # btn 3 does pick my $cstack = $map->get ($x, $y) or return; my $arch = $cstack->[-1]; $arch->{_virtual} and $arch = $arch->{_virtual}; $::MAINWIN->set_pick ($arch) if @$cstack;; } else { my $pick = $::MAINWIN->get_pick; my $as = $map->get ($x, $y); my $arch = { _name => $pick->{_name} }; $self->stack_action ($as, $arch); $map->change_stack ($x, $y, $as); # insert_arch_stack_layer ($as, $arch)); # push @$as, $arch # unless @$as && $as->[-1]->{_name} eq $arch->{_name}; #$map->set ($x, $y, $as); } } 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, we don't want to place anything # on that tile anymore return; } if (@$stack == 0 or not ($stack->[-1]) or $stack->[-1]->{_name} ne $arch->{_name}) { push @$stack, $arch; } } } } package GCE::EditAction::Erase; use GCE::Util; use Gtk2; use strict; our @ISA = qw/GCE::EditAction::RadioModed/; sub want_cursor { 0 } 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, $btn) = @_; $self->SUPER::begin ($map, $x, $y, $btn); $self->edit ($map, $x, $y, $btn); } sub edit { my ($self, $map, $x, $y, $btn) = @_; 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 }); } } } =head1 AUTHOR Marc Lehmann http://home.schmorp.de/ Robin Redeker http://www.ta-sa.org/ =cut 1