ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/StackView.pm
Revision: 1.19
Committed: Thu Dec 27 22:28:01 2007 UTC (16 years, 4 months ago) by root
Branch: MAIN
CVS Tags: pre_cursor_branch, HEAD
Branch point for: cursor
Changes since 1.18: +2 -2 lines
Log Message:
upgrade Crossfire to Deliantra

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 Deliantra;
14 use Deliantra::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 maybe_update_stack_for {
34 my ($self, $mapedit, $x, $y) = @_;
35 if (my $d = $self->{stack_dest}) {
36 if ($d->[0] eq $mapedit && $d->[1] == $x && $d->[2] == $y) {
37 $self->set_stack
38 }
39 }
40 }
41
42 sub set_stack {
43 my ($self, $mapedit, $x, $y) = @_;
44
45 if (defined $mapedit) {
46 $self->{stack_dest} = [$mapedit, $x, $y];
47 } else {
48 $self->set_stack (@{$self->{stack_dest}})
49 if defined $self->{stack_dest};
50 return;
51 }
52
53 for ($self->{stackbox}->get_children) {
54 $self->{stackbox}->remove ($_);
55 }
56
57 my $stack = $mapedit->get ($x, $y);
58 my $idx = $stack ? (@$stack - 1) : 0;
59
60 if ($stack) {
61 for (reverse @$stack) {
62 # FIXME: How to change a stack with a virtual arch????
63 if ($_->{_virtual}) {
64 $idx--;
65 next
66 }
67 my $ownidx = $idx;
68
69 #my $a = $_->{_virtual} || $_;
70 my $a = $_;
71
72 # this is awful, is this really the best way?
73 my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
74
75 fill_pb_from_arch ($pb, $a);
76
77 $self->{stackbox}->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
78 $hb->pack_start (my $delbtn = Gtk2::Button->new_with_label ('del'), 0, 0, 0);
79 $delbtn->signal_connect (clicked => sub {
80
81 #my $oldstack = [ @$stack ];
82 splice @$stack, $ownidx, 1;
83
84 $mapedit->change_begin ('stackedit');
85 $mapedit->change_stack ($x, $y, $stack);
86
87 if (my $changeset = $mapedit->change_end) {
88 splice @{ $mapedit->{undo_stack} ||= [] },
89 $mapedit->{undo_stack_pos}++, 1e6,
90 $changeset;
91 }
92
93 # XXX: force an update ? maybe some more intelligent update later?
94 $self->set_stack ($mapedit, $x, $y);
95 });
96
97 $hb->pack_start (my $elemhdl = new Gtk2::Button, 0, 0, 0);
98 $elemhdl->add (my $hb2 = Gtk2::HBox->new);
99 $elemhdl->signal_connect (clicked => sub {
100 my $ar =
101 GCE::ArchRef->new (
102 arch => $a,
103 source => 'map',
104 cb => sub {
105 $mapedit->change_begin (ref $self);
106 $mapedit->change_stack ($x, $y, $stack);
107 # XXX: Put this into a generic function!!! See also EditTools.pm
108 # FIXME: Fix the automatic update on undo here!
109 if (my $changeset = $mapedit->change_end) {
110 splice @{ $mapedit->{undo_stack} ||= [] },
111 $mapedit->{undo_stack_pos}++, 1e6,
112 $changeset;
113 }
114 }
115 );
116
117 $::MAINWIN->update_attr_editor ($ar)
118 });
119
120 $hb2->pack_start (my $img = (new_from_pixbuf Gtk2::Image $pb), 0, 0, 0);
121 $img->set_alignment (0, 0.5);
122
123 $hb2->pack_start (my $lbl = Gtk2::Label->new ($a->{_name}), 0, 0, 0);
124 $lbl->set_alignment (0, 0.5);
125
126 $elemhdl->drag_source_set (['button1_mask'], ['move'],
127 { target => 'STRING', flags => [], info => 'TARGET_STRING' }
128 );
129 $elemhdl->drag_dest_set (all => ['move'],
130 { target => 'STRING', flags => [], info => 'TARGET_STRING' }
131 );
132
133 GCE::DragHelper::set_drag_source (
134 $elemhdl, arch => sub {
135 { arch => $stack->[$ownidx], stk => $stack, stk_idx => $ownidx }
136 }
137 );
138
139 GCE::DragHelper::set_drag_sink (
140 $elemhdl, arch => sub {
141 my ($darch) = @_;
142
143 if (defined $darch->{stk_idx} and $darch->{stk} == $stack) {
144 my $swapidx = $darch->{stk_idx};
145 ($stack->[$swapidx], $stack->[$ownidx])
146 = ($stack->[$ownidx], $stack->[$swapidx]);
147
148 } else {
149 splice @$stack, $ownidx, 1, (dclone $darch->{arch});
150 }
151
152 # XXX: Insert undo here!
153 $mapedit->set ($x, $y, $stack);
154
155 $self->set_stack ($mapedit, $x, $y);
156 }
157 );
158 $idx--;
159 }
160 }
161 $self->show_all;
162 }
163
164 =head1 AUTHOR
165
166 Marc Lehmann <schmorp@schmorp.de>
167 http://home.schmorp.de/
168
169 Robin Redeker <elmex@ta-sa.org>
170 http://www.ta-sa.org/
171
172 =cut
173 1;
174