package GCE::StackView; =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 GCE::AttrEdit; use GCE::Util; use Glib::Object::Subclass Gtk2::VBox; use strict; sub INIT_INSTANCE { my ($self) = @_; $self->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0); $sw->add_with_viewport ($self->{stackbox} = Gtk2::VBox->new); $sw->set_policy ('automatic', 'automatic'); } sub set_stack { my ($self, $mapedit, $x, $y) = @_; for ($self->{stackbox}->get_children) { $self->{stackbox}->remove ($_); } my $stack = $mapedit->get ($x, $y); my $idx = $stack ? (@$stack - 1) : 0; if ($stack) { for (reverse @$stack) { # FIXME: How to change a stack with a virtual arch???? next if $_->{_virtual}; #my $a = $_->{_virtual} || $_; my $a = $_; # this is awful, is this really the best way? my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE; fill_pb_from_arch ($pb, $a); $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); # 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; } }); }); $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, "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); }); }; $idx--; } } $self->show_all; } =head1 AUTHOR Marc Lehmann http://home.schmorp.de/ Robin Redeker http://www.ta-sa.org/ =cut 1;