ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MapEditor.pm
Revision: 1.62
Committed: Sat Aug 25 15:21:03 2007 UTC (16 years, 9 months ago) by elmex
Branch: MAIN
Changes since 1.61: +32 -12 lines
Log Message:
now the overlay connections also have a yellow border

File Contents

# User Rev Content
1 elmex 1.1 package GCE::MapEditor;
2    
3     =head1 NAME
4    
5     GCE::MapEditor - the map editing widget
6    
7     =cut
8    
9     use Gtk2;
10     use Gtk2::Gdk::Keysyms;
11     use Gtk2::SimpleMenu;
12    
13     use Crossfire;
14 root 1.3 use Crossfire::Map;
15 elmex 1.1 use Crossfire::MapWidget;
16    
17     use GCE::AttrEdit;
18 elmex 1.62 use GCE::Util qw/gtk2_get_color/;
19 elmex 1.44 use GCE::HashDialog;
20 elmex 1.1
21 elmex 1.49 use POSIX qw/strftime/;
22    
23 elmex 1.4 use Glib::Object::Subclass
24 elmex 1.6 Gtk2::Window;
25 elmex 1.1
26 elmex 1.45 use Storable qw/dclone/;
27    
28 elmex 1.1 use strict;
29    
30 elmex 1.44 #################################################################
31     ###### WINDOW MANAGEMENT ########################################
32     #################################################################
33    
34     sub 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 elmex 1.49
41     $main::CFG->{map_info} = main::get_pos_and_size ($self->{map_info})
42     if $self->{map_info};
43 elmex 1.44 }
44    
45     sub 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    
57 elmex 1.35 sub 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 elmex 1.36 $::MAINWIN->{edit_collection}{followexit}->edit ($map, $x, $y, $self)
67 elmex 1.38 },
68 elmex 1.35 ]
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 elmex 1.38
76 elmex 1.47 $menu->append (my $sep = new Gtk2::SeparatorMenuItem);
77     $sep->show;
78 elmex 1.38
79     for my $sr (reverse $self->get_stack_refs ($map, $x, $y)) {
80     my $item = Gtk2::MenuItem->new ($sr->longname);
81 elmex 1.47 $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 elmex 1.38 $item->show;
95     }
96    
97 elmex 1.35 $menu->popup (undef, undef, undef, undef, $event->button, $event->time);
98     }
99    
100 root 1.9 sub build_menu {
101     my ($self) = @_;
102    
103     my $menu_tree = [
104     _File => {
105     item_type => '<Branch>',
106     children => [
107     "_Save" => {
108 root 1.10 callback => sub { $self->save_map },
109 root 1.9 accelerator => '<ctrl>S'
110     },
111 root 1.10 "Save As" => {
112     callback => sub { $self->save_map_as },
113     },
114 elmex 1.49 "Map _Info" => {
115     callback => sub { $self->open_map_info },
116     accelerator => "<ctrl>I",
117     },
118 elmex 1.42 "Map _Properties" => {
119 elmex 1.32 callback => sub { $self->open_map_prop },
120     accelerator => "<ctrl>P"
121     },
122 elmex 1.42 "Map _Attachments" => {
123     callback => sub { $self->open_attach_edit },
124     accelerator => "<ctrl>A"
125     },
126 elmex 1.44 "Map Meta _Info" => {
127     callback => sub { $self->open_meta_info },
128     },
129 elmex 1.45 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 elmex 1.32 "_Map Resize" => {
141     callback => sub { $self->open_resize_map },
142     },
143 root 1.13 "Close" => {
144 elmex 1.31 callback => sub { $self->destroy },
145 elmex 1.60 accelerator => "<ctrl>W",
146 root 1.13 },
147 root 1.10 ]
148 root 1.9 },
149     _Edit => {
150     item_type => '<Branch>',
151     children => [
152     "_Undo" => {
153     callback => sub { $self->undo },
154 elmex 1.17 accelerator => "<ctrl>Z"
155 root 1.9 },
156     "_Redo" => {
157     callback => sub { $self->redo },
158 elmex 1.17 accelerator => "<ctrl>Y"
159     },
160 root 1.9 ]
161     },
162 elmex 1.27 _Go => {
163     item_type => '<Branch>',
164     children => [
165     "_Up" => {
166     callback => sub { $self->follow ('u') },
167 elmex 1.33 accelerator => "<ctrl>Up"
168 elmex 1.27 },
169     "_Down" => {
170     callback => sub { $self->follow ('d') },
171 elmex 1.33 accelerator => "<ctrl>Down"
172 elmex 1.27 },
173     "_Right" => {
174     callback => sub { $self->follow ('r') },
175 elmex 1.33 accelerator => "<ctrl>Right"
176 elmex 1.27 },
177     "_Left" => {
178     callback => sub { $self->follow ('l') },
179 elmex 1.33 accelerator => "<ctrl>Left"
180 elmex 1.27 },
181     ]
182     },
183 elmex 1.29 _Help => {
184     item_type => '<Branch>',
185     children => [
186 elmex 1.30 _Manual => {
187 elmex 1.29 callback => sub { $::MAINWIN->show_help_window },
188     accelerator => "<ctrl>H"
189     },
190     ]
191     },
192 root 1.9 ];
193    
194     my $men =
195     Gtk2::SimpleMenu->new (
196     menu_tree => $menu_tree,
197     default_callback => \&default_cb,
198     );
199    
200 elmex 1.23 for (
201     [i => 'pick'],
202     [p => 'place'],
203 elmex 1.24 [e => 'erase'],
204     [s => 'select'],
205     [l => 'eval'],
206 elmex 1.37 [t => 'connect'],
207 elmex 1.24 [f => 'followexit']
208 elmex 1.23 )
209     {
210 elmex 1.24 my $tool = $_->[1];
211 elmex 1.23 $men->{accel_group}->connect ($Gtk2::Gdk::Keysyms{$_->[0]}, [], 'visible',
212 elmex 1.24 sub { $::MAINWIN->set_edit_tool ($tool) });
213 elmex 1.23 }
214    
215 elmex 1.39 $men->{accel_group}->connect ($Gtk2::Gdk::Keysyms{'r'}, ['control-mask'], 'visible',
216     sub { $self->redo });
217    
218 root 1.9 $self->add_accel_group ($men->{accel_group});
219    
220     return $men->{widget};
221     }
222    
223 elmex 1.44 #################################################################
224     ###### EDIT TOOL STUFF ##########################################
225     #################################################################
226    
227 elmex 1.31 sub set_edit_tool {
228     my ($self, $tool) = @_;
229    
230     $self->{etool} = $tool;
231    
232     if ($self->ea->special_arrow) {
233     $self->{map}{window}->set_cursor (Gtk2::Gdk::Cursor->new ($self->ea->special_arrow));
234 elmex 1.34 } else {
235     # FIXME: Get the original cursor and insert it here
236     $self->{map}{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
237 elmex 1.31 }
238     }
239    
240 elmex 1.19 sub ea {
241     my ($self) = @_;
242 elmex 1.31 $self->{ea_alt} || $self->{etool};
243 elmex 1.19 }
244    
245 elmex 1.34 sub start_drawmode {
246     my ($self, $map) = @_;
247    
248     $self->{draw_mode} and return;
249    
250     # XXX: is this okay? my ($x, $y) = $map->coord ($event->x, $event->y);
251     my ($x, $y) = $map->coord ($map->get_pointer);
252    
253     my $ea = $self->ea;
254    
255 elmex 1.36 $ea->begin ($map, $x, $y, $self);
256    
257     $ea->edit ($map, $x, $y, $self)
258 elmex 1.34 if $x >= 0 and $y >= 0 and $x < $map->{map}{width} and $y < $map->{map}{height};
259    
260     $self->{draw_mode} = [$x, $y];
261     }
262    
263     sub stop_drawmode {
264     my ($self, $map) = @_;
265    
266     $self->{draw_mode} or return;
267    
268     my ($x, $y) = $map->coord ($map->get_pointer);
269    
270     my $ea = $self->ea;
271     $ea->end ($map, $x, $y, $self);
272    
273     delete $self->{draw_mode};
274     }
275    
276 elmex 1.44 #################################################################
277     ###### UTILITY FUNCTIONS ########################################
278     #################################################################
279    
280     sub follow {
281     my ($self, $dir) = @_;
282    
283     my %dir_to_path = (
284     u => 'tile_path_1',
285     d => 'tile_path_3',
286     r => 'tile_path_2',
287     l => 'tile_path_4',
288     );
289    
290     defined $dir_to_path{$dir}
291     or return;
292     my $map = $self->{map}{map}{info}{$dir_to_path{$dir}}
293     or return;
294    
295     $map = map2abs ($map, $self);
296     $::MAINWIN->open_map_editor ($map);
297     }
298    
299 elmex 1.16 # FIXME: Fix the automatic update of the attribute editor! and also the stack view!
300 root 1.9 sub undo {
301     my ($self) = @_;
302 elmex 1.8
303 root 1.9 my $map = $self->{map}; # the Crossfire::MapWidget
304 elmex 1.1
305 root 1.9 $map->{undo_stack_pos}
306     or return;
307 elmex 1.1
308 root 1.9 $map->change_swap ($map->{undo_stack}[--$map->{undo_stack_pos}]);
309     }
310 elmex 1.1
311 elmex 1.38 sub get_stack_refs {
312     my ($self, $map, $x, $y) = @_;
313    
314     my $cstack = $map->get ($x, $y);
315    
316     return [] unless @$cstack;
317    
318     my @refs;
319    
320     for my $arch (@$cstack) {
321     push @refs,
322     GCE::ArchRef->new (
323     arch => $arch,
324 elmex 1.56 source => 'map',
325 elmex 1.38 cb => sub {
326     $map->change_begin ('attredit');
327 elmex 1.61 $map->change_stack ($x, $y, $cstack);
328 elmex 1.38
329     if (my $changeset = $map->change_end) {
330     splice @{ $map->{undo_stack} ||= [] },
331     $map->{undo_stack_pos}++, 1e6,
332     $changeset;
333     }
334     }
335     );
336     }
337    
338     return @refs;
339     }
340    
341 root 1.9 sub redo {
342     my ($self) = @_;
343 elmex 1.7
344 root 1.9 my $map = $self->{map}; # the Crossfire::MapWidget
345 elmex 1.7
346 elmex 1.14 $map->{undo_stack}
347     and $map->{undo_stack_pos} < @{$map->{undo_stack}}
348     or return;
349 elmex 1.1
350 root 1.9 $map->change_swap ($map->{undo_stack}[$map->{undo_stack_pos}++]);
351 root 1.3 }
352    
353 elmex 1.43 sub load_meta_info {
354     my ($mapfile) = @_;
355     if (-e "$mapfile.meta") {
356     open my $metafh, "<", "$mapfile.meta"
357     or warn "Couldn't open meta file $mapfile.meta: $!";
358     my $metadata = do { local $/; <$metafh> };
359     return Crossfire::from_json ($metadata);
360     }
361     }
362    
363     sub save_meta_info {
364     my ($mapfile, $metainfo) = @_;
365     open my $metafh, ">", "$mapfile.meta"
366     or warn "Couldn't write meta file $mapfile.meta: $!";
367     print $metafh Crossfire::to_json ($metainfo);
368     }
369    
370 root 1.3 sub open_map {
371 elmex 1.26 my ($self, $path, $key) = @_;
372    
373     $self->{mapkey} = $key;
374 root 1.3
375 elmex 1.18 if (ref $path) {
376     $self->{map}->set_map ($path);
377 elmex 1.43 delete $self->{meta_info};
378 elmex 1.60 $self->set_window_title;
379 elmex 1.18
380     } else {
381 elmex 1.51 my $ok = 0;
382 elmex 1.52 if (-e $path && -f $path) {
383     $ok = 1;
384     } else {
385 elmex 1.51 unless ($path =~ m/\.map$/) { # yuck
386     my $p = $path . '.map';
387     if ($ok = -e $p && -f $p) {
388     $path = $p;
389     }
390     }
391     }
392     unless ($ok) {
393 elmex 1.52 die "Couldn't open '$path' or find '$path.map': No such file or it is not a file.\n";
394 elmex 1.48 }
395 elmex 1.18 $self->{path} = $path;
396     $self->{map}->set_map (my $m = new_from_file Crossfire::Map $path);
397 elmex 1.43 $self->{meta_info} = load_meta_info ($path);
398 elmex 1.60 $self->set_window_title ($self->{path});
399 elmex 1.59 $::MAINWIN->add_recent($path);
400 elmex 1.18 }
401 elmex 1.61 $self->update_overlays;
402 elmex 1.44 $self->close_windows;
403 root 1.3 }
404    
405 root 1.13 sub save_map {
406     my ($self) = @_;
407    
408 elmex 1.19 if ($self->{path}) {
409     $self->{map}{map}->write_file ($self->{path});
410 elmex 1.43 if ($self->{meta_info}) {
411     save_meta_info ($self->{path}, $self->{meta_info});
412     }
413 elmex 1.22 quick_msg ($self, "saved to $self->{path}");
414 elmex 1.60 $self->set_window_title ($self->{path});
415 elmex 1.59 $::MAINWIN->add_recent($self->{path});
416 elmex 1.22 } else {
417     $self->save_map_as;
418 elmex 1.19 }
419 root 1.13 }
420    
421 elmex 1.60 sub set_window_title {
422     my ($self, $title) = @_;
423    
424     $title = 'Unsaved'
425     unless $title;
426    
427     $self->set_title(File::Basename::basename($title).' - gcrossedit');
428     }
429    
430 root 1.13 sub save_map_as {
431     my ($self) = @_;
432 elmex 1.19
433 elmex 1.22 my $fc = $::MAINWIN->new_filechooser ('gce - save map', 1, $self->{path});
434 elmex 1.19
435     if ('ok' eq $fc->run) {
436    
437     $::MAINWIN->{fc_last_folder} = $fc->get_current_folder;
438     $::MAINWIN->{fc_last_folders}->{$self->{fc_last_folder}}++;
439    
440 elmex 1.59 if($fc->get_filename) {
441     $self->{path} = $fc->get_filename;
442     $self->save_map;
443 elmex 1.43 }
444 elmex 1.19 }
445    
446     $fc->destroy;
447 root 1.13 }
448    
449 elmex 1.61 sub update_overlays {
450     my ($self, $sx, $sy, $stack) = @_;
451     my $conns = {};
452    
453     if (not $stack) {
454     my ($w, $h) = ($self->{map}{width}, $self->{map}{height});
455    
456     for (my $x = 0; $x <= $w; $x++) {
457     for (my $y = 0; $y <= $h; $y++) {
458     my $stk = $self->{map}->get ($x, $y); # XXX: This is goddamnit slow!
459     for (@$stk) {
460     if (defined $_->{connected}) {
461     $conns->{$x}->{$y}->{$_->{connected}} = 1;
462     }
463     }
464     }
465     }
466    
467     # delete prev. overlays
468     for (keys %{$self->{_conn_overlays}}) {
469     $self->{map}->overlay ($_);
470     }
471     } else {
472     # del old overlay for this place
473     my $ovl = "connection_$sx\_$sy";
474     $self->{map}->overlay ($ovl) if delete $self->{_conn_overlays}->{$ovl};
475     for (@$stack) {
476     if (defined $_->{connected}) {
477     $conns->{$sx}->{$sy}->{$_->{connected}} = 1;
478     }
479     }
480     }
481    
482     # put new overlays there
483     for my $x (keys %$conns) {
484     for my $y (keys %{$conns->{$x}}) {
485     my $ovlname = "connection_$x\_$y";
486     my $conns_ovl = join (', ', keys %{$conns->{$x}->{$y}});
487     $self->{_conn_overlays}->{$ovlname} = 1;
488 elmex 1.62 my ($a, $t, $ac)
489     = Gtk2::Pango->parse_markup (
490     "<span size=\"xx-small\">$conns_ovl</span>", ''
491     );
492     my $pl = $self->{map}->create_pango_layout ('');
493     $pl->set_attributes ($a);
494     $pl->set_text ($t);
495     my ($ink_rect, $logical_rect) = $pl->get_pixel_extents;
496 elmex 1.61
497     $self->{map}->overlay (
498     $ovlname, $x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE,
499     sub {
500     my ($mapwin, $x, $y) = @_;
501 elmex 1.62 if (!$self->{_conn_upd_ovl_gc_fg}) {
502     my $gc
503     = $self->{_conn_upd_ovl_gc_fg}
504     = Gtk2::Gdk::GC->new ($mapwin->{window});
505     my $cm = $mapwin->{window}->get_colormap;
506     $gc->set_foreground (gtk2_get_color ($mapwin, "yellow"));
507     $gc->set_background (gtk2_get_color ($mapwin, "black"));
508     }
509     $mapwin->{window}->draw_rectangle (
510     $mapwin->style->black_gc,
511     1,
512     $x, $y, $logical_rect->{width} + 2, $logical_rect->{height} + 2,
513     );
514     $mapwin->{window}->draw_rectangle (
515     $self->{_conn_upd_ovl_gc_fg},
516     0,
517     $x, $y, $logical_rect->{width} + 2, $logical_rect->{height} + 2,
518     );
519 elmex 1.61 $mapwin->{window}->draw_layout_with_colors (
520 elmex 1.62 $mapwin->style->black_gc, $x + 1, $y + 1, $pl,
521 elmex 1.61 $self->{connection_overlay_foreground},
522     $self->{connection_overlay_background},
523     )
524     }
525     );
526     }
527     }
528     }
529    
530 elmex 1.44 #################################################################
531     ###### DIALOGOUES ###############################################
532     #################################################################
533 elmex 1.17
534     sub open_resize_map {
535     my ($self) = @_;
536    
537 elmex 1.44 return if $self->{meta_info_win};
538 elmex 1.17
539 elmex 1.44 my $w = $self->{meta_info_win} = GCE::HashDialogue->new ();
540 elmex 1.17
541 elmex 1.44 $w->init (
542     dialog_default_size => [500, 200, 220, 20],
543     layout_name => 'resize_win',
544     title => 'resize map',
545     ref_hash => $self->{map}{map}{info},
546     dialog => [
547     [width => 'Width' => 'string'],
548     [height => 'Height' => 'string'],
549     ],
550 elmex 1.58 close_on_save => 1,
551     save_button_label => 'resize',
552 elmex 1.44 save_cb => sub {
553     my ($info) = @_;
554     $self->{map}{map}->resize ($info->{width}, $info->{height});
555 elmex 1.58 $self->{map}->set_map ($self->{map}{map});
556 elmex 1.61 $self->update_overlays;
557 elmex 1.44 }
558 elmex 1.27 );
559    
560 elmex 1.44 $w->signal_connect (destroy => sub { delete $self->{meta_info_win} });
561 elmex 1.27
562 elmex 1.44 $w->show_all;
563 elmex 1.27 }
564    
565 elmex 1.42 sub open_attach_edit {
566     my ($self) = @_;
567    
568     my $w = GCE::AttachEditor->new;
569     $w->set_attachment (
570     $self->{map}{map}{info}{attach},
571     sub {
572     if (@{$_[0]}) {
573     $self->{map}{map}{info}{attach} = $_[0]
574     } else {
575     delete $self->{map}{map}{info}{attach};
576     }
577     }
578     );
579     $self->{attach_editor} = $w;
580     $w->signal_connect (destroy => sub { delete $self->{attach_editor} });
581     $w->show_all;
582     }
583    
584 elmex 1.45 sub upload_map_incl {
585     my ($self) = @_;
586    
587     my $meta = dclone $self->{meta_info};
588    
589     my $w = $self->{meta_info_win} = GCE::HashDialogue->new ();
590    
591     $w->init (
592     dialog_default_size => [500, 300, 220, 20],
593     layout_name => 'map_upload_incl',
594     title => 'gce - map inclusion upload',
595     ref_hash => $meta,
596     text_entry => { key => 'changes', label => 'Changes (required for inclusion):' },
597     dialog => [
598     [gameserver => 'Game server' => 'label'],
599     [testserver => 'Test server' => 'label'],
600     [undef => x => 'sep' ],
601     [cf_login => 'Server login name' => 'string'],
602     [cf_password=> 'Password' => 'password'],
603     [path => 'Map path' => 'string'],
604     ],
605 elmex 1.50 close_on_save => 1,
606 elmex 1.45 save_cb => sub {
607     my ($meta) = @_;
608     warn "UPLOAD[".Crossfire::to_json ($meta)."]\n";
609     }
610     );
611    
612     $w->signal_connect (destroy => sub { delete $self->{meta_info_win} });
613    
614     $w->show_all;
615     }
616    
617     sub upload_map_test {
618     my ($self) = @_;
619    
620     my $meta = dclone $self->{meta_info};
621    
622     my $w = $self->{meta_info_win} = GCE::HashDialogue->new ();
623    
624     $w->init (
625     dialog_default_size => [500, 300, 220, 20],
626     layout_name => 'map_upload_test',
627     title => 'gce - map test upload',
628     ref_hash => $meta,
629     dialog => [
630     [gameserver => 'Game server' => 'string'],
631     [testserver => 'Test server' => 'string'],
632     [undef => x => 'sep' ],
633     [cf_login => 'Server login name' => 'string'],
634     [cf_password=> 'Password' => 'password'],
635     [path => 'Map path' => 'string'],
636     ],
637     save_cb => sub {
638     my ($meta) = @_;
639     warn "UPLOAD[".Crossfire::to_json ($meta)."]\n";
640     }
641     );
642    
643     $w->signal_connect (destroy => sub { delete $self->{meta_info_win} });
644    
645     $w->show_all;
646    
647    
648     }
649 elmex 1.44
650     sub open_meta_info {
651 elmex 1.42 my ($self) = @_;
652    
653 elmex 1.44 return if $self->{meta_info_win};
654    
655     my $w = $self->{meta_info_win} = GCE::HashDialogue->new ();
656    
657     $w->init (
658     dialog_default_size => [500, 300, 220, 20],
659     layout_name => 'meta_info_win',
660     title => 'meta info',
661     ref_hash => $self->{meta_info},
662     dialog => [
663     [path => 'Map path' => 'string'],
664     [cf_login => 'Login name' => 'string'],
665     [revision => 'CVS Revision' => 'label'],
666     [cvs_root => 'CVS Root' => 'label'],
667     [lib_root => 'LIB Root' => 'label'],
668     [testserver => 'Test server' => 'label'],
669     [gameserver => 'Game server' => 'label'],
670     ],
671     );
672    
673     $w->signal_connect (destroy => sub { delete $self->{meta_info_win} });
674    
675     $w->show_all;
676 elmex 1.42 }
677    
678 elmex 1.49 sub open_map_info {
679     my ($self) = @_;
680     return if $self->{map_info};
681    
682     my $w = $self->{map_info} = Gtk2::Window->new ('toplevel');
683     $w->set_title ("gcrossedit - map info");
684    
685     $w->add (my $vb = Gtk2::VBox->new);
686     $vb->add (my $sw = Gtk2::ScrolledWindow->new);
687     $sw->set_policy ('automatic', 'automatic');
688     $sw->add (my $txt = Gtk2::TextView->new);
689     $vb->pack_start (my $hb = Gtk2::HBox->new (1, 1), 0, 1, 0);
690     $hb->pack_start (my $svbtn = Gtk2::Button->new ("save"), 1, 1, 0);
691     $hb->pack_start (my $logbtn = Gtk2::Button->new ("add log"), 1, 1, 0);
692     $hb->pack_start (my $closebtn = Gtk2::Button->new ("close"), 1, 1, 0);
693    
694     my $buf = $txt->get_buffer ();
695     $buf->set_text ($self->{map}{map}{info}{msg});
696    
697     $svbtn->signal_connect (clicked => sub {
698     my $buf = $txt->get_buffer ();
699     my $txt = $buf->get_text ($buf->get_start_iter, $buf->get_end_iter, 0);
700     $self->{map}{map}{info}{msg} = $txt;
701     });
702    
703     $logbtn->signal_connect (clicked => sub {
704     my $buf = $txt->get_buffer ();
705     $buf->insert ($buf->get_start_iter, "- " . strftime ("%F %T %Z", localtime (time)) . " by " . ($main::CFG->{username} || $ENV{USER}) . ":\n");
706     $txt->set_buffer ($buf);
707     });
708    
709     $closebtn->signal_connect (clicked => sub {
710     $w->destroy;
711     });
712    
713     ::set_pos_and_size ($w, $main::CFG->{map_info}, 400, 400, 220, 20);
714     $w->signal_connect (destroy => sub {
715     delete $self->{map_info};
716     });
717     $w->show_all;
718     }
719    
720 elmex 1.17 sub open_map_prop {
721     my ($self) = @_;
722    
723 elmex 1.42 return if $self->{map_properties};
724    
725 elmex 1.44 my $w = $self->{map_properties} = GCE::HashDialogue->new ();
726 elmex 1.17
727 elmex 1.44 $w->init (
728     dialog_default_size => [500, 500, 220, 20],
729     layout_name => 'map_prop_win',
730     title => 'map properties',
731     ref_hash => $self->{map}{map}{info},
732 elmex 1.50 close_on_save => 1,
733 elmex 1.44 dialog => [
734 elmex 1.27 [qw/name Name string/],
735     [qw/region Region string/],
736     [qw/enter_x Enter-x string/],
737     [qw/enter_y Enter-y string/],
738     [qw/reset_timeout Reset-timeout string/],
739     [qw/swap_time Swap-timeout string/],
740 elmex 1.44 [undef, qw/x sep/],
741 elmex 1.27 [qw/difficulty Difficulty string/],
742     [qw/windspeed Windspeed string/],
743     [qw/pressure Pressure string/],
744     [qw/humid Humid string/],
745     [qw/temp Temp string/],
746     [qw/darkness Darkness string/],
747     [qw/sky Sky string/],
748     [qw/winddir Winddir string/],
749 elmex 1.44 [undef, qw/x sep/],
750 elmex 1.27 [qw/width Width label/], # sub { $self->{map}{map}->resize ($_[0], $self->{map}{map}{height}) }],
751     [qw/height Height label/], # sub { $self->{map}{map}->resize ($self->{map}{map}{width}, $_[0]) }],
752 elmex 1.44 [undef, qw/x sep/],
753     # [qw/msg Text text/],
754     # [qw/maplore Maplore text/],
755 elmex 1.27 [qw/outdoor Outdoor check/],
756     [qw/unique Unique check/],
757     [qw/fixed_resettime Fixed-resettime check/],
758 elmex 1.55 [per_player => 'Per player' => 'check'],
759     [per_party => 'Per party' => 'check'],
760 elmex 1.54 [no_reset => 'No reset' => 'check'],
761 elmex 1.57 [music => 'Map Music' => 'string'],
762 elmex 1.44 [undef, qw/x sep/],
763 elmex 1.27 [qw/tile_path_1 Northpath string/],
764     [qw/tile_path_2 Eastpath string/],
765     [qw/tile_path_3 Southpath string/],
766     [qw/tile_path_4 Westpath string/],
767     [qw/tile_path_5 Toppath string/],
768     [qw/tile_path_6 Bottompath string/],
769 elmex 1.44 [undef, qw/x sep/],
770     [undef, 'For shop description look in the manual',
771     'button', sub { $::MAINWIN->show_help_window }],
772 elmex 1.39 [qw/shopmin Shopmin string/],
773     [qw/shopmax Shopmax string/],
774     [qw/shoprace Shoprace string/],
775     [qw/shopgreed Shopgreed string/],
776     [qw/shopitems Shopitems string/],
777 elmex 1.44 ]
778     );
779 elmex 1.17
780 elmex 1.44 $w->signal_connect (destroy => sub { delete $self->{map_properties} });
781 elmex 1.17 $w->show_all;
782     }
783    
784 elmex 1.44 #################################################################
785     ###### MAP EDITOR INIT ##########################################
786     #################################################################
787    
788     sub INIT_INSTANCE {
789     my ($self) = @_;
790    
791 elmex 1.60 $self->set_window_title;
792 elmex 1.44 $self->add (my $vb = Gtk2::VBox->new);
793    
794     $vb->pack_start (my $menu = $self->build_menu, 0, 1, 0);
795    
796     $vb->pack_start (my $map = $self->{map} = Crossfire::MapWidget->new, 1, 1, 0);
797    
798 elmex 1.61 $map->signal_connect_after (stack_change => sub {
799     my ($map, $x, $y, $stack) = @_;
800     $self->update_overlays ($x, $y, $stack);
801     });
802     $map->signal_connect_after (swap_stack_change => sub {
803     my ($map, $x, $y, $stack) = @_;
804     $self->update_overlays ($x, $y, $stack);
805     });
806    
807     $self->{connection_overlay_foreground}
808     = Gtk2::Gdk::Color->new (257 * 255, 257 * 255, 0);
809     $self->{connection_overlay_background}
810     = Gtk2::Gdk::Color->new (0, 0, 0);
811 elmex 1.62 # my $ygc
812     # = $self->{connection_overlay_yellow_gc}
813     # = Gtk2::Gdk::GC->new ($self->{map}{window});
814     # $ygc->set_foreground (Gtk2::Gdk::Color->new (257 * 255, 257 * 255, 0));
815 elmex 1.61
816 elmex 1.44 $map->signal_connect_after (key_press_event => sub {
817     my ($map, $event) = @_;
818    
819     my $kv = $event->keyval;
820    
821     my $ret = 0;
822    
823     my ($x, $y) = $map->coord ($map->get_pointer);
824     for ([Control_L => sub { $self->{ea_alt} = $::MAINWIN->{edit_collection}{erase} }],
825     [Alt_L => sub { $self->{ea_alt} = $::MAINWIN->{edit_collection}{pick} }],
826     [c => sub { $::MAINWIN->{edit_collection}{select}->copy }],
827     [v => sub { $::MAINWIN->{edit_collection}{select}->paste ($map, $x, $y) }],
828     [n => sub { $::MAINWIN->{edit_collection}{select}->invoke }],
829     )
830     {
831     my $ed = $_;
832    
833     if ($kv == $Gtk2::Gdk::Keysyms{$ed->[0]}) {
834     my $was_in_draw = defined $self->{draw_mode};
835    
836     $self->stop_drawmode ($map)
837     if $was_in_draw && grep { $ed->[0] eq $_ } qw/Control_L Alt_L/;
838    
839     $ed->[1]->();
840     $ret = 1;
841    
842     $self->start_drawmode ($map)
843     if $was_in_draw && grep { $ed->[0] eq $_ } qw/Control_L Alt_L/;
844     }
845     }
846    
847     if ($self->ea->special_arrow) {
848     $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($self->ea->special_arrow));
849     } else {
850     # FIXME: Get the original cursor and insert it here
851     $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
852     }
853    
854     $ret
855     });
856    
857     $map->signal_connect_after (key_release_event => sub {
858     my ($map, $event) = @_;
859    
860     my $ret = 0;
861    
862     if ($event->keyval == $Gtk2::Gdk::Keysyms{Control_L}
863     or $event->keyval == $Gtk2::Gdk::Keysyms{Alt_L})
864     {
865     my $was_in_draw = defined $self->{draw_mode};
866    
867     $self->stop_drawmode ($map)
868     if $was_in_draw;
869    
870     delete $self->{ea_alt};
871     $ret = 1;
872    
873     $self->start_drawmode ($map)
874     if $was_in_draw;
875     }
876    
877     if ($self->ea->special_arrow) {
878     $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($self->ea->special_arrow));
879     } else {
880     # FIXME: Get the original cursor and insert it here
881     $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
882     }
883    
884     $ret
885     });
886     $map->signal_connect_after (button_press_event => sub {
887     my ($map, $event) = @_;
888    
889     if ((not $self->{draw_mode}) and $event->button == 1) {
890     my $ea = $self->ea;
891    
892     $self->start_drawmode ($map);
893    
894     $ea->want_cursor
895     or $map->disable_tooltip;
896    
897     return 1;
898     } elsif ($event->button == 3) {
899     $self->do_context_menu ($map, $event);
900     return 1;
901     }
902    
903     0
904     });
905    
906     $map->signal_connect_after (motion_notify_event => sub {
907     my ($map, $event) = @_;
908    
909     $self->{draw_mode}
910     or return;
911    
912     my $ea = $self->ea;
913    
914     my ($X, $Y) = @{$self->{draw_mode}}[0,1];
915     my ($x, $y) = $map->coord ($map->get_pointer);
916    
917     while ($x != $X || $y != $Y) {
918    
919     $X++ if $X < $x;
920     $X-- if $X > $x;
921     $Y++ if $Y < $y;
922     $Y-- if $Y > $y;
923    
924     unless ($ea->only_on_click) {
925     $ea->edit ($map, $X, $Y, $self)
926     if $X >= 0 and $Y >= 0 and $X < $map->{map}{width} and $Y < $map->{map}{height};
927     }
928     }
929    
930     @{$self->{draw_mode}}[0,1] = ($X, $Y);
931    
932     1
933     });
934    
935     $map->signal_connect_after (button_release_event => sub {
936     my ($map, $event) = @_;
937    
938     if ($self->{draw_mode} and $event->button == 1) {
939     my $ea = $self->ea;
940    
941     $self->stop_drawmode ($map);
942    
943     $ea->want_cursor
944     or $map->enable_tooltip;
945    
946     return 1;
947     }
948    
949     0
950     });
951    
952     ::set_pos_and_size ($self, $main::CFG->{map_window}, 500, 500, 200, 0);
953     }
954    
955 elmex 1.1 =head1 AUTHOR
956    
957     Marc Lehmann <schmorp@schmorp.de>
958     http://home.schmorp.de/
959    
960     Robin Redeker <elmex@ta-sa.org>
961     http://www.ta-sa.org/
962    
963     =cut
964 root 1.46
965     1
966 elmex 1.1