2021-06-09 16:11:48 +00:00
|
|
|
# label.py Extension to micro-gui providing the Label class
|
|
|
|
|
|
|
|
# Released under the MIT License (MIT). See LICENSE.
|
|
|
|
# Copyright (c) 2021 Peter Hinch
|
|
|
|
from gui.core.ugui import Widget, display
|
|
|
|
from gui.core.writer import Writer
|
|
|
|
from gui.core.colors import *
|
|
|
|
|
|
|
|
# text: str display string int save width
|
|
|
|
class Label(Widget):
|
2022-04-08 13:37:55 +00:00
|
|
|
LEFT = 0
|
|
|
|
CENTRE = 1
|
|
|
|
RIGHT = 2
|
|
|
|
def __init__(self, writer, row, col, text, invert=False, fgcolor=None, bgcolor=BLACK, bdcolor=False, justify=0):
|
2021-06-24 08:58:57 +00:00
|
|
|
self.writer = writer
|
2022-04-08 13:37:55 +00:00
|
|
|
self.justify = justify
|
2021-06-09 16:11:48 +00:00
|
|
|
# Determine width of object
|
|
|
|
if isinstance(text, int):
|
|
|
|
width = text
|
|
|
|
text = None
|
|
|
|
else:
|
|
|
|
width = writer.stringlen(text)
|
|
|
|
height = writer.height
|
|
|
|
self.invert = invert
|
|
|
|
super().__init__(writer, row, col, height, width, fgcolor, bgcolor, bdcolor)
|
2022-04-08 13:37:55 +00:00
|
|
|
self.tcol = col
|
2021-06-09 16:11:48 +00:00
|
|
|
if text is not None:
|
|
|
|
self.value(text, invert)
|
|
|
|
|
|
|
|
def value(self, text=None, invert=False, fgcolor=None, bgcolor=None, bdcolor=None):
|
2022-04-08 13:37:55 +00:00
|
|
|
sl = self.writer.stringlen(text)
|
|
|
|
self.tcol = self.col # Default is left justify
|
|
|
|
if sl > self.width: # Clip
|
2021-09-29 08:08:10 +00:00
|
|
|
font = self.writer.font
|
|
|
|
pos = 0
|
|
|
|
n = 0
|
|
|
|
for ch in text:
|
|
|
|
pos += font.get_ch(ch)[2] # width of current char
|
|
|
|
if pos > self.width:
|
|
|
|
break
|
|
|
|
n += 1
|
|
|
|
text = text[: n]
|
2022-04-08 13:37:55 +00:00
|
|
|
elif self.justify == 1:
|
|
|
|
self.tcol = self.col + (self.width - sl) // 2
|
|
|
|
elif self.justify == 2:
|
|
|
|
self.tcol = self.col + self.width - sl
|
|
|
|
|
2021-06-09 16:11:48 +00:00
|
|
|
txt = super().value(text) # Sets .draw ensuring refresh
|
|
|
|
# Redraw even if no text supplied: colors may have changed.
|
|
|
|
self.invert = invert
|
|
|
|
self.fgcolor = self.def_fgcolor if fgcolor is None else fgcolor
|
|
|
|
self.bgcolor = self.def_bgcolor if bgcolor is None else bgcolor
|
|
|
|
if bdcolor is False:
|
|
|
|
self.def_bdcolor = False
|
|
|
|
self.bdcolor = self.def_bdcolor if bdcolor is None else bdcolor
|
|
|
|
return txt
|
|
|
|
|
|
|
|
def show(self):
|
|
|
|
if super().show(): # Draw or erase border
|
|
|
|
if isinstance(txt := super().value(), str):
|
2022-04-08 13:37:55 +00:00
|
|
|
display.print_left(self.writer, self.tcol, self.row, txt, self.fgcolor, self.bgcolor, self.invert)
|