ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/gde/GCE/StackView.pm
Revision: 1.11
Committed: Sun Mar 19 19:30:38 2006 UTC (20 years, 6 months ago) by elmex
Branch: MAIN
Changes since 1.10: +81 -79 lines
Log Message:
many changes. fixed a bug in stack view with virtual tiles.

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     use Crossfire;
14     use Crossfire::MapWidget;
15    
16     use GCE::AttrEdit;
17 elmex 1.10 use GCE::Util;
18 elmex 1.1
19     use Glib::Object::Subclass Gtk2::VBox;
20    
21     use strict;
22    
23     sub INIT_INSTANCE {
24     my ($self) = @_;
25    
26     $self->pack_start (my $sw = Gtk2::ScrolledWindow->new, 1, 1, 0);
27     $sw->add_with_viewport ($self->{stackbox} = Gtk2::VBox->new);
28 elmex 1.7 $sw->set_policy ('automatic', 'automatic');
29 elmex 1.1 }
30    
31     sub set_stack {
32 elmex 1.4 my ($self, $mapedit, $x, $y) = @_;
33 elmex 1.1
34     for ($self->{stackbox}->get_children) {
35    
36     $self->{stackbox}->remove ($_);
37     }
38    
39 elmex 1.4 my $stack = $mapedit->get ($x, $y);
40 elmex 1.10 my $idx = $stack ? (@$stack - 1) : 0;
41 elmex 1.1
42 elmex 1.11 if ($stack) {
43     for (reverse @$stack) {
44     # FIXME: How to change a stack with a virtual arch????
45     next if $_->{_virtual};
46 elmex 1.1
47 elmex 1.11 #my $a = $_->{_virtual} || $_;
48     my $a = $_;
49 elmex 1.1
50 elmex 1.11 # this is awful, is this really the best way?
51     my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
52 elmex 1.1
53 elmex 1.11 fill_pb_from_arch ($pb, $a);
54 elmex 1.1
55 elmex 1.11 $self->{stackbox}->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
56     $hb->pack_start (my $delbtn = Gtk2::Button->new_with_label ('del'), 0, 0, 0);
57 elmex 1.1 do {
58     my $ownidx = $idx;
59    
60 elmex 1.11 $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 elmex 1.1
68 elmex 1.11 # XXX: force an update ? maybe some more intelligent update later?
69     $self->set_stack ($mapedit, $x, $y);
70 elmex 1.1 });
71 elmex 1.11 };
72    
73     $hb->pack_start (my $elemhdl = new Gtk2::Button, 0, 0, 0);
74     $elemhdl->add (my $hb2 = Gtk2::HBox->new);
75     $elemhdl->signal_connect (clicked => sub {
76     $::MAINWIN->set_pick ($a);
77     $::MAINWIN->update_attr_editor ($a, sub {
78     $mapedit->change_begin (ref $self);
79     $mapedit->change_stack ($x, $y, $stack);
80     # XXX: Put this into a generic function!!! See also EditTools.pm
81     # FIXME: Fix the automatic update on undo here!
82     if (my $changeset = $mapedit->change_end) {
83     splice @{ $mapedit->{undo_stack} ||= [] },
84     $mapedit->{undo_stack_pos}++, 1e6,
85     $changeset;
86     }
87     });
88     });
89    
90     $hb2->pack_start (my $img = (new_from_pixbuf Gtk2::Image $pb), 0, 0, 0);
91     $img->set_alignment (0, 0.5);
92    
93     $hb2->pack_start (my $lbl = Gtk2::Label->new ($a->{_name}), 0, 0, 0);
94     $lbl->set_alignment (0, 0.5);
95 elmex 1.1
96 elmex 1.11 $elemhdl->drag_source_set (['button1_mask'], ['move'],
97     { target => 'STRING', flags => [], info => 'TARGET_STRING' }
98     );
99     $elemhdl->drag_dest_set (all => ['move'],
100     { target => 'STRING', flags => [], info => 'TARGET_STRING' }
101     );
102 elmex 1.1
103 elmex 1.11 do {
104     my $ownidx = $idx;
105 elmex 1.1
106 elmex 1.11 $elemhdl->signal_connect (drag_data_get => sub {
107     my ($widget, $context, $data, $info, $time) = @_;
108 elmex 1.1
109 elmex 1.11 $data->set ($data->target, 8, "stack:$ownidx");
110     });
111 elmex 1.1
112 elmex 1.11 # XXX: I'm unsure here, do i have to issue a get request?
113     # And what if i get the data twice? Wait for transaction end?
114     $elemhdl->signal_connect (drag_data_received => sub {
115     my ($widget, $context, $wx, $wy, $data, $info, $time) = @_;
116 elmex 1.1
117 elmex 1.11 if (($data->length >= 0) && ($data->format == 8)) {
118 elmex 1.4
119 elmex 1.11 $context->finish (1, 0, $time);
120    
121     if ($data->data =~ m/stack:(\d+)/) {
122     my $swapidx = int $1;
123    
124     ($stack->[$swapidx], $stack->[$ownidx])
125     = ($stack->[$ownidx], $stack->[$swapidx]);
126    
127     # XXX: Insert undo here!
128     $mapedit->set ($x, $y, $stack);
129    
130     $self->set_stack ($mapedit, $x, $y);
131     }
132    
133     return;
134 elmex 1.8 }
135 elmex 1.11 $context->finish (0, 0, $time);
136     });
137     };
138     $idx--;
139     }
140 elmex 1.1 }
141     $self->show_all;
142    
143     }
144    
145     =head1 AUTHOR
146    
147     Marc Lehmann <schmorp@schmorp.de>
148     http://home.schmorp.de/
149    
150     Robin Redeker <elmex@ta-sa.org>
151     http://www.ta-sa.org/
152    
153     =cut
154     1;
155