ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.55
Committed: Sat Apr 1 18:45:05 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.54: +2 -3 lines
Log Message:
some improvements

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.55 use Cwd;
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     $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 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 root 1.26 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
61 elmex 1.17 }
62    
63     sub load_layout {
64     my ($self) = @_;
65    
66 elmex 1.37 $self->{fc_last_folders} = $main::CFG->{last_folders};
67    
68 elmex 1.17 $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 elmex 1.44 my $mapkey;
83 elmex 1.39 unless (ref $mapfile) {
84     unless (File::Spec->file_name_is_absolute ($mapfile)) {
85     $mapfile = File::Spec->rel2abs ($mapfile);
86     }
87 elmex 1.46 $mapkey = File::Spec->abs2rel ($mapfile, File::Spec->catfile ($::CFG->{MAPDIR}));
88 elmex 1.38 }
89    
90 elmex 1.17 # XXX: last_map_window is a dirty trick to get the position and size
91     # for save layout
92    
93 elmex 1.38 unless (ref $mapfile) {
94 elmex 1.44 if (defined $self->{loaded_maps}->{$mapkey}) {
95     $self->{loaded_maps}->{$mapkey}->get_toplevel->present;
96 elmex 1.38 return;
97     }
98     }
99    
100 elmex 1.17 my $w = $self->{last_map_window} = GCE::MapEditor->new;
101 root 1.25
102 elmex 1.53 $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 elmex 1.38 unless (ref $mapfile) {
112 elmex 1.40 unless (-e $mapfile) {
113     quick_msg ("file '$mapfile' does not exist!", 0);
114     return;
115     }
116 elmex 1.44 $self->{loaded_maps}->{$mapkey} = $w;
117 elmex 1.38 }
118    
119 elmex 1.44 $w->open_map ($mapfile, $mapkey);
120 elmex 1.17
121 elmex 1.30 ::set_pos_and_size ($w, $main::CFG->{map_window}, 500, 500, 200, 0);
122 elmex 1.17
123 elmex 1.54 $w->set_edit_tool ($self->{sel_editaction});
124    
125 elmex 1.17 $w->show_all;
126     }
127    
128 elmex 1.47 sub show_help_window {
129     my ($self) = @_;
130    
131     return if defined $self->{help_win};
132 elmex 1.51 require Gtk2::Ex::PodViewer;
133 elmex 1.47 my $w = $self->{help_win} = Gtk2::Window->new;
134     $w->set_title ("gce - help");
135     $w->set_default_size (500, 300);
136 elmex 1.53 $w->signal_connect (delete_event => sub {
137     $self->{help_win}->hide; $self->{help_win} = undef;
138     0
139     });
140 elmex 1.47 $w->add (my $sw = Gtk2::ScrolledWindow->new);
141 elmex 1.51 $sw->add (my $h = Gtk2::Ex::PodViewer->new);
142 elmex 1.47 $h->load_string ($::DOCUMENTATION);
143     $w->show_all;
144     }
145    
146 elmex 1.17 sub show_stack_view {
147 elmex 1.15 my ($self) = @_;
148    
149     return if defined $self->{sv};
150    
151 elmex 1.17 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
152 elmex 1.15 $w->set_title ('gce - stack view');
153 root 1.23 $w->signal_connect (delete_event => sub { delete $self->{sv}; 0 });
154 elmex 1.15 $w->add ($self->{sv} = GCE::StackView->new);
155 elmex 1.17
156 elmex 1.30 main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250);
157 elmex 1.17
158 elmex 1.15 $w->show_all;
159     }
160    
161 elmex 1.46 sub show_editor_properties {
162     my ($self) = @_;
163    
164     return if $self->{prop_edit};
165    
166     my $w = $self->{prop_edit} = Gtk2::Window->new;
167 elmex 1.52 $w->set_title ("gce - preferences");
168 elmex 1.46 $w->add (my $t = Gtk2::Table->new (2, 4));
169     $t->attach_defaults (my $lbl1 = Gtk2::Label->new ("CROSSFIRE_LIBDIR"), 0, 1, 0, 1);
170     $t->attach_defaults (my $lib = Gtk2::Entry->new, 1, 2, 0, 1);
171     $lib->set_text ($::CFG->{LIBDIR});
172     $t->attach_defaults (my $lbl2 = Gtk2::Label->new ("Map path"), 0, 1, 1, 2);
173     $t->attach_defaults (my $map = Gtk2::Entry->new, 1, 2, 1, 2);
174     $map->set_text ($::CFG->{MAPDIR});
175     $t->attach_defaults (my $save = Gtk2::Button->new ('save'), 0, 2, 2, 3);
176     $save->signal_connect (clicked => sub {
177     $::CFG->{LIBDIR} = $lib->get_text;
178     $::CFG->{MAPDIR} = $map->get_text;
179     Crossfire::set_libdir ($::CFG->{LIBDIR});
180     Crossfire::load_archetypes;
181     Crossfire::load_tilecache;
182     main::write_cfg ("$Crossfire::VARDIR/gceconfig");
183     $w->destroy;
184     });
185     $t->attach_defaults (my $close = Gtk2::Button->new ('close'), 0, 2, 3, 4);
186     $close->signal_connect (clicked => sub { $w->destroy });
187    
188     $w->signal_connect (delete_event => sub { delete $self->{prop_edit}; 0 });
189    
190     main::set_pos_and_size ($w, $main::CFG->{prop_edit}, 200, 200);
191    
192     $w->show_all;
193     }
194    
195 elmex 1.15 sub show_attr_editor {
196     my ($self) = @_;
197    
198 elmex 1.17 return if $self->{attr_edit};
199    
200 elmex 1.15 my $w = $self->{attr_edit_win} = Gtk2::Window->new;
201     $w->set_title ("gce - edit attrs");
202     $w->add ($self->{attr_edit} = GCE::AttrEdit->new);
203 root 1.23 $w->signal_connect (delete_event => sub { delete $self->{attr_edit}; 0 });
204 elmex 1.17
205 elmex 1.30 main::set_pos_and_size ($w, $main::CFG->{attr_view}, 200, 200);
206 elmex 1.17
207 elmex 1.15 $w->show_all;
208     }
209    
210     sub update_attr_editor {
211 elmex 1.17 my ($self, $arch, $cb) = @_;
212 elmex 1.15
213     return unless $self->{attr_edit};
214    
215 elmex 1.17 $self->{attr_edit}->set_arch ($arch, $cb);
216 elmex 1.15 $self->{attr_edit_win}->set_title ("gce - edit $arch->{_name}");
217     }
218    
219     sub update_stack_view {
220 elmex 1.17 my ($self, $mapedit, $x, $y) = @_;
221 elmex 1.15
222     return unless $self->{sv};
223    
224 elmex 1.17 $self->{sv}->set_stack ($mapedit, $x, $y);
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 "_Attr Editor" => {
284     callback => sub { $self->show_attr_editor },
285     accelerator => "<ctrl>A"
286 elmex 1.2 },
287 elmex 1.17 "_Picker" => {
288     callback => sub { $self->open_pick_window },
289     accelerator => "<ctrl>P"
290     },
291     "_Stack View" => {
292     callback => sub { $self->show_stack_view },
293     accelerator => "<ctrl>V"
294     },
295 elmex 1.2 ]
296 elmex 1.47 },
297 elmex 1.49 _Help => {
298     item_type => '<Branch>',
299     children => [
300 elmex 1.50 _Manual => {
301 elmex 1.49 callback => sub { $self->show_help_window },
302     accelerator => "<ctrl>H"
303     },
304     ]
305     },
306 elmex 1.2 ];
307    
308 elmex 1.13 my $men =
309     Gtk2::SimpleMenu->new (
310     menu_tree => $menu_tree,
311     default_callback => \&default_cb,
312     );
313 elmex 1.10
314 elmex 1.2 $self->add_accel_group ($men->{accel_group});
315    
316 elmex 1.13 return $men->{widget};
317     }
318 elmex 1.10
319 elmex 1.17 sub add_button {
320     my ($self, $table, $plcinfo, $lbl, $cb) = @_;
321    
322     my ($lx, $ly) = @{$plcinfo->{next}};
323    
324     unless ($lx < $plcinfo->{width}) {
325    
326     $ly++;
327     $lx = 0;
328     }
329    
330     $ly < $plcinfo->{height}
331     or die "too many buttons, make table bigger!";
332    
333 elmex 1.42 $table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1);
334 elmex 1.17 $btn->signal_connect (clicked => $cb);
335    
336     $plcinfo->{next} = [$lx + 1, $ly];
337     }
338    
339     sub build_buttons {
340     my ($self) = @_;
341    
342 elmex 1.38 my $tbl = Gtk2::Table->new (2, 4);
343     my $plcinfo = { width => 2, height => 4, next => [0, 0] };
344 elmex 1.17
345 elmex 1.38 $self->{edit_collection}{pick} = GCE::EditAction::Pick->new;
346     $self->{edit_collection}{place} = GCE::EditAction::Place->new;
347     $self->{edit_collection}{erase} = GCE::EditAction::Erase->new;
348     $self->{edit_collection}{select} = GCE::EditAction::Select->new;
349     $self->{edit_collection}{perl} = GCE::EditAction::Perl->new;
350     $self->{edit_collection}{connectexit} = GCE::EditAction::ConnectExit->new;
351     $self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new;
352 elmex 1.17
353 elmex 1.33 $self->set_edit_tool ('pick');
354 elmex 1.28
355 elmex 1.42 $self->add_button ($tbl, $plcinfo, "P_ick", sub { $self->set_edit_tool ('pick') });
356     $self->add_button ($tbl, $plcinfo, "_Place", sub { $self->set_edit_tool ('place') });
357     $self->add_button ($tbl, $plcinfo, "_Erase", sub { $self->set_edit_tool ('erase') });
358     $self->add_button ($tbl, $plcinfo, "_Select", sub { $self->set_edit_tool ('select') });
359     $self->add_button ($tbl, $plcinfo, "Eva_l", sub { $self->set_edit_tool ('perl') });
360     $self->add_button ($tbl, $plcinfo, "Connect E_xit", sub { $self->set_edit_tool ('connectexit') });
361     $self->add_button ($tbl, $plcinfo, "_Follow Exit", sub { $self->set_edit_tool ('followexit') });
362 elmex 1.33
363     return $tbl;
364     }
365    
366     sub set_edit_tool {
367     my ($self, $name) = @_;
368    
369     if ($name eq 'pick') {
370 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
371 elmex 1.33 } elsif ($name eq 'place') {
372 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
373 elmex 1.33 } elsif ($name eq 'erase') {
374 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
375 elmex 1.34 } elsif ($name eq 'select') {
376     $self->update_edit_tool ($self->{edit_collection}{select}, "Select");;
377 elmex 1.35 } elsif ($name eq 'perl') {
378     $self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");;
379 elmex 1.38 } elsif ($name eq 'connectexit') {
380     $self->update_edit_tool ($self->{edit_collection}{connectexit}, "Connect Exit");;
381     } elsif ($name eq 'followexit') {
382     $self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");;
383 elmex 1.33 }
384 elmex 1.17 }
385 elmex 1.10
386 elmex 1.28 sub update_edit_tool {
387     my ($self, $tool, $name) = @_;
388    
389     $self->{edit_tool}->set_text ($name);
390     $self->{sel_editaction} = $tool;
391    
392     my $widget = $tool->tool_widget;
393    
394     for ($self->{edit_tool_cont}->get_children) {
395     $_->hide;
396     $self->{edit_tool_cont}->remove ($_);
397     }
398    
399 elmex 1.53 $_->set_edit_tool ($self->{sel_editaction}) for (values %{$self->{editors}});
400    
401 elmex 1.28 defined $widget or return;
402    
403     $self->{edit_tool_cont}->add ($widget);
404     $widget->show_all;
405     }
406    
407     sub update_pick_view {
408     my ($self, $arch) = @_;
409    
410     defined $arch->{_face}
411     or $arch = $Crossfire::ARCH{$arch->{_name}};
412    
413     fill_pb_from_arch ($self->{pick_view_pb}, $arch);
414     $self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb});
415    
416 elmex 1.33 $self->{pick_view_btn}->set_label ($arch->{_name});
417 elmex 1.28 }
418    
419 elmex 1.13 sub INIT_INSTANCE {
420     my ($self) = @_;
421 elmex 1.10
422 root 1.24 $::MAINWIN = $self;
423 elmex 1.15
424 root 1.48 $self->set_title ("gce - toolbox");
425 elmex 1.10
426 elmex 1.28 $self->{edit_tool} = Gtk2::Label->new;
427     $self->{edit_tool_cont} = Gtk2::VBox->new;
428    
429 elmex 1.13 $self->add (my $vb = Gtk2::VBox->new);
430     $vb->pack_start ($self->build_menu, 0, 1, 0);
431 elmex 1.28
432     $vb->pack_start (my $hb = $self->{pick_view_hb} = Gtk2::HBox->new, 0, 1, 0);
433     $hb->pack_start ($self->{pick_view_img} = Gtk2::Image->new, 0, 1, 0);
434 elmex 1.33 $hb->pack_start ($self->{pick_view_btn} = Gtk2::Button->new, 0, 1, 0);
435 elmex 1.54 GCE::DragHelper::set_drag_source (
436     $self->{pick_view_btn}, arch => sub { { arch => $self->get_pick } }
437     );
438     GCE::DragHelper::set_drag_sink (
439     $self->{pick_view_btn}, arch => sub { $self->set_pick ($_[0]->{arch}) }
440 elmex 1.33 );
441 elmex 1.36 $self->{pick_view_btn}->signal_connect (clicked => sub {
442     $self->update_attr_editor ($self->{pick_arch});
443     });
444 elmex 1.28 $self->{pick_view_pb} = new_arch_pb ();
445    
446     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
447     $vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0);
448    
449     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
450     $vb->pack_start ($self->{edit_tool}, 0, 1, 0);
451    
452     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
453 elmex 1.35 $vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0);
454 elmex 1.10
455 elmex 1.13 # XXX:load $ARGV _cleanly_?
456 root 1.27 $self->open_map_editor ($_)
457     for @ARGV;
458 root 1.11
459 elmex 1.13 $self->signal_connect ('delete-event' => sub {
460     Gtk2->main_quit;
461 elmex 1.10 });
462 elmex 1.17
463 elmex 1.30 ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0);
464 elmex 1.2 }
465    
466     sub new_cb {
467     my ($self) = @_;
468 elmex 1.32
469     my $w = Gtk2::Window->new ('toplevel');
470     my $width = [width => 20];
471     my $height = [height => 20];
472     $w->add (my $tbl = Gtk2::Table->new (2, 3));
473     add_table_widget ($tbl, 0, $width, 'string');
474     add_table_widget ($tbl, 1, $height, 'string');
475     add_table_widget ($tbl, 2, 'new', 'button', sub {
476     if ($width->[1] > 0 and $height->[1] > 0) {
477     my $map = Crossfire::Map->new ($width->[1], $height->[1]);
478 elmex 1.33 $map->resize ($width->[1], $height->[1]);
479 elmex 1.32 $self->open_map_editor ($map);
480     }
481     $w->destroy;
482     1;
483     });
484     add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy });
485     $w->show_all;
486 elmex 1.2 }
487    
488 root 1.25 sub new_filechooser {
489 elmex 1.37 my ($self, $title, $save, $filename) = @_;
490 elmex 1.2
491 elmex 1.33 $title ||= 'gce - open map';
492 root 1.25 my $fc = new Gtk2::FileChooserDialog (
493 elmex 1.33 $title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
494 elmex 1.13 );
495    
496 elmex 1.46 $fc->add_shortcut_folder ($::CFG->{MAPDIR}) if -d $::CFG->{MAPDIR};
497 elmex 1.45 $fc->add_shortcut_folder ($_) for grep { $_ && ($_ ne '') } keys %{$self->{fc_last_folders}};
498 elmex 1.55 $fc->set_current_folder (getcwd);
499 elmex 1.2
500 elmex 1.37 if ($filename) {
501     $fc->set_filename ($filename);
502     }
503    
504 root 1.25 $fc
505     }
506    
507     sub open_cb {
508     my ($self) = @_;
509    
510     my $fc = $self->new_filechooser;
511    
512 elmex 1.2 if ('ok' eq $fc->run) {
513 elmex 1.13
514 elmex 1.10 $self->{fc_last_folder} = $fc->get_current_folder;
515 elmex 1.13 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
516 elmex 1.17
517     $self->open_map_editor ($fc->get_filename);
518 elmex 1.2 }
519    
520     $fc->destroy;
521     }
522    
523 elmex 1.17 sub set_pick {
524     my ($self, $arch) = @_;
525    
526     $self->{pick_arch} = $arch;
527 elmex 1.28 $self->update_pick_view ($arch);
528 elmex 1.17 }
529    
530     sub get_pick {
531     my ($self) = @_;
532    
533     # XXX: This is just to make sure that this function always returns something
534     return $self->{pick_arch} || { _name => 'platinacoin' };
535     }
536    
537 elmex 1.1 =head1 AUTHOR
538    
539     Marc Lehmann <schmorp@schmorp.de>
540     http://home.schmorp.de/
541    
542     Robin Redeker <elmex@ta-sa.org>
543     http://www.ta-sa.org/
544    
545     =cut
546     1;
547