ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.85.2.3
Committed: Mon Dec 21 15:51:30 2009 UTC (14 years, 5 months ago) by elmex
Branch: cursor
Changes since 1.85.2.2: +10 -15 lines
Log Message:
rewrote main parts in internal reference handling.

File Contents

# User Rev Content
1 elmex 1.1 package GCE::MainWindow;
2    
3     =head1 NAME
4    
5 elmex 1.85 GCE::MainWindow - the main window class for gde
6 elmex 1.1
7     =cut
8    
9 elmex 1.58 use Cwd qw/abs_path getcwd/;
10 elmex 1.1 use Gtk2;
11     use Gtk2::Gdk::Keysyms;
12 elmex 1.2 use Gtk2::SimpleMenu;
13 root 1.7
14 root 1.83 use Deliantra;
15     use Deliantra::Map;
16     use Deliantra::MapWidget;
17 root 1.7
18 elmex 1.2 use GCE::AttrEdit;
19 elmex 1.13 use GCE::MapEditor;
20 elmex 1.15 use GCE::StackView;
21 elmex 1.17 use GCE::EditAction;
22     use GCE::PickWindow;
23 elmex 1.2
24 elmex 1.53 use Glib::Object::Subclass
25 elmex 1.13 Gtk2::Window;
26 elmex 1.2
27 elmex 1.28 use GCE::Util;
28 elmex 1.54 use GCE::DragHelper;
29 elmex 1.28
30 elmex 1.85.2.1 use base qw/Object::Event/;
31    
32 elmex 1.2 use strict;
33    
34 elmex 1.85 my $recentfile = "$Deliantra::VARDIR/gderecent";
35 elmex 1.80
36 elmex 1.18 # XXX: make a recursive call from save_layout to all (interesting) sub-widgets
37 elmex 1.17 sub save_layout {
38     my ($self) = @_;
39    
40 elmex 1.58 # $main::CFG->{attr_edit_on} = exists $self->{attr_edit} ? 1 : 0;
41 elmex 1.17 $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 elmex 1.70 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 elmex 1.69
52 elmex 1.75 $self->{worldmap_coord_query}->save_layout ()
53     if $self->{worldmap_coord_query};
54 elmex 1.73
55 elmex 1.37 $main::CFG->{last_folders} = $self->{fc_last_folders};
56    
57 elmex 1.17 $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 elmex 1.59 $self->{attr_edit}->save_layout;
70    
71 root 1.83 main::write_cfg ("$Deliantra::VARDIR/gdeconfig");
72 elmex 1.17 }
73    
74     sub load_layout {
75     my ($self) = @_;
76    
77 elmex 1.37 $self->{fc_last_folders} = $main::CFG->{last_folders};
78    
79 elmex 1.58 # $main::CFG->{attr_edit_on}
80     # and $self->show_attr_editor;
81 elmex 1.17
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 elmex 1.59
89     $self->{attr_edit}->load_layout;
90 elmex 1.17 }
91    
92     sub open_map_editor {
93     my ($self, $mapfile) = @_;
94    
95 elmex 1.44 my $mapkey;
96 elmex 1.39 unless (ref $mapfile) {
97 elmex 1.58 # unless (File::Spec->file_name_is_absolute ($mapfile)) {
98     # $mapfile = File::Spec->rel2abs ($mapfile);
99     # }
100     $mapkey = abs_path ($mapfile);
101 elmex 1.76 # File::Spec->abs2rel ($mapfile, File::Spec->catfile ($::MAPDIR));
102 elmex 1.58 } else {
103     $mapkey = "$mapfile";
104 elmex 1.38 }
105    
106 elmex 1.17 # XXX: last_map_window is a dirty trick to get the position and size
107     # for save layout
108    
109 elmex 1.58 if (defined $self->{loaded_maps}->{$mapkey}) {
110     $self->{loaded_maps}->{$mapkey}->get_toplevel->present;
111     return;
112 elmex 1.38 }
113    
114 elmex 1.17 my $w = $self->{last_map_window} = GCE::MapEditor->new;
115 root 1.25
116 elmex 1.53 $self->{editors}->{$w} = $w;
117    
118     $w->signal_connect (destroy => sub {
119     my ($w) = @_;
120 elmex 1.67 $w->close_windows;
121 elmex 1.53 delete $self->{loaded_maps}->{$w->{mapkey}};
122     delete $self->{editors}->{$w};
123     0;
124     });
125    
126 elmex 1.58 $self->{loaded_maps}->{$mapkey} = $w;
127 elmex 1.38
128 elmex 1.72 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 elmex 1.17
138 elmex 1.54 $w->set_edit_tool ($self->{sel_editaction});
139    
140 elmex 1.17 $w->show_all;
141     }
142    
143 elmex 1.47 sub show_help_window {
144     my ($self) = @_;
145    
146     return if defined $self->{help_win};
147 elmex 1.51 require Gtk2::Ex::PodViewer;
148 elmex 1.47 my $w = $self->{help_win} = Gtk2::Window->new;
149 elmex 1.82 $w->set_title ("deliantra editor - help");
150 elmex 1.47 $w->set_default_size (500, 300);
151 elmex 1.56 $w->signal_connect (destroy => sub {
152 elmex 1.53 $self->{help_win}->hide; $self->{help_win} = undef;
153     0
154     });
155 elmex 1.47 $w->add (my $sw = Gtk2::ScrolledWindow->new);
156 elmex 1.51 $sw->add (my $h = Gtk2::Ex::PodViewer->new);
157 elmex 1.47 $h->load_string ($::DOCUMENTATION);
158     $w->show_all;
159     }
160    
161 elmex 1.17 sub show_stack_view {
162 elmex 1.15 my ($self) = @_;
163    
164     return if defined $self->{sv};
165    
166 elmex 1.17 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
167 elmex 1.82 $w->set_title ('deliantra editor - stack view');
168 elmex 1.56 $w->signal_connect (destroy => sub { delete $self->{sv}; 0 });
169 elmex 1.15 $w->add ($self->{sv} = GCE::StackView->new);
170 elmex 1.17
171 elmex 1.30 main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250);
172 elmex 1.17
173 elmex 1.15 $w->show_all;
174     }
175    
176 elmex 1.46 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 elmex 1.82 $w->set_title ("deliantra editor - preferences");
183 elmex 1.74 $w->add (my $t = Gtk2::Table->new (2, 5));
184 elmex 1.68 $t->attach_defaults (my $lbl1 = Gtk2::Label->new ("LIBDIR"), 0, 1, 0, 1);
185 elmex 1.46 $t->attach_defaults (my $lib = Gtk2::Entry->new, 1, 2, 0, 1);
186     $lib->set_text ($::CFG->{LIBDIR});
187 elmex 1.68 $t->attach_defaults (my $lbl2 = Gtk2::Label->new ("MAPDIR"), 0, 1, 1, 2);
188 elmex 1.46 $t->attach_defaults (my $map = Gtk2::Entry->new, 1, 2, 1, 2);
189     $map->set_text ($::CFG->{MAPDIR});
190 elmex 1.74 $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 elmex 1.46 $save->signal_connect (clicked => sub {
195 elmex 1.74 $::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 root 1.83 Deliantra::set_libdir ($::LIBDIR);
201     Deliantra::load_archetypes;
202     Deliantra::load_tilecache;
203     main::write_cfg ("$Deliantra::VARDIR/gdeconfig");
204 elmex 1.46 $w->destroy;
205     });
206 elmex 1.74 $t->attach_defaults (my $close = Gtk2::Button->new ('close'), 0, 2, 4, 5);
207 elmex 1.46 $close->signal_connect (clicked => sub { $w->destroy });
208    
209 elmex 1.56 $w->signal_connect (destroy => sub { delete $self->{prop_edit}; 0 });
210 elmex 1.46
211     main::set_pos_and_size ($w, $main::CFG->{prop_edit}, 200, 200);
212    
213     $w->show_all;
214     }
215    
216 elmex 1.15 sub show_attr_editor {
217     my ($self) = @_;
218    
219 elmex 1.79 return if $self->{attr_edit_win};
220    
221 elmex 1.15 my $w = $self->{attr_edit_win} = Gtk2::Window->new;
222 elmex 1.82 $w->set_title ("deliantra editor - edit attrs");
223 elmex 1.15 $w->add ($self->{attr_edit} = GCE::AttrEdit->new);
224 elmex 1.79 $w->signal_connect (destroy => sub { delete $self->{attr_edit_win}; 0 });
225 elmex 1.17
226 elmex 1.64 main::set_pos_and_size ($w, $main::CFG->{attr_view}, 400, 300, 250, 0);
227 elmex 1.17
228 elmex 1.15 $w->show_all;
229     }
230    
231     sub update_attr_editor {
232 elmex 1.85.2.3 my ($self, $ar) = @_;
233 elmex 1.15
234 elmex 1.58 $self->{attr_edit}
235 elmex 1.81 or return;
236 elmex 1.15
237 elmex 1.85.2.3 $self->{attr_edit}->update_view ($ar);
238 elmex 1.82 $self->{attr_edit_win}->set_title ("deliantra editor - edit " . $ar->longname);
239 elmex 1.15 }
240    
241 elmex 1.85.2.2 #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 elmex 1.81
247 elmex 1.15 sub update_stack_view {
248 elmex 1.85.2.3 my ($self, $sr) = @_;
249 elmex 1.15
250     return unless $self->{sv};
251    
252 elmex 1.85.2.3 $self->{sv}->set_stack ($sr);
253 elmex 1.15 }
254    
255 elmex 1.17 sub open_pick_window {
256     my ($self, $layout) = @_;
257    
258 root 1.29 # XXX: Yes, also fix this, save _every_ pick window and their positions and their
259 elmex 1.17 # selection
260     my $p = GCE::PickWindow->new ();
261 elmex 1.15
262 elmex 1.17 push @{$self->{open_pick_windows}}, $p;
263    
264     my $idx = (@{$self->{open_pick_windows}}) - 1;
265 elmex 1.15
266 elmex 1.17 $p->signal_connect ('delete-event' => sub {
267     $self->{open_pick_windows}->[$idx] = undef;
268     });
269 elmex 1.10
270 elmex 1.17 if ($layout) {
271 elmex 1.30 main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200);
272 elmex 1.17 }
273 elmex 1.10
274 elmex 1.17 $p->show_all;
275 elmex 1.10
276 elmex 1.17 $p->set_selection ($layout->{selection});
277 elmex 1.10 }
278    
279 elmex 1.80 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 elmex 1.84 $str =~ s/_/-/g;
304 elmex 1.80
305     $str;
306     }
307    
308     sub recent {
309     my ($self) = @_;
310     my @recent;
311    
312     our @recent_entries;
313    
314 elmex 1.84 foreach my $entry (@recent_entries) {
315 elmex 1.80 push @recent, escape_filename($entry) => {
316 elmex 1.84 callback => sub {
317     $self->open_map_editor($entry);
318     $self->build_menu
319     }
320 elmex 1.80 };
321     }
322    
323     push @recent, Empty => { callback => sub {} }
324     unless @recent;
325    
326     @recent;
327     }
328    
329 elmex 1.13 sub build_menu {
330 elmex 1.1 my ($self) = @_;
331    
332 elmex 1.2 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 elmex 1.71 'Open special' => {
345     item_type => '<Branch>',
346     children => [
347     "world map at"=> {
348     callback => sub { $self->open_worldmap_cb },
349     },
350 elmex 1.80 "recent files"=> {
351     item_type => '<Branch>',
352     children => [ $self->recent ],
353     },
354 elmex 1.71 ]
355     },
356 elmex 1.17 "_Save Layout" => {
357     callback => sub { $self->save_layout },
358     accelerator => '<ctrl>L'
359     },
360 elmex 1.52 "_Preferences" => {
361 elmex 1.46 callback => sub { $self->show_editor_properties },
362     accelerator => "<ctrl>T"
363     },
364 elmex 1.49 _Quit => {
365 elmex 1.2 callback => sub { Gtk2->main_quit },
366     accelerator => '<ctrl>Q'
367 elmex 1.47 },
368 elmex 1.2 ]
369     },
370 root 1.24 _Dialogs => {
371 elmex 1.2 item_type => '<Branch>',
372     children => [
373 elmex 1.17 "_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 elmex 1.79 "_Attributes" => {
382     callback => sub { $self->show_attr_editor },
383     accelerator => "<ctrl>A"
384     },
385 elmex 1.2 ]
386 elmex 1.47 },
387 elmex 1.49 _Help => {
388     item_type => '<Branch>',
389     children => [
390 elmex 1.50 _Manual => {
391 elmex 1.49 callback => sub { $self->show_help_window },
392     accelerator => "<ctrl>H"
393     },
394     ]
395     },
396 elmex 1.2 ];
397    
398 elmex 1.13 my $men =
399     Gtk2::SimpleMenu->new (
400     menu_tree => $menu_tree,
401     default_callback => \&default_cb,
402     );
403 elmex 1.10
404 elmex 1.80 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 elmex 1.2 $self->add_accel_group ($men->{accel_group});
416    
417 elmex 1.13 return $men->{widget};
418     }
419 elmex 1.10
420 elmex 1.17 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 elmex 1.42 $table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1);
435 elmex 1.17 $btn->signal_connect (clicked => $cb);
436    
437     $plcinfo->{next} = [$lx + 1, $ly];
438     }
439    
440     sub build_buttons {
441     my ($self) = @_;
442    
443 elmex 1.38 my $tbl = Gtk2::Table->new (2, 4);
444     my $plcinfo = { width => 2, height => 4, next => [0, 0] };
445 elmex 1.17
446 elmex 1.38 $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 elmex 1.63 $self->{edit_collection}{connect} = GCE::EditAction::Connect->new;
452 elmex 1.38 $self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new;
453 elmex 1.17
454 elmex 1.33 $self->set_edit_tool ('pick');
455 elmex 1.28
456 elmex 1.42 $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 elmex 1.63 $self->add_button ($tbl, $plcinfo, "Connec_t", sub { $self->set_edit_tool ('connect') });
462 elmex 1.42 $self->add_button ($tbl, $plcinfo, "_Follow Exit", sub { $self->set_edit_tool ('followexit') });
463 elmex 1.33
464     return $tbl;
465     }
466    
467     sub set_edit_tool {
468     my ($self, $name) = @_;
469    
470     if ($name eq 'pick') {
471 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
472 elmex 1.33 } elsif ($name eq 'place') {
473 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
474 elmex 1.33 } elsif ($name eq 'erase') {
475 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
476 elmex 1.34 } elsif ($name eq 'select') {
477     $self->update_edit_tool ($self->{edit_collection}{select}, "Select");;
478 elmex 1.57 $self->{edit_collection}{select}->update_overlay;
479 elmex 1.35 } elsif ($name eq 'perl') {
480     $self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");;
481 elmex 1.63 } elsif ($name eq 'connect') {
482     $self->update_edit_tool ($self->{edit_collection}{connect}, "Connect");;
483 elmex 1.38 } elsif ($name eq 'followexit') {
484     $self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");;
485 elmex 1.33 }
486 elmex 1.17 }
487 elmex 1.10
488 elmex 1.28 sub update_edit_tool {
489     my ($self, $tool, $name) = @_;
490    
491 elmex 1.57 for (values %{$self->{loaded_maps}}) {
492     $_->{map}->overlay ('selection')
493     }
494    
495 elmex 1.28 $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 elmex 1.53 $_->set_edit_tool ($self->{sel_editaction}) for (values %{$self->{editors}});
506    
507 elmex 1.28 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 root 1.83 or $arch = $Deliantra::ARCH{$arch->{_name}};
518 elmex 1.28
519     fill_pb_from_arch ($self->{pick_view_pb}, $arch);
520     $self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb});
521    
522 elmex 1.33 $self->{pick_view_btn}->set_label ($arch->{_name});
523 elmex 1.28 }
524    
525 elmex 1.13 sub INIT_INSTANCE {
526     my ($self) = @_;
527 elmex 1.10
528 elmex 1.80 {
529     open my $fh, "<$recentfile";
530     binmode $fh;
531     local $/;
532     our @recent_entries = split /\0/, <$fh>;
533     close $fh;
534     }
535    
536 root 1.24 $::MAINWIN = $self;
537 elmex 1.15
538 elmex 1.85.2.2 $self->Object::Event::init_object_events;
539 elmex 1.85.2.1
540 elmex 1.82 $self->set_title ("deliantra editor - toolbox");
541 elmex 1.10
542 elmex 1.28 $self->{edit_tool} = Gtk2::Label->new;
543     $self->{edit_tool_cont} = Gtk2::VBox->new;
544    
545 elmex 1.80 $self->add (my $vb = $self->{vb} = Gtk2::VBox->new);
546 elmex 1.13 $vb->pack_start ($self->build_menu, 0, 1, 0);
547 elmex 1.28 $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 elmex 1.35 $vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0);
554 elmex 1.10
555 elmex 1.13 # XXX:load $ARGV _cleanly_?
556 root 1.27 $self->open_map_editor ($_)
557     for @ARGV;
558 root 1.11
559 elmex 1.13 $self->signal_connect ('delete-event' => sub {
560     Gtk2->main_quit;
561 elmex 1.10 });
562 elmex 1.17
563 elmex 1.30 ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0);
564 elmex 1.58
565    
566     $self->show_attr_editor;
567 elmex 1.2 }
568    
569     sub new_cb {
570     my ($self) = @_;
571 elmex 1.32
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 root 1.83 my $map = Deliantra::Map->new ($width->[1], $height->[1]);
581 elmex 1.69 $map->{info}->{width} = $width->[1];
582     $map->{info}->{height} = $height->[1];
583 elmex 1.33 $map->resize ($width->[1], $height->[1]);
584 elmex 1.32 $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 elmex 1.2 }
592    
593 root 1.25 sub new_filechooser {
594 elmex 1.37 my ($self, $title, $save, $filename) = @_;
595 elmex 1.2
596 elmex 1.82 $title ||= 'deliantra editor - open map';
597 root 1.25 my $fc = new Gtk2::FileChooserDialog (
598 elmex 1.33 $title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
599 elmex 1.13 );
600    
601 elmex 1.78 my @shortcut_folders =
602     grep { $_ && ($_ ne '') && -e $_ } keys %{$self->{fc_last_folders}};
603 elmex 1.76
604 elmex 1.78 $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 elmex 1.76
611 elmex 1.55 $fc->set_current_folder (getcwd);
612 elmex 1.2
613 elmex 1.37 if ($filename) {
614     $fc->set_filename ($filename);
615     }
616    
617 root 1.25 $fc
618     }
619    
620 elmex 1.71 sub new_coord_query {
621 elmex 1.75 my ($self, $finishcb) = @_;
622 elmex 1.71
623 elmex 1.75 my $coordhash = { x => 105, y => 115, worldmap => 1, overlay => 0 };
624 elmex 1.71 my $diag = GCE::HashDialogue->new;
625 elmex 1.75 $self->{"worldmap_coord_query"} = $diag;
626 elmex 1.73 $diag->signal_connect (destroy => sub {
627 elmex 1.75 delete $self->{"worldmap_coord_query"};
628 elmex 1.73 });
629 elmex 1.71 $diag->init (
630 elmex 1.75 layout_name => 'worldmap_coord_query',
631     info => "Open worldmap at ...",
632 elmex 1.71 dialog_default_size => [ 200, 200, 200, 0 ],
633     title => 'Worldmap coordinate entry',
634     ref_hash => $coordhash,
635     dialog => [
636 elmex 1.77 [x => 'X Coordinate' => 'spin', sub { (0, 999, 1) }],
637     [y => 'Y Coordinate' => 'spin', sub { (0, 999, 1) }],
638 elmex 1.75 [worldmap => 'Open worldmap' => 'check'],
639     [overlay => 'Open overlay (CF+)' => 'check'],
640 elmex 1.71 ],
641     save_button_label => 'open',
642     save_cb => sub {
643 elmex 1.75 $finishcb->($_[0]);
644 elmex 1.71 }
645     );
646     $diag->show_all;
647     }
648    
649     sub open_worldmap_cb {
650 elmex 1.75 my ($self) = @_;
651 elmex 1.71
652 elmex 1.75 $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 elmex 1.76 $self->open_map_editor ($::MAPDIR . "/world/world_$x\_$y")
657 elmex 1.75 if $worldmap;
658 elmex 1.76 $self->open_map_editor ($::MAPDIR . "/world-overlay/world_$x\_$y")
659 elmex 1.75 if $overlay;
660 elmex 1.71 });
661     }
662    
663 root 1.25 sub open_cb {
664     my ($self) = @_;
665    
666     my $fc = $self->new_filechooser;
667    
668 elmex 1.2 if ('ok' eq $fc->run) {
669 elmex 1.10 $self->{fc_last_folder} = $fc->get_current_folder;
670 elmex 1.13 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
671 elmex 1.17
672     $self->open_map_editor ($fc->get_filename);
673 elmex 1.2 }
674    
675     $fc->destroy;
676     }
677    
678 elmex 1.17 sub get_pick {
679     my ($self) = @_;
680    
681 elmex 1.58 $self->{attr_edit}
682     or die "Couldn't find attribute editor! SERIOUS BUG!";
683    
684 elmex 1.17 # XXX: This is just to make sure that this function always returns something
685 elmex 1.61
686     my $ar = $self->{attr_edit}->get_arch;
687 elmex 1.85.2.3 return { _name => 'empty_archetype' } unless defined $ar;
688     return $ar->get;
689 elmex 1.17 }
690    
691 elmex 1.85.2.1 #
692     # Map/Cursor interface
693     #
694    
695     sub cursor {
696     my ($self, $map, @a) = @_;
697     $map->cursor (@a)
698     }
699    
700 elmex 1.85.2.2 sub broadcast_cursor_changes {
701 elmex 1.85.2.3 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 elmex 1.85.2.2 }
706 elmex 1.85.2.1
707 elmex 1.1 =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