ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.58
Committed: Tue Apr 4 11:29:54 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.57: +45 -49 lines
Log Message:
Removed pick and made the attribut editor 'the current pick'.
Added context menu to map view with an follow item in it.

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