--- deliantra/gde/GCE/StackView.pm 2006/03/19 19:30:38 1.11 +++ deliantra/gde/GCE/StackView.pm 2007/12/27 22:28:01 1.19 @@ -10,14 +10,16 @@ use Gtk2::Gdk::Keysyms; use Gtk2::SimpleMenu; -use Crossfire; -use Crossfire::MapWidget; +use Deliantra; +use Deliantra::MapWidget; use GCE::AttrEdit; use GCE::Util; use Glib::Object::Subclass Gtk2::VBox; +use Storable qw/dclone/; + use strict; sub INIT_INSTANCE { @@ -28,11 +30,27 @@ $sw->set_policy ('automatic', 'automatic'); } +sub maybe_update_stack_for { + my ($self, $mapedit, $x, $y) = @_; + if (my $d = $self->{stack_dest}) { + if ($d->[0] eq $mapedit && $d->[1] == $x && $d->[2] == $y) { + $self->set_stack + } + } +} + sub set_stack { my ($self, $mapedit, $x, $y) = @_; - for ($self->{stackbox}->get_children) { + if (defined $mapedit) { + $self->{stack_dest} = [$mapedit, $x, $y]; + } else { + $self->set_stack (@{$self->{stack_dest}}) + if defined $self->{stack_dest}; + return; + } + for ($self->{stackbox}->get_children) { $self->{stackbox}->remove ($_); } @@ -42,7 +60,11 @@ if ($stack) { for (reverse @$stack) { # FIXME: How to change a stack with a virtual arch???? - next if $_->{_virtual}; + if ($_->{_virtual}) { + $idx--; + next + } + my $ownidx = $idx; #my $a = $_->{_virtual} || $_; my $a = $_; @@ -54,37 +76,45 @@ $self->{stackbox}->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0); $hb->pack_start (my $delbtn = Gtk2::Button->new_with_label ('del'), 0, 0, 0); - do { - my $ownidx = $idx; - $delbtn->signal_connect (clicked => sub { #my $oldstack = [ @$stack ]; splice @$stack, $ownidx, 1; - # XXX: Insert undo here! - $mapedit->set ($x, $y, $stack); + $mapedit->change_begin ('stackedit'); + $mapedit->change_stack ($x, $y, $stack); + + if (my $changeset = $mapedit->change_end) { + splice @{ $mapedit->{undo_stack} ||= [] }, + $mapedit->{undo_stack_pos}++, 1e6, + $changeset; + } # XXX: force an update ? maybe some more intelligent update later? $self->set_stack ($mapedit, $x, $y); }); - }; $hb->pack_start (my $elemhdl = new Gtk2::Button, 0, 0, 0); $elemhdl->add (my $hb2 = Gtk2::HBox->new); $elemhdl->signal_connect (clicked => sub { - $::MAINWIN->set_pick ($a); - $::MAINWIN->update_attr_editor ($a, sub { - $mapedit->change_begin (ref $self); - $mapedit->change_stack ($x, $y, $stack); - # XXX: Put this into a generic function!!! See also EditTools.pm - # FIXME: Fix the automatic update on undo here! - if (my $changeset = $mapedit->change_end) { - splice @{ $mapedit->{undo_stack} ||= [] }, - $mapedit->{undo_stack_pos}++, 1e6, - $changeset; - } - }); + my $ar = + GCE::ArchRef->new ( + arch => $a, + source => 'map', + cb => sub { + $mapedit->change_begin (ref $self); + $mapedit->change_stack ($x, $y, $stack); + # XXX: Put this into a generic function!!! See also EditTools.pm + # FIXME: Fix the automatic update on undo here! + if (my $changeset = $mapedit->change_end) { + splice @{ $mapedit->{undo_stack} ||= [] }, + $mapedit->{undo_stack_pos}++, 1e6, + $changeset; + } + } + ); + + $::MAINWIN->update_attr_editor ($ar) }); $hb2->pack_start (my $img = (new_from_pixbuf Gtk2::Image $pb), 0, 0, 0); @@ -100,46 +130,35 @@ { target => 'STRING', flags => [], info => 'TARGET_STRING' } ); - do { - my $ownidx = $idx; - - $elemhdl->signal_connect (drag_data_get => sub { - my ($widget, $context, $data, $info, $time) = @_; - - $data->set ($data->target, 8, "stack:$ownidx"); - }); - - # XXX: I'm unsure here, do i have to issue a get request? - # And what if i get the data twice? Wait for transaction end? - $elemhdl->signal_connect (drag_data_received => sub { - my ($widget, $context, $wx, $wy, $data, $info, $time) = @_; - - if (($data->length >= 0) && ($data->format == 8)) { - - $context->finish (1, 0, $time); - - if ($data->data =~ m/stack:(\d+)/) { - my $swapidx = int $1; - - ($stack->[$swapidx], $stack->[$ownidx]) - = ($stack->[$ownidx], $stack->[$swapidx]); - - # XXX: Insert undo here! - $mapedit->set ($x, $y, $stack); - - $self->set_stack ($mapedit, $x, $y); - } - - return; - } - $context->finish (0, 0, $time); - }); - }; + GCE::DragHelper::set_drag_source ( + $elemhdl, arch => sub { + { arch => $stack->[$ownidx], stk => $stack, stk_idx => $ownidx } + } + ); + + GCE::DragHelper::set_drag_sink ( + $elemhdl, arch => sub { + my ($darch) = @_; + + if (defined $darch->{stk_idx} and $darch->{stk} == $stack) { + my $swapidx = $darch->{stk_idx}; + ($stack->[$swapidx], $stack->[$ownidx]) + = ($stack->[$ownidx], $stack->[$swapidx]); + + } else { + splice @$stack, $ownidx, 1, (dclone $darch->{arch}); + } + + # XXX: Insert undo here! + $mapedit->set ($x, $y, $stack); + + $self->set_stack ($mapedit, $x, $y); + } + ); $idx--; } } $self->show_all; - } =head1 AUTHOR