… | |
… | |
11 | use DC::Macro; |
11 | use DC::Macro; |
12 | |
12 | |
13 | our @ISA = DC::UI::Base::; |
13 | our @ISA = DC::UI::Base::; |
14 | |
14 | |
15 | my $magicmap_tex = |
15 | my $magicmap_tex = |
16 | new_from_file DC::Texture DC::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 | |
… | |
… | |
163 | sub invoke_button_down { |
163 | sub invoke_button_down { |
164 | my ($self, $ev, $x, $y) = @_; |
164 | my ($self, $ev, $x, $y) = @_; |
165 | |
165 | |
166 | if ($ev->{button} == 1) { |
166 | if ($ev->{button} == 1) { |
167 | $self->grab_focus; |
167 | $self->grab_focus; |
168 | return unless $::CONN; |
168 | return unless $::CONN && $self->{ctilesize}; |
169 | |
169 | |
170 | my $x = $self->{dx} + DC::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize}; |
170 | my $x = $self->{dx} + DC::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize}; |
171 | my $y = $self->{dy} + DC::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize}; |
171 | my $y = $self->{dy} + DC::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize}; |
172 | |
172 | |
173 | $x -= DC::floor $::MAP->w * 0.5; |
173 | $x -= DC::floor $::MAP->w * 0.5; |
… | |
… | |
201 | ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }], |
201 | ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }], |
202 | ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }], |
202 | ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }], |
203 | ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }], |
203 | ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }], |
204 | ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }], |
204 | ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }], |
205 | # ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }], |
205 | # ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }], |
206 | [ |
|
|
207 | $::PICKUP_ENABLE->{state} |
|
|
208 | ? "Disable automatic pickup" |
|
|
209 | : "Enable automatic pickup", |
|
|
210 | sub { $::PICKUP_ENABLE->toggle } |
|
|
211 | ], |
|
|
212 | ); |
206 | ); |
213 | |
207 | |
214 | if ($::CONN && $::CONN->{editor_support}) { |
208 | if ($::CONN && $::CONN->{editor_support}) { |
215 | # push @items, [ |
209 | # push @items, [ |
216 | # "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", |
210 | # "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", |
… | |
… | |
285 | $self->{need_update} = 1; |
279 | $self->{need_update} = 1; |
286 | $self->SUPER::update; |
280 | $self->SUPER::update; |
287 | } |
281 | } |
288 | |
282 | |
289 | my %DIR = ( |
283 | my %DIR = ( |
290 | ( "," . DC::SDLK_KP5 ), [0, "stay fire"], |
284 | ( "," . DC::SDLK_KP5 ), [0, "stay fire"], |
291 | ( "," . DC::SDLK_KP8 ), [1, "north"], |
285 | ( "," . DC::SDLK_KP8 ), [1, "north"], |
292 | ( "," . DC::SDLK_KP9 ), [2, "northeast"], |
286 | ( "," . DC::SDLK_KP9 ), [2, "northeast"], |
293 | ( "," . DC::SDLK_KP6 ), [3, "east"], |
287 | ( "," . DC::SDLK_KP6 ), [3, "east"], |
294 | ( "," . DC::SDLK_KP3 ), [4, "southeast"], |
288 | ( "," . DC::SDLK_KP3 ), [4, "southeast"], |
295 | ( "," . DC::SDLK_KP2 ), [5, "south"], |
289 | ( "," . DC::SDLK_KP2 ), [5, "south"], |
296 | ( "," . DC::SDLK_KP1 ), [6, "southwest"], |
290 | ( "," . DC::SDLK_KP1 ), [6, "southwest"], |
297 | ( "," . DC::SDLK_KP4 ), [7, "west"], |
291 | ( "," . DC::SDLK_KP4 ), [7, "west"], |
298 | ( "," . DC::SDLK_KP7 ), [8, "northwest"], |
292 | ( "," . DC::SDLK_KP7 ), [8, "northwest"], |
299 | |
293 | |
|
|
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 | |
300 | ( "," . DC::SDLK_UP ), [1, "north"], |
299 | ( "," . DC::SDLK_UP ), [1, "north"], |
301 | ("1," . DC::SDLK_UP ), [2, "northeast"], |
300 | ("1," . DC::SDLK_UP ), [2, "northeast"], |
302 | ( "," . DC::SDLK_RIGHT), [3, "east"], |
301 | ( "," . DC::SDLK_RIGHT ), [3, "east"], |
303 | ("1," . DC::SDLK_RIGHT), [4, "southeast"], |
302 | ("1," . DC::SDLK_RIGHT ), [4, "southeast"], |
304 | ( "," . DC::SDLK_DOWN ), [5, "south"], |
303 | ( "," . DC::SDLK_DOWN ), [5, "south"], |
305 | ("1," . DC::SDLK_DOWN ), [6, "southwest"], |
304 | ("1," . DC::SDLK_DOWN ), [6, "southwest"], |
306 | ( "," . DC::SDLK_LEFT ), [7, "west"], |
305 | ( "," . DC::SDLK_LEFT ), [7, "west"], |
307 | ("1," . DC::SDLK_LEFT ), [8, "northwest"], |
306 | ("1," . DC::SDLK_LEFT ), [8, "northwest"], |
308 | ); |
307 | ); |
309 | |
308 | |
310 | sub invoke_key_down { |
309 | sub invoke_key_down { |
311 | my ($self, $ev) = @_; |
310 | my ($self, $ev) = @_; |
312 | |
311 | |
313 | my $mod = $ev->{mod}; |
312 | my $mod = $ev->{mod}; |
314 | my $sym = $ev->{sym}; |
313 | my $sym = $ev->{sym}; |
315 | my $uni = $ev->{unicode}; |
314 | my $uni = $ev->{unicode}; |
316 | |
315 | |
317 | $mod &= DC::KMOD_CTRL | DC::KMOD_ALT | DC::KMOD_SHIFT; |
316 | $mod &= DC::KMOD_CTRL | DC::KMOD_ALT | DC::KMOD_META | DC::KMOD_SHIFT; |
318 | |
317 | |
319 | # ignore repeated keypresses |
318 | # ignore repeated keypresses |
320 | return if $self->{last_mod} == $mod && $self->{last_sym} == $sym; |
319 | return if $self->{last_mod} == $mod && $self->{last_sym} == $sym; |
321 | $self->{last_mod} = $mod; |
320 | $self->{last_mod} = $mod; |
322 | $self->{last_sym} = $sym; |
321 | $self->{last_sym} = $sym; |
323 | |
322 | |
324 | if ($::CONN && (my $dir = $DIR{(!!($mod & DC::KMOD_ALT)) . ",$sym"})) { |
323 | my $dir = $DIR{ (!!($mod & (DC::KMOD_ALT | DC::KMOD_META))) . ",$sym" }; |
|
|
324 | |
|
|
325 | if ($::CONN && $dir) { |
325 | if ($mod & DC::KMOD_SHIFT) { |
326 | if ($mod & DC::KMOD_SHIFT) { |
326 | $self->{shft}++; |
327 | $self->{shft}++; |
327 | if ($dir->[0] != $self->{fire_dir}) { |
328 | if ($dir->[0] != $self->{fire_dir}) { |
328 | $::CONN->user_send ("fire $dir->[0]"); |
329 | $::CONN->user_send ("fire $dir->[0]"); |
329 | } |
330 | } |
… | |
… | |
356 | $::CONN->user_send ("fire_stop"); |
357 | $::CONN->user_send ("fire_stop"); |
357 | delete $self->{fire_dir}; |
358 | delete $self->{fire_dir}; |
358 | $res = 1; |
359 | $res = 1; |
359 | } |
360 | } |
360 | } else { |
361 | } else { |
361 | if (exists $DIR{(!!($mod & DC::KMOD_ALT)) . ",$sym"} && delete $self->{shft}) { |
362 | my $dir = $DIR{ (!!($mod & (DC::KMOD_ALT | DC::KMOD_META))) . ",$sym" }; |
|
|
363 | |
|
|
364 | if ($dir && delete $self->{shft}) { |
362 | $::CONN->user_send ("fire_stop"); |
365 | $::CONN->user_send ("fire_stop"); |
363 | delete $self->{fire_dir}; |
366 | delete $self->{fire_dir}; |
364 | $res = 1; |
367 | $res = 1; |
365 | } elsif (($sym == DC::SDLK_LSHIFT || $sym == DC::SDLK_RSHIFT) |
368 | } elsif (($sym == DC::SDLK_LSHIFT || $sym == DC::SDLK_RSHIFT) |
366 | && delete $self->{shft}) { # XXX: is RSHIFT ok? |
369 | && delete $self->{shft}) { # XXX: is RSHIFT ok? |
… | |
… | |
368 | delete $self->{fire_dir}; |
371 | delete $self->{fire_dir}; |
369 | $res = 1; |
372 | $res = 1; |
370 | } |
373 | } |
371 | } |
374 | } |
372 | |
375 | |
373 | if (!($mod & DC::KMOD_CTRL ) && delete $self->{ctrl}) { |
376 | if (!($mod & DC::KMOD_CTRL) && delete $self->{ctrl}) { |
374 | $::CONN->user_send ("run_stop"); |
377 | $::CONN->user_send ("run_stop"); |
375 | $res = 1; |
378 | $res = 1; |
376 | } |
379 | } |
377 | |
380 | |
378 | $res |
381 | $res |
… | |
… | |
394 | |
397 | |
395 | sub scroll { |
398 | sub scroll { |
396 | my ($self, $dx, $dy) = @_; |
399 | my ($self, $dx, $dy) = @_; |
397 | |
400 | |
398 | $::MAP->scroll ($dx, $dy); |
401 | $::MAP->scroll ($dx, $dy); |
|
|
402 | |
|
|
403 | $self->{last_fow_texture} = $self->{fow_texture}; |
|
|
404 | $self->{ldx} = $self->{sdx} + $dx * $self->{tilesize}; # smooth displacement |
|
|
405 | $self->{ldy} = $self->{sdy} + $dy * $self->{tilesize}; |
399 | |
406 | |
400 | $self->movement_update; |
407 | $self->movement_update; |
401 | |
408 | |
402 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
409 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
403 | $self->{sdy} += $dy * $self->{tilesize}; |
410 | $self->{sdy} += $dy * $self->{tilesize}; |
… | |
… | |
421 | if ($self->{sdx} || $self->{sdy}) { |
428 | if ($self->{sdx} || $self->{sdy}) { |
422 | my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); |
429 | my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); |
423 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
430 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
424 | |
431 | |
425 | # the minimum time for a single tile movement |
432 | # the minimum time for a single tile movement |
426 | my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK); |
433 | my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1); |
|
|
434 | |
|
|
435 | $spd *= $self->{tilesize}; |
427 | |
436 | |
428 | # jump if "impossibly high" speed |
437 | # jump if "impossibly high" speed |
429 | if ( |
438 | if ( |
430 | (max abs $self->{sdx}, abs $self->{sdy}) |
439 | (max abs $self->{sdx}, abs $self->{sdy}) |
431 | > $spd * $self->{tilesize} * $mintime * 1.1 |
440 | > $spd * $mintime * 2.1 |
432 | ) { |
441 | ) { |
433 | #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 1.0;#d# |
442 | #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 2.1;#d# |
434 | $self->{sdx} = $self->{sdy} = 0; |
443 | $self->{sdx} = $self->{sdy} = 0; |
435 | } else { |
444 | } else { |
436 | my $upd = 0; |
445 | $spd *= $diff * 1.0001; # 1.0001 so that we don't accumulate rounding errors the wrong direction |
437 | |
|
|
438 | $spd *= $self->{tilesize} * $diff * 1.0001; |
|
|
439 | |
446 | |
440 | my $dx = $self->{sdx} < 0 ? -$spd : $spd; |
447 | my $dx = $self->{sdx} < 0 ? -$spd : $spd; |
441 | my $dy = $self->{sdy} < 0 ? -$spd : $spd; |
448 | my $dy = $self->{sdy} < 0 ? -$spd : $spd; |
442 | |
449 | |
443 | if ($self->{sdx} * ($self->{sdx} - $dx) <= 0) { $self->{sdx} = 0 } else { $self->{sdx} -= $dx; $upd = 1 } |
450 | if ($self->{sdx} * ($self->{sdx} - $dx) <= 0) { $self->{sdx} = 0 } else { $self->{sdx} -= $dx } |
444 | if ($self->{sdy} * ($self->{sdy} - $dy) <= 0) { $self->{sdy} = 0 } else { $self->{sdy} -= $dy; $upd = 1 } |
451 | if ($self->{sdy} * ($self->{sdy} - $dy) <= 0) { $self->{sdy} = 0 } else { $self->{sdy} -= $dy } |
445 | |
|
|
446 | $self->update if $upd; |
|
|
447 | } |
452 | } |
|
|
453 | |
|
|
454 | $self->update; |
448 | } |
455 | } |
449 | } else { |
456 | } else { |
450 | $self->{sdx} = $self->{sdy} = 0; |
457 | $self->{sdx} = $self->{sdy} = 0; |
451 | } |
458 | } |
452 | |
459 | |
… | |
… | |
460 | if (delete $self->{need_update}) { |
467 | if (delete $self->{need_update}) { |
461 | $self->movement_update; |
468 | $self->movement_update; |
462 | |
469 | |
463 | my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1; |
470 | my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1; |
464 | |
471 | |
|
|
472 | my $sdx_t = DC::ceil $self->{sdx} / $tilesize; |
|
|
473 | my $sdy_t = DC::ceil $self->{sdy} / $tilesize; |
|
|
474 | |
465 | # width/height of map, in tiles |
475 | # width/height of map, in tiles |
466 | my $sw = $self->{sw} = 1 + DC::ceil $self->{w} / $tilesize; |
476 | my $sw = $self->{sw} = 2 + DC::ceil $self->{w} / $tilesize; |
467 | my $sh = $self->{sh} = 1 + DC::ceil $self->{h} / $tilesize; |
477 | my $sh = $self->{sh} = 2 + DC::ceil $self->{h} / $tilesize; |
468 | |
478 | |
469 | # the map displacement, in tiles |
479 | # the map displacement, in tiles |
470 | my $sx = DC::ceil $::CFG->{map_shift_x} / $tilesize + $self->{sdx} / $self->{tilesize}; |
480 | my $sx = DC::ceil $::CFG->{map_shift_x} / $tilesize + $sdx_t; |
471 | my $sy = DC::ceil $::CFG->{map_shift_y} / $tilesize + $self->{sdy} / $self->{tilesize}; |
481 | my $sy = DC::ceil $::CFG->{map_shift_y} / $tilesize + $sdy_t; |
472 | |
482 | |
473 | # the upper left "visible" corner, in pixels |
483 | # the upper left "visible" corner, in pixels |
474 | my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize; |
484 | my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize; |
475 | my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize; |
485 | my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize; |
476 | |
486 | |
477 | my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx; |
487 | my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx; |
478 | my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy; |
488 | my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy; |
479 | |
489 | |
480 | if ($::CFG->{fow_enable}) { |
490 | if ($::CFG->{fow_enable}) { |
|
|
491 | my ($sdx_t, $sdy_t);#d# do not anchor at player |
|
|
492 | |
481 | my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh); |
493 | my ($w, $h, $data) = $::MAP->fow_texture ( |
|
|
494 | $dx + (min 0, $sdx_t), |
|
|
495 | $dy + (min 0, $sdy_t), |
|
|
496 | $sw + abs $sdx_t, |
|
|
497 | $sh + abs $sdy_t |
|
|
498 | ); |
482 | |
499 | |
483 | $self->{fow_texture} = new DC::Texture |
500 | $self->{fow_texture} = new DC::Texture |
484 | w => $w, |
501 | w => $w, |
485 | h => $h, |
502 | h => $h, |
486 | data => $data, |
503 | data => $data, |
… | |
… | |
491 | } |
508 | } |
492 | |
509 | |
493 | glNewList ($self->{list} ||= glGenList); |
510 | glNewList ($self->{list} ||= glGenList); |
494 | |
511 | |
495 | glPushMatrix; |
512 | glPushMatrix; |
496 | glTranslate $sx0 + $self->{sdx}, $sy0 + $self->{sdy}; |
513 | glTranslate $sx0, $sy0; |
497 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
514 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
|
|
515 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
498 | |
516 | |
499 | $::MAP->draw ($dx, $dy, $sw, $sh, |
517 | $::MAP->draw ($dx, $dy, $sw, $sh, |
500 | $self->{tilesize}, |
518 | $self->{tilesize}, |
501 | $::CONN->{player}{tag}, |
519 | $::CONN->{player}{tag}, |
502 | -$self->{sdx}, -$self->{sdy}); |
520 | -$self->{sdx}, -$self->{sdy}); |
503 | |
521 | |
|
|
522 | #d#glTranslate -$self->{sdx}, -$self->{sdy}; # anchor fow at player |
504 | glScale $self->{tilesize}, $self->{tilesize}; |
523 | glScale $self->{tilesize}, $self->{tilesize}; |
505 | |
524 | |
506 | if (my $tex = $self->{fow_texture}) { |
525 | if (my $tex = $self->{fow_texture}) { |
507 | glPushMatrix; |
|
|
508 | glScale 1/3, 1/3; |
|
|
509 | glEnable GL_TEXTURE_2D; |
526 | glEnable GL_TEXTURE_2D; |
510 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
527 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
511 | |
528 | |
|
|
529 | my $a1 = 0.9; |
|
|
530 | |
|
|
531 | if (0 && $self->{last_fow_texture}) { |
|
|
532 | my $d1 = ($self->{sdx} ** 2 + $self->{sdy} ** 2) ** 0.5; |
|
|
533 | my $d2 = ($self->{ldx} ** 2 + $self->{ldy} ** 2) ** 0.5; |
|
|
534 | |
|
|
535 | if ($d1 * $d2) { |
|
|
536 | my $a2 = $d1 / $d2; |
|
|
537 | |
|
|
538 | $a1 = (1/$a2); |
|
|
539 | |
|
|
540 | #$a1 *= 0.9; |
|
|
541 | #$a2 *= 0.9; |
|
|
542 | |
|
|
543 | printf "%4g %4g %4g\n", $d1, $d2, $a1+$a2;#d# |
|
|
544 | |
|
|
545 | #$a1 = 1 - (1-$a1) * 0.707; |
|
|
546 | #$a2 = 1 - (1-$a2) * 0.707; |
|
|
547 | |
|
|
548 | glColor 1,0,0,1; |
|
|
549 | glBegin GL_QUADS; |
|
|
550 | glVertex 10 + $sdx_t, 10; |
|
|
551 | glVertex 11 + $sdx_t, 10; |
|
|
552 | glVertex 11 + $sdx_t, 11; |
|
|
553 | glVertex 10 + $sdx_t, 11; |
|
|
554 | glEnd; |
|
|
555 | |
|
|
556 | glColor +($::CFG->{fow_intensity}) x 3, $a2; |
|
|
557 | glPushMatrix; |
|
|
558 | glTranslate $sdx_t, $sdy_t; # anchor at player |
|
|
559 | glTranslate $self->{ldx} / -$tilesize, $self->{ldy} / -$tilesize; |
|
|
560 | glScale 1/3, 1/3; |
|
|
561 | $self->{last_fow_texture}->draw_quad_alpha (0, 0); |
|
|
562 | glPopMatrix; |
|
|
563 | } else { |
|
|
564 | delete $self->{last_fow_texture}; |
|
|
565 | } |
|
|
566 | } |
|
|
567 | |
512 | glColor +($::CFG->{fow_intensity}) x 3, 0.9; |
568 | glColor +($::CFG->{fow_intensity}) x 3, $a1; |
|
|
569 | glPushMatrix; |
|
|
570 | #d#glTranslate +(min 0, $sdx_t), (min 0, $sdy_t); # anchor at player |
|
|
571 | glScale 1/3, 1/3; |
513 | $self->{fow_texture}->draw_quad_alpha (0, 0); |
572 | $self->{fow_texture}->draw_quad_alpha (0, 0); |
|
|
573 | glPopMatrix; |
514 | |
574 | |
515 | glDisable GL_TEXTURE_2D; |
575 | glDisable GL_TEXTURE_2D; |
516 | glPopMatrix; |
|
|
517 | } |
576 | } |
518 | |
577 | |
519 | if ($self->{magicmap}) { |
578 | if ($self->{magicmap}) { |
520 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
579 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
521 | |
580 | |
… | |
… | |
591 | sub refresh_hook { |
650 | sub refresh_hook { |
592 | my ($self) = @_; |
651 | my ($self) = @_; |
593 | |
652 | |
594 | if ($::MAP && $self->{texture_atime} < time) { |
653 | if ($::MAP && $self->{texture_atime} < time) { |
595 | my ($w, $h) = @$self{qw(w h)}; |
654 | my ($w, $h) = @$self{qw(w h)}; |
|
|
655 | |
|
|
656 | return unless $w && $h; |
596 | |
657 | |
597 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
658 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
598 | my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
659 | my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
599 | |
660 | |
600 | my $ox = 0.5 * ($w - $sw); |
661 | my $ox = 0.5 * ($w - $sw); |
… | |
… | |
829 | my $text = $self->{entry}->get_text; |
890 | my $text = $self->{entry}->get_text; |
830 | |
891 | |
831 | length $text |
892 | length $text |
832 | or return $self->hide; |
893 | or return $self->hide; |
833 | |
894 | |
834 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
|
|
835 | |
|
|
836 | if ($text ne $self->{last_search}) { |
895 | if ($text ne $self->{last_search}) { |
837 | my @match; |
896 | my @match; |
838 | |
897 | |
839 | if ($text =~ /^(.*?)\s+$/) { |
898 | if ($text =~ /^(.*?)\s+$/) { |
|
|
899 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
840 | @match = [$cmd, "(appended whitespace suppresses completion)"]; |
900 | @match = ([[$cmd,'(appended whitespace suppresses completion)'],$text]); |
841 | } else { |
901 | } else { |
|
|
902 | # @match is [command, penalty, command with arguments] until sort |
|
|
903 | |
|
|
904 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
|
|
905 | |
842 | my $regexp = do { |
906 | my $regexp_abbrev = do { |
843 | my ($beg, @chr) = split //, lc $cmd; |
907 | my ($beg, @chr) = split //, lc $cmd; |
844 | |
908 | |
845 | # the following regex is used to match our "completion entry" |
909 | # the following regex is used to match our "completion entry" |
846 | # to an actual command - the parentheses match kind of "overhead" |
910 | # to an actual command - the parentheses match kind of "overhead" |
847 | # - the more characters the parentheses match, the less attractive |
911 | # - the more characters the parentheses match, the less attractive |
… | |
… | |
849 | my $regexp = "^\Q$beg\E" |
913 | my $regexp = "^\Q$beg\E" |
850 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
914 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
851 | qr<$regexp> |
915 | qr<$regexp> |
852 | }; |
916 | }; |
853 | |
917 | |
854 | my @penalty; |
918 | my $regexp_partial = do { |
|
|
919 | my $regexp = "^\Q$text\E(.*)"; |
|
|
920 | qr<$regexp> |
|
|
921 | }; |
855 | |
922 | |
856 | for (keys %{$self->{command}}) { |
923 | for (keys %{$self->{command}}) { |
857 | if (@penalty = $_ =~ $regexp) { |
924 | my @scores; |
858 | push @match, [$_, length join "", map "::$_", grep defined, @penalty]; |
925 | |
|
|
926 | # 1. Complete command [with args] |
|
|
927 | # command is a prefix of the text |
|
|
928 | # score is length of complete command matched |
|
|
929 | # e.g. "invoke summon pet monster bat" |
|
|
930 | # "invoke" "summon pet monster bat" = 6 |
|
|
931 | # "invoke summon pet monster" "bat" = 25 |
|
|
932 | if ($text =~ /^\Q$_\E(.*)/) { |
|
|
933 | push @scores, [$_, length $_, $text]; |
859 | } |
934 | } |
|
|
935 | |
|
|
936 | # 2. Partial command |
|
|
937 | # text is a prefix of the full command |
|
|
938 | # score is the length of the input text |
|
|
939 | # e.g. "invoke s" |
|
|
940 | # "invoke small fireball" = 8 |
|
|
941 | # "invoke summon pet monster" = 8 |
|
|
942 | |
|
|
943 | if ($_ =~ $regexp_partial) { |
|
|
944 | push @scores, [$_, length $text, $_]; |
|
|
945 | } |
|
|
946 | |
|
|
947 | # 3. Abbreviation match |
|
|
948 | # attempts to use first word of text as an abbreviated command |
|
|
949 | # score is length of word + 1 - 3 per non-word-initial character |
|
|
950 | |
|
|
951 | if (my @penalty = $_ =~ $regexp_abbrev) { |
|
|
952 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
|
|
953 | } |
|
|
954 | |
|
|
955 | # Pick the best option for this command |
|
|
956 | push @match, (sort { |
|
|
957 | $b->[1] <=> $a->[1] |
|
|
958 | } @scores)[0]; |
860 | } |
959 | } |
861 | |
960 | |
|
|
961 | # @match is now [command object, command with arguments] |
862 | @match = map $self->{command}{$_->[0]}, |
962 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
863 | sort { |
963 | sort { |
864 | $a->[1] <=> $b->[1] |
964 | $b->[1] <=> $a->[1] |
865 | or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] |
965 | or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] |
866 | or (length $b->[0]) <=> (length $a->[0]) |
966 | or (length $b->[0]) <=> (length $a->[0]) |
867 | } @match; |
967 | } @match; |
868 | } |
968 | } |
869 | |
969 | |
… | |
… | |
888 | $label->{fg} = [1, 1, 1, 1]; |
988 | $label->{fg} = [1, 1, 1, 1]; |
889 | $label->{bg} = [0, 0, 0, 0]; |
989 | $label->{bg} = [0, 0, 0, 0]; |
890 | } |
990 | } |
891 | |
991 | |
892 | if (@matches) { |
992 | if (@matches) { |
893 | $self->{select} = "$matches[0][0]$arg"; |
993 | $self->{select} = "$matches[0][1]"; |
894 | |
994 | |
895 | $labels[0]->{fg} = [0, 0, 0, 1]; |
995 | $labels[0]->{fg} = [0, 0, 0, 1]; |
896 | $labels[0]->{bg} = [1, 1, 1, 0.8]; |
996 | $labels[0]->{bg} = [1, 1, 1, 0.8]; |
897 | } else { |
997 | } else { |
898 | $self->{select} = "$cmd$arg"; |
998 | $self->{select} = "$text"; |
899 | } |
999 | } |
900 | |
1000 | |
901 | for my $match (@matches) { |
1001 | for my $match (@matches) { |
902 | my $label = shift @labels; |
1002 | my $label = shift @labels; |
903 | |
1003 | |
904 | if (@labels) { |
1004 | if (@labels) { |
905 | $label->set_text ("$match->[0]$arg"); |
1005 | $label->set_text ("$match->[1]"); |
906 | $label->set_tooltip ($match->[1]); |
1006 | $label->set_tooltip ("$match->[0][1]"); |
907 | } else { |
1007 | } else { |
908 | $label->set_text ("..."); |
1008 | $label->set_text ("..."); |
909 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
1009 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
910 | last; |
1010 | last; |
911 | } |
1011 | } |