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; |
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 |
main::write_cfg ("$Crossfire::VARDIR/gceconfig"); |
61 |
} |
62 |
|
63 |
sub load_layout { |
64 |
my ($self) = @_; |
65 |
|
66 |
$self->{fc_last_folders} = $main::CFG->{last_folders}; |
67 |
|
68 |
$main::CFG->{attr_edit_on} |
69 |
and $self->show_attr_editor; |
70 |
|
71 |
$main::CFG->{stack_view_on} |
72 |
and $self->show_stack_view; |
73 |
|
74 |
for (@{$main::CFG->{open_pickers}}) { |
75 |
$self->open_pick_window ($_); |
76 |
} |
77 |
} |
78 |
|
79 |
sub open_map_editor { |
80 |
my ($self, $mapfile) = @_; |
81 |
|
82 |
my $mapkey; |
83 |
unless (ref $mapfile) { |
84 |
unless (File::Spec->file_name_is_absolute ($mapfile)) { |
85 |
$mapfile = File::Spec->rel2abs ($mapfile); |
86 |
} |
87 |
$mapkey = File::Spec->abs2rel ($mapfile, File::Spec->catfile ($::CFG->{MAPDIR})); |
88 |
} |
89 |
|
90 |
# XXX: last_map_window is a dirty trick to get the position and size |
91 |
# for save layout |
92 |
|
93 |
unless (ref $mapfile) { |
94 |
if (defined $self->{loaded_maps}->{$mapkey}) { |
95 |
$self->{loaded_maps}->{$mapkey}->get_toplevel->present; |
96 |
return; |
97 |
} |
98 |
} |
99 |
|
100 |
my $w = $self->{last_map_window} = GCE::MapEditor->new; |
101 |
|
102 |
$self->{editors}->{$w} = $w; |
103 |
|
104 |
$w->signal_connect (destroy => sub { |
105 |
my ($w) = @_; |
106 |
delete $self->{loaded_maps}->{$w->{mapkey}}; |
107 |
delete $self->{editors}->{$w}; |
108 |
0; |
109 |
}); |
110 |
|
111 |
unless (ref $mapfile) { |
112 |
unless (-e $mapfile) { |
113 |
quick_msg ("file '$mapfile' does not exist!", 0); |
114 |
return; |
115 |
} |
116 |
$self->{loaded_maps}->{$mapkey} = $w; |
117 |
} |
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 ("gce - 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 ('gce - 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 ("gce - preferences"); |
168 |
$w->add (my $t = Gtk2::Table->new (2, 4)); |
169 |
$t->attach_defaults (my $lbl1 = Gtk2::Label->new ("CROSSFIRE_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 ("Map path"), 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 |
$::CFG->{LIBDIR} = $lib->get_text; |
178 |
$::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 |
return if $self->{attr_edit}; |
199 |
|
200 |
my $w = $self->{attr_edit_win} = Gtk2::Window->new; |
201 |
$w->set_title ("gce - edit attrs"); |
202 |
$w->add ($self->{attr_edit} = GCE::AttrEdit->new); |
203 |
$w->signal_connect (destroy => sub { delete $self->{attr_edit}; 0 }); |
204 |
|
205 |
main::set_pos_and_size ($w, $main::CFG->{attr_view}, 200, 200); |
206 |
|
207 |
$w->show_all; |
208 |
} |
209 |
|
210 |
sub update_attr_editor { |
211 |
my ($self, $arch, $cb) = @_; |
212 |
|
213 |
return unless $self->{attr_edit}; |
214 |
|
215 |
$self->{attr_edit}->set_arch ($arch, $cb); |
216 |
$self->{attr_edit_win}->set_title ("gce - edit $arch->{_name}"); |
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 |
sub open_pick_window { |
228 |
my ($self, $layout) = @_; |
229 |
|
230 |
# XXX: Yes, also fix this, save _every_ pick window and their positions and their |
231 |
# selection |
232 |
my $p = GCE::PickWindow->new (); |
233 |
|
234 |
push @{$self->{open_pick_windows}}, $p; |
235 |
|
236 |
my $idx = (@{$self->{open_pick_windows}}) - 1; |
237 |
|
238 |
$p->signal_connect ('delete-event' => sub { |
239 |
$self->{open_pick_windows}->[$idx] = undef; |
240 |
}); |
241 |
|
242 |
if ($layout) { |
243 |
main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200); |
244 |
} |
245 |
|
246 |
$p->show_all; |
247 |
|
248 |
$p->set_selection ($layout->{selection}); |
249 |
} |
250 |
|
251 |
sub build_menu { |
252 |
my ($self) = @_; |
253 |
|
254 |
my $menu_tree = [ |
255 |
_File => { |
256 |
item_type => '<Branch>', |
257 |
children => [ |
258 |
_New => { |
259 |
callback => sub { $self->new_cb }, |
260 |
accelerator => '<ctrl>N' |
261 |
}, |
262 |
_Open => { |
263 |
callback => sub { $self->open_cb }, |
264 |
accelerator => '<ctrl>O' |
265 |
}, |
266 |
"_Save Layout" => { |
267 |
callback => sub { $self->save_layout }, |
268 |
accelerator => '<ctrl>L' |
269 |
}, |
270 |
"_Preferences" => { |
271 |
callback => sub { $self->show_editor_properties }, |
272 |
accelerator => "<ctrl>T" |
273 |
}, |
274 |
_Quit => { |
275 |
callback => sub { Gtk2->main_quit }, |
276 |
accelerator => '<ctrl>Q' |
277 |
}, |
278 |
] |
279 |
}, |
280 |
_Dialogs => { |
281 |
item_type => '<Branch>', |
282 |
children => [ |
283 |
"_Attr Editor" => { |
284 |
callback => sub { $self->show_attr_editor }, |
285 |
accelerator => "<ctrl>A" |
286 |
}, |
287 |
"_Picker" => { |
288 |
callback => sub { $self->open_pick_window }, |
289 |
accelerator => "<ctrl>P" |
290 |
}, |
291 |
"_Stack View" => { |
292 |
callback => sub { $self->show_stack_view }, |
293 |
accelerator => "<ctrl>V" |
294 |
}, |
295 |
] |
296 |
}, |
297 |
_Help => { |
298 |
item_type => '<Branch>', |
299 |
children => [ |
300 |
_Manual => { |
301 |
callback => sub { $self->show_help_window }, |
302 |
accelerator => "<ctrl>H" |
303 |
}, |
304 |
] |
305 |
}, |
306 |
]; |
307 |
|
308 |
my $men = |
309 |
Gtk2::SimpleMenu->new ( |
310 |
menu_tree => $menu_tree, |
311 |
default_callback => \&default_cb, |
312 |
); |
313 |
|
314 |
$self->add_accel_group ($men->{accel_group}); |
315 |
|
316 |
return $men->{widget}; |
317 |
} |
318 |
|
319 |
sub add_button { |
320 |
my ($self, $table, $plcinfo, $lbl, $cb) = @_; |
321 |
|
322 |
my ($lx, $ly) = @{$plcinfo->{next}}; |
323 |
|
324 |
unless ($lx < $plcinfo->{width}) { |
325 |
|
326 |
$ly++; |
327 |
$lx = 0; |
328 |
} |
329 |
|
330 |
$ly < $plcinfo->{height} |
331 |
or die "too many buttons, make table bigger!"; |
332 |
|
333 |
$table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1); |
334 |
$btn->signal_connect (clicked => $cb); |
335 |
|
336 |
$plcinfo->{next} = [$lx + 1, $ly]; |
337 |
} |
338 |
|
339 |
sub build_buttons { |
340 |
my ($self) = @_; |
341 |
|
342 |
my $tbl = Gtk2::Table->new (2, 4); |
343 |
my $plcinfo = { width => 2, height => 4, next => [0, 0] }; |
344 |
|
345 |
$self->{edit_collection}{pick} = GCE::EditAction::Pick->new; |
346 |
$self->{edit_collection}{place} = GCE::EditAction::Place->new; |
347 |
$self->{edit_collection}{erase} = GCE::EditAction::Erase->new; |
348 |
$self->{edit_collection}{select} = GCE::EditAction::Select->new; |
349 |
$self->{edit_collection}{perl} = GCE::EditAction::Perl->new; |
350 |
$self->{edit_collection}{connectexit} = GCE::EditAction::ConnectExit->new; |
351 |
$self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new; |
352 |
|
353 |
$self->set_edit_tool ('pick'); |
354 |
|
355 |
$self->add_button ($tbl, $plcinfo, "P_ick", sub { $self->set_edit_tool ('pick') }); |
356 |
$self->add_button ($tbl, $plcinfo, "_Place", sub { $self->set_edit_tool ('place') }); |
357 |
$self->add_button ($tbl, $plcinfo, "_Erase", sub { $self->set_edit_tool ('erase') }); |
358 |
$self->add_button ($tbl, $plcinfo, "_Select", sub { $self->set_edit_tool ('select') }); |
359 |
$self->add_button ($tbl, $plcinfo, "Eva_l", sub { $self->set_edit_tool ('perl') }); |
360 |
$self->add_button ($tbl, $plcinfo, "Connect E_xit", sub { $self->set_edit_tool ('connectexit') }); |
361 |
$self->add_button ($tbl, $plcinfo, "_Follow Exit", sub { $self->set_edit_tool ('followexit') }); |
362 |
|
363 |
return $tbl; |
364 |
} |
365 |
|
366 |
sub set_edit_tool { |
367 |
my ($self, $name) = @_; |
368 |
|
369 |
if ($name eq 'pick') { |
370 |
$self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");; |
371 |
} elsif ($name eq 'place') { |
372 |
$self->update_edit_tool ($self->{edit_collection}{place}, "Place");; |
373 |
} elsif ($name eq 'erase') { |
374 |
$self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");; |
375 |
} elsif ($name eq 'select') { |
376 |
$self->update_edit_tool ($self->{edit_collection}{select}, "Select");; |
377 |
$self->{edit_collection}{select}->update_overlay; |
378 |
} elsif ($name eq 'perl') { |
379 |
$self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");; |
380 |
} elsif ($name eq 'connectexit') { |
381 |
$self->update_edit_tool ($self->{edit_collection}{connectexit}, "Connect Exit");; |
382 |
} elsif ($name eq 'followexit') { |
383 |
$self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");; |
384 |
} |
385 |
} |
386 |
|
387 |
sub update_edit_tool { |
388 |
my ($self, $tool, $name) = @_; |
389 |
|
390 |
for (values %{$self->{loaded_maps}}) { |
391 |
$_->{map}->overlay ('selection') |
392 |
} |
393 |
|
394 |
$self->{edit_tool}->set_text ($name); |
395 |
$self->{sel_editaction} = $tool; |
396 |
|
397 |
my $widget = $tool->tool_widget; |
398 |
|
399 |
for ($self->{edit_tool_cont}->get_children) { |
400 |
$_->hide; |
401 |
$self->{edit_tool_cont}->remove ($_); |
402 |
} |
403 |
|
404 |
$_->set_edit_tool ($self->{sel_editaction}) for (values %{$self->{editors}}); |
405 |
|
406 |
defined $widget or return; |
407 |
|
408 |
$self->{edit_tool_cont}->add ($widget); |
409 |
$widget->show_all; |
410 |
} |
411 |
|
412 |
sub update_pick_view { |
413 |
my ($self, $arch) = @_; |
414 |
|
415 |
defined $arch->{_face} |
416 |
or $arch = $Crossfire::ARCH{$arch->{_name}}; |
417 |
|
418 |
fill_pb_from_arch ($self->{pick_view_pb}, $arch); |
419 |
$self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb}); |
420 |
|
421 |
$self->{pick_view_btn}->set_label ($arch->{_name}); |
422 |
} |
423 |
|
424 |
sub INIT_INSTANCE { |
425 |
my ($self) = @_; |
426 |
|
427 |
$::MAINWIN = $self; |
428 |
|
429 |
$self->set_title ("gce - toolbox"); |
430 |
|
431 |
$self->{edit_tool} = Gtk2::Label->new; |
432 |
$self->{edit_tool_cont} = Gtk2::VBox->new; |
433 |
|
434 |
$self->add (my $vb = Gtk2::VBox->new); |
435 |
$vb->pack_start ($self->build_menu, 0, 1, 0); |
436 |
|
437 |
$vb->pack_start (my $hb = $self->{pick_view_hb} = Gtk2::HBox->new, 0, 1, 0); |
438 |
$hb->pack_start ($self->{pick_view_img} = Gtk2::Image->new, 0, 1, 0); |
439 |
$hb->pack_start ($self->{pick_view_btn} = Gtk2::Button->new, 0, 1, 0); |
440 |
GCE::DragHelper::set_drag_source ( |
441 |
$self->{pick_view_btn}, arch => sub { { arch => $self->get_pick } } |
442 |
); |
443 |
GCE::DragHelper::set_drag_sink ( |
444 |
$self->{pick_view_btn}, arch => sub { $self->set_pick ($_[0]->{arch}) } |
445 |
); |
446 |
$self->{pick_view_btn}->signal_connect (clicked => sub { |
447 |
$self->update_attr_editor ($self->{pick_arch}); |
448 |
}); |
449 |
$self->{pick_view_pb} = new_arch_pb (); |
450 |
|
451 |
$vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0); |
452 |
$vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0); |
453 |
|
454 |
$vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0); |
455 |
$vb->pack_start ($self->{edit_tool}, 0, 1, 0); |
456 |
|
457 |
$vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0); |
458 |
$vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0); |
459 |
|
460 |
# XXX:load $ARGV _cleanly_? |
461 |
$self->open_map_editor ($_) |
462 |
for @ARGV; |
463 |
|
464 |
$self->signal_connect ('delete-event' => sub { |
465 |
Gtk2->main_quit; |
466 |
}); |
467 |
|
468 |
::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0); |
469 |
} |
470 |
|
471 |
sub new_cb { |
472 |
my ($self) = @_; |
473 |
|
474 |
my $w = Gtk2::Window->new ('toplevel'); |
475 |
my $width = [width => 20]; |
476 |
my $height = [height => 20]; |
477 |
$w->add (my $tbl = Gtk2::Table->new (2, 3)); |
478 |
add_table_widget ($tbl, 0, $width, 'string'); |
479 |
add_table_widget ($tbl, 1, $height, 'string'); |
480 |
add_table_widget ($tbl, 2, 'new', 'button', sub { |
481 |
if ($width->[1] > 0 and $height->[1] > 0) { |
482 |
my $map = Crossfire::Map->new ($width->[1], $height->[1]); |
483 |
$map->resize ($width->[1], $height->[1]); |
484 |
$self->open_map_editor ($map); |
485 |
} |
486 |
$w->destroy; |
487 |
1; |
488 |
}); |
489 |
add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy }); |
490 |
$w->show_all; |
491 |
} |
492 |
|
493 |
sub new_filechooser { |
494 |
my ($self, $title, $save, $filename) = @_; |
495 |
|
496 |
$title ||= 'gce - open map'; |
497 |
my $fc = new Gtk2::FileChooserDialog ( |
498 |
$title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok' |
499 |
); |
500 |
|
501 |
$fc->add_shortcut_folder ($::CFG->{MAPDIR}) if -d $::CFG->{MAPDIR}; |
502 |
$fc->add_shortcut_folder ($_) for grep { $_ && ($_ ne '') } keys %{$self->{fc_last_folders}}; |
503 |
$fc->set_current_folder (getcwd); |
504 |
|
505 |
if ($filename) { |
506 |
$fc->set_filename ($filename); |
507 |
} |
508 |
|
509 |
$fc |
510 |
} |
511 |
|
512 |
sub open_cb { |
513 |
my ($self) = @_; |
514 |
|
515 |
my $fc = $self->new_filechooser; |
516 |
|
517 |
if ('ok' eq $fc->run) { |
518 |
|
519 |
$self->{fc_last_folder} = $fc->get_current_folder; |
520 |
$self->{fc_last_folders}->{$self->{fc_last_folder}}++; |
521 |
|
522 |
$self->open_map_editor ($fc->get_filename); |
523 |
} |
524 |
|
525 |
$fc->destroy; |
526 |
} |
527 |
|
528 |
sub set_pick { |
529 |
my ($self, $arch) = @_; |
530 |
|
531 |
$self->{pick_arch} = $arch; |
532 |
$self->update_pick_view ($arch); |
533 |
} |
534 |
|
535 |
sub get_pick { |
536 |
my ($self) = @_; |
537 |
|
538 |
# XXX: This is just to make sure that this function always returns something |
539 |
return $self->{pick_arch} || { _name => 'platinacoin' }; |
540 |
} |
541 |
|
542 |
=head1 AUTHOR |
543 |
|
544 |
Marc Lehmann <schmorp@schmorp.de> |
545 |
http://home.schmorp.de/ |
546 |
|
547 |
Robin Redeker <elmex@ta-sa.org> |
548 |
http://www.ta-sa.org/ |
549 |
|
550 |
=cut |
551 |
1; |
552 |
|