1 |
package GCE::MainWindow; |
2 |
|
3 |
=head1 NAME |
4 |
|
5 |
GCE::MainWindow - the main window class for gce |
6 |
|
7 |
=cut |
8 |
|
9 |
use Cwd qw/abs_path getcwd/; |
10 |
use Gtk2; |
11 |
use Gtk2::Gdk::Keysyms; |
12 |
use Gtk2::SimpleMenu; |
13 |
|
14 |
use Crossfire; |
15 |
use Crossfire::Map; |
16 |
use Crossfire::MapWidget; |
17 |
|
18 |
use GCE::AttrEdit; |
19 |
use GCE::MapEditor; |
20 |
use GCE::StackView; |
21 |
use GCE::EditAction; |
22 |
use GCE::PickWindow; |
23 |
|
24 |
use GCE::AttrTypemap; |
25 |
|
26 |
use Glib::Object::Subclass |
27 |
Gtk2::Window; |
28 |
|
29 |
use GCE::Util; |
30 |
use GCE::DragHelper; |
31 |
|
32 |
use strict; |
33 |
|
34 |
# XXX: make a recursive call from save_layout to all (interesting) sub-widgets |
35 |
sub save_layout { |
36 |
my ($self) = @_; |
37 |
|
38 |
# $main::CFG->{attr_edit_on} = exists $self->{attr_edit} ? 1 : 0; |
39 |
$main::CFG->{stack_view_on} = exists $self->{sv} ? 1 : 0; |
40 |
$main::CFG->{picker_on} = exists $self->{last_pick_window} ? 1 : 0; |
41 |
$main::CFG->{main_window} = main::get_pos_and_size ($self); |
42 |
$main::CFG->{map_window} = main::get_pos_and_size ($self->{last_map_window}) if $self->{last_map_window}; |
43 |
$main::CFG->{stack_view} = main::get_pos_and_size ($self->{sv_win}) if $self->{sv_win}; |
44 |
$main::CFG->{attr_view} = main::get_pos_and_size ($self->{attr_edit_win}) if $self->{attr_edit_win}; |
45 |
|
46 |
$main::CFG->{last_folders} = $self->{fc_last_folders}; |
47 |
|
48 |
$main::CFG->{open_pickers} = []; |
49 |
|
50 |
for (@{$self->{open_pick_windows}}) { |
51 |
|
52 |
next unless defined $_; |
53 |
|
54 |
push @{$main::CFG->{open_pickers}}, { |
55 |
p_and_s => main::get_pos_and_size ($_), |
56 |
selection => $_->{last_selection} |
57 |
}; |
58 |
} |
59 |
|
60 |
$self->{attr_edit}->save_layout; |
61 |
|
62 |
main::write_cfg ("$Crossfire::VARDIR/gceconfig"); |
63 |
} |
64 |
|
65 |
sub load_layout { |
66 |
my ($self) = @_; |
67 |
|
68 |
$self->{fc_last_folders} = $main::CFG->{last_folders}; |
69 |
|
70 |
# $main::CFG->{attr_edit_on} |
71 |
# and $self->show_attr_editor; |
72 |
|
73 |
$main::CFG->{stack_view_on} |
74 |
and $self->show_stack_view; |
75 |
|
76 |
for (@{$main::CFG->{open_pickers}}) { |
77 |
$self->open_pick_window ($_); |
78 |
} |
79 |
|
80 |
$self->{attr_edit}->load_layout; |
81 |
} |
82 |
|
83 |
sub open_map_editor { |
84 |
my ($self, $mapfile) = @_; |
85 |
|
86 |
my $mapkey; |
87 |
unless (ref $mapfile) { |
88 |
# unless (File::Spec->file_name_is_absolute ($mapfile)) { |
89 |
# $mapfile = File::Spec->rel2abs ($mapfile); |
90 |
# } |
91 |
$mapkey = abs_path ($mapfile); |
92 |
# File::Spec->abs2rel ($mapfile, File::Spec->catfile ($::CFG->{MAPDIR})); |
93 |
} else { |
94 |
$mapkey = "$mapfile"; |
95 |
} |
96 |
|
97 |
# XXX: last_map_window is a dirty trick to get the position and size |
98 |
# for save layout |
99 |
|
100 |
if (defined $self->{loaded_maps}->{$mapkey}) { |
101 |
$self->{loaded_maps}->{$mapkey}->get_toplevel->present; |
102 |
return; |
103 |
} |
104 |
|
105 |
my $w = $self->{last_map_window} = GCE::MapEditor->new; |
106 |
|
107 |
$self->{editors}->{$w} = $w; |
108 |
|
109 |
$w->signal_connect (destroy => sub { |
110 |
my ($w) = @_; |
111 |
$w->close_windows; |
112 |
delete $self->{loaded_maps}->{$w->{mapkey}}; |
113 |
delete $self->{editors}->{$w}; |
114 |
0; |
115 |
}); |
116 |
|
117 |
$self->{loaded_maps}->{$mapkey} = $w; |
118 |
|
119 |
$w->open_map ($mapfile, $mapkey); |
120 |
|
121 |
::set_pos_and_size ($w, $main::CFG->{map_window}, 500, 500, 200, 0); |
122 |
|
123 |
$w->set_edit_tool ($self->{sel_editaction}); |
124 |
|
125 |
$w->show_all; |
126 |
} |
127 |
|
128 |
sub show_help_window { |
129 |
my ($self) = @_; |
130 |
|
131 |
return if defined $self->{help_win}; |
132 |
require Gtk2::Ex::PodViewer; |
133 |
my $w = $self->{help_win} = Gtk2::Window->new; |
134 |
$w->set_title ("gcrossedit - help"); |
135 |
$w->set_default_size (500, 300); |
136 |
$w->signal_connect (destroy => sub { |
137 |
$self->{help_win}->hide; $self->{help_win} = undef; |
138 |
0 |
139 |
}); |
140 |
$w->add (my $sw = Gtk2::ScrolledWindow->new); |
141 |
$sw->add (my $h = Gtk2::Ex::PodViewer->new); |
142 |
$h->load_string ($::DOCUMENTATION); |
143 |
$w->show_all; |
144 |
} |
145 |
|
146 |
sub show_stack_view { |
147 |
my ($self) = @_; |
148 |
|
149 |
return if defined $self->{sv}; |
150 |
|
151 |
my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel'); |
152 |
$w->set_title ('gcrossedit - stack view'); |
153 |
$w->signal_connect (destroy => sub { delete $self->{sv}; 0 }); |
154 |
$w->add ($self->{sv} = GCE::StackView->new); |
155 |
|
156 |
main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250); |
157 |
|
158 |
$w->show_all; |
159 |
} |
160 |
|
161 |
sub show_editor_properties { |
162 |
my ($self) = @_; |
163 |
|
164 |
return if $self->{prop_edit}; |
165 |
|
166 |
my $w = $self->{prop_edit} = Gtk2::Window->new; |
167 |
$w->set_title ("gcrossedit - preferences"); |
168 |
$w->add (my $t = Gtk2::Table->new (2, 4)); |
169 |
$t->attach_defaults (my $lbl1 = Gtk2::Label->new ("LIBDIR"), 0, 1, 0, 1); |
170 |
$t->attach_defaults (my $lib = Gtk2::Entry->new, 1, 2, 0, 1); |
171 |
$lib->set_text ($::CFG->{LIBDIR}); |
172 |
$t->attach_defaults (my $lbl2 = Gtk2::Label->new ("MAPDIR"), 0, 1, 1, 2); |
173 |
$t->attach_defaults (my $map = Gtk2::Entry->new, 1, 2, 1, 2); |
174 |
$map->set_text ($::CFG->{MAPDIR}); |
175 |
$t->attach_defaults (my $save = Gtk2::Button->new ('save'), 0, 2, 2, 3); |
176 |
$save->signal_connect (clicked => sub { |
177 |
$::LIBDIR = $::CFG->{LIBDIR} = $lib->get_text; |
178 |
$::MAPDIR = $::CFG->{MAPDIR} = $map->get_text; |
179 |
Crossfire::set_libdir ($::CFG->{LIBDIR}); |
180 |
Crossfire::load_archetypes; |
181 |
Crossfire::load_tilecache; |
182 |
main::write_cfg ("$Crossfire::VARDIR/gceconfig"); |
183 |
$w->destroy; |
184 |
}); |
185 |
$t->attach_defaults (my $close = Gtk2::Button->new ('close'), 0, 2, 3, 4); |
186 |
$close->signal_connect (clicked => sub { $w->destroy }); |
187 |
|
188 |
$w->signal_connect (destroy => sub { delete $self->{prop_edit}; 0 }); |
189 |
|
190 |
main::set_pos_and_size ($w, $main::CFG->{prop_edit}, 200, 200); |
191 |
|
192 |
$w->show_all; |
193 |
} |
194 |
|
195 |
sub show_attr_editor { |
196 |
my ($self) = @_; |
197 |
|
198 |
my $w = $self->{attr_edit_win} = Gtk2::Window->new; |
199 |
$w->set_title ("gcrossedit - edit attrs"); |
200 |
$w->add ($self->{attr_edit} = GCE::AttrEdit->new); |
201 |
|
202 |
main::set_pos_and_size ($w, $main::CFG->{attr_view}, 400, 300, 250, 0); |
203 |
|
204 |
$w->show_all; |
205 |
} |
206 |
|
207 |
sub update_attr_editor { |
208 |
my ($self, $ar) = @_; |
209 |
|
210 |
if (ref ($ar) ne 'GCE::ArchRef') { require Carp; Carp::confess ("$ar no ARCHREF!") } |
211 |
|
212 |
$self->{attr_edit} |
213 |
or die "SERIOUS BUG: Couldn't find attribut editor!"; |
214 |
|
215 |
$self->{attr_edit}->set_arch ($ar, 1); |
216 |
$self->{attr_edit_win}->set_title ("gcrossedit - edit " . $ar->longname); |
217 |
} |
218 |
|
219 |
sub update_stack_view { |
220 |
my ($self, $mapedit, $x, $y) = @_; |
221 |
|
222 |
return unless $self->{sv}; |
223 |
|
224 |
$self->{sv}->set_stack ($mapedit, $x, $y); |
225 |
|
226 |
} |
227 |
|
228 |
sub open_pick_window { |
229 |
my ($self, $layout) = @_; |
230 |
|
231 |
# XXX: Yes, also fix this, save _every_ pick window and their positions and their |
232 |
# selection |
233 |
my $p = GCE::PickWindow->new (); |
234 |
|
235 |
push @{$self->{open_pick_windows}}, $p; |
236 |
|
237 |
my $idx = (@{$self->{open_pick_windows}}) - 1; |
238 |
|
239 |
$p->signal_connect ('delete-event' => sub { |
240 |
$self->{open_pick_windows}->[$idx] = undef; |
241 |
}); |
242 |
|
243 |
if ($layout) { |
244 |
main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200); |
245 |
} |
246 |
|
247 |
$p->show_all; |
248 |
|
249 |
$p->set_selection ($layout->{selection}); |
250 |
} |
251 |
|
252 |
sub build_menu { |
253 |
my ($self) = @_; |
254 |
|
255 |
my $menu_tree = [ |
256 |
_File => { |
257 |
item_type => '<Branch>', |
258 |
children => [ |
259 |
_New => { |
260 |
callback => sub { $self->new_cb }, |
261 |
accelerator => '<ctrl>N' |
262 |
}, |
263 |
_Open => { |
264 |
callback => sub { $self->open_cb }, |
265 |
accelerator => '<ctrl>O' |
266 |
}, |
267 |
"_Save Layout" => { |
268 |
callback => sub { $self->save_layout }, |
269 |
accelerator => '<ctrl>L' |
270 |
}, |
271 |
"_Preferences" => { |
272 |
callback => sub { $self->show_editor_properties }, |
273 |
accelerator => "<ctrl>T" |
274 |
}, |
275 |
_Quit => { |
276 |
callback => sub { Gtk2->main_quit }, |
277 |
accelerator => '<ctrl>Q' |
278 |
}, |
279 |
] |
280 |
}, |
281 |
_Dialogs => { |
282 |
item_type => '<Branch>', |
283 |
children => [ |
284 |
"_Picker" => { |
285 |
callback => sub { $self->open_pick_window }, |
286 |
accelerator => "<ctrl>P" |
287 |
}, |
288 |
"_Stack View" => { |
289 |
callback => sub { $self->show_stack_view }, |
290 |
accelerator => "<ctrl>V" |
291 |
}, |
292 |
] |
293 |
}, |
294 |
_Help => { |
295 |
item_type => '<Branch>', |
296 |
children => [ |
297 |
_Manual => { |
298 |
callback => sub { $self->show_help_window }, |
299 |
accelerator => "<ctrl>H" |
300 |
}, |
301 |
] |
302 |
}, |
303 |
]; |
304 |
|
305 |
my $men = |
306 |
Gtk2::SimpleMenu->new ( |
307 |
menu_tree => $menu_tree, |
308 |
default_callback => \&default_cb, |
309 |
); |
310 |
|
311 |
$self->add_accel_group ($men->{accel_group}); |
312 |
|
313 |
return $men->{widget}; |
314 |
} |
315 |
|
316 |
sub add_button { |
317 |
my ($self, $table, $plcinfo, $lbl, $cb) = @_; |
318 |
|
319 |
my ($lx, $ly) = @{$plcinfo->{next}}; |
320 |
|
321 |
unless ($lx < $plcinfo->{width}) { |
322 |
|
323 |
$ly++; |
324 |
$lx = 0; |
325 |
} |
326 |
|
327 |
$ly < $plcinfo->{height} |
328 |
or die "too many buttons, make table bigger!"; |
329 |
|
330 |
$table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1); |
331 |
$btn->signal_connect (clicked => $cb); |
332 |
|
333 |
$plcinfo->{next} = [$lx + 1, $ly]; |
334 |
} |
335 |
|
336 |
sub build_buttons { |
337 |
my ($self) = @_; |
338 |
|
339 |
my $tbl = Gtk2::Table->new (2, 4); |
340 |
my $plcinfo = { width => 2, height => 4, next => [0, 0] }; |
341 |
|
342 |
$self->{edit_collection}{pick} = GCE::EditAction::Pick->new; |
343 |
$self->{edit_collection}{place} = GCE::EditAction::Place->new; |
344 |
$self->{edit_collection}{erase} = GCE::EditAction::Erase->new; |
345 |
$self->{edit_collection}{select} = GCE::EditAction::Select->new; |
346 |
$self->{edit_collection}{perl} = GCE::EditAction::Perl->new; |
347 |
$self->{edit_collection}{connect} = GCE::EditAction::Connect->new; |
348 |
$self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new; |
349 |
|
350 |
$self->set_edit_tool ('pick'); |
351 |
|
352 |
$self->add_button ($tbl, $plcinfo, "P_ick", sub { $self->set_edit_tool ('pick') }); |
353 |
$self->add_button ($tbl, $plcinfo, "_Place", sub { $self->set_edit_tool ('place') }); |
354 |
$self->add_button ($tbl, $plcinfo, "_Erase", sub { $self->set_edit_tool ('erase') }); |
355 |
$self->add_button ($tbl, $plcinfo, "_Select", sub { $self->set_edit_tool ('select') }); |
356 |
$self->add_button ($tbl, $plcinfo, "Eva_l", sub { $self->set_edit_tool ('perl') }); |
357 |
$self->add_button ($tbl, $plcinfo, "Connec_t", sub { $self->set_edit_tool ('connect') }); |
358 |
$self->add_button ($tbl, $plcinfo, "_Follow Exit", sub { $self->set_edit_tool ('followexit') }); |
359 |
|
360 |
return $tbl; |
361 |
} |
362 |
|
363 |
sub set_edit_tool { |
364 |
my ($self, $name) = @_; |
365 |
|
366 |
if ($name eq 'pick') { |
367 |
$self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");; |
368 |
} elsif ($name eq 'place') { |
369 |
$self->update_edit_tool ($self->{edit_collection}{place}, "Place");; |
370 |
} elsif ($name eq 'erase') { |
371 |
$self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");; |
372 |
} elsif ($name eq 'select') { |
373 |
$self->update_edit_tool ($self->{edit_collection}{select}, "Select");; |
374 |
$self->{edit_collection}{select}->update_overlay; |
375 |
} elsif ($name eq 'perl') { |
376 |
$self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");; |
377 |
} elsif ($name eq 'connect') { |
378 |
$self->update_edit_tool ($self->{edit_collection}{connect}, "Connect");; |
379 |
} elsif ($name eq 'followexit') { |
380 |
$self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");; |
381 |
} |
382 |
} |
383 |
|
384 |
sub update_edit_tool { |
385 |
my ($self, $tool, $name) = @_; |
386 |
|
387 |
for (values %{$self->{loaded_maps}}) { |
388 |
$_->{map}->overlay ('selection') |
389 |
} |
390 |
|
391 |
$self->{edit_tool}->set_text ($name); |
392 |
$self->{sel_editaction} = $tool; |
393 |
|
394 |
my $widget = $tool->tool_widget; |
395 |
|
396 |
for ($self->{edit_tool_cont}->get_children) { |
397 |
$_->hide; |
398 |
$self->{edit_tool_cont}->remove ($_); |
399 |
} |
400 |
|
401 |
$_->set_edit_tool ($self->{sel_editaction}) for (values %{$self->{editors}}); |
402 |
|
403 |
defined $widget or return; |
404 |
|
405 |
$self->{edit_tool_cont}->add ($widget); |
406 |
$widget->show_all; |
407 |
} |
408 |
|
409 |
sub update_pick_view { |
410 |
my ($self, $arch) = @_; |
411 |
|
412 |
defined $arch->{_face} |
413 |
or $arch = $Crossfire::ARCH{$arch->{_name}}; |
414 |
|
415 |
fill_pb_from_arch ($self->{pick_view_pb}, $arch); |
416 |
$self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb}); |
417 |
|
418 |
$self->{pick_view_btn}->set_label ($arch->{_name}); |
419 |
} |
420 |
|
421 |
sub INIT_INSTANCE { |
422 |
my ($self) = @_; |
423 |
|
424 |
$::MAINWIN = $self; |
425 |
|
426 |
$self->set_title ("gcrossedit - toolbox"); |
427 |
|
428 |
$self->{edit_tool} = Gtk2::Label->new; |
429 |
$self->{edit_tool_cont} = Gtk2::VBox->new; |
430 |
|
431 |
$self->add (my $vb = Gtk2::VBox->new); |
432 |
$vb->pack_start ($self->build_menu, 0, 1, 0); |
433 |
$vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0); |
434 |
|
435 |
$vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0); |
436 |
$vb->pack_start ($self->{edit_tool}, 0, 1, 0); |
437 |
|
438 |
$vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0); |
439 |
$vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0); |
440 |
|
441 |
# XXX:load $ARGV _cleanly_? |
442 |
$self->open_map_editor ($_) |
443 |
for @ARGV; |
444 |
|
445 |
$self->signal_connect ('delete-event' => sub { |
446 |
Gtk2->main_quit; |
447 |
}); |
448 |
|
449 |
::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0); |
450 |
|
451 |
|
452 |
$self->show_attr_editor; |
453 |
} |
454 |
|
455 |
sub new_cb { |
456 |
my ($self) = @_; |
457 |
|
458 |
my $w = Gtk2::Window->new ('toplevel'); |
459 |
my $width = [width => 20]; |
460 |
my $height = [height => 20]; |
461 |
$w->add (my $tbl = Gtk2::Table->new (2, 3)); |
462 |
add_table_widget ($tbl, 0, $width, 'string'); |
463 |
add_table_widget ($tbl, 1, $height, 'string'); |
464 |
add_table_widget ($tbl, 2, 'new', 'button', sub { |
465 |
if ($width->[1] > 0 and $height->[1] > 0) { |
466 |
my $map = Crossfire::Map->new ($width->[1], $height->[1]); |
467 |
$map->resize ($width->[1], $height->[1]); |
468 |
$self->open_map_editor ($map); |
469 |
} |
470 |
$w->destroy; |
471 |
1; |
472 |
}); |
473 |
add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy }); |
474 |
$w->show_all; |
475 |
} |
476 |
|
477 |
sub new_filechooser { |
478 |
my ($self, $title, $save, $filename) = @_; |
479 |
|
480 |
$title ||= 'gcrossedit - open map'; |
481 |
my $fc = new Gtk2::FileChooserDialog ( |
482 |
$title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok' |
483 |
); |
484 |
|
485 |
$fc->add_shortcut_folder ($::CFG->{MAPDIR}) if -d $::CFG->{MAPDIR}; |
486 |
$fc->add_shortcut_folder ($_) for grep { $_ && ($_ ne '') } keys %{$self->{fc_last_folders}}; |
487 |
$fc->set_current_folder (getcwd); |
488 |
|
489 |
if ($filename) { |
490 |
$fc->set_filename ($filename); |
491 |
} |
492 |
|
493 |
$fc |
494 |
} |
495 |
|
496 |
sub open_cb { |
497 |
my ($self) = @_; |
498 |
|
499 |
my $fc = $self->new_filechooser; |
500 |
|
501 |
if ('ok' eq $fc->run) { |
502 |
|
503 |
$self->{fc_last_folder} = $fc->get_current_folder; |
504 |
$self->{fc_last_folders}->{$self->{fc_last_folder}}++; |
505 |
|
506 |
$self->open_map_editor ($fc->get_filename); |
507 |
} |
508 |
|
509 |
$fc->destroy; |
510 |
} |
511 |
|
512 |
sub get_pick { |
513 |
my ($self) = @_; |
514 |
|
515 |
$self->{attr_edit} |
516 |
or die "Couldn't find attribute editor! SERIOUS BUG!"; |
517 |
|
518 |
# XXX: This is just to make sure that this function always returns something |
519 |
|
520 |
my $ar = $self->{attr_edit}->get_arch; |
521 |
return { _name => 'platinacoin' } unless defined $ar; |
522 |
return $ar->getarch || { _name => 'platinacoin' }; |
523 |
} |
524 |
|
525 |
=head1 AUTHOR |
526 |
|
527 |
Marc Lehmann <schmorp@schmorp.de> |
528 |
http://home.schmorp.de/ |
529 |
|
530 |
Robin Redeker <elmex@ta-sa.org> |
531 |
http://www.ta-sa.org/ |
532 |
|
533 |
=cut |
534 |
1; |
535 |
|