PEP8 and QOL adjustments

current
Nathan Deren 2018-06-06 09:50:42 -07:00
parent a01b2cc69d
commit b479a88d64
2 changed files with 9 additions and 5 deletions

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
pyqt5==5.10.1

View File

@ -5,6 +5,7 @@ from math import ceil
from PyQt5.QtGui import QGuiApplication, QPixmap, QImage, QColor, QPainter from PyQt5.QtGui import QGuiApplication, QPixmap, QImage, QColor, QPainter
class Font: class Font:
"""A simple class for managing Smeargle's font data.""" """A simple class for managing Smeargle's font data."""
def __init__(self, filename): def __init__(self, filename):
@ -72,6 +73,7 @@ class Font:
"""Calculate the pixel-wise length of the given string.""" """Calculate the pixel-wise length of the given string."""
return sum(self.table[x]['width'] for x in text) return sum(self.table[x]['width'] for x in text)
class Script: class Script:
def __init__(self, filename, max_tiles=0): def __init__(self, filename, max_tiles=0):
self.max_tiles = max_tiles self.max_tiles = max_tiles
@ -90,7 +92,7 @@ class Script:
continue continue
length = font.length(line) length = font.length(line)
length = ceil(length / font.width) * font.width length = ceil(length / font.width) * font.width
if max_tiles > 0 and length > max_tiles: if 0 < max_tiles < length:
print('WARNING: "{}" exceeds {} tiles by {}px; truncating.'.format( print('WARNING: "{}" exceeds {} tiles by {}px; truncating.'.format(
line, line,
int(max_tiles / font.width), int(max_tiles / font.width),
@ -115,7 +117,8 @@ class Script:
return lines return lines
def generate_tilemap(self, font, lines): @staticmethod
def generate_tilemap(font, lines):
tilemap = {} tilemap = {}
raw_tiles = [] raw_tiles = []
compressed_tiles = [] compressed_tiles = []
@ -159,7 +162,7 @@ class Script:
count -= 1 count -= 1
indexes.append((text, ' '.join(tile_idx))) indexes.append((text, ' '.join(tile_idx)))
return (compressed_tiles, raw_tiles, map_idx, indexes, total, unique) return compressed_tiles, raw_tiles, map_idx, indexes, total, unique
def render_tiles(self, font, tiles): def render_tiles(self, font, tiles):
image = QImage(font.width * 16, ceil(len(tiles) / 16) * font.height, QImage.Format_RGB32) image = QImage(font.width * 16, ceil(len(tiles) / 16) * font.height, QImage.Format_RGB32)
@ -186,6 +189,7 @@ class Script:
def render_tiles_to_file(self, font, tiles, filename): def render_tiles_to_file(self, font, tiles, filename):
self.render_tiles(font, tiles).save(filename, 'PNG') self.render_tiles(font, tiles).save(filename, 'PNG')
class Game: class Game:
def __init__(self, filename): def __init__(self, filename):
with open(filename, mode='rb') as f: with open(filename, mode='rb') as f:
@ -254,6 +258,7 @@ class Game:
print('Compressed: ', output_comp) print('Compressed: ', output_comp)
print('Tile<->text: ', output_map) print('Tile<->text: ', output_map)
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
import os import os
@ -276,5 +281,3 @@ if __name__ == '__main__':
print('Processing {}...'.format(script)) print('Processing {}...'.format(script))
game.render_script(script, render_path, output=True) game.render_script(script, render_path, output=True)
print('{} processed.'.format(script)) print('{} processed.'.format(script))