ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.32
Committed: Sun Mar 12 23:32:58 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.31: +18 -8 lines
Log Message:
implemented file->new and implemented attredit

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     my $tbl = Gtk2::Table->new (2, 2);
239     my $plcinfo = { width => 1, height => 3, next => [0, 0] };
240    
241 elmex 1.28 $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    
245 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");
246    
247 elmex 1.17 $self->add_button ($tbl, $plcinfo, "Pick", sub {
248 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
249 elmex 1.17 });
250     $self->add_button ($tbl, $plcinfo, "Place", sub {
251 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
252 elmex 1.17 });
253     $self->add_button ($tbl, $plcinfo, "Erase", sub {
254 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
255 elmex 1.17 });
256    
257     return $tbl;
258     }
259 elmex 1.10
260 elmex 1.28 sub update_edit_tool {
261     my ($self, $tool, $name) = @_;
262    
263     $self->{edit_tool}->set_text ($name);
264     $self->{sel_editaction} = $tool;
265    
266     my $widget = $tool->tool_widget;
267    
268     for ($self->{edit_tool_cont}->get_children) {
269     $_->hide;
270     $self->{edit_tool_cont}->remove ($_);
271     }
272    
273     defined $widget or return;
274    
275     $self->{edit_tool_cont}->add ($widget);
276     $widget->show_all;
277     }
278    
279     sub update_pick_view {
280     my ($self, $arch) = @_;
281    
282     defined $arch->{_face}
283     or $arch = $Crossfire::ARCH{$arch->{_name}};
284    
285     $arch->{_face}
286     or warn "Arch $arch->{_name} has no face!";
287     $arch->{_face}
288     or return;
289    
290     fill_pb_from_arch ($self->{pick_view_pb}, $arch);
291     $self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb});
292    
293     $self->{pick_view_lbl}->set_text ($arch->{_name});
294     }
295    
296 elmex 1.13 sub INIT_INSTANCE {
297     my ($self) = @_;
298 elmex 1.10
299 root 1.24 $::MAINWIN = $self;
300 elmex 1.15
301 elmex 1.13 $self->set_title ("gce - main window");
302 elmex 1.10
303 elmex 1.28 $self->{edit_tool} = Gtk2::Label->new;
304     $self->{edit_tool_cont} = Gtk2::VBox->new;
305    
306 elmex 1.13 $self->add (my $vb = Gtk2::VBox->new);
307     $vb->pack_start ($self->build_menu, 0, 1, 0);
308 elmex 1.28
309     $vb->pack_start (my $hb = $self->{pick_view_hb} = Gtk2::HBox->new, 0, 1, 0);
310     $hb->pack_start ($self->{pick_view_img} = Gtk2::Image->new, 0, 1, 0);
311     $hb->pack_start ($self->{pick_view_lbl} = Gtk2::Label->new, 0, 1, 0);
312     $self->{pick_view_pb} = new_arch_pb ();
313    
314     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
315     $vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0);
316    
317     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
318     $vb->pack_start ($self->{edit_tool}, 0, 1, 0);
319    
320     $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
321     $vb->pack_start ($self->{edit_tool_cont}, 0, 1, 0);
322 elmex 1.10
323 elmex 1.13 # XXX:load $ARGV _cleanly_?
324 root 1.27 $self->open_map_editor ($_)
325     for @ARGV;
326 root 1.11
327 elmex 1.13 $self->signal_connect ('delete-event' => sub {
328     Gtk2->main_quit;
329 elmex 1.10 });
330 elmex 1.17
331 elmex 1.30 ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0);
332 elmex 1.2 }
333    
334     sub new_cb {
335     my ($self) = @_;
336 elmex 1.32
337     my $w = Gtk2::Window->new ('toplevel');
338     my $width = [width => 20];
339     my $height = [height => 20];
340     $w->add (my $tbl = Gtk2::Table->new (2, 3));
341     add_table_widget ($tbl, 0, $width, 'string');
342     add_table_widget ($tbl, 1, $height, 'string');
343     add_table_widget ($tbl, 2, 'new', 'button', sub {
344     if ($width->[1] > 0 and $height->[1] > 0) {
345     my $map = Crossfire::Map->new ($width->[1], $height->[1]);
346     $self->open_map_editor ($map);
347     }
348     $w->destroy;
349     1;
350     });
351     add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy });
352     $w->show_all;
353 elmex 1.2 }
354    
355 root 1.25 sub new_filechooser {
356 elmex 1.2 my ($self) = @_;
357    
358 root 1.25 my $fc = new Gtk2::FileChooserDialog (
359 elmex 1.13 'gce - open map', undef, 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
360     );
361    
362 root 1.27 $fc->add_shortcut_folder ("$Crossfire::LIB/maps") if -d "$Crossfire::LIB/maps";
363 elmex 1.13 $fc->add_shortcut_folder ($_) for keys %{$self->{fc_last_folders}};
364 elmex 1.10 $fc->set_current_folder ($self->{fc_last_folder} || "$Crossfire::LIB/maps");
365 elmex 1.2
366 root 1.25 $fc
367     }
368    
369     sub open_cb {
370     my ($self) = @_;
371    
372     my $fc = $self->new_filechooser;
373    
374 elmex 1.2 if ('ok' eq $fc->run) {
375 elmex 1.13
376 elmex 1.10 $self->{fc_last_folder} = $fc->get_current_folder;
377 elmex 1.13 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
378 elmex 1.17
379     $self->open_map_editor ($fc->get_filename);
380 elmex 1.2 }
381    
382     $fc->destroy;
383     }
384    
385 elmex 1.17 sub set_pick {
386     my ($self, $arch) = @_;
387    
388     $self->{pick_arch} = $arch;
389 elmex 1.28 $self->update_pick_view ($arch);
390 elmex 1.17 }
391    
392     sub get_pick {
393     my ($self) = @_;
394    
395     # XXX: This is just to make sure that this function always returns something
396     return $self->{pick_arch} || { _name => 'platinacoin' };
397     }
398    
399 elmex 1.1 =head1 AUTHOR
400    
401     Marc Lehmann <schmorp@schmorp.de>
402     http://home.schmorp.de/
403    
404     Robin Redeker <elmex@ta-sa.org>
405     http://www.ta-sa.org/
406    
407     =cut
408     1;
409