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.40 by elmex, Tue Aug 15 17:01:34 2006 UTC vs.
Revision 1.59 by elmex, Mon Aug 20 07:56:58 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#################################################################
24 56
25sub do_context_menu { 57sub do_context_menu {
26 my ($self, $map, $event) = @_; 58 my ($self, $map, $event) = @_;
27 59
28 my ($x, $y) = $map->coord ($event->x, $event->y); 60 my ($x, $y) = $map->coord ($event->x, $event->y);
39 $menu->append ($item); 71 $menu->append ($item);
40 $item->show; 72 $item->show;
41 $item->signal_connect (activate => $cm->[1]); 73 $item->signal_connect (activate => $cm->[1]);
42 } 74 }
43 75
44 my $inv_item = Gtk2::MenuItem->new ("Add inventory"); 76 $menu->append (my $sep = new Gtk2::SeparatorMenuItem);
45 $inv_item->set_submenu (my $smenu = new Gtk2::Menu); 77 $sep->show;
46 $menu->append ($inv_item);
47 78
48 for my $sr (reverse $self->get_stack_refs ($map, $x, $y)) { 79 for my $sr (reverse $self->get_stack_refs ($map, $x, $y)) {
49 my $item = Gtk2::MenuItem->new ($sr->longname); 80 my $item = Gtk2::MenuItem->new ($sr->longname);
50 $smenu->append ($item); 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
51 $item->show; 94 $item->show;
52 $item->signal_connect (activate => sub {
53 $sr->add_inv ($::MAINWIN->get_pick);
54 });
55 } 95 }
56 96
57 $inv_item->show;
58 $menu->popup (undef, undef, undef, undef, $event->button, $event->time); 97 $menu->popup (undef, undef, undef, undef, $event->button, $event->time);
59} 98}
60 99
61sub build_menu { 100sub build_menu {
62 my ($self) = @_; 101 my ($self) = @_;
70 accelerator => '<ctrl>S' 109 accelerator => '<ctrl>S'
71 }, 110 },
72 "Save As" => { 111 "Save As" => {
73 callback => sub { $self->save_map_as }, 112 callback => sub { $self->save_map_as },
74 }, 113 },
114 "Map _Info" => {
115 callback => sub { $self->open_map_info },
116 accelerator => "<ctrl>I",
117 },
75 "_Map Properties" => { 118 "Map _Properties" => {
76 callback => sub { $self->open_map_prop }, 119 callback => sub { $self->open_map_prop },
77 accelerator => "<ctrl>P" 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 ]
78 }, 139 },
79 "_Map Resize" => { 140 "_Map Resize" => {
80 callback => sub { $self->open_resize_map }, 141 callback => sub { $self->open_resize_map },
81 }, 142 },
82 "Close" => { 143 "Close" => {
156 $self->add_accel_group ($men->{accel_group}); 217 $self->add_accel_group ($men->{accel_group});
157 218
158 return $men->{widget}; 219 return $men->{widget};
159} 220}
160 221
222#################################################################
223###### EDIT TOOL STUFF ##########################################
224#################################################################
225
161sub set_edit_tool { 226sub set_edit_tool {
162 my ($self, $tool) = @_; 227 my ($self, $tool) = @_;
163 228
164 $self->{etool} = $tool; 229 $self->{etool} = $tool;
165 230
173 238
174sub ea { 239sub ea {
175 my ($self) = @_; 240 my ($self) = @_;
176 $self->{ea_alt} || $self->{etool}; 241 $self->{ea_alt} || $self->{etool};
177} 242}
243
244sub start_drawmode {
245 my ($self, $map) = @_;
246
247 $self->{draw_mode} and return;
248
249 # XXX: is this okay? my ($x, $y) = $map->coord ($event->x, $event->y);
250 my ($x, $y) = $map->coord ($map->get_pointer);
251
252 my $ea = $self->ea;
253
254 $ea->begin ($map, $x, $y, $self);
255
256 $ea->edit ($map, $x, $y, $self)
257 if $x >= 0 and $y >= 0 and $x < $map->{map}{width} and $y < $map->{map}{height};
258
259 $self->{draw_mode} = [$x, $y];
260}
261
262sub stop_drawmode {
263 my ($self, $map) = @_;
264
265 $self->{draw_mode} or return;
266
267 my ($x, $y) = $map->coord ($map->get_pointer);
268
269 my $ea = $self->ea;
270 $ea->end ($map, $x, $y, $self);
271
272 delete $self->{draw_mode};
273}
274
275#################################################################
276###### UTILITY FUNCTIONS ########################################
277#################################################################
278
279sub follow {
280 my ($self, $dir) = @_;
281
282 my %dir_to_path = (
283 u => 'tile_path_1',
284 d => 'tile_path_3',
285 r => 'tile_path_2',
286 l => 'tile_path_4',
287 );
288
289 defined $dir_to_path{$dir}
290 or return;
291 my $map = $self->{map}{map}{info}{$dir_to_path{$dir}}
292 or return;
293
294 $map = map2abs ($map, $self);
295 $::MAINWIN->open_map_editor ($map);
296}
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 $::MAINWIN->add_recent($path);
408 }
409 $self->close_windows;
410}
411
412sub save_map {
413 my ($self) = @_;
414
415 if ($self->{path}) {
416 $self->{map}{map}->write_file ($self->{path});
417 if ($self->{meta_info}) {
418 save_meta_info ($self->{path}, $self->{meta_info});
419 }
420 quick_msg ($self, "saved to $self->{path}");
421 $self->set_title ("gce - map editor - $self->{path}");
422 $::MAINWIN->add_recent($self->{path});
423 } else {
424 $self->save_map_as;
425 }
426}
427
428sub save_map_as {
429 my ($self) = @_;
430
431 my $fc = $::MAINWIN->new_filechooser ('gce - save map', 1, $self->{path});
432
433 if ('ok' eq $fc->run) {
434
435 $::MAINWIN->{fc_last_folder} = $fc->get_current_folder;
436 $::MAINWIN->{fc_last_folders}->{$self->{fc_last_folder}}++;
437
438 if($fc->get_filename) {
439 $self->{path} = $fc->get_filename;
440 $self->save_map;
441 }
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_button_label => 'resize',
469 save_cb => sub {
470 my ($info) = @_;
471 $self->{map}{map}->resize ($info->{width}, $info->{height});
472 $self->{map}->set_map ($self->{map}{map});
473 }
474 );
475
476 $w->signal_connect (destroy => sub { delete $self->{meta_info_win} });
477
478 $w->show_all;
479}
480
481sub open_attach_edit {
482 my ($self) = @_;
483
484 my $w = GCE::AttachEditor->new;
485 $w->set_attachment (
486 $self->{map}{map}{info}{attach},
487 sub {
488 if (@{$_[0]}) {
489 $self->{map}{map}{info}{attach} = $_[0]
490 } else {
491 delete $self->{map}{map}{info}{attach};
492 }
493 }
494 );
495 $self->{attach_editor} = $w;
496 $w->signal_connect (destroy => sub { delete $self->{attach_editor} });
497 $w->show_all;
498}
499
500sub upload_map_incl {
501 my ($self) = @_;
502
503 my $meta = dclone $self->{meta_info};
504
505 my $w = $self->{meta_info_win} = GCE::HashDialogue->new ();
506
507 $w->init (
508 dialog_default_size => [500, 300, 220, 20],
509 layout_name => 'map_upload_incl',
510 title => 'gce - map inclusion upload',
511 ref_hash => $meta,
512 text_entry => { key => 'changes', label => 'Changes (required for inclusion):' },
513 dialog => [
514 [gameserver => 'Game server' => 'label'],
515 [testserver => 'Test server' => 'label'],
516 [undef => x => 'sep' ],
517 [cf_login => 'Server login name' => 'string'],
518 [cf_password=> 'Password' => 'password'],
519 [path => 'Map path' => 'string'],
520 ],
521 close_on_save => 1,
522 save_cb => sub {
523 my ($meta) = @_;
524 warn "UPLOAD[".Crossfire::to_json ($meta)."]\n";
525 }
526 );
527
528 $w->signal_connect (destroy => sub { delete $self->{meta_info_win} });
529
530 $w->show_all;
531}
532
533sub upload_map_test {
534 my ($self) = @_;
535
536 my $meta = dclone $self->{meta_info};
537
538 my $w = $self->{meta_info_win} = GCE::HashDialogue->new ();
539
540 $w->init (
541 dialog_default_size => [500, 300, 220, 20],
542 layout_name => 'map_upload_test',
543 title => 'gce - map test upload',
544 ref_hash => $meta,
545 dialog => [
546 [gameserver => 'Game server' => 'string'],
547 [testserver => 'Test server' => 'string'],
548 [undef => x => 'sep' ],
549 [cf_login => 'Server login name' => 'string'],
550 [cf_password=> 'Password' => 'password'],
551 [path => 'Map path' => 'string'],
552 ],
553 save_cb => sub {
554 my ($meta) = @_;
555 warn "UPLOAD[".Crossfire::to_json ($meta)."]\n";
556 }
557 );
558
559 $w->signal_connect (destroy => sub { delete $self->{meta_info_win} });
560
561 $w->show_all;
562
563
564}
565
566sub open_meta_info {
567 my ($self) = @_;
568
569 return if $self->{meta_info_win};
570
571 my $w = $self->{meta_info_win} = GCE::HashDialogue->new ();
572
573 $w->init (
574 dialog_default_size => [500, 300, 220, 20],
575 layout_name => 'meta_info_win',
576 title => 'meta info',
577 ref_hash => $self->{meta_info},
578 dialog => [
579 [path => 'Map path' => 'string'],
580 [cf_login => 'Login name' => 'string'],
581 [revision => 'CVS Revision' => 'label'],
582 [cvs_root => 'CVS Root' => 'label'],
583 [lib_root => 'LIB Root' => 'label'],
584 [testserver => 'Test server' => 'label'],
585 [gameserver => 'Game server' => 'label'],
586 ],
587 );
588
589 $w->signal_connect (destroy => sub { delete $self->{meta_info_win} });
590
591 $w->show_all;
592}
593
594sub open_map_info {
595 my ($self) = @_;
596 return if $self->{map_info};
597
598 my $w = $self->{map_info} = Gtk2::Window->new ('toplevel');
599 $w->set_title ("gcrossedit - map info");
600
601 $w->add (my $vb = Gtk2::VBox->new);
602 $vb->add (my $sw = Gtk2::ScrolledWindow->new);
603 $sw->set_policy ('automatic', 'automatic');
604 $sw->add (my $txt = Gtk2::TextView->new);
605 $vb->pack_start (my $hb = Gtk2::HBox->new (1, 1), 0, 1, 0);
606 $hb->pack_start (my $svbtn = Gtk2::Button->new ("save"), 1, 1, 0);
607 $hb->pack_start (my $logbtn = Gtk2::Button->new ("add log"), 1, 1, 0);
608 $hb->pack_start (my $closebtn = Gtk2::Button->new ("close"), 1, 1, 0);
609
610 my $buf = $txt->get_buffer ();
611 $buf->set_text ($self->{map}{map}{info}{msg});
612
613 $svbtn->signal_connect (clicked => sub {
614 my $buf = $txt->get_buffer ();
615 my $txt = $buf->get_text ($buf->get_start_iter, $buf->get_end_iter, 0);
616 $self->{map}{map}{info}{msg} = $txt;
617 });
618
619 $logbtn->signal_connect (clicked => sub {
620 my $buf = $txt->get_buffer ();
621 $buf->insert ($buf->get_start_iter, "- " . strftime ("%F %T %Z", localtime (time)) . " by " . ($main::CFG->{username} || $ENV{USER}) . ":\n");
622 $txt->set_buffer ($buf);
623 });
624
625 $closebtn->signal_connect (clicked => sub {
626 $w->destroy;
627 });
628
629 ::set_pos_and_size ($w, $main::CFG->{map_info}, 400, 400, 220, 20);
630 $w->signal_connect (destroy => sub {
631 delete $self->{map_info};
632 });
633 $w->show_all;
634}
635
636sub open_map_prop {
637 my ($self) = @_;
638
639 return if $self->{map_properties};
640
641 my $w = $self->{map_properties} = GCE::HashDialogue->new ();
642
643 $w->init (
644 dialog_default_size => [500, 500, 220, 20],
645 layout_name => 'map_prop_win',
646 title => 'map properties',
647 ref_hash => $self->{map}{map}{info},
648 close_on_save => 1,
649 dialog => [
650 [qw/name Name string/],
651 [qw/region Region string/],
652 [qw/enter_x Enter-x string/],
653 [qw/enter_y Enter-y string/],
654 [qw/reset_timeout Reset-timeout string/],
655 [qw/swap_time Swap-timeout string/],
656 [undef, qw/x sep/],
657 [qw/difficulty Difficulty string/],
658 [qw/windspeed Windspeed string/],
659 [qw/pressure Pressure string/],
660 [qw/humid Humid string/],
661 [qw/temp Temp string/],
662 [qw/darkness Darkness string/],
663 [qw/sky Sky string/],
664 [qw/winddir Winddir string/],
665 [undef, qw/x sep/],
666 [qw/width Width label/], # sub { $self->{map}{map}->resize ($_[0], $self->{map}{map}{height}) }],
667 [qw/height Height label/], # sub { $self->{map}{map}->resize ($self->{map}{map}{width}, $_[0]) }],
668 [undef, qw/x sep/],
669 # [qw/msg Text text/],
670# [qw/maplore Maplore text/],
671 [qw/outdoor Outdoor check/],
672 [qw/unique Unique check/],
673 [qw/fixed_resettime Fixed-resettime check/],
674 [per_player => 'Per player' => 'check'],
675 [per_party => 'Per party' => 'check'],
676 [no_reset => 'No reset' => 'check'],
677 [music => 'Map Music' => 'string'],
678 [undef, qw/x sep/],
679 [qw/tile_path_1 Northpath string/],
680 [qw/tile_path_2 Eastpath string/],
681 [qw/tile_path_3 Southpath string/],
682 [qw/tile_path_4 Westpath string/],
683 [qw/tile_path_5 Toppath string/],
684 [qw/tile_path_6 Bottompath string/],
685 [undef, qw/x sep/],
686 [undef, 'For shop description look in the manual',
687 'button', sub { $::MAINWIN->show_help_window }],
688 [qw/shopmin Shopmin string/],
689 [qw/shopmax Shopmax string/],
690 [qw/shoprace Shoprace string/],
691 [qw/shopgreed Shopgreed string/],
692 [qw/shopitems Shopitems string/],
693 ]
694 );
695
696 $w->signal_connect (destroy => sub { delete $self->{map_properties} });
697 $w->show_all;
698}
699
700#################################################################
701###### MAP EDITOR INIT ##########################################
702#################################################################
178 703
179sub INIT_INSTANCE { 704sub INIT_INSTANCE {
180 my ($self) = @_; 705 my ($self) = @_;
181 706
182 $self->set_title ('gce - map editor'); 707 $self->set_title ('gce - map editor');
319 return 1; 844 return 1;
320 } 845 }
321 846
322 0 847 0
323 }); 848 });
324}
325 849
326sub start_drawmode { 850 ::set_pos_and_size ($self, $main::CFG->{map_window}, 500, 500, 200, 0);
327 my ($self, $map) = @_;
328
329 $self->{draw_mode} and return;
330
331 # XXX: is this okay? my ($x, $y) = $map->coord ($event->x, $event->y);
332 my ($x, $y) = $map->coord ($map->get_pointer);
333
334 my $ea = $self->ea;
335
336 $ea->begin ($map, $x, $y, $self);
337
338 $ea->edit ($map, $x, $y, $self)
339 if $x >= 0 and $y >= 0 and $x < $map->{map}{width} and $y < $map->{map}{height};
340
341 $self->{draw_mode} = [$x, $y];
342}
343
344sub stop_drawmode {
345 my ($self, $map) = @_;
346
347 $self->{draw_mode} or return;
348
349 my ($x, $y) = $map->coord ($map->get_pointer);
350
351 my $ea = $self->ea;
352 $ea->end ($map, $x, $y, $self);
353
354 delete $self->{draw_mode};
355}
356
357# FIXME: Fix the automatic update of the attribute editor! and also the stack view!
358sub undo {
359 my ($self) = @_;
360
361 my $map = $self->{map}; # the Crossfire::MapWidget
362
363 $map->{undo_stack_pos}
364 or return;
365
366 $map->change_swap ($map->{undo_stack}[--$map->{undo_stack_pos}]);
367}
368
369sub get_stack_refs {
370 my ($self, $map, $x, $y) = @_;
371
372 my $cstack = $map->get ($x, $y);
373
374 return [] unless @$cstack;
375
376 my @refs;
377
378 for my $arch (@$cstack) {
379 my ($ox, $oy) = ($x, $y);
380 if ($arch->{_virtual}) {
381 $ox = $arch->{virtual_x};
382 $oy = $arch->{virtual_y};
383 $arch = $arch->{_virtual};
384 $cstack = $map->get ($ox, $oy);
385 # XXX: This heavily blows up if $arch isn't on $cstack now.. and it actually really does :(
386 }
387
388 push @refs,
389 GCE::ArchRef->new (
390 arch => $arch,
391 cb => sub {
392 $map->change_begin ('attredit');
393 $map->change_stack ($ox, $oy, $cstack);
394
395 if (my $changeset = $map->change_end) {
396 splice @{ $map->{undo_stack} ||= [] },
397 $map->{undo_stack_pos}++, 1e6,
398 $changeset;
399 }
400 }
401 );
402 }
403
404 return @refs;
405}
406
407sub redo {
408 my ($self) = @_;
409
410 my $map = $self->{map}; # the Crossfire::MapWidget
411
412 $map->{undo_stack}
413 and $map->{undo_stack_pos} < @{$map->{undo_stack}}
414 or return;
415
416 $map->change_swap ($map->{undo_stack}[$map->{undo_stack_pos}++]);
417}
418
419sub open_map {
420 my ($self, $path, $key) = @_;
421
422 $self->{mapkey} = $key;
423
424 if (ref $path) {
425 $self->{map}->set_map ($path);
426 $self->set_title ('<ram>');
427
428 } else {
429 $self->{path} = $path;
430# print "OPENMAP $path\n";
431 $self->{map}->set_map (my $m = new_from_file Crossfire::Map $path);
432 $self->set_title ("gce - map editor - $self->{path}");
433 require Data::Dumper;
434# print "FOO:" .Data::Dumper::Dumper ($m) . "\n";
435 }
436}
437
438sub save_map {
439 my ($self) = @_;
440
441 if ($self->{path}) {
442 $self->{map}{map}->write_file ($self->{path});
443 quick_msg ($self, "saved to $self->{path}");
444 $self->set_title ("gce - map editor - $self->{path}");
445 } else {
446 $self->save_map_as;
447 }
448}
449
450sub save_map_as {
451 my ($self) = @_;
452
453 my $fc = $::MAINWIN->new_filechooser ('gce - save map', 1, $self->{path});
454
455 if ('ok' eq $fc->run) {
456
457 $::MAINWIN->{fc_last_folder} = $fc->get_current_folder;
458 $::MAINWIN->{fc_last_folders}->{$self->{fc_last_folder}}++;
459
460 $self->{map}{map}->write_file ($self->{path} = $fc->get_filename);
461 quick_msg ($self, "saved to $self->{path}");
462 $self->set_title ("gce - map editor - $self->{path}");
463 }
464
465 $fc->destroy;
466}
467
468sub _add_prop_entry {
469 my ($self, $table, $idx, $key, $desc, $type, $changecb) = @_;
470
471 my $edwid;
472
473 if ($type eq 'string') {
474 $table->attach_defaults (my $lbl = Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
475 $edwid = Gtk2::Entry->new;
476 $edwid->set_text ($self->{map}{map}{info}{$key});
477 $edwid->signal_connect (changed => sub {
478 $self->{map}{map}{info}{$key} = $_[0]->get_text;
479 if ($changecb) {
480 $changecb->($_[0]->get_text);
481 }
482 });
483 $table->attach_defaults ($edwid, 1, 2, $idx, $idx + 1);
484
485 } elsif ($type eq 'button') {
486 $table->attach_defaults (my $b = Gtk2::Button->new_with_label ($desc), 0, 2, $idx, $idx + 1);
487 $b->signal_connect (clicked => ($changecb || sub {}));
488
489 } elsif ($type eq 'label') {
490 $table->attach_defaults (my $lbl = Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
491 $edwid = Gtk2::Label->new ($self->{map}{map}{info}{$key});
492 $table->attach_defaults ($edwid, 1, 2, $idx, $idx + 1);
493
494 } elsif ($type eq 'check') {
495 $table->attach_defaults (my $lbl1 = Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
496 $table->attach_defaults (my $lbl = Gtk2::CheckButton->new, 1, 2, $idx, $idx + 1);
497 $lbl->set_active ($self->{map}{map}{info}{$key});
498 $lbl->signal_connect (toggled => sub {
499 my ($lbl) = @_;
500 $self->{map}{map}{info}{$key} = $lbl->get_active * 1;
501 ($changecb || sub {})->($lbl->get_active);
502 });
503
504 } elsif ($type eq 'sep') {
505 $table->attach_defaults (my $lbl1 = Gtk2::HSeparator->new, 0, 2, $idx, $idx + 1);
506 } else {
507 $edwid = Gtk2::Label->new ("FOO");
508 }
509}
510
511sub open_resize_map {
512 my ($self) = @_;
513
514 my $w = Gtk2::Window->new ('toplevel');
515 $w->set_default_size (250, 150);
516 $w->add (my $sw = Gtk2::ScrolledWindow->new);
517 $sw->add_with_viewport (my $v = Gtk2::VBox->new);
518 $sw->set_policy ('automatic', 'automatic');
519 $v->pack_start (my $t = Gtk2::Table->new (2, 10), 0, 0, 0);
520
521 my $i = 0;
522 for (
523 [qw/width Width string/],
524 [qw/height Height string/],
525 [qw/save Save button/,
526 sub {
527 $self->{map}{map}->resize ($self->{map}{map}{info}{width}, $self->{map}{map}{info}{height});
528 $self->{map}->invalidate_all;
529 $w->destroy;
530 }
531 ],
532 )
533 {
534 $self->_add_prop_entry ($t, $i++, @$_);
535 }
536
537 $w->show_all;
538}
539
540sub follow {
541 my ($self, $dir) = @_;
542
543 my %dir_to_path = (
544 u => 'tile_path_1',
545 d => 'tile_path_3',
546 r => 'tile_path_2',
547 l => 'tile_path_4',
548 );
549
550 defined $dir_to_path{$dir}
551 or return;
552 my $map = $self->{map}{map}{info}{$dir_to_path{$dir}}
553 or return;
554
555 $map = map2abs ($map, $self);
556 $::MAINWIN->open_map_editor ($map);
557}
558
559sub open_map_prop {
560 my ($self) = @_;
561
562
563 my $w = Gtk2::Window->new ('toplevel');
564 $w->set_default_size (500, 500);
565 $w->add (my $sw = Gtk2::ScrolledWindow->new);
566 $sw->add_with_viewport (my $v = Gtk2::VBox->new);
567 $sw->set_policy ('automatic', 'automatic');
568 $v->pack_start (my $t = Gtk2::Table->new (2, 10), 0, 0, 0);
569
570 my $i = 0;
571 for (
572 [qw/name Name string/],
573 [qw/region Region string/],
574 [qw/enter_x Enter-x string/],
575 [qw/enter_y Enter-y string/],
576 [qw/reset_timeout Reset-timeout string/],
577 [qw/swap_time Swap-timeout string/],
578 [qw/x x sep/],
579 [qw/difficulty Difficulty string/],
580 [qw/windspeed Windspeed string/],
581 [qw/pressure Pressure string/],
582 [qw/humid Humid string/],
583 [qw/temp Temp string/],
584 [qw/darkness Darkness string/],
585 [qw/sky Sky string/],
586 [qw/winddir Winddir string/],
587 [qw/x x sep/],
588 [qw/width Width label/], # sub { $self->{map}{map}->resize ($_[0], $self->{map}{map}{height}) }],
589 [qw/height Height label/], # sub { $self->{map}{map}->resize ($self->{map}{map}{width}, $_[0]) }],
590 [qw/x x sep/],
591 [qw/msg Text text/],
592 [qw/maplore Maplore text/],
593 [qw/outdoor Outdoor check/],
594 [qw/unique Unique check/],
595 [qw/fixed_resettime Fixed-resettime check/],
596 [qw/x x sep/],
597 [qw/tile_path_1 Northpath string/],
598 [qw/tile_path_2 Eastpath string/],
599 [qw/tile_path_3 Southpath string/],
600 [qw/tile_path_4 Westpath string/],
601 [qw/tile_path_5 Toppath string/],
602 [qw/tile_path_6 Bottompath string/],
603 [qw/x x sep/],
604 ['x', 'For shop description look in the manual', 'button', sub { $::MAINWIN->show_help_window }],
605 [qw/shopmin Shopmin string/],
606 [qw/shopmax Shopmax string/],
607 [qw/shoprace Shoprace string/],
608 [qw/shopgreed Shopgreed string/],
609 [qw/shopitems Shopitems string/],
610 [qw/x x sep/],
611 ['safe_map', 'Safe map (CF+)', 'check'],
612 )
613 {
614 $self->_add_prop_entry ($t, $i++, @$_);
615 }
616
617 $w->show_all;
618} 851}
619 852
620=head1 AUTHOR 853=head1 AUTHOR
621 854
622 Marc Lehmann <schmorp@schmorp.de> 855 Marc Lehmann <schmorp@schmorp.de>
624 857
625 Robin Redeker <elmex@ta-sa.org> 858 Robin Redeker <elmex@ta-sa.org>
626 http://www.ta-sa.org/ 859 http://www.ta-sa.org/
627 860
628=cut 861=cut
6291;
630 862
8631
864

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines