user event id defined in config
This commit is contained in:
parent
461ea716a6
commit
966700db92
|
@ -15,7 +15,7 @@ class Audio(GameChild):
|
|||
def __init__(self, game):
|
||||
GameChild.__init__(self, game)
|
||||
self.load_fx()
|
||||
self.subscribe_to(Input.command_event, self.mute)
|
||||
self.subscribe_to(self.get_user_event_id(), self.mute)
|
||||
|
||||
def load_fx(self):
|
||||
fx = {}
|
||||
|
@ -26,7 +26,7 @@ class Audio(GameChild):
|
|||
self.fx = fx
|
||||
|
||||
def mute(self, event):
|
||||
if event.command == "mute":
|
||||
if self.is_command(event, "mute"):
|
||||
self.muted = not self.muted
|
||||
self.set_volume()
|
||||
|
||||
|
|
|
@ -9,8 +9,6 @@ class EventDelegate(GameChild):
|
|||
GameChild.__init__(self, game)
|
||||
self.subscribers = dict()
|
||||
self.disable()
|
||||
if self.is_debug_mode():
|
||||
print "Event ID range: %i - %i" % (NOEVENT, NUMEVENTS)
|
||||
|
||||
def enable(self):
|
||||
self.enabled = True
|
||||
|
@ -25,15 +23,15 @@ class EventDelegate(GameChild):
|
|||
kind = evt.type
|
||||
if kind in subscribers:
|
||||
for subscriber in subscribers[kind]:
|
||||
if self.is_debug_mode():
|
||||
print "Passing %s to %s" % (evt, subscriber)
|
||||
self.print_debug("Passing {0} to {1}".\
|
||||
format(evt, subscriber))
|
||||
subscriber(evt)
|
||||
else:
|
||||
event.pump()
|
||||
|
||||
def add_subscriber(self, kind, callback):
|
||||
if self.is_debug_mode():
|
||||
print "Subscribing %s to %i" % (callback, kind)
|
||||
self.print_debug("Subscribing {0} to {1}".\
|
||||
format(callback, kind))
|
||||
subscribers = self.subscribers
|
||||
if kind not in subscribers:
|
||||
subscribers[kind] = list()
|
||||
|
|
|
@ -26,7 +26,7 @@ class Game(GameChild, Animation):
|
|||
pygame.init()
|
||||
self.set_children()
|
||||
self.subscribe_to(QUIT, self.end)
|
||||
self.subscribe_to(Input.command_event, self.end)
|
||||
self.subscribe_to(self.get_user_event_id(), self.end)
|
||||
self.clear_queue()
|
||||
self.delegate.enable()
|
||||
|
||||
|
@ -76,5 +76,5 @@ class Game(GameChild, Animation):
|
|||
pass
|
||||
|
||||
def end(self, evt):
|
||||
if evt.type == QUIT or evt.command == "quit":
|
||||
if evt.type == QUIT or self.is_command(evt, "quit"):
|
||||
self.stop()
|
||||
|
|
|
@ -2,6 +2,7 @@ from os.path import exists, join, basename, normpath, abspath
|
|||
from sys import argv
|
||||
|
||||
from pygame import mixer
|
||||
from pygame.locals import *
|
||||
|
||||
import Game
|
||||
|
||||
|
@ -40,10 +41,10 @@ class GameChild:
|
|||
path = join(root, rel_path)
|
||||
if (exists(path)):
|
||||
return path
|
||||
self.print_debug_statement("Couldn't find resource: {0}, {1}".\
|
||||
self.print_debug("Couldn't find resource: {0}, {1}".\
|
||||
format(section, option))
|
||||
|
||||
def print_debug_statement(self, statement):
|
||||
def print_debug(self, statement):
|
||||
if self.is_debug_mode():
|
||||
print statement
|
||||
|
||||
|
@ -61,3 +62,12 @@ class GameChild:
|
|||
|
||||
def is_debug_mode(self):
|
||||
return "-d" in argv
|
||||
|
||||
def get_user_event_id(self):
|
||||
return globals()[self.get_configuration().get("event",
|
||||
"user-event-id")]
|
||||
|
||||
def is_command(self, evt, cmd):
|
||||
name = self.get_configuration().get("event", "command-event-name")
|
||||
return evt.type == self.get_user_event_id() and evt.name == name and \
|
||||
evt.command == cmd
|
||||
|
|
|
@ -5,8 +5,6 @@ from GameChild import *
|
|||
|
||||
class Input(GameChild):
|
||||
|
||||
command_event = USEREVENT + 7
|
||||
|
||||
def __init__(self, game):
|
||||
GameChild.__init__(self, game)
|
||||
self.joystick = Joystick()
|
||||
|
@ -29,29 +27,31 @@ class Input(GameChild):
|
|||
key_map = {}
|
||||
for command, keys in self.get_configuration().items("keys"):
|
||||
key_map[command] = []
|
||||
if type(keys) == str:
|
||||
keys = [keys]
|
||||
for key in keys:
|
||||
key_map[command].append(globals()[key])
|
||||
self.key_map = key_map
|
||||
|
||||
def translate_key_press(self, evt):
|
||||
if self.is_debug_mode():
|
||||
print "You pressed %i, suppressed => %s" % (evt.key, self.suppressed)
|
||||
self.print_debug("You pressed {0}, suppressed => {1}".\
|
||||
format(evt.key, self.suppressed))
|
||||
if not self.suppressed:
|
||||
key = evt.key
|
||||
for command, keys in self.key_map.iteritems():
|
||||
for cmd, keys in self.key_map.iteritems():
|
||||
if key in keys:
|
||||
self.post_command(command)
|
||||
self.post_command(cmd)
|
||||
|
||||
def post_command(self, name):
|
||||
if self.is_debug_mode():
|
||||
print "Posting %s command with id %i" % (name, self.command_event)
|
||||
event.post(event.Event(self.command_event, command=name))
|
||||
def post_command(self, cmd):
|
||||
eid = self.get_user_event_id()
|
||||
self.print_debug("Posting {0} command with id {1}".format(cmd, eid))
|
||||
name = self.get_configuration().get("event", "command-event-name")
|
||||
event.post(event.Event(eid, name=name, command=cmd))
|
||||
|
||||
def translate_joy_button(self, evt):
|
||||
if not self.suppressed:
|
||||
button = evt.button
|
||||
config = self.get_configuration()
|
||||
code = self.command_event
|
||||
if button == config["joy-advance"]:
|
||||
self.post_command("advance")
|
||||
if button == config["joy-pause"]:
|
||||
|
@ -61,7 +61,6 @@ class Input(GameChild):
|
|||
if not self.suppressed:
|
||||
axis = evt.axis
|
||||
value = evt.value
|
||||
code = self.command_event
|
||||
if axis == 1:
|
||||
if value < 0:
|
||||
self.post_command("up")
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from os import mkdir
|
||||
from os import makedirs
|
||||
from os.path import exists, join
|
||||
from sys import exc_info
|
||||
from time import strftime
|
||||
|
@ -12,24 +12,24 @@ class ScreenGrabber(GameChild):
|
|||
|
||||
def __init__(self, game):
|
||||
GameChild.__init__(self, game)
|
||||
self.subscribe_to(Input.command_event, self.save_display)
|
||||
self.subscribe_to(self.get_user_event_id(), self.save_display)
|
||||
|
||||
def save_display(self, event):
|
||||
if event.command == "capture-screen":
|
||||
directory = self.get_resource("capture-path")
|
||||
if self.is_command(event, "capture-screen"):
|
||||
directory = self.get_configuration().get("screen-captures", "path")
|
||||
try:
|
||||
if not exists(directory):
|
||||
mkdir(directory)
|
||||
makedirs(directory)
|
||||
name = self.build_name()
|
||||
path = join(directory, name)
|
||||
capture = image.save(self.get_screen(), path)
|
||||
print "Saved screen capture to %s" % directory + name
|
||||
self.print_debug("Saved screen capture to {0}".format(path))
|
||||
except:
|
||||
print "Couldn't save screen capture to %s, %s" % \
|
||||
(directory, exc_info()[1])
|
||||
self.print_debug("Couldn't save screen capture to {0}, {1}".\
|
||||
format(directory, exc_info()[1]))
|
||||
|
||||
def build_name(self):
|
||||
config = self.get_configuration()
|
||||
prefix = config["capture-file-name-format"]
|
||||
extension = config["capture-extension"]
|
||||
return strftime(prefix) + extension
|
||||
config = self.get_configuration().get_section("screen-captures")
|
||||
prefix = config["file-name-format"]
|
||||
extension = config["file-extension"]
|
||||
return "{0}.{1}".format(strftime(prefix), extension)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from os import sep, getcwd
|
||||
from os.path import join, exists, basename, dirname
|
||||
from os.path import join, exists, basename, dirname, expanduser
|
||||
from sys import argv
|
||||
from pprint import pformat
|
||||
|
||||
|
@ -19,7 +19,7 @@ class Configuration(RawConfigParser):
|
|||
self.set_type_declarations(type_declarations)
|
||||
self.read_defaults()
|
||||
self.read_project_config_file()
|
||||
self.print_debug_statement(self)
|
||||
self.print_debug(self)
|
||||
|
||||
def set_type_declarations(self, type_declarations):
|
||||
if type_declarations is None:
|
||||
|
@ -55,7 +55,7 @@ class Configuration(RawConfigParser):
|
|||
new = ""
|
||||
if path[0] == sep:
|
||||
new += sep
|
||||
return new + join(*path.split(sep))
|
||||
return expanduser("{0}{1}".format(new, join(*path.split(sep))))
|
||||
|
||||
def read_defaults(self):
|
||||
self.read(join(dirname(__file__), self.defaults_file_path))
|
||||
|
@ -73,7 +73,7 @@ class Configuration(RawConfigParser):
|
|||
if path:
|
||||
self.read(path)
|
||||
else:
|
||||
self.print_debug_statement("No configuration file found")
|
||||
self.print_debug("No configuration file found")
|
||||
self.set_resources_search_path()
|
||||
self.set_screen_captures_path()
|
||||
|
||||
|
@ -91,7 +91,7 @@ class Configuration(RawConfigParser):
|
|||
def is_shared_mode(self):
|
||||
return "-s" in argv
|
||||
|
||||
def print_debug_statement(self, statement):
|
||||
def print_debug(self, statement):
|
||||
if self.is_debug_mode():
|
||||
print statement
|
||||
|
||||
|
|
|
@ -17,9 +17,16 @@ centered = yes
|
|||
[screen-captures]
|
||||
# path =
|
||||
rel-path = caps
|
||||
file-name-format = %Y-%m-%d_%H:%M:%S
|
||||
file-extension = png
|
||||
|
||||
[keys]
|
||||
up = K_UP, K_w
|
||||
right = K_RIGHT, K_d
|
||||
down = K_DOWN, K_s
|
||||
left = K_LEFT, K_a
|
||||
capture-screen = K_F9
|
||||
|
||||
[event]
|
||||
user-event-id = USEREVENT
|
||||
command-event-name = command
|
||||
|
|
Loading…
Reference in New Issue