ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.72
Committed: Fri Jan 5 13:38:07 2007 UTC (17 years, 5 months ago) by elmex
Branch: MAIN
Changes since 1.71: +9 -1 lines
Log Message:
improved error handling a bit when opening maps.

File Contents

# Content
1 package GCE::MainWindow;
2
3 =head1 NAME
4
5 GCE::MainWindow - the main window class for gce
6
7 =cut
8
9 use Cwd qw/abs_path getcwd/;
10 use Gtk2;
11 use Gtk2::Gdk::Keysyms;
12 use Gtk2::SimpleMenu;
13
14 use Crossfire;
15 use Crossfire::Map;
16 use Crossfire::MapWidget;
17
18 use GCE::AttrEdit;
19 use GCE::MapEditor;
20 use GCE::StackView;
21 use GCE::EditAction;
22 use GCE::PickWindow;
23
24 use Glib::Object::Subclass
25 Gtk2::Window;
26
27 use GCE::Util;
28 use GCE::DragHelper;
29
30 use strict;
31
32 # XXX: make a recursive call from save_layout to all (interesting) sub-widgets
33 sub save_layout {
34 my ($self) = @_;
35
36 # $main::CFG->{attr_edit_on} = exists $self->{attr_edit} ? 1 : 0;
37 $main::CFG->{stack_view_on} = exists $self->{sv} ? 1 : 0;
38 $main::CFG->{picker_on} = exists $self->{last_pick_window} ? 1 : 0;
39 $main::CFG->{main_window} = main::get_pos_and_size ($self);
40 $main::CFG->{stack_view} = main::get_pos_and_size ($self->{sv_win}) if $self->{sv_win};
41 $main::CFG->{attr_view} = main::get_pos_and_size ($self->{attr_edit_win}) if $self->{attr_edit_win};
42
43 if ($self->{last_map_window}) {
44 $main::CFG->{map_window} = main::get_pos_and_size ($self->{last_map_window});
45 $self->{last_map_window}->save_layout ();
46 }
47
48 $main::CFG->{last_folders} = $self->{fc_last_folders};
49
50 $main::CFG->{open_pickers} = [];
51
52 for (@{$self->{open_pick_windows}}) {
53
54 next unless defined $_;
55
56 push @{$main::CFG->{open_pickers}}, {
57 p_and_s => main::get_pos_and_size ($_),
58 selection => $_->{last_selection}
59 };
60 }
61
62 $self->{attr_edit}->save_layout;
63
64 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
65 }
66
67 sub load_layout {
68 my ($self) = @_;
69
70 $self->{fc_last_folders} = $main::CFG->{last_folders};
71
72 # $main::CFG->{attr_edit_on}
73 # and $self->show_attr_editor;
74
75 $main::CFG->{stack_view_on}
76 and $self->show_stack_view;
77
78 for (@{$main::CFG->{open_pickers}}) {
79 $self->open_pick_window ($_);
80 }
81
82 $self->{attr_edit}->load_layout;
83 }
84
85 sub open_map_editor {
86 my ($self, $mapfile) = @_;
87
88 my $mapkey;
89 unless (ref $mapfile) {
90 # unless (File::Spec->file_name_is_absolute ($mapfile)) {
91 # $mapfile = File::Spec->rel2abs ($mapfile);
92 # }
93 $mapkey = abs_path ($mapfile);
94 # File::Spec->abs2rel ($mapfile, File::Spec->catfile ($::CFG->{MAPDIR}));
95 } else {
96 $mapkey = "$mapfile";
97 }
98
99 # XXX: last_map_window is a dirty trick to get the position and size
100 # for save layout
101
102 if (defined $self->{loaded_maps}->{$mapkey}) {
103 $self->{loaded_maps}->{$mapkey}->get_toplevel->present;
104 return;
105 }
106
107 my $w = $self->{last_map_window} = GCE::MapEditor->new;
108
109 $self->{editors}->{$w} = $w;
110
111 $w->signal_connect (destroy => sub {
112 my ($w) = @_;
113 $w->close_windows;
114 delete $self->{loaded_maps}->{$w->{mapkey}};
115 delete $self->{editors}->{$w};
116 0;
117 });
118
119 $self->{loaded_maps}->{$mapkey} = $w;
120
121 eval { $w->open_map ($mapfile, $mapkey) };
122 if ($@) {
123 quick_msg ($self, "$@", 1);
124 $w->close_windows;
125 delete $self->{loaded_maps}->{$w->{mapkey}};
126 delete $self->{editors}->{$w};
127 $w->destroy;
128 return;
129 }
130
131 $w->set_edit_tool ($self->{sel_editaction});
132
133 $w->show_all;
134 }
135
136 sub show_help_window {
137 my ($self) = @_;
138
139 return if defined $self->{help_win};
140 require Gtk2::Ex::PodViewer;
141 my $w = $self->{help_win} = Gtk2::Window->new;
142 $w->set_title ("gcrossedit - help");
143 $w->set_default_size (500, 300);
144 $w->signal_connect (destroy => sub {
145 $self->{help_win}->hide; $self->{help_win} = undef;
146 0
147 });
148 $w->add (my $sw = Gtk2::ScrolledWindow->new);
149 $sw->add (my $h = Gtk2::Ex::PodViewer->new);
150 $h->load_string ($::DOCUMENTATION);
151 $w->show_all;
152 }
153
154 sub show_stack_view {
155 my ($self) = @_;
156
157 return if defined $self->{sv};
158
159 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
160 $w->set_title ('gcrossedit - stack view');
161 $w->signal_connect (destroy => sub { delete $self->{sv}; 0 });
162 $w->add ($self->{sv} = GCE::StackView->new);
163
164 main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250);
165
166 $w->show_all;
167 }
168
169 sub show_editor_properties {
170 my ($self) = @_;
171
172 return if $self->{prop_edit};
173
174 my $w = $self->{prop_edit} = Gtk2::Window->new;
175 $w->set_title ("gcrossedit - preferences");
176 $w->add (my $t = Gtk2::Table->new (2, 4));
177 $t->attach_defaults (my $lbl1 = Gtk2::Label->new ("LIBDIR"), 0, 1, 0, 1);
178 $t->attach_defaults (my $lib = Gtk2::Entry->new, 1, 2, 0, 1);
179 $lib->set_text ($::CFG->{LIBDIR});
180 $t->attach_defaults (my $lbl2 = Gtk2::Label->new ("MAPDIR"), 0, 1, 1, 2);
181 $t->attach_defaults (my $map = Gtk2::Entry->new, 1, 2, 1, 2);
182 $map->set_text ($::CFG->{MAPDIR});
183 $t->attach_defaults (my $save = Gtk2::Button->new ('save'), 0, 2, 2, 3);
184 $save->signal_connect (clicked => sub {
185 $::LIBDIR = $::CFG->{LIBDIR} = $lib->get_text;
186 $::MAPDIR = $::CFG->{MAPDIR} = $map->get_text;
187 Crossfire::set_libdir ($::CFG->{LIBDIR});
188 Crossfire::load_archetypes;
189 Crossfire::load_tilecache;
190 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
191 $w->destroy;
192 });
193 $t->attach_defaults (my $close = Gtk2::Button->new ('close'), 0, 2, 3, 4);
194 $close->signal_connect (clicked => sub { $w->destroy });
195
196 $w->signal_connect (destroy => sub { delete $self->{prop_edit}; 0 });
197
198 main::set_pos_and_size ($w, $main::CFG->{prop_edit}, 200, 200);
199
200 $w->show_all;
201 }
202
203 sub show_attr_editor {
204 my ($self) = @_;
205
206 my $w = $self->{attr_edit_win} = Gtk2::Window->new;
207 $w->set_title ("gcrossedit - edit attrs");
208 $w->add ($self->{attr_edit} = GCE::AttrEdit->new);
209
210 main::set_pos_and_size ($w, $main::CFG->{attr_view}, 400, 300, 250, 0);
211
212 $w->show_all;
213 }
214
215 sub update_attr_editor {
216 my ($self, $ar) = @_;
217
218 if (ref ($ar) ne 'GCE::ArchRef') { require Carp; Carp::confess ("$ar no ARCHREF!") }
219
220 $self->{attr_edit}
221 or die "SERIOUS BUG: Couldn't find attribut editor!";
222
223 $self->{attr_edit}->set_arch ($ar, 1);
224 $self->{attr_edit_win}->set_title ("gcrossedit - edit " . $ar->longname);
225 }
226
227 sub update_stack_view {
228 my ($self, $mapedit, $x, $y) = @_;
229
230 return unless $self->{sv};
231
232 $self->{sv}->set_stack ($mapedit, $x, $y);
233
234 }
235
236 sub open_pick_window {
237 my ($self, $layout) = @_;
238
239 # XXX: Yes, also fix this, save _every_ pick window and their positions and their
240 # selection
241 my $p = GCE::PickWindow->new ();
242
243 push @{$self->{open_pick_windows}}, $p;
244
245 my $idx = (@{$self->{open_pick_windows}}) - 1;
246
247 $p->signal_connect ('delete-event' => sub {
248 $self->{open_pick_windows}->[$idx] = undef;
249 });
250
251 if ($layout) {
252 main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200);
253 }
254
255 $p->show_all;
256
257 $p->set_selection ($layout->{selection});
258 }
259
260 sub build_menu {
261 my ($self) = @_;
262
263 my $menu_tree = [
264 _File => {
265 item_type => '<Branch>',
266 children => [
267 _New => {
268 callback => sub { $self->new_cb },
269 accelerator => '<ctrl>N'
270 },
271 _Open => {
272 callback => sub { $self->open_cb },
273 accelerator => '<ctrl>O'
274 },
275 'Open special' => {
276 item_type => '<Branch>',
277 children => [
278 "world map at"=> {
279 callback => sub { $self->open_worldmap_cb },
280 },
281 "world overlay map at (CF+);"=> {
282 callback => sub { $self->open_worldmap_cb ('overlay') },
283 },
284 ]
285 },
286 "_Save Layout" => {
287 callback => sub { $self->save_layout },
288 accelerator => '<ctrl>L'
289 },
290 "_Preferences" => {
291 callback => sub { $self->show_editor_properties },
292 accelerator => "<ctrl>T"
293 },
294 _Quit => {
295 callback => sub { Gtk2->main_quit },
296 accelerator => '<ctrl>Q'
297 },
298 ]
299 },
300 _Dialogs => {
301 item_type => '<Branch>',
302 children => [
303 "_Picker" => {
304 callback => sub { $self->open_pick_window },
305 accelerator => "<ctrl>P"
306 },
307 "_Stack View" => {
308 callback => sub { $self->show_stack_view },
309 accelerator => "<ctrl>V"
310 },
311 ]
312 },
313 _Help => {
314 item_type => '<Branch>',
315 children => [
316 _Manual => {
317 callback => sub { $self->show_help_window },
318 accelerator => "<ctrl>H"
319 },
320 ]
321 },
322 ];
323
324 my $men =
325 Gtk2::SimpleMenu->new (
326 menu_tree => $menu_tree,
327 default_callback => \&default_cb,
328 );
329
330 $self->add_accel_group ($men->{accel_group});
331
332 return $men->{widget};
333 }
334
335 sub add_button {
336 my ($self, $table, $plcinfo, $lbl, $cb) = @_;
337
338 my ($lx, $ly) = @{$plcinfo->{next}};
339
340 unless ($lx < $plcinfo->{width}) {
341
342 $ly++;
343 $lx = 0;
344 }
345
346 $ly < $plcinfo->{height}
347 or die "too many buttons, make table bigger!";
348
349 $table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1);
350 $btn->signal_connect (clicked => $cb);
351
352 $plcinfo->{next} = [$lx + 1, $ly];
353 }
354
355 sub build_buttons {
356 my ($self) = @_;
357
358 my $tbl = Gtk2::Table->new (2, 4);
359 my $plcinfo = { width => 2, height => 4, next => [0, 0] };
360
361 $self->{edit_collection}{pick} = GCE::EditAction::Pick->new;
362 $self->{edit_collection}{place} = GCE::EditAction::Place->new;
363 $self->{edit_collection}{erase} = GCE::EditAction::Erase->new;
364 $self->{edit_collection}{select} = GCE::EditAction::Select->new;
365 $self->{edit_collection}{perl} = GCE::EditAction::Perl->new;
366 $self->{edit_collection}{connect} = GCE::EditAction::Connect->new;
367 $self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new;
368
369 $self->set_edit_tool ('pick');
370
371 $self->add_button ($tbl, $plcinfo, "P_ick", sub { $self->set_edit_tool ('pick') });
372 $self->add_button ($tbl, $plcinfo, "_Place", sub { $self->set_edit_tool ('place') });
373 $self->add_button ($tbl, $plcinfo, "_Erase", sub { $self->set_edit_tool ('erase') });
374 $self->add_button ($tbl, $plcinfo, "_Select", sub { $self->set_edit_tool ('select') });
375 $self->add_button ($tbl, $plcinfo, "Eva_l", sub { $self->set_edit_tool ('perl') });
376 $self->add_button ($tbl, $plcinfo, "Connec_t", sub { $self->set_edit_tool ('connect') });
377 $self->add_button ($tbl, $plcinfo, "_Follow Exit", sub { $self->set_edit_tool ('followexit') });
378
379 return $tbl;
380 }
381
382 sub set_edit_tool {
383 my ($self, $name) = @_;
384
385 if ($name eq 'pick') {
386 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
387 } elsif ($name eq 'place') {
388 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
389 } elsif ($name eq 'erase') {
390 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
391 } elsif ($name eq 'select') {
392 $self->update_edit_tool ($self->{edit_collection}{select}, "Select");;
393 $self->{edit_collection}{select}->update_overlay;
394 } elsif ($name eq 'perl') {
395 $self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");;
396 } elsif ($name eq 'connect') {
397 $self->update_edit_tool ($self->{edit_collection}{connect}, "Connect");;
398 } elsif ($name eq 'followexit') {
399 $self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");;
400 }
401 }
402
403 sub update_edit_tool {
404 my ($self, $tool, $name) = @_;
405
406 for (values %{$self->{loaded_maps}}) {
407 $_->{map}->overlay ('selection')
408 }
409
410 $self->{edit_tool}->set_text ($name);
411 $self->{sel_editaction} = $tool;
412
413 my $widget = $tool->tool_widget;
414
415 for ($self->{edit_tool_cont}->get_children) {
416 $_->hide;
417 $self->{edit_tool_cont}->remove ($_);
418 }
419
420 $_->set_edit_tool ($self->{sel_editaction}) for (values %{$self->{editors}});
421
422 defined $widget or return;
423
424 $self->{edit_tool_cont}->add ($widget);
425 $widget->show_all;
426 }
427
428 sub update_pick_view {
429 my ($self, $arch) = @_;
430
431 defined $arch->{_face}
432 or $arch = $Crossfire::ARCH{$arch->{_name}};
433
434 fill_pb_from_arch ($self->{pick_view_pb}, $arch);
435 $self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb});
436
437 $self->{pick_view_btn}->set_label ($arch->{_name});
438 }
439
440 sub INIT_INSTANCE {
441 my ($self) = @_;
442
443 $::MAINWIN = $self;
444
445 $self->set_title ("gcrossedit - toolbox");
446
447 $self->{edit_tool} = Gtk2::Label->new;
448 $self->{edit_tool_cont} = Gtk2::VBox->new;
449
450 $self->add (my $vb = Gtk2::VBox->new);
451 $vb->pack_start ($self->build_menu, 0, 1, 0);
452 $vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0);
453
454 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
455 $vb->pack_start ($self->{edit_tool}, 0, 1, 0);
456
457 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
458 $vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0);
459
460 # XXX:load $ARGV _cleanly_?
461 $self->open_map_editor ($_)
462 for @ARGV;
463
464 $self->signal_connect ('delete-event' => sub {
465 Gtk2->main_quit;
466 });
467
468 ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0);
469
470
471 $self->show_attr_editor;
472 }
473
474 sub new_cb {
475 my ($self) = @_;
476
477 my $w = Gtk2::Window->new ('toplevel');
478 my $width = [width => 20];
479 my $height = [height => 20];
480 $w->add (my $tbl = Gtk2::Table->new (2, 3));
481 add_table_widget ($tbl, 0, $width, 'string');
482 add_table_widget ($tbl, 1, $height, 'string');
483 add_table_widget ($tbl, 2, 'new', 'button', sub {
484 if ($width->[1] > 0 and $height->[1] > 0) {
485 my $map = Crossfire::Map->new ($width->[1], $height->[1]);
486 $map->{info}->{width} = $width->[1];
487 $map->{info}->{height} = $height->[1];
488 $map->resize ($width->[1], $height->[1]);
489 $self->open_map_editor ($map);
490 }
491 $w->destroy;
492 1;
493 });
494 add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy });
495 $w->show_all;
496 }
497
498 sub new_filechooser {
499 my ($self, $title, $save, $filename) = @_;
500
501 $title ||= 'gcrossedit - open map';
502 my $fc = new Gtk2::FileChooserDialog (
503 $title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
504 );
505
506 $fc->add_shortcut_folder ($::CFG->{MAPDIR}) if -d $::CFG->{MAPDIR};
507 $fc->add_shortcut_folder ($_) for grep { $_ && ($_ ne '') } keys %{$self->{fc_last_folders}};
508 $fc->set_current_folder (getcwd);
509
510 if ($filename) {
511 $fc->set_filename ($filename);
512 }
513
514 $fc
515 }
516
517 sub new_coord_query {
518 my ($self, $mode, $finishcb) = @_;
519
520 my $coordhash = { x => 105, y => 115 };
521 my $diag = GCE::HashDialogue->new;
522 $diag->init (
523 layout_name => 'worldmap_coord_query_'.$mode,
524 info => "Open $mode worldmap at ...",
525 dialog_default_size => [ 200, 200, 200, 0 ],
526 title => 'Worldmap coordinate entry',
527 ref_hash => $coordhash,
528 dialog => [
529 [x => 'X Coordinate' => 'string'],
530 [y => 'Y Coordinate' => 'string'],
531 ],
532 save_button_label => 'open',
533 save_cb => sub {
534 $finishcb->($mode, $_[0]->{x}, $_[0]->{y});
535 }
536 );
537 $diag->show_all;
538 }
539
540 sub open_worldmap_cb {
541 my ($self, $mode) = @_;
542
543 $self->new_coord_query ($mode, sub {
544 my ($mode, $x, $y) = @_;
545 my $map = $::CFG->{MAPDIR}
546 . "/world" . ($mode eq 'overlay' ? '-overlay' : '')
547 . "/world_$x\_$y";
548 $self->open_map_editor ($map);
549 });
550 }
551
552 sub open_cb {
553 my ($self) = @_;
554
555 my $fc = $self->new_filechooser;
556
557 if ('ok' eq $fc->run) {
558
559 $self->{fc_last_folder} = $fc->get_current_folder;
560 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
561
562 $self->open_map_editor ($fc->get_filename);
563 }
564
565 $fc->destroy;
566 }
567
568 sub get_pick {
569 my ($self) = @_;
570
571 $self->{attr_edit}
572 or die "Couldn't find attribute editor! SERIOUS BUG!";
573
574 # XXX: This is just to make sure that this function always returns something
575
576 my $ar = $self->{attr_edit}->get_arch;
577 return { _name => 'platinacoin' } unless defined $ar;
578 return $ar->getarch || { _name => 'platinacoin' };
579 }
580
581 =head1 AUTHOR
582
583 Marc Lehmann <schmorp@schmorp.de>
584 http://home.schmorp.de/
585
586 Robin Redeker <elmex@ta-sa.org>
587 http://www.ta-sa.org/
588
589 =cut
590 1;
591