Document label alignment. Simplify logic.

pull/46/head
peterhinch 2022-12-19 13:49:03 +00:00
rodzic a2e0327c87
commit ec6a086a04
2 zmienionych plików z 20 dodań i 13 usunięć

Wyświetl plik

@ -496,6 +496,9 @@ Constructor args:
7. `bgcolor=None`
8. `bdcolor=False` If `False` no border is displayed. If `None` a border is
shown in the `Writer` foreground color. If a color is passed, it is used.
9. `align=ALIGN_LEFT` By default text in labels is left aligned. Options are
`ALIGN_RIGHT` and `ALIGN_CENTER`. These options can only take effect if a
large enough field width is passed to `text`.
The constructor displays the string at the required location.
@ -506,10 +509,17 @@ Methods:
* `fgcolor=None` Foreground color: if `None` the `Writer` default is used.
* `bgcolor=None` Background color, as per foreground.
* `bdcolor=None` Border color. As per above except that if `False` is
passed, no border is displayed. This clears a previously drawn border.
passed, no border is displayed. This clears a previously drawn border.
* `align=None` Use alignment specified in constructor unless one of the
module constants is passed.
Returns the current text string.
2. `show` No args. (Re)draws the label. Primarily for internal use by GUI.
Module Constants:
* `ALIGN_LEFT=0`
* `ALIGN_RIGHT=1`
* `ALIGN_CENTER=2`
If populating a label would cause it to extend beyond the screen boundary a
warning is printed at the console. The label may appear at an unexpected place.
The following is a complete "Hello world" script.

Wyświetl plik

@ -1,7 +1,7 @@
# label.py Label class for nano-gui
# Released under the MIT License (MIT). See LICENSE.
# Copyright (c) 2018-2020 Peter Hinch
# Copyright (c) 2018-2022 Peter Hinch
from micropython import const
from gui.core.nanogui import DObject
@ -26,7 +26,7 @@ class Label(DObject):
if text is not None:
self.value(text, invert)
def value(self, text=None, invert=False, fgcolor=None, bgcolor=None, bdcolor=None):
def value(self, text=None, invert=False, fgcolor=None, bgcolor=None, bdcolor=None, align=None):
txt = super().value(text)
# Redraw even if no text supplied: colors may have changed.
self.invert = invert
@ -35,6 +35,8 @@ class Label(DObject):
if bdcolor is False:
self.def_bdcolor = False
self.bdcolor = self.def_bdcolor if bdcolor is None else bdcolor
if align is not None:
self.align = align
self.show()
return txt
@ -45,17 +47,12 @@ class Label(DObject):
super().show() # Draw or erase border
wri = self.writer
dev = self.device
if self.align == ALIGN_LEFT:
Writer.set_textpos(dev, self.row, self.col)
else:
rcol = 0 # Relative column of LHS of text
if self.align:
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)
if self.width > txt_width:
rcol = self.width - txt_width if self.align == ALIGN_RIGHT else self.width // 2 - txt_width // 2
Writer.set_textpos(dev, self.row, self.col + rcol)
wri.setcolor(self.fgcolor, self.bgcolor)
wri.printstring(txt, self.invert)
wri.setcolor() # Restore defaults