- path-list type added for config variables
- audio panel lists sound effects and their file paths
This commit is contained in:
parent
7abf359ede
commit
a30464edf5
119
pgfw/Audio.py
119
pgfw/Audio.py
|
@ -1,7 +1,10 @@
|
|||
import os, re, pygame
|
||||
|
||||
from .GameChild import *
|
||||
from .Sprite import *
|
||||
from .Input import *
|
||||
from .Animation import *
|
||||
from .extension import *
|
||||
|
||||
class Audio(GameChild):
|
||||
|
||||
|
@ -14,6 +17,7 @@ class Audio(GameChild):
|
|||
self.volume = self.BASE_VOLUME
|
||||
if self.check_command_line("-mute"):
|
||||
self.volume = 0
|
||||
self.audio_panel = AudioPanel(self)
|
||||
self.subscribe(self.respond)
|
||||
self.sfx = {}
|
||||
self.load_sfx()
|
||||
|
@ -57,9 +61,7 @@ class Audio(GameChild):
|
|||
path = [path]
|
||||
for root in path:
|
||||
prefix = ""
|
||||
print(root)
|
||||
root = self.get_resource(root)
|
||||
print(root)
|
||||
print("checking {} for sound effects".format(root))
|
||||
if root:
|
||||
if os.path.isfile(root):
|
||||
|
@ -92,11 +94,13 @@ class Audio(GameChild):
|
|||
if sound not in self.original_volumes.keys():
|
||||
self.original_volumes[sound] = sound.get_volume()
|
||||
sound.set_volume(self.original_volumes[sound] * self.volume)
|
||||
self.audio_panel.update()
|
||||
|
||||
|
||||
class SoundEffect(GameChild, pygame.mixer.Sound):
|
||||
|
||||
def __init__(self, parent, path, volume=1.0):
|
||||
self.path = path
|
||||
GameChild.__init__(self, parent)
|
||||
pygame.mixer.Sound.__init__(self, path)
|
||||
self.display_surface = self.get_display_surface()
|
||||
|
@ -116,3 +120,114 @@ class SoundEffect(GameChild, pygame.mixer.Sound):
|
|||
def get_panning(self, position):
|
||||
return 1 - max(0, ((position - .5) * 2)), \
|
||||
1 + min(0, ((position - .5) * 2))
|
||||
|
||||
|
||||
class AudioPanel(Animation):
|
||||
|
||||
MARGIN = 6
|
||||
|
||||
def __init__(self, parent):
|
||||
Animation.__init__(self, parent)
|
||||
self.subscribe(self.respond)
|
||||
self.subscribe(self.respond, pygame.MOUSEBUTTONDOWN)
|
||||
self.reset()
|
||||
|
||||
def reset(self):
|
||||
self.row_offset = 0
|
||||
self.deactivate()
|
||||
|
||||
def activate(self):
|
||||
self.active = True
|
||||
self.set_rows()
|
||||
|
||||
def deactivate(self):
|
||||
self.active = False
|
||||
|
||||
# button 5 means fingers slide up
|
||||
# button 4 means finders slide down
|
||||
def respond(self, event):
|
||||
print("audio panel received %s" % event)
|
||||
if self.get_delegate().compare(event, "toggle-audio-panel") and \
|
||||
pygame.key.get_mods() & KMOD_CTRL and pygame.key.get_mods() & KMOD_SHIFT:
|
||||
if self.active:
|
||||
self.deactivate()
|
||||
else:
|
||||
self.activate()
|
||||
elif self.active:
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
print("audio panel received mouse down event %s" % event)
|
||||
if event.button == 5:
|
||||
self.row_offset += 1
|
||||
elif event.button == 4:
|
||||
self.row_offset -= 1
|
||||
|
||||
def set_rows(self):
|
||||
self.rows = []
|
||||
for key in sorted(self.parent.sfx):
|
||||
self.rows.append(AudioPanelRow(self, key))
|
||||
|
||||
def update(self):
|
||||
if self.active:
|
||||
Animation.update(self)
|
||||
ds = self.get_display_surface()
|
||||
dsr = ds.get_rect()
|
||||
corner = Vector(self.MARGIN, self.MARGIN)
|
||||
index = self.row_offset
|
||||
while corner.y < dsr.height - self.MARGIN:
|
||||
row = self.rows[index % len(self.rows)]
|
||||
row.location.topleft = corner.copy()
|
||||
row.update()
|
||||
corner.y += row.location.height + self.MARGIN
|
||||
index += 1
|
||||
|
||||
|
||||
class AudioPanelRow(Sprite):
|
||||
|
||||
BACKGROUND = pygame.Color(255, 255, 255)
|
||||
FOREGROUND = pygame.Color(0, 0, 0)
|
||||
WIDTH = .6
|
||||
FONT_SIZE = 18
|
||||
HEIGHT = 30
|
||||
INDENT = 4
|
||||
|
||||
def __init__(self, parent, key):
|
||||
Sprite.__init__(self, parent)
|
||||
self.key = key
|
||||
self.build()
|
||||
|
||||
def build(self):
|
||||
font = pygame.font.Font(
|
||||
self.get_resource(self.get_configuration("audio", "panel-font")),
|
||||
self.FONT_SIZE)
|
||||
ds = self.get_display_surface()
|
||||
dsr = ds.get_rect()
|
||||
surface = pygame.Surface((dsr.w * self.WIDTH, self.HEIGHT), pygame.SRCALPHA)
|
||||
surface.fill(self.BACKGROUND)
|
||||
self.add_frame(surface)
|
||||
name_sprite = Sprite(self)
|
||||
name = font.render(self.key + ":", True, self.FOREGROUND)
|
||||
name_sprite.add_frame(name)
|
||||
name_sprite.display_surface = surface
|
||||
name_sprite.location.midleft = self.INDENT, self.location.centery
|
||||
name_sprite.update()
|
||||
file_sprite = Sprite(self)
|
||||
box = get_boxed_surface(
|
||||
pygame.Surface((self.location.w - name_sprite.location.w - self.INDENT * 3,
|
||||
self.location.height - 4), pygame.SRCALPHA),
|
||||
border=self.FOREGROUND)
|
||||
file_sprite.add_frame(box)
|
||||
file_sprite.location.midright = self.location.right - self.INDENT, self.location.centery
|
||||
file_sprite.display_surface = surface
|
||||
# file_text = render_box(
|
||||
# font, self.get_sound_effect().path, color=self.FOREGROUND,
|
||||
# border=self.FOREGROUND, padding=2)
|
||||
file_name_sprite = Sprite(self)
|
||||
file_name_text = font.render(self.get_sound_effect().path, True, self.FOREGROUND)
|
||||
file_name_sprite.add_frame(file_name_text)
|
||||
file_name_sprite.display_surface = box
|
||||
file_name_sprite.location.midright = file_sprite.location.w - self.INDENT, file_sprite.location.h / 2
|
||||
file_name_sprite.update()
|
||||
file_sprite.update()
|
||||
|
||||
def get_sound_effect(self):
|
||||
return self.get_game().get_audio().sfx[self.key]
|
||||
|
|
|
@ -123,6 +123,7 @@ class Configuration(RawConfigParser):
|
|||
set_option(section, "volume-up", "K_F2", False)
|
||||
set_option(section, "volume-mute", "K_F3", False)
|
||||
set_option(section, "toggle-interpolator", "K_F7", False)
|
||||
set_option(section, "toggle-audio-panel", "K_a", False)
|
||||
section = "joy"
|
||||
add_section(section)
|
||||
set_option(section, "advance", "7", False)
|
||||
|
@ -145,6 +146,7 @@ class Configuration(RawConfigParser):
|
|||
set_option(section, "sfx-project-path", "sfx", False)
|
||||
set_option(section, "sfx-volume", "1.0", False)
|
||||
set_option(section, "sfx-extensions", "wav, ogg, mp3", False)
|
||||
set_option(section, "panel-font", None, False)
|
||||
section = "interpolator-gui"
|
||||
add_section(section)
|
||||
set_option(section, "margin", "80", False)
|
||||
|
@ -287,6 +289,8 @@ class Configuration(RawConfigParser):
|
|||
return [int(member) for member in value.split(types.list_member_sep)]
|
||||
elif pair in types["float-list"]:
|
||||
return [float(member) for member in value.split(types.list_member_sep)]
|
||||
elif pair in types["path-list"]:
|
||||
return [self.translate_path(member.strip()) for member in value.split(types.list_member_sep)]
|
||||
return value
|
||||
|
||||
def set_screen_captures_path(self):
|
||||
|
@ -433,9 +437,12 @@ class TypeDeclarations(dict):
|
|||
|
||||
"audio": {
|
||||
|
||||
"list": [
|
||||
"sfx-extensions", "sfx-default-path", "sfx-repository-path",
|
||||
"sfx-project-path"
|
||||
"list": "sfx-extensions",
|
||||
|
||||
"path": "panel-font",
|
||||
|
||||
"path-list": [
|
||||
"sfx-default-path", "sfx-repository-path", "sfx-project-path"
|
||||
],
|
||||
|
||||
"float": "sfx-volume"
|
||||
|
@ -459,7 +466,8 @@ class TypeDeclarations(dict):
|
|||
|
||||
def __init__(self):
|
||||
dict.__init__(self, {"bool": [], "int": [], "float": [], "path": [],
|
||||
"list": [], "int-list": [], "float-list": []})
|
||||
"list": [], "int-list": [], "float-list": [],
|
||||
"path-list": []})
|
||||
self.add_chart(self.defaults)
|
||||
self.add_chart(self.additional_defaults)
|
||||
|
||||
|
|
|
@ -175,10 +175,10 @@ class Input(GameChild):
|
|||
post("mouse-double-click-left", pos=pos)
|
||||
last = get_secs()
|
||||
self.last_mouse_down_left = last
|
||||
if "mouse" not in self.any_press_ignored_keys:
|
||||
if "mouse" not in self.any_press_ignored:
|
||||
self.post_any_command(event.button)
|
||||
if event.type == MOUSEBUTTONUP:
|
||||
if "mouse" not in self.any_press_ignored_keys:
|
||||
if "mouse" not in self.any_press_ignored:
|
||||
self.post_any_command(event.button, True)
|
||||
|
||||
def get_axes(self):
|
||||
|
|
Loading…
Reference in New Issue