changed config structure
This commit is contained in:
parent
bf2663e9d7
commit
371c1d3cca
40
config.py
40
config.py
@ -1,10 +1,38 @@
|
|||||||
import json
|
import json
|
||||||
from typing import Any
|
import re
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
def __init__(self) -> None:
|
DEFAULT_FONT_FAMILY = "Ubuntu Mono"
|
||||||
with open("config.json", "r") as f:
|
DEFAULT_FONT_SIZE = 16
|
||||||
self.params = json.load(f)
|
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:
|
def __init__(self, path: str = "config.json") -> None:
|
||||||
return self.params[param]
|
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()
|
62
renderer.py
62
renderer.py
@ -18,14 +18,14 @@ class Renderer:
|
|||||||
self.config = config
|
self.config = config
|
||||||
pygame.init()
|
pygame.init()
|
||||||
self.win = pygame.display.set_mode([Renderer.WIDTH, Renderer.HEIGHT])
|
self.win = pygame.display.set_mode([Renderer.WIDTH, Renderer.HEIGHT])
|
||||||
self.font = pygame.font.SysFont(self.config.get("defaultFontFamily"), self.config.get("defaultFontSize"))
|
self.font = pygame.font.SysFont(self.config.DEFAULT_FONT_FAMILY, self.config.DEFAULT_FONT_SIZE)
|
||||||
self.italicFont = pygame.font.SysFont(self.config.get("italicFontFamily"), self.config.get("italicFontSize"), italic=True)
|
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:
|
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])
|
self.drawStructure(schema.structures["main"], schema.structures, self.margins[3], self.margins[0])
|
||||||
|
|
||||||
@ -41,11 +41,11 @@ class Renderer:
|
|||||||
ox: float = 0,
|
ox: float = 0,
|
||||||
oy: float = 0) -> float:
|
oy: float = 0) -> float:
|
||||||
|
|
||||||
bgCol = self.config.get("backgroundColor")
|
bgCol = self.config.BACKGROUND_COLOR
|
||||||
txtCol = self.config.get("textColor")
|
txtCol = self.config.TEXT_COLOR
|
||||||
borderCol = self.config.get("borderColor")
|
borderCol = self.config.BORDER_COLOR
|
||||||
bitW = self.config.get("bitWidth")
|
bitW = self.config.BIT_WIDTH
|
||||||
bitH = self.config.get("bitHeight")
|
bitH = self.config.BIT_HEIGHT
|
||||||
|
|
||||||
bitsX, bitsY = ox, oy + bitH
|
bitsX, bitsY = ox, oy + bitH
|
||||||
bitsWidth = struct.bits * bitW
|
bitsWidth = struct.bits * bitW
|
||||||
@ -95,15 +95,15 @@ class Renderer:
|
|||||||
return descY
|
return descY
|
||||||
|
|
||||||
def drawUnderbracket(self, start: float, end: float, bitsY: float) -> None:
|
def drawUnderbracket(self, start: float, end: float, bitsY: float) -> None:
|
||||||
bitW = self.config.get("bitWidth")
|
bitW = self.config.BIT_WIDTH
|
||||||
bitH = self.config.get("bitHeight")
|
bitH = self.config.BIT_HEIGHT
|
||||||
|
|
||||||
x0 = start + bitW/2
|
x0 = start + bitW/2
|
||||||
x1 = end - bitW/2
|
x1 = end - bitW/2
|
||||||
y0 = bitsY + bitH * 1.25
|
y0 = bitsY + bitH * 1.25
|
||||||
y1 = bitsY + bitH * 1.5
|
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, y0],
|
||||||
[x0, y1],
|
[x0, y1],
|
||||||
[x1, y1],
|
[x1, y1],
|
||||||
@ -111,10 +111,10 @@ class Renderer:
|
|||||||
])
|
])
|
||||||
|
|
||||||
def drawLink(self, startX: float, startY: float, endX: float, endY: float) -> None:
|
def drawLink(self, startX: float, startY: float, endX: float, endY: float) -> None:
|
||||||
bitH = self.config.get("bitHeight")
|
bitH = self.config.BIT_HEIGHT
|
||||||
arrowMargin = self.config.get("arrowMargin")
|
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, startY + bitH*1.5],
|
||||||
[startX, endY + bitH/2],
|
[startX, endY + bitH/2],
|
||||||
[endX - arrowMargin, endY + bitH/2]
|
[endX - arrowMargin, endY + bitH/2]
|
||||||
@ -128,8 +128,8 @@ class Renderer:
|
|||||||
descX: float,
|
descX: float,
|
||||||
descY: float) -> tuple[float, float]:
|
descY: float) -> tuple[float, float]:
|
||||||
|
|
||||||
bitW = self.config.get("bitWidth")
|
bitW = self.config.BIT_WIDTH
|
||||||
bitH = self.config.get("bitHeight")
|
bitH = self.config.BIT_HEIGHT
|
||||||
|
|
||||||
descX = max(descX, rStartX + rWidth/2 + bitW)
|
descX = max(descX, rStartX + rWidth/2 + bitW)
|
||||||
|
|
||||||
@ -138,7 +138,7 @@ class Renderer:
|
|||||||
midX = rStartX + rWidth/2
|
midX = rStartX + rWidth/2
|
||||||
self.drawLink(midX, rStartY, descX, descY)
|
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])
|
self.win.blit(descTxt, [descX, descY + (bitH - descTxt.get_height())/2])
|
||||||
|
|
||||||
descY += descTxt.get_height()
|
descY += descTxt.get_height()
|
||||||
@ -146,14 +146,14 @@ class Renderer:
|
|||||||
if range_.values is not None and range_.dependsOn is None:
|
if range_.values is not None and range_.dependsOn is None:
|
||||||
descX, descY = self.drawValues(range_.values, descX, descY)
|
descX, descY = self.drawValues(range_.values, descX, descY)
|
||||||
|
|
||||||
descY += self.config.get("descriptionMargin")
|
descY += self.config.DESCRIPTION_MARGIN
|
||||||
|
|
||||||
return (descX, descY)
|
return (descX, descY)
|
||||||
|
|
||||||
def drawValues(self, values: dict[str, str], descX: float, descY: float) -> tuple[float, float]:
|
def drawValues(self, values: dict[str, str], descX: float, descY: float) -> tuple[float, float]:
|
||||||
textCol = self.config.get("textColor")
|
textCol = self.config.TEXT_COLOR
|
||||||
bitW = self.config.get("bitWidth")
|
bitW = self.config.BIT_HEIGHT
|
||||||
gap = self.config.get("valuesGap")
|
gap = self.config.VALUES_GAP
|
||||||
|
|
||||||
for val, desc in sorted(values.items(), key=lambda vd: vd[0]):
|
for val, desc in sorted(values.items(), key=lambda vd: vd[0]):
|
||||||
descY += gap
|
descY += gap
|
||||||
@ -173,9 +173,9 @@ class Renderer:
|
|||||||
descX: float,
|
descX: float,
|
||||||
descY: float) -> tuple[float, float]:
|
descY: float) -> tuple[float, float]:
|
||||||
|
|
||||||
bitW = self.config.get("bitWidth")
|
bitW = self.config.BIT_WIDTH
|
||||||
bitH = self.config.get("bitHeight")
|
bitH = self.config.BIT_HEIGHT
|
||||||
arrowMargin = self.config.get("arrowMargin")
|
arrowMargin = self.config.ARROW_MARGIN
|
||||||
|
|
||||||
rStartI = struct.bits - range_.end - 1
|
rStartI = struct.bits - range_.end - 1
|
||||||
rStartX = bitsX + rStartI * bitW
|
rStartX = bitsX + rStartI * bitW
|
||||||
@ -226,12 +226,12 @@ class Renderer:
|
|||||||
return (descX, descY)
|
return (descX, descY)
|
||||||
|
|
||||||
def drawArrow(self, sx: float, sy: float, ex: float, ey: float, label: str = "") -> None:
|
def drawArrow(self, sx: float, sy: float, ex: float, ey: float, label: str = "") -> None:
|
||||||
dashLen = self.config.get("dashLength")
|
dashLen = self.config.DASH_LENGTH
|
||||||
dashSpace = self.config.get("dashSpace")
|
dashSpace = self.config.DASH_SPACE
|
||||||
arrowSize = self.config.get("arrowSize")
|
arrowSize = self.config.ARROW_SIZE
|
||||||
linkCol = self.config.get("linkColor")
|
linkCol = self.config.LINK_COLOR
|
||||||
textCol = self.config.get("textColor")
|
textCol = self.config.TEXT_COLOR
|
||||||
arrowLabelDist = self.config.get("arrowLabelDistance")
|
arrowLabelDist = self.config.ARROW_LABEL_DISTANCE
|
||||||
|
|
||||||
start = Vec(sx, sy)
|
start = Vec(sx, sy)
|
||||||
end = Vec(ex, ey)
|
end = Vec(ex, ey)
|
||||||
|
@ -5,8 +5,8 @@ from renderer import Renderer
|
|||||||
from structure import Structure
|
from structure import Structure
|
||||||
|
|
||||||
class InstructionSetSchema:
|
class InstructionSetSchema:
|
||||||
def __init__(self, path: str) -> None:
|
def __init__(self, path: str, configPath: str = "config.json") -> None:
|
||||||
self.config = Config()
|
self.config = Config(configPath)
|
||||||
self.path = path
|
self.path = path
|
||||||
self.load()
|
self.load()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user