ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.79
Committed: Sat Aug 18 10:04:11 2007 UTC (16 years, 10 months ago) by elmex
Branch: MAIN
Changes since 1.78: +7 -0 lines
Log Message:
applied irwiss' patch to bring back the attribute editor once it was closed, thanks!

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