82 lines
2.3 KiB
Python
82 lines
2.3 KiB
Python
import pygame.draw
|
|
|
|
import src.gui as gui
|
|
|
|
|
|
class BlockColor:
|
|
PINK = gui.Color(128, 0, 128)
|
|
VIOLET = gui.Color(73, 0, 128)
|
|
GREEN = gui.Color(0, 128, 0)
|
|
BLUE = gui.Color(255, 0, 0)
|
|
YELLOW = gui.Color(255, 255, 0)
|
|
|
|
|
|
class CodeBlock(gui.ListItem):
|
|
TEXT_SIZE = 24
|
|
TEXT_COLOR = gui.Color(255, 255, 255)
|
|
TEXT_FONT = 'freesansbold.ttf'
|
|
RADIUS_CORNERS = 30
|
|
PADDING = 20
|
|
SPACE = 10
|
|
|
|
def __init__(self, color: gui.Color, icon: gui.Image):
|
|
gui.ListItem.__init__(self, gui.Canvas(gui.Dimensions(500, 100)))
|
|
self.icon = icon
|
|
self.color = color
|
|
self.content = []
|
|
|
|
def add_text(self, text: str) -> None:
|
|
for word in text.split(' '):
|
|
self.add_visualization(
|
|
gui.Text(word, size = self.TEXT_SIZE, font_family = self.TEXT_FONT, color = self.TEXT_COLOR)
|
|
)
|
|
|
|
def add_visualization(self, visualization: gui.Visualization) -> None:
|
|
self.content.append(visualization)
|
|
|
|
def draw(self, canvas: gui.Canvas) -> None:
|
|
self.canvas.clear()
|
|
|
|
self._render_background()
|
|
|
|
self.canvas.draw_inside(self.icon, gui.Position(self.PADDING, self.PADDING))
|
|
|
|
line_dimensions = gui.Dimensions(self.icon.get_dimensions().width, self.PADDING)
|
|
line_dimensions.width += self.PADDING
|
|
|
|
max_height = self.icon.get_dimensions().height
|
|
|
|
for drawable in self.content:
|
|
line_dimensions.width += self.SPACE
|
|
max_height = max(max_height, drawable.get_dimensions().height)
|
|
|
|
if line_dimensions.width + drawable.get_dimensions().width > self.canvas.get_dimensions().width:
|
|
line_dimensions.width = self.PADDING + self.icon.get_dimensions().width + self.SPACE
|
|
line_dimensions.height += max_height
|
|
max_height = drawable.get_dimensions().height
|
|
|
|
self.canvas.draw_inside(drawable, gui.Position(line_dimensions.width, line_dimensions.height))
|
|
|
|
line_dimensions.width += drawable.get_dimensions().width
|
|
|
|
gui.Visualization.draw(self, canvas)
|
|
|
|
def _render_background(self):
|
|
self.canvas.clear(None)
|
|
|
|
pygame.draw.rect(
|
|
self.canvas.surface,
|
|
self.color.as_tuple(),
|
|
(0, 0, self.canvas.get_dimensions().width, self.canvas.get_dimensions().height),
|
|
0,
|
|
self.RADIUS_CORNERS
|
|
)
|
|
|
|
|
|
class Code(gui.ItemList):
|
|
def __init__(self, position: gui.Position):
|
|
gui.ItemList.__init__(self, position, gui.Dimensions(500, 250))
|
|
|
|
def add_code_block(self, code_block: CodeBlock):
|
|
self.add_item(code_block)
|