sound fx panning; mouse events; optional kwargs for events

This commit is contained in:
Frank DeMarco 2012-10-01 23:02:11 -04:00
parent dfa83f466c
commit c0ed51b2c9
5 changed files with 72 additions and 12 deletions

View File

@ -1,7 +1,7 @@
from os import listdir
from os.path import join
from pygame.mixer import Channel, Sound, music
from pygame.mixer import Channel, Sound, music, find_channel
from GameChild import *
from Input import *
@ -52,9 +52,16 @@ class Audio(GameChild):
self.current_channel = None
self.paused = False
def play_fx(self, name):
def play_fx(self, name, panning=.5):
if not self.muted:
self.fx[name].play()
channel = find_channel(True)
if panning != .5:
offset = 1 - abs(panning - .5) * 2
if panning < .5:
channel.set_volume(1, offset)
else:
channel.set_volume(offset, 1)
channel.play(self.fx[name])
def pause(self):
channel = self.current_channel

View File

@ -72,6 +72,9 @@ class Configuration(RawConfigParser):
set_option(section, "rel-path", "caps")
set_option(section, "file-name-format", "%Y-%m-%d_%H:%M:%S")
set_option(section, "file-extension", "png")
section = "mouse"
add_section(section)
set_option(section, "double-click-time-limit", ".5")
section = "keys"
add_section(section)
set_option(section, "up", "K_UP, K_w")
@ -81,10 +84,17 @@ class Configuration(RawConfigParser):
set_option(section, "capture-screen", "K_F9")
set_option(section, "toggle-fullscreen", "K_F11")
set_option(section, "reset-game", "K_F8")
section = "joy"
add_section(section)
set_option(section, "advance", "7")
set_option(section, "pause", "7")
section = "event"
add_section(section)
set_option(section, "custom-event-id", "USEREVENT")
set_option(section, "command-event-name", "command")
section = "audio"
add_section(section)
set_option(section, "sfx-path", "aud/fx/")
def read_project_config_file(self):
path = self.locate_project_config_file()
@ -231,7 +241,10 @@ class TypeDeclarations(dict):
"description-file", "main-object",
"icon-path", "windows-dist-path",
"package-root"]},
"keys": {"list": ["up", "right", "down", "left"]}}
"mouse": {"float": "double-click-time-limit"},
"keys": {"list": ["up", "right", "down", "left"]},
"joy": {"int": ["advance", "pause"]},
"audio": {"path": "sfx-path"}}
additional_defaults = {}
def __init__(self):

View File

@ -30,7 +30,6 @@ class Game(GameChild, Animation):
self.subscribe_to(self.get_custom_event_id(), self.end)
self.clear_queue()
self.delegate.enable()
def init_gamechild(self):
GameChild.__init__(self)

View File

@ -17,8 +17,13 @@ class GameChild:
current = current.parent
return current
def get_configuration(self):
return self.get_game().configuration
def get_configuration(self, section=None, option=None):
config = self.get_game().configuration
if option and section:
return config.get(section, option)
if section:
return config.get_section(section)
return config
def get_input(self):
return self.get_game().input

View File

@ -1,3 +1,5 @@
from time import time
from pygame import event, joystick as joy, key as keyboard
from pygame.locals import *
@ -7,6 +9,7 @@ class Input(GameChild):
def __init__(self, game):
GameChild.__init__(self, game)
self.last_mouse_down_left = None
self.joystick = Joystick()
self.unsuppress()
self.subscribe_to_events()
@ -16,6 +19,8 @@ class Input(GameChild):
self.subscribe_to(KEYDOWN, self.translate_key_press)
self.subscribe_to(JOYBUTTONDOWN, self.translate_joy_button)
self.subscribe_to(JOYAXISMOTION, self.translate_axis_motion)
self.subscribe_to(MOUSEBUTTONDOWN, self.translate_mouse_input)
self.subscribe_to(MOUSEBUTTONUP, self.translate_mouse_input)
def suppress(self):
self.suppressed = True
@ -42,20 +47,20 @@ class Input(GameChild):
if key in keys:
self.post_command(cmd)
def post_command(self, cmd):
def post_command(self, cmd, **kwargs):
config = self.get_configuration().get_section("event")
eid = self.get_custom_event_id()
self.print_debug("Posting {0} command with id {1}".format(cmd, eid))
name = config["command-event-name"]
event.post(event.Event(eid, name=name, command=cmd))
event.post(event.Event(eid, name=name, command=cmd, **kwargs))
def translate_joy_button(self, evt):
if not self.suppressed:
button = evt.button
config = self.get_configuration()
if button == config["joy-advance"]:
config = self.get_configuration().get_section("joy")
if button == config["advance"]:
self.post_command("advance")
if button == config["joy-pause"]:
if button == config["pause"]:
self.post_command("pause")
def translate_axis_motion(self, evt):
@ -95,6 +100,37 @@ class Input(GameChild):
if poll[key]:
return True
def translate_mouse_input(self, evt):
button = evt.button
pos = evt.pos
post = self.post_command
if evt.type == MOUSEBUTTONDOWN:
if button == 1:
last = self.last_mouse_down_left
if last:
limit = self.get_configuration("mouse",
"double-click-time-limit")
if time() - last < limit:
post("mouse-double-click-left", pos=pos)
last = time()
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 = {}
for direction in "up", "right", "down", "left":