ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/gde/GCE/MapEditor.pm
Revision: 1.23
Committed: Thu Mar 16 22:19:35 2006 UTC (20 years, 7 months ago) by elmex
Branch: MAIN
Changes since 1.22: +20 -18 lines
Log Message:
implemented type display in attreditor

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