ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/AttrEdit.pm
Revision: 1.30
Committed: Tue Apr 4 13:05:51 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.29: +61 -46 lines
Log Message:
Implemented a different inventory editor, added context menu to picker to
add stuff to the inventory (drag&drop doesn't work yet, will be done later).

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