Files
8bitMyGoUI/shop/quantity.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

95 lines
2.4 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 shop
import (
"fmt"
"log/slog"
"strconv"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type QuantityOptions struct {
// Min is the least; Max the most, zero meaning 99.
Min, Max int
Disabled bool
}
type QuantityResult struct {
Element *ui.Element
Changed bool
}
// Quantity steps − n + within its limits.
func Quantity(c *ui.Context, text string, value *int, o QuantityOptions) QuantityResult {
k := pixel.K(c)
lo, hi := o.Min, pixel.Or("Quantity Max", o.Max, 99)
if lo < 0 || hi < lo || *value < lo || *value > hi {
panic(fmt.Sprintf("shop: Quantity %d outside %d to %d", *value, lo, hi))
}
shell, base, st := pixel.Shell(c, func() *ui.Element {
return ui.Box(c).Focusable().Role(ui.RoleStepper).Label(text).Disabled(o.Disabled)
})
old, nw := *value, pixel.Measure(strconv.Itoa(hi))+6
if !base.IsDisabled() {
step := 0
// Enter and Space are taken, so clicks come from pointers.
pixel.Shortcut(base, 0, ui.KeyEnter)
pixel.Shortcut(base, 0, ui.KeySpace)
if pixel.Shortcut(base, 0, ui.KeyLeft) || pixel.Shortcut(base, 0, ui.KeyDown) {
step--
}
if pixel.Shortcut(base, 0, ui.KeyRight) || pixel.Shortcut(base, 0, ui.KeyUp) {
step++
}
if base.Clicked() {
if x, _, _ := base.PointerPosition(); int(x/k.Dot()) < 14 {
step--
} else if int(x/k.Dot()) >= 14+nw {
step++
}
}
// One at a time toward the limit, so nothing overflows.
for ; step > 0 && *value < hi; step-- {
*value++
}
for ; step < 0 && *value > lo; step++ {
*value--
}
if pixel.Shortcut(base, 0, ui.KeyHome) {
*value = lo
}
if pixel.Shortcut(base, 0, ui.KeyEnd) {
*value = hi
}
}
n := *value
base.Description(strconv.Itoa(n))
shell.Size(k.D(28+nw), k.D(pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
// A side at its limit fades; it goes no further.
for _, b := range []struct {
x int
sp *pixel.Sprite
on bool
}{{0, icon.Minus, n > lo}, {14 + nw, icon.Plus, n < hi}} {
ink := pixel.Faint
if b.on {
ink = pixel.Ink
}
g.Frame(b.x, 1, 14, 14, pixel.Raised, pixel.Face)
g.Sprite(b.x+1, 2, b.sp, 0, ink, 1)
}
s := strconv.Itoa(n)
g.Text(14+(nw-pixel.Measure(s))/2, 0, s, pixel.Ink, 1)
g.Finish(st)
})
res := QuantityResult{Element: shell, Changed: *value != old}
if res.Changed {
slog.Debug("8bit: quantity", "label", text, "value", *value)
}
return res
}