ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/gde/GCE/MapEditor.pm
Revision: 1.38
Committed: Fri Jun 2 13:39:50 2006 UTC (20 years, 4 months ago) by elmex
Branch: MAIN
Changes since 1.37: +54 -1 lines
Log Message:
added 'Add inventory' context menu to mapeditor.

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.22 use GCE::Util;
19 elmex 1.1
20 elmex 1.4 use Glib::Object::Subclass
21 elmex 1.6 Gtk2::Window;
22 elmex 1.1
23     use strict;
24    
25 elmex 1.35 sub do_context_menu {
26     my ($self, $map, $event) = @_;
27    
28     my ($x, $y) = $map->coord ($event->x, $event->y);
29    
30     my $menu = Gtk2::Menu->new;
31     foreach my $cm (
32     [
33     Follow => sub {
34 elmex 1.36 $::MAINWIN->{edit_collection}{followexit}->edit ($map, $x, $y, $self)
35 elmex 1.38 },
36 elmex 1.35 ]
37     ) {
38     my $item = Gtk2::MenuItem->new ($cm->[0]);
39     $menu->append ($item);
40     $item->show;
41     $item->signal_connect (activate => $cm->[1]);
42     }
43 elmex 1.38
44     my $inv_item = Gtk2::MenuItem->new ("Add inventory");
45     $inv_item->set_submenu (my $smenu = new Gtk2::Menu);
46     $menu->append ($inv_item);
47    
48     for my $sr (reverse $self->get_stack_refs ($map, $x, $y)) {
49     my $item = Gtk2::MenuItem->new ($sr->longname);
50     $smenu->append ($item);
51     $item->show;
52     $item->signal_connect (activate => sub {
53     $sr->add_inv ($::MAINWIN->get_pick);
54     });
55     }
56    
57     $inv_item->show;
58 elmex 1.35 $menu->popup (undef, undef, undef, undef, $event->button, $event->time);
59     }
60    
61 root 1.9 sub build_menu {
62     my ($self) = @_;
63    
64     my $menu_tree = [
65     _File => {
66     item_type => '<Branch>',
67     children => [
68     "_Save" => {
69 root 1.10 callback => sub { $self->save_map },
70 root 1.9 accelerator => '<ctrl>S'
71     },
72 root 1.10 "Save As" => {
73     callback => sub { $self->save_map_as },
74     },
75 elmex 1.32 "_Map Properties" => {
76     callback => sub { $self->open_map_prop },
77     accelerator => "<ctrl>P"
78     },
79     "_Map Resize" => {
80     callback => sub { $self->open_resize_map },
81     accelerator => "<ctrl>R"
82     },
83 root 1.13 "Close" => {
84 elmex 1.31 callback => sub { $self->destroy },
85 root 1.13 },
86 root 1.10 ]
87 root 1.9 },
88     _Edit => {
89     item_type => '<Branch>',
90     children => [
91     "_Undo" => {
92     callback => sub { $self->undo },
93 elmex 1.17 accelerator => "<ctrl>Z"
94 root 1.9 },
95     "_Redo" => {
96     callback => sub { $self->redo },
97 elmex 1.17 accelerator => "<ctrl>Y"
98     },
99 root 1.9 ]
100     },
101 elmex 1.27 _Go => {
102     item_type => '<Branch>',
103     children => [
104     "_Up" => {
105     callback => sub { $self->follow ('u') },
106 elmex 1.33 accelerator => "<ctrl>Up"
107 elmex 1.27 },
108     "_Down" => {
109     callback => sub { $self->follow ('d') },
110 elmex 1.33 accelerator => "<ctrl>Down"
111 elmex 1.27 },
112     "_Right" => {
113     callback => sub { $self->follow ('r') },
114 elmex 1.33 accelerator => "<ctrl>Right"
115 elmex 1.27 },
116     "_Left" => {
117     callback => sub { $self->follow ('l') },
118 elmex 1.33 accelerator => "<ctrl>Left"
119 elmex 1.27 },
120     ]
121     },
122 elmex 1.29 _Help => {
123     item_type => '<Branch>',
124     children => [
125 elmex 1.30 _Manual => {
126 elmex 1.29 callback => sub { $::MAINWIN->show_help_window },
127     accelerator => "<ctrl>H"
128     },
129     ]
130     },
131 root 1.9 ];
132    
133     my $men =
134     Gtk2::SimpleMenu->new (
135     menu_tree => $menu_tree,
136     default_callback => \&default_cb,
137     );
138    
139 elmex 1.23 for (
140     [i => 'pick'],
141     [p => 'place'],
142 elmex 1.24 [e => 'erase'],
143     [s => 'select'],
144     [l => 'eval'],
145 elmex 1.37 [t => 'connect'],
146 elmex 1.24 [f => 'followexit']
147 elmex 1.23 )
148     {
149 elmex 1.24 my $tool = $_->[1];
150 elmex 1.23 $men->{accel_group}->connect ($Gtk2::Gdk::Keysyms{$_->[0]}, [], 'visible',
151 elmex 1.24 sub { $::MAINWIN->set_edit_tool ($tool) });
152 elmex 1.23 }
153    
154 root 1.9 $self->add_accel_group ($men->{accel_group});
155    
156     return $men->{widget};
157     }
158    
159 elmex 1.31 sub set_edit_tool {
160     my ($self, $tool) = @_;
161    
162     $self->{etool} = $tool;
163    
164     if ($self->ea->special_arrow) {
165     $self->{map}{window}->set_cursor (Gtk2::Gdk::Cursor->new ($self->ea->special_arrow));
166 elmex 1.34 } else {
167     # FIXME: Get the original cursor and insert it here
168     $self->{map}{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
169 elmex 1.31 }
170     }
171    
172 elmex 1.19 sub ea {
173     my ($self) = @_;
174 elmex 1.31 $self->{ea_alt} || $self->{etool};
175 elmex 1.19 }
176    
177 elmex 1.1 sub INIT_INSTANCE {
178     my ($self) = @_;
179    
180 elmex 1.6 $self->set_title ('gce - map editor');
181     $self->add (my $vb = Gtk2::VBox->new);
182 elmex 1.1
183 root 1.9 $vb->pack_start (my $menu = $self->build_menu, 0, 1, 0);
184    
185     $vb->pack_start (my $map = $self->{map} = Crossfire::MapWidget->new, 1, 1, 0);
186    
187 elmex 1.31 $map->signal_connect_after (key_press_event => sub {
188 elmex 1.19 my ($map, $event) = @_;
189 root 1.9
190 elmex 1.19 my $kv = $event->keyval;
191 root 1.9
192 elmex 1.31 my $ret = 0;
193    
194 elmex 1.23 my ($x, $y) = $map->coord ($map->get_pointer);
195     for ([Control_L => sub { $self->{ea_alt} = $::MAINWIN->{edit_collection}{erase} }],
196     [Alt_L => sub { $self->{ea_alt} = $::MAINWIN->{edit_collection}{pick} }],
197     [c => sub { $::MAINWIN->{edit_collection}{select}->copy }],
198 elmex 1.28 [v => sub { $::MAINWIN->{edit_collection}{select}->paste ($map, $x, $y) }],
199 elmex 1.24 [n => sub { $::MAINWIN->{edit_collection}{select}->invoke }],
200 elmex 1.23 )
201     {
202 elmex 1.34 my $ed = $_;
203    
204     if ($kv == $Gtk2::Gdk::Keysyms{$ed->[0]}) {
205     my $was_in_draw = defined $self->{draw_mode};
206    
207     $self->stop_drawmode ($map)
208     if $was_in_draw && grep { $ed->[0] eq $_ } qw/Control_L Alt_L/;
209    
210     $ed->[1]->();
211 elmex 1.31 $ret = 1;
212 elmex 1.34
213     $self->start_drawmode ($map)
214     if $was_in_draw && grep { $ed->[0] eq $_ } qw/Control_L Alt_L/;
215 elmex 1.23 }
216 elmex 1.19 }
217 root 1.9
218 elmex 1.19 if ($self->ea->special_arrow) {
219     $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($self->ea->special_arrow));
220 elmex 1.34 } else {
221     # FIXME: Get the original cursor and insert it here
222     $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
223 elmex 1.19 }
224 elmex 1.31
225     $ret
226 elmex 1.19 });
227 elmex 1.31
228     $map->signal_connect_after (key_release_event => sub {
229 elmex 1.19 my ($map, $event) = @_;
230    
231 elmex 1.31 my $ret = 0;
232    
233 elmex 1.19 if ($event->keyval == $Gtk2::Gdk::Keysyms{Control_L}
234     or $event->keyval == $Gtk2::Gdk::Keysyms{Alt_L})
235     {
236 elmex 1.34 my $was_in_draw = defined $self->{draw_mode};
237    
238     $self->stop_drawmode ($map)
239     if $was_in_draw;
240    
241 elmex 1.19 delete $self->{ea_alt};
242 elmex 1.31 $ret = 1;
243 elmex 1.34
244     $self->start_drawmode ($map)
245     if $was_in_draw;
246 elmex 1.19 }
247    
248     if ($self->ea->special_arrow) {
249     $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($self->ea->special_arrow));
250     } else {
251 elmex 1.31 # FIXME: Get the original cursor and insert it here
252 elmex 1.19 $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
253     }
254 elmex 1.31
255     $ret
256 elmex 1.19 });
257 elmex 1.31 $map->signal_connect_after (button_press_event => sub {
258 elmex 1.19 my ($map, $event) = @_;
259 elmex 1.1
260 elmex 1.31 if ((not $self->{draw_mode}) and $event->button == 1) {
261 elmex 1.19 my $ea = $self->ea;
262 elmex 1.4
263 elmex 1.34 $self->start_drawmode ($map);
264 root 1.9
265 elmex 1.19 $ea->want_cursor
266     or $map->disable_tooltip;
267 root 1.9
268 elmex 1.19 return 1;
269 elmex 1.35 } elsif ($event->button == 3) {
270     $self->do_context_menu ($map, $event);
271     return 1;
272 elmex 1.19 }
273 elmex 1.31
274 elmex 1.19 0
275 root 1.9 });
276    
277     $map->signal_connect_after (motion_notify_event => sub {
278     my ($map, $event) = @_;
279    
280     $self->{draw_mode}
281     or return;
282    
283 elmex 1.19 my $ea = $self->ea;
284    
285     my ($X, $Y) = @{$self->{draw_mode}}[0,1];
286 root 1.9 my ($x, $y) = $map->coord ($map->get_pointer);
287 elmex 1.4
288 root 1.9 while ($x != $X || $y != $Y) {
289 elmex 1.4
290 root 1.9 $X++ if $X < $x;
291     $X-- if $X > $x;
292     $Y++ if $Y < $y;
293     $Y-- if $Y > $y;
294 elmex 1.4
295 elmex 1.22 $ea->edit ($map, $X, $Y, $self)
296 elmex 1.15 if $X >= 0 and $Y >= 0 and $X < $map->{map}{width} and $Y < $map->{map}{height};
297 root 1.9 }
298 elmex 1.4
299 elmex 1.19 @{$self->{draw_mode}}[0,1] = ($X, $Y);
300 elmex 1.1
301 root 1.9 1
302     });
303 elmex 1.1
304 elmex 1.31 $map->signal_connect_after (button_release_event => sub {
305 root 1.9 my ($map, $event) = @_;
306 elmex 1.1
307 elmex 1.31 if ($self->{draw_mode} and $event->button == 1) {
308 elmex 1.19 my $ea = $self->ea;
309 elmex 1.1
310 elmex 1.34 $self->stop_drawmode ($map);
311 elmex 1.4
312 root 1.9 $ea->want_cursor
313     or $map->enable_tooltip;
314 elmex 1.1
315 root 1.9 return 1;
316     }
317 elmex 1.1
318 root 1.9 0
319     });
320     }
321 elmex 1.8
322 elmex 1.34 sub start_drawmode {
323     my ($self, $map) = @_;
324    
325     $self->{draw_mode} and return;
326    
327     # XXX: is this okay? my ($x, $y) = $map->coord ($event->x, $event->y);
328     my ($x, $y) = $map->coord ($map->get_pointer);
329    
330     my $ea = $self->ea;
331    
332 elmex 1.36 $ea->begin ($map, $x, $y, $self);
333    
334     $ea->edit ($map, $x, $y, $self)
335 elmex 1.34 if $x >= 0 and $y >= 0 and $x < $map->{map}{width} and $y < $map->{map}{height};
336    
337     $self->{draw_mode} = [$x, $y];
338     }
339    
340     sub stop_drawmode {
341     my ($self, $map) = @_;
342    
343     $self->{draw_mode} or return;
344    
345     my ($x, $y) = $map->coord ($map->get_pointer);
346    
347     my $ea = $self->ea;
348     $ea->end ($map, $x, $y, $self);
349    
350     delete $self->{draw_mode};
351     }
352    
353 elmex 1.16 # FIXME: Fix the automatic update of the attribute editor! and also the stack view!
354 root 1.9 sub undo {
355     my ($self) = @_;
356 elmex 1.8
357 root 1.9 my $map = $self->{map}; # the Crossfire::MapWidget
358 elmex 1.1
359 root 1.9 $map->{undo_stack_pos}
360     or return;
361 elmex 1.1
362 root 1.9 $map->change_swap ($map->{undo_stack}[--$map->{undo_stack_pos}]);
363     }
364 elmex 1.1
365 elmex 1.38 sub get_stack_refs {
366     my ($self, $map, $x, $y) = @_;
367    
368     my $cstack = $map->get ($x, $y);
369    
370     return [] unless @$cstack;
371    
372     my @refs;
373    
374     for my $arch (@$cstack) {
375     my ($ox, $oy) = ($x, $y);
376     if ($arch->{_virtual}) {
377     $ox = $arch->{virtual_x};
378     $oy = $arch->{virtual_y};
379     $arch = $arch->{_virtual};
380     $cstack = $map->get ($ox, $oy);
381     # XXX: This heavily blows up if $arch isn't on $cstack now.. and it actually really does :(
382     }
383    
384     push @refs,
385     GCE::ArchRef->new (
386     arch => $arch,
387     cb => sub {
388     $map->change_begin ('attredit');
389     $map->change_stack ($ox, $oy, $cstack);
390    
391     if (my $changeset = $map->change_end) {
392     splice @{ $map->{undo_stack} ||= [] },
393     $map->{undo_stack_pos}++, 1e6,
394     $changeset;
395     }
396     }
397     );
398     }
399    
400     return @refs;
401     }
402    
403 root 1.9 sub redo {
404     my ($self) = @_;
405 elmex 1.7
406 root 1.9 my $map = $self->{map}; # the Crossfire::MapWidget
407 elmex 1.7
408 elmex 1.14 $map->{undo_stack}
409     and $map->{undo_stack_pos} < @{$map->{undo_stack}}
410     or return;
411 elmex 1.1
412 root 1.9 $map->change_swap ($map->{undo_stack}[$map->{undo_stack_pos}++]);
413 root 1.3 }
414    
415     sub open_map {
416 elmex 1.26 my ($self, $path, $key) = @_;
417    
418     $self->{mapkey} = $key;
419 root 1.3
420 elmex 1.18 if (ref $path) {
421     $self->{map}->set_map ($path);
422    
423     } else {
424     $self->{path} = $path;
425 elmex 1.17 # print "OPENMAP $path\n";
426 elmex 1.18 $self->{map}->set_map (my $m = new_from_file Crossfire::Map $path);
427     require Data::Dumper;
428     # print "FOO:" .Data::Dumper::Dumper ($m) . "\n";
429     }
430 root 1.3 }
431    
432 root 1.13 sub save_map {
433     my ($self) = @_;
434    
435 elmex 1.19 if ($self->{path}) {
436     $self->{map}{map}->write_file ($self->{path});
437 elmex 1.22 quick_msg ($self, "saved to $self->{path}");
438     } else {
439     $self->save_map_as;
440 elmex 1.19 }
441 root 1.13 }
442    
443     sub save_map_as {
444     my ($self) = @_;
445 elmex 1.19
446 elmex 1.22 my $fc = $::MAINWIN->new_filechooser ('gce - save map', 1, $self->{path});
447 elmex 1.19
448     if ('ok' eq $fc->run) {
449    
450     $::MAINWIN->{fc_last_folder} = $fc->get_current_folder;
451     $::MAINWIN->{fc_last_folders}->{$self->{fc_last_folder}}++;
452    
453     $self->{map}{map}->write_file ($self->{path} = $fc->get_filename);
454 elmex 1.22 quick_msg ($self, "saved to $self->{path}");
455 elmex 1.19 }
456    
457     $fc->destroy;
458 root 1.13 }
459    
460 elmex 1.17 sub _add_prop_entry {
461     my ($self, $table, $idx, $key, $desc, $type, $changecb) = @_;
462    
463     my $edwid;
464    
465     if ($type eq 'string') {
466     $table->attach_defaults (my $lbl = Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
467     $edwid = Gtk2::Entry->new;
468     $edwid->set_text ($self->{map}{map}{info}{$key});
469     $edwid->signal_connect (changed => sub {
470     $self->{map}{map}{info}{$key} = $_[0]->get_text;
471     if ($changecb) {
472     $changecb->($_[0]->get_text);
473     }
474     });
475     $table->attach_defaults ($edwid, 1, 2, $idx, $idx + 1);
476    
477     } elsif ($type eq 'button') {
478     $table->attach_defaults (my $b = Gtk2::Button->new_with_label ($desc), 0, 2, $idx, $idx + 1);
479     $b->signal_connect (clicked => ($changecb || sub {}));
480    
481     } elsif ($type eq 'label') {
482     $table->attach_defaults (my $lbl = Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
483     $edwid = Gtk2::Label->new ($self->{map}{map}{info}{$key});
484     $table->attach_defaults ($edwid, 1, 2, $idx, $idx + 1);
485    
486 elmex 1.27 } elsif ($type eq 'check') {
487     $table->attach_defaults (my $lbl1 = Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
488     $table->attach_defaults (my $lbl = Gtk2::CheckButton->new, 1, 2, $idx, $idx + 1);
489     $lbl->set_active ($self->{map}{map}{info}{$key});
490     $lbl->signal_connect (toggled => sub {
491     my ($lbl) = @_;
492     $self->{map}{map}{info}{$key} = $lbl->get_active * 1;
493     ($changecb || sub {})->($lbl->get_active);
494     });
495    
496     } elsif ($type eq 'sep') {
497     $table->attach_defaults (my $lbl1 = Gtk2::HSeparator->new, 0, 2, $idx, $idx + 1);
498 elmex 1.17 } else {
499     $edwid = Gtk2::Label->new ("FOO");
500     }
501     }
502    
503     sub open_resize_map {
504     my ($self) = @_;
505    
506     my $w = Gtk2::Window->new ('toplevel');
507     $w->set_default_size (250, 150);
508     $w->add (my $sw = Gtk2::ScrolledWindow->new);
509     $sw->add_with_viewport (my $v = Gtk2::VBox->new);
510     $sw->set_policy ('automatic', 'automatic');
511     $v->pack_start (my $t = Gtk2::Table->new (2, 10), 0, 0, 0);
512    
513     my $i = 0;
514     for (
515     [qw/width Width string/],
516     [qw/height Height string/],
517     [qw/save Save button/,
518     sub {
519     $self->{map}{map}->resize ($self->{map}{map}{info}{width}, $self->{map}{map}{info}{height});
520 elmex 1.19 $self->{map}->invalidate_all;
521     $w->destroy;
522 elmex 1.17 }
523     ],
524     )
525     {
526     $self->_add_prop_entry ($t, $i++, @$_);
527     }
528    
529     $w->show_all;
530     }
531    
532 elmex 1.27 sub follow {
533     my ($self, $dir) = @_;
534    
535     my %dir_to_path = (
536     u => 'tile_path_1',
537     d => 'tile_path_3',
538     r => 'tile_path_2',
539     l => 'tile_path_4',
540     );
541    
542     defined $dir_to_path{$dir}
543     or return;
544     my $map = $self->{map}{map}{info}{$dir_to_path{$dir}}
545     or return;
546    
547     $map = map2abs ($map, $self);
548     $::MAINWIN->open_map_editor ($map);
549     }
550    
551 elmex 1.17 sub open_map_prop {
552     my ($self) = @_;
553    
554    
555     my $w = Gtk2::Window->new ('toplevel');
556     $w->set_default_size (500, 500);
557     $w->add (my $sw = Gtk2::ScrolledWindow->new);
558     $sw->add_with_viewport (my $v = Gtk2::VBox->new);
559     $sw->set_policy ('automatic', 'automatic');
560     $v->pack_start (my $t = Gtk2::Table->new (2, 10), 0, 0, 0);
561    
562     my $i = 0;
563     for (
564 elmex 1.27 [qw/name Name string/],
565     [qw/region Region string/],
566     [qw/enter_x Enter-x string/],
567     [qw/enter_y Enter-y string/],
568     [qw/reset_timeout Reset-timeout string/],
569     [qw/swap_time Swap-timeout string/],
570     [qw/x x sep/],
571     [qw/difficulty Difficulty string/],
572     [qw/windspeed Windspeed string/],
573     [qw/pressure Pressure string/],
574     [qw/humid Humid string/],
575     [qw/temp Temp string/],
576     [qw/darkness Darkness string/],
577     [qw/sky Sky string/],
578     [qw/winddir Winddir string/],
579     [qw/x x sep/],
580     [qw/width Width label/], # sub { $self->{map}{map}->resize ($_[0], $self->{map}{map}{height}) }],
581     [qw/height Height label/], # sub { $self->{map}{map}->resize ($self->{map}{map}{width}, $_[0]) }],
582     [qw/x x sep/],
583     [qw/msg Text text/],
584     [qw/maplore Maplore text/],
585     [qw/outdoor Outdoor check/],
586     [qw/unique Unique check/],
587     [qw/fixed_resettime Fixed-resettime check/],
588     [qw/x x sep/],
589     [qw/tile_path_1 Northpath string/],
590     [qw/tile_path_2 Eastpath string/],
591     [qw/tile_path_3 Southpath string/],
592     [qw/tile_path_4 Westpath string/],
593     [qw/tile_path_5 Toppath string/],
594     [qw/tile_path_6 Bottompath string/],
595 elmex 1.17 )
596     {
597     $self->_add_prop_entry ($t, $i++, @$_);
598     }
599    
600     $w->show_all;
601     }
602    
603 elmex 1.1 =head1 AUTHOR
604    
605     Marc Lehmann <schmorp@schmorp.de>
606     http://home.schmorp.de/
607    
608     Robin Redeker <elmex@ta-sa.org>
609     http://www.ta-sa.org/
610    
611     =cut
612     1;
613