ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MapEditor.pm
Revision: 1.14
Committed: Fri Mar 10 20:32:47 2006 UTC (18 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.13: +3 -2 lines
Log Message:
implemented edit tools widgets and improved the placement tool

File Contents

# User Rev Content
1 elmex 1.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 root 1.3 use Crossfire::Map;
15 elmex 1.1 use Crossfire::MapWidget;
16    
17     use GCE::AttrEdit;
18    
19 elmex 1.4 use Glib::Object::Subclass
20 elmex 1.6 Gtk2::Window;
21 elmex 1.1
22     use strict;
23    
24 root 1.9 sub build_menu {
25     my ($self) = @_;
26    
27     my $menu_tree = [
28     _File => {
29     item_type => '<Branch>',
30     children => [
31     "_Save" => {
32 root 1.10 callback => sub { $self->save_map },
33 root 1.9 accelerator => '<ctrl>S'
34     },
35 root 1.10 "Save As" => {
36     callback => sub { $self->save_map_as },
37     },
38 root 1.13 "Close" => {
39     callback => sub { $self->delete; 1 },
40     },
41 root 1.10 ]
42 root 1.9 },
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 elmex 1.1 sub INIT_INSTANCE {
72     my ($self) = @_;
73    
74 elmex 1.6 $self->set_title ('gce - map editor');
75     $self->add (my $vb = Gtk2::VBox->new);
76 elmex 1.1
77 root 1.13 $self->signal_connect (delete_event => sub { $self->delete });
78    
79 root 1.9 $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 elmex 1.1
97 root 1.9 $self->{draw_mode} = [$ea, $x, $y, $btn];
98 elmex 1.1
99 root 1.9 $ea->want_cursor
100     or $map->disable_tooltip;
101 elmex 1.4
102 root 1.9 if ($ea->special_arrow) {
103 elmex 1.4
104 root 1.9 $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 elmex 1.4
123 root 1.9 while ($x != $X || $y != $Y) {
124 elmex 1.4
125 root 1.9 $X++ if $X < $x;
126     $X-- if $X > $x;
127     $Y++ if $Y < $y;
128     $Y-- if $Y > $y;
129 elmex 1.4
130 root 1.9 $self->{draw_mode}[0]->edit ($map, $X, $Y, $self->{draw_mode}[3]);
131     }
132 elmex 1.4
133 root 1.9 @{$self->{draw_mode}}[1,2] = ($X, $Y);
134 elmex 1.1
135 root 1.9 1
136     });
137 elmex 1.1
138 root 1.9 $map->signal_connect (button_release_event => sub {
139     my ($map, $event) = @_;
140 elmex 1.1
141 root 1.9 if ($self->{draw_mode}
142     and my $ea = $self->{draw_mode}[0]
143     and $self->{draw_mode}[3] == $event->button) {
144 elmex 1.1
145 root 1.9 my ($x, $y) = $map->coord ($map->get_pointer);
146 elmex 1.4
147 root 1.9 $ea->end ($map, $x, $y, $event->button);
148 elmex 1.1
149 root 1.9 if ($ea->special_arrow) {
150 elmex 1.1
151 root 1.9 # XXX: Get the original cursor and insert it here
152     $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
153     }
154 elmex 1.1
155 root 1.9 delete $self->{draw_mode};
156 elmex 1.4
157 root 1.9 $ea->want_cursor
158     or $map->enable_tooltip;
159 elmex 1.1
160 root 1.9 return 1;
161     }
162 elmex 1.1
163 root 1.9 0
164     });
165     }
166 elmex 1.8
167 root 1.9 sub undo {
168     my ($self) = @_;
169 elmex 1.8
170 root 1.9 my $map = $self->{map}; # the Crossfire::MapWidget
171 elmex 1.1
172 root 1.9 $map->{undo_stack_pos}
173     or return;
174 elmex 1.1
175 root 1.9 $map->change_swap ($map->{undo_stack}[--$map->{undo_stack_pos}]);
176     }
177 elmex 1.1
178 root 1.9 sub redo {
179     my ($self) = @_;
180 elmex 1.7
181 root 1.9 my $map = $self->{map}; # the Crossfire::MapWidget
182 elmex 1.7
183 elmex 1.14 $map->{undo_stack}
184     and $map->{undo_stack_pos} < @{$map->{undo_stack}}
185     or return;
186 elmex 1.1
187 root 1.9 $map->change_swap ($map->{undo_stack}[$map->{undo_stack_pos}++]);
188 root 1.3 }
189    
190     sub delete_arch {
191     my ($self, $x, $y) = @_;
192    
193     defined $self->{map}
194     or return 0;
195    
196 root 1.5 my $as = $self->{map}->get ($x, $y);
197     pop @$as;
198     $self->{map}->set ($x, $y, $as);
199 root 1.3 }
200    
201     sub place_pick {
202     my ($self, $x, $y) = @_;
203    
204 root 1.9 my $pick = $::MAINWIN->get_pick;
205 root 1.5 my $as = $self->{map}->get ($x, $y);
206 root 1.3
207 elmex 1.6 my $arch = { _name => $pick->{_name} };
208 root 1.3
209 root 1.5 push @$as, $arch
210     unless @$as && $as->[-1]->{_name} eq $arch->{_name};
211 root 1.3
212 root 1.5 $self->{map}->set ($x, $y, $as);
213 root 1.3 }
214    
215 root 1.13 sub delete {
216     my ($self) = @_;
217    
218     # check and modla dialog if "dirty"
219    
220     $self->destroy;
221     }
222    
223 root 1.3 sub open_map {
224     my ($self, $path) = @_;
225    
226 root 1.13 $self->{path} = $path;
227 root 1.3 $self->{map}->set_map (new_from_file Crossfire::Map $path);
228     }
229    
230 root 1.13 sub save_map {
231     my ($self) = @_;
232    
233     $self->{map}{map}->write_file ($self->{path});
234     }
235    
236     sub save_map_as {
237     my ($self) = @_;
238     warn "save_map_as nyi\n";
239     }
240    
241 elmex 1.1 =head1 AUTHOR
242    
243     Marc Lehmann <schmorp@schmorp.de>
244     http://home.schmorp.de/
245    
246     Robin Redeker <elmex@ta-sa.org>
247     http://www.ta-sa.org/
248    
249     =cut
250     1;
251