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 { } 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::Pick; our @ISA = qw/GCE::EditAction/; 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 { my ($arch) = @_; print "UPD: $arch\n"; #d# $map->change_stack ($x, $y, $cstack); }); $::MAINWIN->update_stack_view ($map, $x, $y); } package GCE::EditAction::Place; use GCE::Util; use Gtk2; use strict; our @ISA = qw/GCE::EditAction/; sub _add_mode_button { my ($self, $vb, $lbl, $mode) = @_; $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; $b->set_active (1); $self->set_place_mode ($mode); } $b->signal_connect (clicked => sub { $self->set_place_mode ($mode); }); } sub set_place_mode { my ($self, $mode) = @_; $self->{place_mode} = $mode; } 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->{widget} = $vb; } sub tool_widget { my ($self) = @_; return $self->{widget}; } 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->{place_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; our @ISA = qw/GCE::EditAction/; 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) = @_; my $as = $map->get ($x, $y); pop @$as; $map->change_stack ($x, $y, $as); } =head1 AUTHOR Marc Lehmann http://home.schmorp.de/ Robin Redeker http://www.ta-sa.org/ =cut 1