Files
8bitMyGoUI/code/spritesheet.go
T
ZacharyZhang-NY b4b4d67b92 Audit keyboard, focus, veils, motion and clocks across all 500
A cross-cutting pass, each fix with a test that failed first.

Veils: MyGo still hands a key to a base that turned off this frame,
so every element key now goes through pixel.Shortcut, which an
archtest enforces. Input handlers record and the build applies them
only while enabled; the editor turns away a veil-frame letter or
copy; value bases get a copy and keep only enabled turns. Popups
give a held focus back to their opener when the veil lifts.

Motion: S5 is enforced at the clock. Every Tick, Since, Step,
Toward and Shot runs at 4-15 fps; blinks keep 1 Hz on 4 fps steps,
fast animations step at 15, and drawn time is quantized to frames.

Clocks stepping back: Since holds at zero without overflow, and
k.Elapsed moves a stored start to now, so nothing goes negative,
runs backward or indexes out of range.

Also: logs count typed text instead of holding it, gallery names
use glyphs the font has, removable rows are keyed, components take
their own Options (archtest), and sweeps over all 508 demos check
veiled keys, clicks, wheels, the clock stepping back and names.
2026-10-12 16:03:04 -04:00

109 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package code
import (
"fmt"
"log/slog"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type SpriteSheetOptions struct {
// Scale is dots per sprite dot, 1–4; zero means 2.
Scale int
// Columns of frames; zero means 4.
Columns int
// FPS is the playback speed, 4–15; zero means 6.
FPS int
Disabled bool
}
type SpriteSheetResult struct {
Element *ui.Element
// Changed is a frame chosen or play pressed.
Changed bool
}
type sheetKey struct{}
// sheet is the frame play began on, and when.
type sheet struct {
from int
since time.Time
}
// SpriteSheet lays frames in a grid; playing steps through them.
func SpriteSheet(c *ui.Context, text string, sp *pixel.Sprite, frame *int, playing *bool, o SpriteSheetOptions) SpriteSheetResult {
k := pixel.K(c)
n, s := sp.Frames(), pixel.Or("SpriteSheet Scale", o.Scale, 2)
fps, cols := pixel.Or("SpriteSheet FPS", o.FPS, 6), min(n, pixel.Or("SpriteSheet Columns", o.Columns, 4))
if s > 4 || *frame < 0 || *frame >= n {
panic(fmt.Sprintf("code: SpriteSheet frame %d of %d at scale %d", *frame, n, s))
}
if fps < 4 || fps > 15 {
panic(fmt.Sprintf("code: SpriteSheet FPS %d outside 4–15", fps))
}
var res SpriteSheetResult
res.Element = ui.Row(c).Gap(k.D(8)).AlignItems(ui.Start)
sh := ui.Local(res.Element, sheetKey{}, func() sheet { return sheet{} })
// Playback is the content asked for, so it keeps time.
switch {
case !*playing:
sh.since = time.Time{}
case sh.since.IsZero():
sh.from, sh.since = *frame, k.Now()
}
if *playing {
per := time.Second / time.Duration(fps)
m := int(k.Elapsed(&sh.since) / per)
*frame = (sh.from + m) % n
k.At(c, sh.since.Add(time.Duration(m+1)*per))
}
cw, ch := sp.W*s+4, sp.H*s+4
res.Element.Children(func() {
old := *frame
shell, base, st, _ := pixel.Pad(c, text, cols, n, cw, ch, frame, o.Disabled)
base.Description(fmt.Sprintf("%d / %d", *frame+1, n))
if *frame != old {
res.Changed, sh.from, sh.since = true, *frame, k.Now()
}
at := *frame
shell.Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
for i := range n {
x, y := i%cols*cw, i/cols*ch
g.Frame(x, y, cw, ch, pixel.Flat, pixel.Well)
if i == at {
g.Cursor(x, y, cw, ch)
}
g.Sprite(x+2, y+2, sp, i, pixel.Ink, s)
}
g.Finish(st)
})
ui.Column(c).Gap(k.D(4)).AlignItems(ui.Center).Children(func() {
// The preview doubles the grid's scale, up to 4.
big := min(4, 2*s)
ui.Box(c).Size(k.D(sp.W*big+8), k.D(sp.H*big+8)).Role(ui.RoleImage).Label(fmt.Sprintf("%s %d", text, at+1)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Frame(0, 0, g.W, g.H, pixel.Sunken, pixel.Well)
g.Sprite(4, 4, sp, at, pixel.Ink, big)
})
sym := icon.Play
if *playing {
sym = icon.Pause
}
if input.Toggle(c, "", playing, input.ToggleOptions{Icon: sym, Label: k.Msg("play"), Disabled: o.Disabled}).Changed {
res.Changed = true
}
})
})
if res.Changed {
slog.Debug("8bit: sprite sheet", "label", text, "frame", *frame, "playing", *playing)
}
return res
}