any press

This commit is contained in:
Frank DeMarco 2013-11-14 14:30:32 +09:00
parent fd196fe2f9
commit 703dbd906d
1 changed files with 66 additions and 29 deletions

View File

@ -14,6 +14,7 @@ class Input(GameChild):
self.joystick = Joystick()
self.delegate = self.get_delegate()
self.load_configuration()
self.set_any_press_ignore_list()
self.unsuppress()
self.subscribe_to_events()
self.build_key_map()
@ -25,20 +26,23 @@ class Input(GameChild):
self.double_click_time_limit = self.get_configuration(
"mouse", "double-click-time-limit")
def subscribe_to_events(self):
self.subscribe(self.translate_key_press, KEYDOWN)
self.subscribe(self.translate_key_release, KEYUP)
self.subscribe(self.translate_joy_press, JOYBUTTONDOWN)
self.subscribe(self.translate_axis_motion, JOYAXISMOTION)
self.subscribe(self.translate_mouse_input, MOUSEBUTTONDOWN)
self.subscribe(self.translate_mouse_input, MOUSEBUTTONUP)
def suppress(self):
self.suppressed = True
def set_any_press_ignore_list(self):
self.any_press_ignored = set(["capture-screen", "toggle-fullscreen",
"reset-game", "record-video", "quit"])
self.any_press_ignored_keys = set()
def unsuppress(self):
self.suppressed = False
def subscribe_to_events(self):
self.subscribe(self.translate_key, KEYDOWN)
self.subscribe(self.translate_key, KEYUP)
self.subscribe(self.translate_joy_button, JOYBUTTONDOWN)
self.subscribe(self.translate_joy_button, JOYBUTTONUP)
self.subscribe(self.translate_axis_motion, JOYAXISMOTION)
self.subscribe(self.translate_mouse_input, MOUSEBUTTONDOWN)
self.subscribe(self.translate_mouse_input, MOUSEBUTTONUP)
def build_key_map(self):
key_map = {}
for command, keys in self.key_commands:
@ -52,28 +56,38 @@ class Input(GameChild):
def build_joy_button_map(self):
self.joy_button_map = self.get_configuration("joy")
def translate_key_press(self, event):
def suppress(self):
self.suppressed = True
def translate_key(self, event):
if not self.suppressed:
cancel = event.type == KEYUP
posted = None
key = event.key
for cmd, keys in self.key_map.iteritems():
if key in keys:
self.post_command(cmd)
self.post_command(cmd, cancel=cancel)
posted = cmd
if (not posted or posted not in self.any_press_ignored) and \
key not in self.any_press_ignored_keys:
self.post_any_command(key, cancel)
def post_command(self, cmd, **attributes):
self.delegate.post(cmd, **attributes)
def translate_key_release(self, event):
if not self.suppressed:
key = event.key
for cmd, keys in self.key_map.iteritems():
if key in keys:
self.post_command(cmd, cancel=True)
def post_any_command(self, id, cancel=False):
self.post_command("any", id=id, cancel=cancel)
def translate_joy_press(self, event):
def translate_joy_button(self, event):
if not self.suppressed:
cancel = event.type == JOYBUTTONUP
posted = None
for command, button in self.joy_button_map.iteritems():
if button == event.button:
self.post_command(command)
self.post_command(command, cancel=cancel)
posted = command
if not posted or posted not in self.any_press_ignored:
self.post_any_command(event.button, cancel)
def translate_axis_motion(self, event):
if not self.suppressed:
@ -82,16 +96,22 @@ class Input(GameChild):
if not value:
for command in "up", "right", "down", "left":
self.post_command(command, cancel=True)
if axis == 1:
if value < 0:
self.post_command("up")
elif value > 0:
self.post_command("down")
if command not in self.any_press_ignored:
self.post_any_command(command, True)
else:
if value > 0:
self.post_command("right")
elif value < 0:
self.post_command("left")
if axis == 1:
if value < 0:
command = "up"
elif value > 0:
command = "down"
else:
if value > 0:
command = "right"
elif value < 0:
command = "left"
self.post_command(command)
if command not in self.any_press_ignored:
self.post_any_command(command)
def is_command_active(self, command):
if not self.suppressed:
@ -136,6 +156,23 @@ class Input(GameChild):
axes[direction] = self.is_command_active(direction)
return axes
def register_any_press_ignore(self, *args, **attributes):
self.any_press_ignored.update(args)
self.any_press_ignored_keys.update(self.extract_keys(attributes))
def extract_keys(self, attributes):
keys = []
if "keys" in attributes:
keys = attributes["keys"]
if type(keys) == int:
keys = [keys]
return keys
def unregister_any_press_ignore(self, *args, **attributes):
self.any_press_ignored.difference_update(args)
self.any_press_ignored_keys.difference_update(
self.extract_keys(attributes))
class Joystick: