cancel flag

This commit is contained in:
Frank DeMarco 2013-04-03 00:46:14 +09:00
parent 47b3bc1feb
commit b586be3b3b
4 changed files with 57 additions and 87 deletions

View File

@ -71,6 +71,9 @@ class Configuration(RawConfigParser):
set_option(section, "icon-path", "")
set_option(section, "skip-frames", "no")
set_option(section, "fullscreen", "no")
section = "input"
add_section(section)
set_option(section, "release-suffix", "-release")
section = "sprite"
add_section(section)
set_option(section, "transparent-color", "magenta")
@ -101,6 +104,7 @@ class Configuration(RawConfigParser):
set_option(section, "user-event-id", "USEREVENT")
set_option(section, "command-id-offset", "1")
set_option(section, "command-key", "command")
set_option(section, "cancel-flag-key", "cancel")
section = "audio"
add_section(section)
set_option(section, "sfx-path", "aud/fx/")

View File

@ -8,14 +8,22 @@ class Delegate(GameChild):
def __init__(self, game):
GameChild.__init__(self, game)
self.subscribers = dict()
self.load_configuration()
self.disable()
def enable(self):
self.enabled = True
def disable(self):
self.enabled = False
def load_configuration(self):
config = self.get_configuration("event")
self.cancel_flag_key = config["cancel-flag-key"]
self.command_key = config["command-key"]
self.command_event_id = config["command-id-offset"] + \
globals()[config["user-event-id"]]
def enable(self):
self.enabled = True
def dispatch(self):
if self.enabled:
subscribers = self.subscribers
@ -31,52 +39,40 @@ class Delegate(GameChild):
def add_subscriber(self, callback, kind=None):
self.print_debug("Subscribing %s to %s" % (callback, kind))
if kind is None:
kind = self.get_command_event_id()
kind = self.command_event_id
subscribers = self.subscribers
if kind not in subscribers:
subscribers[kind] = list()
subscribers[kind].append(callback)
def get_command_event_id(self):
config = self.get_configuration("event")
return config["command-id-offset"] + globals()[config["user-event-id"]]
def is_command(self, event):
return event.type == self.command_event_id
def remove_subscriber(self, callback, kind=None):
if kind is None:
kind = self.get_command_event_id()
kind = self.command_event_id
self.subscribers[kind].remove(callback)
def compare(self, evt, commands=None, **attributes):
def compare(self, evt, commands=None, cancel=False, **attributes):
self.add_cancel_flag_to_attributes(attributes, cancel)
if commands is not None and not isinstance(commands, list):
commands = [commands]
if not attributes:
return self.command_in_list(evt, commands)
if commands is not None:
if not self.command_in_list(evt, commands):
return False
return all(key in evt.dict and evt.dict[key] == value for \
key, value in attributes.iteritems())
def add_cancel_flag_to_attributes(self, attributes, cancel):
attributes[self.cancel_flag_key] = cancel
def command_in_list(self, evt, commands):
return self.get_command_attribute(evt) in commands
def get_command_attribute(self, evt):
return evt.dict[self.get_command_key()]
return evt.dict[self.command_key]
def get_command_key(self):
return self.get_configuration("event", "command-key")
def build_command(self, command, **attributes):
return Event(self.get_command_event_id(),
self.add_command_to_attributes(command, attributes))
def add_command_to_attributes(self, command, attributes):
attributes[self.get_command_key()] = command
return attributes
def post(self, command=None, kind=None, **attributes):
if kind == None:
event = self.build_command(command, **attributes)
else:
event = Event(kind, attributes)
post(event)
def post(self, command=None, cancel=False, **attributes):
attributes[self.command_key] = command
self.add_cancel_flag_to_attributes(attributes, cancel)
post(Event(self.command_event_id, attributes))

View File

@ -74,32 +74,5 @@ class GameChild:
def subscribe(self, callback, kind=None):
self.get_game().delegate.add_subscriber(callback, kind)
def unsubscribe(self, callback, kind=None):
self.get_game().delegate.remove_subscriber(callback, kind)
# def get_command_event_id(self):
# config = self.get_configuration("event")
# return config["command-id-offset"] + globals()[config("user-event-id")]
# def is_command(self, evt, names, **attributes):
# if not isinstance(names, list):
# names = [names]
# return evt.type == self.get_custom_event_id() and \
# evt.dict[self.get_command_attribute()] in names and \
# self.event_matches(evt, attributes)
# def event_matches(self, evt, attributes):
# return all(key in evt.dict and evt.dict[key] == value for \
# key, value in attributes.iteritems())
# def get_command_attribute(self):
# return self.get_configuration("event", "command-attribute")
# def post_command(self, command, interval=None, **attributes):
# attributes[self.get_command_default_attribute()] = command
# evt = event.Event(self.get_command_event_id(), attributes)
# if interval is not None:
# time.set_timer(evt, interval)
# else:
# event.post(evt)

View File

@ -1,6 +1,6 @@
from time import time as get_secs
from pygame import event, joystick as joy, key as keyboard
from pygame import joystick as joy, key as keyboard
from pygame.locals import *
from GameChild import *
@ -11,13 +11,18 @@ class Input(GameChild):
GameChild.__init__(self, game)
self.last_mouse_down_left = None
self.joystick = Joystick()
self.load_configuration()
self.unsuppress()
self.subscribe_to_events()
self.build_key_map()
def load_configuration(self):
self.release_suffix = self.get_configuration("input", "release-suffix")
def subscribe_to_events(self):
self.subscribe(self.translate_key_press, KEYDOWN)
self.subscribe(self.translate_joy_button, JOYBUTTONDOWN)
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)
@ -38,11 +43,9 @@ class Input(GameChild):
key_map[command].append(globals()[key])
self.key_map = key_map
def translate_key_press(self, evt):
self.print_debug("You pressed %s, suppressed => %s" %\
(evt.key, self.suppressed))
def translate_key_press(self, event):
if not self.suppressed:
key = evt.key
key = event.key
for cmd, keys in self.key_map.iteritems():
if key in keys:
self.post_command(cmd)
@ -50,19 +53,29 @@ class Input(GameChild):
def post_command(self, cmd, **attributes):
self.get_delegate().post(cmd, **attributes)
def translate_joy_button(self, evt):
def translate_key_release(self, event):
if not self.suppressed:
button = evt.button
key = event.key
for cmd, keys in self.key_map.iteritems():
if key in keys:
self.post_command(cmd, cancel=True)
def translate_joy_press(self, event):
if not self.suppressed:
button = event.button
config = self.get_configuration().get_section("joy")
if button == config["advance"]:
self.post_command("advance")
if button == config["pause"]:
self.post_command("pause")
def translate_axis_motion(self, evt):
def translate_axis_motion(self, event):
if not self.suppressed:
axis = evt.axis
value = evt.value
axis = event.axis
value = event.value
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")
@ -94,11 +107,11 @@ class Input(GameChild):
if poll[key]:
return True
def translate_mouse_input(self, evt):
button = evt.button
pos = evt.pos
def translate_mouse_input(self, event):
button = event.button
pos = event.pos
post = self.post_command
if evt.type == MOUSEBUTTONDOWN:
if event.type == MOUSEBUTTONDOWN:
if button == 1:
last = self.last_mouse_down_left
if last:
@ -108,22 +121,6 @@ class Input(GameChild):
post("mouse-double-click-left", pos=pos)
last = get_secs()
self.last_mouse_down_left = last
post("mouse-down-left", pos=pos)
elif button == 2:
post("mouse-down-middle", pos=pos)
elif button == 3:
post("mouse-down-right", pos=pos)
elif button == 4:
post("mouse-scroll-up", pos=pos)
elif button == 5:
post("mouse-scroll-down", pos=pos)
elif evt.type == MOUSEBUTTONUP:
if button == 1:
post("mouse-up-left", pos=pos)
elif button == 2:
post("mouse-up-middle", pos=pos)
elif button == 3:
post("mouse-up-right", pos=pos)
def get_axes(self):
axes = {}