ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.74
Committed: Fri Jan 5 19:20:44 2007 UTC (17 years, 5 months ago) by elmex
Branch: MAIN
Changes since 1.73: +12 -6 lines
Log Message:
added map info dialog where you can add logs now with timestamp and username,
which is by default the environment variable USER or a configured string in the
editor preferences/properties.

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