ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/AttrEdit.pm
Revision: 1.19
Committed: Thu Mar 16 11:59:34 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.18: +17 -16 lines
Log Message:
implemented connect tool for exits and some minor improvements in saving

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.19 use GCE::Util;
14 elmex 1.15 use GCE::InventoryEditor;
15    
16 elmex 1.13 use Glib::Object::Subclass Gtk2::VBox;
17 elmex 1.3
18     use Crossfire;
19 elmex 1.1
20     sub INIT_INSTANCE {
21     my ($self) = @_;
22    
23 elmex 1.13 $self->pack_start (my $lbl = $self->{arch_name_lbl} = Gtk2::Label->new, 0, 1, 0);
24     $self->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     if (ref $value) {
34     $arch->{$key} = $value;
35    
36     } else {
37     if (not defined $al_arch->{$key}) {
38 elmex 1.19 if (not defined $value) {
39 elmex 1.8 # try to normalize
40     delete $arch->{$key};
41     } else {
42     # try to normalize
43     $arch->{$key} = $value;
44     }
45     } else {
46     if ($al_arch->{$key} ne $value) {
47     $arch->{$key} = $value;
48     } else {
49     # try to normalize
50     delete $arch->{$key};
51     }
52     }
53     }
54 elmex 1.3
55     } else {
56    
57     delete $arch->{$key};
58     }
59 elmex 1.1
60 elmex 1.7 $self->{change_cb}->($arch)
61     if defined $self->{change_cb};
62 elmex 1.1 }
63    
64 elmex 1.5 sub set_attr {
65     my ($self, $key, $value) = @_;
66    
67     my $attr = $self->{arch}->{$key};
68    
69     unless (ref $attr) {
70    
71     $self->update_arch ($self->{arch}, $key, $value);
72     }
73     }
74    
75 elmex 1.2 sub set_arch {
76 elmex 1.7 my ($self, $arch, $change_cb) = @_;
77    
78     $self->{change_cb} = $change_cb;
79 elmex 1.3
80 elmex 1.4 $self->{arch} = $arch;
81    
82 elmex 1.8 $self->{arch_name_lbl}->set_text (
83     $arch->{_name} . ($arch->{name} ? " - $arch->{name}" : "")
84     );
85    
86     my $al_arch = $Crossfire::ARCH{$arch->{_name}};
87 elmex 1.18 $self->hide;
88     $self->{ntbook}->remove ($_)
89     for $self->{ntbook}->get_children;
90 elmex 1.8
91 elmex 1.18 my $ar = Crossfire::arch_attr $arch;
92 elmex 1.12 # warn "FO1:" . Data::Dumper::Dumper ($al_arch) . ">\n";
93     # warn "FO2:" . Data::Dumper::Dumper ($ar) . ">\n";
94     # warn "REAL: " . Data::Dumper::Dumper ($arch) . "\n";
95 elmex 1.9
96 elmex 1.18 $self->{ttip} = Gtk2::Tooltips->new;
97 elmex 1.8
98 elmex 1.18 #$self->add_section_edit_widgets ($self->{ntbook}, 'general', $arch, $ar->{attr});
99 elmex 1.8
100 elmex 1.18 for my $sec (@{$ar->{section}}) {
101     my $secname = shift @$sec;
102     $self->add_section_edit_widgets ($self->{ntbook}, $secname, $arch, $sec);#$sects{$sec});
103     }
104 elmex 1.8
105 elmex 1.18 for my $key (qw/lore msg/) {
106     $self->{ntbook}->append_page (my $v = Gtk2::VBox->new, $key);
107     $v->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
108     $sw->set_policy ('automatic', 'automatic');
109     $sw->add (my $tb = $self->{"${key}_txt"} = Gtk2::TextView->new);
110     my $buf = $tb->get_buffer->set_text ($arch->{$key});
111    
112     $v->pack_start (my $b = Gtk2::Button->new_with_label ("save"), 0, 1, 0);
113     $b->signal_connect (clicked => sub {
114     my $buf = $tb->get_buffer;
115     $self->update_arch ($arch, $key,
116     $buf->get_text ($buf->get_start_iter, $buf->get_end_iter, 0)
117     );
118     });
119     }
120 elmex 1.15
121 elmex 1.18 $self->{ntbook}->append_page (my $inv = GCE::InventoryEditor->new, 'inventory');
122     $inv->set_arch ($arch, $change_cb);
123 elmex 1.8
124 elmex 1.18 $self->{ttip}->enable;
125 elmex 1.3
126 elmex 1.18 $self->show_all;
127 elmex 1.8 }
128    
129     sub add_section_edit_widgets {
130     my ($self, $ntbook, $name, $arch, $section) = @_;
131    
132 elmex 1.13 $self->{ntbook}->append_page (my $sw = Gtk2::ScrolledWindow->new, $name);
133     $sw->set_policy ('always', 'always');
134     $sw->add_with_viewport (my $vb = Gtk2::VBox->new);
135 elmex 1.8 $vb->pack_start (my $table = new Gtk2::Table (2, $cnt), 0, 1, 0);
136    
137     my $i = 0;
138 elmex 1.11 for my $sec (@$section) {
139     my $bwid = Gtk2::EventBox->new;
140 elmex 1.12 my $al = Gtk2::Alignment->new (0.0, 0.5, 0, 1);
141 elmex 1.11 my $key = $sec->[0];
142     $sec = $sec->[1];
143 elmex 1.19 $al->add (Gtk2::Label->new (def ($sec->{name}, $key)));
144 elmex 1.12 $bwid->add ($al);
145 elmex 1.11 if ($sec->{desc} !~ m/^\s*$/s) {
146     $self->{ttip}->set_tip ($bwid, $sec->{desc});
147 elmex 1.9 }
148 elmex 1.12 $table->attach ($bwid, 0, 1, $i, $i + 1, ['shrink','fill'], 'fill', 5, 0);
149 elmex 1.11
150     $al = Gtk2::Alignment->new (0.0, 0.5, 1, 0);
151     $al->add ($self->get_edit_widget ($key, $sec, $arch, $bwid));
152     $table->attach ($al, 1, 2, $i, $i + 1, ['expand', 'fill'], 'expand', 0, 0);
153 elmex 1.8 $i++;
154 elmex 1.3 }
155 elmex 1.8 }
156 elmex 1.3
157 elmex 1.11 sub label_set_color_default {
158     my ($self, $lbl, $arch, $key, $val) = @_;
159     my $al_arch = $Crossfire::ARCH{$arch->{_name}};
160 elmex 1.9
161 elmex 1.12 # my $fgcolor = $lbl->get_default_style-> Gtk2::Gdk::Color->parse ("lightblue");
162     # my $fgcolor2 = Gtk2::Gdk::Color->parse ("black");#grey");
163 elmex 1.11
164 elmex 1.12 if ( (defined $al_arch->{$key} and $al_arch->{$key} ne $val)
165 elmex 1.19 or (not (defined $al_arch->{$key}) and defined $val))
166 elmex 1.12 {
167     for (qw/normal active prelight selected insensitive/) {
168     $lbl->modify_bg ($_, $lbl->get_default_style->bg ('active'));
169     $lbl->modify_fg ($_, $lbl->get_default_style->fg ('active'));
170     }
171     } else {
172     for (qw/normal active prelight selected insensitive/) {
173     $lbl->modify_bg ($_, $lbl->get_default_style->bg ($_));
174     }
175     }
176 elmex 1.9 }
177    
178 elmex 1.17 # XXX: Warning: Ugly code ahead:
179 elmex 1.8 sub get_edit_widget {
180 elmex 1.11 my ($self, $key, $edspec, $arch, $lbl) = @_;
181 elmex 1.4
182 elmex 1.8 my $type = $edspec->{type};
183 elmex 1.7 my $al_arch = $Crossfire::ARCH{$arch->{_name}};
184 elmex 1.3
185 elmex 1.8 if ($type eq 'bool') {
186 elmex 1.19 my $boolval = def ($edspec->{value}, [0, 1]);
187 elmex 1.15
188 elmex 1.19 my $chk = new Gtk2::CheckButton (def ($edspec->{name}, $key));
189 elmex 1.15
190 elmex 1.12 $self->{ttip}->set_tip ($chk, $al_arch->{$key} * 1);
191 elmex 1.15
192 elmex 1.19 $chk->set_active (def ($arch->{$key}, $al_arch->{$key}) == $boolval->[1]);
193     $self->label_set_color_default ($lbl, $arch, $key, def ($arch->{$key}, $al_arch->{$key}));
194 elmex 1.8 $chk->signal_connect (clicked => sub {
195     my ($chk) = @_;
196 elmex 1.15 $self->label_set_color_default ($lbl, $arch, $key, $boolval->[$chk->get_active * 1]);
197     $self->update_arch ($arch, $key, $boolval->[$chk->get_active * 1]);
198 elmex 1.8 });
199     return $chk
200    
201     } elsif (grep { $type eq $_ } qw/string int treasurelist float/) {
202     my $entry = new Gtk2::Entry;
203 elmex 1.12 $self->{ttip}->set_tip ($entry, $al_arch->{$key});
204 elmex 1.19 $entry->set_text (def ($arch->{$key}, $al_arch->{$key}));
205     $self->label_set_color_default ($lbl, $arch, $key, def ($arch->{$key}, $al_arch->{$key}));
206 elmex 1.10 $entry->signal_connect (changed => sub {
207 elmex 1.8 my ($entry) = @_;
208 elmex 1.12 $self->label_set_color_default ($lbl, $arch, $key, $entry->get_text);
209 elmex 1.8 $self->update_arch ($arch, $key, $entry->get_text);
210     });
211     return $entry
212    
213     } elsif ($type eq 'spell' or $type eq 'nz_spell') { # XXX: nz_spell bug in datafiles?
214     my $comb = Gtk2::ComboBox->new_text;
215     my $spells_idx = {};
216     my $spells_cmb_idx = {};
217     my $sp = \%Crossfire::Data::SPELL;
218    
219     $comb->append_text ("<none>");
220    
221     my $idx = 1; # XXX: replace this idx with a more save/correct method?
222     for (sort { $sp->{$a} cmp $sp->{$b} } keys %$sp) {
223     $spells_cmd_idx{$idx} = $_;
224     $spells_idx{$_} = $idx++;
225    
226     $comb->append_text ($sp->{$_});
227     }
228 elmex 1.12 #XXX: FIXME: $self->{ttip}->set_tip ($comb, $sp->{$al_arch->{$key}});
229 elmex 1.19 $comb->set_active ($spells_idx{def ($arch->{$key}, $al_arch->{$key})});
230     $self->label_set_color_default ($lbl, $arch, $key, def ($arch->{$key}, $al_arch->{$key}));
231 elmex 1.8
232     $comb->signal_connect (changed => sub {
233     my ($comb) = @_;
234 elmex 1.12 $self->label_set_color_default ($lbl, $arch, $key, $spells_cmd_idx{$comb->get_active});
235 elmex 1.8 $self->update_arch ($arch, $key, $spells_cmd_idx{$comb->get_active});
236     });
237     return $comb
238 elmex 1.3
239 elmex 1.14 } elsif ($type eq 'bitmask') {
240 elmex 1.19 my $lblb = Gtk2::Label->new ("bitmask: " . (def ($arch->{$key}, $al_arch->{$key})) * 1);
241 elmex 1.14 my $btn = Gtk2::Button->new;
242     $self->{ttip}->set_tip ($btn, $al_arch->{$key});
243     $btn->add ($lblb);
244 elmex 1.19 my $chval = def ($arch->{$key}, $al_arch->{$key});
245 elmex 1.15 my $menu = $self->create_bitmask_menu ($edspec->{value}, $lbl, $lblb, $arch, $key, \$chval);
246 elmex 1.14
247 elmex 1.19 $self->label_set_color_default ($lbl, $arch, $key, def ($arch->{$key}, $al_arch->{$key}));
248 elmex 1.14
249     $btn->signal_connect (button_press_event => sub {
250     my ($btn, $ev) = @_;
251     $menu->popup (undef, undef, undef, undef, $ev->button, 0);
252     });
253     return $btn;
254    
255     } elsif ($type eq 'list') {
256 elmex 1.19 my $lblb = Gtk2::Label->new ($edspec->{value}->{def($arch->{$key}, $al_arch->{$key}) * 1});
257 elmex 1.14 my $btn = Gtk2::Button->new;
258     $self->{ttip}->set_tip ($btn, $edspec->{value}->{$al_arch->{$key}});
259     $btn->add ($lblb);
260     my $menu = $self->create_list_menu ($edspec->{value}, $lbl, $lblb, $arch, $key);
261    
262 elmex 1.19 $self->label_set_color_default ($lbl, $arch, $key, def ($arch->{$key}, $al_arch->{$key}));
263 elmex 1.14
264     $btn->signal_connect (button_press_event => sub {
265     my ($btn, $ev) = @_;
266     $menu->popup (undef, undef, undef, undef, $ev->button, 0);
267     });
268     return $btn;
269    
270 elmex 1.8 } elsif ($type eq 'fixed') {
271     return Gtk2::Label->new ("$edspec->{name} = $edspec->{value}");
272 elmex 1.3
273 elmex 1.9 } elsif ($type eq 'text') {
274     my $b = $arch->{$key};
275     $b =~ s/\n\r?//gs;
276     if (length $b > 20) {
277     $b = (substr $b, 0, 20) . "...";
278     }
279     return Gtk2::Label->new ($b);
280    
281    
282 elmex 1.3 } else {
283 elmex 1.8 return Gtk2::Label->new ("$key => $edspec->{name} ($type)");
284 elmex 1.2
285 elmex 1.3 }
286 elmex 1.2 }
287    
288 elmex 1.14 sub bitmask_to_list {
289     my ($self, $bitlist, $bits) = @_;
290    
291     my @l;
292     for (%$bitlist) {
293     if ($bits & (1 << $_)) {
294     push @l, $bitlist->{$_};
295     }
296     }
297     return @l;
298     }
299    
300     sub create_list_menu {
301     my ($self, $list, $clbl, $lbl, $arch, $key) = @_;
302    
303     my $menu = Gtk2::Menu->new;
304    
305     for my $item (sort keys %$list) {
306     my $lbltxt = $list->{$item};
307     my $menuitem = Gtk2::MenuItem->new_with_label ($lbltxt);
308     $menuitem->signal_connect (activate => sub {
309     my ($menuitem) = @_;
310     $lbl->set_text ($list->{$item});
311     $self->label_set_color_default ($clbl, $arch, $key, $item);
312     $self->update_arch ($arch, $key, $item);
313     });
314     $menu->append ($menuitem);
315     $menuitem->show;
316     }
317    
318     return $menu;
319     }
320    
321     sub create_bitmask_menu {
322 elmex 1.15 my ($self, $bits, $clbl, $lbl, $arch, $key, $rval) = @_;
323 elmex 1.14
324     my $menu = Gtk2::Menu->new;
325    
326     for my $bit (keys %$bits) {
327     my $lbltxt = $bits->{$bit};
328     my $menuitem = Gtk2::CheckMenuItem->new_with_label ($lbltxt);
329 elmex 1.15 if ($$rval & (1 << $bit)) {
330 elmex 1.14 $menuitem->set_active (1);#$arch->{$key} & (1 << $bit));
331     }
332     $menuitem->signal_connect (toggled => sub {
333     my ($menuitem) = @_;
334     my $newval = $arch->{$key};
335 elmex 1.15 $$rval &= ~(1 << $bit);
336     $$rval |= (1 << $bit) if $menuitem->get_active;
337     $lbl->set_text ("bitmask: " . ($$rval * 1));
338     $self->label_set_color_default ($clbl, $arch, $key, $$rval);
339     $self->update_arch ($arch, $key, $$rval);
340 elmex 1.14 });
341     $menu->append ($menuitem);
342     $menuitem->show;
343     }
344    
345     return $menu;
346     }
347    
348 elmex 1.1
349     =head1 AUTHOR
350    
351     Marc Lehmann <schmorp@schmorp.de>
352     http://home.schmorp.de/
353    
354     Robin Redeker <elmex@ta-sa.org>
355     http://www.ta-sa.org/
356    
357     =cut
358     1;