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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines