ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/gde/GCE/MapEditor.pm
Revision: 1.22
Committed: Thu Mar 16 11:59:34 2006 UTC (20 years, 7 months ago) by elmex
Branch: MAIN
Changes since 1.21: +10 -4 lines
Log Message:
implemented connect tool for exits and some minor improvements in saving

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 root 1.9 sub build_menu {
26     my ($self) = @_;
27    
28     my $menu_tree = [
29     _File => {
30     item_type => '<Branch>',
31     children => [
32     "_Save" => {
33 root 1.10 callback => sub { $self->save_map },
34 root 1.9 accelerator => '<ctrl>S'
35     },
36 root 1.10 "Save As" => {
37     callback => sub { $self->save_map_as },
38     },
39 root 1.13 "Close" => {
40     callback => sub { $self->delete; 1 },
41     },
42 root 1.10 ]
43 root 1.9 },
44     _Edit => {
45     item_type => '<Branch>',
46     children => [
47     "_Undo" => {
48     callback => sub { $self->undo },
49 elmex 1.17 accelerator => "<ctrl>Z"
50 root 1.9 },
51     "_Redo" => {
52     callback => sub { $self->redo },
53 elmex 1.17 accelerator => "<ctrl>Y"
54     },
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 root 1.9 },
63     ]
64     },
65    
66     ];
67    
68     my $men =
69     Gtk2::SimpleMenu->new (
70     menu_tree => $menu_tree,
71     default_callback => \&default_cb,
72     );
73    
74 elmex 1.19 $men->{accel_group}->connect ($Gtk2::Gdk::Keysyms{'i'}, [], 'visible',
75     sub { $::MAINWIN->set_edit_tool ('pick') });
76     $men->{accel_group}->connect ($Gtk2::Gdk::Keysyms{'p'}, [], 'visible',
77     sub { $::MAINWIN->set_edit_tool ('place') });
78     $men->{accel_group}->connect ($Gtk2::Gdk::Keysyms{'e'}, [], 'visible',
79     sub { $::MAINWIN->set_edit_tool ('erase') });
80 root 1.9 $self->add_accel_group ($men->{accel_group});
81    
82     return $men->{widget};
83     }
84    
85 elmex 1.19 sub ea {
86     my ($self) = @_;
87     $self->{ea_alt} || $::MAINWIN->{sel_editaction};
88     }
89    
90 elmex 1.1 sub INIT_INSTANCE {
91     my ($self) = @_;
92    
93 elmex 1.6 $self->set_title ('gce - map editor');
94     $self->add (my $vb = Gtk2::VBox->new);
95 elmex 1.1
96 root 1.13 $self->signal_connect (delete_event => sub { $self->delete });
97    
98 root 1.9 $vb->pack_start (my $menu = $self->build_menu, 0, 1, 0);
99    
100     $vb->pack_start (my $map = $self->{map} = Crossfire::MapWidget->new, 1, 1, 0);
101    
102 elmex 1.19 $self->signal_connect (focus_in_event => sub {
103     my $ea = $::MAINWIN->{sel_editaction};
104     if ($ea->special_arrow) {
105     $self->{map}->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($ea->special_arrow));
106     } else {
107     $self->{map}->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
108     }
109 elmex 1.22 delete $self->{ea_alt};
110 elmex 1.19 });
111    
112     $map->signal_connect (key_press_event => sub {
113     my ($map, $event) = @_;
114 root 1.9
115 elmex 1.19 my $kv = $event->keyval;
116 root 1.9
117 elmex 1.19 if ($kv == $Gtk2::Gdk::Keysyms{Control_L}) {
118     $self->{ea_alt} = $::MAINWIN->{edit_collection}{erase};
119 root 1.9
120 elmex 1.19 } elsif ($kv == $Gtk2::Gdk::Keysyms{Alt_L}) {
121     $self->{ea_alt} = $::MAINWIN->{edit_collection}{pick};
122 elmex 1.21 } elsif ($kv == $Gtk2::Gdk::Keysyms{x}) {
123     $::MAINWIN->{edit_collection}{select}->move;
124 elmex 1.20 } elsif ($kv == $Gtk2::Gdk::Keysyms{c}) {
125     $::MAINWIN->{edit_collection}{select}->copy;
126     } elsif ($kv == $Gtk2::Gdk::Keysyms{v}) {
127     my ($x, $y) = $map->coord ($map->get_pointer);
128     $::MAINWIN->{edit_collection}{select}->paste ($x, $y);
129 elmex 1.19 }
130 root 1.9
131 elmex 1.19 if ($self->ea->special_arrow) {
132     $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($self->ea->special_arrow));
133     }
134     1;
135     });
136     $map->signal_connect (key_release_event => sub {
137     my ($map, $event) = @_;
138    
139     if ($event->keyval == $Gtk2::Gdk::Keysyms{Control_L}
140     or $event->keyval == $Gtk2::Gdk::Keysyms{Alt_L})
141     {
142     delete $self->{ea_alt};
143     }
144    
145     if ($self->ea->special_arrow) {
146     $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($self->ea->special_arrow));
147     } else {
148     # XXX: Get the original cursor and insert it here
149     $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
150     }
151     1;
152     });
153     $map->signal_connect (button_press_event => sub {
154     my ($map, $event) = @_;
155 elmex 1.1
156 elmex 1.19 my ($x, $y) = $map->coord ($event->x, $event->y);
157     my $as = $map->get ($x, $y);
158 elmex 1.1
159 elmex 1.19 if ((not $self->{draw_mode}) and $event->button != 2) {
160 elmex 1.4
161 elmex 1.19 my $ea = $self->ea;
162 elmex 1.4
163 elmex 1.22 $ea->begin ($map, $x, $y, $self)
164 elmex 1.19 if $x >= 0 and $y >= 0 and $x < $map->{map}{width} and $y < $map->{map}{height};
165    
166     $self->{draw_mode} = [$x, $y];
167 root 1.9
168 elmex 1.19 $ea->want_cursor
169     or $map->disable_tooltip;
170 root 1.9
171 elmex 1.19 return 1;
172     }
173     0
174 root 1.9 });
175    
176     $map->signal_connect_after (motion_notify_event => sub {
177     my ($map, $event) = @_;
178    
179     $self->{draw_mode}
180     or return;
181    
182 elmex 1.19 my $ea = $self->ea;
183    
184     my ($X, $Y) = @{$self->{draw_mode}}[0,1];
185 root 1.9 my ($x, $y) = $map->coord ($map->get_pointer);
186 elmex 1.4
187 root 1.9 while ($x != $X || $y != $Y) {
188 elmex 1.4
189 root 1.9 $X++ if $X < $x;
190     $X-- if $X > $x;
191     $Y++ if $Y < $y;
192     $Y-- if $Y > $y;
193 elmex 1.4
194 elmex 1.22 $ea->edit ($map, $X, $Y, $self)
195 elmex 1.15 if $X >= 0 and $Y >= 0 and $X < $map->{map}{width} and $Y < $map->{map}{height};
196 root 1.9 }
197 elmex 1.4
198 elmex 1.19 @{$self->{draw_mode}}[0,1] = ($X, $Y);
199 elmex 1.1
200 root 1.9 1
201     });
202 elmex 1.1
203 root 1.9 $map->signal_connect (button_release_event => sub {
204     my ($map, $event) = @_;
205 elmex 1.1
206 elmex 1.19 if ($self->{draw_mode}) {
207 root 1.9 my ($x, $y) = $map->coord ($map->get_pointer);
208 elmex 1.4
209 elmex 1.19 my $ea = $self->ea;
210 elmex 1.22 $ea->end ($map, $x, $y, $self);
211 elmex 1.1
212 root 1.9 delete $self->{draw_mode};
213 elmex 1.4
214 root 1.9 $ea->want_cursor
215     or $map->enable_tooltip;
216 elmex 1.1
217 root 1.9 return 1;
218     }
219 elmex 1.1
220 root 1.9 0
221     });
222     }
223 elmex 1.8
224 elmex 1.16 # FIXME: Fix the automatic update of the attribute editor! and also the stack view!
225 root 1.9 sub undo {
226     my ($self) = @_;
227 elmex 1.8
228 root 1.9 my $map = $self->{map}; # the Crossfire::MapWidget
229 elmex 1.1
230 root 1.9 $map->{undo_stack_pos}
231     or return;
232 elmex 1.1
233 root 1.9 $map->change_swap ($map->{undo_stack}[--$map->{undo_stack_pos}]);
234     }
235 elmex 1.1
236 root 1.9 sub redo {
237     my ($self) = @_;
238 elmex 1.7
239 root 1.9 my $map = $self->{map}; # the Crossfire::MapWidget
240 elmex 1.7
241 elmex 1.14 $map->{undo_stack}
242     and $map->{undo_stack_pos} < @{$map->{undo_stack}}
243     or return;
244 elmex 1.1
245 root 1.9 $map->change_swap ($map->{undo_stack}[$map->{undo_stack_pos}++]);
246 root 1.3 }
247    
248 root 1.13 sub delete {
249     my ($self) = @_;
250    
251     # check and modla dialog if "dirty"
252    
253     $self->destroy;
254     }
255    
256 root 1.3 sub open_map {
257     my ($self, $path) = @_;
258    
259 elmex 1.18 if (ref $path) {
260     $self->{map}->set_map ($path);
261    
262     } else {
263     $self->{path} = $path;
264 elmex 1.17 # print "OPENMAP $path\n";
265 elmex 1.18 $self->{map}->set_map (my $m = new_from_file Crossfire::Map $path);
266     require Data::Dumper;
267     # print "FOO:" .Data::Dumper::Dumper ($m) . "\n";
268     }
269 root 1.3 }
270    
271 root 1.13 sub save_map {
272     my ($self) = @_;
273    
274 elmex 1.19 if ($self->{path}) {
275     $self->{map}{map}->write_file ($self->{path});
276 elmex 1.22 quick_msg ($self, "saved to $self->{path}");
277     } else {
278     $self->save_map_as;
279 elmex 1.19 }
280 root 1.13 }
281    
282     sub save_map_as {
283     my ($self) = @_;
284 elmex 1.19
285 elmex 1.22 my $fc = $::MAINWIN->new_filechooser ('gce - save map', 1, $self->{path});
286 elmex 1.19
287     if ('ok' eq $fc->run) {
288    
289     $::MAINWIN->{fc_last_folder} = $fc->get_current_folder;
290     $::MAINWIN->{fc_last_folders}->{$self->{fc_last_folder}}++;
291    
292     $self->{map}{map}->write_file ($self->{path} = $fc->get_filename);
293 elmex 1.22 quick_msg ($self, "saved to $self->{path}");
294 elmex 1.19 }
295    
296     $fc->destroy;
297 root 1.13 }
298    
299 elmex 1.17 sub _add_prop_entry {
300     my ($self, $table, $idx, $key, $desc, $type, $changecb) = @_;
301    
302     my $edwid;
303    
304     if ($type eq 'string') {
305     $table->attach_defaults (my $lbl = Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
306     $edwid = Gtk2::Entry->new;
307     $edwid->set_text ($self->{map}{map}{info}{$key});
308     $edwid->signal_connect (changed => sub {
309     $self->{map}{map}{info}{$key} = $_[0]->get_text;
310     if ($changecb) {
311     $changecb->($_[0]->get_text);
312     }
313     });
314     $table->attach_defaults ($edwid, 1, 2, $idx, $idx + 1);
315    
316     } elsif ($type eq 'button') {
317     $table->attach_defaults (my $b = Gtk2::Button->new_with_label ($desc), 0, 2, $idx, $idx + 1);
318     $b->signal_connect (clicked => ($changecb || sub {}));
319    
320     } elsif ($type eq 'label') {
321     $table->attach_defaults (my $lbl = Gtk2::Label->new ($desc), 0, 1, $idx, $idx + 1);
322     $edwid = Gtk2::Label->new ($self->{map}{map}{info}{$key});
323     $table->attach_defaults ($edwid, 1, 2, $idx, $idx + 1);
324    
325     } else {
326     $edwid = Gtk2::Label->new ("FOO");
327     }
328     }
329    
330     sub open_resize_map {
331     my ($self) = @_;
332    
333     my $w = Gtk2::Window->new ('toplevel');
334     $w->set_default_size (250, 150);
335     $w->add (my $sw = Gtk2::ScrolledWindow->new);
336     $sw->add_with_viewport (my $v = Gtk2::VBox->new);
337     $sw->set_policy ('automatic', 'automatic');
338     $v->pack_start (my $t = Gtk2::Table->new (2, 10), 0, 0, 0);
339    
340     my $i = 0;
341     for (
342     [qw/width Width string/],
343     [qw/height Height string/],
344     [qw/save Save button/,
345     sub {
346     $self->{map}{map}->resize ($self->{map}{map}{info}{width}, $self->{map}{map}{info}{height});
347 elmex 1.19 $self->{map}->invalidate_all;
348     $w->destroy;
349 elmex 1.17 }
350     ],
351     )
352     {
353     $self->_add_prop_entry ($t, $i++, @$_);
354     }
355    
356     $w->show_all;
357     }
358    
359     sub open_map_prop {
360     my ($self) = @_;
361    
362    
363     my $w = Gtk2::Window->new ('toplevel');
364     $w->set_default_size (500, 500);
365     $w->add (my $sw = Gtk2::ScrolledWindow->new);
366     $sw->add_with_viewport (my $v = Gtk2::VBox->new);
367     $sw->set_policy ('automatic', 'automatic');
368     $v->pack_start (my $t = Gtk2::Table->new (2, 10), 0, 0, 0);
369    
370     my $i = 0;
371     for (
372     [qw/name Name string/],
373     [qw/region Region string/],
374     [qw/difficulty Difficulty string/],
375     [qw/width Width label/], # sub { $self->{map}{map}->resize ($_[0], $self->{map}{map}{height}) }],
376     [qw/height Height label/],# sub { $self->{map}{map}->resize ($self->{map}{map}{width}, $_[0]) }],
377     [qw/msg Text text/],
378     [qw/tile_path_1 Northpath string/],
379     [qw/tile_path_2 Eastpath string/],
380     [qw/tile_path_3 Southpath string/],
381     [qw/tile_path_4 Westpath string/],
382     [qw/tile_path_5 Toppath string/],
383     [qw/tile_path_6 Bottompath string/],
384     )
385     {
386     $self->_add_prop_entry ($t, $i++, @$_);
387     }
388    
389     $w->show_all;
390     }
391    
392     # 'info' => {
393     # 'windspeed' => '10',
394     # 'outdoor' => '1',
395     # 'width' => '20',
396     # 'pressure' => '8',
397     # 'test' => '',
398     # 'tile_path_2' => 'east',
399     # 'tile_path_3' => 'south',
400     # 'enter_y' => '2',
401     # 'tile_path_6' => 'bottom',
402     # 'enter_x' => '1',
403     # 'tile_path_5' => 'top',
404     # 'darkness' => '4',
405     # 'maplore' => '',
406     # 'sky' => '12',
407     # 'winddir' => '11',
408     # 'unique' => '1',
409     # 'msg' => 'Creator: CF Java FUCK Map Editor
410     #Date: 3/12/2006
411     #',
412     # 'difficulty' => '3',
413     # 'humid' => '9',
414     # 'endmaplore' => '',
415     # 'fixed_resettime' => '1',
416     # 'name' => 'test',
417     # 'region' => 'REGION',
418     # 'height' => '40',
419     # 'reset_timeout' => '6',
420     # '_name' => 'map',
421     # 'swap_time' => '5',
422     # 'temp' => '7',
423     # 'tile_path_4' => 'west',
424     # 'tile_path_1' => 'north'
425     # },
426    
427    
428 elmex 1.1 =head1 AUTHOR
429    
430     Marc Lehmann <schmorp@schmorp.de>
431     http://home.schmorp.de/
432    
433     Robin Redeker <elmex@ta-sa.org>
434     http://www.ta-sa.org/
435    
436     =cut
437     1;
438