ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/InventoryEditor.pm
Revision: 1.10
Committed: Tue Dec 5 15:21:51 2006 UTC (17 years, 6 months ago) by elmex
Branch: MAIN
CVS Tags: rel-2_2, rel-2_0, rel-2_1
Changes since 1.9: +1 -1 lines
Log Message:
handling undefined archetypes now more gracefully by defaulting
it's type to 'empty_archetype' and opening a error dialog to inform
the user about the inconsistent map.

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.9 use GCE::ArchRef;
21 elmex 1.1
22     use strict;
23    
24     sub INIT_INSTANCE {
25     my ($self) = @_;
26    
27     $self->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
28     $sw->add_with_viewport (my $vb = Gtk2::VBox->new);
29     $vb->pack_start ($self->{invarch} = Gtk2::VBox->new, 0, 1, 0);
30     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
31     $vb->pack_start ($self->{stackbox} = Gtk2::VBox->new, 1, 1, 0);
32    
33     $sw->set_policy ('automatic', 'automatic');
34     }
35    
36 elmex 1.8 sub push_inv_hist {
37     my ($self, $arch) = @_;
38     push @{$self->{inv_hist}}, $arch;
39     }
40    
41     sub clear_inv_hist {
42     my ($self) = @_;
43     delete $self->{inv_hist};
44     }
45    
46 elmex 1.1 sub set_arch {
47 elmex 1.9 my ($self, $ar) = @_;
48    
49     if ((defined $self->{cur_arch}) && ($self->{cur_arch} != $ar)) {
50     $self->{cur_arch}->remove_on_change ('invedit')
51     if defined $self->{cur_arch};
52     $ar->add_on_change (invedit => sub { $self->set_arch ($_[0]) });
53    
54     } elsif (not defined $self->{cur_arch}) {
55     $ar->add_on_change (invedit => sub { $self->set_arch ($_[0]) });
56     }
57    
58     $self->{cur_arch} = $ar;
59 elmex 1.1
60     $self->{invarch}->remove ($_) for $self->{invarch}->get_children;
61 elmex 1.2
62 elmex 1.9 if (($self->{inv_hist} || [])->[-1] != $ar) {
63     # unless (@{$self->{inv_hist} || []}) {
64     push @{$self->{inv_hist}}, $ar;
65 elmex 1.8 }
66    
67     for my $invar (@{$self->{inv_hist}}) {
68     my $pba = new_arch_pb;
69 elmex 1.9 fill_pb_from_arch ($pba, $invar->getarch);
70 elmex 1.8 my $hb = Gtk2::HBox->new;
71     $hb->pack_start ((new_from_pixbuf Gtk2::Image $pba), 0, 0, 0);
72 elmex 1.9 $hb->pack_start (my $elemhdl = Gtk2::Button->new ($invar->get ('_name')), 0, 1, 0);
73 elmex 1.8 $elemhdl->signal_connect (clicked => sub {
74     while (pop @{$self->{inv_hist}} != $invar) { 1 }
75     push @{$self->{inv_hist}}, $invar;
76 elmex 1.9 $::MAINWIN->{attr_edit}->set_arch ($invar);
77 elmex 1.8 });
78     GCE::DragHelper::set_drag_source (
79 elmex 1.9 $elemhdl, arch => sub { { arch => $invar } }
80 elmex 1.8 );
81    
82     GCE::DragHelper::set_drag_sink (
83     $elemhdl, arch => sub {
84     return unless $_[0]->{arch};
85 elmex 1.9 $invar->add_inv (dclone ($_[0]->{arch}->getarch));
86     $self->set_arch ($invar) #XXX: untested yet
87 elmex 1.8 }
88     );
89    
90     $self->{invarch}->add ($hb);
91     }
92 elmex 1.1
93     $self->{stackbox}->remove ($_) for $self->{stackbox}->get_children;
94    
95 elmex 1.9 my $inv = $ar->get_inv_refs;
96 elmex 1.1 my $idx = 0;
97    
98     for my $ia (@$inv) {
99     my $pb = new_arch_pb;
100 elmex 1.7 my $ownidx = $idx;
101 elmex 1.1
102 elmex 1.9 my $a = $ia->archetype;
103     fill_pb_from_arch ($pb, $ia->getarch);
104 elmex 1.1
105     $self->{stackbox}->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
106     $hb->pack_start (my $delbtn = Gtk2::Button->new_with_label ('del'), 0, 0, 0);
107     $delbtn->signal_connect (clicked => sub {
108    
109     #my $oldstack = [ @$stack ];
110 elmex 1.9 $ar->remove_inv ($ownidx); #splice @$inv, $ownidx, 1;
111     $self->set_arch ($ar);
112 elmex 1.1 });
113    
114     $hb->pack_start (my $elemhdl = new Gtk2::Button, 0, 0, 0);
115     $elemhdl->add (my $hb2 = Gtk2::HBox->new);
116     $elemhdl->signal_connect (clicked => sub {
117 elmex 1.9 push @{$self->{inv_hist}}, $ia;
118     $::MAINWIN->{attr_edit}->set_arch ($ia);
119 elmex 1.1 });
120    
121     $hb2->pack_start (my $img = (new_from_pixbuf Gtk2::Image $pb), 0, 0, 0);
122     $img->set_alignment (0, 0.5);
123    
124 elmex 1.10 $hb2->pack_start (my $lbl = Gtk2::Label->new ($ia->get ('_name')), 0, 0, 0);
125 elmex 1.1 $lbl->set_alignment (0, 0.5);
126    
127 elmex 1.7 GCE::DragHelper::set_drag_source (
128     $elemhdl, arch => sub {
129     { arch => $inv->[$ownidx], inv => $inv, inv_idx => $ownidx }
130     }
131     );
132    
133     GCE::DragHelper::set_drag_sink (
134     $elemhdl, arch => sub {
135     my ($darch) = @_;
136    
137     if (defined $darch->{inv_idx} && $darch->{inv} == $inv) {
138     my $swapidx = $darch->{inv_idx};
139 elmex 1.9 $ar->{arch}->swap_inv ($swapidx, $ownidx);
140 elmex 1.7 } else {
141 elmex 1.9 $darch->{arch}->replace_inv ($ownidx, dclone $darch->{arch});
142 elmex 1.7 }
143    
144 elmex 1.9 $self->set_arch ($ar);
145 elmex 1.7 }
146     );
147 elmex 1.1 $idx++;
148     }
149     $self->show_all;
150    
151     }
152    
153     =head1 AUTHOR
154    
155     Marc Lehmann <schmorp@schmorp.de>
156     http://home.schmorp.de/
157    
158     Robin Redeker <elmex@ta-sa.org>
159     http://www.ta-sa.org/
160    
161     =cut
162     1;
163