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