kopia lustrzana https://github.com/peterhinch/micropython-nano-gui
Added text alignment to the Label widget
rodzic
b9b93e2443
commit
b2956ea9a3
|
@ -6,9 +6,13 @@
|
||||||
from gui.core.nanogui import DObject
|
from gui.core.nanogui import DObject
|
||||||
from gui.core.writer import Writer
|
from gui.core.writer import Writer
|
||||||
|
|
||||||
|
ALIGN_LEFT = 0
|
||||||
|
ALIGN_RIGHT = 1
|
||||||
|
ALIGN_CENTER = 2
|
||||||
|
|
||||||
# text: str display string int save width
|
# text: str display string int save width
|
||||||
class Label(DObject):
|
class Label(DObject):
|
||||||
def __init__(self, writer, row, col, text, invert=False, fgcolor=None, bgcolor=None, bdcolor=False):
|
def __init__(self, writer, row, col, text, invert=False, fgcolor=None, bgcolor=None, bdcolor=False, align=ALIGN_LEFT):
|
||||||
# Determine width of object
|
# Determine width of object
|
||||||
if isinstance(text, int):
|
if isinstance(text, int):
|
||||||
width = text
|
width = text
|
||||||
|
@ -17,6 +21,7 @@ class Label(DObject):
|
||||||
width = writer.stringlen(text)
|
width = writer.stringlen(text)
|
||||||
height = writer.height
|
height = writer.height
|
||||||
super().__init__(writer, row, col, height, width, fgcolor, bgcolor, bdcolor)
|
super().__init__(writer, row, col, height, width, fgcolor, bgcolor, bdcolor)
|
||||||
|
self.align = align
|
||||||
if text is not None:
|
if text is not None:
|
||||||
self.value(text, invert)
|
self.value(text, invert)
|
||||||
|
|
||||||
|
@ -39,7 +44,17 @@ class Label(DObject):
|
||||||
super().show() # Draw or erase border
|
super().show() # Draw or erase border
|
||||||
wri = self.writer
|
wri = self.writer
|
||||||
dev = self.device
|
dev = self.device
|
||||||
Writer.set_textpos(dev, self.row, self.col)
|
if self.align == ALIGN_LEFT:
|
||||||
|
Writer.set_textpos(dev, self.row, self.col)
|
||||||
|
else:
|
||||||
|
txt_width = wri.stringlen(txt)
|
||||||
|
if self.width <= txt_width:
|
||||||
|
Writer.set_textpos(dev, self.row, self.col)
|
||||||
|
else:
|
||||||
|
if self.align == ALIGN_RIGHT:
|
||||||
|
Writer.set_textpos(dev, self.row, self.col + self.width - txt_width)
|
||||||
|
else:
|
||||||
|
Writer.set_textpos(dev, self.row, self.col + self.width // 2 - txt_width // 2)
|
||||||
wri.setcolor(self.fgcolor, self.bgcolor)
|
wri.setcolor(self.fgcolor, self.bgcolor)
|
||||||
wri.printstring(txt, self.invert)
|
wri.printstring(txt, self.invert)
|
||||||
wri.setcolor() # Restore defaults
|
wri.setcolor() # Restore defaults
|
||||||
|
|
Ładowanie…
Reference in New Issue