ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/gde/GCE/StackView.pm
Revision: 1.14
Committed: Tue Apr 4 21:12:08 2006 UTC (20 years, 6 months ago) by elmex
Branch: MAIN
Changes since 1.13: +17 -11 lines
Log Message:
Changed updating of attribute editor and inventory editor in a major manner.
We have now an abstraction layer between attribute editor and arch/object that
handles updating of the map and calling change_begin/set/end.

Works fine after testing, please watchout for bugs in attribute editor,
inventory editor and updating of these.

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 elmex 1.14 my $ar =
76     GCE::ArchRef->new (
77     arch => $a,
78     cb => sub {
79     $mapedit->change_begin (ref $self);
80     $mapedit->change_stack ($x, $y, $stack);
81     # XXX: Put this into a generic function!!! See also EditTools.pm
82     # FIXME: Fix the automatic update on undo here!
83     if (my $changeset = $mapedit->change_end) {
84     splice @{ $mapedit->{undo_stack} ||= [] },
85     $mapedit->{undo_stack_pos}++, 1e6,
86     $changeset;
87     }
88     }
89     );
90    
91     $::MAINWIN->update_attr_editor ($a)
92 elmex 1.11 });
93    
94     $hb2->pack_start (my $img = (new_from_pixbuf Gtk2::Image $pb), 0, 0, 0);
95     $img->set_alignment (0, 0.5);
96    
97     $hb2->pack_start (my $lbl = Gtk2::Label->new ($a->{_name}), 0, 0, 0);
98     $lbl->set_alignment (0, 0.5);
99 elmex 1.1
100 elmex 1.11 $elemhdl->drag_source_set (['button1_mask'], ['move'],
101     { target => 'STRING', flags => [], info => 'TARGET_STRING' }
102     );
103     $elemhdl->drag_dest_set (all => ['move'],
104     { target => 'STRING', flags => [], info => 'TARGET_STRING' }
105     );
106 elmex 1.1
107 elmex 1.12 GCE::DragHelper::set_drag_source (
108     $elemhdl, arch => sub {
109     { arch => $stack->[$ownidx], stk => $stack, stk_idx => $ownidx }
110     }
111     );
112    
113     GCE::DragHelper::set_drag_sink (
114     $elemhdl, arch => sub {
115     my ($darch) = @_;
116    
117     if (defined $darch->{stk_idx} and $darch->{stk} == $stack) {
118     my $swapidx = $darch->{stk_idx};
119     ($stack->[$swapidx], $stack->[$ownidx])
120     = ($stack->[$ownidx], $stack->[$swapidx]);
121    
122     } else {
123     splice @$stack, $ownidx, 1, (dclone $darch->{arch});
124     }
125    
126     # XXX: Insert undo here!
127     $mapedit->set ($x, $y, $stack);
128    
129     $self->set_stack ($mapedit, $x, $y);
130     }
131     );
132 elmex 1.11 $idx--;
133     }
134 elmex 1.1 }
135     $self->show_all;
136     }
137    
138     =head1 AUTHOR
139    
140     Marc Lehmann <schmorp@schmorp.de>
141     http://home.schmorp.de/
142    
143     Robin Redeker <elmex@ta-sa.org>
144     http://www.ta-sa.org/
145    
146     =cut
147     1;
148