roc/inputs.py
2011-04-24 20:46:22 -06:00

77 lines
1.3 KiB
Python
Executable file

from pygame.locals import *
import video
state = {}
commands = {}
bindings = {
K_LEFT: '+left',
K_RIGHT: '+right',
K_UP: '+up',
K_DOWN: '+down',
K_RETURN: 'select',
K_COMMA: '+leanleft',
K_PERIOD: '+leanright',
K_q: 'exit',
K_d: 'debug',
K_TAB: '+accel',
K_BACKSPACE: '+brake',
}
aliases = {}
keys = {}
def keyup(key):
global commands, keys, bindings, aliases
if not key in keys:
return
del keys[key]
if key in bindings:
bind = bindings[key]
if bind in commands:
del commands[bind]
if bind and bind[1:] in state:
del state[bind[1:]]
def keydown(key):
global commands, keys, bindings, aliases
keys[key] = 0
if key in bindings:
bind = bindings[key]
if bind in aliases:
"execute alias TODO"
elif bind and bind[0] == '+':
state[bind[1:]] = 0
add_command(bind[1:])
elif bind:
add_command(bind)
def add_command(cmd):
global commands
if not cmd in commands:
commands[cmd] = 1
return
else:
commands[cmd] += 1
def received_command(cmd):
if cmd in commands:
if commands[cmd] > 0:
commands[cmd] -= 1
return True
return False
def received_command_multi(cmd):
if cmd in commands:
if commands[cmd] > 0:
commands[cmd] = 0
return True
return False
def next_frame():
global commands
if video.skipping_next_frame():
return
commands = {}