ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MainWindow.pm
Revision: 1.36
Committed: Thu Mar 16 01:13:57 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.35: +3 -5 lines
Log Message:
removed face checking and implemented pick editing

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->{open_pickers} = [];
45
46 for (@{$self->{open_pick_windows}}) {
47
48 next unless defined $_;
49
50 push @{$main::CFG->{open_pickers}}, {
51 p_and_s => main::get_pos_and_size ($_),
52 selection => $_->{last_selection}
53 };
54 }
55
56 main::write_cfg ("$Crossfire::VARDIR/gceconfig");
57 }
58
59 sub load_layout {
60 my ($self) = @_;
61
62 $main::CFG->{attr_edit_on}
63 and $self->show_attr_editor;
64
65 $main::CFG->{stack_view_on}
66 and $self->show_stack_view;
67
68 for (@{$main::CFG->{open_pickers}}) {
69 $self->open_pick_window ($_);
70 }
71 }
72
73 sub open_map_editor {
74 my ($self, $mapfile) = @_;
75
76 # XXX: last_map_window is a dirty trick to get the position and size
77 # for save layout
78
79 my $w = $self->{last_map_window} = GCE::MapEditor->new;
80
81 $w->open_map ($mapfile);
82
83 ::set_pos_and_size ($w, $main::CFG->{map_window}, 500, 500, 200, 0);
84
85 $w->show_all;
86 }
87
88 sub show_stack_view {
89 my ($self) = @_;
90
91 return if defined $self->{sv};
92
93 my $w = $self->{sv_win} = Gtk2::Window->new ('toplevel');
94 $w->set_title ('gce - stack view');
95 $w->signal_connect (delete_event => sub { delete $self->{sv}; 0 });
96 $w->add ($self->{sv} = GCE::StackView->new);
97
98 main::set_pos_and_size ($w, $main::CFG->{stack_view}, 150, 250);
99
100 $w->show_all;
101 }
102
103 sub show_attr_editor {
104 my ($self) = @_;
105
106 return if $self->{attr_edit};
107
108 my $w = $self->{attr_edit_win} = Gtk2::Window->new;
109 $w->set_title ("gce - edit attrs");
110 $w->add ($self->{attr_edit} = GCE::AttrEdit->new);
111 $w->signal_connect (delete_event => sub { delete $self->{attr_edit}; 0 });
112
113 main::set_pos_and_size ($w, $main::CFG->{attr_view}, 200, 200);
114
115 $w->show_all;
116 }
117
118 sub update_attr_editor {
119 my ($self, $arch, $cb) = @_;
120
121 return unless $self->{attr_edit};
122
123 $self->{attr_edit}->set_arch ($arch, $cb);
124 $self->{attr_edit_win}->set_title ("gce - edit $arch->{_name}");
125 }
126
127 sub update_stack_view {
128 my ($self, $mapedit, $x, $y) = @_;
129
130 return unless $self->{sv};
131
132 $self->{sv}->set_stack ($mapedit, $x, $y);
133 }
134
135 sub open_pick_window {
136 my ($self, $layout) = @_;
137
138 # XXX: Yes, also fix this, save _every_ pick window and their positions and their
139 # selection
140 my $p = GCE::PickWindow->new ();
141
142 push @{$self->{open_pick_windows}}, $p;
143
144 my $idx = (@{$self->{open_pick_windows}}) - 1;
145
146 $p->signal_connect ('delete-event' => sub {
147 $self->{open_pick_windows}->[$idx] = undef;
148 });
149
150 if ($layout) {
151 main::set_pos_and_size ($p, $layout->{p_and_s}, 200, 200);
152 }
153
154 $p->show_all;
155
156 $p->set_selection ($layout->{selection});
157 }
158
159 sub build_menu {
160 my ($self) = @_;
161
162 my $menu_tree = [
163 _File => {
164 item_type => '<Branch>',
165 children => [
166 _New => {
167 callback => sub { $self->new_cb },
168 accelerator => '<ctrl>N'
169 },
170 _Open => {
171 callback => sub { $self->open_cb },
172 accelerator => '<ctrl>O'
173 },
174 "_Save Layout" => {
175 callback => sub { $self->save_layout },
176 accelerator => '<ctrl>L'
177 },
178 _Quit => {
179 callback => sub { Gtk2->main_quit },
180 accelerator => '<ctrl>Q'
181 }
182 ]
183 },
184 _Dialogs => {
185 item_type => '<Branch>',
186 children => [
187 "_Attr Editor" => {
188 callback => sub { $self->show_attr_editor },
189 accelerator => "<ctrl>A"
190 },
191 "_Picker" => {
192 callback => sub { $self->open_pick_window },
193 accelerator => "<ctrl>P"
194 },
195 "_Stack View" => {
196 callback => sub { $self->show_stack_view },
197 accelerator => "<ctrl>V"
198 },
199 ]
200 }
201
202 ];
203
204 my $men =
205 Gtk2::SimpleMenu->new (
206 menu_tree => $menu_tree,
207 default_callback => \&default_cb,
208 );
209
210 $self->add_accel_group ($men->{accel_group});
211
212 return $men->{widget};
213 }
214
215 sub add_button {
216 my ($self, $table, $plcinfo, $lbl, $cb) = @_;
217
218 my ($lx, $ly) = @{$plcinfo->{next}};
219
220 unless ($lx < $plcinfo->{width}) {
221
222 $ly++;
223 $lx = 0;
224 }
225
226 $ly < $plcinfo->{height}
227 or die "too many buttons, make table bigger!";
228
229 $table->attach_defaults (my $btn = Gtk2::Button->new_with_label ($lbl), $lx, $lx + 1, $ly, $ly + 1);
230 $btn->signal_connect (clicked => $cb);
231
232 $plcinfo->{next} = [$lx + 1, $ly];
233 }
234
235 sub build_buttons {
236 my ($self) = @_;
237
238 my $tbl = Gtk2::Table->new (2, 5);
239 my $plcinfo = { width => 1, height => 5, next => [0, 0] };
240
241 $self->{edit_collection}{pick} = GCE::EditAction::Pick->new;
242 $self->{edit_collection}{place} = GCE::EditAction::Place->new;
243 $self->{edit_collection}{erase} = GCE::EditAction::Erase->new;
244 $self->{edit_collection}{select}= GCE::EditAction::Select->new;
245 $self->{edit_collection}{perl} = GCE::EditAction::Perl->new;
246
247 $self->set_edit_tool ('pick');
248
249 $self->add_button ($tbl, $plcinfo, "Pick", sub { $self->set_edit_tool ('pick') });
250 $self->add_button ($tbl, $plcinfo, "Place", sub { $self->set_edit_tool ('place') });
251 $self->add_button ($tbl, $plcinfo, "Erase", sub { $self->set_edit_tool ('erase') });
252 $self->add_button ($tbl, $plcinfo, "Select", sub { $self->set_edit_tool ('select') });
253 $self->add_button ($tbl, $plcinfo, "Eval", sub { $self->set_edit_tool ('perl') });
254
255 return $tbl;
256 }
257
258 sub set_edit_tool {
259 my ($self, $name) = @_;
260
261 if ($name eq 'pick') {
262 $self->update_edit_tool ($self->{edit_collection}{pick}, "Pick");;
263 } elsif ($name eq 'place') {
264 $self->update_edit_tool ($self->{edit_collection}{place}, "Place");;
265 } elsif ($name eq 'erase') {
266 $self->update_edit_tool ($self->{edit_collection}{erase}, "Erase");;
267 } elsif ($name eq 'select') {
268 $self->update_edit_tool ($self->{edit_collection}{select}, "Select");;
269 } elsif ($name eq 'perl') {
270 $self->update_edit_tool ($self->{edit_collection}{perl}, "Eval");;
271 }
272 }
273
274 sub update_edit_tool {
275 my ($self, $tool, $name) = @_;
276
277 $self->{edit_tool}->set_text ($name);
278 $self->{sel_editaction} = $tool;
279
280 my $widget = $tool->tool_widget;
281
282 for ($self->{edit_tool_cont}->get_children) {
283 $_->hide;
284 $self->{edit_tool_cont}->remove ($_);
285 }
286
287 defined $widget or return;
288
289 $self->{edit_tool_cont}->add ($widget);
290 $widget->show_all;
291 }
292
293 sub update_pick_view {
294 my ($self, $arch) = @_;
295
296 defined $arch->{_face}
297 or $arch = $Crossfire::ARCH{$arch->{_name}};
298
299 fill_pb_from_arch ($self->{pick_view_pb}, $arch);
300 $self->{pick_view_img}->set_from_pixbuf ($self->{pick_view_pb});
301
302 $self->{pick_view_btn}->set_label ($arch->{_name});
303 }
304
305 sub INIT_INSTANCE {
306 my ($self) = @_;
307
308 $::MAINWIN = $self;
309
310 $self->set_title ("gce - main window");
311
312 $self->{edit_tool} = Gtk2::Label->new;
313 $self->{edit_tool_cont} = Gtk2::VBox->new;
314
315 $self->add (my $vb = Gtk2::VBox->new);
316 $vb->pack_start ($self->build_menu, 0, 1, 0);
317
318 $vb->pack_start (my $hb = $self->{pick_view_hb} = Gtk2::HBox->new, 0, 1, 0);
319 $hb->pack_start ($self->{pick_view_img} = Gtk2::Image->new, 0, 1, 0);
320 $hb->pack_start ($self->{pick_view_btn} = Gtk2::Button->new, 0, 1, 0);
321 $self->{pick_view_btn}->drag_source_set (['button1_mask'], ['move'],
322 { target => 'STRING', flags => [], info => 'TARGET_STRING' }
323 );
324 $self->{pick_view_btn}->signal_connect (drag_data_get => sub {
325 my ($widget, $context, $data, $info, $time) = @_;
326 $data->set ($data->target, 8, "pick");
327 });
328 $self->{pick_view_btn}->signal_connect (clicked => sub {
329 $self->update_attr_editor ($self->{pick_arch});
330 });
331 $self->{pick_view_pb} = new_arch_pb ();
332
333 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
334 $vb->pack_start (my $tbl = $self->build_buttons, 0, 1, 0);
335
336 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
337 $vb->pack_start ($self->{edit_tool}, 0, 1, 0);
338
339 $vb->pack_start (Gtk2::HSeparator->new, 0, 1, 0);
340 $vb->pack_start ($self->{edit_tool_cont}, 1, 1, 0);
341
342 # XXX:load $ARGV _cleanly_?
343 $self->open_map_editor ($_)
344 for @ARGV;
345
346 $self->signal_connect ('delete-event' => sub {
347 Gtk2->main_quit;
348 });
349
350 ::set_pos_and_size ($self, $main::CFG->{main_window}, 150, 200, 0, 0);
351 }
352
353 sub new_cb {
354 my ($self) = @_;
355
356 my $w = Gtk2::Window->new ('toplevel');
357 my $width = [width => 20];
358 my $height = [height => 20];
359 $w->add (my $tbl = Gtk2::Table->new (2, 3));
360 add_table_widget ($tbl, 0, $width, 'string');
361 add_table_widget ($tbl, 1, $height, 'string');
362 add_table_widget ($tbl, 2, 'new', 'button', sub {
363 if ($width->[1] > 0 and $height->[1] > 0) {
364 my $map = Crossfire::Map->new ($width->[1], $height->[1]);
365 $map->resize ($width->[1], $height->[1]);
366 $self->open_map_editor ($map);
367 }
368 $w->destroy;
369 1;
370 });
371 add_table_widget ($tbl, 3, 'close', 'button', sub { $w->destroy });
372 $w->show_all;
373 }
374
375 sub new_filechooser {
376 my ($self, $title, $save) = @_;
377
378 $title ||= 'gce - open map';
379 my $fc = new Gtk2::FileChooserDialog (
380 $title, undef, $save ? 'save' : 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok'
381 );
382
383 $fc->add_shortcut_folder ("$Crossfire::LIB/maps") if -d "$Crossfire::LIB/maps";
384 $fc->add_shortcut_folder ($_) for keys %{$self->{fc_last_folders}};
385 $fc->set_current_folder ($self->{fc_last_folder} || "$Crossfire::LIB/maps");
386
387 $fc
388 }
389
390 sub open_cb {
391 my ($self) = @_;
392
393 my $fc = $self->new_filechooser;
394
395 if ('ok' eq $fc->run) {
396
397 $self->{fc_last_folder} = $fc->get_current_folder;
398 $self->{fc_last_folders}->{$self->{fc_last_folder}}++;
399
400 $self->open_map_editor ($fc->get_filename);
401 }
402
403 $fc->destroy;
404 }
405
406 sub set_pick {
407 my ($self, $arch) = @_;
408
409 $self->{pick_arch} = $arch;
410 $self->update_pick_view ($arch);
411 }
412
413 sub get_pick {
414 my ($self) = @_;
415
416 # XXX: This is just to make sure that this function always returns something
417 return $self->{pick_arch} || { _name => 'platinacoin' };
418 }
419
420 =head1 AUTHOR
421
422 Marc Lehmann <schmorp@schmorp.de>
423 http://home.schmorp.de/
424
425 Robin Redeker <elmex@ta-sa.org>
426 http://www.ta-sa.org/
427
428 =cut
429 1;
430