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