package GCE::InventoryEditor; =head1 NAME GCE::StackView - the stack window class for gce =cut use Gtk2; use Gtk2::Gdk::Keysyms; use Gtk2::SimpleMenu; use Crossfire; use Crossfire::MapWidget; use Storable qw/dclone/; use Glib::Object::Subclass Gtk2::VBox; use GCE::Util; use strict; sub INIT_INSTANCE { my ($self) = @_; $self->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0); $sw->add_with_viewport (my $vb = Gtk2::VBox->new); $vb->pack_start ($self->{invarch} = Gtk2::VBox->new, 0, 1, 0); $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0); $vb->pack_start ($self->{stackbox} = Gtk2::VBox->new, 1, 1, 0); $sw->set_policy ('automatic', 'automatic'); } sub set_arch { my ($self, $arch, $cb) = @_; $self->{invarch}->remove ($_) for $self->{invarch}->get_children; my $pba = new_arch_pb; fill_pb_from_arch ($pba, $arch); my $hb = Gtk2::HBox->new; $hb->pack_start ((new_from_pixbuf Gtk2::Image $pba), 0, 0, 0); $hb->pack_start (my $elemhdl = Gtk2::Button->new ($arch->{_name}), 0, 1, 0); $elemhdl->drag_dest_set (all => ['move'], { target => 'STRING', flags => [], info => 'TARGET_STRING' } ); # 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/pick/) { my $swapidx = int $1; # FIXME: do a deep clone push @{$arch->{inventory}}, dclone $::MAINWIN->get_pick; $cb->() if $cb; $self->set_arch ($arch, $cb); } return; } $context->finish (0, 0, $time); }); $self->{invarch}->add ($hb); $self->{stackbox}->remove ($_) for $self->{stackbox}->get_children; my $inv = $arch->{inventory}; my $idx = 0; for my $ia (@$inv) { my $pb = new_arch_pb; my $a = $Crossfire::ARCH{$ia->{_name}}; fill_pb_from_arch ($pb, $ia); $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 @$inv, $ownidx, 1; $cb->() if $cb; $self->set_arch ($arch, $cb); }); }; $hb->pack_start (my $elemhdl = new Gtk2::Button, 0, 0, 0); $elemhdl->add (my $hb2 = Gtk2::HBox->new); $elemhdl->signal_connect (clicked => sub { $::MAINWIN->update_attr_editor ($ia, $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; # } # }); }); $hb2->pack_start (my $img = (new_from_pixbuf Gtk2::Image $pb), 0, 0, 0); $img->set_alignment (0, 0.5); $hb2->pack_start (my $lbl = Gtk2::Label->new ($a->{_name}), 0, 0, 0); $lbl->set_alignment (0, 0.5); $elemhdl->drag_source_set (['button1_mask'], ['move'], { target => 'STRING', flags => [], info => 'TARGET_STRING' } ); $elemhdl->drag_dest_set (all => ['move'], { 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, "inventory:$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/inventory:(\d+)/) { my $swapidx = int $1; ($inv->[$swapidx], $inv->[$ownidx]) = ($inv->[$ownidx], $inv->[$swapidx]); $cb->() if $cb; $self->set_arch ($arch, $cb); } return; } $context->finish (0, 0, $time); }); }; $idx++; } $self->show_all; } =head1 AUTHOR Marc Lehmann http://home.schmorp.de/ Robin Redeker http://www.ta-sa.org/ =cut 1;