ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/AttrEdit.pm
Revision: 1.26
Committed: Sun Apr 2 10:58:52 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.25: +19 -0 lines
Log Message:
changed map navigation to cursor keys and made active tab stay the same on object switch in attreditor

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 elmex 1.26 # get current page (to remember it for later)
123     my $pgnum = $self->{ntbook}->get_current_page;
124     my $curwid = $self->{ntbook}->get_nth_page ($pgnum);
125     my $curpage_text = defined $curwid ? $self->{ntbook}->get_tab_label_text ($curwid) : undef;
126    
127 elmex 1.7 $self->{change_cb} = $change_cb;
128 elmex 1.3
129 elmex 1.4 $self->{arch} = $arch;
130    
131 elmex 1.21 my $ar = Crossfire::arch_attr $arch;
132    
133 elmex 1.8 $self->{arch_name_lbl}->set_text (
134 elmex 1.21 $arch->{_name} . ($arch->{name} ? " - $arch->{name}" : "") . " ($ar->{name})"
135 elmex 1.8 );
136    
137 elmex 1.23 fill_pb_from_arch ($self->{arch_pb}, $arch);
138     $self->{arch_img}->set_from_pixbuf ($self->{arch_pb});
139    
140 elmex 1.8 my $al_arch = $Crossfire::ARCH{$arch->{_name}};
141 elmex 1.18 $self->hide;
142     $self->{ntbook}->remove ($_)
143     for $self->{ntbook}->get_children;
144 elmex 1.8
145 elmex 1.18 $self->{ttip} = Gtk2::Tooltips->new;
146 elmex 1.8
147 elmex 1.18 for my $sec (@{$ar->{section}}) {
148     my $secname = shift @$sec;
149     $self->add_section_edit_widgets ($self->{ntbook}, $secname, $arch, $sec);#$sects{$sec});
150     }
151 elmex 1.8
152 elmex 1.18 for my $key (qw/lore msg/) {
153     $self->{ntbook}->append_page (my $v = Gtk2::VBox->new, $key);
154     $v->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
155     $sw->set_policy ('automatic', 'automatic');
156     $sw->add (my $tb = $self->{"${key}_txt"} = Gtk2::TextView->new);
157     my $buf = $tb->get_buffer;
158 elmex 1.20 $buf->set_text ($arch->{$key});
159     $buf->signal_connect (changed => sub {
160     my ($buf) = @_;
161     $self->update_arch ($arch, $key,
162     $buf->get_text ($buf->get_start_iter, $buf->get_end_iter, 0)
163     );
164     });
165 elmex 1.18 }
166 elmex 1.15
167 elmex 1.25 $self->{ntbook}->append_page (my $v = Gtk2::VBox->new, 'doc');
168     $v->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
169     $sw->set_policy ('automatic', 'automatic');
170     $sw->add (my $tb = $self->{"doc_txt"} = Gtk2::TextView->new);
171     my $buf = $tb->get_buffer;
172     my $desc = pseudohtml2txt $ar->{desc};
173     my $use = pseudohtml2txt $ar->{use};
174     $buf->set_text ("Description:\n\n$desc\n\nUse:\n\n$use");
175     $tb->set_editable (0);
176     $tb->set_wrap_mode ('word');
177    
178 elmex 1.18 $self->{ttip}->enable;
179 elmex 1.3
180 elmex 1.18 $self->show_all;
181 elmex 1.26
182     # reset the current page if found
183     # XXX: it's braindamaged: it has to be done AFTER show all for some reason
184     if (defined $curpage_text) {
185    
186     for (my $i = 0; $i <= $self->{ntbook}->get_n_pages; $i++) {
187     my $w = $self->{ntbook}->get_nth_page ($i);
188    
189     if ($w && $self->{ntbook}->get_tab_label_text ($w) eq $curpage_text) {
190     $self->{ntbook}->set_current_page ($i);
191     last;
192     }
193     }
194     }
195 elmex 1.8 }
196    
197     sub add_section_edit_widgets {
198     my ($self, $ntbook, $name, $arch, $section) = @_;
199    
200 elmex 1.13 $self->{ntbook}->append_page (my $sw = Gtk2::ScrolledWindow->new, $name);
201     $sw->set_policy ('always', 'always');
202     $sw->add_with_viewport (my $vb = Gtk2::VBox->new);
203 elmex 1.8 $vb->pack_start (my $table = new Gtk2::Table (2, $cnt), 0, 1, 0);
204    
205     my $i = 0;
206 elmex 1.11 for my $sec (@$section) {
207     my $bwid = Gtk2::EventBox->new;
208 elmex 1.12 my $al = Gtk2::Alignment->new (0.0, 0.5, 0, 1);
209 elmex 1.11 my $key = $sec->[0];
210     $sec = $sec->[1];
211 elmex 1.19 $al->add (Gtk2::Label->new (def ($sec->{name}, $key)));
212 elmex 1.12 $bwid->add ($al);
213 elmex 1.11 if ($sec->{desc} !~ m/^\s*$/s) {
214     $self->{ttip}->set_tip ($bwid, $sec->{desc});
215 elmex 1.9 }
216 elmex 1.12 $table->attach ($bwid, 0, 1, $i, $i + 1, ['shrink','fill'], 'fill', 5, 0);
217 elmex 1.11
218     $al = Gtk2::Alignment->new (0.0, 0.5, 1, 0);
219     $al->add ($self->get_edit_widget ($key, $sec, $arch, $bwid));
220     $table->attach ($al, 1, 2, $i, $i + 1, ['expand', 'fill'], 'expand', 0, 0);
221 elmex 1.8 $i++;
222 elmex 1.3 }
223 elmex 1.8 }
224 elmex 1.3
225 elmex 1.11 sub label_set_color_default {
226     my ($self, $lbl, $arch, $key, $val) = @_;
227     my $al_arch = $Crossfire::ARCH{$arch->{_name}};
228 elmex 1.9
229 elmex 1.12 if ( (defined $al_arch->{$key} and $al_arch->{$key} ne $val)
230 elmex 1.22 or (not (defined $al_arch->{$key}) and $val))
231 elmex 1.12 {
232     for (qw/normal active prelight selected insensitive/) {
233     $lbl->modify_bg ($_, $lbl->get_default_style->bg ('active'));
234     $lbl->modify_fg ($_, $lbl->get_default_style->fg ('active'));
235     }
236     } else {
237     for (qw/normal active prelight selected insensitive/) {
238     $lbl->modify_bg ($_, $lbl->get_default_style->bg ($_));
239     }
240     }
241 elmex 1.9 }
242    
243 elmex 1.17 # XXX: Warning: Ugly code ahead:
244 elmex 1.8 sub get_edit_widget {
245 elmex 1.11 my ($self, $key, $edspec, $arch, $lbl) = @_;
246 elmex 1.4
247 elmex 1.8 my $type = $edspec->{type};
248 elmex 1.7 my $al_arch = $Crossfire::ARCH{$arch->{_name}};
249 elmex 1.3
250 elmex 1.8 if ($type eq 'bool') {
251 elmex 1.19 my $boolval = def ($edspec->{value}, [0, 1]);
252 elmex 1.15
253 elmex 1.19 my $chk = new Gtk2::CheckButton (def ($edspec->{name}, $key));
254 elmex 1.15
255 elmex 1.12 $self->{ttip}->set_tip ($chk, $al_arch->{$key} * 1);
256 elmex 1.15
257 elmex 1.19 $chk->set_active (def ($arch->{$key}, $al_arch->{$key}) == $boolval->[1]);
258     $self->label_set_color_default ($lbl, $arch, $key, def ($arch->{$key}, $al_arch->{$key}));
259 elmex 1.8 $chk->signal_connect (clicked => sub {
260     my ($chk) = @_;
261 elmex 1.15 $self->label_set_color_default ($lbl, $arch, $key, $boolval->[$chk->get_active * 1]);
262     $self->update_arch ($arch, $key, $boolval->[$chk->get_active * 1]);
263 elmex 1.8 });
264     return $chk
265    
266     } elsif (grep { $type eq $_ } qw/string int treasurelist float/) {
267     my $entry = new Gtk2::Entry;
268 elmex 1.12 $self->{ttip}->set_tip ($entry, $al_arch->{$key});
269 elmex 1.19 $entry->set_text (def ($arch->{$key}, $al_arch->{$key}));
270     $self->label_set_color_default ($lbl, $arch, $key, def ($arch->{$key}, $al_arch->{$key}));
271 elmex 1.10 $entry->signal_connect (changed => sub {
272 elmex 1.8 my ($entry) = @_;
273 elmex 1.12 $self->label_set_color_default ($lbl, $arch, $key, $entry->get_text);
274 elmex 1.8 $self->update_arch ($arch, $key, $entry->get_text);
275     });
276     return $entry
277    
278     } elsif ($type eq 'spell' or $type eq 'nz_spell') { # XXX: nz_spell bug in datafiles?
279     my $comb = Gtk2::ComboBox->new_text;
280     my $spells_idx = {};
281     my $spells_cmb_idx = {};
282     my $sp = \%Crossfire::Data::SPELL;
283    
284     $comb->append_text ("<none>");
285    
286     my $idx = 1; # XXX: replace this idx with a more save/correct method?
287     for (sort { $sp->{$a} cmp $sp->{$b} } keys %$sp) {
288     $spells_cmd_idx{$idx} = $_;
289     $spells_idx{$_} = $idx++;
290    
291     $comb->append_text ($sp->{$_});
292     }
293 elmex 1.12 #XXX: FIXME: $self->{ttip}->set_tip ($comb, $sp->{$al_arch->{$key}});
294 elmex 1.19 $comb->set_active ($spells_idx{def ($arch->{$key}, $al_arch->{$key})});
295     $self->label_set_color_default ($lbl, $arch, $key, def ($arch->{$key}, $al_arch->{$key}));
296 elmex 1.8
297     $comb->signal_connect (changed => sub {
298     my ($comb) = @_;
299 elmex 1.12 $self->label_set_color_default ($lbl, $arch, $key, $spells_cmd_idx{$comb->get_active});
300 elmex 1.8 $self->update_arch ($arch, $key, $spells_cmd_idx{$comb->get_active});
301     });
302     return $comb
303 elmex 1.3
304 elmex 1.14 } elsif ($type eq 'bitmask') {
305 elmex 1.19 my $lblb = Gtk2::Label->new ("bitmask: " . (def ($arch->{$key}, $al_arch->{$key})) * 1);
306 elmex 1.14 my $btn = Gtk2::Button->new;
307     $self->{ttip}->set_tip ($btn, $al_arch->{$key});
308     $btn->add ($lblb);
309 elmex 1.19 my $chval = def ($arch->{$key}, $al_arch->{$key});
310 elmex 1.15 my $menu = $self->create_bitmask_menu ($edspec->{value}, $lbl, $lblb, $arch, $key, \$chval);
311 elmex 1.14
312 elmex 1.19 $self->label_set_color_default ($lbl, $arch, $key, def ($arch->{$key}, $al_arch->{$key}));
313 elmex 1.14
314     $btn->signal_connect (button_press_event => sub {
315     my ($btn, $ev) = @_;
316     $menu->popup (undef, undef, undef, undef, $ev->button, 0);
317     });
318     return $btn;
319    
320     } elsif ($type eq 'list') {
321 elmex 1.19 my $lblb = Gtk2::Label->new ($edspec->{value}->{def($arch->{$key}, $al_arch->{$key}) * 1});
322 elmex 1.14 my $btn = Gtk2::Button->new;
323     $self->{ttip}->set_tip ($btn, $edspec->{value}->{$al_arch->{$key}});
324     $btn->add ($lblb);
325     my $menu = $self->create_list_menu ($edspec->{value}, $lbl, $lblb, $arch, $key);
326    
327 elmex 1.19 $self->label_set_color_default ($lbl, $arch, $key, def ($arch->{$key}, $al_arch->{$key}));
328 elmex 1.14
329     $btn->signal_connect (button_press_event => sub {
330     my ($btn, $ev) = @_;
331     $menu->popup (undef, undef, undef, undef, $ev->button, 0);
332     });
333     return $btn;
334    
335 elmex 1.8 } elsif ($type eq 'fixed') {
336     return Gtk2::Label->new ("$edspec->{name} = $edspec->{value}");
337 elmex 1.3
338 elmex 1.9 } elsif ($type eq 'text') {
339 elmex 1.20 return Gtk2::Label->new ("<see $key tab>");
340 elmex 1.9
341 elmex 1.3 } else {
342 elmex 1.8 return Gtk2::Label->new ("$key => $edspec->{name} ($type)");
343 elmex 1.2
344 elmex 1.3 }
345 elmex 1.2 }
346    
347 elmex 1.14 sub bitmask_to_list {
348     my ($self, $bitlist, $bits) = @_;
349    
350     my @l;
351     for (%$bitlist) {
352     if ($bits & (1 << $_)) {
353     push @l, $bitlist->{$_};
354     }
355     }
356     return @l;
357     }
358    
359     sub create_list_menu {
360     my ($self, $list, $clbl, $lbl, $arch, $key) = @_;
361    
362     my $menu = Gtk2::Menu->new;
363    
364     for my $item (sort keys %$list) {
365     my $lbltxt = $list->{$item};
366     my $menuitem = Gtk2::MenuItem->new_with_label ($lbltxt);
367     $menuitem->signal_connect (activate => sub {
368     my ($menuitem) = @_;
369     $lbl->set_text ($list->{$item});
370     $self->label_set_color_default ($clbl, $arch, $key, $item);
371     $self->update_arch ($arch, $key, $item);
372     });
373     $menu->append ($menuitem);
374     $menuitem->show;
375     }
376    
377     return $menu;
378     }
379    
380     sub create_bitmask_menu {
381 elmex 1.15 my ($self, $bits, $clbl, $lbl, $arch, $key, $rval) = @_;
382 elmex 1.14
383     my $menu = Gtk2::Menu->new;
384    
385 elmex 1.24 for my $bit (sort keys %$bits) {
386 elmex 1.14 my $lbltxt = $bits->{$bit};
387     my $menuitem = Gtk2::CheckMenuItem->new_with_label ($lbltxt);
388 elmex 1.15 if ($$rval & (1 << $bit)) {
389 elmex 1.14 $menuitem->set_active (1);#$arch->{$key} & (1 << $bit));
390     }
391     $menuitem->signal_connect (toggled => sub {
392     my ($menuitem) = @_;
393     my $newval = $arch->{$key};
394 elmex 1.15 $$rval &= ~(1 << $bit);
395     $$rval |= (1 << $bit) if $menuitem->get_active;
396     $lbl->set_text ("bitmask: " . ($$rval * 1));
397     $self->label_set_color_default ($clbl, $arch, $key, $$rval);
398     $self->update_arch ($arch, $key, $$rval);
399 elmex 1.14 });
400     $menu->append ($menuitem);
401     $menuitem->show;
402     }
403    
404     return $menu;
405     }
406    
407 elmex 1.1
408     =head1 AUTHOR
409    
410     Marc Lehmann <schmorp@schmorp.de>
411     http://home.schmorp.de/
412    
413     Robin Redeker <elmex@ta-sa.org>
414     http://www.ta-sa.org/
415    
416     =cut
417     1;