pwnagotchi/pwnagotchi/ui/components.py
Simone Margaritelli cbf7511e17 misc: refactored code to work as a setup.py based package
Signed-off-by: Simone Margaritelli <evilsocket@gmail.com>
2019-10-05 23:23:31 +02:00

75 lines
2.2 KiB
Python

from PIL import Image
from textwrap import TextWrapper
class Widget(object):
def __init__(self, xy, color=0):
self.xy = xy
self.color = color
def draw(self, canvas, drawer):
raise Exception("not implemented")
class Bitmap(Widget):
def __init__(self, path, xy, color=0):
super().__init__(xy, color)
self.image = Image.open(path)
def draw(self, canvas, drawer):
canvas.paste(self.image, self.xy)
class Line(Widget):
def __init__(self, xy, color=0, width=1):
super().__init__(xy, color)
self.width = width
def draw(self, canvas, drawer):
drawer.line(self.xy, fill=self.color, width=self.width)
class Rect(Widget):
def draw(self, canvas, drawer):
drawer.rectangle(self.xy, outline=self.color)
class FilledRect(Widget):
def draw(self, canvas, drawer):
drawer.rectangle(self.xy, fill=self.color)
class Text(Widget):
def __init__(self, value="", position=(0, 0), font=None, color=0, wrap=False, max_length=0):
super().__init__(position, color)
self.value = value
self.font = font
self.wrap = wrap
self.max_length = max_length
self.wrapper = TextWrapper(width=self.max_length, replace_whitespace=False) if wrap else None
def draw(self, canvas, drawer):
if self.value is not None:
if self.wrap:
text = '\n'.join(self.wrapper.wrap(self.value))
else:
text = self.value
drawer.text(self.xy, text, font=self.font, fill=self.color)
class LabeledValue(Widget):
def __init__(self, label, value="", position=(0, 0), label_font=None, text_font=None, color=0):
super().__init__(position, color)
self.label = label
self.value = value
self.label_font = label_font
self.text_font = text_font
def draw(self, canvas, drawer):
if self.label is None:
drawer.text(self.xy, self.value, font=self.label_font, fill=self.color)
else:
pos = self.xy
drawer.text(pos, self.label, font=self.label_font, fill=self.color)
drawer.text((pos[0] + 5 + 5 * len(self.label), pos[1]), self.value, font=self.text_font, fill=self.color)