ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/AttrEdit.pm
Revision: 1.8
Committed: Sun Mar 12 12:18:55 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.7: +135 -72 lines
Log Message:
Implemented first parts of the new attribute editor.

File Contents

# User Rev Content
1 elmex 1.1 package GCE::AttrEdit;
2    
3     =head1 NAME
4    
5     GCE::AttrEdit - an edit wiget for attributes
6    
7     =cut
8    
9     use Gtk2;
10     use Gtk2::Gdk::Keysyms;
11     use Gtk2::SimpleList;
12    
13 elmex 1.8 use Glib::Object::Subclass Gtk2::ScrolledWindow;
14 elmex 1.3
15     use GCE::AttrList;
16    
17     use Crossfire;
18 elmex 1.1
19     sub INIT_INSTANCE {
20     my ($self) = @_;
21    
22 elmex 1.8 $self->add_with_viewport (my $vb = new Gtk2::VBox);
23 elmex 1.4
24 elmex 1.8 $vb->pack_start (my $lbl = $self->{arch_name_lbl} = Gtk2::Label->new, 0, 1, 0);
25     $vb->pack_start (my $ntbook = $self->{ntbook} = Gtk2::Notebook->new, 1, 1, 0);
26     $ntbook->append_page (new Gtk2::Button ("foo"), "test");
27 elmex 1.3 }
28    
29     sub update_arch {
30     my ($self, $arch, $key, $value) = @_;
31    
32     if ($value ne '') {
33    
34 elmex 1.8 my $al_arch = $Crossfire::ARCH{$arch->{_name}};
35     print "**********************> SET $key = $value\n";
36     if (ref $value) {
37     $arch->{$key} = $value;
38    
39     } else {
40     if (not defined $al_arch->{$key}) {
41     if (not $value) {
42     # try to normalize
43     print ">>>>>>>>>>>>>>>>>>>>>> DEL $key\n";
44     delete $arch->{$key};
45     } else {
46     # try to normalize
47     print ">>>>>>>>>>>>>>>>>>>>>> SET $key = $value\n";
48     $arch->{$key} = $value;
49     }
50     } else {
51     if ($al_arch->{$key} ne $value) {
52     print ">>>>>>>>>>>>>>>>>>>>>>> SET $key = $value\n";
53     $arch->{$key} = $value;
54     } else {
55     # try to normalize
56     print ">>>>>>>>>>>>>>>>>>>>>> DEL $key\n";
57     delete $arch->{$key};
58     }
59     }
60     }
61 elmex 1.3
62     } else {
63    
64     delete $arch->{$key};
65     }
66 elmex 1.1
67 elmex 1.7 $self->{change_cb}->($arch)
68     if defined $self->{change_cb};
69 elmex 1.1 }
70    
71 elmex 1.5 sub set_attr {
72     my ($self, $key, $value) = @_;
73    
74     my $attr = $self->{arch}->{$key};
75    
76     unless (ref $attr) {
77    
78     $self->update_arch ($self->{arch}, $key, $value);
79     }
80     }
81    
82 elmex 1.2 sub set_arch {
83 elmex 1.7 my ($self, $arch, $change_cb) = @_;
84    
85     $self->{change_cb} = $change_cb;
86 elmex 1.3
87 elmex 1.4 $self->{arch} = $arch;
88    
89 elmex 1.8 $self->{arch_name_lbl}->set_text (
90     $arch->{_name} . ($arch->{name} ? " - $arch->{name}" : "")
91     );
92    
93 elmex 1.7 if (not defined $change_cb) {
94 elmex 1.3
95 elmex 1.8 } else {
96    
97     }
98    
99     my $al_arch = $Crossfire::ARCH{$arch->{_name}};
100    
101     if ($al_arch) {
102     $self->hide;
103     $self->{ntbook}->remove ($_)
104     for $self->{ntbook}->get_children;
105    
106     my $ar = Crossfire::arch_attr $al_arch;
107     warn "FO1:" . Data::Dumper::Dumper ($al_arch) . ">\n";
108     warn "FO2:" . Data::Dumper::Dumper ($ar) . ">\n";
109     warn "REAL: " . Data::Dumper::Dumper ($arch) . "\n";
110    
111     $self->add_section_edit_widgets ($self->{ntbook}, 'general', $arch, $ar->{attr});
112    
113     my %sects = map { @$_ } @{$ar->{section}};
114    
115     for my $sec (keys %sects) {
116     my $cnt = scalar keys %{$sects{$sec}};
117     $self->add_section_edit_widgets ($self->{ntbook}, $sec, $arch, $sects{$sec});
118     }
119    
120     # for (qw/lore msg/) {
121     # $self->{ntbook}->append_page (my $v = Gtk2::VBox->new, $_);
122     # }
123    
124     $self->show_all;
125 elmex 1.3
126     } else {
127 elmex 1.8 print "NOARCH FOR: $arch->{_name}\n";
128     }
129     }
130    
131     sub add_section_edit_widgets {
132     my ($self, $ntbook, $name, $arch, $section) = @_;
133    
134 elmex 1.3
135 elmex 1.8 $self->{ntbook}->append_page (my $vb = Gtk2::VBox->new, $name);
136     $vb->pack_start (my $table = new Gtk2::Table (2, $cnt), 0, 1, 0);
137    
138     my $i = 0;
139     for (keys %{$section}) {
140     $table->attach_defaults (my $btn = Gtk2::Button->new_with_label ($section->{$_}->{name} || $_),
141     0, 1, $i, $i + 1
142     );
143     $table->attach_defaults (
144     $self->get_edit_widget ($_, $section->{$_}, $arch),
145     1, 2, $i, $i + 1
146     );
147     $i++;
148 elmex 1.3 }
149 elmex 1.8 }
150 elmex 1.3
151 elmex 1.8 sub get_edit_widget {
152     my ($self, $key, $edspec, $arch) = @_;
153 elmex 1.4
154 elmex 1.8 my $type = $edspec->{type};
155 elmex 1.7 my $al_arch = $Crossfire::ARCH{$arch->{_name}};
156 elmex 1.3
157 elmex 1.8 if ($type eq 'bool') {
158     my $chk = new Gtk2::CheckButton ($edspec->{name} || $key);
159     $chk->set_active ($arch->{$key} || $al_arch->{$key});
160     $chk->signal_connect (clicked => sub {
161     my ($chk) = @_;
162     $self->update_arch ($arch, $key, $chk->get_active * 1);
163     print "SET $attr :" . $chk->get_active . "\n";
164     });
165     return $chk
166    
167     } elsif (grep { $type eq $_ } qw/string int treasurelist float/) {
168     my $entry = new Gtk2::Entry;
169     $entry->set_text ($arch->{$key} || $al_arch->{$key});
170     $entry->signal_connect (activate => sub {
171     my ($entry) = @_;
172     $self->update_arch ($arch, $key, $entry->get_text);
173     });
174     return $entry
175    
176     # } elsif ($type eq 'bitmask') {
177     } elsif ($type eq 'spell' or $type eq 'nz_spell') { # XXX: nz_spell bug in datafiles?
178     my $comb = Gtk2::ComboBox->new_text;
179     my $spells_idx = {};
180     my $spells_cmb_idx = {};
181     my $sp = \%Crossfire::Data::SPELL;
182    
183     $comb->append_text ("<none>");
184    
185     my $idx = 1; # XXX: replace this idx with a more save/correct method?
186     for (sort { $sp->{$a} cmp $sp->{$b} } keys %$sp) {
187     $spells_cmd_idx{$idx} = $_;
188     $spells_idx{$_} = $idx++;
189    
190     $comb->append_text ($sp->{$_});
191     }
192     $comb->set_active ($spells_idx{$arch->{$key} || $al_arch->{$key}});
193    
194     $comb->signal_connect (changed => sub {
195     my ($comb) = @_;
196     $self->update_arch ($arch, $key, $spells_cmd_idx{$comb->get_active});
197     });
198     return $comb
199 elmex 1.3
200 elmex 1.8 } elsif ($type eq 'fixed') {
201     return Gtk2::Label->new ("$edspec->{name} = $edspec->{value}");
202 elmex 1.3
203     } else {
204 elmex 1.8 return Gtk2::Label->new ("$key => $edspec->{name} ($type)");
205 elmex 1.2
206 elmex 1.3 }
207 elmex 1.2 }
208    
209 elmex 1.1
210     =head1 AUTHOR
211    
212     Marc Lehmann <schmorp@schmorp.de>
213     http://home.schmorp.de/
214    
215     Robin Redeker <elmex@ta-sa.org>
216     http://www.ta-sa.org/
217    
218     =cut
219     1;