ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MapEditor.pm
(Generate patch)

Comparing deliantra/gde/GCE/MapEditor.pm (file contents):
Revision 1.27 by elmex, Fri Mar 17 17:59:43 2006 UTC vs.
Revision 1.57 by elmex, Fri Aug 10 12:20:51 2007 UTC

14use Crossfire::Map; 14use Crossfire::Map;
15use Crossfire::MapWidget; 15use Crossfire::MapWidget;
16 16
17use GCE::AttrEdit; 17use GCE::AttrEdit;
18use GCE::Util; 18use GCE::Util;
19use GCE::HashDialog;
20
21use POSIX qw/strftime/;
19 22
20use Glib::Object::Subclass 23use Glib::Object::Subclass
21 Gtk2::Window; 24 Gtk2::Window;
22 25
26use Storable qw/dclone/;
27
23use strict; 28use strict;
29
30#################################################################
31###### WINDOW MANAGEMENT ########################################
32#################################################################
33
34sub save_layout {
35 my ($self) = @_;
36
37 $self->{attach_editor}->save_layout if $self->{attach_editor};
38 $self->{map_properties}->save_layout if $self->{map_properties};
39 $self->{meta_info_win}->save_layout if $self->{meta_info_win};
40
41 $main::CFG->{map_info} = main::get_pos_and_size ($self->{map_info})
42 if $self->{map_info};
43}
44
45sub close_windows {
46 my ($self) = @_;
47
48 $self->{attach_editor}->destroy if $self->{attach_editor};
49 $self->{map_properties}->destroy if $self->{map_properties};
50 $self->{meta_info_win}->destroy if $self->{meta_info_win};
51}
52
53#################################################################
54###### MENU MANAGEMENT ##########################################
55#################################################################
56
57sub do_context_menu {
58 my ($self, $map, $event) = @_;
59
60 my ($x, $y) = $map->coord ($event->x, $event->y);
61
62 my $menu = Gtk2::Menu->new;
63 foreach my $cm (
64 [
65 Follow => sub {
66 $::MAINWIN->{edit_collection}{followexit}->edit ($map, $x, $y, $self)
67 },
68 ]
69 ) {
70 my $item = Gtk2::MenuItem->new ($cm->[0]);
71 $menu->append ($item);
72 $item->show;
73 $item->signal_connect (activate => $cm->[1]);
74 }
75
76 $menu->append (my $sep = new Gtk2::SeparatorMenuItem);
77 $sep->show;
78
79 for my $sr (reverse $self->get_stack_refs ($map, $x, $y)) {
80 my $item = Gtk2::MenuItem->new ($sr->longname);
81 $menu->append ($item);
82 $item->set_submenu (my $smenu = new Gtk2::Menu);
83
84 for my $act (
85 [ 'Add inventory' => sub { $_[0]->add_inv ($::MAINWIN->get_pick) } ],
86 [ 'Find in picker' => sub { $::MAINWIN->open_pick_window ({ selection => $sr->picker_folder }) } ],
87 ) {
88 my $sitem = Gtk2::MenuItem->new ($act->[0]);
89 $smenu->append ($sitem);
90 $sitem->signal_connect (activate => sub { $act->[1]->($sr) });
91 $sitem->show;
92 }
93
94 $item->show;
95 }
96
97 $menu->popup (undef, undef, undef, undef, $event->button, $event->time);
98}
24 99
25sub build_menu { 100sub build_menu {
26 my ($self) = @_; 101 my ($self) = @_;
27 102
28 my $menu_tree = [ 103 my $menu_tree = [
34 accelerator => '<ctrl>S' 109 accelerator => '<ctrl>S'
35 }, 110 },
36 "Save As" => { 111 "Save As" => {
37 callback => sub { $self->save_map_as }, 112 callback => sub { $self->save_map_as },
38 }, 113 },
114 "Map _Info" => {
115 callback => sub { $self->open_map_info },
116 accelerator => "<ctrl>I",
117 },
118 "Map _Properties" => {
119 callback => sub { $self->open_map_prop },
120 accelerator => "<ctrl>P"
121 },
122 "Map _Attachments" => {
123 callback => sub { $self->open_attach_edit },
124 accelerator => "<ctrl>A"
125 },
126 "Map Meta _Info" => {
127 callback => sub { $self->open_meta_info },
128 },
129 Upload => {
130 item_type => '<Branch>',
131 children => [
132 "Upload for testing" => {
133 callback => sub { $self->upload_map_test },
134 },
135 "Upload for inclusion" => {
136 callback => sub { $self->upload_map_incl },
137 },
138 ]
139 },
140 "_Map Resize" => {
141 callback => sub { $self->open_resize_map },
142 },
39 "Close" => { 143 "Close" => {
40 callback => sub { $self->delete; 1 }, 144 callback => sub { $self->destroy },
41 }, 145 },
42 ] 146 ]
43 }, 147 },
44 _Edit => { 148 _Edit => {
45 item_type => '<Branch>', 149 item_type => '<Branch>',
50 }, 154 },
51 "_Redo" => { 155 "_Redo" => {
52 callback => sub { $self->redo }, 156 callback => sub { $self->redo },
53 accelerator => "<ctrl>Y" 157 accelerator => "<ctrl>Y"
54 }, 158 },
55 "_Map Properties" => {
56 callback => sub { $self->open_map_prop },
57 accelerator => "<ctrl>P"
58 },
59 "_Map Resize" => {
60 callback => sub { $self->open_resize_map },
61 accelerator => "<ctrl>R"
62 },
63 ] 159 ]
64 }, 160 },
65 _Go => { 161 _Go => {
66 item_type => '<Branch>', 162 item_type => '<Branch>',
67 children => [ 163 children => [
68 "_Up" => { 164 "_Up" => {
69 callback => sub { $self->follow ('u') }, 165 callback => sub { $self->follow ('u') },
70 accelerator => "<ctrl>U" 166 accelerator => "<ctrl>Up"
71 }, 167 },
72 "_Down" => { 168 "_Down" => {
73 callback => sub { $self->follow ('d') }, 169 callback => sub { $self->follow ('d') },
74 accelerator => "<ctrl>D" 170 accelerator => "<ctrl>Down"
75 }, 171 },
76 "_Right" => { 172 "_Right" => {
77 callback => sub { $self->follow ('r') }, 173 callback => sub { $self->follow ('r') },
78 accelerator => "<ctrl>R" 174 accelerator => "<ctrl>Right"
79 }, 175 },
80 "_Left" => { 176 "_Left" => {
81 callback => sub { $self->follow ('l') }, 177 callback => sub { $self->follow ('l') },
82 accelerator => "<ctrl>L" 178 accelerator => "<ctrl>Left"
83 }, 179 },
84 ] 180 ]
85 }, 181 },
86 182 _Help => {
183 item_type => '<Branch>',
184 children => [
185 _Manual => {
186 callback => sub { $::MAINWIN->show_help_window },
187 accelerator => "<ctrl>H"
188 },
189 ]
190 },
87 ]; 191 ];
88 192
89 my $men = 193 my $men =
90 Gtk2::SimpleMenu->new ( 194 Gtk2::SimpleMenu->new (
91 menu_tree => $menu_tree, 195 menu_tree => $menu_tree,
96 [i => 'pick'], 200 [i => 'pick'],
97 [p => 'place'], 201 [p => 'place'],
98 [e => 'erase'], 202 [e => 'erase'],
99 [s => 'select'], 203 [s => 'select'],
100 [l => 'eval'], 204 [l => 'eval'],
101 [x => 'connectexit'], 205 [t => 'connect'],
102 [f => 'followexit'] 206 [f => 'followexit']
103 ) 207 )
104 { 208 {
105 my $tool = $_->[1]; 209 my $tool = $_->[1];
106 $men->{accel_group}->connect ($Gtk2::Gdk::Keysyms{$_->[0]}, [], 'visible', 210 $men->{accel_group}->connect ($Gtk2::Gdk::Keysyms{$_->[0]}, [], 'visible',
107 sub { $::MAINWIN->set_edit_tool ($tool) }); 211 sub { $::MAINWIN->set_edit_tool ($tool) });
108 } 212 }
109 213
214 $men->{accel_group}->connect ($Gtk2::Gdk::Keysyms{'r'}, ['control-mask'], 'visible',
215 sub { $self->redo });
216
110 $self->add_accel_group ($men->{accel_group}); 217 $self->add_accel_group ($men->{accel_group});
111 218
112 return $men->{widget}; 219 return $men->{widget};
113} 220}
114 221
222#################################################################
223###### EDIT TOOL STUFF ##########################################
224#################################################################
225
226sub set_edit_tool {
227 my ($self, $tool) = @_;
228
229 $self->{etool} = $tool;
230
231 if ($self->ea->special_arrow) {
232 $self->{map}{window}->set_cursor (Gtk2::Gdk::Cursor->new ($self->ea->special_arrow));
233 } else {
234 # FIXME: Get the original cursor and insert it here
235 $self->{map}{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
236 }
237}
238
115sub ea { 239sub ea {
116 my ($self) = @_; 240 my ($self) = @_;
117 $self->{ea_alt} || $::MAINWIN->{sel_editaction}; 241 $self->{ea_alt} || $self->{etool};
118} 242}
119 243
120sub INIT_INSTANCE { 244sub start_drawmode {
121 my ($self) = @_; 245 my ($self, $map) = @_;
122 246
123 $self->set_title ('gce - map editor'); 247 $self->{draw_mode} and return;
124 $self->add (my $vb = Gtk2::VBox->new);
125 248
126 $self->signal_connect (delete_event => sub { $self->delete; 1 }); 249 # XXX: is this okay? my ($x, $y) = $map->coord ($event->x, $event->y);
127
128 $vb->pack_start (my $menu = $self->build_menu, 0, 1, 0);
129
130 $vb->pack_start (my $map = $self->{map} = Crossfire::MapWidget->new, 1, 1, 0);
131
132 $self->signal_connect (focus_in_event => sub {
133 my $ea = $::MAINWIN->{sel_editaction};
134 if ($ea->special_arrow) {
135 $self->{map}->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($ea->special_arrow));
136 } else {
137 $self->{map}->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
138 }
139 delete $self->{ea_alt};
140 });
141
142 $map->signal_connect (key_press_event => sub {
143 my ($map, $event) = @_;
144
145 my $kv = $event->keyval;
146
147 my ($x, $y) = $map->coord ($map->get_pointer); 250 my ($x, $y) = $map->coord ($map->get_pointer);
148 for ([Control_L => sub { $self->{ea_alt} = $::MAINWIN->{edit_collection}{erase} }],
149 [Alt_L => sub { $self->{ea_alt} = $::MAINWIN->{edit_collection}{pick} }],
150 [c => sub { $::MAINWIN->{edit_collection}{select}->copy }],
151 [v => sub { $::MAINWIN->{edit_collection}{select}->paste ($x, $y) }],
152 [n => sub { $::MAINWIN->{edit_collection}{select}->invoke }],
153 )
154 {
155 if ($kv == $Gtk2::Gdk::Keysyms{$_->[0]}) {
156 $_->[1]->();
157 }
158 }
159 251
160 if ($self->ea->special_arrow) {
161 $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($self->ea->special_arrow));
162 }
163 1;
164 });
165 $map->signal_connect (key_release_event => sub {
166 my ($map, $event) = @_;
167
168 if ($event->keyval == $Gtk2::Gdk::Keysyms{Control_L}
169 or $event->keyval == $Gtk2::Gdk::Keysyms{Alt_L})
170 {
171 delete $self->{ea_alt};
172 }
173
174 if ($self->ea->special_arrow) {
175 $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($self->ea->special_arrow));
176 } else {
177 # XXX: Get the original cursor and insert it here
178 $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
179 }
180 1;
181 });
182 $map->signal_connect (button_press_event => sub {
183 my ($map, $event) = @_;
184
185 my ($x, $y) = $map->coord ($event->x, $event->y);
186 my $as = $map->get ($x, $y);
187
188 if ((not $self->{draw_mode}) and $event->button != 2) {
189
190 my $ea = $self->ea; 252 my $ea = $self->ea;
191 253
192 $ea->begin ($map, $x, $y, $self) 254 $ea->begin ($map, $x, $y, $self);
255
256 $ea->edit ($map, $x, $y, $self)
193 if $x >= 0 and $y >= 0 and $x < $map->{map}{width} and $y < $map->{map}{height}; 257 if $x >= 0 and $y >= 0 and $x < $map->{map}{width} and $y < $map->{map}{height};
194 258
195 $self->{draw_mode} = [$x, $y]; 259 $self->{draw_mode} = [$x, $y];
260}
196 261
197 $ea->want_cursor 262sub stop_drawmode {
198 or $map->disable_tooltip; 263 my ($self, $map) = @_;
199 264
200 return 1;
201 }
202 0
203 });
204
205 $map->signal_connect_after (motion_notify_event => sub {
206 my ($map, $event) = @_;
207
208 $self->{draw_mode} 265 $self->{draw_mode} or return;
209 or return;
210 266
211 my $ea = $self->ea;
212
213 my ($X, $Y) = @{$self->{draw_mode}}[0,1];
214 my ($x, $y) = $map->coord ($map->get_pointer); 267 my ($x, $y) = $map->coord ($map->get_pointer);
215 268
216 while ($x != $X || $y != $Y) {
217
218 $X++ if $X < $x;
219 $X-- if $X > $x;
220 $Y++ if $Y < $y;
221 $Y-- if $Y > $y;
222
223 $ea->edit ($map, $X, $Y, $self)
224 if $X >= 0 and $Y >= 0 and $X < $map->{map}{width} and $Y < $map->{map}{height};
225 }
226
227 @{$self->{draw_mode}}[0,1] = ($X, $Y);
228
229 1
230 });
231
232 $map->signal_connect (button_release_event => sub {
233 my ($map, $event) = @_;
234
235 if ($self->{draw_mode}) {
236 my ($x, $y) = $map->coord ($map->get_pointer);
237
238 my $ea = $self->ea; 269 my $ea = $self->ea;
239 $ea->end ($map, $x, $y, $self); 270 $ea->end ($map, $x, $y, $self);
240 271
241 delete $self->{draw_mode}; 272 delete $self->{draw_mode};
242
243 $ea->want_cursor
244 or $map->enable_tooltip;
245
246 return 1;
247 }
248
249 0
250 });
251} 273}
252 274
253# FIXME: Fix the automatic update of the attribute editor! and also the stack view! 275#################################################################
254sub undo { 276###### UTILITY FUNCTIONS ########################################
255 my ($self) = @_; 277#################################################################
256
257 my $map = $self->{map}; # the Crossfire::MapWidget
258
259 $map->{undo_stack_pos}
260 or return;
261
262 $map->change_swap ($map->{undo_stack}[--$map->{undo_stack_pos}]);
263}
264
265sub redo {
266 my ($self) = @_;
267
268 my $map = $self->{map}; # the Crossfire::MapWidget
269
270 $map->{undo_stack}
271 and $map->{undo_stack_pos} < @{$map->{undo_stack}}
272 or return;
273
274 $map->change_swap ($map->{undo_stack}[$map->{undo_stack_pos}++]);
275}
276
277sub delete {
278 my ($self) = @_;
279
280 # check and modla dialog if "dirty"
281
282 if ($self->{mapkey}) {
283 # XXX: This should be in a delete event handler in the MainWindow.pm... but it doesnt work
284 delete $::MAINWIN->{loaded_maps}->{$self->{mapkey}};
285 }
286
287 $self->destroy;
288}
289
290sub open_map {
291 my ($self, $path, $key) = @_;
292
293 $self->{mapkey} = $key;
294
295 if (ref $path) {
296 $self->{map}->set_map ($path);
297
298 } else {
299 $self->{path} = $path;
300# print "OPENMAP $path\n";
301 $self->{map}->set_map (my $m = new_from_file Crossfire::Map $path);
302 require Data::Dumper;
303# print "FOO:" .Data::Dumper::Dumper ($m) . "\n";
304 }
305}
306
307sub save_map {
308 my ($self) = @_;
309
310 if ($self->{path}) {
311 $self->{map}{map}->write_file ($self->{path});
312 quick_msg ($self, "saved to $self->{path}");
313 } else {
314 $self->save_map_as;
315 }
316}
317
318sub save_map_as {
319 my ($self) = @_;
320
321 my $fc = $::MAINWIN->new_filechooser ('gce - save map', 1, $self->{path});
322
323 if ('ok' eq $fc->run) {
324
325 $::MAINWIN->{fc_last_folder} = $fc->get_current_folder;
326 $::MAINWIN->{fc_last_folders}->{$self->{fc_last_folder}}++;
327
328 $self->{map}{map}->write_file ($self->{path} = $fc->get_filename);
329 quick_msg ($self, "saved to $self->{path}");
330 }
331
332 $fc->destroy;
333}
334
335sub _add_prop_entry {
336 my ($self, $table, $idx, $key, $desc, $type, $changecb) = @_;
337
338 my $edwid;
339
340 if ($type eq 'string') {
341 $table->attach_defaults (my $lbl = Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
342 $edwid = Gtk2::Entry->new;
343 $edwid->set_text ($self->{map}{map}{info}{$key});
344 $edwid->signal_connect (changed => sub {
345 $self->{map}{map}{info}{$key} = $_[0]->get_text;
346 if ($changecb) {
347 $changecb->($_[0]->get_text);
348 }
349 });
350 $table->attach_defaults ($edwid, 1, 2, $idx, $idx + 1);
351
352 } elsif ($type eq 'button') {
353 $table->attach_defaults (my $b = Gtk2::Button->new_with_label ($desc), 0, 2, $idx, $idx + 1);
354 $b->signal_connect (clicked => ($changecb || sub {}));
355
356 } elsif ($type eq 'label') {
357 $table->attach_defaults (my $lbl = Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
358 $edwid = Gtk2::Label->new ($self->{map}{map}{info}{$key});
359 $table->attach_defaults ($edwid, 1, 2, $idx, $idx + 1);
360
361 } elsif ($type eq 'check') {
362 $table->attach_defaults (my $lbl1 = Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
363 $table->attach_defaults (my $lbl = Gtk2::CheckButton->new, 1, 2, $idx, $idx + 1);
364 $lbl->set_active ($self->{map}{map}{info}{$key});
365 $lbl->signal_connect (toggled => sub {
366 my ($lbl) = @_;
367 $self->{map}{map}{info}{$key} = $lbl->get_active * 1;
368 ($changecb || sub {})->($lbl->get_active);
369 });
370
371 } elsif ($type eq 'sep') {
372 $table->attach_defaults (my $lbl1 = Gtk2::HSeparator->new, 0, 2, $idx, $idx + 1);
373 } else {
374 $edwid = Gtk2::Label->new ("FOO");
375 }
376}
377
378sub open_resize_map {
379 my ($self) = @_;
380
381 my $w = Gtk2::Window->new ('toplevel');
382 $w->set_default_size (250, 150);
383 $w->add (my $sw = Gtk2::ScrolledWindow->new);
384 $sw->add_with_viewport (my $v = Gtk2::VBox->new);
385 $sw->set_policy ('automatic', 'automatic');
386 $v->pack_start (my $t = Gtk2::Table->new (2, 10), 0, 0, 0);
387
388 my $i = 0;
389 for (
390 [qw/width Width string/],
391 [qw/height Height string/],
392 [qw/save Save button/,
393 sub {
394 $self->{map}{map}->resize ($self->{map}{map}{info}{width}, $self->{map}{map}{info}{height});
395 $self->{map}->invalidate_all;
396 $w->destroy;
397 }
398 ],
399 )
400 {
401 $self->_add_prop_entry ($t, $i++, @$_);
402 }
403
404 $w->show_all;
405}
406 278
407sub follow { 279sub follow {
408 my ($self, $dir) = @_; 280 my ($self, $dir) = @_;
409 281
410 my %dir_to_path = ( 282 my %dir_to_path = (
421 293
422 $map = map2abs ($map, $self); 294 $map = map2abs ($map, $self);
423 $::MAINWIN->open_map_editor ($map); 295 $::MAINWIN->open_map_editor ($map);
424} 296}
425 297
298# FIXME: Fix the automatic update of the attribute editor! and also the stack view!
299sub undo {
300 my ($self) = @_;
301
302 my $map = $self->{map}; # the Crossfire::MapWidget
303
304 $map->{undo_stack_pos}
305 or return;
306
307 $map->change_swap ($map->{undo_stack}[--$map->{undo_stack_pos}]);
308}
309
310sub get_stack_refs {
311 my ($self, $map, $x, $y) = @_;
312
313 my $cstack = $map->get ($x, $y);
314
315 return [] unless @$cstack;
316
317 my @refs;
318
319 for my $arch (@$cstack) {
320 my ($ox, $oy) = ($x, $y);
321 if ($arch->{_virtual}) {
322 $ox = $arch->{virtual_x};
323 $oy = $arch->{virtual_y};
324 $arch = $arch->{_virtual};
325 $cstack = $map->get ($ox, $oy);
326 # XXX: This heavily blows up if $arch isn't on $cstack now.. and it actually really does :(
327 }
328
329 push @refs,
330 GCE::ArchRef->new (
331 arch => $arch,
332 source => 'map',
333 cb => sub {
334 $map->change_begin ('attredit');
335 $map->change_stack ($ox, $oy, $cstack);
336
337 if (my $changeset = $map->change_end) {
338 splice @{ $map->{undo_stack} ||= [] },
339 $map->{undo_stack_pos}++, 1e6,
340 $changeset;
341 }
342 }
343 );
344 }
345
346 return @refs;
347}
348
349sub redo {
350 my ($self) = @_;
351
352 my $map = $self->{map}; # the Crossfire::MapWidget
353
354 $map->{undo_stack}
355 and $map->{undo_stack_pos} < @{$map->{undo_stack}}
356 or return;
357
358 $map->change_swap ($map->{undo_stack}[$map->{undo_stack_pos}++]);
359}
360
361sub load_meta_info {
362 my ($mapfile) = @_;
363 if (-e "$mapfile.meta") {
364 open my $metafh, "<", "$mapfile.meta"
365 or warn "Couldn't open meta file $mapfile.meta: $!";
366 my $metadata = do { local $/; <$metafh> };
367 return Crossfire::from_json ($metadata);
368 }
369}
370
371sub save_meta_info {
372 my ($mapfile, $metainfo) = @_;
373 open my $metafh, ">", "$mapfile.meta"
374 or warn "Couldn't write meta file $mapfile.meta: $!";
375 print $metafh Crossfire::to_json ($metainfo);
376}
377
378sub open_map {
379 my ($self, $path, $key) = @_;
380
381 $self->{mapkey} = $key;
382
383 if (ref $path) {
384 $self->{map}->set_map ($path);
385 delete $self->{meta_info};
386 $self->set_title ('<ram>');
387
388 } else {
389 my $ok = 0;
390 if (-e $path && -f $path) {
391 $ok = 1;
392 } else {
393 unless ($path =~ m/\.map$/) { # yuck
394 my $p = $path . '.map';
395 if ($ok = -e $p && -f $p) {
396 $path = $p;
397 }
398 }
399 }
400 unless ($ok) {
401 die "Couldn't open '$path' or find '$path.map': No such file or it is not a file.\n";
402 }
403 $self->{path} = $path;
404 $self->{map}->set_map (my $m = new_from_file Crossfire::Map $path);
405 $self->{meta_info} = load_meta_info ($path);
406 $self->set_title ("gce - map editor - $self->{path}");
407 }
408 $self->close_windows;
409}
410
411sub save_map {
412 my ($self) = @_;
413
414 if ($self->{path}) {
415 $self->{map}{map}->write_file ($self->{path});
416 if ($self->{meta_info}) {
417 save_meta_info ($self->{path}, $self->{meta_info});
418 }
419 quick_msg ($self, "saved to $self->{path}");
420 $self->set_title ("gce - map editor - $self->{path}");
421 } else {
422 $self->save_map_as;
423 }
424}
425
426sub save_map_as {
427 my ($self) = @_;
428
429 my $fc = $::MAINWIN->new_filechooser ('gce - save map', 1, $self->{path});
430
431 if ('ok' eq $fc->run) {
432
433 $::MAINWIN->{fc_last_folder} = $fc->get_current_folder;
434 $::MAINWIN->{fc_last_folders}->{$self->{fc_last_folder}}++;
435
436 $self->{map}{map}->write_file ($self->{path} = $fc->get_filename);
437 if ($self->{meta_info}) {
438 save_meta_info ($self->{path}, $self->{meta_info});
439 }
440 quick_msg ($self, "saved to $self->{path}");
441 $self->set_title ("gce - map editor - $self->{path}");
442 }
443
444 $fc->destroy;
445}
446
447#################################################################
448###### DIALOGOUES ###############################################
449#################################################################
450
451sub open_resize_map {
452 my ($self) = @_;
453
454 return if $self->{meta_info_win};
455
456 my $w = $self->{meta_info_win} = GCE::HashDialogue->new ();
457
458 $w->init (
459 dialog_default_size => [500, 200, 220, 20],
460 layout_name => 'resize_win',
461 title => 'resize map',
462 ref_hash => $self->{map}{map}{info},
463 dialog => [
464 [width => 'Width' => 'string'],
465 [height => 'Height' => 'string'],
466 ],
467 close_on_save => 1,
468 save_cb => sub {
469 my ($info) = @_;
470 $self->{map}{map}->resize ($info->{width}, $info->{height});
471 $self->{map}->invalidate_all;
472 }
473 );
474
475 $w->signal_connect (destroy => sub { delete $self->{meta_info_win} });
476
477 $w->show_all;
478}
479
480sub open_attach_edit {
481 my ($self) = @_;
482
483 my $w = GCE::AttachEditor->new;
484 $w->set_attachment (
485 $self->{map}{map}{info}{attach},
486 sub {
487 if (@{$_[0]}) {
488 $self->{map}{map}{info}{attach} = $_[0]
489 } else {
490 delete $self->{map}{map}{info}{attach};
491 }
492 }
493 );
494 $self->{attach_editor} = $w;
495 $w->signal_connect (destroy => sub { delete $self->{attach_editor} });
496 $w->show_all;
497}
498
499sub upload_map_incl {
500 my ($self) = @_;
501
502 my $meta = dclone $self->{meta_info};
503
504 my $w = $self->{meta_info_win} = GCE::HashDialogue->new ();
505
506 $w->init (
507 dialog_default_size => [500, 300, 220, 20],
508 layout_name => 'map_upload_incl',
509 title => 'gce - map inclusion upload',
510 ref_hash => $meta,
511 text_entry => { key => 'changes', label => 'Changes (required for inclusion):' },
512 dialog => [
513 [gameserver => 'Game server' => 'label'],
514 [testserver => 'Test server' => 'label'],
515 [undef => x => 'sep' ],
516 [cf_login => 'Server login name' => 'string'],
517 [cf_password=> 'Password' => 'password'],
518 [path => 'Map path' => 'string'],
519 ],
520 close_on_save => 1,
521 save_cb => sub {
522 my ($meta) = @_;
523 warn "UPLOAD[".Crossfire::to_json ($meta)."]\n";
524 }
525 );
526
527 $w->signal_connect (destroy => sub { delete $self->{meta_info_win} });
528
529 $w->show_all;
530}
531
532sub upload_map_test {
533 my ($self) = @_;
534
535 my $meta = dclone $self->{meta_info};
536
537 my $w = $self->{meta_info_win} = GCE::HashDialogue->new ();
538
539 $w->init (
540 dialog_default_size => [500, 300, 220, 20],
541 layout_name => 'map_upload_test',
542 title => 'gce - map test upload',
543 ref_hash => $meta,
544 dialog => [
545 [gameserver => 'Game server' => 'string'],
546 [testserver => 'Test server' => 'string'],
547 [undef => x => 'sep' ],
548 [cf_login => 'Server login name' => 'string'],
549 [cf_password=> 'Password' => 'password'],
550 [path => 'Map path' => 'string'],
551 ],
552 save_cb => sub {
553 my ($meta) = @_;
554 warn "UPLOAD[".Crossfire::to_json ($meta)."]\n";
555 }
556 );
557
558 $w->signal_connect (destroy => sub { delete $self->{meta_info_win} });
559
560 $w->show_all;
561
562
563}
564
565sub open_meta_info {
566 my ($self) = @_;
567
568 return if $self->{meta_info_win};
569
570 my $w = $self->{meta_info_win} = GCE::HashDialogue->new ();
571
572 $w->init (
573 dialog_default_size => [500, 300, 220, 20],
574 layout_name => 'meta_info_win',
575 title => 'meta info',
576 ref_hash => $self->{meta_info},
577 dialog => [
578 [path => 'Map path' => 'string'],
579 [cf_login => 'Login name' => 'string'],
580 [revision => 'CVS Revision' => 'label'],
581 [cvs_root => 'CVS Root' => 'label'],
582 [lib_root => 'LIB Root' => 'label'],
583 [testserver => 'Test server' => 'label'],
584 [gameserver => 'Game server' => 'label'],
585 ],
586 );
587
588 $w->signal_connect (destroy => sub { delete $self->{meta_info_win} });
589
590 $w->show_all;
591}
592
593sub open_map_info {
594 my ($self) = @_;
595 return if $self->{map_info};
596
597 my $w = $self->{map_info} = Gtk2::Window->new ('toplevel');
598 $w->set_title ("gcrossedit - map info");
599
600 $w->add (my $vb = Gtk2::VBox->new);
601 $vb->add (my $sw = Gtk2::ScrolledWindow->new);
602 $sw->set_policy ('automatic', 'automatic');
603 $sw->add (my $txt = Gtk2::TextView->new);
604 $vb->pack_start (my $hb = Gtk2::HBox->new (1, 1), 0, 1, 0);
605 $hb->pack_start (my $svbtn = Gtk2::Button->new ("save"), 1, 1, 0);
606 $hb->pack_start (my $logbtn = Gtk2::Button->new ("add log"), 1, 1, 0);
607 $hb->pack_start (my $closebtn = Gtk2::Button->new ("close"), 1, 1, 0);
608
609 my $buf = $txt->get_buffer ();
610 $buf->set_text ($self->{map}{map}{info}{msg});
611
612 $svbtn->signal_connect (clicked => sub {
613 my $buf = $txt->get_buffer ();
614 my $txt = $buf->get_text ($buf->get_start_iter, $buf->get_end_iter, 0);
615 $self->{map}{map}{info}{msg} = $txt;
616 });
617
618 $logbtn->signal_connect (clicked => sub {
619 my $buf = $txt->get_buffer ();
620 $buf->insert ($buf->get_start_iter, "- " . strftime ("%F %T %Z", localtime (time)) . " by " . ($main::CFG->{username} || $ENV{USER}) . ":\n");
621 $txt->set_buffer ($buf);
622 });
623
624 $closebtn->signal_connect (clicked => sub {
625 $w->destroy;
626 });
627
628 ::set_pos_and_size ($w, $main::CFG->{map_info}, 400, 400, 220, 20);
629 $w->signal_connect (destroy => sub {
630 delete $self->{map_info};
631 });
632 $w->show_all;
633}
634
426sub open_map_prop { 635sub open_map_prop {
427 my ($self) = @_; 636 my ($self) = @_;
428 637
638 return if $self->{map_properties};
429 639
430 my $w = Gtk2::Window->new ('toplevel'); 640 my $w = $self->{map_properties} = GCE::HashDialogue->new ();
431 $w->set_default_size (500, 500);
432 $w->add (my $sw = Gtk2::ScrolledWindow->new);
433 $sw->add_with_viewport (my $v = Gtk2::VBox->new);
434 $sw->set_policy ('automatic', 'automatic');
435 $v->pack_start (my $t = Gtk2::Table->new (2, 10), 0, 0, 0);
436 641
437 my $i = 0; 642 $w->init (
438 for ( 643 dialog_default_size => [500, 500, 220, 20],
644 layout_name => 'map_prop_win',
645 title => 'map properties',
646 ref_hash => $self->{map}{map}{info},
647 close_on_save => 1,
648 dialog => [
439 [qw/name Name string/], 649 [qw/name Name string/],
440 [qw/region Region string/], 650 [qw/region Region string/],
441 [qw/enter_x Enter-x string/], 651 [qw/enter_x Enter-x string/],
442 [qw/enter_y Enter-y string/], 652 [qw/enter_y Enter-y string/],
443 [qw/reset_timeout Reset-timeout string/], 653 [qw/reset_timeout Reset-timeout string/],
444 [qw/swap_time Swap-timeout string/], 654 [qw/swap_time Swap-timeout string/],
445 [qw/x x sep/], 655 [undef, qw/x sep/],
446 [qw/difficulty Difficulty string/], 656 [qw/difficulty Difficulty string/],
447 [qw/windspeed Windspeed string/], 657 [qw/windspeed Windspeed string/],
448 [qw/pressure Pressure string/], 658 [qw/pressure Pressure string/],
449 [qw/humid Humid string/], 659 [qw/humid Humid string/],
450 [qw/temp Temp string/], 660 [qw/temp Temp string/],
451 [qw/darkness Darkness string/], 661 [qw/darkness Darkness string/],
452 [qw/sky Sky string/], 662 [qw/sky Sky string/],
453 [qw/winddir Winddir string/], 663 [qw/winddir Winddir string/],
454 [qw/x x sep/], 664 [undef, qw/x sep/],
455 [qw/width Width label/], # sub { $self->{map}{map}->resize ($_[0], $self->{map}{map}{height}) }], 665 [qw/width Width label/], # sub { $self->{map}{map}->resize ($_[0], $self->{map}{map}{height}) }],
456 [qw/height Height label/], # sub { $self->{map}{map}->resize ($self->{map}{map}{width}, $_[0]) }], 666 [qw/height Height label/], # sub { $self->{map}{map}->resize ($self->{map}{map}{width}, $_[0]) }],
457 [qw/x x sep/], 667 [undef, qw/x sep/],
458 [qw/msg Text text/], 668 # [qw/msg Text text/],
459 [qw/maplore Maplore text/], 669# [qw/maplore Maplore text/],
460 [qw/outdoor Outdoor check/], 670 [qw/outdoor Outdoor check/],
461 [qw/unique Unique check/], 671 [qw/unique Unique check/],
462 [qw/fixed_resettime Fixed-resettime check/], 672 [qw/fixed_resettime Fixed-resettime check/],
463 [qw/x x sep/], 673 [per_player => 'Per player' => 'check'],
674 [per_party => 'Per party' => 'check'],
675 [no_reset => 'No reset' => 'check'],
676 [music => 'Map Music' => 'string'],
677 [undef, qw/x sep/],
464 [qw/tile_path_1 Northpath string/], 678 [qw/tile_path_1 Northpath string/],
465 [qw/tile_path_2 Eastpath string/], 679 [qw/tile_path_2 Eastpath string/],
466 [qw/tile_path_3 Southpath string/], 680 [qw/tile_path_3 Southpath string/],
467 [qw/tile_path_4 Westpath string/], 681 [qw/tile_path_4 Westpath string/],
468 [qw/tile_path_5 Toppath string/], 682 [qw/tile_path_5 Toppath string/],
469 [qw/tile_path_6 Bottompath string/], 683 [qw/tile_path_6 Bottompath string/],
684 [undef, qw/x sep/],
685 [undef, 'For shop description look in the manual',
686 'button', sub { $::MAINWIN->show_help_window }],
687 [qw/shopmin Shopmin string/],
688 [qw/shopmax Shopmax string/],
689 [qw/shoprace Shoprace string/],
690 [qw/shopgreed Shopgreed string/],
691 [qw/shopitems Shopitems string/],
470 ) 692 ]
471 { 693 );
472 $self->_add_prop_entry ($t, $i++, @$_);
473 }
474 694
695 $w->signal_connect (destroy => sub { delete $self->{map_properties} });
475 $w->show_all; 696 $w->show_all;
697}
698
699#################################################################
700###### MAP EDITOR INIT ##########################################
701#################################################################
702
703sub INIT_INSTANCE {
704 my ($self) = @_;
705
706 $self->set_title ('gce - map editor');
707 $self->add (my $vb = Gtk2::VBox->new);
708
709 $vb->pack_start (my $menu = $self->build_menu, 0, 1, 0);
710
711 $vb->pack_start (my $map = $self->{map} = Crossfire::MapWidget->new, 1, 1, 0);
712
713 $map->signal_connect_after (key_press_event => sub {
714 my ($map, $event) = @_;
715
716 my $kv = $event->keyval;
717
718 my $ret = 0;
719
720 my ($x, $y) = $map->coord ($map->get_pointer);
721 for ([Control_L => sub { $self->{ea_alt} = $::MAINWIN->{edit_collection}{erase} }],
722 [Alt_L => sub { $self->{ea_alt} = $::MAINWIN->{edit_collection}{pick} }],
723 [c => sub { $::MAINWIN->{edit_collection}{select}->copy }],
724 [v => sub { $::MAINWIN->{edit_collection}{select}->paste ($map, $x, $y) }],
725 [n => sub { $::MAINWIN->{edit_collection}{select}->invoke }],
726 )
727 {
728 my $ed = $_;
729
730 if ($kv == $Gtk2::Gdk::Keysyms{$ed->[0]}) {
731 my $was_in_draw = defined $self->{draw_mode};
732
733 $self->stop_drawmode ($map)
734 if $was_in_draw && grep { $ed->[0] eq $_ } qw/Control_L Alt_L/;
735
736 $ed->[1]->();
737 $ret = 1;
738
739 $self->start_drawmode ($map)
740 if $was_in_draw && grep { $ed->[0] eq $_ } qw/Control_L Alt_L/;
741 }
742 }
743
744 if ($self->ea->special_arrow) {
745 $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($self->ea->special_arrow));
746 } else {
747 # FIXME: Get the original cursor and insert it here
748 $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
749 }
750
751 $ret
752 });
753
754 $map->signal_connect_after (key_release_event => sub {
755 my ($map, $event) = @_;
756
757 my $ret = 0;
758
759 if ($event->keyval == $Gtk2::Gdk::Keysyms{Control_L}
760 or $event->keyval == $Gtk2::Gdk::Keysyms{Alt_L})
761 {
762 my $was_in_draw = defined $self->{draw_mode};
763
764 $self->stop_drawmode ($map)
765 if $was_in_draw;
766
767 delete $self->{ea_alt};
768 $ret = 1;
769
770 $self->start_drawmode ($map)
771 if $was_in_draw;
772 }
773
774 if ($self->ea->special_arrow) {
775 $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($self->ea->special_arrow));
776 } else {
777 # FIXME: Get the original cursor and insert it here
778 $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
779 }
780
781 $ret
782 });
783 $map->signal_connect_after (button_press_event => sub {
784 my ($map, $event) = @_;
785
786 if ((not $self->{draw_mode}) and $event->button == 1) {
787 my $ea = $self->ea;
788
789 $self->start_drawmode ($map);
790
791 $ea->want_cursor
792 or $map->disable_tooltip;
793
794 return 1;
795 } elsif ($event->button == 3) {
796 $self->do_context_menu ($map, $event);
797 return 1;
798 }
799
800 0
801 });
802
803 $map->signal_connect_after (motion_notify_event => sub {
804 my ($map, $event) = @_;
805
806 $self->{draw_mode}
807 or return;
808
809 my $ea = $self->ea;
810
811 my ($X, $Y) = @{$self->{draw_mode}}[0,1];
812 my ($x, $y) = $map->coord ($map->get_pointer);
813
814 while ($x != $X || $y != $Y) {
815
816 $X++ if $X < $x;
817 $X-- if $X > $x;
818 $Y++ if $Y < $y;
819 $Y-- if $Y > $y;
820
821 unless ($ea->only_on_click) {
822 $ea->edit ($map, $X, $Y, $self)
823 if $X >= 0 and $Y >= 0 and $X < $map->{map}{width} and $Y < $map->{map}{height};
824 }
825 }
826
827 @{$self->{draw_mode}}[0,1] = ($X, $Y);
828
829 1
830 });
831
832 $map->signal_connect_after (button_release_event => sub {
833 my ($map, $event) = @_;
834
835 if ($self->{draw_mode} and $event->button == 1) {
836 my $ea = $self->ea;
837
838 $self->stop_drawmode ($map);
839
840 $ea->want_cursor
841 or $map->enable_tooltip;
842
843 return 1;
844 }
845
846 0
847 });
848
849 ::set_pos_and_size ($self, $main::CFG->{map_window}, 500, 500, 200, 0);
476} 850}
477 851
478=head1 AUTHOR 852=head1 AUTHOR
479 853
480 Marc Lehmann <schmorp@schmorp.de> 854 Marc Lehmann <schmorp@schmorp.de>
482 856
483 Robin Redeker <elmex@ta-sa.org> 857 Robin Redeker <elmex@ta-sa.org>
484 http://www.ta-sa.org/ 858 http://www.ta-sa.org/
485 859
486=cut 860=cut
4871;
488 861
8621
863

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines