ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/gde/GCE/MapEditor.pm
Revision: 1.12
Committed: Tue Feb 21 18:07:39 2006 UTC (20 years, 8 months ago) by root
Branch: MAIN
Changes since 1.11: +0 -2 lines
Log Message:
*** empty log message ***

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     ]
39 root 1.9 },
40     _Edit => {
41     item_type => '<Branch>',
42     children => [
43     "_Undo" => {
44     callback => sub { $self->undo },
45     accelerator => "<ctrl>Z"
46     },
47     "_Redo" => {
48     callback => sub { $self->redo },
49     accelerator => "<ctrl>Y"
50     },
51    
52     ]
53     },
54    
55     ];
56    
57     my $men =
58     Gtk2::SimpleMenu->new (
59     menu_tree => $menu_tree,
60     default_callback => \&default_cb,
61     );
62    
63     $self->add_accel_group ($men->{accel_group});
64    
65     return $men->{widget};
66     }
67    
68 elmex 1.1 sub INIT_INSTANCE {
69     my ($self) = @_;
70    
71 elmex 1.6 $self->set_title ('gce - map editor');
72     $self->add (my $vb = Gtk2::VBox->new);
73 elmex 1.1
74 root 1.9 $vb->pack_start (my $menu = $self->build_menu, 0, 1, 0);
75    
76     $vb->pack_start (my $map = $self->{map} = Crossfire::MapWidget->new, 1, 1, 0);
77    
78     $map->signal_connect (button_press_event => sub {
79     my ($map, $event) = @_;
80    
81     my ($x, $y) = $map->coord ($event->x, $event->y);
82     my $as = $map->get ($x, $y);
83    
84     my $btn = $event->button;
85    
86     if ((not $self->{draw_mode})
87     and $btn != 2
88     and my $ea = $::MAINWIN->{sel_editaction}) {
89    
90     $ea->begin ($map, $x, $y, $btn);
91 elmex 1.1
92 root 1.9 $self->{draw_mode} = [$ea, $x, $y, $btn];
93 elmex 1.1
94 root 1.9 $ea->want_cursor
95     or $map->disable_tooltip;
96 elmex 1.4
97 root 1.9 if ($ea->special_arrow) {
98 elmex 1.4
99 root 1.9 $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ($ea->special_arrow));
100     #'GDK_QUESTION_ARROW'));
101     }
102    
103     return 1;
104     }
105    
106     0
107     });
108    
109     $map->signal_connect_after (motion_notify_event => sub {
110     my ($map, $event) = @_;
111    
112     $self->{draw_mode}
113     or return;
114    
115     my ($X, $Y) = @{$self->{draw_mode}}[1,2];
116     my ($x, $y) = $map->coord ($map->get_pointer);
117 elmex 1.4
118 root 1.9 while ($x != $X || $y != $Y) {
119 elmex 1.4
120 root 1.9 $X++ if $X < $x;
121     $X-- if $X > $x;
122     $Y++ if $Y < $y;
123     $Y-- if $Y > $y;
124 elmex 1.4
125 root 1.9 $self->{draw_mode}[0]->edit ($map, $X, $Y, $self->{draw_mode}[3]);
126     }
127 elmex 1.4
128 root 1.9 @{$self->{draw_mode}}[1,2] = ($X, $Y);
129 elmex 1.1
130 root 1.9 1
131     });
132 elmex 1.1
133 root 1.9 $map->signal_connect (button_release_event => sub {
134     my ($map, $event) = @_;
135 elmex 1.1
136 root 1.9 if ($self->{draw_mode}
137     and my $ea = $self->{draw_mode}[0]
138     and $self->{draw_mode}[3] == $event->button) {
139 elmex 1.1
140 root 1.9 my ($x, $y) = $map->coord ($map->get_pointer);
141 elmex 1.4
142 root 1.9 $ea->end ($map, $x, $y, $event->button);
143 elmex 1.1
144 root 1.9 if ($ea->special_arrow) {
145 elmex 1.1
146 root 1.9 # XXX: Get the original cursor and insert it here
147     $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
148     }
149 elmex 1.1
150 root 1.9 delete $self->{draw_mode};
151 elmex 1.4
152 root 1.9 $ea->want_cursor
153     or $map->enable_tooltip;
154 elmex 1.1
155 root 1.9 return 1;
156     }
157 elmex 1.1
158 root 1.9 0
159     });
160     }
161 elmex 1.8
162 root 1.9 sub undo {
163     my ($self) = @_;
164 elmex 1.8
165 root 1.9 my $map = $self->{map}; # the Crossfire::MapWidget
166 elmex 1.1
167 root 1.9 $map->{undo_stack_pos}
168     or return;
169 elmex 1.1
170 root 1.9 $map->change_swap ($map->{undo_stack}[--$map->{undo_stack_pos}]);
171     }
172 elmex 1.1
173 root 1.9 sub redo {
174     my ($self) = @_;
175 elmex 1.7
176 root 1.9 my $map = $self->{map}; # the Crossfire::MapWidget
177 elmex 1.7
178 root 1.9 $map->{undo_stack_pos} < @{$map->{undo_stack}}
179     or return;
180 elmex 1.1
181 root 1.9 $map->change_swap ($map->{undo_stack}[$map->{undo_stack_pos}++]);
182 root 1.3 }
183    
184     sub delete_arch {
185     my ($self, $x, $y) = @_;
186    
187     defined $self->{map}
188     or return 0;
189    
190 root 1.5 my $as = $self->{map}->get ($x, $y);
191     pop @$as;
192     $self->{map}->set ($x, $y, $as);
193 root 1.3 }
194    
195     sub place_pick {
196     my ($self, $x, $y) = @_;
197    
198 root 1.9 my $pick = $::MAINWIN->get_pick;
199 root 1.5 my $as = $self->{map}->get ($x, $y);
200 root 1.3
201 elmex 1.6 my $arch = { _name => $pick->{_name} };
202 root 1.3
203 root 1.5 push @$as, $arch
204     unless @$as && $as->[-1]->{_name} eq $arch->{_name};
205 root 1.3
206 root 1.5 $self->{map}->set ($x, $y, $as);
207 root 1.3 }
208    
209     sub open_map {
210     my ($self, $path) = @_;
211    
212     $self->{map}->set_map (new_from_file Crossfire::Map $path);
213     }
214    
215 elmex 1.1 =head1 AUTHOR
216    
217     Marc Lehmann <schmorp@schmorp.de>
218     http://home.schmorp.de/
219    
220     Robin Redeker <elmex@ta-sa.org>
221     http://www.ta-sa.org/
222    
223     =cut
224     1;
225