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