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

# Content
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 use Gtk2::SimpleMenu;
12
13 use Crossfire;
14 use Crossfire::Map;
15 use Crossfire::MapWidget;
16
17 use GCE::AttrEdit;
18 use GCE::MapEditor;
19 use GCE::StackView;
20 use GCE::EditAction;
21 use GCE::PickWindow;
22
23 use GCE::AttrTypemap;
24
25 use Glib::Object::Subclass
26 Gtk2::Window;
27
28 use GCE::Util;
29
30 use strict;
31
32 # XXX: make a recursive call from save_layout to all (interesting) sub-widgets
33 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 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
57 }
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
81 $w->open_map ($mapfile);
82
83 ::set_pos_and_size ($w, $main::CFG->{map_window}, 500, 500, 200, 0);
84
85 $w->show_all;
86 }
87
88 sub show_stack_view {
89 my ($self) = @_;
90
91 return if defined $self->{sv};
92
93 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
94 $w->set_title ('gce - stack view');
95 $w->signal_connect (delete_event => sub { delete $self->{sv}; 0 });
96 $w->add ($self->{sv} = GCE::StackView->new);
97
98 main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250);
99
100 $w->show_all;
101 }
102
103 sub show_attr_editor {
104 my ($self) = @_;
105
106 return if $self->{attr_edit};
107
108 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 $w->signal_connect (delete_event => sub { delete $self->{attr_edit}; 0 });
112
113 main::set_pos_and_size ($w, $main::CFG->{attr_view}, 200, 200);
114
115 $w->show_all;
116 }
117
118 sub update_attr_editor {
119 my ($self, $arch, $cb) = @_;
120
121 return unless $self->{attr_edit};
122
123 $self->{attr_edit}->set_arch ($arch, $cb);
124 $self->{attr_edit_win}->set_title ("gce - edit $arch->{_name}");
125 }
126
127 sub update_stack_view {
128 my ($self, $mapedit, $x, $y) = @_;
129
130 return unless $self->{sv};
131
132 $self->{sv}->set_stack ($mapedit, $x, $y);
133 }
134
135 sub open_pick_window {
136 my ($self, $layout) = @_;
137
138 # XXX: Yes, also fix this, save _every_ pick window and their positions and their
139 # selection
140 my $p = GCE::PickWindow->new ();
141
142 push @{$self->{open_pick_windows}}, $p;
143
144 my $idx = (@{$self->{open_pick_windows}}) - 1;
145
146 $p->signal_connect ('delete-event' => sub {
147 $self->{open_pick_windows}->[$idx] = undef;
148 });
149
150 if ($layout) {
151 main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200);
152 }
153
154 $p->show_all;
155
156 $p->set_selection ($layout->{selection});
157 }
158
159 sub build_menu {
160 my ($self) = @_;
161
162 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 "_Save Layout" => {
175 callback => sub { $self->save_layout },
176 accelerator => '<ctrl>L'
177 },
178 _Quit => {
179 callback => sub { Gtk2->main_quit },
180 accelerator => '<ctrl>Q'
181 }
182 ]
183 },
184 _Dialogs => {
185 item_type => '<Branch>',
186 children => [
187 "_Attr Editor" => {
188 callback => sub { $self->show_attr_editor },
189 accelerator => "<ctrl>A"
190 },
191 "_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 ]
200 }
201
202 ];
203
204 my $men =
205 Gtk2::SimpleMenu->new (
206 menu_tree => $menu_tree,
207 default_callback => \&default_cb,
208 );
209
210 $self->add_accel_group ($men->{accel_group});
211
212 return $men->{widget};
213 }
214
215 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 $self->{edit_collection}{pick} = GCE::EditAction::Pick->new;
242 $self->{edit_collection}{place} = GCE::EditAction::Place->new;
243 $self->{edit_collection}{erase} = GCE::EditAction::Erase->new;
244
245 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");
246
247 $self->add_button ($tbl, $plcinfo, "Pick", sub {
248 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
249 });
250 $self->add_button ($tbl, $plcinfo, "Place", sub {
251 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
252 });
253 $self->add_button ($tbl, $plcinfo, "Erase", sub {
254 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
255 });
256
257 return $tbl;
258 }
259
260 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 sub INIT_INSTANCE {
297 my ($self) = @_;
298
299 $::MAINWIN = $self;
300
301 $self->set_title ("gce - main window");
302
303 $self->{edit_tool} = Gtk2::Label->new;
304 $self->{edit_tool_cont} = Gtk2::VBox->new;
305
306 $self->add (my $vb = Gtk2::VBox->new);
307 $vb->pack_start ($self->build_menu, 0, 1, 0);
308
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
323 # XXX:load $ARGV _cleanly_?
324 $self->open_map_editor ($_)
325 for @ARGV;
326
327 $self->signal_connect ('delete-event' => sub {
328 Gtk2->main_quit;
329 });
330
331 ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0);
332 }
333
334 sub new_cb {
335 my ($self) = @_;
336
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 }
354
355 sub new_filechooser {
356 my ($self) = @_;
357
358 my $fc = new Gtk2::FileChooserDialog (
359 'gce - open map', undef, 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
360 );
361
362 $fc->add_shortcut_folder ("$Crossfire::LIB/maps") if -d "$Crossfire::LIB/maps";
363 $fc->add_shortcut_folder ($_) for keys %{$self->{fc_last_folders}};
364 $fc->set_current_folder ($self->{fc_last_folder} || "$Crossfire::LIB/maps");
365
366 $fc
367 }
368
369 sub open_cb {
370 my ($self) = @_;
371
372 my $fc = $self->new_filechooser;
373
374 if ('ok' eq $fc->run) {
375
376 $self->{fc_last_folder} = $fc->get_current_folder;
377 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
378
379 $self->open_map_editor ($fc->get_filename);
380 }
381
382 $fc->destroy;
383 }
384
385 sub set_pick {
386 my ($self, $arch) = @_;
387
388 $self->{pick_arch} = $arch;
389 $self->update_pick_view ($arch);
390 }
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 =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