ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.65
Committed: Wed Aug 9 13:46:54 2006 UTC (17 years, 10 months ago) by root
Branch: MAIN
Changes since 1.64: +0 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 package GCE::MainWindow;
2    
3     =head1 NAME
4    
5     GCE::MainWindow - the main window class for gce
6    
7     =cut
8    
9 elmex 1.58 use Cwd qw/abs_path getcwd/;
10 elmex 1.1 use Gtk2;
11     use Gtk2::Gdk::Keysyms;
12 elmex 1.2 use Gtk2::SimpleMenu;
13 root 1.7
14     use Crossfire;
15 elmex 1.32 use Crossfire::Map;
16 root 1.7 use Crossfire::MapWidget;
17    
18 elmex 1.2 use GCE::AttrEdit;
19 elmex 1.13 use GCE::MapEditor;
20 elmex 1.15 use GCE::StackView;
21 elmex 1.17 use GCE::EditAction;
22     use GCE::PickWindow;
23 elmex 1.2
24 elmex 1.14 use GCE::AttrTypemap;
25    
26 elmex 1.53 use Glib::Object::Subclass
27 elmex 1.13 Gtk2::Window;
28 elmex 1.2
29 elmex 1.28 use GCE::Util;
30 elmex 1.54 use GCE::DragHelper;
31 elmex 1.28
32 elmex 1.2 use strict;
33    
34 elmex 1.18 # XXX: make a recursive call from save_layout to all (interesting) sub-widgets
35 elmex 1.17 sub save_layout {
36     my ($self) = @_;
37    
38 elmex 1.58 # $main::CFG->{attr_edit_on} = exists $self->{attr_edit} ? 1 : 0;
39 elmex 1.17 $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 elmex 1.37 $main::CFG->{last_folders} = $self->{fc_last_folders};
47    
48 elmex 1.17 $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 elmex 1.59 $self->{attr_edit}->save_layout;
61    
62 root 1.26 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
63 elmex 1.17 }
64    
65     sub load_layout {
66     my ($self) = @_;
67    
68 elmex 1.37 $self->{fc_last_folders} = $main::CFG->{last_folders};
69    
70 elmex 1.58 # $main::CFG->{attr_edit_on}
71     # and $self->show_attr_editor;
72 elmex 1.17
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 elmex 1.59
80     $self->{attr_edit}->load_layout;
81 elmex 1.17 }
82    
83     sub open_map_editor {
84     my ($self, $mapfile) = @_;
85    
86 elmex 1.44 my $mapkey;
87 elmex 1.39 unless (ref $mapfile) {
88 elmex 1.58 # 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 elmex 1.38 }
96    
97 elmex 1.17 # XXX: last_map_window is a dirty trick to get the position and size
98     # for save layout
99    
100 elmex 1.58 if (defined $self->{loaded_maps}->{$mapkey}) {
101     $self->{loaded_maps}->{$mapkey}->get_toplevel->present;
102     return;
103 elmex 1.38 }
104    
105 elmex 1.17 my $w = $self->{last_map_window} = GCE::MapEditor->new;
106 root 1.25
107 elmex 1.53 $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 elmex 1.58 $self->{loaded_maps}->{$mapkey} = $w;
117 elmex 1.38
118 elmex 1.44 $w->open_map ($mapfile, $mapkey);
119 elmex 1.17
120 elmex 1.30 ::set_pos_and_size ($w, $main::CFG->{map_window}, 500, 500, 200, 0);
121 elmex 1.17
122 elmex 1.54 $w->set_edit_tool ($self->{sel_editaction});
123    
124 elmex 1.17 $w->show_all;
125     }
126    
127 elmex 1.47 sub show_help_window {
128     my ($self) = @_;
129    
130     return if defined $self->{help_win};
131 elmex 1.51 require Gtk2::Ex::PodViewer;
132 elmex 1.47 my $w = $self->{help_win} = Gtk2::Window->new;
133     $w->set_title ("gce - help");
134     $w->set_default_size (500, 300);
135 elmex 1.56 $w->signal_connect (destroy => sub {
136 elmex 1.53 $self->{help_win}->hide; $self->{help_win} = undef;
137     0
138     });
139 elmex 1.47 $w->add (my $sw = Gtk2::ScrolledWindow->new);
140 elmex 1.51 $sw->add (my $h = Gtk2::Ex::PodViewer->new);
141 elmex 1.47 $h->load_string ($::DOCUMENTATION);
142     $w->show_all;
143     }
144    
145 elmex 1.17 sub show_stack_view {
146 elmex 1.15 my ($self) = @_;
147    
148     return if defined $self->{sv};
149    
150 elmex 1.17 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
151 elmex 1.15 $w->set_title ('gce - stack view');
152 elmex 1.56 $w->signal_connect (destroy => sub { delete $self->{sv}; 0 });
153 elmex 1.15 $w->add ($self->{sv} = GCE::StackView->new);
154 elmex 1.17
155 elmex 1.30 main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250);
156 elmex 1.17
157 elmex 1.15 $w->show_all;
158     }
159    
160 elmex 1.46 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 elmex 1.52 $w->set_title ("gce - preferences");
167 elmex 1.46 $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 elmex 1.56 $w->signal_connect (destroy => sub { delete $self->{prop_edit}; 0 });
188 elmex 1.46
189     main::set_pos_and_size ($w, $main::CFG->{prop_edit}, 200, 200);
190    
191     $w->show_all;
192     }
193    
194 elmex 1.15 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 elmex 1.17
201 elmex 1.64 main::set_pos_and_size ($w, $main::CFG->{attr_view}, 400, 300, 250, 0);
202 elmex 1.17
203 elmex 1.15 $w->show_all;
204     }
205    
206     sub update_attr_editor {
207 elmex 1.61 my ($self, $ar) = @_;
208    
209     if (ref ($ar) ne 'GCE::ArchRef') { require Carp; Carp::confess ("$ar no ARCHREF!") }
210 elmex 1.15
211 elmex 1.58 $self->{attr_edit}
212     or die "SERIOUS BUG: Couldn't find attribut editor!";
213 elmex 1.15
214 elmex 1.61 $self->{attr_edit}->set_arch ($ar, 1);
215     $self->{attr_edit_win}->set_title ("gce - edit " . $ar->longname);
216 elmex 1.15 }
217    
218     sub update_stack_view {
219 elmex 1.17 my ($self, $mapedit, $x, $y) = @_;
220 elmex 1.15
221     return unless $self->{sv};
222    
223 elmex 1.17 $self->{sv}->set_stack ($mapedit, $x, $y);
224 elmex 1.62
225 elmex 1.15 }
226    
227 elmex 1.17 sub open_pick_window {
228     my ($self, $layout) = @_;
229    
230 root 1.29 # XXX: Yes, also fix this, save _every_ pick window and their positions and their
231 elmex 1.17 # selection
232     my $p = GCE::PickWindow->new ();
233 elmex 1.15
234 elmex 1.17 push @{$self->{open_pick_windows}}, $p;
235    
236     my $idx = (@{$self->{open_pick_windows}}) - 1;
237 elmex 1.15
238 elmex 1.17 $p->signal_connect ('delete-event' => sub {
239     $self->{open_pick_windows}->[$idx] = undef;
240     });
241 elmex 1.10
242 elmex 1.17 if ($layout) {
243 elmex 1.30 main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200);
244 elmex 1.17 }
245 elmex 1.10
246 elmex 1.17 $p->show_all;
247 elmex 1.10
248 elmex 1.17 $p->set_selection ($layout->{selection});
249 elmex 1.10 }
250    
251 elmex 1.13 sub build_menu {
252 elmex 1.1 my ($self) = @_;
253    
254 elmex 1.2 my $menu_tree = [
255     _File => {
256     item_type => '<Branch>',
257     children => [
258     _New => {
259     callback => sub { $self->new_cb },
260     accelerator => '<ctrl>N'
261     },
262     _Open => {
263     callback => sub { $self->open_cb },
264     accelerator => '<ctrl>O'
265     },
266 elmex 1.17 "_Save Layout" => {
267     callback => sub { $self->save_layout },
268     accelerator => '<ctrl>L'
269     },
270 elmex 1.52 "_Preferences" => {
271 elmex 1.46 callback => sub { $self->show_editor_properties },
272     accelerator => "<ctrl>T"
273     },
274 elmex 1.49 _Quit => {
275 elmex 1.2 callback => sub { Gtk2->main_quit },
276     accelerator => '<ctrl>Q'
277 elmex 1.47 },
278 elmex 1.2 ]
279     },
280 root 1.24 _Dialogs => {
281 elmex 1.2 item_type => '<Branch>',
282     children => [
283 elmex 1.17 "_Picker" => {
284     callback => sub { $self->open_pick_window },
285     accelerator => "<ctrl>P"
286     },
287     "_Stack View" => {
288     callback => sub { $self->show_stack_view },
289     accelerator => "<ctrl>V"
290     },
291 elmex 1.2 ]
292 elmex 1.47 },
293 elmex 1.49 _Help => {
294     item_type => '<Branch>',
295     children => [
296 elmex 1.50 _Manual => {
297 elmex 1.49 callback => sub { $self->show_help_window },
298     accelerator => "<ctrl>H"
299     },
300     ]
301     },
302 elmex 1.2 ];
303    
304 elmex 1.13 my $men =
305     Gtk2::SimpleMenu->new (
306     menu_tree => $menu_tree,
307     default_callback => \&default_cb,
308     );
309 elmex 1.10
310 elmex 1.2 $self->add_accel_group ($men->{accel_group});
311    
312 elmex 1.13 return $men->{widget};
313     }
314 elmex 1.10
315 elmex 1.17 sub add_button {
316     my ($self, $table, $plcinfo, $lbl, $cb) = @_;
317    
318     my ($lx, $ly) = @{$plcinfo->{next}};
319    
320     unless ($lx < $plcinfo->{width}) {
321    
322     $ly++;
323     $lx = 0;
324     }
325    
326     $ly < $plcinfo->{height}
327     or die "too many buttons, make table bigger!";
328    
329 elmex 1.42 $table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1);
330 elmex 1.17 $btn->signal_connect (clicked => $cb);
331    
332     $plcinfo->{next} = [$lx + 1, $ly];
333     }
334    
335     sub build_buttons {
336     my ($self) = @_;
337    
338 elmex 1.38 my $tbl = Gtk2::Table->new (2, 4);
339     my $plcinfo = { width => 2, height => 4, next => [0, 0] };
340 elmex 1.17
341 elmex 1.38 $self->{edit_collection}{pick} = GCE::EditAction::Pick->new;
342     $self->{edit_collection}{place} = GCE::EditAction::Place->new;
343     $self->{edit_collection}{erase} = GCE::EditAction::Erase->new;
344     $self->{edit_collection}{select} = GCE::EditAction::Select->new;
345     $self->{edit_collection}{perl} = GCE::EditAction::Perl->new;
346 elmex 1.63 $self->{edit_collection}{connect} = GCE::EditAction::Connect->new;
347 elmex 1.38 $self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new;
348 elmex 1.17
349 elmex 1.33 $self->set_edit_tool ('pick');
350 elmex 1.28
351 elmex 1.42 $self->add_button ($tbl, $plcinfo, "P_ick", sub { $self->set_edit_tool ('pick') });
352     $self->add_button ($tbl, $plcinfo, "_Place", sub { $self->set_edit_tool ('place') });
353     $self->add_button ($tbl, $plcinfo, "_Erase", sub { $self->set_edit_tool ('erase') });
354     $self->add_button ($tbl, $plcinfo, "_Select", sub { $self->set_edit_tool ('select') });
355     $self->add_button ($tbl, $plcinfo, "Eva_l", sub { $self->set_edit_tool ('perl') });
356 elmex 1.63 $self->add_button ($tbl, $plcinfo, "Connec_t", sub { $self->set_edit_tool ('connect') });
357 elmex 1.42 $self->add_button ($tbl, $plcinfo, "_Follow Exit", sub { $self->set_edit_tool ('followexit') });
358 elmex 1.33
359     return $tbl;
360     }
361    
362     sub set_edit_tool {
363     my ($self, $name) = @_;
364    
365     if ($name eq 'pick') {
366 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
367 elmex 1.33 } elsif ($name eq 'place') {
368 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
369 elmex 1.33 } elsif ($name eq 'erase') {
370 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
371 elmex 1.34 } elsif ($name eq 'select') {
372     $self->update_edit_tool ($self->{edit_collection}{select}, "Select");;
373 elmex 1.57 $self->{edit_collection}{select}->update_overlay;
374 elmex 1.35 } elsif ($name eq 'perl') {
375     $self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");;
376 elmex 1.63 } elsif ($name eq 'connect') {
377     $self->update_edit_tool ($self->{edit_collection}{connect}, "Connect");;
378 elmex 1.38 } elsif ($name eq 'followexit') {
379     $self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");;
380 elmex 1.33 }
381 elmex 1.17 }
382 elmex 1.10
383 elmex 1.28 sub update_edit_tool {
384     my ($self, $tool, $name) = @_;
385    
386 elmex 1.57 for (values %{$self->{loaded_maps}}) {
387     $_->{map}->overlay ('selection')
388     }
389    
390 elmex 1.28 $self->{edit_tool}->set_text ($name);
391     $self->{sel_editaction} = $tool;
392    
393     my $widget = $tool->tool_widget;
394    
395     for ($self->{edit_tool_cont}->get_children) {
396     $_->hide;
397     $self->{edit_tool_cont}->remove ($_);
398     }
399    
400 elmex 1.53 $_->set_edit_tool ($self->{sel_editaction}) for (values %{$self->{editors}});
401    
402 elmex 1.28 defined $widget or return;
403    
404     $self->{edit_tool_cont}->add ($widget);
405     $widget->show_all;
406     }
407    
408     sub update_pick_view {
409     my ($self, $arch) = @_;
410    
411     defined $arch->{_face}
412     or $arch = $Crossfire::ARCH{$arch->{_name}};
413    
414     fill_pb_from_arch ($self->{pick_view_pb}, $arch);
415     $self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb});
416    
417 elmex 1.33 $self->{pick_view_btn}->set_label ($arch->{_name});
418 elmex 1.28 }
419    
420 elmex 1.13 sub INIT_INSTANCE {
421     my ($self) = @_;
422 elmex 1.10
423 root 1.24 $::MAINWIN = $self;
424 elmex 1.15
425 root 1.48 $self->set_title ("gce - toolbox");
426 elmex 1.10
427 elmex 1.28 $self->{edit_tool} = Gtk2::Label->new;
428     $self->{edit_tool_cont} = Gtk2::VBox->new;
429    
430 elmex 1.13 $self->add (my $vb = Gtk2::VBox->new);
431     $vb->pack_start ($self->build_menu, 0, 1, 0);
432 elmex 1.28 $vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0);
433    
434     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
435     $vb->pack_start ($self->{edit_tool}, 0, 1, 0);
436    
437     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
438 elmex 1.35 $vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0);
439 elmex 1.10
440 elmex 1.13 # XXX:load $ARGV _cleanly_?
441 root 1.27 $self->open_map_editor ($_)
442     for @ARGV;
443 root 1.11
444 elmex 1.13 $self->signal_connect ('delete-event' => sub {
445     Gtk2->main_quit;
446 elmex 1.10 });
447 elmex 1.17
448 elmex 1.30 ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0);
449 elmex 1.58
450    
451     $self->show_attr_editor;
452 elmex 1.2 }
453    
454     sub new_cb {
455     my ($self) = @_;
456 elmex 1.32
457     my $w = Gtk2::Window->new ('toplevel');
458     my $width = [width => 20];
459     my $height = [height => 20];
460     $w->add (my $tbl = Gtk2::Table->new (2, 3));
461     add_table_widget ($tbl, 0, $width, 'string');
462     add_table_widget ($tbl, 1, $height, 'string');
463     add_table_widget ($tbl, 2, 'new', 'button', sub {
464     if ($width->[1] > 0 and $height->[1] > 0) {
465     my $map = Crossfire::Map->new ($width->[1], $height->[1]);
466 elmex 1.33 $map->resize ($width->[1], $height->[1]);
467 elmex 1.32 $self->open_map_editor ($map);
468     }
469     $w->destroy;
470     1;
471     });
472     add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy });
473     $w->show_all;
474 elmex 1.2 }
475    
476 root 1.25 sub new_filechooser {
477 elmex 1.37 my ($self, $title, $save, $filename) = @_;
478 elmex 1.2
479 elmex 1.33 $title ||= 'gce - open map';
480 root 1.25 my $fc = new Gtk2::FileChooserDialog (
481 elmex 1.33 $title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
482 elmex 1.13 );
483    
484 elmex 1.46 $fc->add_shortcut_folder ($::CFG->{MAPDIR}) if -d $::CFG->{MAPDIR};
485 elmex 1.45 $fc->add_shortcut_folder ($_) for grep { $_ && ($_ ne '') } keys %{$self->{fc_last_folders}};
486 elmex 1.55 $fc->set_current_folder (getcwd);
487 elmex 1.2
488 elmex 1.37 if ($filename) {
489     $fc->set_filename ($filename);
490     }
491    
492 root 1.25 $fc
493     }
494    
495     sub open_cb {
496     my ($self) = @_;
497    
498     my $fc = $self->new_filechooser;
499    
500 elmex 1.2 if ('ok' eq $fc->run) {
501 elmex 1.13
502 elmex 1.10 $self->{fc_last_folder} = $fc->get_current_folder;
503 elmex 1.13 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
504 elmex 1.17
505     $self->open_map_editor ($fc->get_filename);
506 elmex 1.2 }
507    
508     $fc->destroy;
509     }
510    
511 elmex 1.17 sub get_pick {
512     my ($self) = @_;
513    
514 elmex 1.58 $self->{attr_edit}
515     or die "Couldn't find attribute editor! SERIOUS BUG!";
516    
517 elmex 1.17 # XXX: This is just to make sure that this function always returns something
518 elmex 1.61
519     my $ar = $self->{attr_edit}->get_arch;
520     return { _name => 'platinacoin' } unless defined $ar;
521     return $ar->getarch || { _name => 'platinacoin' };
522 elmex 1.17 }
523    
524 elmex 1.1 =head1 AUTHOR
525    
526     Marc Lehmann <schmorp@schmorp.de>
527     http://home.schmorp.de/
528    
529     Robin Redeker <elmex@ta-sa.org>
530     http://www.ta-sa.org/
531    
532     =cut
533     1;
534