ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/AttrEdit.pm
Revision: 1.25
Committed: Sun Apr 2 09:54:18 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.24: +33 -22 lines
Log Message:
added 'use/desc' page for types and fixed the 'setme' bug with check_inv (and other
archs of course :)

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