ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.17
Committed: Mon Feb 20 13:30:28 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.16: +186 -40 lines
Log Message:
implemented edit actions and layout 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     use Crossfire::MapWidget;
15    
16 elmex 1.2 use GCE::AttrEdit;
17 elmex 1.13 use GCE::MapEditor;
18 elmex 1.15 use GCE::StackView;
19 elmex 1.17 use GCE::EditAction;
20     use GCE::PickWindow;
21 elmex 1.2
22 elmex 1.14 use GCE::AttrTypemap;
23    
24 elmex 1.2 use Glib::Object::Subclass
25 elmex 1.13 Gtk2::Window;
26 elmex 1.2
27     use strict;
28    
29 elmex 1.15 our $MAINWIN;
30    
31 elmex 1.17 sub save_layout {
32     my ($self) = @_;
33    
34     $main::CFG->{attr_edit_on} = exists $self->{attr_edit} ? 1 : 0;
35     $main::CFG->{stack_view_on} = exists $self->{sv} ? 1 : 0;
36     $main::CFG->{picker_on} = exists $self->{last_pick_window} ? 1 : 0;
37     $main::CFG->{main_window} = main::get_pos_and_size ($self);
38     $main::CFG->{map_window} = main::get_pos_and_size ($self->{last_map_window}) if $self->{last_map_window};
39     $main::CFG->{stack_view} = main::get_pos_and_size ($self->{sv_win}) if $self->{sv_win};
40     $main::CFG->{attr_view} = main::get_pos_and_size ($self->{attr_edit_win}) if $self->{attr_edit_win};
41    
42     $main::CFG->{open_pickers} = [];
43    
44     for (@{$self->{open_pick_windows}}) {
45    
46     next unless defined $_;
47    
48     push @{$main::CFG->{open_pickers}}, {
49     p_and_s => main::get_pos_and_size ($_),
50     selection => $_->{last_selection}
51     };
52     }
53    
54     main::write_cfg ($main::VARDIR . '/config');
55     }
56    
57     sub load_layout {
58     my ($self) = @_;
59    
60     $main::CFG->{attr_edit_on}
61     and $self->show_attr_editor;
62    
63     $main::CFG->{stack_view_on}
64     and $self->show_stack_view;
65    
66     for (@{$main::CFG->{open_pickers}}) {
67     $self->open_pick_window ($_);
68     }
69     }
70    
71     sub open_map_editor {
72     my ($self, $mapfile) = @_;
73    
74     # XXX: last_map_window is a dirty trick to get the position and size
75     # for save layout
76    
77     my $w = $self->{last_map_window} = GCE::MapEditor->new;
78     $self->{mapedit} = $w->{mapedit};
79     $w->open_map ($mapfile);
80    
81     main::set_pos_and_size ($w, $main::CFG->{map_window});
82    
83     $w->show_all;
84     # my $w = $self->{last_map_window} = Gtk2::Window->new ('toplevel');
85     # $w->set_title ('gce - map editor');
86     # $w->add (my $mapedit = $self->{mapedit} = new GCE::MapEditor);
87     # $mapedit->open_map ($mapfile);
88     #
89     #
90     # $w->show_all;
91     }
92    
93     sub show_stack_view {
94 elmex 1.15 my ($self) = @_;
95    
96     return if defined $self->{sv};
97    
98 elmex 1.17 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
99 elmex 1.15 $w->set_title ('gce - stack view');
100 elmex 1.17 $w->signal_connect ('delete-event' => sub { delete $self->{sv}; 0 });
101 elmex 1.15 $w->add ($self->{sv} = GCE::StackView->new);
102 elmex 1.17
103     main::set_pos_and_size ($w, $main::CFG->{stack_view});
104    
105 elmex 1.15 $w->show_all;
106     }
107    
108     sub show_attr_editor {
109     my ($self) = @_;
110    
111 elmex 1.17 return if $self->{attr_edit};
112    
113 elmex 1.15 my $w = $self->{attr_edit_win} = Gtk2::Window->new;
114     $w->set_title ("gce - edit attrs");
115     $w->add ($self->{attr_edit} = GCE::AttrEdit->new);
116     $w->signal_connect ('delete-event' => sub { delete $self->{attr_edit}; 0 });
117 elmex 1.17
118     main::set_pos_and_size ($w, $main::CFG->{attr_view});
119    
120 elmex 1.15 $w->show_all;
121     }
122    
123     sub update_attr_editor {
124 elmex 1.17 my ($self, $arch, $cb) = @_;
125 elmex 1.15
126     return unless $self->{attr_edit};
127    
128 elmex 1.17 $self->{attr_edit}->set_arch ($arch, $cb);
129 elmex 1.15 $self->{attr_edit_win}->set_title ("gce - edit $arch->{_name}");
130     }
131    
132     sub update_stack_view {
133 elmex 1.17 my ($self, $mapedit, $x, $y) = @_;
134 elmex 1.15
135     return unless $self->{sv};
136    
137 elmex 1.17 $self->{sv}->set_stack ($mapedit, $x, $y);
138 elmex 1.15 }
139    
140 elmex 1.17 sub open_pick_window {
141     my ($self, $layout) = @_;
142    
143     # XXX: Yes, also fix this, save _every_ pick window and their posistions and their
144     # selection
145     my $p = GCE::PickWindow->new ();
146 elmex 1.15
147 elmex 1.17 push @{$self->{open_pick_windows}}, $p;
148    
149     my $idx = (@{$self->{open_pick_windows}}) - 1;
150 elmex 1.15
151 elmex 1.17 $p->signal_connect ('delete-event' => sub {
152     $self->{open_pick_windows}->[$idx] = undef;
153     });
154 elmex 1.10
155 elmex 1.17 if ($layout) {
156     main::set_pos_and_size ($p, $layout->{p_and_s});
157     }
158 elmex 1.10
159 elmex 1.17 $p->show_all;
160 elmex 1.10
161 elmex 1.17 $p->set_selection ($layout->{selection});
162 elmex 1.10 }
163    
164 elmex 1.13 sub build_menu {
165 elmex 1.1 my ($self) = @_;
166    
167 elmex 1.2 my $menu_tree = [
168     _File => {
169     item_type => '<Branch>',
170     children => [
171     _New => {
172     callback => sub { $self->new_cb },
173     accelerator => '<ctrl>N'
174     },
175     _Open => {
176     callback => sub { $self->open_cb },
177     accelerator => '<ctrl>O'
178     },
179 elmex 1.17 "_Save Layout" => {
180     callback => sub { $self->save_layout },
181     accelerator => '<ctrl>L'
182     },
183 elmex 1.2 _Quit => {
184     callback => sub { Gtk2->main_quit },
185     accelerator => '<ctrl>Q'
186     }
187     ]
188     },
189     _Edit => {
190     item_type => '<Branch>',
191     children => [
192 elmex 1.17 "_Attr Editor" => {
193     callback => sub { $self->show_attr_editor },
194     accelerator => "<ctrl>A"
195 elmex 1.2 },
196 elmex 1.17 "_Picker" => {
197     callback => sub { $self->open_pick_window },
198     accelerator => "<ctrl>P"
199     },
200     "_Stack View" => {
201     callback => sub { $self->show_stack_view },
202     accelerator => "<ctrl>V"
203     },
204    
205     ]
206     },
207     _View => {
208     item_type => '<Branch>',
209     children => [
210 elmex 1.15 "Open _Attr Editor" => {
211     callback => sub { $self->show_attr_editor },
212     accelerator => "<ctrl>A"
213     },
214 elmex 1.9 "Open _Picker" => {
215 elmex 1.13 callback => sub { $self->open_pick_window },
216     accelerator => "<ctrl>P"
217 elmex 1.9 },
218 elmex 1.15 "Open _Stack View" => {
219 elmex 1.17 callback => sub { $self->show_stack_view },
220 elmex 1.15 accelerator => "<ctrl>V"
221     },
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     my $tbl = Gtk2::Table->new (2, 2);
263     my $plcinfo = { width => 1, height => 3, next => [0, 0] };
264    
265     $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    
269     $self->add_button ($tbl, $plcinfo, "Pick", sub {
270     $self->{sel_editaction} = $self->{edit_collection}{pick};
271     $self->{edit_tool}->set_text ("Pick");
272     });
273     $self->add_button ($tbl, $plcinfo, "Place", sub {
274     $self->{sel_editaction} = $self->{edit_collection}{place};
275     $self->{edit_tool}->set_text ("Place");
276     });
277     $self->add_button ($tbl, $plcinfo, "Erase", sub {
278     $self->{sel_editaction} = $self->{edit_collection}{erase};
279     $self->{edit_tool}->set_text ("Erase");
280     });
281    
282     return $tbl;
283     }
284 elmex 1.10
285 root 1.11
286 elmex 1.13 sub INIT_INSTANCE {
287     my ($self) = @_;
288 elmex 1.10
289 elmex 1.15 $MAINWIN = $self;
290    
291 elmex 1.13 $self->set_title ("gce - main window");
292 elmex 1.10
293 elmex 1.13 $self->add (my $vb = Gtk2::VBox->new);
294     $vb->pack_start ($self->build_menu, 0, 1, 0);
295 elmex 1.17 $vb->pack_start (my $tbl = $self->build_buttons, 1, 1, 0);
296     $vb->pack_start ($self->{edit_tool} = Gtk2::Label->new, 0, 1, 0);
297     $vb->pack_start ($self->{pick_view} = Gtk2::Label->new, 0, 1, 0);
298 elmex 1.10
299 elmex 1.13 # XXX:load $ARGV _cleanly_?
300 elmex 1.17 $self->open_map_editor ($ARGV[0] || "$Crossfire::LIB/maps/dragonisland/advguild3");
301 root 1.11
302 elmex 1.13 $self->signal_connect ('delete-event' => sub {
303     Gtk2->main_quit;
304 elmex 1.10 });
305 elmex 1.17
306     main::set_pos_and_size ($self, $main::CFG->{main_window});
307 elmex 1.2 }
308    
309     sub new_cb {
310     my ($self) = @_;
311     die "NOT IMPLEMENTED YET";
312     }
313    
314     sub open_cb {
315     my ($self) = @_;
316    
317 elmex 1.13 my $fc =
318     Gtk2::FileChooserDialog->new (
319     'gce - open map', undef, 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
320     );
321    
322 elmex 1.2 $fc->add_shortcut_folder ("$Crossfire::LIB/maps");
323 elmex 1.13 $fc->add_shortcut_folder ($_) for keys %{$self->{fc_last_folders}};
324 elmex 1.10 $fc->set_current_folder ($self->{fc_last_folder} || "$Crossfire::LIB/maps");
325 elmex 1.2
326     if ('ok' eq $fc->run) {
327 elmex 1.13
328 elmex 1.10 $self->{fc_last_folder} = $fc->get_current_folder;
329 elmex 1.13 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
330 elmex 1.17
331     $self->open_map_editor ($fc->get_filename);
332 elmex 1.2 }
333    
334     $fc->destroy;
335     }
336    
337 elmex 1.17 sub set_pick {
338     my ($self, $arch) = @_;
339    
340     $self->{pick_arch} = $arch;
341     $self->{pick_view}->set_text ($arch->{_name});
342     }
343    
344     sub get_pick {
345     my ($self) = @_;
346    
347     # XXX: This is just to make sure that this function always returns something
348     return $self->{pick_arch} || { _name => 'platinacoin' };
349     }
350    
351 elmex 1.1 =head1 AUTHOR
352    
353     Marc Lehmann <schmorp@schmorp.de>
354     http://home.schmorp.de/
355    
356     Robin Redeker <elmex@ta-sa.org>
357     http://www.ta-sa.org/
358    
359     =cut
360     1;
361