ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/AttrEdit.pm
Revision: 1.29
Committed: Tue Apr 4 11:29:54 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.28: +21 -15 lines
Log Message:
Removed pick and made the attribut editor 'the current pick'.
Added context menu to map view with an follow item in it.

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