ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/AttrEdit.pm
Revision: 1.11
Committed: Sun Mar 12 23:32:58 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.10: +40 -41 lines
Log Message:
implemented file->new and implemented attredit

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 Crossfire;
16 elmex 1.1
17     sub INIT_INSTANCE {
18     my ($self) = @_;
19    
20 elmex 1.8 $self->add_with_viewport (my $vb = new Gtk2::VBox);
21 elmex 1.10 $self->set_policy ('automatic', 'automatic');
22 elmex 1.4
23 elmex 1.8 $vb->pack_start (my $lbl = $self->{arch_name_lbl} = Gtk2::Label->new, 0, 1, 0);
24     $vb->pack_start (my $ntbook = $self->{ntbook} = Gtk2::Notebook->new, 1, 1, 0);
25 elmex 1.3 }
26    
27     sub update_arch {
28     my ($self, $arch, $key, $value) = @_;
29    
30     if ($value ne '') {
31    
32 elmex 1.8 my $al_arch = $Crossfire::ARCH{$arch->{_name}};
33     print "**********************> SET $key = $value\n";
34     if (ref $value) {
35     $arch->{$key} = $value;
36    
37     } else {
38     if (not defined $al_arch->{$key}) {
39     if (not $value) {
40     # try to normalize
41     print ">>>>>>>>>>>>>>>>>>>>>> DEL $key\n";
42     delete $arch->{$key};
43     } else {
44     # try to normalize
45     print ">>>>>>>>>>>>>>>>>>>>>> SET $key = $value\n";
46     $arch->{$key} = $value;
47     }
48     } else {
49     if ($al_arch->{$key} ne $value) {
50     print ">>>>>>>>>>>>>>>>>>>>>>> SET $key = $value\n";
51     $arch->{$key} = $value;
52     } else {
53     # try to normalize
54     print ">>>>>>>>>>>>>>>>>>>>>> DEL $key\n";
55     delete $arch->{$key};
56     }
57     }
58     }
59 elmex 1.3
60     } else {
61    
62     delete $arch->{$key};
63     }
64 elmex 1.1
65 elmex 1.7 $self->{change_cb}->($arch)
66     if defined $self->{change_cb};
67 elmex 1.1 }
68    
69 elmex 1.5 sub set_attr {
70     my ($self, $key, $value) = @_;
71    
72     my $attr = $self->{arch}->{$key};
73    
74     unless (ref $attr) {
75    
76     $self->update_arch ($self->{arch}, $key, $value);
77     }
78     }
79    
80 elmex 1.2 sub set_arch {
81 elmex 1.7 my ($self, $arch, $change_cb) = @_;
82    
83     $self->{change_cb} = $change_cb;
84 elmex 1.3
85 elmex 1.4 $self->{arch} = $arch;
86    
87 elmex 1.8 $self->{arch_name_lbl}->set_text (
88     $arch->{_name} . ($arch->{name} ? " - $arch->{name}" : "")
89     );
90    
91 elmex 1.7 if (not defined $change_cb) {
92 elmex 1.3
93 elmex 1.8 } else {
94    
95     }
96    
97     my $al_arch = $Crossfire::ARCH{$arch->{_name}};
98    
99     if ($al_arch) {
100     $self->hide;
101     $self->{ntbook}->remove ($_)
102     for $self->{ntbook}->get_children;
103    
104     my $ar = Crossfire::arch_attr $al_arch;
105 elmex 1.11 warn "FO1:" . Data::Dumper::Dumper ($al_arch) . ">\n";
106     warn "FO2:" . Data::Dumper::Dumper ($ar) . ">\n";
107     warn "REAL: " . Data::Dumper::Dumper ($arch) . "\n";
108 elmex 1.9
109     $self->{ttip} = Gtk2::Tooltips->new;
110 elmex 1.8
111 elmex 1.11 #$self->add_section_edit_widgets ($self->{ntbook}, 'general', $arch, $ar->{attr});
112 elmex 1.8
113 elmex 1.11 for my $sec (@{$ar->{section}}) {
114     # my $cnt = scalar keys %{$sects{$sec}};
115     my $secname = shift @$sec;
116     $self->add_section_edit_widgets ($self->{ntbook}, $secname, $arch, $sec);#$sects{$sec});
117 elmex 1.8 }
118    
119 elmex 1.9 for my $key (qw/lore msg/) {
120     $self->{ntbook}->append_page (my $v = Gtk2::VBox->new, $key);
121     $v->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
122     $sw->set_policy ('automatic', 'automatic');
123     $sw->add (my $tb = $self->{"${key}_txt"} = Gtk2::TextView->new);
124     my $buf = $tb->get_buffer->set_text ($arch->{$key});
125    
126     $v->pack_start (my $b = Gtk2::Button->new_with_label ("save"), 0, 1, 0);
127     $b->signal_connect (clicked => sub {
128     my $buf = $tb->get_buffer;
129     $self->update_arch ($arch, $key,
130     $buf->get_text ($buf->get_start_iter, $buf->get_end_iter, 0)
131     );
132     });
133     }
134    
135     $self->{ttip}->enable;
136 elmex 1.8
137     $self->show_all;
138 elmex 1.3
139     } else {
140 elmex 1.8 print "NOARCH FOR: $arch->{_name}\n";
141     }
142     }
143    
144     sub add_section_edit_widgets {
145     my ($self, $ntbook, $name, $arch, $section) = @_;
146    
147     $self->{ntbook}->append_page (my $vb = Gtk2::VBox->new, $name);
148     $vb->pack_start (my $table = new Gtk2::Table (2, $cnt), 0, 1, 0);
149    
150     my $i = 0;
151 elmex 1.11 for my $sec (@$section) {
152     my $bwid = Gtk2::EventBox->new;
153     my $key = $sec->[0];
154     $sec = $sec->[1];
155     $bwid->add (Gtk2::Label->new ($sec->{name} || $key));
156     if ($sec->{desc} !~ m/^\s*$/s) {
157     $self->{ttip}->set_tip ($bwid, $sec->{desc});
158 elmex 1.9 }
159 elmex 1.11 my $al = Gtk2::Alignment->new (0.0, 0.5, 0, 0);
160     $al->add ($bwid);
161     $table->attach ($al, 0, 1, $i, $i + 1, ['shrink','fill'], 'fill', 5, 0);
162    
163     $al = Gtk2::Alignment->new (0.0, 0.5, 1, 0);
164     $al->add ($self->get_edit_widget ($key, $sec, $arch, $bwid));
165     $table->attach ($al, 1, 2, $i, $i + 1, ['expand', 'fill'], 'expand', 0, 0);
166 elmex 1.8 $i++;
167 elmex 1.3 }
168 elmex 1.8 }
169 elmex 1.3
170 elmex 1.11 sub label_set_color_default {
171     my ($self, $lbl, $arch, $key, $val) = @_;
172     my $al_arch = $Crossfire::ARCH{$arch->{_name}};
173 elmex 1.9
174 elmex 1.11 my $fgcolor = Gtk2::Gdk::Color->parse ("lightblue");
175     my $fgcolor2 = Gtk2::Gdk::Color->parse ("black");#grey");
176    
177     if ($al_arch->{$key} eq $arch->{$key}) {
178     $lbl->modify_base (qw/normal/, $fgcolor);
179     } else {
180     $lbl->modify_base (qw/normal/, $fgcolor2);
181     }
182 elmex 1.9 }
183    
184 elmex 1.8 sub get_edit_widget {
185 elmex 1.11 my ($self, $key, $edspec, $arch, $lbl) = @_;
186 elmex 1.4
187 elmex 1.8 my $type = $edspec->{type};
188 elmex 1.7 my $al_arch = $Crossfire::ARCH{$arch->{_name}};
189 elmex 1.3
190 elmex 1.8 if ($type eq 'bool') {
191     my $chk = new Gtk2::CheckButton ($edspec->{name} || $key);
192     $chk->set_active ($arch->{$key} || $al_arch->{$key});
193 elmex 1.11 $self->label_set_color_default ($lbl, $arch, $key, $arch->{$key} || $al_arch->{$key});
194 elmex 1.8 $chk->signal_connect (clicked => sub {
195     my ($chk) = @_;
196     $self->update_arch ($arch, $key, $chk->get_active * 1);
197 elmex 1.11 $self->label_set_color_default ($lbl, $arch, $key, $arch->{$key});
198 elmex 1.8 print "SET $attr :" . $chk->get_active . "\n";
199     });
200     return $chk
201    
202     } elsif (grep { $type eq $_ } qw/string int treasurelist float/) {
203     my $entry = new Gtk2::Entry;
204     $entry->set_text ($arch->{$key} || $al_arch->{$key});
205 elmex 1.11 $self->label_set_color_default ($lbl, $arch, $key, $arch->{$key} || $al_arch->{$key});
206 elmex 1.10 $entry->signal_connect (changed => sub {
207 elmex 1.8 my ($entry) = @_;
208     $self->update_arch ($arch, $key, $entry->get_text);
209 elmex 1.11 $self->label_set_color_default ($lbl, $arch, $key, $arch->{$key});
210 elmex 1.8 });
211     return $entry
212    
213     # } elsif ($type eq 'bitmask') {
214     } elsif ($type eq 'spell' or $type eq 'nz_spell') { # XXX: nz_spell bug in datafiles?
215     my $comb = Gtk2::ComboBox->new_text;
216     my $spells_idx = {};
217     my $spells_cmb_idx = {};
218     my $sp = \%Crossfire::Data::SPELL;
219    
220     $comb->append_text ("<none>");
221    
222     my $idx = 1; # XXX: replace this idx with a more save/correct method?
223     for (sort { $sp->{$a} cmp $sp->{$b} } keys %$sp) {
224     $spells_cmd_idx{$idx} = $_;
225     $spells_idx{$_} = $idx++;
226    
227     $comb->append_text ($sp->{$_});
228     }
229     $comb->set_active ($spells_idx{$arch->{$key} || $al_arch->{$key}});
230 elmex 1.11 $self->label_set_color_default ($lbl, $arch, $key, $arch->{$key} || $al_arch->{$key});
231 elmex 1.8
232     $comb->signal_connect (changed => sub {
233     my ($comb) = @_;
234     $self->update_arch ($arch, $key, $spells_cmd_idx{$comb->get_active});
235 elmex 1.11 $self->label_set_color_default ($lbl, $arch, $key, $arch->{$key});
236 elmex 1.8 });
237     return $comb
238 elmex 1.3
239 elmex 1.8 } elsif ($type eq 'fixed') {
240     return Gtk2::Label->new ("$edspec->{name} = $edspec->{value}");
241 elmex 1.3
242 elmex 1.9 } elsif ($type eq 'text') {
243     my $b = $arch->{$key};
244     $b =~ s/\n\r?//gs;
245     if (length $b > 20) {
246     $b = (substr $b, 0, 20) . "...";
247     }
248     return Gtk2::Label->new ($b);
249    
250    
251 elmex 1.3 } else {
252 elmex 1.8 return Gtk2::Label->new ("$key => $edspec->{name} ($type)");
253 elmex 1.2
254 elmex 1.3 }
255 elmex 1.2 }
256    
257 elmex 1.1
258     =head1 AUTHOR
259    
260     Marc Lehmann <schmorp@schmorp.de>
261     http://home.schmorp.de/
262    
263     Robin Redeker <elmex@ta-sa.org>
264     http://www.ta-sa.org/
265    
266     =cut
267     1;