ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.46
Committed: Mon Mar 20 02:53:49 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.45: +41 -3 lines
Log Message:
added property window

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 Gtk2;
10 use Gtk2::Gdk::Keysyms;
11 use Gtk2::SimpleMenu;
12
13 use Crossfire;
14 use Crossfire::Map;
15 use Crossfire::MapWidget;
16
17 use GCE::AttrEdit;
18 use GCE::MapEditor;
19 use GCE::StackView;
20 use GCE::EditAction;
21 use GCE::PickWindow;
22
23 use GCE::AttrTypemap;
24
25 use Glib::Object::Subclass
26 Gtk2::Window;
27
28 use GCE::Util;
29
30 use strict;
31
32 # XXX: make a recursive call from save_layout to all (interesting) sub-widgets
33 sub save_layout {
34 my ($self) = @_;
35
36 $main::CFG->{attr_edit_on} = exists $self->{attr_edit} ? 1 : 0;
37 $main::CFG->{stack_view_on} = exists $self->{sv} ? 1 : 0;
38 $main::CFG->{picker_on} = exists $self->{last_pick_window} ? 1 : 0;
39 $main::CFG->{main_window} = main::get_pos_and_size ($self);
40 $main::CFG->{map_window} = main::get_pos_and_size ($self->{last_map_window}) if $self->{last_map_window};
41 $main::CFG->{stack_view} = main::get_pos_and_size ($self->{sv_win}) if $self->{sv_win};
42 $main::CFG->{attr_view} = main::get_pos_and_size ($self->{attr_edit_win}) if $self->{attr_edit_win};
43
44 $main::CFG->{last_folder} = $self->{fc_last_folder};
45 $main::CFG->{last_folders} = $self->{fc_last_folders};
46
47 $main::CFG->{open_pickers} = [];
48
49 for (@{$self->{open_pick_windows}}) {
50
51 next unless defined $_;
52
53 push @{$main::CFG->{open_pickers}}, {
54 p_and_s => main::get_pos_and_size ($_),
55 selection => $_->{last_selection}
56 };
57 }
58
59 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
60 }
61
62 sub load_layout {
63 my ($self) = @_;
64
65 $self->{fc_last_folder} = $main::CFG->{last_folder};
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 unless (ref $mapfile) {
103 unless (-e $mapfile) {
104 quick_msg ("file '$mapfile' does not exist!", 0);
105 return;
106 }
107 $self->{loaded_maps}->{$mapkey} = $w;
108 # XXX: loaded_maps entry is deleted in MapEditor on destroy event handler
109 }
110
111 $w->open_map ($mapfile, $mapkey);
112
113 ::set_pos_and_size ($w, $main::CFG->{map_window}, 500, 500, 200, 0);
114
115 $w->show_all;
116 }
117
118 sub show_stack_view {
119 my ($self) = @_;
120
121 return if defined $self->{sv};
122
123 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
124 $w->set_title ('gce - stack view');
125 $w->signal_connect (delete_event => sub { delete $self->{sv}; 0 });
126 $w->add ($self->{sv} = GCE::StackView->new);
127
128 main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250);
129
130 $w->show_all;
131 }
132
133 sub show_editor_properties {
134 my ($self) = @_;
135
136 return if $self->{prop_edit};
137
138 my $w = $self->{prop_edit} = Gtk2::Window->new;
139 $w->set_title ("gce - properties");
140 $w->add (my $t = Gtk2::Table->new (2, 4));
141 $t->attach_defaults (my $lbl1 = Gtk2::Label->new ("CROSSFIRE_LIBDIR"), 0, 1, 0, 1);
142 $t->attach_defaults (my $lib = Gtk2::Entry->new, 1, 2, 0, 1);
143 $lib->set_text ($::CFG->{LIBDIR});
144 $t->attach_defaults (my $lbl2 = Gtk2::Label->new ("Map path"), 0, 1, 1, 2);
145 $t->attach_defaults (my $map = Gtk2::Entry->new, 1, 2, 1, 2);
146 $map->set_text ($::CFG->{MAPDIR});
147 $t->attach_defaults (my $save = Gtk2::Button->new ('save'), 0, 2, 2, 3);
148 $save->signal_connect (clicked => sub {
149 $::CFG->{LIBDIR} = $lib->get_text;
150 $::CFG->{MAPDIR} = $map->get_text;
151 Crossfire::set_libdir ($::CFG->{LIBDIR});
152 Crossfire::load_archetypes;
153 Crossfire::load_tilecache;
154 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
155 $w->destroy;
156 });
157 $t->attach_defaults (my $close = Gtk2::Button->new ('close'), 0, 2, 3, 4);
158 $close->signal_connect (clicked => sub { $w->destroy });
159
160 $w->signal_connect (delete_event => sub { delete $self->{prop_edit}; 0 });
161
162 main::set_pos_and_size ($w, $main::CFG->{prop_edit}, 200, 200);
163
164 $w->show_all;
165 }
166
167 sub show_attr_editor {
168 my ($self) = @_;
169
170 return if $self->{attr_edit};
171
172 my $w = $self->{attr_edit_win} = Gtk2::Window->new;
173 $w->set_title ("gce - edit attrs");
174 $w->add ($self->{attr_edit} = GCE::AttrEdit->new);
175 $w->signal_connect (delete_event => sub { delete $self->{attr_edit}; 0 });
176
177 main::set_pos_and_size ($w, $main::CFG->{attr_view}, 200, 200);
178
179 $w->show_all;
180 }
181
182 sub update_attr_editor {
183 my ($self, $arch, $cb) = @_;
184
185 return unless $self->{attr_edit};
186
187 $self->{attr_edit}->set_arch ($arch, $cb);
188 $self->{attr_edit_win}->set_title ("gce - edit $arch->{_name}");
189 }
190
191 sub update_stack_view {
192 my ($self, $mapedit, $x, $y) = @_;
193
194 return unless $self->{sv};
195
196 $self->{sv}->set_stack ($mapedit, $x, $y);
197 }
198
199 sub open_pick_window {
200 my ($self, $layout) = @_;
201
202 # XXX: Yes, also fix this, save _every_ pick window and their positions and their
203 # selection
204 my $p = GCE::PickWindow->new ();
205
206 push @{$self->{open_pick_windows}}, $p;
207
208 my $idx = (@{$self->{open_pick_windows}}) - 1;
209
210 $p->signal_connect ('delete-event' => sub {
211 $self->{open_pick_windows}->[$idx] = undef;
212 });
213
214 if ($layout) {
215 main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200);
216 }
217
218 $p->show_all;
219
220 $p->set_selection ($layout->{selection});
221 }
222
223 sub build_menu {
224 my ($self) = @_;
225
226 my $menu_tree = [
227 _File => {
228 item_type => '<Branch>',
229 children => [
230 _New => {
231 callback => sub { $self->new_cb },
232 accelerator => '<ctrl>N'
233 },
234 _Open => {
235 callback => sub { $self->open_cb },
236 accelerator => '<ctrl>O'
237 },
238 "_Save Layout" => {
239 callback => sub { $self->save_layout },
240 accelerator => '<ctrl>L'
241 },
242 "_Properties" => {
243 callback => sub { $self->show_editor_properties },
244 accelerator => "<ctrl>T"
245 },
246 _Quit => {
247 callback => sub { Gtk2->main_quit },
248 accelerator => '<ctrl>Q'
249 }
250 ]
251 },
252 _Dialogs => {
253 item_type => '<Branch>',
254 children => [
255 "_Attr Editor" => {
256 callback => sub { $self->show_attr_editor },
257 accelerator => "<ctrl>A"
258 },
259 "_Picker" => {
260 callback => sub { $self->open_pick_window },
261 accelerator => "<ctrl>P"
262 },
263 "_Stack View" => {
264 callback => sub { $self->show_stack_view },
265 accelerator => "<ctrl>V"
266 },
267 ]
268 }
269
270 ];
271
272 my $men =
273 Gtk2::SimpleMenu->new (
274 menu_tree => $menu_tree,
275 default_callback => \&default_cb,
276 );
277
278 $self->add_accel_group ($men->{accel_group});
279
280 return $men->{widget};
281 }
282
283 sub add_button {
284 my ($self, $table, $plcinfo, $lbl, $cb) = @_;
285
286 my ($lx, $ly) = @{$plcinfo->{next}};
287
288 unless ($lx < $plcinfo->{width}) {
289
290 $ly++;
291 $lx = 0;
292 }
293
294 $ly < $plcinfo->{height}
295 or die "too many buttons, make table bigger!";
296
297 $table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1);
298 $btn->signal_connect (clicked => $cb);
299
300 $plcinfo->{next} = [$lx + 1, $ly];
301 }
302
303 sub build_buttons {
304 my ($self) = @_;
305
306 my $tbl = Gtk2::Table->new (2, 4);
307 my $plcinfo = { width => 2, height => 4, next => [0, 0] };
308
309 $self->{edit_collection}{pick} = GCE::EditAction::Pick->new;
310 $self->{edit_collection}{place} = GCE::EditAction::Place->new;
311 $self->{edit_collection}{erase} = GCE::EditAction::Erase->new;
312 $self->{edit_collection}{select} = GCE::EditAction::Select->new;
313 $self->{edit_collection}{perl} = GCE::EditAction::Perl->new;
314 $self->{edit_collection}{connectexit} = GCE::EditAction::ConnectExit->new;
315 $self->{edit_collection}{followexit} = GCE::EditAction::FollowExit->new;
316
317 $self->set_edit_tool ('pick');
318
319 $self->add_button ($tbl, $plcinfo, "P_ick", sub { $self->set_edit_tool ('pick') });
320 $self->add_button ($tbl, $plcinfo, "_Place", sub { $self->set_edit_tool ('place') });
321 $self->add_button ($tbl, $plcinfo, "_Erase", sub { $self->set_edit_tool ('erase') });
322 $self->add_button ($tbl, $plcinfo, "_Select", sub { $self->set_edit_tool ('select') });
323 $self->add_button ($tbl, $plcinfo, "Eva_l", sub { $self->set_edit_tool ('perl') });
324 $self->add_button ($tbl, $plcinfo, "Connect E_xit", sub { $self->set_edit_tool ('connectexit') });
325 $self->add_button ($tbl, $plcinfo, "_Follow Exit", sub { $self->set_edit_tool ('followexit') });
326
327 return $tbl;
328 }
329
330 sub set_edit_tool {
331 my ($self, $name) = @_;
332
333 if ($name eq 'pick') {
334 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
335 } elsif ($name eq 'place') {
336 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
337 } elsif ($name eq 'erase') {
338 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
339 } elsif ($name eq 'select') {
340 $self->update_edit_tool ($self->{edit_collection}{select}, "Select");;
341 } elsif ($name eq 'perl') {
342 $self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");;
343 } elsif ($name eq 'connectexit') {
344 $self->update_edit_tool ($self->{edit_collection}{connectexit}, "Connect Exit");;
345 } elsif ($name eq 'followexit') {
346 $self->update_edit_tool ($self->{edit_collection}{followexit}, "Follow Exit");;
347 }
348 }
349
350 sub update_edit_tool {
351 my ($self, $tool, $name) = @_;
352
353 $self->{edit_tool}->set_text ($name);
354 $self->{sel_editaction} = $tool;
355
356 my $widget = $tool->tool_widget;
357
358 for ($self->{edit_tool_cont}->get_children) {
359 $_->hide;
360 $self->{edit_tool_cont}->remove ($_);
361 }
362
363 defined $widget or return;
364
365 $self->{edit_tool_cont}->add ($widget);
366 $widget->show_all;
367 }
368
369 sub update_pick_view {
370 my ($self, $arch) = @_;
371
372 defined $arch->{_face}
373 or $arch = $Crossfire::ARCH{$arch->{_name}};
374
375 fill_pb_from_arch ($self->{pick_view_pb}, $arch);
376 $self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb});
377
378 $self->{pick_view_btn}->set_label ($arch->{_name});
379 }
380
381 sub INIT_INSTANCE {
382 my ($self) = @_;
383
384 $::MAINWIN = $self;
385
386 $self->set_title ("gce - main window");
387
388 $self->{edit_tool} = Gtk2::Label->new;
389 $self->{edit_tool_cont} = Gtk2::VBox->new;
390
391 $self->add (my $vb = Gtk2::VBox->new);
392 $vb->pack_start ($self->build_menu, 0, 1, 0);
393
394 $vb->pack_start (my $hb = $self->{pick_view_hb} = Gtk2::HBox->new, 0, 1, 0);
395 $hb->pack_start ($self->{pick_view_img} = Gtk2::Image->new, 0, 1, 0);
396 $hb->pack_start ($self->{pick_view_btn} = Gtk2::Button->new, 0, 1, 0);
397 $self->{pick_view_btn}->drag_source_set (['button1_mask'], ['move'],
398 { target => 'STRING', flags => [], info => 'TARGET_STRING' }
399 );
400 $self->{pick_view_btn}->signal_connect (drag_data_get => sub {
401 my ($widget, $context, $data, $info, $time) = @_;
402 $data->set ($data->target, 8, "pick");
403 });
404 $self->{pick_view_btn}->signal_connect (clicked => sub {
405 $self->update_attr_editor ($self->{pick_arch});
406 });
407 $self->{pick_view_pb} = new_arch_pb ();
408
409 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
410 $vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0);
411
412 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
413 $vb->pack_start ($self->{edit_tool}, 0, 1, 0);
414
415 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
416 $vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0);
417
418 # XXX:load $ARGV _cleanly_?
419 $self->open_map_editor ($_)
420 for @ARGV;
421
422 $self->signal_connect ('delete-event' => sub {
423 Gtk2->main_quit;
424 });
425
426 ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0);
427 }
428
429 sub new_cb {
430 my ($self) = @_;
431
432 my $w = Gtk2::Window->new ('toplevel');
433 my $width = [width => 20];
434 my $height = [height => 20];
435 $w->add (my $tbl = Gtk2::Table->new (2, 3));
436 add_table_widget ($tbl, 0, $width, 'string');
437 add_table_widget ($tbl, 1, $height, 'string');
438 add_table_widget ($tbl, 2, 'new', 'button', sub {
439 if ($width->[1] > 0 and $height->[1] > 0) {
440 my $map = Crossfire::Map->new ($width->[1], $height->[1]);
441 $map->resize ($width->[1], $height->[1]);
442 $self->open_map_editor ($map);
443 }
444 $w->destroy;
445 1;
446 });
447 add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy });
448 $w->show_all;
449 }
450
451 sub new_filechooser {
452 my ($self, $title, $save, $filename) = @_;
453
454 $title ||= 'gce - open map';
455 my $fc = new Gtk2::FileChooserDialog (
456 $title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
457 );
458
459 $fc->add_shortcut_folder ($::CFG->{MAPDIR}) if -d $::CFG->{MAPDIR};
460 $fc->add_shortcut_folder ($_) for grep { $_ && ($_ ne '') } keys %{$self->{fc_last_folders}};
461 $fc->set_current_folder ($self->{fc_last_folder} || $::CFG->{MAPDIR});
462
463 if ($filename) {
464 $fc->set_filename ($filename);
465 }
466
467 $fc
468 }
469
470 sub open_cb {
471 my ($self) = @_;
472
473 my $fc = $self->new_filechooser;
474
475 if ('ok' eq $fc->run) {
476
477 $self->{fc_last_folder} = $fc->get_current_folder;
478 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
479
480 $self->open_map_editor ($fc->get_filename);
481 }
482
483 $fc->destroy;
484 }
485
486 sub set_pick {
487 my ($self, $arch) = @_;
488
489 $self->{pick_arch} = $arch;
490 $self->update_pick_view ($arch);
491 }
492
493 sub get_pick {
494 my ($self) = @_;
495
496 # XXX: This is just to make sure that this function always returns something
497 return $self->{pick_arch} || { _name => 'platinacoin' };
498 }
499
500 =head1 AUTHOR
501
502 Marc Lehmann <schmorp@schmorp.de>
503 http://home.schmorp.de/
504
505 Robin Redeker <elmex@ta-sa.org>
506 http://www.ta-sa.org/
507
508 =cut
509 1;
510