ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MapEditor.pm
Revision: 1.14
Committed: Fri Mar 10 20:32:47 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.13: +3 -2 lines
Log Message:
implemented edit tools widgets and improved the placement tool

File Contents

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