1 |
package GCE::MapEditor; |
2 |
|
3 |
=head1 NAME |
4 |
|
5 |
GCE::MapEditor - the map editing widget |
6 |
|
7 |
=cut |
8 |
|
9 |
use Gtk2; |
10 |
use Gtk2::Gdk::Keysyms; |
11 |
use Gtk2::SimpleMenu; |
12 |
|
13 |
use Crossfire; |
14 |
use Crossfire::Map; |
15 |
use Crossfire::MapWidget; |
16 |
|
17 |
use GCE::AttrEdit; |
18 |
|
19 |
use Glib::Object::Subclass |
20 |
Gtk2::Window; |
21 |
|
22 |
use strict; |
23 |
|
24 |
sub build_menu { |
25 |
my ($self) = @_; |
26 |
|
27 |
my $menu_tree = [ |
28 |
_File => { |
29 |
item_type => '<Branch>', |
30 |
children => [ |
31 |
"_Save" => { |
32 |
callback => sub { $self->save_map }, |
33 |
accelerator => '<ctrl>S' |
34 |
}, |
35 |
], |
36 |
children => [ |
37 |
"Save As" => { |
38 |
callback => sub { $self->save_map_as }, |
39 |
}, |
40 |
] |
41 |
}, |
42 |
_Edit => { |
43 |
item_type => '<Branch>', |
44 |
children => [ |
45 |
"_Undo" => { |
46 |
callback => sub { $self->undo }, |
47 |
accelerator => "<ctrl>Z" |
48 |
}, |
49 |
"_Redo" => { |
50 |
callback => sub { $self->redo }, |
51 |
accelerator => "<ctrl>Y" |
52 |
}, |
53 |
|
54 |
] |
55 |
}, |
56 |
|
57 |
]; |
58 |
|
59 |
my $men = |
60 |
Gtk2::SimpleMenu->new ( |
61 |
menu_tree => $menu_tree, |
62 |
default_callback => \&default_cb, |
63 |
); |
64 |
|
65 |
$self->add_accel_group ($men->{accel_group}); |
66 |
|
67 |
return $men->{widget}; |
68 |
} |
69 |
|
70 |
sub INIT_INSTANCE { |
71 |
my ($self) = @_; |
72 |
|
73 |
$self->set_title ('gce - map editor'); |
74 |
$self->add (my $vb = Gtk2::VBox->new); |
75 |
|
76 |
$vb->pack_start (my $menu = $self->build_menu, 0, 1, 0); |
77 |
|
78 |
$vb->pack_start (my $map = $self->{map} = Crossfire::MapWidget->new, 1, 1, 0); |
79 |
|
80 |
$map->signal_connect (button_press_event => sub { |
81 |
my ($map, $event) = @_; |
82 |
|
83 |
my ($x, $y) = $map->coord ($event->x, $event->y); |
84 |
my $as = $map->get ($x, $y); |
85 |
|
86 |
my $btn = $event->button; |
87 |
|
88 |
if ((not $self->{draw_mode}) |
89 |
and $btn != 2 |
90 |
and my $ea = $::MAINWIN->{sel_editaction}) { |
91 |
|
92 |
$ea->begin ($map, $x, $y, $btn); |
93 |
|
94 |
$self->{draw_mode} = [$ea, $x, $y, $btn]; |
95 |
|
96 |
$ea->want_cursor |
97 |
or $map->disable_tooltip; |
98 |
|
99 |
if ($ea->special_arrow) { |
100 |
|
101 |
$map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($ea->special_arrow)); |
102 |
#'GDK_QUESTION_ARROW')); |
103 |
} |
104 |
|
105 |
return 1; |
106 |
} |
107 |
|
108 |
0 |
109 |
}); |
110 |
|
111 |
$map->signal_connect_after (motion_notify_event => sub { |
112 |
my ($map, $event) = @_; |
113 |
|
114 |
$self->{draw_mode} |
115 |
or return; |
116 |
|
117 |
my ($X, $Y) = @{$self->{draw_mode}}[1,2]; |
118 |
my ($x, $y) = $map->coord ($map->get_pointer); |
119 |
|
120 |
while ($x != $X || $y != $Y) { |
121 |
|
122 |
$X++ if $X < $x; |
123 |
$X-- if $X > $x; |
124 |
$Y++ if $Y < $y; |
125 |
$Y-- if $Y > $y; |
126 |
|
127 |
$self->{draw_mode}[0]->edit ($map, $X, $Y, $self->{draw_mode}[3]); |
128 |
} |
129 |
|
130 |
@{$self->{draw_mode}}[1,2] = ($X, $Y); |
131 |
|
132 |
1 |
133 |
}); |
134 |
|
135 |
$map->signal_connect (button_release_event => sub { |
136 |
my ($map, $event) = @_; |
137 |
|
138 |
if ($self->{draw_mode} |
139 |
and my $ea = $self->{draw_mode}[0] |
140 |
and $self->{draw_mode}[3] == $event->button) { |
141 |
|
142 |
my ($x, $y) = $map->coord ($map->get_pointer); |
143 |
|
144 |
$ea->end ($map, $x, $y, $event->button); |
145 |
|
146 |
if ($ea->special_arrow) { |
147 |
|
148 |
# XXX: Get the original cursor and insert it here |
149 |
$map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR')); |
150 |
} |
151 |
|
152 |
delete $self->{draw_mode}; |
153 |
|
154 |
$ea->want_cursor |
155 |
or $map->enable_tooltip; |
156 |
|
157 |
return 1; |
158 |
} |
159 |
|
160 |
0 |
161 |
}); |
162 |
} |
163 |
|
164 |
sub undo { |
165 |
my ($self) = @_; |
166 |
|
167 |
my $map = $self->{map}; # the Crossfire::MapWidget |
168 |
|
169 |
$map->{undo_stack_pos} |
170 |
or return; |
171 |
|
172 |
$map->change_swap ($map->{undo_stack}[--$map->{undo_stack_pos}]); |
173 |
} |
174 |
|
175 |
sub redo { |
176 |
my ($self) = @_; |
177 |
|
178 |
my $map = $self->{map}; # the Crossfire::MapWidget |
179 |
|
180 |
$map->{undo_stack_pos} < @{$map->{undo_stack}} |
181 |
or return; |
182 |
|
183 |
$map->change_swap ($map->{undo_stack}[$map->{undo_stack_pos}++]); |
184 |
} |
185 |
|
186 |
sub delete_arch { |
187 |
my ($self, $x, $y) = @_; |
188 |
|
189 |
defined $self->{map} |
190 |
or return 0; |
191 |
|
192 |
my $as = $self->{map}->get ($x, $y); |
193 |
pop @$as; |
194 |
$self->{map}->set ($x, $y, $as); |
195 |
} |
196 |
|
197 |
sub place_pick { |
198 |
my ($self, $x, $y) = @_; |
199 |
|
200 |
my $pick = $::MAINWIN->get_pick; |
201 |
my $as = $self->{map}->get ($x, $y); |
202 |
|
203 |
my $arch = { _name => $pick->{_name} }; |
204 |
|
205 |
push @$as, $arch |
206 |
unless @$as && $as->[-1]->{_name} eq $arch->{_name}; |
207 |
|
208 |
$self->{map}->set ($x, $y, $as); |
209 |
} |
210 |
|
211 |
sub open_map { |
212 |
my ($self, $path) = @_; |
213 |
|
214 |
$self->{map}->set_map (new_from_file Crossfire::Map $path); |
215 |
} |
216 |
|
217 |
=head1 AUTHOR |
218 |
|
219 |
Marc Lehmann <schmorp@schmorp.de> |
220 |
http://home.schmorp.de/ |
221 |
|
222 |
Robin Redeker <elmex@ta-sa.org> |
223 |
http://www.ta-sa.org/ |
224 |
|
225 |
=cut |
226 |
1; |
227 |
|