ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.40
Committed: Thu Mar 16 21:55:03 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.39: +4 -0 lines
Log Message:
implemented error checking for non existant maps

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     use Gtk2;
10     use Gtk2::Gdk::Keysyms;
11 elmex 1.2 use Gtk2::SimpleMenu;
12 root 1.7
13     use Crossfire;
14 elmex 1.32 use Crossfire::Map;
15 root 1.7 use Crossfire::MapWidget;
16    
17 elmex 1.2 use GCE::AttrEdit;
18 elmex 1.13 use GCE::MapEditor;
19 elmex 1.15 use GCE::StackView;
20 elmex 1.17 use GCE::EditAction;
21     use GCE::PickWindow;
22 elmex 1.2
23 elmex 1.14 use GCE::AttrTypemap;
24    
25 elmex 1.2 use Glib::Object::Subclass
26 elmex 1.13 Gtk2::Window;
27 elmex 1.2
28 elmex 1.28 use GCE::Util;
29    
30 elmex 1.2 use strict;
31    
32 elmex 1.18 # XXX: make a recursive call from save_layout to all (interesting) sub-widgets
33 elmex 1.17 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 elmex 1.37 $main::CFG->{last_folder} = $self->{fc_last_folder};
45     $main::CFG->{last_folders} = $self->{fc_last_folders};
46    
47 elmex 1.17 $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 root 1.26 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
60 elmex 1.17 }
61    
62     sub load_layout {
63     my ($self) = @_;
64    
65 elmex 1.37 $self->{fc_last_folder} = $main::CFG->{last_folder};
66     $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.39 unless (ref $mapfile) {
83     unless (File::Spec->file_name_is_absolute ($mapfile)) {
84     $mapfile = File::Spec->rel2abs ($mapfile);
85     }
86 elmex 1.38 }
87    
88 elmex 1.17 # XXX: last_map_window is a dirty trick to get the position and size
89     # for save layout
90    
91 elmex 1.38 unless (ref $mapfile) {
92     if (defined $self->{loaded_maps}->{$mapfile}) {
93     $self->{loaded_maps}->{$mapfile}->get_toplevel->present;
94     return;
95     }
96     }
97    
98 elmex 1.17 my $w = $self->{last_map_window} = GCE::MapEditor->new;
99 root 1.25
100 elmex 1.38 unless (ref $mapfile) {
101 elmex 1.40 unless (-e $mapfile) {
102     quick_msg ("file '$mapfile' does not exist!", 0);
103     return;
104     }
105 elmex 1.38 $self->{loaded_maps}->{$mapfile} = $w;
106     $w->signal_connect ('delete-event' => sub {
107     delete $self->{loaded_maps}->{$mapfile};
108     });
109     }
110    
111 elmex 1.17 $w->open_map ($mapfile);
112    
113 elmex 1.30 ::set_pos_and_size ($w, $main::CFG->{map_window}, 500, 500, 200, 0);
114 elmex 1.17
115     $w->show_all;
116     }
117    
118     sub show_stack_view {
119 elmex 1.15 my ($self) = @_;
120    
121     return if defined $self->{sv};
122    
123 elmex 1.17 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
124 elmex 1.15 $w->set_title ('gce - stack view');
125 root 1.23 $w->signal_connect (delete_event => sub { delete $self->{sv}; 0 });
126 elmex 1.15 $w->add ($self->{sv} = GCE::StackView->new);
127 elmex 1.17
128 elmex 1.30 main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250);
129 elmex 1.17
130 elmex 1.15 $w->show_all;
131     }
132    
133     sub show_attr_editor {
134     my ($self) = @_;
135    
136 elmex 1.17 return if $self->{attr_edit};
137    
138 elmex 1.15 my $w = $self->{attr_edit_win} = Gtk2::Window->new;
139     $w->set_title ("gce - edit attrs");
140     $w->add ($self->{attr_edit} = GCE::AttrEdit->new);
141 root 1.23 $w->signal_connect (delete_event => sub { delete $self->{attr_edit}; 0 });
142 elmex 1.17
143 elmex 1.30 main::set_pos_and_size ($w, $main::CFG->{attr_view}, 200, 200);
144 elmex 1.17
145 elmex 1.15 $w->show_all;
146     }
147    
148     sub update_attr_editor {
149 elmex 1.17 my ($self, $arch, $cb) = @_;
150 elmex 1.15
151     return unless $self->{attr_edit};
152    
153 elmex 1.17 $self->{attr_edit}->set_arch ($arch, $cb);
154 elmex 1.15 $self->{attr_edit_win}->set_title ("gce - edit $arch->{_name}");
155     }
156    
157     sub update_stack_view {
158 elmex 1.17 my ($self, $mapedit, $x, $y) = @_;
159 elmex 1.15
160     return unless $self->{sv};
161    
162 elmex 1.17 $self->{sv}->set_stack ($mapedit, $x, $y);
163 elmex 1.15 }
164    
165 elmex 1.17 sub open_pick_window {
166     my ($self, $layout) = @_;
167    
168 root 1.29 # XXX: Yes, also fix this, save _every_ pick window and their positions and their
169 elmex 1.17 # selection
170     my $p = GCE::PickWindow->new ();
171 elmex 1.15
172 elmex 1.17 push @{$self->{open_pick_windows}}, $p;
173    
174     my $idx = (@{$self->{open_pick_windows}}) - 1;
175 elmex 1.15
176 elmex 1.17 $p->signal_connect ('delete-event' => sub {
177     $self->{open_pick_windows}->[$idx] = undef;
178     });
179 elmex 1.10
180 elmex 1.17 if ($layout) {
181 elmex 1.30 main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200);
182 elmex 1.17 }
183 elmex 1.10
184 elmex 1.17 $p->show_all;
185 elmex 1.10
186 elmex 1.17 $p->set_selection ($layout->{selection});
187 elmex 1.10 }
188    
189 elmex 1.13 sub build_menu {
190 elmex 1.1 my ($self) = @_;
191    
192 elmex 1.2 my $menu_tree = [
193     _File => {
194     item_type => '<Branch>',
195     children => [
196     _New => {
197     callback => sub { $self->new_cb },
198     accelerator => '<ctrl>N'
199     },
200     _Open => {
201     callback => sub { $self->open_cb },
202     accelerator => '<ctrl>O'
203     },
204 elmex 1.17 "_Save Layout" => {
205     callback => sub { $self->save_layout },
206     accelerator => '<ctrl>L'
207     },
208 elmex 1.2 _Quit => {
209     callback => sub { Gtk2->main_quit },
210     accelerator => '<ctrl>Q'
211     }
212     ]
213     },
214 root 1.24 _Dialogs => {
215 elmex 1.2 item_type => '<Branch>',
216     children => [
217 elmex 1.17 "_Attr Editor" => {
218     callback => sub { $self->show_attr_editor },
219     accelerator => "<ctrl>A"
220 elmex 1.2 },
221 elmex 1.17 "_Picker" => {
222     callback => sub { $self->open_pick_window },
223     accelerator => "<ctrl>P"
224     },
225     "_Stack View" => {
226     callback => sub { $self->show_stack_view },
227     accelerator => "<ctrl>V"
228     },
229 elmex 1.2 ]
230 elmex 1.9 }
231 elmex 1.17
232 elmex 1.2 ];
233    
234 elmex 1.13 my $men =
235     Gtk2::SimpleMenu->new (
236     menu_tree => $menu_tree,
237     default_callback => \&default_cb,
238     );
239 elmex 1.10
240 elmex 1.2 $self->add_accel_group ($men->{accel_group});
241    
242 elmex 1.13 return $men->{widget};
243     }
244 elmex 1.10
245 elmex 1.17 sub add_button {
246     my ($self, $table, $plcinfo, $lbl, $cb) = @_;
247    
248     my ($lx, $ly) = @{$plcinfo->{next}};
249    
250     unless ($lx < $plcinfo->{width}) {
251    
252     $ly++;
253     $lx = 0;
254     }
255    
256     $ly < $plcinfo->{height}
257     or die "too many buttons, make table bigger!";
258    
259     $table->attach_defaults (my $btn = Gtk2::Button->new_with_label ($lbl), $lx, $lx + 1, $ly, $ly + 1);
260     $btn->signal_connect (clicked => $cb);
261    
262     $plcinfo->{next} = [$lx + 1, $ly];
263     }
264    
265     sub build_buttons {
266     my ($self) = @_;
267    
268 elmex 1.38 my $tbl = Gtk2::Table->new (2, 4);
269     my $plcinfo = { width => 2, height => 4, next => [0, 0] };
270 elmex 1.17
271 elmex 1.38 $self->{edit_collection}{pick} = GCE::EditAction::Pick->new;
272     $self->{edit_collection}{place} = GCE::EditAction::Place->new;
273     $self->{edit_collection}{erase} = GCE::EditAction::Erase->new;
274     $self->{edit_collection}{select} = GCE::EditAction::Select->new;
275     $self->{edit_collection}{perl} = GCE::EditAction::Perl->new;
276     $self->{edit_collection}{connectexit} = GCE::EditAction::ConnectExit->new;
277     $self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new;
278 elmex 1.17
279 elmex 1.33 $self->set_edit_tool ('pick');
280 elmex 1.28
281 elmex 1.33 $self->add_button ($tbl, $plcinfo, "Pick", sub { $self->set_edit_tool ('pick') });
282     $self->add_button ($tbl, $plcinfo, "Place", sub { $self->set_edit_tool ('place') });
283     $self->add_button ($tbl, $plcinfo, "Erase", sub { $self->set_edit_tool ('erase') });
284 elmex 1.34 $self->add_button ($tbl, $plcinfo, "Select", sub { $self->set_edit_tool ('select') });
285 elmex 1.35 $self->add_button ($tbl, $plcinfo, "Eval", sub { $self->set_edit_tool ('perl') });
286 elmex 1.38 $self->add_button ($tbl, $plcinfo, "Connect Exit", sub { $self->set_edit_tool ('connectexit') });
287     $self->add_button ($tbl, $plcinfo, "Follow Exit", sub { $self->set_edit_tool ('followexit') });
288 elmex 1.33
289     return $tbl;
290     }
291    
292     sub set_edit_tool {
293     my ($self, $name) = @_;
294    
295     if ($name eq 'pick') {
296 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
297 elmex 1.33 } elsif ($name eq 'place') {
298 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
299 elmex 1.33 } elsif ($name eq 'erase') {
300 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
301 elmex 1.34 } elsif ($name eq 'select') {
302     $self->update_edit_tool ($self->{edit_collection}{select}, "Select");;
303 elmex 1.35 } elsif ($name eq 'perl') {
304     $self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");;
305 elmex 1.38 } elsif ($name eq 'connectexit') {
306     $self->update_edit_tool ($self->{edit_collection}{connectexit}, "Connect Exit");;
307     } elsif ($name eq 'followexit') {
308     $self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");;
309 elmex 1.33 }
310 elmex 1.17 }
311 elmex 1.10
312 elmex 1.28 sub update_edit_tool {
313     my ($self, $tool, $name) = @_;
314    
315     $self->{edit_tool}->set_text ($name);
316     $self->{sel_editaction} = $tool;
317    
318     my $widget = $tool->tool_widget;
319    
320     for ($self->{edit_tool_cont}->get_children) {
321     $_->hide;
322     $self->{edit_tool_cont}->remove ($_);
323     }
324    
325     defined $widget or return;
326    
327     $self->{edit_tool_cont}->add ($widget);
328     $widget->show_all;
329     }
330    
331     sub update_pick_view {
332     my ($self, $arch) = @_;
333    
334     defined $arch->{_face}
335     or $arch = $Crossfire::ARCH{$arch->{_name}};
336    
337     fill_pb_from_arch ($self->{pick_view_pb}, $arch);
338     $self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb});
339    
340 elmex 1.33 $self->{pick_view_btn}->set_label ($arch->{_name});
341 elmex 1.28 }
342    
343 elmex 1.13 sub INIT_INSTANCE {
344     my ($self) = @_;
345 elmex 1.10
346 root 1.24 $::MAINWIN = $self;
347 elmex 1.15
348 elmex 1.13 $self->set_title ("gce - main window");
349 elmex 1.10
350 elmex 1.28 $self->{edit_tool} = Gtk2::Label->new;
351     $self->{edit_tool_cont} = Gtk2::VBox->new;
352    
353 elmex 1.13 $self->add (my $vb = Gtk2::VBox->new);
354     $vb->pack_start ($self->build_menu, 0, 1, 0);
355 elmex 1.28
356     $vb->pack_start (my $hb = $self->{pick_view_hb} = Gtk2::HBox->new, 0, 1, 0);
357     $hb->pack_start ($self->{pick_view_img} = Gtk2::Image->new, 0, 1, 0);
358 elmex 1.33 $hb->pack_start ($self->{pick_view_btn} = Gtk2::Button->new, 0, 1, 0);
359     $self->{pick_view_btn}->drag_source_set (['button1_mask'], ['move'],
360     { target => 'STRING', flags => [], info => 'TARGET_STRING' }
361     );
362     $self->{pick_view_btn}->signal_connect (drag_data_get => sub {
363     my ($widget, $context, $data, $info, $time) = @_;
364     $data->set ($data->target, 8, "pick");
365     });
366 elmex 1.36 $self->{pick_view_btn}->signal_connect (clicked => sub {
367     $self->update_attr_editor ($self->{pick_arch});
368     });
369 elmex 1.28 $self->{pick_view_pb} = new_arch_pb ();
370    
371     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
372     $vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0);
373    
374     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
375     $vb->pack_start ($self->{edit_tool}, 0, 1, 0);
376    
377     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
378 elmex 1.35 $vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0);
379 elmex 1.10
380 elmex 1.13 # XXX:load $ARGV _cleanly_?
381 root 1.27 $self->open_map_editor ($_)
382     for @ARGV;
383 root 1.11
384 elmex 1.13 $self->signal_connect ('delete-event' => sub {
385     Gtk2->main_quit;
386 elmex 1.10 });
387 elmex 1.17
388 elmex 1.30 ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0);
389 elmex 1.2 }
390    
391     sub new_cb {
392     my ($self) = @_;
393 elmex 1.32
394     my $w = Gtk2::Window->new ('toplevel');
395     my $width = [width => 20];
396     my $height = [height => 20];
397     $w->add (my $tbl = Gtk2::Table->new (2, 3));
398     add_table_widget ($tbl, 0, $width, 'string');
399     add_table_widget ($tbl, 1, $height, 'string');
400     add_table_widget ($tbl, 2, 'new', 'button', sub {
401     if ($width->[1] > 0 and $height->[1] > 0) {
402     my $map = Crossfire::Map->new ($width->[1], $height->[1]);
403 elmex 1.33 $map->resize ($width->[1], $height->[1]);
404 elmex 1.32 $self->open_map_editor ($map);
405     }
406     $w->destroy;
407     1;
408     });
409     add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy });
410     $w->show_all;
411 elmex 1.2 }
412    
413 root 1.25 sub new_filechooser {
414 elmex 1.37 my ($self, $title, $save, $filename) = @_;
415 elmex 1.2
416 elmex 1.33 $title ||= 'gce - open map';
417 root 1.25 my $fc = new Gtk2::FileChooserDialog (
418 elmex 1.33 $title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
419 elmex 1.13 );
420    
421 root 1.27 $fc->add_shortcut_folder ("$Crossfire::LIB/maps") if -d "$Crossfire::LIB/maps";
422 elmex 1.13 $fc->add_shortcut_folder ($_) for keys %{$self->{fc_last_folders}};
423 elmex 1.10 $fc->set_current_folder ($self->{fc_last_folder} || "$Crossfire::LIB/maps");
424 elmex 1.2
425 elmex 1.37 if ($filename) {
426     $fc->set_filename ($filename);
427     }
428    
429 root 1.25 $fc
430     }
431    
432     sub open_cb {
433     my ($self) = @_;
434    
435     my $fc = $self->new_filechooser;
436    
437 elmex 1.2 if ('ok' eq $fc->run) {
438 elmex 1.13
439 elmex 1.10 $self->{fc_last_folder} = $fc->get_current_folder;
440 elmex 1.13 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
441 elmex 1.17
442     $self->open_map_editor ($fc->get_filename);
443 elmex 1.2 }
444    
445     $fc->destroy;
446     }
447    
448 elmex 1.17 sub set_pick {
449     my ($self, $arch) = @_;
450    
451     $self->{pick_arch} = $arch;
452 elmex 1.28 $self->update_pick_view ($arch);
453 elmex 1.17 }
454    
455     sub get_pick {
456     my ($self) = @_;
457    
458     # XXX: This is just to make sure that this function always returns something
459     return $self->{pick_arch} || { _name => 'platinacoin' };
460     }
461    
462 elmex 1.1 =head1 AUTHOR
463    
464     Marc Lehmann <schmorp@schmorp.de>
465     http://home.schmorp.de/
466    
467     Robin Redeker <elmex@ta-sa.org>
468     http://www.ta-sa.org/
469    
470     =cut
471     1;
472