ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.53
Committed: Sun Mar 26 15:41:59 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.52: +16 -3 lines
Log Message:
fixed hopefully some modifier bugs and made code easier

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