package Blacknote::Input; use strict; use warnings FATAL => qw(all); use Blacknote::Logging; use Blacknote::System::State qw( bnsm_get_state STATE_GAME STATE_MENU bnsm_get_focused_window bnsm_get_player ); use List::Util qw(any); use Curses; use Carp qw(croak confess); use Data::Dumper; use constant KEY_ESC => -1; keypad(0); sub process_input { # During autoturn, we don't process input my $player = bnsm_get_player; if($player->autoturn){ return 1; # Continue } my $ch = getch; my $code = undef; if($ch eq chr(27)){ nodelay(1); $ch = getch; $ch = getch; if($ch eq KEY_ESC){ $code = KEY_ESC; }else{ # NOTE: This table should be a global constant somewhere # since we are mapping input codes to arrow keys and other escape sequences my $table = { B => KEY_DOWN, C => KEY_RIGHT, D => KEY_LEFT, A => KEY_UP, }; $code = $table->{$ch}; $code //= ord($ch); } nodelay(0); }else{ $code = ord($ch); } DEBUG "Keyname for ".$code.": ".keyname($code); my $state = bnsm_get_state; if($state == STATE_GAME){ # In GAME state, we control the player if(exists $Blacknote::System::State::KEYMAP{Rouge}->{keyname($code)}){ my $action_name = $Blacknote::System::State::KEYMAP{Rouge}->{keyname($code)}; DEBUG "Executing binding `$action_name'"; $Blacknote::System::State::KEYBINDING->exec_binding($action_name); } return 1; }elsif($state == STATE_MENU){ my $win = bnsm_get_focused_window; DEBUG "Pressed key: " . $code . " during MENU state"; if(any {keyname($code) eq $_} qw(a b c d e f g h i j k l m n o p r s t u v w x y z)){ $win->keyboard_selection(keyname($code)); }elsif($code == KEY_ESC){ $win->close; }elsif($code == 10){ # Enter $win->keyboard_selection("ENTER"); } return 1; } } 1; .