1 | package CFPlus::MapWidget; |
1 | package DC::MapWidget; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | use utf8; |
4 | use utf8; |
5 | |
5 | |
6 | use List::Util qw(min max); |
6 | use List::Util qw(min max); |
7 | |
7 | |
8 | use CFPlus; |
8 | use DC; |
9 | use CFPlus::OpenGL; |
9 | use DC::OpenGL; |
10 | use CFPlus::UI; |
10 | use DC::UI; |
11 | use CFPlus::Macro; |
11 | use DC::Macro; |
12 | |
12 | |
13 | our @ISA = CFPlus::UI::Base::; |
13 | our @ISA = DC::UI::Base::; |
14 | |
14 | |
15 | my $magicmap_tex = |
15 | my $magicmap_tex = |
16 | new_from_file CFPlus::Texture CFPlus::find_rcfile "magicmap.png", |
16 | new_from_resource DC::Texture "magicmap.png", |
17 | mipmap => 1, wrap => 0, internalformat => GL_ALPHA; |
17 | mipmap => 1, wrap => 0, internalformat => GL_ALPHA; |
18 | |
18 | |
19 | sub new { |
19 | sub new { |
20 | my $class = shift; |
20 | my $class = shift; |
21 | |
21 | |
22 | my $self = $class->SUPER::new ( |
22 | my $self = $class->SUPER::new ( |
23 | z => -1, |
23 | z => -1, |
24 | can_focus => 1, |
24 | can_focus => 1, |
25 | list => glGenList, |
|
|
26 | tilesize => 32, |
25 | tilesize => 32, |
27 | @_ |
26 | @_ |
28 | ); |
27 | ); |
29 | |
28 | |
30 | $self->{completer} = new CFPlus::MapWidget::Command:: |
|
|
31 | command => $self->{command}, |
|
|
32 | tooltip => "#completer_help", |
|
|
33 | ; |
|
|
34 | |
|
|
35 | $self |
29 | $self |
36 | } |
|
|
37 | |
|
|
38 | sub set_tilesize { |
|
|
39 | my ($self, $tilesize) = @_; |
|
|
40 | |
|
|
41 | $self->{tilesize} = $tilesize; |
|
|
42 | } |
30 | } |
43 | |
31 | |
44 | sub add_command { |
32 | sub add_command { |
45 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
33 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
46 | |
34 | |
… | |
… | |
48 | |
36 | |
49 | $tooltip =~ s/^\s+//; |
37 | $tooltip =~ s/^\s+//; |
50 | $tooltip = "<big>$data</big>\n\n$tooltip"; |
38 | $tooltip = "<big>$data</big>\n\n$tooltip"; |
51 | $tooltip =~ s/\s+$//; |
39 | $tooltip =~ s/\s+$//; |
52 | |
40 | |
53 | $self->{completer}{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; |
41 | $::COMPLETER->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; |
54 | } |
42 | } |
55 | |
43 | |
56 | sub clr_commands { |
44 | sub clr_commands { |
57 | my ($self) = @_; |
45 | my ($self) = @_; |
58 | |
46 | |
59 | %{$self->{completer}{command}} = (); |
47 | %{$::COMPLETER->{command}} = (); |
60 | |
48 | |
61 | $self->{completer}->hide |
49 | $::COMPLETER->hide |
62 | if $self->{completer}; |
50 | if $::COMPLETER; |
63 | } |
51 | } |
64 | |
52 | |
65 | sub server_login { |
53 | sub server_login { |
66 | my ($server) = @_; |
54 | my ($server) = @_; |
67 | |
55 | |
… | |
… | |
72 | |
60 | |
73 | sub editor_invoke { |
61 | sub editor_invoke { |
74 | my $editsup = $::CONN && $::CONN->{editor_support} |
62 | my $editsup = $::CONN && $::CONN->{editor_support} |
75 | or return; |
63 | or return; |
76 | |
64 | |
77 | CFPlus::background { |
65 | DC::background { |
78 | print "preparing editor startup...\n"; |
66 | print "preparing editor startup...\n"; |
79 | |
67 | |
80 | my $server = $editsup->{gameserver} || "default"; |
68 | my $server = $editsup->{gameserver} || "default"; |
81 | $server =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
69 | $server =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
82 | |
70 | |
83 | local $ENV{CROSSFIRE_MAPDIR} = my $mapdir = "$Crossfire::VARDIR/map.$server"; mkdir $mapdir; |
71 | local $ENV{CROSSFIRE_MAPDIR} = my $mapdir = "$Deliantra::VARDIR/map.$server"; mkdir $mapdir; |
84 | local $ENV{CROSSFIRE_LIBDIR} = my $libdir = "$Crossfire::VARDIR/lib.$server"; mkdir $libdir; |
72 | local $ENV{CROSSFIRE_LIBDIR} = my $libdir = "$Deliantra::VARDIR/lib.$server"; mkdir $libdir; |
85 | |
73 | |
86 | print "map directory is $mapdir\n"; |
74 | print "map directory is $mapdir\n"; |
87 | print "lib directory is $libdir\n"; |
75 | print "lib directory is $libdir\n"; |
88 | |
76 | |
89 | my $ua = CFPlus::lwp_useragent; |
77 | my $ua = DC::lwp_useragent; |
90 | |
78 | |
91 | for my $file (qw(archetypes crossfire.0)) { |
79 | for my $file (qw(archetypes crossfire.0)) { |
92 | my $url = "$editsup->{lib_root}$file"; |
80 | my $url = "$editsup->{lib_root}$file"; |
93 | print "mirroring $url...\n"; |
81 | print "mirroring $url...\n"; |
94 | CFPlus::lwp_check $ua->mirror ($url, "$libdir/$file"); |
82 | DC::lwp_check $ua->mirror ($url, "$libdir/$file"); |
95 | printf "%s size %d octets\n", $file, -s "$libdir/$file"; |
83 | printf "%s size %d octets\n", $file, -s "$libdir/$file"; |
96 | } |
84 | } |
97 | |
85 | |
98 | if (1) { # upload a map |
86 | if (1) { # upload a map |
99 | my $mapname = $::CONN->{map_info}[0]; |
87 | my $mapname = $::CONN->{map_info}[0]; |
… | |
… | |
104 | |
92 | |
105 | print "getting map revision for $mapname...\n"; |
93 | print "getting map revision for $mapname...\n"; |
106 | |
94 | |
107 | # try to get the most recent head revision, what a hack, |
95 | # try to get the most recent head revision, what a hack, |
108 | # this should have been returned while downloading *sigh* |
96 | # this should have been returned while downloading *sigh* |
109 | my $log = (CFPlus::lwp_check $ua->get ("$editsup->{cvs_root}/$mapname?view=log&logsort=rev"))->decoded_content; |
97 | my $log = (DC::lwp_check $ua->get ("$editsup->{cvs_root}/$mapname?view=log&logsort=rev"))->decoded_content; |
110 | |
98 | |
111 | if ($log =~ /\?rev=(\d+\.\d+)"/) { |
99 | if ($log =~ /\?rev=(\d+\.\d+)"/) { |
112 | my $rev = $1; |
100 | my $rev = $1; |
113 | |
101 | |
114 | print "downloading revision $rev...\n"; |
102 | print "downloading revision $rev...\n"; |
115 | |
103 | |
116 | my $map = (CFPlus::lwp_check $ua->get ("$editsup->{cvs_root}/$mapname?rev=$rev"))->decoded_content; |
104 | my $map = (DC::lwp_check $ua->get ("$editsup->{cvs_root}/$mapname?rev=$rev"))->decoded_content; |
117 | |
105 | |
118 | my $meta = { |
106 | my $meta = { |
119 | %$editsup, |
107 | %$editsup, |
120 | path => $mapname, |
108 | path => $mapname, |
121 | revision => $rev, |
109 | revision => $rev, |
… | |
… | |
126 | require File::Path; |
114 | require File::Path; |
127 | |
115 | |
128 | File::Path::mkpath (File::Basename::dirname ($mappath)); |
116 | File::Path::mkpath (File::Basename::dirname ($mappath)); |
129 | open my $fh, ">:raw:perlio", "$mappath.meta" |
117 | open my $fh, ">:raw:perlio", "$mappath.meta" |
130 | or die "$mappath.meta: $!\n"; |
118 | or die "$mappath.meta: $!\n"; |
131 | print $fh CFPlus::to_json $meta; |
119 | print $fh DC::encode_json $meta; |
132 | close $fh; |
120 | close $fh; |
133 | open my $fh, ">:raw:perlio:utf8", $mappath |
121 | open my $fh, ">:raw:perlio:utf8", $mappath |
134 | or die "$mappath: $!\n"; |
122 | or die "$mappath: $!\n"; |
135 | print $fh $map; |
123 | print $fh $map; |
136 | close $fh; |
124 | close $fh; |
… | |
… | |
175 | sub invoke_button_down { |
163 | sub invoke_button_down { |
176 | my ($self, $ev, $x, $y) = @_; |
164 | my ($self, $ev, $x, $y) = @_; |
177 | |
165 | |
178 | if ($ev->{button} == 1) { |
166 | if ($ev->{button} == 1) { |
179 | $self->grab_focus; |
167 | $self->grab_focus; |
180 | return unless $::CONN; |
168 | return unless $::CONN && $self->{ctilesize}; |
181 | |
169 | |
182 | my $x = $self->{dx} + CFPlus::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize}; |
170 | my $x = $self->{dx} + DC::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize}; |
183 | my $y = $self->{dy} + CFPlus::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize}; |
171 | my $y = $self->{dy} + DC::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize}; |
184 | |
172 | |
185 | $x -= CFPlus::floor $::MAP->w * 0.5; |
173 | $x -= DC::floor $::MAP->w * 0.5; |
186 | $y -= CFPlus::floor $::MAP->h * 0.5; |
174 | $y -= DC::floor $::MAP->h * 0.5; |
187 | |
175 | |
188 | if ($::CONN) { |
176 | if ($::CONN) { |
189 | if ($::IN_BUILD_MODE) { |
|
|
190 | $::CONN->buildat ($::IN_BUILD_MODE, $x, $y); |
|
|
191 | } else { |
|
|
192 | $::CONN->lookat ($x, $y) |
177 | $::CONN->lookat ($x, $y) |
193 | } |
|
|
194 | } |
178 | } |
195 | |
179 | |
196 | } elsif ($ev->{button} == 2) { |
180 | } elsif ($ev->{button} == 2) { |
197 | $self->grab_focus; |
181 | $self->grab_focus; |
198 | return unless $::CONN; |
182 | return unless $::CONN; |
… | |
… | |
216 | ["Statistics\tF2", sub { ::toggle_player_page ($::STATS_PAGE) }], |
200 | ["Statistics\tF2", sub { ::toggle_player_page ($::STATS_PAGE) }], |
217 | ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }], |
201 | ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }], |
218 | ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }], |
202 | ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }], |
219 | ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }], |
203 | ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }], |
220 | ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }], |
204 | ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }], |
221 | ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }], |
205 | # ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }], |
222 | [ |
|
|
223 | $::PICKUP_ENABLE->{state} |
|
|
224 | ? "Disable automatic pickup" |
|
|
225 | : "Enable automatic pickup", |
|
|
226 | sub { $::PICKUP_ENABLE->toggle } |
|
|
227 | ], |
|
|
228 | ); |
206 | ); |
229 | |
207 | |
230 | if ($::CONN && $::CONN->{editor_support}) { |
208 | if ($::CONN && $::CONN->{editor_support}) { |
231 | push @items, [ |
209 | # push @items, [ |
232 | "Edit this map <span size='xx-small'>(" . (CFPlus::asxml $::CONN->{map_info}[0]) . ")</span>", |
210 | # "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", |
233 | \&editor_invoke, |
211 | # \&editor_invoke, |
234 | ]; |
212 | # ]; |
235 | |
213 | |
236 | for my $type (qw(test name)) { |
214 | for my $type (qw(test name)) { |
237 | $::CONN->{editor_support}{type} ne $type |
215 | $::CONN->{editor_support}{type} ne $type |
238 | or next; |
216 | or next; |
239 | my $server = $::CONN->{editor_support}{"${type}server"} |
217 | my $server = $::CONN->{editor_support}{"${type}server"} |
240 | or next; |
218 | or next; |
241 | |
219 | |
242 | push @items, [ |
220 | push @items, [ |
243 | "Login on $type server <span size='xx-small'>(" . (CFPlus::asxml $server) . ")</span>", |
221 | "Login on $type server <span size='xx-small'>(" . (DC::asxml $server) . ")</span>", |
244 | sub { server_login $server }, |
222 | sub { server_login $server }, |
245 | ]; |
223 | ]; |
246 | } |
224 | } |
247 | } |
225 | } |
248 | |
226 | |
… | |
… | |
256 | } |
234 | } |
257 | } |
235 | } |
258 | ], |
236 | ], |
259 | ; |
237 | ; |
260 | |
238 | |
261 | (new CFPlus::UI::Menu |
239 | (new DC::UI::Menu |
262 | items => \@items, |
240 | items => \@items, |
263 | )->popup ($ev); |
241 | )->popup ($ev); |
264 | } |
242 | } |
265 | |
243 | |
266 | 1 |
244 | 1 |
… | |
… | |
288 | |
266 | |
289 | sub size_request { |
267 | sub size_request { |
290 | my ($self) = @_; |
268 | my ($self) = @_; |
291 | |
269 | |
292 | ( |
270 | ( |
293 | $self->{tilesize} * CFPlus::ceil $::WIDTH / $self->{tilesize}, |
271 | $self->{tilesize} * DC::ceil $::WIDTH / $self->{tilesize}, |
294 | $self->{tilesize} * CFPlus::ceil $::HEIGHT / $self->{tilesize}, |
272 | $self->{tilesize} * DC::ceil $::HEIGHT / $self->{tilesize}, |
295 | ) |
273 | ) |
296 | } |
274 | } |
297 | |
275 | |
298 | sub update { |
276 | sub update { |
299 | my ($self) = @_; |
277 | my ($self) = @_; |
… | |
… | |
301 | $self->{need_update} = 1; |
279 | $self->{need_update} = 1; |
302 | $self->SUPER::update; |
280 | $self->SUPER::update; |
303 | } |
281 | } |
304 | |
282 | |
305 | my %DIR = ( |
283 | my %DIR = ( |
306 | CFPlus::SDLK_KP5, [0, "stay fire"], |
284 | ( "," . DC::SDLK_KP5 ), [0, "stay fire"], |
307 | CFPlus::SDLK_KP8, [1, "north"], |
285 | ( "," . DC::SDLK_KP8 ), [1, "north"], |
308 | CFPlus::SDLK_KP9, [2, "northeast"], |
286 | ( "," . DC::SDLK_KP9 ), [2, "northeast"], |
309 | CFPlus::SDLK_KP6, [3, "east"], |
287 | ( "," . DC::SDLK_KP6 ), [3, "east"], |
310 | CFPlus::SDLK_KP3, [4, "southeast"], |
288 | ( "," . DC::SDLK_KP3 ), [4, "southeast"], |
311 | CFPlus::SDLK_KP2, [5, "south"], |
289 | ( "," . DC::SDLK_KP2 ), [5, "south"], |
312 | CFPlus::SDLK_KP1, [6, "southwest"], |
290 | ( "," . DC::SDLK_KP1 ), [6, "southwest"], |
313 | CFPlus::SDLK_KP4, [7, "west"], |
291 | ( "," . DC::SDLK_KP4 ), [7, "west"], |
314 | CFPlus::SDLK_KP7, [8, "northwest"], |
292 | ( "," . DC::SDLK_KP7 ), [8, "northwest"], |
315 | |
293 | |
316 | CFPlus::SDLK_UP, [1, "north"], |
294 | ( "," . DC::SDLK_PAGEUP ), [2, "northeast"], |
|
|
295 | ( "," . DC::SDLK_PAGEDOWN ), [4, "southeast"], |
|
|
296 | ( "," . DC::SDLK_END ), [6, "southwest"], |
|
|
297 | ( "," . DC::SDLK_HOME ), [8, "northwest"], |
|
|
298 | |
|
|
299 | ( "," . DC::SDLK_UP ), [1, "north"], |
|
|
300 | ("1," . DC::SDLK_UP ), [2, "northeast"], |
317 | CFPlus::SDLK_RIGHT, [3, "east"], |
301 | ( "," . DC::SDLK_RIGHT ), [3, "east"], |
318 | CFPlus::SDLK_DOWN, [5, "south"], |
302 | ("1," . DC::SDLK_RIGHT ), [4, "southeast"], |
319 | CFPlus::SDLK_LEFT, [7, "west"], |
303 | ( "," . DC::SDLK_DOWN ), [5, "south"], |
|
|
304 | ("1," . DC::SDLK_DOWN ), [6, "southwest"], |
|
|
305 | ( "," . DC::SDLK_LEFT ), [7, "west"], |
|
|
306 | ("1," . DC::SDLK_LEFT ), [8, "northwest"], |
320 | ); |
307 | ); |
321 | |
308 | |
322 | sub invoke_key_down { |
309 | sub invoke_key_down { |
323 | my ($self, $ev) = @_; |
310 | my ($self, $ev) = @_; |
324 | |
311 | |
325 | my $mod = $ev->{mod}; |
312 | my $mod = $ev->{mod}; |
326 | my $sym = $ev->{sym}; |
313 | my $sym = $ev->{sym}; |
327 | my $uni = $ev->{unicode}; |
314 | my $uni = $ev->{unicode}; |
328 | |
315 | |
329 | $mod &= CFPlus::KMOD_CTRL | CFPlus::KMOD_ALT | CFPlus::KMOD_SHIFT; |
316 | $mod &= DC::KMOD_CTRL | DC::KMOD_ALT | DC::KMOD_META | DC::KMOD_SHIFT; |
330 | |
317 | |
331 | if ($sym == 9) { |
318 | # ignore repeated keypresses |
332 | ($mod & CFPlus::KMOD_SHIFT ? $::CONSOLE->{window} : $::PL_WINDOW)->toggle_visibility; |
319 | return if $self->{last_mod} == $mod && $self->{last_sym} == $sym; |
333 | } elsif ($sym == CFPlus::SDLK_F1 && !$mod) { |
320 | $self->{last_mod} = $mod; |
334 | $::HELP_WINDOW->toggle_visibility; |
321 | $self->{last_sym} = $sym; |
335 | } elsif ($sym == CFPlus::SDLK_F2 && !$mod) { |
|
|
336 | ::toggle_player_page ($::STATS_PAGE); |
|
|
337 | } elsif ($sym == CFPlus::SDLK_F3 && !$mod) { |
|
|
338 | ::toggle_player_page ($::SKILL_PAGE); |
|
|
339 | } elsif ($sym == CFPlus::SDLK_F4 && !$mod) { |
|
|
340 | ::toggle_player_page ($::SPELL_PAGE); |
|
|
341 | } elsif ($sym == CFPlus::SDLK_F5 && !$mod) { |
|
|
342 | ::toggle_player_page ($::INVENTORY_PAGE); |
|
|
343 | } elsif ($sym == CFPlus::SDLK_F9 && !$mod) { |
|
|
344 | $::SETUP_DIALOG->toggle_visibility; |
|
|
345 | } elsif (!$::CONN) { |
|
|
346 | return 0; # bindings further down need a valid connection |
|
|
347 | |
322 | |
348 | } elsif ($uni == ord ",") { |
323 | my $dir = $DIR{ (!!($mod & (DC::KMOD_ALT | DC::KMOD_META))) . ",$sym" }; |
349 | $::CONN->user_send ("take"); |
324 | |
350 | } elsif ($uni == ord " ") { |
325 | if ($::CONN && $dir) { |
351 | $::CONN->user_send ("apply"); |
|
|
352 | } elsif ($uni == 13) { |
|
|
353 | $::CONN->user_send ("examine"); |
|
|
354 | } elsif ($uni == ord ".") { |
|
|
355 | $::CONN->user_send ($self->{completer}{last_command}) |
|
|
356 | if exists $self->{completer}{last_command}; |
|
|
357 | } elsif (my @macros = CFPlus::Macro::match_event $ev) { |
|
|
358 | $::CONN->macro_send ($_) for @macros; |
|
|
359 | } elsif (($sym == CFPlus::SDLK_KP_PLUS && !$mod) || $uni == ord "+") { |
|
|
360 | $::CONN->user_send ("rotateshoottype +"); |
|
|
361 | } elsif (($sym == CFPlus::SDLK_KP_MINUS && !$mod) || $uni == ord "-") { |
|
|
362 | $::CONN->user_send ("rotateshoottype -"); |
|
|
363 | } elsif ($uni == ord '!') { |
|
|
364 | $self->{completer}->set_prefix ("shout "); |
|
|
365 | $self->{completer}->show; |
|
|
366 | } elsif ($uni == ord '"') { |
|
|
367 | $self->{completer}->set_prefix ("$::CFG->{say_command} "); |
|
|
368 | $self->{completer}->show; |
|
|
369 | } elsif ($uni == ord "'") { |
|
|
370 | $self->{completer}->set_prefix (""); |
|
|
371 | $self->{completer}->show; |
|
|
372 | } elsif (exists $DIR{$sym}) { |
|
|
373 | if ($mod & CFPlus::KMOD_SHIFT) { |
326 | if ($mod & DC::KMOD_SHIFT) { |
374 | $self->{shft}++; |
327 | $self->{shft}++; |
375 | if ($DIR{$sym}[0] != $self->{fire_dir}) { |
328 | if ($dir->[0] != $self->{fire_dir}) { |
376 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
329 | $::CONN->user_send ("fire $dir->[0]"); |
377 | } |
330 | } |
378 | $self->{fire_dir} = $DIR{$sym}[0]; |
331 | $self->{fire_dir} = $dir->[0]; |
379 | } elsif ($mod & CFPlus::KMOD_CTRL) { |
332 | } elsif ($mod & DC::KMOD_CTRL) { |
380 | $self->{ctrl}++; |
333 | $self->{ctrl}++; |
381 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
334 | $::CONN->user_send ("run $dir->[0]"); |
382 | } else { |
335 | } else { |
383 | $::CONN->user_send ("$DIR{$sym}[1]"); |
336 | $::CONN->user_send ("$dir->[1]"); |
384 | } |
337 | } |
385 | } elsif ((ord 'a') <= $uni && $uni <= (ord 'z')) { |
338 | |
386 | $self->{completer}->inject_key_down ($ev); |
|
|
387 | $self->{completer}->show; |
|
|
388 | } else { |
|
|
389 | return 0; |
339 | return 1; |
390 | } |
|
|
391 | |
|
|
392 | 1 |
340 | } |
|
|
341 | |
|
|
342 | 0 |
393 | } |
343 | } |
394 | |
344 | |
395 | sub invoke_key_up { |
345 | sub invoke_key_up { |
396 | my ($self, $ev) = @_; |
346 | my ($self, $ev) = @_; |
397 | |
347 | |
398 | my $res = 0; |
348 | my $res = 0; |
399 | my $mod = $ev->{mod}; |
349 | my $mod = $ev->{mod}; |
400 | my $sym = $ev->{sym}; |
350 | my $sym = $ev->{sym}; |
401 | |
351 | |
|
|
352 | delete $self->{last_mod}; |
|
|
353 | delete $self->{last_sym}; |
|
|
354 | |
402 | if ($::CFG->{shift_fire_stop}) { |
355 | if ($::CFG->{shift_fire_stop}) { |
403 | if (!($mod & CFPlus::KMOD_SHIFT) && delete $self->{shft}) { |
356 | if (!($mod & DC::KMOD_SHIFT) && delete $self->{shft}) { |
404 | $::CONN->user_send ("fire_stop"); |
357 | $::CONN->user_send ("fire_stop"); |
405 | delete $self->{fire_dir}; |
358 | delete $self->{fire_dir}; |
406 | $res = 1; |
359 | $res = 1; |
407 | } |
360 | } |
408 | } else { |
361 | } else { |
|
|
362 | my $dir = $DIR{ (!!($mod & (DC::KMOD_ALT | DC::KMOD_META))) . ",$sym" }; |
|
|
363 | |
409 | if (exists $DIR{$sym} && delete $self->{shft}) { |
364 | if ($dir && delete $self->{shft}) { |
410 | $::CONN->user_send ("fire_stop"); |
365 | $::CONN->user_send ("fire_stop"); |
411 | delete $self->{fire_dir}; |
366 | delete $self->{fire_dir}; |
412 | $res = 1; |
367 | $res = 1; |
413 | } elsif (($sym == CFPlus::SDLK_LSHIFT || $sym == CFPlus::SDLK_RSHIFT) && delete $self->{shft}) { # XXX: is RSHIFT ok? |
368 | } elsif (($sym == DC::SDLK_LSHIFT || $sym == DC::SDLK_RSHIFT) |
|
|
369 | && delete $self->{shft}) { # XXX: is RSHIFT ok? |
414 | $::CONN->user_send ("fire_stop"); |
370 | $::CONN->user_send ("fire_stop"); |
415 | delete $self->{fire_dir}; |
371 | delete $self->{fire_dir}; |
416 | $res = 1; |
372 | $res = 1; |
417 | } |
373 | } |
418 | } |
374 | } |
419 | |
375 | |
420 | if (!($mod & CFPlus::KMOD_CTRL ) && delete $self->{ctrl}) { |
376 | if (!($mod & DC::KMOD_CTRL) && delete $self->{ctrl}) { |
421 | $::CONN->user_send ("run_stop"); |
377 | $::CONN->user_send ("run_stop"); |
422 | $res = 1; |
378 | $res = 1; |
423 | } |
379 | } |
424 | |
380 | |
425 | $res |
381 | $res |
… | |
… | |
431 | $self->refresh_hook; |
387 | $self->refresh_hook; |
432 | |
388 | |
433 | 0 |
389 | 0 |
434 | } |
390 | } |
435 | |
391 | |
|
|
392 | sub set_tilesize { |
|
|
393 | my ($self, $tilesize) = @_; |
|
|
394 | |
|
|
395 | $self->{tilesize} = $tilesize; |
|
|
396 | } |
|
|
397 | |
|
|
398 | sub scroll { |
|
|
399 | my ($self, $dx, $dy) = @_; |
|
|
400 | |
|
|
401 | $self->movement_update; |
|
|
402 | |
|
|
403 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
|
|
404 | $self->{sdy} += $dy * $self->{tilesize}; |
|
|
405 | |
|
|
406 | # save old fow texture, if applicable |
|
|
407 | $self->{last_fow_texture} = $self->{fow_texture} |
|
|
408 | if $::CFG->{smooth_transitions}; |
|
|
409 | $self->{lfdx} = $dx; |
|
|
410 | $self->{lfdy} = $dy; |
|
|
411 | $self->{lmdx} = $self->{dx}; |
|
|
412 | $self->{lmdy} = $self->{dy}; |
|
|
413 | |
|
|
414 | $::MAP->scroll ($dx, $dy); |
|
|
415 | } |
|
|
416 | |
436 | sub set_magicmap { |
417 | sub set_magicmap { |
437 | my ($self, $w, $h, $x, $y, $data) = @_; |
418 | my ($self, $w, $h, $x, $y, $data) = @_; |
438 | |
419 | |
439 | $x -= $::MAP->ox + 1 + int 0.5 * $::MAP->w; |
420 | $x -= $::MAP->ox + 1 + int 0.5 * $::MAP->w; |
440 | $y -= $::MAP->oy + 1 + int 0.5 * $::MAP->h; |
421 | $y -= $::MAP->oy + 1 + int 0.5 * $::MAP->h; |
… | |
… | |
442 | $self->{magicmap} = [$x, $y, $w, $h, $data]; |
423 | $self->{magicmap} = [$x, $y, $w, $h, $data]; |
443 | |
424 | |
444 | $self->update; |
425 | $self->update; |
445 | } |
426 | } |
446 | |
427 | |
|
|
428 | sub movement_update { |
|
|
429 | my ($self) = @_; |
|
|
430 | |
|
|
431 | if ($::CFG->{smooth_movement}) { |
|
|
432 | if ($self->{sdx} || $self->{sdy}) { |
|
|
433 | my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); |
|
|
434 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
|
|
435 | |
|
|
436 | # the minimum time for a single tile movement |
|
|
437 | my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1); |
|
|
438 | |
|
|
439 | $spd *= $self->{tilesize}; |
|
|
440 | |
|
|
441 | # jump if "impossibly high" speed |
|
|
442 | if ( |
|
|
443 | (max abs $self->{sdx}, abs $self->{sdy}) |
|
|
444 | > $spd * $mintime * 2.1 |
|
|
445 | ) { |
|
|
446 | #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 2.1;#d# |
|
|
447 | $self->{sdx} = $self->{sdy} = 0; |
|
|
448 | } else { |
|
|
449 | $spd *= $diff * 1.0001; # 1.0001 so that we don't accumulate rounding errors the wrong direction |
|
|
450 | |
|
|
451 | my $dx = $self->{sdx} < 0 ? -$spd : $spd; |
|
|
452 | my $dy = $self->{sdy} < 0 ? -$spd : $spd; |
|
|
453 | |
|
|
454 | if ($self->{sdx} * ($self->{sdx} - $dx) <= 0) { $self->{sdx} = 0 } else { $self->{sdx} -= $dx } |
|
|
455 | if ($self->{sdy} * ($self->{sdy} - $dy) <= 0) { $self->{sdy} = 0 } else { $self->{sdy} -= $dy } |
|
|
456 | } |
|
|
457 | |
|
|
458 | $self->update; |
|
|
459 | } |
|
|
460 | } else { |
|
|
461 | $self->{sdx} = $self->{sdy} = 0; |
|
|
462 | } |
|
|
463 | |
|
|
464 | $self->{last_update} = EV::time; |
|
|
465 | } |
|
|
466 | |
447 | sub refresh_hook { |
467 | sub refresh_hook { |
448 | my ($self) = @_; |
468 | my ($self) = @_; |
449 | |
469 | |
450 | return unless $::MAP; |
470 | if ($::MAP && $::CONN) { |
451 | |
|
|
452 | if (delete $self->{need_update}) { |
471 | if (delete $self->{need_update}) { |
|
|
472 | $self->movement_update; |
|
|
473 | |
453 | my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1; |
474 | my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1; |
454 | |
475 | |
|
|
476 | my $sdx_t = DC::ceil $self->{sdx} / $tilesize; |
|
|
477 | my $sdy_t = DC::ceil $self->{sdy} / $tilesize; |
|
|
478 | |
|
|
479 | # width/height of map, in tiles |
455 | my $sw = $self->{sw} = 1 + CFPlus::ceil $self->{w} / $tilesize; |
480 | my $sw = $self->{sw} = 2 + DC::ceil $self->{w} / $tilesize; |
456 | my $sh = $self->{sh} = 1 + CFPlus::ceil $self->{h} / $tilesize; |
481 | my $sh = $self->{sh} = 2 + DC::ceil $self->{h} / $tilesize; |
457 | |
482 | |
|
|
483 | # the map displacement, in tiles |
458 | my $sx = CFPlus::ceil $::CFG->{map_shift_x} / $tilesize; |
484 | my $sx = DC::ceil $::CFG->{map_shift_x} / $tilesize + $sdx_t; |
459 | my $sy = CFPlus::ceil $::CFG->{map_shift_y} / $tilesize; |
485 | my $sy = DC::ceil $::CFG->{map_shift_y} / $tilesize + $sdy_t; |
460 | |
486 | |
|
|
487 | # the upper left "visible" corner, in pixels |
461 | my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize; |
488 | my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize; |
462 | my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize; |
489 | my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize; |
463 | |
490 | |
464 | my $dx = $self->{dx} = CFPlus::ceil 0.5 * ($::MAP->w - $sw) - $sx; |
491 | my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx; |
465 | my $dy = $self->{dy} = CFPlus::ceil 0.5 * ($::MAP->h - $sh) - $sy; |
492 | my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy; |
466 | |
493 | |
467 | if ($::CFG->{fow_enable}) { |
494 | if ($::CFG->{fow_enable}) { |
468 | my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh); |
495 | my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh); |
469 | |
496 | |
470 | $self->{fow_texture} = new CFPlus::Texture |
497 | $self->{fow_texture} = new DC::Texture |
471 | w => $w, |
498 | w => $w, |
472 | h => $h, |
499 | h => $h, |
473 | data => $data, |
500 | data => $data, |
474 | internalformat => GL_ALPHA, |
501 | internalformat => GL_ALPHA, |
475 | format => GL_ALPHA; |
502 | format => GL_ALPHA; |
476 | } else { |
503 | } else { |
477 | delete $self->{fow_texture}; |
504 | delete $self->{fow_texture}; |
478 | } |
505 | } |
479 | |
506 | |
480 | glNewList $self->{list}; |
507 | glNewList ($self->{list} ||= glGenList); |
481 | |
508 | |
482 | glPushMatrix; |
|
|
483 | glTranslate $sx0, $sy0; |
|
|
484 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
|
|
485 | |
|
|
486 | $::MAP->draw ($dx, $dy, $sw, $sh, $self->{tilesize}); |
|
|
487 | |
|
|
488 | glScale $self->{tilesize}, $self->{tilesize}; |
|
|
489 | |
|
|
490 | if (my $tex = $self->{fow_texture}) { |
|
|
491 | glPushMatrix; |
509 | glPushMatrix; |
492 | glScale 1/3, 1/3; |
510 | glTranslate $sx0, $sy0; |
493 | glEnable GL_TEXTURE_2D; |
511 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
494 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
512 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
495 | |
513 | |
496 | glColor +($::CFG->{fow_intensity}) x 3, 0.9; |
514 | $::MAP->draw ($dx, $dy, $sw, $sh, |
497 | $self->{fow_texture}->draw_quad_alpha (0, 0); |
515 | $self->{tilesize}, |
|
|
516 | $::CONN->{player}{tag}, |
|
|
517 | -$self->{sdx}, -$self->{sdy}); |
498 | |
518 | |
499 | glDisable GL_TEXTURE_2D; |
519 | glScale $self->{tilesize}, $self->{tilesize}; |
|
|
520 | |
|
|
521 | if (my $tex = $self->{fow_texture}) { |
|
|
522 | if ($DC::OpenGL::GL_MULTITEX && $self->{last_fow_texture}) {#d# |
|
|
523 | my $d1 = DC::distance $self->{sdx}, $self->{sdy}; |
|
|
524 | my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize; |
|
|
525 | |
|
|
526 | if ($d1 * $d2) { |
|
|
527 | DC::Texture::draw_fow_texture |
|
|
528 | $::CFG->{fow_intensity}, |
|
|
529 | @{$self->{fow_texture}}{qw(name s t w h)}, |
|
|
530 | (min 1, $d1 / $d2), |
|
|
531 | $self->{lmdx} - $dx - $self->{lfdx}, |
|
|
532 | $self->{lmdy} - $dy - $self->{lfdy}, |
|
|
533 | @{$self->{last_fow_texture}}{qw(name s t w h)}; |
|
|
534 | } else { |
|
|
535 | delete $self->{last_fow_texture}; |
|
|
536 | } |
|
|
537 | } |
|
|
538 | |
|
|
539 | unless ($self->{last_fow_texture}) { |
|
|
540 | DC::Texture::draw_fow_texture |
|
|
541 | $::CFG->{fow_intensity}, |
|
|
542 | @{$self->{fow_texture}}{qw(name s t w h)}; |
|
|
543 | } |
|
|
544 | } |
|
|
545 | |
|
|
546 | if ($self->{magicmap}) { |
|
|
547 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
|
|
548 | |
|
|
549 | $x += $::MAP->ox + $self->{dx}; |
|
|
550 | $y += $::MAP->oy + $self->{dy}; |
|
|
551 | |
|
|
552 | glTranslate - $x - 1, - $y - 1; |
|
|
553 | glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; |
|
|
554 | $::MAP->draw_magicmap ($w, $h, $data); |
|
|
555 | } |
|
|
556 | |
500 | glPopMatrix; |
557 | glPopMatrix; |
501 | } |
|
|
502 | |
|
|
503 | if ($self->{magicmap}) { |
|
|
504 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
|
|
505 | |
|
|
506 | $x += $::MAP->ox + $self->{dx}; |
|
|
507 | $y += $::MAP->oy + $self->{dy}; |
|
|
508 | |
|
|
509 | glTranslate - $x - 1, - $y - 1; |
|
|
510 | glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; |
|
|
511 | $::MAP->draw_magicmap ($x, $y, $w, $h, $data); |
|
|
512 | } |
|
|
513 | |
|
|
514 | glPopMatrix; |
|
|
515 | glEndList; |
558 | glEndList; |
|
|
559 | } |
|
|
560 | } else { |
|
|
561 | glDeleteList delete $self->{list} |
|
|
562 | if $self->{list}; |
516 | } |
563 | } |
517 | |
|
|
518 | } |
564 | } |
519 | |
565 | |
520 | sub draw { |
566 | sub draw { |
521 | my ($self) = @_; |
567 | my ($self) = @_; |
522 | |
568 | |
523 | $self->{root}->on_post_alloc (prepare => sub { $self->refresh_hook }); |
569 | $self->{root}->on_post_alloc (prepare => sub { $self->refresh_hook }); |
524 | |
570 | |
525 | return unless $self->{list}; |
571 | return unless $self->{list}; |
526 | |
572 | |
527 | my $focused = $CFPlus::UI::FOCUS == $self |
573 | my $focused = $DC::UI::FOCUS == $self |
528 | || $CFPlus::UI::FOCUS == $self->{completer}{entry}; |
574 | || $DC::UI::FOCUS == $::COMPLETER->{entry}; |
529 | |
575 | |
530 | return |
576 | return |
531 | unless $focused || !$::FAST; |
577 | unless $focused || !$::FAST; |
532 | |
578 | |
533 | glCallList $self->{list}; |
579 | glCallList $self->{list}; |
534 | |
580 | |
535 | # TNT2 emulates logops in software (or worse :) |
581 | # TNT2 emulates logops in software (or worse :) |
536 | unless ($focused) { |
582 | unless ($focused) { |
537 | glColor 0.4, 0.2, 0.2, 0.6; |
583 | glColor_premultiply 0, 0, 1, 0.25; |
538 | glEnable GL_BLEND; |
584 | glEnable GL_BLEND; |
539 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
585 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
540 | glBegin GL_QUADS; |
586 | glBegin GL_QUADS; |
541 | glVertex 0, 0; |
587 | glVertex 0, 0; |
542 | glVertex 0, $::HEIGHT; |
588 | glVertex 0, $::HEIGHT; |
543 | glVertex $::WIDTH, $::HEIGHT; |
589 | glVertex $::WIDTH, $::HEIGHT; |
544 | glVertex $::WIDTH, 0; |
590 | glVertex $::WIDTH, 0; |
… | |
… | |
553 | glDeleteList $self->{list}; |
599 | glDeleteList $self->{list}; |
554 | |
600 | |
555 | $self->SUPER::DESTROY; |
601 | $self->SUPER::DESTROY; |
556 | } |
602 | } |
557 | |
603 | |
558 | package CFPlus::MapWidget::MapMap; |
604 | package DC::MapWidget::MapMap; |
559 | |
605 | |
560 | use strict; |
606 | use strict; |
561 | use utf8; |
607 | use utf8; |
562 | |
608 | |
563 | our @ISA = CFPlus::UI::Base::; |
609 | our @ISA = DC::UI::Base::; |
564 | |
610 | |
565 | use Time::HiRes qw(time); |
611 | use Time::HiRes qw(time); |
566 | use CFPlus::OpenGL; |
612 | use DC::OpenGL; |
567 | |
613 | |
568 | sub size_request { |
614 | sub size_request { |
569 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
615 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
570 | } |
616 | } |
571 | |
617 | |
… | |
… | |
573 | my ($self) = @_; |
619 | my ($self) = @_; |
574 | |
620 | |
575 | if ($::MAP && $self->{texture_atime} < time) { |
621 | if ($::MAP && $self->{texture_atime} < time) { |
576 | my ($w, $h) = @$self{qw(w h)}; |
622 | my ($w, $h) = @$self{qw(w h)}; |
577 | |
623 | |
|
|
624 | return unless $w && $h; |
|
|
625 | |
578 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
626 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
579 | my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
627 | my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
580 | |
628 | |
581 | my $ox = 0.5 * ($w - $sw); |
629 | my $ox = 0.5 * ($w - $sw); |
582 | my $oy = 0.5 * ($h - $sh); |
630 | my $oy = 0.5 * ($h - $sh); |
… | |
… | |
596 | $self->{y0} = $y0; |
644 | $self->{y0} = $y0; |
597 | |
645 | |
598 | $self->{texture_atime} = time + 1/3; |
646 | $self->{texture_atime} = time + 1/3; |
599 | |
647 | |
600 | $self->{texture} = |
648 | $self->{texture} = |
601 | new CFPlus::Texture |
649 | new DC::Texture |
602 | w => $w, |
650 | w => $w, |
603 | h => $h, |
651 | h => $h, |
604 | data => $::MAP->mapmap (-$ox, -$oy, $w, $h), |
652 | data => $::MAP->mapmap (-$ox, -$oy, $w, $h), |
605 | type => $CFPlus::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
653 | type => $DC::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
606 | } |
654 | } |
607 | } |
655 | } |
608 | |
656 | |
609 | sub invoke_visibility_change { |
657 | sub invoke_visibility_change { |
610 | my ($self) = @_; |
658 | my ($self) = @_; |
… | |
… | |
656 | glEnd; |
704 | glEnd; |
657 | |
705 | |
658 | glDisable GL_BLEND; |
706 | glDisable GL_BLEND; |
659 | } |
707 | } |
660 | |
708 | |
661 | package CFPlus::MapWidget::Command; |
709 | package DC::MapWidget::Command; |
662 | |
710 | |
663 | use strict; |
711 | use strict; |
664 | |
712 | |
665 | use CFPlus::OpenGL; |
713 | use DC::OpenGL; |
666 | |
714 | |
667 | our @ISA = CFPlus::UI::Frame::; |
715 | our @ISA = DC::UI::Frame::; |
668 | |
716 | |
669 | sub new { |
717 | sub new { |
670 | my $class = shift; |
718 | my $class = shift; |
671 | |
719 | |
672 | my $self = $class->SUPER::new ( |
720 | my $self = $class->SUPER::new ( |
673 | bg => [0, 0, 0, 0.8], |
721 | bg => [0, 0, 0, 0.8], |
674 | @_, |
722 | @_, |
675 | ); |
723 | ); |
676 | |
724 | |
677 | $self->add ($self->{vbox} = new CFPlus::UI::VBox); |
725 | $self->add ($self->{vbox} = new DC::UI::VBox); |
678 | |
726 | |
679 | $self->{label} = [ |
727 | $self->{label} = [ |
680 | map |
728 | map |
681 | CFPlus::UI::Label->new ( |
729 | DC::UI::Label->new ( |
|
|
730 | align => 0, |
682 | can_hover => 1, |
731 | can_hover => 1, |
683 | can_events => 1, |
732 | can_events => 1, |
684 | tooltip_width => 0.33, |
733 | tooltip_width => 0.33, |
685 | fontsize => $_, |
734 | fontsize => $_, |
686 | ), (0.8) x 16 |
735 | ), (0.8) x 16 |
687 | ]; |
736 | ]; |
688 | |
737 | |
689 | $self->{entry} = new CFPlus::UI::Entry |
738 | $self->{entry} = new DC::UI::Entry |
690 | on_changed => sub { |
739 | on_changed => sub { |
691 | $self->update_labels; |
740 | $self->update_labels; |
692 | 0 |
741 | 0 |
693 | }, |
742 | }, |
694 | on_button_down => sub { |
743 | on_button_down => sub { |
695 | my ($entry, $ev, $x, $y) = @_; |
744 | my ($entry, $ev, $x, $y) = @_; |
696 | |
745 | |
697 | if ($ev->{button} == 3) { |
746 | if ($ev->{button} == 3) { |
698 | (new CFPlus::UI::Menu |
747 | (new DC::UI::Menu |
699 | items => [ |
748 | items => [ |
700 | ["bind <i>" . (CFPlus::asxml $self->{select}) . "</i> to a key" |
749 | ["bind <i>" . (DC::asxml $self->{select}) . "</i> to a key" |
701 | => sub { CFPlus::Macro::quick_macro [$self->{select}], sub { $entry->grab_focus } }] |
750 | => sub { DC::Macro::quick_macro [$self->{select}], sub { $entry->grab_focus } }] |
702 | ], |
751 | ], |
703 | )->popup ($ev); |
752 | )->popup ($ev); |
704 | return 1; |
753 | return 1; |
705 | } |
754 | } |
706 | 0 |
755 | 0 |
… | |
… | |
721 | $self->hide; |
770 | $self->hide; |
722 | } |
771 | } |
723 | } elsif ($ev->{sym} == 27) { |
772 | } elsif ($ev->{sym} == 27) { |
724 | $self->{hist_ptr} = 0; |
773 | $self->{hist_ptr} = 0; |
725 | $self->hide; |
774 | $self->hide; |
726 | } elsif ($ev->{sym} == CFPlus::SDLK_DOWN) { |
775 | } elsif ($ev->{sym} == DC::SDLK_DOWN) { |
727 | if ($self->{hist_ptr} > 1) { |
776 | if ($self->{hist_ptr} > 1) { |
728 | $self->{hist_ptr}--; |
777 | $self->{hist_ptr}--; |
729 | $self->{entry}->set_text ($self->{history}->[$self->{hist_ptr} - 1]); |
778 | $self->{entry}->set_text ($self->{history}->[$self->{hist_ptr} - 1]); |
730 | } elsif ($self->{hist_ptr} > 0) { |
779 | } elsif ($self->{hist_ptr} > 0) { |
731 | $self->{hist_ptr}--; |
780 | $self->{hist_ptr}--; |
… | |
… | |
733 | } else { |
782 | } else { |
734 | ++$self->{select_offset} |
783 | ++$self->{select_offset} |
735 | if $self->{select_offset} < $#{ $self->{last_match} || [] }; |
784 | if $self->{select_offset} < $#{ $self->{last_match} || [] }; |
736 | } |
785 | } |
737 | $self->update_labels; |
786 | $self->update_labels; |
738 | } elsif ($ev->{sym} == CFPlus::SDLK_UP) { |
787 | } elsif ($ev->{sym} == DC::SDLK_UP) { |
739 | if ($self->{select_offset}) { |
788 | if ($self->{select_offset}) { |
740 | --$self->{select_offset} |
789 | --$self->{select_offset} |
741 | } else { |
790 | } else { |
742 | unless ($self->{hist_ptr}) { |
791 | unless ($self->{hist_ptr}) { |
743 | $self->{hist_saveback} = $self->{entry}->get_text; |
792 | $self->{hist_saveback} = $self->{entry}->get_text; |
… | |
… | |
809 | my $text = $self->{entry}->get_text; |
858 | my $text = $self->{entry}->get_text; |
810 | |
859 | |
811 | length $text |
860 | length $text |
812 | or return $self->hide; |
861 | or return $self->hide; |
813 | |
862 | |
814 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
|
|
815 | |
|
|
816 | if ($text ne $self->{last_search}) { |
863 | if ($text ne $self->{last_search}) { |
817 | my @match; |
864 | my @match; |
818 | |
865 | |
819 | if ($text =~ /^(.*?)\s+$/) { |
866 | if ($text =~ /^(.*?)\s+$/) { |
|
|
867 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
820 | @match = [$cmd, "(appended whitespace suppresses completion)"]; |
868 | @match = ([[$cmd,'(appended whitespace suppresses completion)'],$text]); |
821 | } else { |
869 | } else { |
|
|
870 | # @match is [command, penalty, command with arguments] until sort |
|
|
871 | |
|
|
872 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
|
|
873 | |
822 | my $regexp = do { |
874 | my $regexp_abbrev = do { |
823 | my ($beg, @chr) = split //, lc $cmd; |
875 | my ($beg, @chr) = split //, lc $cmd; |
824 | |
876 | |
825 | # the following regex is used to match our "completion entry" |
877 | # the following regex is used to match our "completion entry" |
826 | # to an actual command - the parentheses match kind of "overhead" |
878 | # to an actual command - the parentheses match kind of "overhead" |
827 | # - the more characters the parentheses match, the less attractive |
879 | # - the more characters the parentheses match, the less attractive |
… | |
… | |
829 | my $regexp = "^\Q$beg\E" |
881 | my $regexp = "^\Q$beg\E" |
830 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
882 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
831 | qr<$regexp> |
883 | qr<$regexp> |
832 | }; |
884 | }; |
833 | |
885 | |
834 | my @penalty; |
886 | my $regexp_partial = do { |
|
|
887 | my $regexp = "^\Q$text\E(.*)"; |
|
|
888 | qr<$regexp> |
|
|
889 | }; |
835 | |
890 | |
836 | for (keys %{$self->{command}}) { |
891 | for (keys %{$self->{command}}) { |
837 | if (@penalty = $_ =~ $regexp) { |
892 | my @scores; |
838 | push @match, [$_, length join "", map "::$_", grep defined, @penalty]; |
893 | |
|
|
894 | # 1. Complete command [with args] |
|
|
895 | # command is a prefix of the text |
|
|
896 | # score is length of complete command matched |
|
|
897 | # e.g. "invoke summon pet monster bat" |
|
|
898 | # "invoke" "summon pet monster bat" = 6 |
|
|
899 | # "invoke summon pet monster" "bat" = 25 |
|
|
900 | if ($text =~ /^\Q$_\E(.*)/) { |
|
|
901 | push @scores, [$_, length $_, $text]; |
839 | } |
902 | } |
|
|
903 | |
|
|
904 | # 2. Partial command |
|
|
905 | # text is a prefix of the full command |
|
|
906 | # score is the length of the input text |
|
|
907 | # e.g. "invoke s" |
|
|
908 | # "invoke small fireball" = 8 |
|
|
909 | # "invoke summon pet monster" = 8 |
|
|
910 | |
|
|
911 | if ($_ =~ $regexp_partial) { |
|
|
912 | push @scores, [$_, length $text, $_]; |
|
|
913 | } |
|
|
914 | |
|
|
915 | # 3. Abbreviation match |
|
|
916 | # attempts to use first word of text as an abbreviated command |
|
|
917 | # score is length of word + 1 - 3 per non-word-initial character |
|
|
918 | |
|
|
919 | if (my @penalty = $_ =~ $regexp_abbrev) { |
|
|
920 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
|
|
921 | } |
|
|
922 | |
|
|
923 | # Pick the best option for this command |
|
|
924 | push @match, (sort { |
|
|
925 | $b->[1] <=> $a->[1] |
|
|
926 | } @scores)[0]; |
840 | } |
927 | } |
841 | |
928 | |
|
|
929 | # @match is now [command object, command with arguments] |
842 | @match = map $self->{command}{$_->[0]}, |
930 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
843 | sort { |
931 | sort { |
844 | $a->[1] <=> $b->[1] |
932 | $b->[1] <=> $a->[1] |
845 | or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] |
933 | or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] |
846 | or (length $b->[0]) <=> (length $a->[0]) |
934 | or (length $b->[0]) <=> (length $a->[0]) |
847 | } @match; |
935 | } @match; |
848 | } |
936 | } |
849 | |
937 | |
… | |
… | |
868 | $label->{fg} = [1, 1, 1, 1]; |
956 | $label->{fg} = [1, 1, 1, 1]; |
869 | $label->{bg} = [0, 0, 0, 0]; |
957 | $label->{bg} = [0, 0, 0, 0]; |
870 | } |
958 | } |
871 | |
959 | |
872 | if (@matches) { |
960 | if (@matches) { |
873 | $self->{select} = "$matches[0][0]$arg"; |
961 | $self->{select} = "$matches[0][1]"; |
874 | |
962 | |
875 | $labels[0]->{fg} = [0, 0, 0, 1]; |
963 | $labels[0]->{fg} = [0, 0, 0, 1]; |
876 | $labels[0]->{bg} = [1, 1, 1, 0.8]; |
964 | $labels[0]->{bg} = [1, 1, 1, 0.8]; |
877 | } else { |
965 | } else { |
878 | $self->{select} = "$cmd$arg"; |
966 | $self->{select} = "$text"; |
879 | } |
967 | } |
880 | |
968 | |
881 | for my $match (@matches) { |
969 | for my $match (@matches) { |
882 | my $label = shift @labels; |
970 | my $label = shift @labels; |
883 | |
971 | |
884 | if (@labels) { |
972 | if (@labels) { |
885 | $label->set_text ("$match->[0]$arg"); |
973 | $label->set_text ("$match->[1]"); |
886 | $label->set_tooltip ($match->[1]); |
974 | $label->set_tooltip ("$match->[0][1]"); |
887 | } else { |
975 | } else { |
888 | $label->set_text ("..."); |
976 | $label->set_text ("..."); |
889 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
977 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
890 | last; |
978 | last; |
891 | } |
979 | } |
… | |
… | |
901 | |
989 | |
902 | sub _draw { |
990 | sub _draw { |
903 | my ($self) = @_; |
991 | my ($self) = @_; |
904 | |
992 | |
905 | # hack |
993 | # hack |
906 | local $CFPlus::UI::FOCUS = $self->{entry}; |
994 | local $DC::UI::FOCUS = $self->{entry}; |
907 | |
995 | |
908 | $self->SUPER::_draw; |
996 | $self->SUPER::_draw; |
909 | } |
997 | } |
910 | |
998 | |
911 | 1 |
999 | 1 |