changed config structure

This commit is contained in:
Louis Heredero 2023-11-24 15:01:51 +01:00
parent bf2663e9d7
commit 371c1d3cca
3 changed files with 67 additions and 39 deletions

View File

@ -1,10 +1,38 @@
import json
from typing import Any
import re
class Config:
def __init__(self) -> None:
with open("config.json", "r") as f:
self.params = json.load(f)
DEFAULT_FONT_FAMILY = "Ubuntu Mono"
DEFAULT_FONT_SIZE = 16
ITALIC_FONT_FAMILY = "Ubuntu Mono"
ITALIC_FONT_SIZE = 14
BACKGROUND_COLOR = [255, 255, 255]
TEXT_COLOR = [0, 0, 0]
LINK_COLOR = [0, 0, 0]
BIT_I_COLOR = [0, 0, 0]
BORDER_COLOR = [0, 0, 0]
BIT_WIDTH = 30
BIT_HEIGHT = 30
DESCRIPTION_MARGIN = 10
DASH_LENGTH = 6
DASH_SPACE = 4
ARROW_SIZE = 10
MARGINS = [20, 20, 20, 20]
ARROW_MARGIN = 4
VALUES_GAP = 5
ARROW_LABEL_DISTANCE = 5
def get(self, param: str) -> Any:
return self.params[param]
def __init__(self, path: str = "config.json") -> None:
self.load(path)
def load(self, path: str) -> None:
with open(path, "r") as f:
config = json.load(f)
for k, v in config.items():
k = Config.formatKey(k)
if hasattr(Config, k):
setattr(Config, k, v)
def formatKey(key: str) -> str:
return re.sub(r"([a-z])([A-Z])", r"\1_\2", key).upper()

View File

@ -18,14 +18,14 @@ class Renderer:
self.config = config
pygame.init()
self.win = pygame.display.set_mode([Renderer.WIDTH, Renderer.HEIGHT])
self.font = pygame.font.SysFont(self.config.get("defaultFontFamily"), self.config.get("defaultFontSize"))
self.italicFont = pygame.font.SysFont(self.config.get("italicFontFamily"), self.config.get("italicFontSize"), italic=True)
self.font = pygame.font.SysFont(self.config.DEFAULT_FONT_FAMILY, self.config.DEFAULT_FONT_SIZE)
self.italicFont = pygame.font.SysFont(self.config.ITALIC_FONT_FAMILY, self.config.ITALIC_FONT_SIZE, italic=True)
self.margins = self.config.get("margins")
self.margins = self.config.MARGINS
def render(self, schema: InstructionSetSchema) -> None:
self.win.fill(self.config.get("backgroundColor"))
self.win.fill(self.config.BACKGROUND_COLOR)
self.drawStructure(schema.structures["main"], schema.structures, self.margins[3], self.margins[0])
@ -41,11 +41,11 @@ class Renderer:
ox: float = 0,
oy: float = 0) -> float:
bgCol = self.config.get("backgroundColor")
txtCol = self.config.get("textColor")
borderCol = self.config.get("borderColor")
bitW = self.config.get("bitWidth")
bitH = self.config.get("bitHeight")
bgCol = self.config.BACKGROUND_COLOR
txtCol = self.config.TEXT_COLOR
borderCol = self.config.BORDER_COLOR
bitW = self.config.BIT_WIDTH
bitH = self.config.BIT_HEIGHT
bitsX, bitsY = ox, oy + bitH
bitsWidth = struct.bits * bitW
@ -95,15 +95,15 @@ class Renderer:
return descY
def drawUnderbracket(self, start: float, end: float, bitsY: float) -> None:
bitW = self.config.get("bitWidth")
bitH = self.config.get("bitHeight")
bitW = self.config.BIT_WIDTH
bitH = self.config.BIT_HEIGHT
x0 = start + bitW/2
x1 = end - bitW/2
y0 = bitsY + bitH * 1.25
y1 = bitsY + bitH * 1.5
pygame.draw.lines(self.win, self.config.get("linkColor"), False, [
pygame.draw.lines(self.win, self.config.LINK_COLOR, False, [
[x0, y0],
[x0, y1],
[x1, y1],
@ -111,10 +111,10 @@ class Renderer:
])
def drawLink(self, startX: float, startY: float, endX: float, endY: float) -> None:
bitH = self.config.get("bitHeight")
arrowMargin = self.config.get("arrowMargin")
bitH = self.config.BIT_HEIGHT
arrowMargin = self.config.ARROW_MARGIN
pygame.draw.lines(self.win, self.config.get("linkColor"), False, [
pygame.draw.lines(self.win, self.config.LINK_COLOR, False, [
[startX, startY + bitH*1.5],
[startX, endY + bitH/2],
[endX - arrowMargin, endY + bitH/2]
@ -128,8 +128,8 @@ class Renderer:
descX: float,
descY: float) -> tuple[float, float]:
bitW = self.config.get("bitWidth")
bitH = self.config.get("bitHeight")
bitW = self.config.BIT_WIDTH
bitH = self.config.BIT_HEIGHT
descX = max(descX, rStartX + rWidth/2 + bitW)
@ -138,7 +138,7 @@ class Renderer:
midX = rStartX + rWidth/2
self.drawLink(midX, rStartY, descX, descY)
descTxt = self.font.render(range_.description, True, self.config.get("textColor"))
descTxt = self.font.render(range_.description, True, self.config.TEXT_COLOR)
self.win.blit(descTxt, [descX, descY + (bitH - descTxt.get_height())/2])
descY += descTxt.get_height()
@ -146,14 +146,14 @@ class Renderer:
if range_.values is not None and range_.dependsOn is None:
descX, descY = self.drawValues(range_.values, descX, descY)
descY += self.config.get("descriptionMargin")
descY += self.config.DESCRIPTION_MARGIN
return (descX, descY)
def drawValues(self, values: dict[str, str], descX: float, descY: float) -> tuple[float, float]:
textCol = self.config.get("textColor")
bitW = self.config.get("bitWidth")
gap = self.config.get("valuesGap")
textCol = self.config.TEXT_COLOR
bitW = self.config.BIT_HEIGHT
gap = self.config.VALUES_GAP
for val, desc in sorted(values.items(), key=lambda vd: vd[0]):
descY += gap
@ -173,9 +173,9 @@ class Renderer:
descX: float,
descY: float) -> tuple[float, float]:
bitW = self.config.get("bitWidth")
bitH = self.config.get("bitHeight")
arrowMargin = self.config.get("arrowMargin")
bitW = self.config.BIT_WIDTH
bitH = self.config.BIT_HEIGHT
arrowMargin = self.config.ARROW_MARGIN
rStartI = struct.bits - range_.end - 1
rStartX = bitsX + rStartI * bitW
@ -226,12 +226,12 @@ class Renderer:
return (descX, descY)
def drawArrow(self, sx: float, sy: float, ex: float, ey: float, label: str = "") -> None:
dashLen = self.config.get("dashLength")
dashSpace = self.config.get("dashSpace")
arrowSize = self.config.get("arrowSize")
linkCol = self.config.get("linkColor")
textCol = self.config.get("textColor")
arrowLabelDist = self.config.get("arrowLabelDistance")
dashLen = self.config.DASH_LENGTH
dashSpace = self.config.DASH_SPACE
arrowSize = self.config.ARROW_SIZE
linkCol = self.config.LINK_COLOR
textCol = self.config.TEXT_COLOR
arrowLabelDist = self.config.ARROW_LABEL_DISTANCE
start = Vec(sx, sy)
end = Vec(ex, ey)

View File

@ -5,8 +5,8 @@ from renderer import Renderer
from structure import Structure
class InstructionSetSchema:
def __init__(self, path: str) -> None:
self.config = Config()
def __init__(self, path: str, configPath: str = "config.json") -> None:
self.config = Config(configPath)
self.path = path
self.load()