Add: pages, text component

This commit is contained in:
Andrey Egorov
2024-09-29 21:09:59 +03:00
parent bad142a565
commit a4fda8303b
13 changed files with 504 additions and 84 deletions

View File

@ -1,7 +1,10 @@
package components
import "io"
// Some general types
// Rectangle and its' suplements
type rect struct {
x int
y int
@ -18,3 +21,37 @@ type RectSetter interface {
SetPos(x, y int)
SetSize(w, h int)
}
// Mask
// Implementation dependent !!!
// Now for mt12232
type mask struct {
bits uint32
}
func (m *mask) Update(r rect) {
m.bits = 0
y0 := min(3, max(0, int(r.y/8)))
y1 := min(3, max(0, int((r.y+r.h-1)/8)))
x0 := min(1, max(0, int(r.x/61)))
x1 := min(1, max(0, int((r.x+r.w-1)/61)))
//log.Println(x0, y0, x1, y1)
for y := y0; y <= y1; y++ {
for x := x0; x <= x1; x++ {
m.bits |= (1 << (x*4 + y))
}
}
}
// Component - all components should implement this interface
type Component interface {
RectGetter
Draw() // Draw and flush
io.Closer
}

View File

@ -2,28 +2,23 @@ package components
import (
"fmt"
"io"
"gitea.unprism.ru/yotia/display-test/drawer"
)
type progressBar struct {
drawer drawer.Drawer // Drawer with dysplay
mask uint32 // Flush mask
mask mask // Flush mask
progress float64
rect rect // Rect
}
type ProgressBar interface {
RectGetter
Component
SetProgress(coef float64) // Value from 0 to 1
Draw() // For any cases
Clear() // Clear space of bar
Flush() // For those times like after SetPos
io.Closer
}
// Creation function
@ -45,34 +40,14 @@ func NewProgressBar(drawer drawer.Drawer, x, y, w, h int) (ProgressBar, error) {
rect: rect{x, y, w, h},
}
pb.updateMask()
pb.Draw()
pb.Flush()
pb.mask.Update(pb.rect)
// pb.Draw()
return &pb, nil
}
func (pb *progressBar) Flush() {
pb.drawer.GetDisplay().FlushByMask(pb.mask)
}
func (pb *progressBar) updateMask() {
pb.mask = 0
// Implementation dependent !!!
// Now for mt12232
y0 := min(3, max(0, int(pb.rect.y/8)))
y1 := min(3, max(0, int((pb.rect.y+pb.rect.h-1)/8)))
x0 := min(1, max(0, int(pb.rect.x/61)))
x1 := min(1, max(0, int((pb.rect.x+pb.rect.w-1)/61)))
//log.Println(x0, y0, x1, y1)
for y := y0; y <= y1; y++ {
for x := x0; x <= x1; x++ {
pb.mask |= (1 << (x*4 + y))
}
}
func (pb *progressBar) flush() {
pb.drawer.GetDisplay().FlushByMask(pb.mask.bits)
}
func (pb *progressBar) Close() error {
@ -92,6 +67,7 @@ func (pb *progressBar) Draw() {
pb.Clear()
pb.drawer.PutBar(pb.rect.x, pb.rect.y, pb.rect.x+pb.rect.w, pb.rect.y+pb.rect.h, 0xFF)
pb.drawBar()
pb.flush()
}
// Draw only bar(without frames)
@ -105,7 +81,7 @@ func (pb *progressBar) SetProgress(coef float64) {
pb.progress = max(0, min(1, coef))
pb.drawBar()
pb.Flush()
pb.flush()
}
func (pb *progressBar) GetPos() (int, int) {

85
components/text.go Normal file
View File

@ -0,0 +1,85 @@
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.Clear() // Assume that is draw is invoked string has been changed
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
t.updateW()
t.mask.Update(t.rect)
t.Draw()
}
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
}