display-test/components/text.go
2024-10-06 21:31:23 +03:00

94 lines
1.7 KiB
Go

package components
import (
"fmt"
"gitea.unprism.ru/yotia/display-test/drawer"
)
type text struct {
drawer drawer.Drawer // Drawer with dysplay
mask mask // Flush mask
str string
rect rect // Rect
}
type Text interface {
Component
RectSetter
SetStr(newStr string) // Update string and redraw
Clear() // Clear space of bar
}
// Creation function
// Now the only way to chage shape is to recreate component
func NewText(d drawer.Drawer, x, y int) (Text, error) {
if err := d.GetDisplay().IsReady(); err != nil {
return nil, fmt.Errorf("display is ready: %w", err)
}
t := text{
drawer: d,
rect: rect{x, y, 0, drawer.FontCharH},
}
t.mask.Update(t.rect)
//t.Draw()
return &t, nil
}
func (t *text) Close() error {
return nil
}
func (t *text) Clear() {
t.drawer.FillBar(t.rect.x, t.rect.y, t.rect.x+t.rect.w, t.rect.y+t.rect.h, 0x00)
}
func (t *text) Draw() {
t.drawer.PutText(t.rect.x, t.rect.y, t.str)
t.drawer.GetDisplay().FlushByMask(t.mask.bits)
}
func (t *text) updateW() {
t.rect.w = len(t.str)*(drawer.FontCharW+drawer.CharGap) - drawer.CharGap
}
func (t *text) SetStr(str string) {
t.str = str
newW := len(t.str)*(drawer.FontCharW+drawer.CharGap) - drawer.CharGap
if t.rect.w-newW > 0 {
// Need to clear some space
t.Clear()
t.drawer.FillBar(t.rect.x+newW, t.rect.y, t.rect.x+t.rect.w, t.rect.y+t.rect.h, 0)
} else {
t.rect.w = newW
t.mask.Update(t.rect)
}
t.Draw()
t.rect.w = newW
t.mask.Update(t.rect)
}
func (t *text) GetPos() (int, int) {
return t.rect.x, t.rect.y
}
func (t *text) GetSize() (int, int) {
return t.rect.w, t.rect.h
}
func (t *text) SetPos(x, y int) {
t.rect.x = x
t.rect.y = y
t.mask.Update(t.rect)
}
func (t *text) SetSize(w, h int) {
// Empty
}