ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/AttrEdit.pm
Revision: 1.12
Committed: Mon Mar 13 00:32:51 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.11: +31 -21 lines
Log Message:
improved attribute editor

File Contents

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