ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/InventoryEditor.pm
Revision: 1.4
Committed: Thu Mar 16 01:13:57 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.3: +2 -1 lines
Log Message:
removed face checking and implemented pick editing

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
20 use strict;
21
22 sub INIT_INSTANCE {
23 my ($self) = @_;
24
25 $self->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
26 $sw->add_with_viewport (my $vb = Gtk2::VBox->new);
27 $vb->pack_start ($self->{invarch} = Gtk2::VBox->new, 0, 1, 0);
28 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
29 $vb->pack_start ($self->{stackbox} = Gtk2::VBox->new, 1, 1, 0);
30
31 $sw->set_policy ('automatic', 'automatic');
32 }
33
34 sub set_arch {
35 my ($self, $arch, $cb) = @_;
36
37 $self->{invarch}->remove ($_) for $self->{invarch}->get_children;
38 my $pba = new_arch_pb;
39 fill_pb_from_arch ($pba, $arch);
40 my $hb = Gtk2::HBox->new;
41 $hb->pack_start ((new_from_pixbuf Gtk2::Image $pba), 0, 0, 0);
42 $hb->pack_start (my $elemhdl = Gtk2::Button->new ($arch->{_name}), 0, 1, 0);
43 $elemhdl->drag_dest_set (all => ['move'],
44 { target => 'STRING', flags => [], info => 'TARGET_STRING' }
45 );
46 # XXX: I'm unsure here, do i have to issue a get request?
47 # And what if i get the data twice? Wait for transaction end?
48 $elemhdl->signal_connect (drag_data_received => sub {
49 my ($widget, $context, $wx, $wy, $data, $info, $time) = @_;
50
51 if (($data->length >= 0) && ($data->format == 8)) {
52
53 $context->finish (1, 0, $time);
54
55 if ($data->data =~ m/pick/) {
56 my $swapidx = int $1;
57
58 # FIXME: do a deep clone
59 push @{$arch->{inventory}},
60 dclone $::MAINWIN->get_pick->{_name};
61
62 $cb->() if $cb;
63 $self->set_arch ($arch, $cb);
64 }
65
66 return;
67 }
68 $context->finish (0, 0, $time);
69 });
70
71 $self->{invarch}->add ($hb);
72
73 $self->{stackbox}->remove ($_) for $self->{stackbox}->get_children;
74
75 my $inv = $arch->{inventory};
76 my $idx = 0;
77
78 for my $ia (@$inv) {
79 my $pb = new_arch_pb;
80
81 my $a = $Crossfire::ARCH{$ia->{_name}};
82 fill_pb_from_arch ($pb, $a);
83
84 $self->{stackbox}->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
85 $hb->pack_start (my $delbtn = Gtk2::Button->new_with_label ('del'), 0, 0, 0);
86 do {
87 my $ownidx = $idx;
88
89 $delbtn->signal_connect (clicked => sub {
90
91 #my $oldstack = [ @$stack ];
92 splice @$inv, $ownidx, 1;
93
94 $cb->() if $cb;
95 $self->set_arch ($arch, $cb);
96 });
97 };
98
99 $hb->pack_start (my $elemhdl = new Gtk2::Button, 0, 0, 0);
100 $elemhdl->add (my $hb2 = Gtk2::HBox->new);
101 $elemhdl->signal_connect (clicked => sub {
102 $::MAINWIN->update_attr_editor ($a, sub {
103 # $mapedit->change_begin (ref $self);
104 # $mapedit->change_stack ($x, $y, $stack);
105 # # XXX: Put this into a generic function!!! See also EditTools.pm
106 # # FIXME: Fix the automatic update on undo here!
107 # if (my $changeset = $mapedit->change_end) {
108 # splice @{ $mapedit->{undo_stack} ||= [] },
109 # $mapedit->{undo_stack_pos}++, 1e6,
110 # $changeset;
111 # }
112 });
113 });
114
115 $hb2->pack_start (my $img = (new_from_pixbuf Gtk2::Image $pb), 0, 0, 0);
116 $img->set_alignment (0, 0.5);
117
118 $hb2->pack_start (my $lbl = Gtk2::Label->new ($a->{_name}), 0, 0, 0);
119 $lbl->set_alignment (0, 0.5);
120
121 $elemhdl->drag_source_set (['button1_mask'], ['move'],
122 { target => 'STRING', flags => [], info => 'TARGET_STRING' }
123 );
124 $elemhdl->drag_dest_set (all => ['move'],
125 { target => 'STRING', flags => [], info => 'TARGET_STRING' }
126 );
127
128 do {
129 my $ownidx = $idx;
130
131 $elemhdl->signal_connect (drag_data_get => sub {
132 my ($widget, $context, $data, $info, $time) = @_;
133
134 $data->set ($data->target, 8, "inventory:$ownidx");
135 });
136
137 # XXX: I'm unsure here, do i have to issue a get request?
138 # And what if i get the data twice? Wait for transaction end?
139 $elemhdl->signal_connect (drag_data_received => sub {
140 my ($widget, $context, $wx, $wy, $data, $info, $time) = @_;
141
142 if (($data->length >= 0) && ($data->format == 8)) {
143
144 $context->finish (1, 0, $time);
145
146 if ($data->data =~ m/inventory:(\d+)/) {
147 my $swapidx = int $1;
148
149 ($inv->[$swapidx], $inv->[$ownidx])
150 = ($inv->[$ownidx], $inv->[$swapidx]);
151
152 $cb->() if $cb;
153 $self->set_arch ($arch, $cb);
154 }
155
156 return;
157 }
158 $context->finish (0, 0, $time);
159 });
160 };
161 $idx++;
162 }
163 $self->show_all;
164
165 }
166
167 =head1 AUTHOR
168
169 Marc Lehmann <schmorp@schmorp.de>
170 http://home.schmorp.de/
171
172 Robin Redeker <elmex@ta-sa.org>
173 http://www.ta-sa.org/
174
175 =cut
176 1;
177