1 |
package GCE::MainWindow; |
2 |
|
3 |
=head1 NAME |
4 |
|
5 |
GCE::MainWindow - the main window class for gde |
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 Deliantra; |
15 |
use Deliantra::Map; |
16 |
use Deliantra::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 Glib::Object::Subclass |
25 |
Gtk2::Window; |
26 |
|
27 |
use GCE::Util; |
28 |
use GCE::DragHelper; |
29 |
|
30 |
use base qw/Object::Event/; |
31 |
|
32 |
use strict; |
33 |
|
34 |
my $recentfile = "$Deliantra::VARDIR/gderecent"; |
35 |
|
36 |
# XXX: make a recursive call from save_layout to all (interesting) sub-widgets |
37 |
sub save_layout { |
38 |
my ($self) = @_; |
39 |
|
40 |
# $main::CFG->{attr_edit_on} = exists $self->{attr_edit} ? 1 : 0; |
41 |
$main::CFG->{stack_view_on} = exists $self->{sv} ? 1 : 0; |
42 |
$main::CFG->{picker_on} = exists $self->{last_pick_window} ? 1 : 0; |
43 |
$main::CFG->{main_window} = main::get_pos_and_size ($self); |
44 |
$main::CFG->{stack_view} = main::get_pos_and_size ($self->{sv_win}) if $self->{sv_win}; |
45 |
$main::CFG->{attr_view} = main::get_pos_and_size ($self->{attr_edit_win}) if $self->{attr_edit_win}; |
46 |
|
47 |
if ($self->{last_map_window}) { |
48 |
$main::CFG->{map_window} = main::get_pos_and_size ($self->{last_map_window}); |
49 |
$self->{last_map_window}->save_layout (); |
50 |
} |
51 |
|
52 |
$self->{worldmap_coord_query}->save_layout () |
53 |
if $self->{worldmap_coord_query}; |
54 |
|
55 |
$main::CFG->{last_folders} = $self->{fc_last_folders}; |
56 |
|
57 |
$main::CFG->{open_pickers} = []; |
58 |
|
59 |
for (@{$self->{open_pick_windows}}) { |
60 |
|
61 |
next unless defined $_; |
62 |
|
63 |
push @{$main::CFG->{open_pickers}}, { |
64 |
p_and_s => main::get_pos_and_size ($_), |
65 |
selection => $_->{last_selection} |
66 |
}; |
67 |
} |
68 |
|
69 |
$self->{attr_edit}->save_layout; |
70 |
|
71 |
main::write_cfg ("$Deliantra::VARDIR/gdeconfig"); |
72 |
} |
73 |
|
74 |
sub load_layout { |
75 |
my ($self) = @_; |
76 |
|
77 |
$self->{fc_last_folders} = $main::CFG->{last_folders}; |
78 |
|
79 |
# $main::CFG->{attr_edit_on} |
80 |
# and $self->show_attr_editor; |
81 |
|
82 |
$main::CFG->{stack_view_on} |
83 |
and $self->show_stack_view; |
84 |
|
85 |
for (@{$main::CFG->{open_pickers}}) { |
86 |
$self->open_pick_window ($_); |
87 |
} |
88 |
|
89 |
$self->{attr_edit}->load_layout; |
90 |
} |
91 |
|
92 |
sub open_map_editor { |
93 |
my ($self, $mapfile) = @_; |
94 |
|
95 |
my $mapkey; |
96 |
unless (ref $mapfile) { |
97 |
# unless (File::Spec->file_name_is_absolute ($mapfile)) { |
98 |
# $mapfile = File::Spec->rel2abs ($mapfile); |
99 |
# } |
100 |
$mapkey = abs_path ($mapfile); |
101 |
# File::Spec->abs2rel ($mapfile, File::Spec->catfile ($::MAPDIR)); |
102 |
} else { |
103 |
$mapkey = "$mapfile"; |
104 |
} |
105 |
|
106 |
# XXX: last_map_window is a dirty trick to get the position and size |
107 |
# for save layout |
108 |
|
109 |
if (defined $self->{loaded_maps}->{$mapkey}) { |
110 |
$self->{loaded_maps}->{$mapkey}->get_toplevel->present; |
111 |
return; |
112 |
} |
113 |
|
114 |
my $w = $self->{last_map_window} = GCE::MapEditor->new; |
115 |
|
116 |
$self->{editors}->{$w} = $w; |
117 |
|
118 |
$w->signal_connect (destroy => sub { |
119 |
my ($w) = @_; |
120 |
$w->close_windows; |
121 |
delete $self->{loaded_maps}->{$w->{mapkey}}; |
122 |
delete $self->{editors}->{$w}; |
123 |
0; |
124 |
}); |
125 |
|
126 |
$self->{loaded_maps}->{$mapkey} = $w; |
127 |
|
128 |
eval { $w->open_map ($mapfile, $mapkey) }; |
129 |
if ($@) { |
130 |
quick_msg ($self, "$@", 1); |
131 |
$w->close_windows; |
132 |
delete $self->{loaded_maps}->{$w->{mapkey}}; |
133 |
delete $self->{editors}->{$w}; |
134 |
$w->destroy; |
135 |
return; |
136 |
} |
137 |
|
138 |
$w->set_edit_tool ($self->{sel_editaction}); |
139 |
|
140 |
$w->show_all; |
141 |
} |
142 |
|
143 |
sub show_help_window { |
144 |
my ($self) = @_; |
145 |
|
146 |
return if defined $self->{help_win}; |
147 |
require Gtk2::Ex::PodViewer; |
148 |
my $w = $self->{help_win} = Gtk2::Window->new; |
149 |
$w->set_title ("deliantra editor - help"); |
150 |
$w->set_default_size (500, 300); |
151 |
$w->signal_connect (destroy => sub { |
152 |
$self->{help_win}->hide; $self->{help_win} = undef; |
153 |
0 |
154 |
}); |
155 |
$w->add (my $sw = Gtk2::ScrolledWindow->new); |
156 |
$sw->add (my $h = Gtk2::Ex::PodViewer->new); |
157 |
$h->load_string ($::DOCUMENTATION); |
158 |
$w->show_all; |
159 |
} |
160 |
|
161 |
sub show_stack_view { |
162 |
my ($self) = @_; |
163 |
|
164 |
return if defined $self->{sv}; |
165 |
|
166 |
my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel'); |
167 |
$w->set_title ('deliantra editor - stack view'); |
168 |
$w->signal_connect (destroy => sub { delete $self->{sv}; 0 }); |
169 |
$w->add ($self->{sv} = GCE::StackView->new); |
170 |
|
171 |
main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250); |
172 |
|
173 |
$w->show_all; |
174 |
} |
175 |
|
176 |
sub show_editor_properties { |
177 |
my ($self) = @_; |
178 |
|
179 |
return if $self->{prop_edit}; |
180 |
|
181 |
my $w = $self->{prop_edit} = Gtk2::Window->new; |
182 |
$w->set_title ("deliantra editor - preferences"); |
183 |
$w->add (my $t = Gtk2::Table->new (2, 5)); |
184 |
$t->attach_defaults (my $lbl1 = Gtk2::Label->new ("LIBDIR"), 0, 1, 0, 1); |
185 |
$t->attach_defaults (my $lib = Gtk2::Entry->new, 1, 2, 0, 1); |
186 |
$lib->set_text ($::CFG->{LIBDIR}); |
187 |
$t->attach_defaults (my $lbl2 = Gtk2::Label->new ("MAPDIR"), 0, 1, 1, 2); |
188 |
$t->attach_defaults (my $map = Gtk2::Entry->new, 1, 2, 1, 2); |
189 |
$map->set_text ($::CFG->{MAPDIR}); |
190 |
$t->attach_defaults (my $lbl1 = Gtk2::Label->new ("Username"), 0, 1, 2, 3); |
191 |
$t->attach_defaults (my $usern = Gtk2::Entry->new, 1, 2, 2, 3); |
192 |
$usern->set_text ($::CFG->{username}); |
193 |
$t->attach_defaults (my $save = Gtk2::Button->new ('save'), 0, 2, 3, 4); |
194 |
$save->signal_connect (clicked => sub { |
195 |
$::CFG->{LIBDIR} = $lib->get_text; |
196 |
$::CFG->{MAPDIR} = $map->get_text; |
197 |
$::LIBDIR = $::CFG->{LIBDIR} if $::CFG->{LIBDIR}; |
198 |
$::MAPDIR = $::CFG->{MAPDIR} if $::CFG->{MAPDIR}; |
199 |
$::CFG->{username} = $usern->get_text; |
200 |
Deliantra::set_libdir ($::LIBDIR); |
201 |
Deliantra::load_archetypes; |
202 |
Deliantra::load_tilecache; |
203 |
main::write_cfg ("$Deliantra::VARDIR/gdeconfig"); |
204 |
$w->destroy; |
205 |
}); |
206 |
$t->attach_defaults (my $close = Gtk2::Button->new ('close'), 0, 2, 4, 5); |
207 |
$close->signal_connect (clicked => sub { $w->destroy }); |
208 |
|
209 |
$w->signal_connect (destroy => sub { delete $self->{prop_edit}; 0 }); |
210 |
|
211 |
main::set_pos_and_size ($w, $main::CFG->{prop_edit}, 200, 200); |
212 |
|
213 |
$w->show_all; |
214 |
} |
215 |
|
216 |
sub show_attr_editor { |
217 |
my ($self) = @_; |
218 |
|
219 |
return if $self->{attr_edit_win}; |
220 |
|
221 |
my $w = $self->{attr_edit_win} = Gtk2::Window->new; |
222 |
$w->set_title ("deliantra editor - edit attrs"); |
223 |
$w->add ($self->{attr_edit} = GCE::AttrEdit->new); |
224 |
$w->signal_connect (destroy => sub { delete $self->{attr_edit_win}; 0 }); |
225 |
|
226 |
main::set_pos_and_size ($w, $main::CFG->{attr_view}, 400, 300, 250, 0); |
227 |
|
228 |
$w->show_all; |
229 |
} |
230 |
|
231 |
sub update_attr_editor { |
232 |
my ($self, $ar) = @_; |
233 |
|
234 |
$self->{attr_edit} |
235 |
or return; |
236 |
|
237 |
$self->{attr_edit}->update_view ($ar); |
238 |
$self->{attr_edit_win}->set_title ("deliantra editor - edit " . $ar->longname); |
239 |
} |
240 |
|
241 |
#sub update_map_pos { |
242 |
# my ($self, $mapedit, $x, $y) = @_; |
243 |
# $self->{sv}->maybe_update_stack_for ($mapedit, $x, $y) |
244 |
# if $self->{sv}; |
245 |
#} |
246 |
|
247 |
sub update_stack_view { |
248 |
my ($self, $sr) = @_; |
249 |
|
250 |
return unless $self->{sv}; |
251 |
|
252 |
$self->{sv}->set_stack ($sr); |
253 |
} |
254 |
|
255 |
sub open_pick_window { |
256 |
my ($self, $layout) = @_; |
257 |
|
258 |
# XXX: Yes, also fix this, save _every_ pick window and their positions and their |
259 |
# selection |
260 |
my $p = GCE::PickWindow->new (); |
261 |
|
262 |
push @{$self->{open_pick_windows}}, $p; |
263 |
|
264 |
my $idx = (@{$self->{open_pick_windows}}) - 1; |
265 |
|
266 |
$p->signal_connect ('delete-event' => sub { |
267 |
$self->{open_pick_windows}->[$idx] = undef; |
268 |
}); |
269 |
|
270 |
if ($layout) { |
271 |
main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200); |
272 |
} |
273 |
|
274 |
$p->show_all; |
275 |
|
276 |
$p->set_selection ($layout->{selection}); |
277 |
} |
278 |
|
279 |
sub add_recent { |
280 |
my ($self, $entry) = @_; |
281 |
our @recent_entries; |
282 |
|
283 |
@recent_entries = grep { $_ ne $entry } @recent_entries; |
284 |
unshift @recent_entries, $entry; |
285 |
splice @recent_entries, 5; |
286 |
|
287 |
open my $fh, ">$recentfile" or die "Can't write to file $recentfile: $!"; |
288 |
binmode $fh; |
289 |
local $/; |
290 |
print $fh (join "\0", @recent_entries); |
291 |
close $fh; |
292 |
|
293 |
$self->build_menu; |
294 |
} |
295 |
|
296 |
sub escape_filename { |
297 |
my $str = shift; |
298 |
|
299 |
$str = Glib::filename_to_unicode($str); |
300 |
|
301 |
# escape to prevent Gtk2::SimpleMenu parsing these especially |
302 |
$str =~ s/\//\\\//g; |
303 |
$str =~ s/_/-/g; |
304 |
|
305 |
$str; |
306 |
} |
307 |
|
308 |
sub recent { |
309 |
my ($self) = @_; |
310 |
my @recent; |
311 |
|
312 |
our @recent_entries; |
313 |
|
314 |
foreach my $entry (@recent_entries) { |
315 |
push @recent, escape_filename($entry) => { |
316 |
callback => sub { |
317 |
$self->open_map_editor($entry); |
318 |
$self->build_menu |
319 |
} |
320 |
}; |
321 |
} |
322 |
|
323 |
push @recent, Empty => { callback => sub {} } |
324 |
unless @recent; |
325 |
|
326 |
@recent; |
327 |
} |
328 |
|
329 |
sub build_menu { |
330 |
my ($self) = @_; |
331 |
|
332 |
my $menu_tree = [ |
333 |
_File => { |
334 |
item_type => '<Branch>', |
335 |
children => [ |
336 |
_New => { |
337 |
callback => sub { $self->new_cb }, |
338 |
accelerator => '<ctrl>N' |
339 |
}, |
340 |
_Open => { |
341 |
callback => sub { $self->open_cb }, |
342 |
accelerator => '<ctrl>O' |
343 |
}, |
344 |
'Open special' => { |
345 |
item_type => '<Branch>', |
346 |
children => [ |
347 |
"world map at"=> { |
348 |
callback => sub { $self->open_worldmap_cb }, |
349 |
}, |
350 |
"recent files"=> { |
351 |
item_type => '<Branch>', |
352 |
children => [ $self->recent ], |
353 |
}, |
354 |
] |
355 |
}, |
356 |
"_Save Layout" => { |
357 |
callback => sub { $self->save_layout }, |
358 |
accelerator => '<ctrl>L' |
359 |
}, |
360 |
"_Preferences" => { |
361 |
callback => sub { $self->show_editor_properties }, |
362 |
accelerator => "<ctrl>T" |
363 |
}, |
364 |
_Quit => { |
365 |
callback => sub { Gtk2->main_quit }, |
366 |
accelerator => '<ctrl>Q' |
367 |
}, |
368 |
] |
369 |
}, |
370 |
_Dialogs => { |
371 |
item_type => '<Branch>', |
372 |
children => [ |
373 |
"_Picker" => { |
374 |
callback => sub { $self->open_pick_window }, |
375 |
accelerator => "<ctrl>P" |
376 |
}, |
377 |
"_Stack View" => { |
378 |
callback => sub { $self->show_stack_view }, |
379 |
accelerator => "<ctrl>V" |
380 |
}, |
381 |
"_Attributes" => { |
382 |
callback => sub { $self->show_attr_editor }, |
383 |
accelerator => "<ctrl>A" |
384 |
}, |
385 |
] |
386 |
}, |
387 |
_Help => { |
388 |
item_type => '<Branch>', |
389 |
children => [ |
390 |
_Manual => { |
391 |
callback => sub { $self->show_help_window }, |
392 |
accelerator => "<ctrl>H" |
393 |
}, |
394 |
] |
395 |
}, |
396 |
]; |
397 |
|
398 |
my $men = |
399 |
Gtk2::SimpleMenu->new ( |
400 |
menu_tree => $menu_tree, |
401 |
default_callback => \&default_cb, |
402 |
); |
403 |
|
404 |
for ($self->{vb}->get_children) { # Rebuild menu |
405 |
if ($_->isa ('Gtk2::MenuBar')) { |
406 |
$_->hide; |
407 |
$self->{vb}->remove($_); |
408 |
|
409 |
$self->{vb}->pack_start ($men->{widget}, 0, 1, 0); |
410 |
$self->{vb}->reorder_child ($men->{widget}, 0); |
411 |
$self->{vb}->show_all; |
412 |
} |
413 |
} |
414 |
|
415 |
$self->add_accel_group ($men->{accel_group}); |
416 |
|
417 |
return $men->{widget}; |
418 |
} |
419 |
|
420 |
sub add_button { |
421 |
my ($self, $table, $plcinfo, $lbl, $cb) = @_; |
422 |
|
423 |
my ($lx, $ly) = @{$plcinfo->{next}}; |
424 |
|
425 |
unless ($lx < $plcinfo->{width}) { |
426 |
|
427 |
$ly++; |
428 |
$lx = 0; |
429 |
} |
430 |
|
431 |
$ly < $plcinfo->{height} |
432 |
or die "too many buttons, make table bigger!"; |
433 |
|
434 |
$table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1); |
435 |
$btn->signal_connect (clicked => $cb); |
436 |
|
437 |
$plcinfo->{next} = [$lx + 1, $ly]; |
438 |
} |
439 |
|
440 |
sub build_buttons { |
441 |
my ($self) = @_; |
442 |
|
443 |
my $tbl = Gtk2::Table->new (2, 4); |
444 |
my $plcinfo = { width => 2, height => 4, next => [0, 0] }; |
445 |
|
446 |
$self->{edit_collection}{pick} = GCE::EditAction::Pick->new; |
447 |
$self->{edit_collection}{place} = GCE::EditAction::Place->new; |
448 |
$self->{edit_collection}{erase} = GCE::EditAction::Erase->new; |
449 |
$self->{edit_collection}{select} = GCE::EditAction::Select->new; |
450 |
$self->{edit_collection}{perl} = GCE::EditAction::Perl->new; |
451 |
$self->{edit_collection}{connect} = GCE::EditAction::Connect->new; |
452 |
$self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new; |
453 |
|
454 |
$self->set_edit_tool ('pick'); |
455 |
|
456 |
$self->add_button ($tbl, $plcinfo, "P_ick", sub { $self->set_edit_tool ('pick') }); |
457 |
$self->add_button ($tbl, $plcinfo, "_Place", sub { $self->set_edit_tool ('place') }); |
458 |
$self->add_button ($tbl, $plcinfo, "_Erase", sub { $self->set_edit_tool ('erase') }); |
459 |
$self->add_button ($tbl, $plcinfo, "_Select", sub { $self->set_edit_tool ('select') }); |
460 |
$self->add_button ($tbl, $plcinfo, "Eva_l", sub { $self->set_edit_tool ('perl') }); |
461 |
$self->add_button ($tbl, $plcinfo, "Connec_t", sub { $self->set_edit_tool ('connect') }); |
462 |
$self->add_button ($tbl, $plcinfo, "_Follow Exit", sub { $self->set_edit_tool ('followexit') }); |
463 |
|
464 |
return $tbl; |
465 |
} |
466 |
|
467 |
sub set_edit_tool { |
468 |
my ($self, $name) = @_; |
469 |
|
470 |
if ($name eq 'pick') { |
471 |
$self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");; |
472 |
} elsif ($name eq 'place') { |
473 |
$self->update_edit_tool ($self->{edit_collection}{place}, "Place");; |
474 |
} elsif ($name eq 'erase') { |
475 |
$self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");; |
476 |
} elsif ($name eq 'select') { |
477 |
$self->update_edit_tool ($self->{edit_collection}{select}, "Select");; |
478 |
$self->{edit_collection}{select}->update_overlay; |
479 |
} elsif ($name eq 'perl') { |
480 |
$self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");; |
481 |
} elsif ($name eq 'connect') { |
482 |
$self->update_edit_tool ($self->{edit_collection}{connect}, "Connect");; |
483 |
} elsif ($name eq 'followexit') { |
484 |
$self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");; |
485 |
} |
486 |
} |
487 |
|
488 |
sub update_edit_tool { |
489 |
my ($self, $tool, $name) = @_; |
490 |
|
491 |
for (values %{$self->{loaded_maps}}) { |
492 |
$_->{map}->overlay ('selection') |
493 |
} |
494 |
|
495 |
$self->{edit_tool}->set_text ($name); |
496 |
$self->{sel_editaction} = $tool; |
497 |
|
498 |
my $widget = $tool->tool_widget; |
499 |
|
500 |
for ($self->{edit_tool_cont}->get_children) { |
501 |
$_->hide; |
502 |
$self->{edit_tool_cont}->remove ($_); |
503 |
} |
504 |
|
505 |
$_->set_edit_tool ($self->{sel_editaction}) for (values %{$self->{editors}}); |
506 |
|
507 |
defined $widget or return; |
508 |
|
509 |
$self->{edit_tool_cont}->add ($widget); |
510 |
$widget->show_all; |
511 |
} |
512 |
|
513 |
sub update_pick_view { |
514 |
my ($self, $arch) = @_; |
515 |
|
516 |
defined $arch->{_face} |
517 |
or $arch = $Deliantra::ARCH{$arch->{_name}}; |
518 |
|
519 |
fill_pb_from_arch ($self->{pick_view_pb}, $arch); |
520 |
$self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb}); |
521 |
|
522 |
$self->{pick_view_btn}->set_label ($arch->{_name}); |
523 |
} |
524 |
|
525 |
sub INIT_INSTANCE { |
526 |
my ($self) = @_; |
527 |
|
528 |
{ |
529 |
open my $fh, "<$recentfile"; |
530 |
binmode $fh; |
531 |
local $/; |
532 |
our @recent_entries = split /\0/, <$fh>; |
533 |
close $fh; |
534 |
} |
535 |
|
536 |
$::MAINWIN = $self; |
537 |
|
538 |
$self->Object::Event::init_object_events; |
539 |
|
540 |
$self->set_title ("deliantra editor - toolbox"); |
541 |
|
542 |
$self->{edit_tool} = Gtk2::Label->new; |
543 |
$self->{edit_tool_cont} = Gtk2::VBox->new; |
544 |
|
545 |
$self->add (my $vb = $self->{vb} = Gtk2::VBox->new); |
546 |
$vb->pack_start ($self->build_menu, 0, 1, 0); |
547 |
$vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0); |
548 |
|
549 |
$vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0); |
550 |
$vb->pack_start ($self->{edit_tool}, 0, 1, 0); |
551 |
|
552 |
$vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0); |
553 |
$vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0); |
554 |
|
555 |
# XXX:load $ARGV _cleanly_? |
556 |
$self->open_map_editor ($_) |
557 |
for @ARGV; |
558 |
|
559 |
$self->signal_connect ('delete-event' => sub { |
560 |
Gtk2->main_quit; |
561 |
}); |
562 |
|
563 |
::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0); |
564 |
|
565 |
|
566 |
$self->show_attr_editor; |
567 |
} |
568 |
|
569 |
sub new_cb { |
570 |
my ($self) = @_; |
571 |
|
572 |
my $w = Gtk2::Window->new ('toplevel'); |
573 |
my $width = [width => 20]; |
574 |
my $height = [height => 20]; |
575 |
$w->add (my $tbl = Gtk2::Table->new (2, 3)); |
576 |
add_table_widget ($tbl, 0, $width, 'string'); |
577 |
add_table_widget ($tbl, 1, $height, 'string'); |
578 |
add_table_widget ($tbl, 2, 'new', 'button', sub { |
579 |
if ($width->[1] > 0 and $height->[1] > 0) { |
580 |
my $map = Deliantra::Map->new ($width->[1], $height->[1]); |
581 |
$map->{info}->{width} = $width->[1]; |
582 |
$map->{info}->{height} = $height->[1]; |
583 |
$map->resize ($width->[1], $height->[1]); |
584 |
$self->open_map_editor ($map); |
585 |
} |
586 |
$w->destroy; |
587 |
1; |
588 |
}); |
589 |
add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy }); |
590 |
$w->show_all; |
591 |
} |
592 |
|
593 |
sub new_filechooser { |
594 |
my ($self, $title, $save, $filename) = @_; |
595 |
|
596 |
$title ||= 'deliantra editor - open map'; |
597 |
my $fc = new Gtk2::FileChooserDialog ( |
598 |
$title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok' |
599 |
); |
600 |
|
601 |
my @shortcut_folders = |
602 |
grep { $_ && ($_ ne '') && -e $_ } keys %{$self->{fc_last_folders}}; |
603 |
|
604 |
$fc->add_shortcut_folder ($_) for @shortcut_folders; |
605 |
|
606 |
unless (grep { $::MAPDIR eq $_ } @shortcut_folders) { |
607 |
$fc->add_shortcut_folder ($::MAPDIR) |
608 |
if -d $::MAPDIR; |
609 |
} |
610 |
|
611 |
$fc->set_current_folder (getcwd); |
612 |
|
613 |
if ($filename) { |
614 |
$fc->set_filename ($filename); |
615 |
} |
616 |
|
617 |
$fc |
618 |
} |
619 |
|
620 |
sub new_coord_query { |
621 |
my ($self, $finishcb) = @_; |
622 |
|
623 |
my $coordhash = { x => 105, y => 115, worldmap => 1, overlay => 0 }; |
624 |
my $diag = GCE::HashDialogue->new; |
625 |
$self->{"worldmap_coord_query"} = $diag; |
626 |
$diag->signal_connect (destroy => sub { |
627 |
delete $self->{"worldmap_coord_query"}; |
628 |
}); |
629 |
$diag->init ( |
630 |
layout_name => 'worldmap_coord_query', |
631 |
info => "Open worldmap at ...", |
632 |
dialog_default_size => [ 200, 200, 200, 0 ], |
633 |
title => 'Worldmap coordinate entry', |
634 |
ref_hash => $coordhash, |
635 |
dialog => [ |
636 |
[x => 'X Coordinate' => 'spin', sub { (0, 999, 1) }], |
637 |
[y => 'Y Coordinate' => 'spin', sub { (0, 999, 1) }], |
638 |
[worldmap => 'Open worldmap' => 'check'], |
639 |
[overlay => 'Open overlay (CF+)' => 'check'], |
640 |
], |
641 |
save_button_label => 'open', |
642 |
save_cb => sub { |
643 |
$finishcb->($_[0]); |
644 |
} |
645 |
); |
646 |
$diag->show_all; |
647 |
} |
648 |
|
649 |
sub open_worldmap_cb { |
650 |
my ($self) = @_; |
651 |
|
652 |
$self->new_coord_query (sub { |
653 |
my ($info) = @_; |
654 |
my ($x, $y) = ($info->{x}, $info->{y}); |
655 |
my ($worldmap, $overlay) = ($info->{worldmap}, $info->{overlay}); |
656 |
$self->open_map_editor ($::MAPDIR . "/world/world_$x\_$y") |
657 |
if $worldmap; |
658 |
$self->open_map_editor ($::MAPDIR . "/world-overlay/world_$x\_$y") |
659 |
if $overlay; |
660 |
}); |
661 |
} |
662 |
|
663 |
sub open_cb { |
664 |
my ($self) = @_; |
665 |
|
666 |
my $fc = $self->new_filechooser; |
667 |
|
668 |
if ('ok' eq $fc->run) { |
669 |
$self->{fc_last_folder} = $fc->get_current_folder; |
670 |
$self->{fc_last_folders}->{$self->{fc_last_folder}}++; |
671 |
|
672 |
$self->open_map_editor ($fc->get_filename); |
673 |
} |
674 |
|
675 |
$fc->destroy; |
676 |
} |
677 |
|
678 |
sub get_pick { |
679 |
my ($self) = @_; |
680 |
|
681 |
$self->{attr_edit} |
682 |
or die "Couldn't find attribute editor! SERIOUS BUG!"; |
683 |
|
684 |
# XXX: This is just to make sure that this function always returns something |
685 |
|
686 |
my $ar = $self->{attr_edit}->get_arch; |
687 |
return { _name => 'empty_archetype' } unless defined $ar; |
688 |
return $ar->get; |
689 |
} |
690 |
|
691 |
# |
692 |
# Map/Cursor interface |
693 |
# |
694 |
|
695 |
sub cursor { |
696 |
my ($self, $map, @a) = @_; |
697 |
$map->cursor (@a) |
698 |
} |
699 |
|
700 |
sub broadcast_cursor_changes { |
701 |
my ($self, $map, $x, $y, $action, $invalid) = @_; |
702 |
my $cursor = $map->cursor_at ($x, $y); |
703 |
warn "CURSOR UPDTEAD($action, $invalid) $cursor!\n"; |
704 |
$cursor->changed ($action, $invalid) if defined $cursor; |
705 |
} |
706 |
|
707 |
=head1 AUTHOR |
708 |
|
709 |
Marc Lehmann <schmorp@schmorp.de> |
710 |
http://home.schmorp.de/ |
711 |
|
712 |
Robin Redeker <elmex@ta-sa.org> |
713 |
http://www.ta-sa.org/ |
714 |
|
715 |
=cut |
716 |
1; |
717 |
|