added support for transparency

This commit is contained in:
Louis Heredero 2024-01-28 13:30:51 +01:00
parent f5ddbf4a69
commit 946f4a3b5c
2 changed files with 23 additions and 13 deletions

View File

@ -18,6 +18,7 @@ class Renderer:
self.config = config
pygame.init()
self.win = pygame.display.set_mode([Renderer.WIDTH, Renderer.HEIGHT])
self.surf = pygame.Surface([Renderer.WIDTH, Renderer.HEIGHT], pygame.SRCALPHA)
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)
@ -26,13 +27,15 @@ class Renderer:
def render(self, schema: InstructionSetSchema) -> None:
self.win.fill(self.config.BACKGROUND_COLOR)
self.surf.fill(self.config.BACKGROUND_COLOR)
self.drawStructure(schema.structures["main"], schema.structures, self.margins[3], self.margins[0])
self.win.blit(self.surf, [0, 0])
pygame.display.flip()
def save(self, path: str) -> None:
pygame.image.save(self.win, path)
pygame.image.save(self.surf, path)
def drawStructure(self,
struct: Structure,
@ -52,20 +55,20 @@ class Renderer:
startBit = struct.start
# Draw rectangle around structure
pygame.draw.rect(self.win, borderCol, [bitsX, bitsY, bitsWidth, bitH], 2)
pygame.draw.rect(self.surf, borderCol, [bitsX, bitsY, bitsWidth, bitH], 2)
for i in range(struct.bits):
bitX = ox + i * bitW
bitITxt = self.font.render(str(struct.bits - i - 1 + startBit), True, txtCol)
self.win.blit(bitITxt, [
self.surf.blit(bitITxt, [
bitX + (bitW - bitITxt.get_width())/2,
oy + (bitH - bitITxt.get_height())/2
])
# Draw separator
if i != 0:
pygame.draw.line(self.win, borderCol, [bitX, bitsY], [bitX, bitsY + bitH])
pygame.draw.line(self.surf, borderCol, [bitX, bitsY], [bitX, bitsY + bitH])
ranges = struct.getSortedRanges()
descX = ox + max(0, (struct.bits-12) * bitW)
@ -81,8 +84,8 @@ class Renderer:
nameTxt = self.font.render(range_.name, True, txtCol)
nameX = rStartX + (rWidth - nameTxt.get_width())/2
nameY = bitsY + (bitH - nameTxt.get_height())/2
pygame.draw.rect(self.win, bgCol, [rStartX + bitW/2, nameY, rWidth - bitW, nameTxt.get_height()], 0)
self.win.blit(nameTxt, [nameX, nameY])
pygame.draw.rect(self.surf, bgCol, [rStartX + bitW/2, nameY, rWidth - bitW, nameTxt.get_height()], 0)
self.surf.blit(nameTxt, [nameX, nameY])
if range_.description:
descX, descY = self.drawDescription(range_, rStartX, bitsY, rWidth, descX, descY)
@ -103,7 +106,7 @@ class Renderer:
y0 = bitsY + bitH * 1.25
y1 = bitsY + bitH * 1.5
pygame.draw.lines(self.win, self.config.LINK_COLOR, False, [
pygame.draw.lines(self.surf, self.config.LINK_COLOR, False, [
[x0, y0],
[x0, y1],
[x1, y1],
@ -114,7 +117,7 @@ class Renderer:
bitH = self.config.BIT_HEIGHT
arrowMargin = self.config.ARROW_MARGIN
pygame.draw.lines(self.win, self.config.LINK_COLOR, False, [
pygame.draw.lines(self.surf, self.config.LINK_COLOR, False, [
[startX, startY + bitH*1.5],
[startX, endY + bitH/2],
[endX - arrowMargin, endY + bitH/2]
@ -139,7 +142,7 @@ class Renderer:
self.drawLink(midX, rStartY, descX, descY)
descTxt = self.font.render(range_.description, True, self.config.TEXT_COLOR)
self.win.blit(descTxt, [descX, descY + (bitH - descTxt.get_height())/2])
self.surf.blit(descTxt, [descX, descY + (bitH - descTxt.get_height())/2])
descY += descTxt.get_height()
@ -158,7 +161,7 @@ class Renderer:
for val, desc in sorted(values.items(), key=lambda vd: vd[0]):
descY += gap
valTxt = self.italicFont.render(f"{val} = {desc}", True, textCol)
self.win.blit(valTxt, [descX + bitW/2, descY])
self.surf.blit(valTxt, [descX + bitW/2, descY])
descY += valTxt.get_height()
return (descX, descY)
@ -243,21 +246,21 @@ class Renderer:
for i in range(dashes):
a = start + d * i * (dashLen + dashSpace)
b = a + d*dashLen
pygame.draw.line(self.win, linkCol, [a.x, a.y], [b.x, b.y])
pygame.draw.line(self.surf, linkCol, [a.x, a.y], [b.x, b.y])
n = Vec(d.y, -d.x)
width = arrowSize / 1.5
p1 = end - d * arrowSize + n * width
p2 = end - d * arrowSize - n * width
pygame.draw.polygon(self.win, linkCol, [
pygame.draw.polygon(self.surf, linkCol, [
[end.x, end.y],
[p1.x, p1.y],
[p2.x, p2.y]], 0)
if label:
txt = self.italicFont.render(label, True, textCol)
self.win.blit(txt, [
self.surf.blit(txt, [
(start.x + end.x - txt.get_width())/2,
(start.y + end.y)/2 + arrowLabelDist
])

7
transparent.json Normal file
View File

@ -0,0 +1,7 @@
{
"backgroundColor": [0, 0, 0, 0],
"textColor": [128, 128, 128],
"linkColor": [128, 128, 128],
"bitIColor": [128, 128, 128],
"borderColor": [128, 128, 128]
}