ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/StackView.pm
Revision: 1.12
Committed: Sun Mar 26 20:42:12 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.11: +28 -40 lines
Log Message:
implemented better drag&drop. moved inventory editor out of the attribute editor.

File Contents

# Content
1 package GCE::StackView;
2
3 =head1 NAME
4
5 GCE::StackView - the stack window class for gce
6
7 =cut
8
9 use Gtk2;
10 use Gtk2::Gdk::Keysyms;
11 use Gtk2::SimpleMenu;
12
13 use Crossfire;
14 use Crossfire::MapWidget;
15
16 use GCE::AttrEdit;
17 use GCE::Util;
18
19 use Glib::Object::Subclass Gtk2::VBox;
20
21 use Storable qw/dclone/;
22
23 use strict;
24
25 sub INIT_INSTANCE {
26 my ($self) = @_;
27
28 $self->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
29 $sw->add_with_viewport ($self->{stackbox} = Gtk2::VBox->new);
30 $sw->set_policy ('automatic', 'automatic');
31 }
32
33 sub set_stack {
34 my ($self, $mapedit, $x, $y) = @_;
35
36 for ($self->{stackbox}->get_children) {
37
38 $self->{stackbox}->remove ($_);
39 }
40
41 my $stack = $mapedit->get ($x, $y);
42 my $idx = $stack ? (@$stack - 1) : 0;
43
44 if ($stack) {
45 for (reverse @$stack) {
46 # FIXME: How to change a stack with a virtual arch????
47 next if $_->{_virtual};
48 my $ownidx = $idx;
49
50 #my $a = $_->{_virtual} || $_;
51 my $a = $_;
52
53 # this is awful, is this really the best way?
54 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
55
56 fill_pb_from_arch ($pb, $a);
57
58 $self->{stackbox}->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
59 $hb->pack_start (my $delbtn = Gtk2::Button->new_with_label ('del'), 0, 0, 0);
60 $delbtn->signal_connect (clicked => sub {
61
62 #my $oldstack = [ @$stack ];
63 splice @$stack, $ownidx, 1;
64
65 # XXX: Insert undo here!
66 $mapedit->set ($x, $y, $stack);
67
68 # XXX: force an update ? maybe some more intelligent update later?
69 $self->set_stack ($mapedit, $x, $y);
70 });
71
72 $hb->pack_start (my $elemhdl = new Gtk2::Button, 0, 0, 0);
73 $elemhdl->add (my $hb2 = Gtk2::HBox->new);
74 $elemhdl->signal_connect (clicked => sub {
75 $::MAINWIN->set_pick ($a);
76 $::MAINWIN->update_attr_editor ($a, sub {
77 $mapedit->change_begin (ref $self);
78 $mapedit->change_stack ($x, $y, $stack);
79 # XXX: Put this into a generic function!!! See also EditTools.pm
80 # FIXME: Fix the automatic update on undo here!
81 if (my $changeset = $mapedit->change_end) {
82 splice @{ $mapedit->{undo_stack} ||= [] },
83 $mapedit->{undo_stack_pos}++, 1e6,
84 $changeset;
85 }
86 });
87 });
88
89 $hb2->pack_start (my $img = (new_from_pixbuf Gtk2::Image $pb), 0, 0, 0);
90 $img->set_alignment (0, 0.5);
91
92 $hb2->pack_start (my $lbl = Gtk2::Label->new ($a->{_name}), 0, 0, 0);
93 $lbl->set_alignment (0, 0.5);
94
95 $elemhdl->drag_source_set (['button1_mask'], ['move'],
96 { target => 'STRING', flags => [], info => 'TARGET_STRING' }
97 );
98 $elemhdl->drag_dest_set (all => ['move'],
99 { target => 'STRING', flags => [], info => 'TARGET_STRING' }
100 );
101
102 GCE::DragHelper::set_drag_source (
103 $elemhdl, arch => sub {
104 { arch => $stack->[$ownidx], stk => $stack, stk_idx => $ownidx }
105 }
106 );
107
108 GCE::DragHelper::set_drag_sink (
109 $elemhdl, arch => sub {
110 my ($darch) = @_;
111
112 if (defined $darch->{stk_idx} and $darch->{stk} == $stack) {
113 my $swapidx = $darch->{stk_idx};
114 ($stack->[$swapidx], $stack->[$ownidx])
115 = ($stack->[$ownidx], $stack->[$swapidx]);
116
117 } else {
118 splice @$stack, $ownidx, 1, (dclone $darch->{arch});
119 }
120
121 # XXX: Insert undo here!
122 $mapedit->set ($x, $y, $stack);
123
124 $self->set_stack ($mapedit, $x, $y);
125 }
126 );
127 $idx--;
128 }
129 }
130 $self->show_all;
131 }
132
133 =head1 AUTHOR
134
135 Marc Lehmann <schmorp@schmorp.de>
136 http://home.schmorp.de/
137
138 Robin Redeker <elmex@ta-sa.org>
139 http://www.ta-sa.org/
140
141 =cut
142 1;
143