ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.50
Committed: Mon Mar 20 04:14:28 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.49: +1 -1 lines
Log Message:
renamed help to manual in help menu

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