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, 5 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

# User Rev Content
1 elmex 1.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 root 1.19 use Deliantra;
14     use Deliantra::MapWidget;
15 elmex 1.1
16     use GCE::AttrEdit;
17 elmex 1.10 use GCE::Util;
18 elmex 1.1
19     use Glib::Object::Subclass Gtk2::VBox;
20    
21 elmex 1.12 use Storable qw/dclone/;
22    
23 elmex 1.1 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 elmex 1.7 $sw->set_policy ('automatic', 'automatic');
31 elmex 1.1 }
32    
33 elmex 1.18 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 elmex 1.1 sub set_stack {
43 elmex 1.4 my ($self, $mapedit, $x, $y) = @_;
44 elmex 1.1
45 elmex 1.18 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 elmex 1.1 for ($self->{stackbox}->get_children) {
54     $self->{stackbox}->remove ($_);
55     }
56    
57 elmex 1.4 my $stack = $mapedit->get ($x, $y);
58 elmex 1.10 my $idx = $stack ? (@$stack - 1) : 0;
59 elmex 1.1
60 elmex 1.11 if ($stack) {
61     for (reverse @$stack) {
62     # FIXME: How to change a stack with a virtual arch????
63 elmex 1.16 if ($_->{_virtual}) {
64     $idx--;
65     next
66     }
67 elmex 1.12 my $ownidx = $idx;
68 elmex 1.1
69 elmex 1.11 #my $a = $_->{_virtual} || $_;
70     my $a = $_;
71 elmex 1.1
72 elmex 1.11 # this is awful, is this really the best way?
73     my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
74 elmex 1.1
75 elmex 1.11 fill_pb_from_arch ($pb, $a);
76 elmex 1.1
77 elmex 1.11 $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 elmex 1.18 $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 elmex 1.1
93 elmex 1.11 # XXX: force an update ? maybe some more intelligent update later?
94     $self->set_stack ($mapedit, $x, $y);
95 elmex 1.1 });
96 elmex 1.11
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 elmex 1.14 my $ar =
101     GCE::ArchRef->new (
102     arch => $a,
103 elmex 1.17 source => 'map',
104 elmex 1.14 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 elmex 1.15 $::MAINWIN->update_attr_editor ($ar)
118 elmex 1.11 });
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 elmex 1.1
126 elmex 1.11 $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 elmex 1.1
133 elmex 1.12 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 elmex 1.11 $idx--;
159     }
160 elmex 1.1 }
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