ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.59
Committed: Tue Apr 4 13:05:51 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.58: +4 -0 lines
Log Message:
Implemented a different inventory editor, added context menu to picker to
add stuff to the inventory (drag&drop doesn't work yet, will be done later).

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 GCE::AttrTypemap;
25
26 use Glib::Object::Subclass
27 Gtk2::Window;
28
29 use GCE::Util;
30 use GCE::DragHelper;
31
32 use strict;
33
34 # XXX: make a recursive call from save_layout to all (interesting) sub-widgets
35 sub save_layout {
36 my ($self) = @_;
37
38 # $main::CFG->{attr_edit_on} = exists $self->{attr_edit} ? 1 : 0;
39 $main::CFG->{stack_view_on} = exists $self->{sv} ? 1 : 0;
40 $main::CFG->{picker_on} = exists $self->{last_pick_window} ? 1 : 0;
41 $main::CFG->{main_window} = main::get_pos_and_size ($self);
42 $main::CFG->{map_window} = main::get_pos_and_size ($self->{last_map_window}) if $self->{last_map_window};
43 $main::CFG->{stack_view} = main::get_pos_and_size ($self->{sv_win}) if $self->{sv_win};
44 $main::CFG->{attr_view} = main::get_pos_and_size ($self->{attr_edit_win}) if $self->{attr_edit_win};
45
46 $main::CFG->{last_folders} = $self->{fc_last_folders};
47
48 $main::CFG->{open_pickers} = [];
49
50 for (@{$self->{open_pick_windows}}) {
51
52 next unless defined $_;
53
54 push @{$main::CFG->{open_pickers}}, {
55 p_and_s => main::get_pos_and_size ($_),
56 selection => $_->{last_selection}
57 };
58 }
59
60 $self->{attr_edit}->save_layout;
61
62 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
63 }
64
65 sub load_layout {
66 my ($self) = @_;
67
68 $self->{fc_last_folders} = $main::CFG->{last_folders};
69
70 # $main::CFG->{attr_edit_on}
71 # and $self->show_attr_editor;
72
73 $main::CFG->{stack_view_on}
74 and $self->show_stack_view;
75
76 for (@{$main::CFG->{open_pickers}}) {
77 $self->open_pick_window ($_);
78 }
79
80 $self->{attr_edit}->load_layout;
81 }
82
83 sub open_map_editor {
84 my ($self, $mapfile) = @_;
85
86 my $mapkey;
87 unless (ref $mapfile) {
88 # unless (File::Spec->file_name_is_absolute ($mapfile)) {
89 # $mapfile = File::Spec->rel2abs ($mapfile);
90 # }
91 $mapkey = abs_path ($mapfile);
92 # File::Spec->abs2rel ($mapfile, File::Spec->catfile ($::CFG->{MAPDIR}));
93 } else {
94 $mapkey = "$mapfile";
95 }
96
97 # XXX: last_map_window is a dirty trick to get the position and size
98 # for save layout
99
100 if (defined $self->{loaded_maps}->{$mapkey}) {
101 $self->{loaded_maps}->{$mapkey}->get_toplevel->present;
102 return;
103 }
104
105 my $w = $self->{last_map_window} = GCE::MapEditor->new;
106
107 $self->{editors}->{$w} = $w;
108
109 $w->signal_connect (destroy => sub {
110 my ($w) = @_;
111 delete $self->{loaded_maps}->{$w->{mapkey}};
112 delete $self->{editors}->{$w};
113 0;
114 });
115
116 $self->{loaded_maps}->{$mapkey} = $w;
117
118 $w->open_map ($mapfile, $mapkey);
119
120 ::set_pos_and_size ($w, $main::CFG->{map_window}, 500, 500, 200, 0);
121
122 $w->set_edit_tool ($self->{sel_editaction});
123
124 $w->show_all;
125 }
126
127 sub show_help_window {
128 my ($self) = @_;
129
130 return if defined $self->{help_win};
131 require Gtk2::Ex::PodViewer;
132 my $w = $self->{help_win} = Gtk2::Window->new;
133 $w->set_title ("gce - help");
134 $w->set_default_size (500, 300);
135 $w->signal_connect (destroy => sub {
136 $self->{help_win}->hide; $self->{help_win} = undef;
137 0
138 });
139 $w->add (my $sw = Gtk2::ScrolledWindow->new);
140 $sw->add (my $h = Gtk2::Ex::PodViewer->new);
141 $h->load_string ($::DOCUMENTATION);
142 $w->show_all;
143 }
144
145 sub show_stack_view {
146 my ($self) = @_;
147
148 return if defined $self->{sv};
149
150 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
151 $w->set_title ('gce - stack view');
152 $w->signal_connect (destroy => sub { delete $self->{sv}; 0 });
153 $w->add ($self->{sv} = GCE::StackView->new);
154
155 main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250);
156
157 $w->show_all;
158 }
159
160 sub show_editor_properties {
161 my ($self) = @_;
162
163 return if $self->{prop_edit};
164
165 my $w = $self->{prop_edit} = Gtk2::Window->new;
166 $w->set_title ("gce - preferences");
167 $w->add (my $t = Gtk2::Table->new (2, 4));
168 $t->attach_defaults (my $lbl1 = Gtk2::Label->new ("CROSSFIRE_LIBDIR"), 0, 1, 0, 1);
169 $t->attach_defaults (my $lib = Gtk2::Entry->new, 1, 2, 0, 1);
170 $lib->set_text ($::CFG->{LIBDIR});
171 $t->attach_defaults (my $lbl2 = Gtk2::Label->new ("Map path"), 0, 1, 1, 2);
172 $t->attach_defaults (my $map = Gtk2::Entry->new, 1, 2, 1, 2);
173 $map->set_text ($::CFG->{MAPDIR});
174 $t->attach_defaults (my $save = Gtk2::Button->new ('save'), 0, 2, 2, 3);
175 $save->signal_connect (clicked => sub {
176 $::CFG->{LIBDIR} = $lib->get_text;
177 $::CFG->{MAPDIR} = $map->get_text;
178 Crossfire::set_libdir ($::CFG->{LIBDIR});
179 Crossfire::load_archetypes;
180 Crossfire::load_tilecache;
181 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
182 $w->destroy;
183 });
184 $t->attach_defaults (my $close = Gtk2::Button->new ('close'), 0, 2, 3, 4);
185 $close->signal_connect (clicked => sub { $w->destroy });
186
187 $w->signal_connect (destroy => sub { delete $self->{prop_edit}; 0 });
188
189 main::set_pos_and_size ($w, $main::CFG->{prop_edit}, 200, 200);
190
191 $w->show_all;
192 }
193
194 sub show_attr_editor {
195 my ($self) = @_;
196
197 my $w = $self->{attr_edit_win} = Gtk2::Window->new;
198 $w->set_title ("gce - edit attrs");
199 $w->add ($self->{attr_edit} = GCE::AttrEdit->new);
200 $w->signal_connect (destroy => sub { Gtk2->main_quit; 0 });
201
202 main::set_pos_and_size ($w, $main::CFG->{attr_view}, 200, 200);
203
204 $w->show_all;
205 }
206
207 sub update_attr_editor {
208 my ($self, $arch, $cb) = @_;
209
210 $self->{attr_edit}
211 or die "SERIOUS BUG: Couldn't find attribut editor!";
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 "_Picker" => {
282 callback => sub { $self->open_pick_window },
283 accelerator => "<ctrl>P"
284 },
285 "_Stack View" => {
286 callback => sub { $self->show_stack_view },
287 accelerator => "<ctrl>V"
288 },
289 ]
290 },
291 _Help => {
292 item_type => '<Branch>',
293 children => [
294 _Manual => {
295 callback => sub { $self->show_help_window },
296 accelerator => "<ctrl>H"
297 },
298 ]
299 },
300 ];
301
302 my $men =
303 Gtk2::SimpleMenu->new (
304 menu_tree => $menu_tree,
305 default_callback => \&default_cb,
306 );
307
308 $self->add_accel_group ($men->{accel_group});
309
310 return $men->{widget};
311 }
312
313 sub add_button {
314 my ($self, $table, $plcinfo, $lbl, $cb) = @_;
315
316 my ($lx, $ly) = @{$plcinfo->{next}};
317
318 unless ($lx < $plcinfo->{width}) {
319
320 $ly++;
321 $lx = 0;
322 }
323
324 $ly < $plcinfo->{height}
325 or die "too many buttons, make table bigger!";
326
327 $table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1);
328 $btn->signal_connect (clicked => $cb);
329
330 $plcinfo->{next} = [$lx + 1, $ly];
331 }
332
333 sub build_buttons {
334 my ($self) = @_;
335
336 my $tbl = Gtk2::Table->new (2, 4);
337 my $plcinfo = { width => 2, height => 4, next => [0, 0] };
338
339 $self->{edit_collection}{pick} = GCE::EditAction::Pick->new;
340 $self->{edit_collection}{place} = GCE::EditAction::Place->new;
341 $self->{edit_collection}{erase} = GCE::EditAction::Erase->new;
342 $self->{edit_collection}{select} = GCE::EditAction::Select->new;
343 $self->{edit_collection}{perl} = GCE::EditAction::Perl->new;
344 $self->{edit_collection}{connectexit} = GCE::EditAction::ConnectExit->new;
345 $self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new;
346
347 $self->set_edit_tool ('pick');
348
349 $self->add_button ($tbl, $plcinfo, "P_ick", sub { $self->set_edit_tool ('pick') });
350 $self->add_button ($tbl, $plcinfo, "_Place", sub { $self->set_edit_tool ('place') });
351 $self->add_button ($tbl, $plcinfo, "_Erase", sub { $self->set_edit_tool ('erase') });
352 $self->add_button ($tbl, $plcinfo, "_Select", sub { $self->set_edit_tool ('select') });
353 $self->add_button ($tbl, $plcinfo, "Eva_l", sub { $self->set_edit_tool ('perl') });
354 $self->add_button ($tbl, $plcinfo, "Connect E_xit", sub { $self->set_edit_tool ('connectexit') });
355 $self->add_button ($tbl, $plcinfo, "_Follow Exit", sub { $self->set_edit_tool ('followexit') });
356
357 return $tbl;
358 }
359
360 sub set_edit_tool {
361 my ($self, $name) = @_;
362
363 if ($name eq 'pick') {
364 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
365 } elsif ($name eq 'place') {
366 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
367 } elsif ($name eq 'erase') {
368 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
369 } elsif ($name eq 'select') {
370 $self->update_edit_tool ($self->{edit_collection}{select}, "Select");;
371 $self->{edit_collection}{select}->update_overlay;
372 } elsif ($name eq 'perl') {
373 $self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");;
374 } elsif ($name eq 'connectexit') {
375 $self->update_edit_tool ($self->{edit_collection}{connectexit}, "Connect Exit");;
376 } elsif ($name eq 'followexit') {
377 $self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");;
378 }
379 }
380
381 sub update_edit_tool {
382 my ($self, $tool, $name) = @_;
383
384 for (values %{$self->{loaded_maps}}) {
385 $_->{map}->overlay ('selection')
386 }
387
388 $self->{edit_tool}->set_text ($name);
389 $self->{sel_editaction} = $tool;
390
391 my $widget = $tool->tool_widget;
392
393 for ($self->{edit_tool_cont}->get_children) {
394 $_->hide;
395 $self->{edit_tool_cont}->remove ($_);
396 }
397
398 $_->set_edit_tool ($self->{sel_editaction}) for (values %{$self->{editors}});
399
400 defined $widget or return;
401
402 $self->{edit_tool_cont}->add ($widget);
403 $widget->show_all;
404 }
405
406 sub update_pick_view {
407 my ($self, $arch) = @_;
408
409 defined $arch->{_face}
410 or $arch = $Crossfire::ARCH{$arch->{_name}};
411
412 fill_pb_from_arch ($self->{pick_view_pb}, $arch);
413 $self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb});
414
415 $self->{pick_view_btn}->set_label ($arch->{_name});
416 }
417
418 sub INIT_INSTANCE {
419 my ($self) = @_;
420
421 $::MAINWIN = $self;
422
423 $self->set_title ("gce - toolbox");
424
425 $self->{edit_tool} = Gtk2::Label->new;
426 $self->{edit_tool_cont} = Gtk2::VBox->new;
427
428 $self->add (my $vb = Gtk2::VBox->new);
429 $vb->pack_start ($self->build_menu, 0, 1, 0);
430
431 # $vb->pack_start (my $hb = $self->{pick_view_hb} = Gtk2::HBox->new, 0, 1, 0);
432 # $hb->pack_start ($self->{pick_view_img} = Gtk2::Image->new, 0, 1, 0);
433 # $hb->pack_start ($self->{pick_view_btn} = Gtk2::Button->new, 0, 1, 0);
434 # GCE::DragHelper::set_drag_source (
435 # $self->{pick_view_btn}, arch => sub { { arch => $self->get_pick } }
436 # );
437 # GCE::DragHelper::set_drag_sink (
438 # $self->{pick_view_btn}, arch => sub { $self->set_pick ($_[0]->{arch}) }
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 $self->show_attr_editor;
466 }
467
468 sub new_cb {
469 my ($self) = @_;
470
471 my $w = Gtk2::Window->new ('toplevel');
472 my $width = [width => 20];
473 my $height = [height => 20];
474 $w->add (my $tbl = Gtk2::Table->new (2, 3));
475 add_table_widget ($tbl, 0, $width, 'string');
476 add_table_widget ($tbl, 1, $height, 'string');
477 add_table_widget ($tbl, 2, 'new', 'button', sub {
478 if ($width->[1] > 0 and $height->[1] > 0) {
479 my $map = Crossfire::Map->new ($width->[1], $height->[1]);
480 $map->resize ($width->[1], $height->[1]);
481 $self->open_map_editor ($map);
482 }
483 $w->destroy;
484 1;
485 });
486 add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy });
487 $w->show_all;
488 }
489
490 sub new_filechooser {
491 my ($self, $title, $save, $filename) = @_;
492
493 $title ||= 'gce - open map';
494 my $fc = new Gtk2::FileChooserDialog (
495 $title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
496 );
497
498 $fc->add_shortcut_folder ($::CFG->{MAPDIR}) if -d $::CFG->{MAPDIR};
499 $fc->add_shortcut_folder ($_) for grep { $_ && ($_ ne '') } keys %{$self->{fc_last_folders}};
500 $fc->set_current_folder (getcwd);
501
502 if ($filename) {
503 $fc->set_filename ($filename);
504 }
505
506 $fc
507 }
508
509 sub open_cb {
510 my ($self) = @_;
511
512 my $fc = $self->new_filechooser;
513
514 if ('ok' eq $fc->run) {
515
516 $self->{fc_last_folder} = $fc->get_current_folder;
517 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
518
519 $self->open_map_editor ($fc->get_filename);
520 }
521
522 $fc->destroy;
523 }
524
525 #sub set_pick {
526 # my ($self, $arch) = @_;
527 #
528 # $self->{pick_arch} = $arch;
529 # $self->update_pick_view ($arch);
530 #}
531
532 sub get_pick {
533 my ($self) = @_;
534
535 $self->{attr_edit}
536 or die "Couldn't find attribute editor! SERIOUS BUG!";
537
538 # XXX: This is just to make sure that this function always returns something
539 return $self->{attr_edit}->get_arch || { _name => 'platinacoin' };
540 }
541
542 =head1 AUTHOR
543
544 Marc Lehmann <schmorp@schmorp.de>
545 http://home.schmorp.de/
546
547 Robin Redeker <elmex@ta-sa.org>
548 http://www.ta-sa.org/
549
550 =cut
551 1;
552