ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.43
Committed: Thu Mar 16 23:58:10 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.42: +1 -3 lines
Log Message:
some minor improvements (also in the eval tool)

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