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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines