ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/InventoryEditor.pm
Revision: 1.8
Committed: Tue Apr 4 13:05:51 2006 UTC (20 years, 7 months ago) by elmex
Branch: MAIN
Changes since 1.7: +42 -22 lines
Log Message:
Implemented a different inventory editor, added context menu to picker to
add stuff to the inventory (drag&drop doesn't work yet, will be done later).

File Contents

# User Rev Content
1 elmex 1.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 elmex 1.4 use Storable qw/dclone/;
16 elmex 1.1
17     use Glib::Object::Subclass Gtk2::VBox;
18     use GCE::Util;
19 elmex 1.7 use GCE::DragHelper;
20 elmex 1.1
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 elmex 1.8 sub push_inv_hist {
36     my ($self, $arch) = @_;
37     push @{$self->{inv_hist}}, $arch;
38     }
39    
40     sub clear_inv_hist {
41     my ($self) = @_;
42     delete $self->{inv_hist};
43     }
44    
45 elmex 1.1 sub set_arch {
46 elmex 1.2 my ($self, $arch, $cb) = @_;
47 elmex 1.1
48     $self->{invarch}->remove ($_) for $self->{invarch}->get_children;
49 elmex 1.2
50 elmex 1.8 unless (@{$self->{inv_hist} || []}) {
51     push @{$self->{inv_hist}}, [$arch, $cb];
52     }
53    
54     for my $invar (@{$self->{inv_hist}}) {
55     my $pba = new_arch_pb;
56     fill_pb_from_arch ($pba, $invar->[0]);
57     my $hb = Gtk2::HBox->new;
58     $hb->pack_start ((new_from_pixbuf Gtk2::Image $pba), 0, 0, 0);
59     $hb->pack_start (my $elemhdl = Gtk2::Button->new ($invar->[0]->{_name}), 0, 1, 0);
60     $elemhdl->signal_connect (clicked => sub {
61     while (pop @{$self->{inv_hist}} != $invar) { 1 }
62     push @{$self->{inv_hist}}, $invar;
63     $::MAINWIN->{attr_edit}->update ($invar->[0]);
64     });
65     GCE::DragHelper::set_drag_source (
66     $elemhdl, arch => sub { { arch => $invar->[0] } }
67     );
68    
69     GCE::DragHelper::set_drag_sink (
70     $elemhdl, arch => sub {
71     return unless $_[0]->{arch};
72     push @{$invar->[0]->{inventory}}, dclone $_[0]->{arch};
73     $cb->() if $cb;
74     $self->set_arch ($invar, $cb)
75     }
76     );
77    
78     $self->{invarch}->add ($hb);
79     }
80 elmex 1.1
81     $self->{stackbox}->remove ($_) for $self->{stackbox}->get_children;
82    
83     my $inv = $arch->{inventory};
84     my $idx = 0;
85    
86     for my $ia (@$inv) {
87     my $pb = new_arch_pb;
88 elmex 1.7 my $ownidx = $idx;
89 elmex 1.1
90 elmex 1.2 my $a = $Crossfire::ARCH{$ia->{_name}};
91 elmex 1.6 fill_pb_from_arch ($pb, $ia);
92 elmex 1.1
93     $self->{stackbox}->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
94     $hb->pack_start (my $delbtn = Gtk2::Button->new_with_label ('del'), 0, 0, 0);
95     $delbtn->signal_connect (clicked => sub {
96    
97     #my $oldstack = [ @$stack ];
98     splice @$inv, $ownidx, 1;
99    
100 elmex 1.2 $cb->() if $cb;
101     $self->set_arch ($arch, $cb);
102 elmex 1.1 });
103    
104     $hb->pack_start (my $elemhdl = new Gtk2::Button, 0, 0, 0);
105     $elemhdl->add (my $hb2 = Gtk2::HBox->new);
106     $elemhdl->signal_connect (clicked => sub {
107 elmex 1.8 push @{$self->{inv_hist}}, [$ia, $cb];
108     $::MAINWIN->{attr_edit}->update ($ia);
109 elmex 1.1 });
110    
111     $hb2->pack_start (my $img = (new_from_pixbuf Gtk2::Image $pb), 0, 0, 0);
112     $img->set_alignment (0, 0.5);
113    
114     $hb2->pack_start (my $lbl = Gtk2::Label->new ($a->{_name}), 0, 0, 0);
115     $lbl->set_alignment (0, 0.5);
116    
117 elmex 1.7 GCE::DragHelper::set_drag_source (
118     $elemhdl, arch => sub {
119     { arch => $inv->[$ownidx], inv => $inv, inv_idx => $ownidx }
120     }
121     );
122    
123     GCE::DragHelper::set_drag_sink (
124     $elemhdl, arch => sub {
125     my ($darch) = @_;
126    
127     if (defined $darch->{inv_idx} && $darch->{inv} == $inv) {
128     my $swapidx = $darch->{inv_idx};
129     ($inv->[$swapidx], $inv->[$ownidx])
130     = ($inv->[$ownidx], $inv->[$swapidx]);
131     } else {
132     splice @$inv, $ownidx, 1, (dclone $darch->{arch});
133     }
134    
135     $cb->() if $cb;
136     $self->set_arch ($arch, $cb);
137     }
138     );
139 elmex 1.1 $idx++;
140     }
141     $self->show_all;
142    
143     }
144    
145     =head1 AUTHOR
146    
147     Marc Lehmann <schmorp@schmorp.de>
148     http://home.schmorp.de/
149    
150     Robin Redeker <elmex@ta-sa.org>
151     http://www.ta-sa.org/
152    
153     =cut
154     1;
155