ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MapEditor.pm
Revision: 1.13
Committed: Tue Feb 21 18:18:35 2006 UTC (18 years, 4 months ago) by root
Branch: MAIN
Changes since 1.12: +25 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 package GCE::MapEditor;
2
3 =head1 NAME
4
5 GCE::MapEditor - the map editing widget
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
19 use Glib::Object::Subclass
20 Gtk2::Window;
21
22 use strict;
23
24 sub build_menu {
25 my ($self) = @_;
26
27 my $menu_tree = [
28 _File => {
29 item_type => '<Branch>',
30 children => [
31 "_Save" => {
32 callback => sub { $self->save_map },
33 accelerator => '<ctrl>S'
34 },
35 "Save As" => {
36 callback => sub { $self->save_map_as },
37 },
38 "Close" => {
39 callback => sub { $self->delete; 1 },
40 },
41 ]
42 },
43 _Edit => {
44 item_type => '<Branch>',
45 children => [
46 "_Undo" => {
47 callback => sub { $self->undo },
48 accelerator => "<ctrl>Z"
49 },
50 "_Redo" => {
51 callback => sub { $self->redo },
52 accelerator => "<ctrl>Y"
53 },
54
55 ]
56 },
57
58 ];
59
60 my $men =
61 Gtk2::SimpleMenu->new (
62 menu_tree => $menu_tree,
63 default_callback => \&default_cb,
64 );
65
66 $self->add_accel_group ($men->{accel_group});
67
68 return $men->{widget};
69 }
70
71 sub INIT_INSTANCE {
72 my ($self) = @_;
73
74 $self->set_title ('gce - map editor');
75 $self->add (my $vb = Gtk2::VBox->new);
76
77 $self->signal_connect (delete_event => sub { $self->delete });
78
79 $vb->pack_start (my $menu = $self->build_menu, 0, 1, 0);
80
81 $vb->pack_start (my $map = $self->{map} = Crossfire::MapWidget->new, 1, 1, 0);
82
83 $map->signal_connect (button_press_event => sub {
84 my ($map, $event) = @_;
85
86 my ($x, $y) = $map->coord ($event->x, $event->y);
87 my $as = $map->get ($x, $y);
88
89 my $btn = $event->button;
90
91 if ((not $self->{draw_mode})
92 and $btn != 2
93 and my $ea = $::MAINWIN->{sel_editaction}) {
94
95 $ea->begin ($map, $x, $y, $btn);
96
97 $self->{draw_mode} = [$ea, $x, $y, $btn];
98
99 $ea->want_cursor
100 or $map->disable_tooltip;
101
102 if ($ea->special_arrow) {
103
104 $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($ea->special_arrow));
105 #'GDK_QUESTION_ARROW'));
106 }
107
108 return 1;
109 }
110
111 0
112 });
113
114 $map->signal_connect_after (motion_notify_event => sub {
115 my ($map, $event) = @_;
116
117 $self->{draw_mode}
118 or return;
119
120 my ($X, $Y) = @{$self->{draw_mode}}[1,2];
121 my ($x, $y) = $map->coord ($map->get_pointer);
122
123 while ($x != $X || $y != $Y) {
124
125 $X++ if $X < $x;
126 $X-- if $X > $x;
127 $Y++ if $Y < $y;
128 $Y-- if $Y > $y;
129
130 $self->{draw_mode}[0]->edit ($map, $X, $Y, $self->{draw_mode}[3]);
131 }
132
133 @{$self->{draw_mode}}[1,2] = ($X, $Y);
134
135 1
136 });
137
138 $map->signal_connect (button_release_event => sub {
139 my ($map, $event) = @_;
140
141 if ($self->{draw_mode}
142 and my $ea = $self->{draw_mode}[0]
143 and $self->{draw_mode}[3] == $event->button) {
144
145 my ($x, $y) = $map->coord ($map->get_pointer);
146
147 $ea->end ($map, $x, $y, $event->button);
148
149 if ($ea->special_arrow) {
150
151 # XXX: Get the original cursor and insert it here
152 $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
153 }
154
155 delete $self->{draw_mode};
156
157 $ea->want_cursor
158 or $map->enable_tooltip;
159
160 return 1;
161 }
162
163 0
164 });
165 }
166
167 sub undo {
168 my ($self) = @_;
169
170 my $map = $self->{map}; # the Crossfire::MapWidget
171
172 $map->{undo_stack_pos}
173 or return;
174
175 $map->change_swap ($map->{undo_stack}[--$map->{undo_stack_pos}]);
176 }
177
178 sub redo {
179 my ($self) = @_;
180
181 my $map = $self->{map}; # the Crossfire::MapWidget
182
183 $map->{undo_stack_pos} < @{$map->{undo_stack}}
184 or return;
185
186 $map->change_swap ($map->{undo_stack}[$map->{undo_stack_pos}++]);
187 }
188
189 sub delete_arch {
190 my ($self, $x, $y) = @_;
191
192 defined $self->{map}
193 or return 0;
194
195 my $as = $self->{map}->get ($x, $y);
196 pop @$as;
197 $self->{map}->set ($x, $y, $as);
198 }
199
200 sub place_pick {
201 my ($self, $x, $y) = @_;
202
203 my $pick = $::MAINWIN->get_pick;
204 my $as = $self->{map}->get ($x, $y);
205
206 my $arch = { _name => $pick->{_name} };
207
208 push @$as, $arch
209 unless @$as && $as->[-1]->{_name} eq $arch->{_name};
210
211 $self->{map}->set ($x, $y, $as);
212 }
213
214 sub delete {
215 my ($self) = @_;
216
217 # check and modla dialog if "dirty"
218
219 $self->destroy;
220 }
221
222 sub open_map {
223 my ($self, $path) = @_;
224
225 $self->{path} = $path;
226 $self->{map}->set_map (new_from_file Crossfire::Map $path);
227 }
228
229 sub save_map {
230 my ($self) = @_;
231
232 $self->{map}{map}->write_file ($self->{path});
233 }
234
235 sub save_map_as {
236 my ($self) = @_;
237 warn "save_map_as nyi\n";
238 }
239
240 =head1 AUTHOR
241
242 Marc Lehmann <schmorp@schmorp.de>
243 http://home.schmorp.de/
244
245 Robin Redeker <elmex@ta-sa.org>
246 http://www.ta-sa.org/
247
248 =cut
249 1;
250