ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.35
Committed: Thu Mar 16 00:12:06 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.34: +7 -3 lines
Log Message:
added perl eval tool and removed debugging output

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