ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/InventoryEditor.pm
Revision: 1.7
Committed: Sun Mar 26 20:42:12 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.6: +37 -79 lines
Log Message:
implemented better drag&drop. moved inventory editor out of the attribute editor.

File Contents

# Content
1 package GCE::InventoryEditor;
2
3 =head1 NAME
4
5 GCE::StackView - the stack window class for gce
6
7 =cut
8
9 use Gtk2;
10 use Gtk2::Gdk::Keysyms;
11 use Gtk2::SimpleMenu;
12
13 use Crossfire;
14 use Crossfire::MapWidget;
15 use Storable qw/dclone/;
16
17 use Glib::Object::Subclass Gtk2::VBox;
18 use GCE::Util;
19 use GCE::DragHelper;
20
21 use strict;
22
23 sub INIT_INSTANCE {
24 my ($self) = @_;
25
26 $self->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
27 $sw->add_with_viewport (my $vb = Gtk2::VBox->new);
28 $vb->pack_start ($self->{invarch} = Gtk2::VBox->new, 0, 1, 0);
29 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
30 $vb->pack_start ($self->{stackbox} = Gtk2::VBox->new, 1, 1, 0);
31
32 $sw->set_policy ('automatic', 'automatic');
33 }
34
35 sub set_arch {
36 my ($self, $arch, $cb) = @_;
37
38 $self->{invarch}->remove ($_) for $self->{invarch}->get_children;
39 my $pba = new_arch_pb;
40 fill_pb_from_arch ($pba, $arch);
41 my $hb = Gtk2::HBox->new;
42 $hb->pack_start ((new_from_pixbuf Gtk2::Image $pba), 0, 0, 0);
43 $hb->pack_start (my $elemhdl = Gtk2::Button->new ($arch->{_name}), 0, 1, 0);
44 $elemhdl->signal_connect (clicked => sub {
45 $::MAINWIN->update_attr_editor ($arch, $cb);
46 });
47 GCE::DragHelper::set_drag_source (
48 $elemhdl, arch => sub { { arch => $arch } }
49 );
50
51 GCE::DragHelper::set_drag_sink (
52 $elemhdl, arch => sub {
53 return unless $_[0]->{arch};
54 push @{$arch->{inventory}}, dclone $_[0]->{arch};
55 $cb->() if $cb;
56 $self->set_arch ($arch, $cb)
57 }
58 );
59
60 $self->{invarch}->add ($hb);
61
62 $self->{stackbox}->remove ($_) for $self->{stackbox}->get_children;
63
64 my $inv = $arch->{inventory};
65 my $idx = 0;
66
67 for my $ia (@$inv) {
68 my $pb = new_arch_pb;
69 my $ownidx = $idx;
70
71 my $a = $Crossfire::ARCH{$ia->{_name}};
72 fill_pb_from_arch ($pb, $ia);
73
74 $self->{stackbox}->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
75 $hb->pack_start (my $delbtn = Gtk2::Button->new_with_label ('del'), 0, 0, 0);
76 $delbtn->signal_connect (clicked => sub {
77
78 #my $oldstack = [ @$stack ];
79 splice @$inv, $ownidx, 1;
80
81 $cb->() if $cb;
82 $self->set_arch ($arch, $cb);
83 });
84
85 $hb->pack_start (my $elemhdl = new Gtk2::Button, 0, 0, 0);
86 $elemhdl->add (my $hb2 = Gtk2::HBox->new);
87 $elemhdl->signal_connect (clicked => sub {
88 $::MAINWIN->update_attr_editor ($ia, $cb);
89 });
90
91 $hb2->pack_start (my $img = (new_from_pixbuf Gtk2::Image $pb), 0, 0, 0);
92 $img->set_alignment (0, 0.5);
93
94 $hb2->pack_start (my $lbl = Gtk2::Label->new ($a->{_name}), 0, 0, 0);
95 $lbl->set_alignment (0, 0.5);
96
97 GCE::DragHelper::set_drag_source (
98 $elemhdl, arch => sub {
99 { arch => $inv->[$ownidx], inv => $inv, inv_idx => $ownidx }
100 }
101 );
102
103 GCE::DragHelper::set_drag_sink (
104 $elemhdl, arch => sub {
105 my ($darch) = @_;
106
107 if (defined $darch->{inv_idx} && $darch->{inv} == $inv) {
108 my $swapidx = $darch->{inv_idx};
109 ($inv->[$swapidx], $inv->[$ownidx])
110 = ($inv->[$ownidx], $inv->[$swapidx]);
111 } else {
112 splice @$inv, $ownidx, 1, (dclone $darch->{arch});
113 }
114
115 $cb->() if $cb;
116 $self->set_arch ($arch, $cb);
117 }
118 );
119 $idx++;
120 }
121 $self->show_all;
122
123 }
124
125 =head1 AUTHOR
126
127 Marc Lehmann <schmorp@schmorp.de>
128 http://home.schmorp.de/
129
130 Robin Redeker <elmex@ta-sa.org>
131 http://www.ta-sa.org/
132
133 =cut
134 1;
135