ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.75
Committed: Sat Jan 6 09:41:17 2007 UTC (17 years, 5 months ago) by elmex
Branch: MAIN
CVS Tags: rel-2_0
Changes since 1.74: +22 -23 lines
Log Message:
improved coordselection dialog

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 ($::CFG->{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 my $w = $self->{attr_edit_win} = Gtk2::Window->new;
216 $w->set_title ("gcrossedit - edit attrs");
217 $w->add ($self->{attr_edit} = GCE::AttrEdit->new);
218
219 main::set_pos_and_size ($w, $main::CFG->{attr_view}, 400, 300, 250, 0);
220
221 $w->show_all;
222 }
223
224 sub update_attr_editor {
225 my ($self, $ar) = @_;
226
227 if (ref ($ar) ne 'GCE::ArchRef') { require Carp; Carp::confess ("$ar no ARCHREF!") }
228
229 $self->{attr_edit}
230 or die "SERIOUS BUG: Couldn't find attribut editor!";
231
232 $self->{attr_edit}->set_arch ($ar, 1);
233 $self->{attr_edit_win}->set_title ("gcrossedit - edit " . $ar->longname);
234 }
235
236 sub update_stack_view {
237 my ($self, $mapedit, $x, $y) = @_;
238
239 return unless $self->{sv};
240
241 $self->{sv}->set_stack ($mapedit, $x, $y);
242
243 }
244
245 sub open_pick_window {
246 my ($self, $layout) = @_;
247
248 # XXX: Yes, also fix this, save _every_ pick window and their positions and their
249 # selection
250 my $p = GCE::PickWindow->new ();
251
252 push @{$self->{open_pick_windows}}, $p;
253
254 my $idx = (@{$self->{open_pick_windows}}) - 1;
255
256 $p->signal_connect ('delete-event' => sub {
257 $self->{open_pick_windows}->[$idx] = undef;
258 });
259
260 if ($layout) {
261 main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200);
262 }
263
264 $p->show_all;
265
266 $p->set_selection ($layout->{selection});
267 }
268
269 sub build_menu {
270 my ($self) = @_;
271
272 my $menu_tree = [
273 _File => {
274 item_type => '<Branch>',
275 children => [
276 _New => {
277 callback => sub { $self->new_cb },
278 accelerator => '<ctrl>N'
279 },
280 _Open => {
281 callback => sub { $self->open_cb },
282 accelerator => '<ctrl>O'
283 },
284 'Open special' => {
285 item_type => '<Branch>',
286 children => [
287 "world map at"=> {
288 callback => sub { $self->open_worldmap_cb },
289 },
290 ]
291 },
292 "_Save Layout" => {
293 callback => sub { $self->save_layout },
294 accelerator => '<ctrl>L'
295 },
296 "_Preferences" => {
297 callback => sub { $self->show_editor_properties },
298 accelerator => "<ctrl>T"
299 },
300 _Quit => {
301 callback => sub { Gtk2->main_quit },
302 accelerator => '<ctrl>Q'
303 },
304 ]
305 },
306 _Dialogs => {
307 item_type => '<Branch>',
308 children => [
309 "_Picker" => {
310 callback => sub { $self->open_pick_window },
311 accelerator => "<ctrl>P"
312 },
313 "_Stack View" => {
314 callback => sub { $self->show_stack_view },
315 accelerator => "<ctrl>V"
316 },
317 ]
318 },
319 _Help => {
320 item_type => '<Branch>',
321 children => [
322 _Manual => {
323 callback => sub { $self->show_help_window },
324 accelerator => "<ctrl>H"
325 },
326 ]
327 },
328 ];
329
330 my $men =
331 Gtk2::SimpleMenu->new (
332 menu_tree => $menu_tree,
333 default_callback => \&default_cb,
334 );
335
336 $self->add_accel_group ($men->{accel_group});
337
338 return $men->{widget};
339 }
340
341 sub add_button {
342 my ($self, $table, $plcinfo, $lbl, $cb) = @_;
343
344 my ($lx, $ly) = @{$plcinfo->{next}};
345
346 unless ($lx < $plcinfo->{width}) {
347
348 $ly++;
349 $lx = 0;
350 }
351
352 $ly < $plcinfo->{height}
353 or die "too many buttons, make table bigger!";
354
355 $table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1);
356 $btn->signal_connect (clicked => $cb);
357
358 $plcinfo->{next} = [$lx + 1, $ly];
359 }
360
361 sub build_buttons {
362 my ($self) = @_;
363
364 my $tbl = Gtk2::Table->new (2, 4);
365 my $plcinfo = { width => 2, height => 4, next => [0, 0] };
366
367 $self->{edit_collection}{pick} = GCE::EditAction::Pick->new;
368 $self->{edit_collection}{place} = GCE::EditAction::Place->new;
369 $self->{edit_collection}{erase} = GCE::EditAction::Erase->new;
370 $self->{edit_collection}{select} = GCE::EditAction::Select->new;
371 $self->{edit_collection}{perl} = GCE::EditAction::Perl->new;
372 $self->{edit_collection}{connect} = GCE::EditAction::Connect->new;
373 $self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new;
374
375 $self->set_edit_tool ('pick');
376
377 $self->add_button ($tbl, $plcinfo, "P_ick", sub { $self->set_edit_tool ('pick') });
378 $self->add_button ($tbl, $plcinfo, "_Place", sub { $self->set_edit_tool ('place') });
379 $self->add_button ($tbl, $plcinfo, "_Erase", sub { $self->set_edit_tool ('erase') });
380 $self->add_button ($tbl, $plcinfo, "_Select", sub { $self->set_edit_tool ('select') });
381 $self->add_button ($tbl, $plcinfo, "Eva_l", sub { $self->set_edit_tool ('perl') });
382 $self->add_button ($tbl, $plcinfo, "Connec_t", sub { $self->set_edit_tool ('connect') });
383 $self->add_button ($tbl, $plcinfo, "_Follow Exit", sub { $self->set_edit_tool ('followexit') });
384
385 return $tbl;
386 }
387
388 sub set_edit_tool {
389 my ($self, $name) = @_;
390
391 if ($name eq 'pick') {
392 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
393 } elsif ($name eq 'place') {
394 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
395 } elsif ($name eq 'erase') {
396 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
397 } elsif ($name eq 'select') {
398 $self->update_edit_tool ($self->{edit_collection}{select}, "Select");;
399 $self->{edit_collection}{select}->update_overlay;
400 } elsif ($name eq 'perl') {
401 $self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");;
402 } elsif ($name eq 'connect') {
403 $self->update_edit_tool ($self->{edit_collection}{connect}, "Connect");;
404 } elsif ($name eq 'followexit') {
405 $self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");;
406 }
407 }
408
409 sub update_edit_tool {
410 my ($self, $tool, $name) = @_;
411
412 for (values %{$self->{loaded_maps}}) {
413 $_->{map}->overlay ('selection')
414 }
415
416 $self->{edit_tool}->set_text ($name);
417 $self->{sel_editaction} = $tool;
418
419 my $widget = $tool->tool_widget;
420
421 for ($self->{edit_tool_cont}->get_children) {
422 $_->hide;
423 $self->{edit_tool_cont}->remove ($_);
424 }
425
426 $_->set_edit_tool ($self->{sel_editaction}) for (values %{$self->{editors}});
427
428 defined $widget or return;
429
430 $self->{edit_tool_cont}->add ($widget);
431 $widget->show_all;
432 }
433
434 sub update_pick_view {
435 my ($self, $arch) = @_;
436
437 defined $arch->{_face}
438 or $arch = $Crossfire::ARCH{$arch->{_name}};
439
440 fill_pb_from_arch ($self->{pick_view_pb}, $arch);
441 $self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb});
442
443 $self->{pick_view_btn}->set_label ($arch->{_name});
444 }
445
446 sub INIT_INSTANCE {
447 my ($self) = @_;
448
449 $::MAINWIN = $self;
450
451 $self->set_title ("gcrossedit - toolbox");
452
453 $self->{edit_tool} = Gtk2::Label->new;
454 $self->{edit_tool_cont} = Gtk2::VBox->new;
455
456 $self->add (my $vb = Gtk2::VBox->new);
457 $vb->pack_start ($self->build_menu, 0, 1, 0);
458 $vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0);
459
460 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
461 $vb->pack_start ($self->{edit_tool}, 0, 1, 0);
462
463 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
464 $vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0);
465
466 # XXX:load $ARGV _cleanly_?
467 $self->open_map_editor ($_)
468 for @ARGV;
469
470 $self->signal_connect ('delete-event' => sub {
471 Gtk2->main_quit;
472 });
473
474 ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0);
475
476
477 $self->show_attr_editor;
478 }
479
480 sub new_cb {
481 my ($self) = @_;
482
483 my $w = Gtk2::Window->new ('toplevel');
484 my $width = [width => 20];
485 my $height = [height => 20];
486 $w->add (my $tbl = Gtk2::Table->new (2, 3));
487 add_table_widget ($tbl, 0, $width, 'string');
488 add_table_widget ($tbl, 1, $height, 'string');
489 add_table_widget ($tbl, 2, 'new', 'button', sub {
490 if ($width->[1] > 0 and $height->[1] > 0) {
491 my $map = Crossfire::Map->new ($width->[1], $height->[1]);
492 $map->{info}->{width} = $width->[1];
493 $map->{info}->{height} = $height->[1];
494 $map->resize ($width->[1], $height->[1]);
495 $self->open_map_editor ($map);
496 }
497 $w->destroy;
498 1;
499 });
500 add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy });
501 $w->show_all;
502 }
503
504 sub new_filechooser {
505 my ($self, $title, $save, $filename) = @_;
506
507 $title ||= 'gcrossedit - open map';
508 my $fc = new Gtk2::FileChooserDialog (
509 $title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
510 );
511
512 $fc->add_shortcut_folder ($::CFG->{MAPDIR}) if -d $::CFG->{MAPDIR};
513 $fc->add_shortcut_folder ($_) for grep { $_ && ($_ ne '') } keys %{$self->{fc_last_folders}};
514 $fc->set_current_folder (getcwd);
515
516 if ($filename) {
517 $fc->set_filename ($filename);
518 }
519
520 $fc
521 }
522
523 sub new_coord_query {
524 my ($self, $finishcb) = @_;
525
526 my $coordhash = { x => 105, y => 115, worldmap => 1, overlay => 0 };
527 my $diag = GCE::HashDialogue->new;
528 $self->{"worldmap_coord_query"} = $diag;
529 $diag->signal_connect (destroy => sub {
530 delete $self->{"worldmap_coord_query"};
531 });
532 $diag->init (
533 layout_name => 'worldmap_coord_query',
534 info => "Open worldmap at ...",
535 dialog_default_size => [ 200, 200, 200, 0 ],
536 title => 'Worldmap coordinate entry',
537 ref_hash => $coordhash,
538 dialog => [
539 [x => 'X Coordinate' => 'spin', sub { (100, 129, 1) }],
540 [y => 'Y Coordinate' => 'spin', sub { (100, 129, 1) }],
541 [worldmap => 'Open worldmap' => 'check'],
542 [overlay => 'Open overlay (CF+)' => 'check'],
543 ],
544 save_button_label => 'open',
545 save_cb => sub {
546 $finishcb->($_[0]);
547 }
548 );
549 $diag->show_all;
550 }
551
552 sub open_worldmap_cb {
553 my ($self) = @_;
554
555 $self->new_coord_query (sub {
556 my ($info) = @_;
557 my ($x, $y) = ($info->{x}, $info->{y});
558 my ($worldmap, $overlay) = ($info->{worldmap}, $info->{overlay});
559 $self->open_map_editor ($::CFG->{MAPDIR} . "/world/world_$x\_$y")
560 if $worldmap;
561 $self->open_map_editor ($::CFG->{MAPDIR} . "/world-overlay/world_$x\_$y")
562 if $overlay;
563 });
564 }
565
566 sub open_cb {
567 my ($self) = @_;
568
569 my $fc = $self->new_filechooser;
570
571 if ('ok' eq $fc->run) {
572
573 $self->{fc_last_folder} = $fc->get_current_folder;
574 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
575
576 $self->open_map_editor ($fc->get_filename);
577 }
578
579 $fc->destroy;
580 }
581
582 sub get_pick {
583 my ($self) = @_;
584
585 $self->{attr_edit}
586 or die "Couldn't find attribute editor! SERIOUS BUG!";
587
588 # XXX: This is just to make sure that this function always returns something
589
590 my $ar = $self->{attr_edit}->get_arch;
591 return { _name => 'platinacoin' } unless defined $ar;
592 return $ar->getarch || { _name => 'platinacoin' };
593 }
594
595 =head1 AUTHOR
596
597 Marc Lehmann <schmorp@schmorp.de>
598 http://home.schmorp.de/
599
600 Robin Redeker <elmex@ta-sa.org>
601 http://www.ta-sa.org/
602
603 =cut
604 1;
605