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