ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/AttrEdit.pm
Revision: 1.46.2.2
Committed: Mon Dec 21 15:51:30 2009 UTC (14 years, 5 months ago) by elmex
Branch: cursor
Changes since 1.46.2.1: +81 -106 lines
Log Message:
rewrote main parts in internal reference handling.

File Contents

# Content
1 package GCE::AttrEdit;
2
3 =head1 NAME
4
5 GCE::AttrEdit - an edit wiget for attributes
6
7 =cut
8
9 use Scalar::Util qw/weaken/;
10
11 use Gtk2;
12 use Gtk2::Gdk::Keysyms;
13 use Gtk2::SimpleList;
14
15 use GCE::Util;
16 use GCE::InventoryEditor;
17 use GCE::AttachEditor;
18
19 use Glib::Object::Subclass
20 Gtk2::HPaned;
21
22 use Deliantra;
23 use strict;
24
25 sub save_layout {
26 my ($self) = @_;
27
28 $::CFG->{attr_view_hpane_pos} = $self->get_position;
29 $::CFG->{attr_view_show_use} = $self->{use_btn}->get_active * 1;
30
31 $self->{attach_editor}->save_layout
32 if $self->{attach_editor};
33 }
34
35 sub load_layout {
36 my ($self) = @_;
37
38 $self->set_position ($::CFG->{attr_view_hpane_pos} || 350);
39 $self->{use_btn}->set_active ($::CFG->{attr_view_show_use} * 1);
40
41 $self->{use_al}->remove ($_) for $self->{use_al}->get_children;
42 if ($self->{use_btn}->get_active) {
43 $self->{use_al}->add ($self->{use_lbl});
44 $self->{use_lbl}->show;
45 }
46 }
47
48 sub INIT_INSTANCE {
49 my ($self) = @_;
50
51 my $pb = $self->{arch_pb} = new_arch_pb;
52
53 $self->add (my $topvb = Gtk2::VBox->new);
54 $topvb->pack_start (my $menubar = Gtk2::MenuBar->new, 0, 0, 0);
55 $menubar->append (my $actions_menu = Gtk2::MenuItem->new ('Actions'));
56 my $actmenu = Gtk2::Menu->new;
57 $actmenu->append (my $defitm = Gtk2::MenuItem->new ('reset to defaults'));
58 $defitm->signal_connect (activate => sub {
59 my $ar = $self->{archref};
60 $ar->reset_to_defaults;
61 #XXXAR $self->set_arch ($arch, $self->{change_cb});
62 });
63
64 $actmenu->append (my $attitm = Gtk2::MenuItem->new ('attach'));
65 $attitm->signal_connect (activate => sub {
66 my $ar = $self->{archref};
67 return unless $ar;
68 unless ($self->{attach_editor}) {
69 my $w = GCE::AttachEditor->new;
70 $w->set_attachment (
71 $ar->attr ('attach'),
72 sub {
73 if (@{$_[0]}) {
74 $ar->attr_set (attach => $_[0], undef, 'attr_edit')
75 } else {
76 $ar->attr_set (attach => undef, undef, 'attr_edit')
77 }
78 }
79 );
80 $self->{attach_editor} = $w;
81 $w->signal_connect (destroy => sub { delete $self->{attach_editor} });
82 $w->show_all;
83 }
84 });
85
86 $actions_menu->set_submenu ($actmenu);
87 $actmenu->show_all;
88
89 $topvb->pack_start (my $hb2 = Gtk2::HBox->new, 0, 1, 0);
90 $hb2->pack_start (my $img = $self->{arch_img} = (new_from_pixbuf Gtk2::Image $pb), 0, 0, 0);
91 $img->set_alignment (0, 0.5);
92
93 $hb2->pack_start (my $lbl = $self->{arch_name_lbl} = Gtk2::Label->new, 0, 0, 0);
94 $lbl->set_alignment (0, 0.5);
95
96 $hb2->pack_start (my $statbtn = $self->{arch_stat_btn} = Gtk2::Button->new, 0, 0, 0);
97
98 $topvb->pack_start (my $docal = Gtk2::Alignment->new (0, 0.5, 0, 1), 0, 1, 0);
99 $topvb->pack_start (my $usebtn = $self->{use_btn} = Gtk2::ToggleButton->new ('show use'), 0, 1, 0);
100 $topvb->pack_start (my $useal = $self->{use_al} = Gtk2::Alignment->new (0, 0.5, 0, 1), 0, 1, 0);
101 $topvb->pack_start (my $ntbook = $self->{ntbook} = Gtk2::Notebook->new, 1, 1, 0);
102 $ntbook->set_scrollable (1);
103 $docal->add ($self->{doc_lbl} = Gtk2::Label->new);
104
105 $usebtn->set_active (0);
106 $self->{use_lbl} = Gtk2::Label->new;
107 $usebtn->signal_connect (toggled => sub {
108 my ($usebtn) = @_;
109
110 $useal->remove ($_) for $useal->get_children;
111 if ($usebtn->get_active) {
112 $useal->add ($self->{use_lbl});
113 $self->{use_lbl}->show;
114 }
115 });
116 $self->{doc_lbl}->set_line_wrap (1);
117 $self->{use_lbl}->set_line_wrap (1);
118
119 $self->add2 (my $sw = Gtk2::ScrolledWindow->new);
120 $sw->set_policy ('automatic', 'automatic');
121 $sw->add_with_viewport (my $inv = $self->{inv_edit} = GCE::InventoryEditor->new);
122 }
123
124 sub get_arch {
125 my ($self) = @_;
126 $self->{arch}
127 }
128
129 sub update_attrs {
130 my ($self) = @_;
131
132 my $ar = $self->{arch};
133
134 $self->{attach_editor}->destroy
135 if $self->{attach_editor};
136
137 # XXX: port to new scheme
138 #$self->{inv_edit}->clear_inv_hist if $clear_inv;
139 #$self->{inv_edit}->set_arch ($ar);
140
141 $self->{arch_name_lbl}->set_text ($ar->longname);
142 warn "LONGNAME: " . $ar->longname . "\n";
143
144 fill_pb_from_arch ($self->{arch_pb}, $ar->get);
145 $self->{arch_img}->set_from_pixbuf ($self->{arch_pb});
146 $self->label_set_color ($self->{arch_name_lbl}, 0);
147
148 $self->{arch_stat_btn}->set_label ($ar->{source});
149
150 # get current page (to remember it for later)
151 my $pgnum = $self->{ntbook}->get_current_page;
152 my $curwid = $self->{ntbook}->get_nth_page ($pgnum);
153 my $curpage_text =
154 defined $curwid
155 ? $self->{ntbook}->get_tab_label_text ($curwid)
156 : undef;
157
158 my $al_arch = $ar->archetype;
159
160 $self->hide;
161 $self->{ntbook}->remove ($_)
162 for $self->{ntbook}->get_children;
163
164 $self->{ttip} = Gtk2::Tooltips->new;
165
166 my $type = $ar->type;
167
168 for my $sec (@{$type->{section}}) {
169 my $secname = shift @$sec;
170 $self->add_section_edit_widgets ($self->{ntbook}, $secname, $ar, $sec);
171 }
172
173 for my $key (qw/lore msg/) {
174 $self->{ntbook}->append_page (my $v = Gtk2::VBox->new, $key);
175 $v->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
176 $sw->set_policy ('automatic', 'automatic');
177 $sw->add (my $tb = $self->{"${key}_txt"} = Gtk2::TextView->new);
178 my $buf = $tb->get_buffer;
179
180 $buf->set_text ($ar->attr ($key));
181
182 $buf->signal_connect (changed => sub {
183 my ($buf) = @_;
184
185 my $txt =
186 $buf->get_text (
187 $buf->get_start_iter, $buf->get_end_iter, 0);
188
189 if ($txt ne $ar->attr ($key)) {
190 $ar->attr_set ($key, $txt, 'attr_edit');
191 }
192
193 1
194 });
195 }
196
197 my $desc = pseudohtml2txt $type->{desc};
198 my $use = pseudohtml2txt $type->{use};
199 $self->{doc_lbl}->set_text ($desc);
200 $self->{use_lbl}->set_text ($use);
201
202 $self->{ttip}->enable;
203
204 $self->show_all;
205
206 # reset the current page if found
207 # XXX: it's braindamaged: it has to be done AFTER show all for some reason
208 if (defined $curpage_text) {
209
210 for (my $i = 0; $i <= $self->{ntbook}->get_n_pages; $i++) {
211 my $w = $self->{ntbook}->get_nth_page ($i);
212
213 if ($w && $self->{ntbook}->get_tab_label_text ($w) eq $curpage_text) {
214 $self->{ntbook}->set_current_page ($i);
215 last;
216 }
217 }
218 }
219 }
220
221 sub update_view {
222 my ($self, $ar) = @_;
223
224 unless (defined $ar) {
225 delete $self->{arch_cb_reg};
226 $ar = $self->{arch};
227 }
228
229 unless (defined $ar) { # fallback in case the arch was deleted!
230 delete $self->{arch_cb_reg};
231 my $sr = GCE::StackRef->new (owner => 'attr_edit');
232 $ar = $sr->cursor (0);
233 }
234
235 $self->{arch} = $ar;
236
237 unless (defined $self->{arch_cb_reg}) {
238 $self->{arch_cb_reg} = $ar->reg_cb (changed => sub {
239 my ($ar, $change, $invalid) = @_;
240
241 if ($invalid) {
242 delete $self->{arch};
243 $self->update_view;
244
245 } elsif ($change =~ /attr_edit!attribute_edit/) {
246 # nop, skip update_view, WE changed!
247
248 } else {
249 $self->update_view;
250 }
251 });
252 }
253
254 $self->update_attrs;
255
256 $ar
257 }
258
259 sub add_section_edit_widgets {
260 my ($self, $ntbook, $name, $ar, $section) = @_;
261
262 $self->{ntbook}->append_page (my $sw = Gtk2::ScrolledWindow->new, $name);
263 $sw->set_policy ('automatic', 'automatic');
264 $sw->add_with_viewport (my $vb = Gtk2::VBox->new);
265 $vb->pack_start (my $table = new Gtk2::Table (2, (scalar @$section) + 1), 0, 1, 0);
266
267 my $i = 0;
268 for my $sec (@$section) {
269 my ($key, $sec) = ($sec->[0], $sec->[1]);
270
271 next if grep { $key eq $_ } qw/msg lore/;
272
273 my $bwid = Gtk2::EventBox->new;
274 $bwid->add (my $al = Gtk2::Alignment->new (0.0, 0.5, 0, 1));
275 $al->add (Gtk2::Label->new (def ($sec->{name}, $key)));
276
277 if ($sec->{desc} !~ m/^\s*$/s) {
278 $self->{ttip}->set_tip ($bwid, $sec->{desc});
279 }
280
281 $table->attach ($bwid, 0, 1, $i, $i + 1, ['shrink','fill'], 'fill', 5, 0);
282
283 $al = Gtk2::Alignment->new (0.0, 0.5, 1, 0);
284 $table->attach ($al, 1, 2, $i, $i + 1, ['expand', 'fill'], 'expand', 0, 0);
285 $al->add ($self->get_edit_widget ($key, $sec, $ar, $bwid));
286
287 $i++;
288 }
289
290 $ar
291 }
292
293 sub label_set_color {
294 my ($self, $lbl, $dark) = @_;
295 if ($dark) {
296 for (qw/normal active prelight selected insensitive/) {
297 $lbl->modify_bg ($_, $lbl->get_default_style->bg ('active'));
298 $lbl->modify_fg ($_, $lbl->get_default_style->fg ('active'));
299 }
300
301 } else {
302 for (qw/normal active prelight selected insensitive/) {
303 $lbl->modify_bg ($_, $lbl->get_default_style->bg ($_));
304 }
305 }
306
307 }
308
309 sub label_set_color_default {
310 my ($self, $lbl, $ar, $key, $val) = @_;
311 require Carp; $ar or Carp::confess ("UNDEF");
312 my $al_arch = $ar->archetype;
313 $self->label_set_color ($lbl, $ar->field_value_is_default ($key, $val));
314
315 }
316
317 # XXX: Warning: Ugly code ahead:
318 sub get_edit_widget {
319 my ($self, $key, $edspec, $ar, $lbl) = @_;
320
321 my $type = $edspec->{type};
322 my $al_arch = $ar->archetype;
323
324 if ($type eq 'bool') {
325 my $boolval = def ($edspec->{value}, [0, 1]);
326
327 $self->label_set_color_default ($lbl, $ar, $key, $ar->attr_or_arch ($key));
328
329 my $chk = new Gtk2::CheckButton (def ($edspec->{name}, $key));
330 $chk->set_active ($ar->attr_or_arch ($key) == $boolval->[1]);
331 $chk->signal_connect (clicked => sub {
332 my ($chk) = @_;
333
334 $ar->attr_set ($key, $boolval->[$chk->get_active * 1], $type, 'attr_edit');
335
336 $self->label_set_color_default ($lbl, $ar, $key, $boolval->[$chk->get_active * 1]);
337 });
338
339 $self->{ttip}->set_tip ($chk, $al_arch->{$key} * 1);
340
341 return $chk
342
343 } elsif (grep { $type eq $_ } qw/string int treasurelist float/) {
344 $self->label_set_color_default ($lbl, $ar, $key, $ar->attr_or_arch ($key));
345
346 my $entry = new Gtk2::Entry;
347 $entry->set_text ($ar->attr_or_arch ($key));
348 $entry->signal_connect (changed => sub {
349 my ($entry) = @_;
350 $self->label_set_color_default ($lbl, $ar, $key, $entry->get_text);
351 $ar->attr_set ($key, $entry->get_text, $type, 'attr_edit');
352 1
353 });
354
355 $self->{ttip}->set_tip ($entry, $ar->archetype->{$key});
356
357 return $entry
358
359 } elsif ($type eq 'spell' or $type eq 'nz_spell') { # XXX: nz_spell bug in datafiles?
360 my $comb = Gtk2::ComboBox->new_text;
361 my $spells_idx = {};
362 my $spells_cmd_idx = {};
363 my $sp = \%Deliantra::Data::SPELL;
364
365 $comb->append_text ("<none>");
366
367 my $idx = 1; # XXX: replace this idx with a more save/correct method?
368 for (sort { $sp->{$a} cmp $sp->{$b} } keys %$sp) {
369 $spells_cmd_idx->{$idx} = $_;
370 $spells_idx->{$_} = $idx++;
371
372 $comb->append_text ($sp->{$_});
373 }
374 #XXX: FIXME: $self->{ttip}->set_tip ($comb, $sp->{$al_arch->{$key}});
375
376 $comb->set_active ($spells_idx->{$ar->attr_or_arch ($key)});
377 $self->label_set_color_default ($lbl, $ar, $key, $ar->attr_or_arch ($key));
378
379 $comb->signal_connect (changed => sub {
380 my ($comb) = @_;
381 $self->label_set_color_default ($lbl, $ar, $key, $spells_cmd_idx->{$comb->get_active});
382 $ar->attr_set ($key, $spells_cmd_idx->{$comb->get_active}, $type, 'attr_edit');
383 });
384 return $comb
385
386 } elsif ($type eq 'bitmask') {
387 my $chval = $ar->attr_or_arch ($key);
388 my $btn = Gtk2::Button->new;
389 $btn->add (my $lblb = Gtk2::Label->new ("bitmask: " . ($chval * 1)));
390 $self->{ttip}->set_tip ($btn, $al_arch->{$key});
391
392 my $menu = $self->create_bitmask_menu ($edspec->{value}, $lbl, $lblb, $ar, $key, \$chval);
393
394 $self->label_set_color_default ($lbl, $ar, $key, $chval);
395
396 $btn->signal_connect (button_press_event => sub {
397 my ($btn, $ev) = @_;
398 $menu->popup (undef, undef, undef, undef, $ev->button, 0);
399 });
400
401 return $btn;
402
403 } elsif ($type eq 'list') {
404 my $lblb = Gtk2::Label->new ($edspec->{value}->{$ar->attr_or_arch ($key) * 1});
405 my $btn = Gtk2::Button->new;
406 $self->{ttip}->set_tip ($btn, $edspec->{value}->{$al_arch->{$key}});
407 $btn->add ($lblb);
408 my $menu = $self->create_list_menu ($edspec->{value}, $lbl, $lblb, $ar, $key);
409
410 $self->label_set_color_default ($lbl, $ar, $key, $ar->attr_or_arch ($key));
411
412 $btn->signal_connect (button_press_event => sub {
413 my ($btn, $ev) = @_;
414 $menu->popup (undef, undef, undef, undef, $ev->button, 0);
415 });
416 return $btn;
417
418 } elsif ($type eq 'fixed') {
419 return Gtk2::Label->new ("$edspec->{name} = $edspec->{value}");
420
421 } elsif ($type eq 'text') {
422 return Gtk2::Label->new ("<see $key tab>");
423
424 } elsif ($type eq 'movement_type') { # ok... this is quite a big one... awww
425 my $a = Gtk2::Alignment->new (0, 0, 0, 0);
426 $a->add (my $v = Gtk2::VBox->new (0, 0));
427 $a->set_padding (4, 4, 0, 0);
428 my $btns1 = Gtk2::HButtonBox->new;
429 my $btns2 = Gtk2::HButtonBox->new;
430 $btns1->set_layout ('start');
431 $btns2->set_layout ('start');
432
433 $v->pack_start ($btns1, 0, 1, 0);
434 $v->pack_start ($btns2, 0, 1, 0);
435
436 my @lblbtns;
437 my $calc_lbl = sub { # update callback for the buttons
438 for (@lblbtns) {
439 my $btn = $_->[0];
440 my $lbl = $_->[1];
441
442 if (defined $ar->attr ($key)) {
443 my $res = $ar->attr ($key) >= $lbl;
444
445 if (defined $res) {
446 $self->label_set_color ($btn, 0);
447 $lbl = ($res ? "+" : "-") . $lbl;
448
449 } else {
450 $self->label_set_color ($btn, 1);
451 $lbl = "$lbl?";
452 }
453 } else {
454 $self->label_set_color ($btn, 1);
455 $lbl = "$lbl?";
456 }
457
458 $btn->set_label ($lbl);
459 }
460 };
461
462 $self->label_set_color_default ($lbl, $ar, $key, $ar->attr_or_arch ($key));
463
464 my $mid = (scalar @Deliantra::MOVE_TYPE) / 2;
465 my $cnt = 0;
466 my $box = $btns1;
467
468 for my $mty (@Deliantra::MOVE_TYPE) {
469 $box->pack_start (my $btn = Gtk2::Button->new ($calc_lbl->()), 0, 1, 0);
470 push @lblbtns, [$btn, $mty];
471
472 $self->{ttip}->set_tip ($btn, $ar->archetype->{$key});
473 $btn->signal_connect (clicked => sub {
474 my $v = $ar->attr ($key) || Deliantra::MoveType->new;
475
476 $v x= $mty;
477 $v = $v eq '' ? undef : $v;
478 $ar->attr_set ($key, $v, $type, 'attr_edit');
479
480 $self->label_set_color_default ($lbl, $ar, $key, $v);
481 $calc_lbl->();
482 });
483
484 if (++$cnt >= $mid) { $box = $btns2 } # for wrapping into the second button box
485 }
486 $calc_lbl->();
487
488 return $a;
489
490 } else {
491 return Gtk2::Label->new ("$key => $edspec->{name} ($type)");
492
493 }
494 }
495
496 sub bitmask_to_list {
497 my ($self, $bitlist, $bits) = @_;
498
499 my @l;
500 for (%$bitlist) {
501 if ($bits & (1 << $_)) {
502 push @l, $bitlist->{$_};
503 }
504 }
505 return @l;
506 }
507
508 sub create_list_menu {
509 my ($self, $list, $clbl, $lbl, $ar, $key) = @_;
510
511 my $menu = Gtk2::Menu->new;
512
513 for my $item (sort keys %$list) {
514 my $lbltxt = $list->{$item};
515 my $menuitem = Gtk2::MenuItem->new_with_label ($lbltxt);
516 $menuitem->signal_connect (activate => sub {
517 my ($menuitem) = @_;
518 $lbl->set_text ($list->{$item});
519 $self->label_set_color_default ($clbl, $ar, $key, $item);
520 $ar->attr_set ($key, $item, undef, 'attr_edit');
521 });
522 $menu->append ($menuitem);
523 $menuitem->show;
524 }
525
526 return $menu;
527 }
528
529 sub create_bitmask_menu {
530 my ($self, $bits, $clbl, $lbl, $ar, $key, $rval) = @_;
531
532 my $menu = Gtk2::Menu->new;
533
534 for my $bit (sort { $a <=> $b } keys %$bits) {
535 my $lbltxt = $bits->{$bit};
536 my $menuitem = Gtk2::CheckMenuItem->new_with_label ($lbltxt);
537 if ($$rval & (1 << $bit)) {
538 $menuitem->set_active (1);#$arch->{$key} & (1 << $bit));
539 }
540 $menuitem->signal_connect (toggled => sub {
541 my ($menuitem) = @_;
542 my $newval = $ar->attr ($key);
543 $$rval &= ~(1 << $bit);
544 $$rval |= (1 << $bit) if $menuitem->get_active;
545 $lbl->set_text ("bitmask: " . ($$rval * 1));
546 $self->label_set_color_default ($clbl, $ar, $key, $$rval);
547 $ar->attr_set ($key, $$rval, undef, 'attr_edit');
548 });
549 $menu->append ($menuitem);
550 $menuitem->show;
551 }
552
553 return $menu;
554 }
555
556
557 =head1 AUTHOR
558
559 Marc Lehmann <schmorp@schmorp.de>
560 http://home.schmorp.de/
561
562 Robin Redeker <elmex@ta-sa.org>
563 http://www.ta-sa.org/
564
565 =cut
566 1;