ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MapEditor.pm
Revision: 1.5
Committed: Sun Feb 12 04:50:29 2006 UTC (18 years, 4 months ago) by root
Branch: MAIN
Changes since 1.4: +18 -21 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::VBox,
21 ;
22
23 use strict;
24
25 sub INIT_INSTANCE {
26 my ($self) = @_;
27
28 my $map = new Crossfire::MapWidget;
29
30 # XXX: Make a nicer pick-view (with picture)
31 $self->pack_start (my $lbl = Gtk2::Label->new ("no selection"), 0, 1, 0);
32 $self->{pick_view} = $lbl;
33
34 $self->pack_start ($map, 1, 1, 0);
35 $self->{map} = $map;
36
37 $map->signal_connect (key_press_event => sub {
38 my ($map, $event) = @_;
39
40 $event->keyval == $Gtk2::Gdk::Keysyms{Shift_L}
41 and $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_QUESTION_ARROW'));
42
43 $event->keyval == $Gtk2::Gdk::Keysyms{Control_L}
44 and $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_HAND2'));
45
46 });
47
48 # XXX: What happens if the release got lost?
49 $map->signal_connect (key_release_event => sub {
50 my ($map, $event) = @_;
51
52 $map->{window}->set_cursor (Gtk2::Gdk::Cursor->new ('GDK_LEFT_PTR'));
53
54 });
55 $map->signal_connect (button_press_event => sub {
56 my ($map, $event) = @_;
57
58 my ($x, $y) = $map->coord ($event->x, $event->y);
59 my $as = $self->{map}->get ($x, $y);
60
61 if ($event->button == 1) {
62
63 if ($event->state * "control-mask") {
64 $self->update_pick ($as->[-1]) if @$as;
65 }
66
67 unless ($event->state * "shift-mask") {
68 $map->disable_tooltip unless $self->{draw_mode};
69 $self->{draw_mode} = [1, $x, $y];
70 $self->place_pick ($x, $y);
71 $as = $self->{map}->get ($x, $y);
72 }
73
74 $GCE::MainWindow::MAINWIN->update_attr_editor ($as->[-1]) if @$as;
75 $GCE::MainWindow::MAINWIN->update_stack_view ($self, $as, $x, $y);
76
77 return 1;
78
79 } elsif ($event->button == 3) {
80
81 $map->disable_tooltip unless $self->{draw_mode};
82 $self->{draw_mode} = [2, $x, $y];
83 $self->delete_arch ($x, $y);
84
85 $as = $self->{map}->get ($x, $y);
86 $GCE::MainWindow::MAINWIN->update_attr_editor ($as->[-1]) if @$as;
87 $GCE::MainWindow::MAINWIN->update_stack_view ($self, $as, $x, $y);
88
89 return 1;
90 }
91
92 0
93 });
94
95 $map->signal_connect_after (motion_notify_event => sub {
96 my ($map, $event) = @_;
97
98 $self->{draw_mode}
99 or return;
100
101 my ($X, $Y) = @{$self->{draw_mode}}[1,2];
102 my ($x, $y) = $map->coord ($map->get_pointer);
103
104 while ($x != $X || $y != $Y) {
105
106 $X++ if $X < $x;
107 $X-- if $X > $x;
108 $Y++ if $Y < $y;
109 $Y-- if $Y > $y;
110
111 if ($self->{draw_mode}[0] == 1) {
112
113 $self->place_pick ($X, $Y);
114
115 } elsif ($self->{draw_mode}[0] == 2) {
116
117 $self->delete_arch ($X, $Y);
118 }
119 }
120
121 @{$self->{draw_mode}}[1,2] = ($X, $Y);
122
123 1
124 });
125
126 $map->signal_connect (button_release_event => sub {
127 my ($map, $event) = @_;
128
129 $map->enable_tooltip if delete $self->{draw_mode};
130
131 0
132 });
133 }
134
135 sub set_place_arch {
136 my ($self, $arch) = @_;
137
138 $self->{pick_arch} = $arch;
139 }
140
141 sub delete_arch {
142 my ($self, $x, $y) = @_;
143
144 defined $self->{map}
145 or return 0;
146
147 my $as = $self->{map}->get ($x, $y);
148 pop @$as;
149 $self->{map}->set ($x, $y, $as);
150 }
151
152 sub place_pick {
153 my ($self, $x, $y) = @_;
154
155 defined $self->{map}
156 and defined $self->{pick_arch}
157 or return 0;
158
159 my $as = $self->{map}->get ($x, $y);
160
161 my $arch = {
162 _name => $self->{pick_arch}->{_name},
163 };
164
165 push @$as, $arch
166 unless @$as && $as->[-1]->{_name} eq $arch->{_name};
167
168 $self->{map}->set ($x, $y, $as);
169 }
170
171 sub open_map {
172 my ($self, $path) = @_;
173
174 $self->{map}->set_map (new_from_file Crossfire::Map $path);
175 }
176
177 sub update_pick {
178 my ($self, $arch) = @_;
179
180 $self->{pick_arch} = $arch;
181 $self->{pick_view}->set_text ($arch->{_name});
182 }
183
184 =head1 AUTHOR
185
186 Marc Lehmann <schmorp@schmorp.de>
187 http://home.schmorp.de/
188
189 Robin Redeker <elmex@ta-sa.org>
190 http://www.ta-sa.org/
191
192 =cut
193 1;
194