Add text components 021-040 with demos and behavior tests

This commit is contained in:
ZacharyZhang-NY committed 2026-10-11 18:20:12 -04:00
1 parent 43098065bf
commit 59fde4096d
30 files changed
+1702 -7

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
// Package text draws words in the pixel font.
package text
import (
"fmt"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
// block is a w×h dot text box that fn draws.
func block(c *ui.Context, k pixel.Kit, s string, w, h int, fn func(g pixel.Canvas)) *ui.Element {
return ui.Box(c).Size(k.D(w), k.D(h)).Label(s).Role(ui.RoleText).Draw(func(p *ui.Painter, r ui.Rect) {
fn(k.Canvas(p, r))
})
}
// orDef is v, or def for zero; negative panics.
func orDef(name string, v, def int) int {
if v < 0 {
panic(fmt.Sprintf("text: %s %d is negative", name, v))
}
if v == 0 {
return def
}
return v
}
// scale is s, or 1 for zero; outside 1–4 panics.
func scale(s int) int {
s = orDef("Scale", s, 1)
if s > 4 {
panic(fmt.Sprintf("text: Scale %d outside 1–4", s))
}
return s
}
// slotOr is s, or def for the zero slot.
func slotOr(s, def pixel.Slot) pixel.Slot {
if s == pixel.Ink {
return def
}
return s
}
// each walks s rune by rune, with x in dots.
func each(s string, sc int, fn func(i int, r rune, x int)) {
i, x := 0, 0
for _, r := range s {
fn(i, r, x)
x += pixel.Measure(string(r)) * sc
i++
}
}
// noise is a seeded hash for jitter and scrambles.
func noise(seed, i, step int) uint32 {
x := uint32(seed*73856093 ^ i*19349663 ^ step*83492791)
x ^= x >> 13
x *= 0x5bd1e995
return x ^ x>>15
}