ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/gde/GCE/StackView.pm
Revision: 1.12
Committed: Sun Mar 26 20:42:12 2006 UTC (20 years, 6 months ago) by elmex
Branch: MAIN
Changes since 1.11: +28 -40 lines
Log Message:
implemented better drag&drop. moved inventory editor out of the attribute editor.

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