ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.61
Committed: Tue Apr 4 21:12:08 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.60: +9 -27 lines
Log Message:
Changed updating of attribute editor and inventory editor in a major manner.
We have now an abstraction layer between attribute editor and arch/object that
handles updating of the map and calling change_begin/set/end.

Works fine after testing, please watchout for bugs in attribute editor,
inventory editor and updating of these.

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 $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 delete $self->{loaded_maps}->{$w->{mapkey}};
112 delete $self->{editors}->{$w};
113 0;
114 });
115
116 $self->{loaded_maps}->{$mapkey} = $w;
117
118 $w->open_map ($mapfile, $mapkey);
119
120 ::set_pos_and_size ($w, $main::CFG->{map_window}, 500, 500, 200, 0);
121
122 $w->set_edit_tool ($self->{sel_editaction});
123
124 $w->show_all;
125 }
126
127 sub show_help_window {
128 my ($self) = @_;
129
130 return if defined $self->{help_win};
131 require Gtk2::Ex::PodViewer;
132 my $w = $self->{help_win} = Gtk2::Window->new;
133 $w->set_title ("gce - help");
134 $w->set_default_size (500, 300);
135 $w->signal_connect (destroy => sub {
136 $self->{help_win}->hide; $self->{help_win} = undef;
137 0
138 });
139 $w->add (my $sw = Gtk2::ScrolledWindow->new);
140 $sw->add (my $h = Gtk2::Ex::PodViewer->new);
141 $h->load_string ($::DOCUMENTATION);
142 $w->show_all;
143 }
144
145 sub show_stack_view {
146 my ($self) = @_;
147
148 return if defined $self->{sv};
149
150 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
151 $w->set_title ('gce - stack view');
152 $w->signal_connect (destroy => sub { delete $self->{sv}; 0 });
153 $w->add ($self->{sv} = GCE::StackView->new);
154
155 main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250);
156
157 $w->show_all;
158 }
159
160 sub show_editor_properties {
161 my ($self) = @_;
162
163 return if $self->{prop_edit};
164
165 my $w = $self->{prop_edit} = Gtk2::Window->new;
166 $w->set_title ("gce - preferences");
167 $w->add (my $t = Gtk2::Table->new (2, 4));
168 $t->attach_defaults (my $lbl1 = Gtk2::Label->new ("CROSSFIRE_LIBDIR"), 0, 1, 0, 1);
169 $t->attach_defaults (my $lib = Gtk2::Entry->new, 1, 2, 0, 1);
170 $lib->set_text ($::CFG->{LIBDIR});
171 $t->attach_defaults (my $lbl2 = Gtk2::Label->new ("Map path"), 0, 1, 1, 2);
172 $t->attach_defaults (my $map = Gtk2::Entry->new, 1, 2, 1, 2);
173 $map->set_text ($::CFG->{MAPDIR});
174 $t->attach_defaults (my $save = Gtk2::Button->new ('save'), 0, 2, 2, 3);
175 $save->signal_connect (clicked => sub {
176 $::CFG->{LIBDIR} = $lib->get_text;
177 $::CFG->{MAPDIR} = $map->get_text;
178 Crossfire::set_libdir ($::CFG->{LIBDIR});
179 Crossfire::load_archetypes;
180 Crossfire::load_tilecache;
181 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
182 $w->destroy;
183 });
184 $t->attach_defaults (my $close = Gtk2::Button->new ('close'), 0, 2, 3, 4);
185 $close->signal_connect (clicked => sub { $w->destroy });
186
187 $w->signal_connect (destroy => sub { delete $self->{prop_edit}; 0 });
188
189 main::set_pos_and_size ($w, $main::CFG->{prop_edit}, 200, 200);
190
191 $w->show_all;
192 }
193
194 sub show_attr_editor {
195 my ($self) = @_;
196
197 my $w = $self->{attr_edit_win} = Gtk2::Window->new;
198 $w->set_title ("gce - edit attrs");
199 $w->add ($self->{attr_edit} = GCE::AttrEdit->new);
200 $w->signal_connect (destroy => sub { Gtk2->main_quit; 0 });
201
202 main::set_pos_and_size ($w, $main::CFG->{attr_view}, 400, 300);
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 ("gce - 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 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 "_Picker" => {
284 callback => sub { $self->open_pick_window },
285 accelerator => "<ctrl>P"
286 },
287 "_Stack View" => {
288 callback => sub { $self->show_stack_view },
289 accelerator => "<ctrl>V"
290 },
291 ]
292 },
293 _Help => {
294 item_type => '<Branch>',
295 children => [
296 _Manual => {
297 callback => sub { $self->show_help_window },
298 accelerator => "<ctrl>H"
299 },
300 ]
301 },
302 ];
303
304 my $men =
305 Gtk2::SimpleMenu->new (
306 menu_tree => $menu_tree,
307 default_callback => \&default_cb,
308 );
309
310 $self->add_accel_group ($men->{accel_group});
311
312 return $men->{widget};
313 }
314
315 sub add_button {
316 my ($self, $table, $plcinfo, $lbl, $cb) = @_;
317
318 my ($lx, $ly) = @{$plcinfo->{next}};
319
320 unless ($lx < $plcinfo->{width}) {
321
322 $ly++;
323 $lx = 0;
324 }
325
326 $ly < $plcinfo->{height}
327 or die "too many buttons, make table bigger!";
328
329 $table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1);
330 $btn->signal_connect (clicked => $cb);
331
332 $plcinfo->{next} = [$lx + 1, $ly];
333 }
334
335 sub build_buttons {
336 my ($self) = @_;
337
338 my $tbl = Gtk2::Table->new (2, 4);
339 my $plcinfo = { width => 2, height => 4, next => [0, 0] };
340
341 $self->{edit_collection}{pick} = GCE::EditAction::Pick->new;
342 $self->{edit_collection}{place} = GCE::EditAction::Place->new;
343 $self->{edit_collection}{erase} = GCE::EditAction::Erase->new;
344 $self->{edit_collection}{select} = GCE::EditAction::Select->new;
345 $self->{edit_collection}{perl} = GCE::EditAction::Perl->new;
346 $self->{edit_collection}{connectexit} = GCE::EditAction::ConnectExit->new;
347 $self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new;
348
349 $self->set_edit_tool ('pick');
350
351 $self->add_button ($tbl, $plcinfo, "P_ick", sub { $self->set_edit_tool ('pick') });
352 $self->add_button ($tbl, $plcinfo, "_Place", sub { $self->set_edit_tool ('place') });
353 $self->add_button ($tbl, $plcinfo, "_Erase", sub { $self->set_edit_tool ('erase') });
354 $self->add_button ($tbl, $plcinfo, "_Select", sub { $self->set_edit_tool ('select') });
355 $self->add_button ($tbl, $plcinfo, "Eva_l", sub { $self->set_edit_tool ('perl') });
356 $self->add_button ($tbl, $plcinfo, "Connect E_xit", sub { $self->set_edit_tool ('connectexit') });
357 $self->add_button ($tbl, $plcinfo, "_Follow Exit", sub { $self->set_edit_tool ('followexit') });
358
359 return $tbl;
360 }
361
362 sub set_edit_tool {
363 my ($self, $name) = @_;
364
365 if ($name eq 'pick') {
366 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
367 } elsif ($name eq 'place') {
368 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
369 } elsif ($name eq 'erase') {
370 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
371 } elsif ($name eq 'select') {
372 $self->update_edit_tool ($self->{edit_collection}{select}, "Select");;
373 $self->{edit_collection}{select}->update_overlay;
374 } elsif ($name eq 'perl') {
375 $self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");;
376 } elsif ($name eq 'connectexit') {
377 $self->update_edit_tool ($self->{edit_collection}{connectexit}, "Connect Exit");;
378 } elsif ($name eq 'followexit') {
379 $self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");;
380 }
381 }
382
383 sub update_edit_tool {
384 my ($self, $tool, $name) = @_;
385
386 for (values %{$self->{loaded_maps}}) {
387 $_->{map}->overlay ('selection')
388 }
389
390 $self->{edit_tool}->set_text ($name);
391 $self->{sel_editaction} = $tool;
392
393 my $widget = $tool->tool_widget;
394
395 for ($self->{edit_tool_cont}->get_children) {
396 $_->hide;
397 $self->{edit_tool_cont}->remove ($_);
398 }
399
400 $_->set_edit_tool ($self->{sel_editaction}) for (values %{$self->{editors}});
401
402 defined $widget or return;
403
404 $self->{edit_tool_cont}->add ($widget);
405 $widget->show_all;
406 }
407
408 sub update_pick_view {
409 my ($self, $arch) = @_;
410
411 defined $arch->{_face}
412 or $arch = $Crossfire::ARCH{$arch->{_name}};
413
414 fill_pb_from_arch ($self->{pick_view_pb}, $arch);
415 $self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb});
416
417 $self->{pick_view_btn}->set_label ($arch->{_name});
418 }
419
420 sub INIT_INSTANCE {
421 my ($self) = @_;
422
423 $::MAINWIN = $self;
424
425 $self->set_title ("gce - toolbox");
426
427 $self->{edit_tool} = Gtk2::Label->new;
428 $self->{edit_tool_cont} = Gtk2::VBox->new;
429
430 $self->add (my $vb = Gtk2::VBox->new);
431 $vb->pack_start ($self->build_menu, 0, 1, 0);
432 $vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0);
433
434 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
435 $vb->pack_start ($self->{edit_tool}, 0, 1, 0);
436
437 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
438 $vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0);
439
440 # XXX:load $ARGV _cleanly_?
441 $self->open_map_editor ($_)
442 for @ARGV;
443
444 $self->signal_connect ('delete-event' => sub {
445 Gtk2->main_quit;
446 });
447
448 ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0);
449
450
451 $self->show_attr_editor;
452 }
453
454 sub new_cb {
455 my ($self) = @_;
456
457 my $w = Gtk2::Window->new ('toplevel');
458 my $width = [width => 20];
459 my $height = [height => 20];
460 $w->add (my $tbl = Gtk2::Table->new (2, 3));
461 add_table_widget ($tbl, 0, $width, 'string');
462 add_table_widget ($tbl, 1, $height, 'string');
463 add_table_widget ($tbl, 2, 'new', 'button', sub {
464 if ($width->[1] > 0 and $height->[1] > 0) {
465 my $map = Crossfire::Map->new ($width->[1], $height->[1]);
466 $map->resize ($width->[1], $height->[1]);
467 $self->open_map_editor ($map);
468 }
469 $w->destroy;
470 1;
471 });
472 add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy });
473 $w->show_all;
474 }
475
476 sub new_filechooser {
477 my ($self, $title, $save, $filename) = @_;
478
479 $title ||= 'gce - open map';
480 my $fc = new Gtk2::FileChooserDialog (
481 $title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
482 );
483
484 $fc->add_shortcut_folder ($::CFG->{MAPDIR}) if -d $::CFG->{MAPDIR};
485 $fc->add_shortcut_folder ($_) for grep { $_ && ($_ ne '') } keys %{$self->{fc_last_folders}};
486 $fc->set_current_folder (getcwd);
487
488 if ($filename) {
489 $fc->set_filename ($filename);
490 }
491
492 $fc
493 }
494
495 sub open_cb {
496 my ($self) = @_;
497
498 my $fc = $self->new_filechooser;
499
500 if ('ok' eq $fc->run) {
501
502 $self->{fc_last_folder} = $fc->get_current_folder;
503 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
504
505 $self->open_map_editor ($fc->get_filename);
506 }
507
508 $fc->destroy;
509 }
510
511 sub get_pick {
512 my ($self) = @_;
513
514 $self->{attr_edit}
515 or die "Couldn't find attribute editor! SERIOUS BUG!";
516
517 # XXX: This is just to make sure that this function always returns something
518
519 my $ar = $self->{attr_edit}->get_arch;
520 return { _name => 'platinacoin' } unless defined $ar;
521 return $ar->getarch || { _name => 'platinacoin' };
522 }
523
524 =head1 AUTHOR
525
526 Marc Lehmann <schmorp@schmorp.de>
527 http://home.schmorp.de/
528
529 Robin Redeker <elmex@ta-sa.org>
530 http://www.ta-sa.org/
531
532 =cut
533 1;
534