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

# User Rev Content
1 elmex 1.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 elmex 1.2 use Gtk2::SimpleMenu;
12 root 1.7
13     use Crossfire;
14 elmex 1.32 use Crossfire::Map;
15 root 1.7 use Crossfire::MapWidget;
16    
17 elmex 1.2 use GCE::AttrEdit;
18 elmex 1.13 use GCE::MapEditor;
19 elmex 1.15 use GCE::StackView;
20 elmex 1.17 use GCE::EditAction;
21     use GCE::PickWindow;
22 elmex 1.2
23 elmex 1.14 use GCE::AttrTypemap;
24    
25 elmex 1.2 use Glib::Object::Subclass
26 elmex 1.13 Gtk2::Window;
27 elmex 1.2
28 elmex 1.28 use GCE::Util;
29    
30 elmex 1.2 use strict;
31    
32 elmex 1.18 # XXX: make a recursive call from save_layout to all (interesting) sub-widgets
33 elmex 1.17 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 elmex 1.37 $main::CFG->{last_folder} = $self->{fc_last_folder};
45     $main::CFG->{last_folders} = $self->{fc_last_folders};
46    
47 elmex 1.17 $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 root 1.26 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
60 elmex 1.17 }
61    
62     sub load_layout {
63     my ($self) = @_;
64    
65 elmex 1.37 $self->{fc_last_folder} = $main::CFG->{last_folder};
66     $self->{fc_last_folders} = $main::CFG->{last_folders};
67    
68 elmex 1.17 $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 elmex 1.44 my $mapkey;
83 elmex 1.39 unless (ref $mapfile) {
84     unless (File::Spec->file_name_is_absolute ($mapfile)) {
85     $mapfile = File::Spec->rel2abs ($mapfile);
86     }
87 elmex 1.46 $mapkey = File::Spec->abs2rel ($mapfile, File::Spec->catfile ($::CFG->{MAPDIR}));
88 elmex 1.38 }
89    
90 elmex 1.17 # XXX: last_map_window is a dirty trick to get the position and size
91     # for save layout
92    
93 elmex 1.38 unless (ref $mapfile) {
94 elmex 1.44 if (defined $self->{loaded_maps}->{$mapkey}) {
95     $self->{loaded_maps}->{$mapkey}->get_toplevel->present;
96 elmex 1.38 return;
97     }
98     }
99    
100 elmex 1.17 my $w = $self->{last_map_window} = GCE::MapEditor->new;
101 root 1.25
102 elmex 1.38 unless (ref $mapfile) {
103 elmex 1.40 unless (-e $mapfile) {
104     quick_msg ("file '$mapfile' does not exist!", 0);
105     return;
106     }
107 elmex 1.44 $self->{loaded_maps}->{$mapkey} = $w;
108 elmex 1.43 # XXX: loaded_maps entry is deleted in MapEditor on destroy event handler
109 elmex 1.38 }
110    
111 elmex 1.44 $w->open_map ($mapfile, $mapkey);
112 elmex 1.17
113 elmex 1.30 ::set_pos_and_size ($w, $main::CFG->{map_window}, 500, 500, 200, 0);
114 elmex 1.17
115     $w->show_all;
116     }
117    
118     sub show_stack_view {
119 elmex 1.15 my ($self) = @_;
120    
121     return if defined $self->{sv};
122    
123 elmex 1.17 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
124 elmex 1.15 $w->set_title ('gce - stack view');
125 root 1.23 $w->signal_connect (delete_event => sub { delete $self->{sv}; 0 });
126 elmex 1.15 $w->add ($self->{sv} = GCE::StackView->new);
127 elmex 1.17
128 elmex 1.30 main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250);
129 elmex 1.17
130 elmex 1.15 $w->show_all;
131     }
132    
133 elmex 1.46 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 elmex 1.15 sub show_attr_editor {
168     my ($self) = @_;
169    
170 elmex 1.17 return if $self->{attr_edit};
171    
172 elmex 1.15 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 root 1.23 $w->signal_connect (delete_event => sub { delete $self->{attr_edit}; 0 });
176 elmex 1.17
177 elmex 1.30 main::set_pos_and_size ($w, $main::CFG->{attr_view}, 200, 200);
178 elmex 1.17
179 elmex 1.15 $w->show_all;
180     }
181    
182     sub update_attr_editor {
183 elmex 1.17 my ($self, $arch, $cb) = @_;
184 elmex 1.15
185     return unless $self->{attr_edit};
186    
187 elmex 1.17 $self->{attr_edit}->set_arch ($arch, $cb);
188 elmex 1.15 $self->{attr_edit_win}->set_title ("gce - edit $arch->{_name}");
189     }
190    
191     sub update_stack_view {
192 elmex 1.17 my ($self, $mapedit, $x, $y) = @_;
193 elmex 1.15
194     return unless $self->{sv};
195    
196 elmex 1.17 $self->{sv}->set_stack ($mapedit, $x, $y);
197 elmex 1.15 }
198    
199 elmex 1.17 sub open_pick_window {
200     my ($self, $layout) = @_;
201    
202 root 1.29 # XXX: Yes, also fix this, save _every_ pick window and their positions and their
203 elmex 1.17 # selection
204     my $p = GCE::PickWindow->new ();
205 elmex 1.15
206 elmex 1.17 push @{$self->{open_pick_windows}}, $p;
207    
208     my $idx = (@{$self->{open_pick_windows}}) - 1;
209 elmex 1.15
210 elmex 1.17 $p->signal_connect ('delete-event' => sub {
211     $self->{open_pick_windows}->[$idx] = undef;
212     });
213 elmex 1.10
214 elmex 1.17 if ($layout) {
215 elmex 1.30 main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200);
216 elmex 1.17 }
217 elmex 1.10
218 elmex 1.17 $p->show_all;
219 elmex 1.10
220 elmex 1.17 $p->set_selection ($layout->{selection});
221 elmex 1.10 }
222    
223 elmex 1.13 sub build_menu {
224 elmex 1.1 my ($self) = @_;
225    
226 elmex 1.2 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 elmex 1.17 "_Save Layout" => {
239     callback => sub { $self->save_layout },
240     accelerator => '<ctrl>L'
241     },
242 elmex 1.46 "_Properties" => {
243     callback => sub { $self->show_editor_properties },
244     accelerator => "<ctrl>T"
245     },
246 elmex 1.2 _Quit => {
247     callback => sub { Gtk2->main_quit },
248     accelerator => '<ctrl>Q'
249     }
250     ]
251     },
252 root 1.24 _Dialogs => {
253 elmex 1.2 item_type => '<Branch>',
254     children => [
255 elmex 1.17 "_Attr Editor" => {
256     callback => sub { $self->show_attr_editor },
257     accelerator => "<ctrl>A"
258 elmex 1.2 },
259 elmex 1.17 "_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 elmex 1.2 ]
268 elmex 1.9 }
269 elmex 1.17
270 elmex 1.2 ];
271    
272 elmex 1.13 my $men =
273     Gtk2::SimpleMenu->new (
274     menu_tree => $menu_tree,
275     default_callback => \&default_cb,
276     );
277 elmex 1.10
278 elmex 1.2 $self->add_accel_group ($men->{accel_group});
279    
280 elmex 1.13 return $men->{widget};
281     }
282 elmex 1.10
283 elmex 1.17 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 elmex 1.42 $table->attach_defaults (my $btn = Gtk2::Button->new_with_mnemonic ($lbl), $lx, $lx + 1, $ly, $ly + 1);
298 elmex 1.17 $btn->signal_connect (clicked => $cb);
299    
300     $plcinfo->{next} = [$lx + 1, $ly];
301     }
302    
303     sub build_buttons {
304     my ($self) = @_;
305    
306 elmex 1.38 my $tbl = Gtk2::Table->new (2, 4);
307     my $plcinfo = { width => 2, height => 4, next => [0, 0] };
308 elmex 1.17
309 elmex 1.38 $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 elmex 1.17
317 elmex 1.33 $self->set_edit_tool ('pick');
318 elmex 1.28
319 elmex 1.42 $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 elmex 1.33
327     return $tbl;
328     }
329    
330     sub set_edit_tool {
331     my ($self, $name) = @_;
332    
333     if ($name eq 'pick') {
334 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
335 elmex 1.33 } elsif ($name eq 'place') {
336 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
337 elmex 1.33 } elsif ($name eq 'erase') {
338 elmex 1.28 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
339 elmex 1.34 } elsif ($name eq 'select') {
340     $self->update_edit_tool ($self->{edit_collection}{select}, "Select");;
341 elmex 1.35 } elsif ($name eq 'perl') {
342     $self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");;
343 elmex 1.38 } 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 elmex 1.33 }
348 elmex 1.17 }
349 elmex 1.10
350 elmex 1.28 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 elmex 1.33 $self->{pick_view_btn}->set_label ($arch->{_name});
379 elmex 1.28 }
380    
381 elmex 1.13 sub INIT_INSTANCE {
382     my ($self) = @_;
383 elmex 1.10
384 root 1.24 $::MAINWIN = $self;
385 elmex 1.15
386 elmex 1.13 $self->set_title ("gce - main window");
387 elmex 1.10
388 elmex 1.28 $self->{edit_tool} = Gtk2::Label->new;
389     $self->{edit_tool_cont} = Gtk2::VBox->new;
390    
391 elmex 1.13 $self->add (my $vb = Gtk2::VBox->new);
392     $vb->pack_start ($self->build_menu, 0, 1, 0);
393 elmex 1.28
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 elmex 1.33 $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 elmex 1.36 $self->{pick_view_btn}->signal_connect (clicked => sub {
405     $self->update_attr_editor ($self->{pick_arch});
406     });
407 elmex 1.28 $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 elmex 1.35 $vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0);
417 elmex 1.10
418 elmex 1.13 # XXX:load $ARGV _cleanly_?
419 root 1.27 $self->open_map_editor ($_)
420     for @ARGV;
421 root 1.11
422 elmex 1.13 $self->signal_connect ('delete-event' => sub {
423     Gtk2->main_quit;
424 elmex 1.10 });
425 elmex 1.17
426 elmex 1.30 ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0);
427 elmex 1.2 }
428    
429     sub new_cb {
430     my ($self) = @_;
431 elmex 1.32
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 elmex 1.33 $map->resize ($width->[1], $height->[1]);
442 elmex 1.32 $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 elmex 1.2 }
450    
451 root 1.25 sub new_filechooser {
452 elmex 1.37 my ($self, $title, $save, $filename) = @_;
453 elmex 1.2
454 elmex 1.33 $title ||= 'gce - open map';
455 root 1.25 my $fc = new Gtk2::FileChooserDialog (
456 elmex 1.33 $title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
457 elmex 1.13 );
458    
459 elmex 1.46 $fc->add_shortcut_folder ($::CFG->{MAPDIR}) if -d $::CFG->{MAPDIR};
460 elmex 1.45 $fc->add_shortcut_folder ($_) for grep { $_ && ($_ ne '') } keys %{$self->{fc_last_folders}};
461 elmex 1.46 $fc->set_current_folder ($self->{fc_last_folder} || $::CFG->{MAPDIR});
462 elmex 1.2
463 elmex 1.37 if ($filename) {
464     $fc->set_filename ($filename);
465     }
466    
467 root 1.25 $fc
468     }
469    
470     sub open_cb {
471     my ($self) = @_;
472    
473     my $fc = $self->new_filechooser;
474    
475 elmex 1.2 if ('ok' eq $fc->run) {
476 elmex 1.13
477 elmex 1.10 $self->{fc_last_folder} = $fc->get_current_folder;
478 elmex 1.13 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
479 elmex 1.17
480     $self->open_map_editor ($fc->get_filename);
481 elmex 1.2 }
482    
483     $fc->destroy;
484     }
485    
486 elmex 1.17 sub set_pick {
487     my ($self, $arch) = @_;
488    
489     $self->{pick_arch} = $arch;
490 elmex 1.28 $self->update_pick_view ($arch);
491 elmex 1.17 }
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 elmex 1.1 =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