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