ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.60
Committed: Tue Apr 4 13:10:25 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.59: +1 -1 lines
Log Message:
fixed default sizes of attribut editor and inventory

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