ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.37
Committed: Thu Mar 16 11:59:34 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.36: +22 -8 lines
Log Message:
implemented connect tool for exits and some minor improvements in saving

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