Allow left, center, and right alignment of text
This commit is contained in:
+27
-3
@@ -9,6 +9,14 @@ import (
|
||||
rgbmatrix "git.dvdt.dev/david/bitcoin-ticker-pi/go-rpi-rgb-led-matrix"
|
||||
)
|
||||
|
||||
type Alignment uint8
|
||||
|
||||
const (
|
||||
Left Alignment = iota
|
||||
Center
|
||||
Right
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnsupportedSlotHeight = errors.New("unsupported slot height")
|
||||
)
|
||||
@@ -33,15 +41,17 @@ func drawRow(canvas *rgbmatrix.Canvas, row []color.RGBA, x, y int) {
|
||||
}
|
||||
|
||||
type Slot struct {
|
||||
alignment Alignment
|
||||
segmentLimit int
|
||||
segmentHeight int
|
||||
segmentWidth int
|
||||
x, y int
|
||||
x, xoffset, y int
|
||||
text []Segment
|
||||
}
|
||||
|
||||
func NewSlot(limit, height, width, x, y int) *Slot {
|
||||
func NewSlot(alignment Alignment, limit, height, width, x, y int) *Slot {
|
||||
return &Slot{
|
||||
alignment: alignment,
|
||||
segmentLimit: limit,
|
||||
segmentHeight: height,
|
||||
segmentWidth: width,
|
||||
@@ -61,6 +71,20 @@ func (s *Slot) Update(text []Segment) error {
|
||||
}...)
|
||||
}
|
||||
|
||||
// Default to left alignment.
|
||||
s.xoffset = 0
|
||||
if s.alignment == Center {
|
||||
s.xoffset = ((s.segmentLimit * s.segmentWidth) - (len(text) * s.segmentWidth)) / 2
|
||||
}
|
||||
if s.alignment == Right {
|
||||
s.xoffset = (s.segmentLimit * s.segmentWidth) - (len(text) * s.segmentWidth)
|
||||
}
|
||||
|
||||
fmt.Printf("segmentLimit: %d\n", s.segmentLimit)
|
||||
fmt.Printf("segmentWidth: %d\n", s.segmentWidth)
|
||||
fmt.Printf("len(text): %d\n", len(text))
|
||||
fmt.Printf("xoffset value: %d\n", s.xoffset)
|
||||
|
||||
var font fonts.Font
|
||||
switch s.segmentHeight {
|
||||
case 5:
|
||||
@@ -88,6 +112,6 @@ func (s *Slot) Update(text []Segment) error {
|
||||
|
||||
func (s *Slot) Draw(canvas *rgbmatrix.Canvas) {
|
||||
for i, segment := range s.text {
|
||||
segment.Draw(canvas, s.x+(i*s.segmentWidth), s.y)
|
||||
segment.Draw(canvas, s.x+(i*s.segmentWidth)+s.xoffset, s.y)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user