ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/gde/GCE/StackView.pm
Revision: 1.19.2.1
Committed: Tue Jul 7 15:42:18 2009 UTC (17 years, 3 months ago) by elmex
Branch: cursor
Changes since 1.19: +39 -46 lines
Log Message:
*** empty log message ***

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 elmex 1.19.2.1 $::MAINWIN->reg_cb (
33     cursor_updates => sub {
34     my ($mainwin, $cursors) = @_;
35    
36     if (grep { $_ == $self->{archref} } @$cursors) {
37     $self->set_stack; # just force an update
38     }
39 elmex 1.18 }
40 elmex 1.19.2.1 );
41 elmex 1.18 }
42    
43 elmex 1.1 sub set_stack {
44 elmex 1.4 my ($self, $mapedit, $x, $y) = @_;
45 elmex 1.1
46 elmex 1.18 if (defined $mapedit) {
47 elmex 1.19.2.1 $self->{archref} = $::MAINWIN->cursor ($mapedit, 'stack_view', $x, $y);
48    
49 elmex 1.18 } else {
50 elmex 1.19.2.1 $mapedit = $self->{archref}->{map};
51     ($x, $y) = ($self->{archref}->{x}, $self->{archref}->{y});
52 elmex 1.18 }
53    
54 elmex 1.19.2.1 my $stack = $mapedit->{map}->get_ro ($x, $y);
55     my $idx = $stack ? (@$stack - 1) : 0;
56    
57 elmex 1.1 for ($self->{stackbox}->get_children) {
58     $self->{stackbox}->remove ($_);
59     }
60    
61 elmex 1.11 if ($stack) {
62     for (reverse @$stack) {
63     # FIXME: How to change a stack with a virtual arch????
64 elmex 1.16 if ($_->{_virtual}) {
65     $idx--;
66     next
67     }
68 elmex 1.12 my $ownidx = $idx;
69 elmex 1.1
70 elmex 1.11 #my $a = $_->{_virtual} || $_;
71     my $a = $_;
72 elmex 1.1
73 elmex 1.11 # this is awful, is this really the best way?
74     my $pb = new Gtk2::Gdk::Pixbuf 'rgb', 1, 8, TILESIZE, TILESIZE;
75 elmex 1.1
76 elmex 1.11 fill_pb_from_arch ($pb, $a);
77 elmex 1.1
78 elmex 1.11 $self->{stackbox}->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
79     $hb->pack_start (my $delbtn = Gtk2::Button->new_with_label ('del'), 0, 0, 0);
80     $delbtn->signal_connect (clicked => sub {
81    
82 elmex 1.19.2.1 $mapedit->{map}->change_begin ('stackedit_delete');
83 elmex 1.11
84 elmex 1.19.2.1 my $stack = $mapedit->{map}->get ($x, $y);
85     splice @$stack, $ownidx, 1;
86     $mapedit->{map}->change_stack ($x, $y, $stack);
87 elmex 1.18
88 elmex 1.19.2.1 if (my $changeset = $mapedit->{map}->change_end) {
89     splice @{ $mapedit->{map}->{undo_stack} ||= [] },
90     $mapedit->{map}->{undo_stack_pos}++, 1e6,
91 elmex 1.18 $changeset;
92     }
93 elmex 1.1
94 elmex 1.19.2.1 $::MAINWIN->broadcast_cursor_changes ($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.19.2.1 $::MAINWIN->update_attr_editor ($mapedit, $x, $y);
101 elmex 1.11 });
102    
103     $hb2->pack_start (my $img = (new_from_pixbuf Gtk2::Image $pb), 0, 0, 0);
104     $img->set_alignment (0, 0.5);
105    
106     $hb2->pack_start (my $lbl = Gtk2::Label->new ($a->{_name}), 0, 0, 0);
107     $lbl->set_alignment (0, 0.5);
108 elmex 1.1
109 elmex 1.11 $elemhdl->drag_source_set (['button1_mask'], ['move'],
110     { target => 'STRING', flags => [], info => 'TARGET_STRING' }
111     );
112     $elemhdl->drag_dest_set (all => ['move'],
113     { target => 'STRING', flags => [], info => 'TARGET_STRING' }
114     );
115 elmex 1.1
116 elmex 1.12 GCE::DragHelper::set_drag_source (
117 elmex 1.19.2.1 $elemhdl, arch => sub {
118 elmex 1.12 { arch => $stack->[$ownidx], stk => $stack, stk_idx => $ownidx }
119     }
120     );
121    
122     GCE::DragHelper::set_drag_sink (
123     $elemhdl, arch => sub {
124     my ($darch) = @_;
125    
126 elmex 1.19.2.1 $mapedit->{map}->change_begin ('stackedit_swap');
127    
128     my $stack = $mapedit->{map}->get ($x, $y);
129    
130     if (defined $darch->{stk_idx}) { # FIXME: check stack source!
131 elmex 1.12 my $swapidx = $darch->{stk_idx};
132     ($stack->[$swapidx], $stack->[$ownidx])
133     = ($stack->[$ownidx], $stack->[$swapidx]);
134    
135     } else {
136     splice @$stack, $ownidx, 1, (dclone $darch->{arch});
137     }
138    
139 elmex 1.19.2.1 $mapedit->{map}->change_stack ($x, $y, $stack);
140    
141     if (my $changeset = $mapedit->{map}->change_end) {
142     splice @{ $mapedit->{map}->{undo_stack} ||= [] },
143     $mapedit->{map}->{undo_stack_pos}++, 1e6,
144     $changeset;
145     }
146 elmex 1.12
147 elmex 1.19.2.1 $::MAINWIN->broadcast_cursor_changes ($mapedit, $x, $y);
148 elmex 1.12 }
149     );
150 elmex 1.11 $idx--;
151     }
152 elmex 1.1 }
153 elmex 1.19.2.1
154 elmex 1.1 $self->show_all;
155     }
156    
157     =head1 AUTHOR
158    
159     Marc Lehmann <schmorp@schmorp.de>
160     http://home.schmorp.de/
161    
162     Robin Redeker <elmex@ta-sa.org>
163     http://www.ta-sa.org/
164    
165     =cut
166     1;
167