136 lines
3.0 KiB
Python
136 lines
3.0 KiB
Python
from itertools import chain
|
|
import io
|
|
import os
|
|
import subprocess
|
|
|
|
import pygame
|
|
import pygame.locals
|
|
|
|
|
|
def get_pygame_keydict():
|
|
result = {}
|
|
|
|
pygame_keys = [
|
|
item for item in dir(pygame.locals) if item.startswith('K_')
|
|
]
|
|
|
|
for key in pygame_keys:
|
|
result[key] = getattr(pygame.locals, key)
|
|
|
|
return result
|
|
|
|
|
|
def get_keys(keys_dict, a):
|
|
result = []
|
|
for key in a:
|
|
result.append(keys_dict[key])
|
|
return result
|
|
|
|
|
|
def aspect_scale(img, bx, by):
|
|
""" Scales 'img' to fit into box bx/by.
|
|
This method will retain the original image's aspect ratio """
|
|
ix, iy = img.get_size()
|
|
if ix > iy:
|
|
# fit to width
|
|
scale_factor = bx / float(ix)
|
|
sy = scale_factor * iy
|
|
if sy > by:
|
|
scale_factor = by / float(iy)
|
|
sx = scale_factor * ix
|
|
sy = by
|
|
else:
|
|
sx = bx
|
|
else:
|
|
# fit to height
|
|
scale_factor = by / float(iy)
|
|
sx = scale_factor * ix
|
|
if sx > bx:
|
|
scale_factor = bx / float(ix)
|
|
sx = bx
|
|
sy = scale_factor * iy
|
|
else:
|
|
sy = by
|
|
|
|
return pygame.transform.scale(img, (int(sx), int(sy)))
|
|
|
|
|
|
def image_from_data(data, image_size):
|
|
if data:
|
|
with io.BytesIO(data) as f:
|
|
surface = pygame.image.load(f)
|
|
surface = aspect_scale(surface, image_size[0], image_size[1])
|
|
else:
|
|
surface = pygame.Surface(image_size) # pylint: disable=E1121
|
|
|
|
return surface
|
|
|
|
|
|
def addscanlines(surface):
|
|
width = surface.get_width()
|
|
for y in range(surface.get_height()):
|
|
if y % 2:
|
|
pygame.draw.line(surface, (0, 0, 0), (0, y), (width, y))
|
|
|
|
return surface
|
|
|
|
|
|
def truncline(text, font, maxwidth):
|
|
real = len(text)
|
|
stext = text
|
|
l = font.size(text)[0]
|
|
cut = 0
|
|
a = 0
|
|
done = 1
|
|
while l > maxwidth:
|
|
a = a + 1
|
|
n = text.rsplit(None, a)[0]
|
|
if stext == n:
|
|
cut += 1
|
|
stext = n[:-cut]
|
|
else:
|
|
stext = n
|
|
l = font.size(stext)[0]
|
|
real = len(stext)
|
|
done = 0
|
|
return real, done, stext
|
|
|
|
|
|
def wrapline(text, font, maxwidth):
|
|
done = 0
|
|
wrapped = []
|
|
|
|
while not done:
|
|
nl, done, stext = truncline(text, font, maxwidth)
|
|
wrapped.append(stext.strip())
|
|
text = text[nl:]
|
|
return wrapped
|
|
|
|
|
|
def wrap_multi_line(text, font, maxwidth):
|
|
""" returns text taking new lines into account.
|
|
"""
|
|
if type(text) is str:
|
|
lines = chain(*(wrapline(line, font, maxwidth)
|
|
for line in text.splitlines()))
|
|
else:
|
|
lines = chain(*(wrapline(line, font, maxwidth) for line in text))
|
|
|
|
return list(lines)
|
|
|
|
|
|
def run_emulator(emu_type, exe, rom):
|
|
old_path = os.getcwd()
|
|
os.chdir(os.path.split(exe)[0])
|
|
|
|
if emu_type == 'MAME':
|
|
subprocess.run([exe, rom])
|
|
elif emu_type == 'MESS':
|
|
subprocess.run([exe, rom])
|
|
elif emu_type == 'CSV':
|
|
subprocess.run([exe.replace('<romname>', rom)])
|
|
|
|
os.chdir(old_path)
|
|
|
|
return True
|