kopia lustrzana https://github.com/peterhinch/micropython-nano-gui
Document label alignment. Simplify logic.
rodzic
a2e0327c87
commit
ec6a086a04
10
README.md
10
README.md
|
@ -496,6 +496,9 @@ Constructor args:
|
||||||
7. `bgcolor=None`
|
7. `bgcolor=None`
|
||||||
8. `bdcolor=False` If `False` no border is displayed. If `None` a border is
|
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.
|
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.
|
The constructor displays the string at the required location.
|
||||||
|
|
||||||
|
@ -507,9 +510,16 @@ Methods:
|
||||||
* `bgcolor=None` Background color, as per foreground.
|
* `bgcolor=None` Background color, as per foreground.
|
||||||
* `bdcolor=None` Border color. As per above except that if `False` is
|
* `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.
|
Returns the current text string.
|
||||||
2. `show` No args. (Re)draws the label. Primarily for internal use by GUI.
|
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
|
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.
|
warning is printed at the console. The label may appear at an unexpected place.
|
||||||
The following is a complete "Hello world" script.
|
The following is a complete "Hello world" script.
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# label.py Label class for nano-gui
|
# label.py Label class for nano-gui
|
||||||
|
|
||||||
# Released under the MIT License (MIT). See LICENSE.
|
# 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 micropython import const
|
||||||
from gui.core.nanogui import DObject
|
from gui.core.nanogui import DObject
|
||||||
|
@ -26,7 +26,7 @@ class Label(DObject):
|
||||||
if text is not None:
|
if text is not None:
|
||||||
self.value(text, invert)
|
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)
|
txt = super().value(text)
|
||||||
# Redraw even if no text supplied: colors may have changed.
|
# Redraw even if no text supplied: colors may have changed.
|
||||||
self.invert = invert
|
self.invert = invert
|
||||||
|
@ -35,6 +35,8 @@ class Label(DObject):
|
||||||
if bdcolor is False:
|
if bdcolor is False:
|
||||||
self.def_bdcolor = False
|
self.def_bdcolor = False
|
||||||
self.bdcolor = self.def_bdcolor if bdcolor is None else bdcolor
|
self.bdcolor = self.def_bdcolor if bdcolor is None else bdcolor
|
||||||
|
if align is not None:
|
||||||
|
self.align = align
|
||||||
self.show()
|
self.show()
|
||||||
return txt
|
return txt
|
||||||
|
|
||||||
|
@ -45,17 +47,12 @@ 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
|
||||||
if self.align == ALIGN_LEFT:
|
rcol = 0 # Relative column of LHS of text
|
||||||
Writer.set_textpos(dev, self.row, self.col)
|
if self.align:
|
||||||
else:
|
|
||||||
txt_width = wri.stringlen(txt)
|
txt_width = wri.stringlen(txt)
|
||||||
if self.width <= txt_width:
|
if self.width > txt_width:
|
||||||
Writer.set_textpos(dev, self.row, self.col)
|
rcol = self.width - txt_width if self.align == ALIGN_RIGHT else self.width // 2 - txt_width // 2
|
||||||
else:
|
Writer.set_textpos(dev, self.row, self.col + rcol)
|
||||||
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