ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/gde/GCE/StackView.pm
Revision: 1.17
Committed: Sun Jun 24 08:09:34 2007 UTC (19 years, 3 months ago) by elmex
Branch: MAIN
CVS Tags: rel-2_2
Changes since 1.16: +1 -0 lines
Log Message:
moved the buttons in the attribute editor into a menu and
added a button which displays the source of the selected
archetype for now until some better mechanism is found.

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 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     sub set_stack {
34 elmex 1.4 my ($self, $mapedit, $x, $y) = @_;
35 elmex 1.1
36     for ($self->{stackbox}->get_children) {
37    
38     $self->{stackbox}->remove ($_);
39     }
40    
41 elmex 1.4 my $stack = $mapedit->get ($x, $y);
42 elmex 1.10 my $idx = $stack ? (@$stack - 1) : 0;
43 elmex 1.1
44 elmex 1.11 if ($stack) {
45     for (reverse @$stack) {
46     # FIXME: How to change a stack with a virtual arch????
47 elmex 1.16 if ($_->{_virtual}) {
48     $idx--;
49     next
50     }
51 elmex 1.12 my $ownidx = $idx;
52 elmex 1.1
53 elmex 1.11 #my $a = $_->{_virtual} || $_;
54     my $a = $_;
55 elmex 1.1
56 elmex 1.11 # this is awful, is this really the best way?
57     my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
58 elmex 1.1
59 elmex 1.11 fill_pb_from_arch ($pb, $a);
60 elmex 1.1
61 elmex 1.11 $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 elmex 1.1
71 elmex 1.11 # XXX: force an update ? maybe some more intelligent update later?
72     $self->set_stack ($mapedit, $x, $y);
73 elmex 1.1 });
74 elmex 1.11
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 elmex 1.14 my $ar =
79     GCE::ArchRef->new (
80     arch => $a,
81 elmex 1.17 source => 'map',
82 elmex 1.14 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 elmex 1.15 $::MAINWIN->update_attr_editor ($ar)
96 elmex 1.11 });
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 elmex 1.1
104 elmex 1.11 $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 elmex 1.1
111 elmex 1.12 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 elmex 1.11 $idx--;
137     }
138 elmex 1.1 }
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