Files
8bitMyGoUI/input/knob.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

56 lines
1.5 KiB
Go

package input
import (
"math"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type KnobOptions struct {
// Min and Max are zero for 0 to 1.
Min, Max float64
// Step snaps the value; zero means none.
Step float64
Disabled bool
}
type KnobResult = SliderResult
type knobKey struct{}
// Knob turns through 16 angles by vertical drag or arrows.
func Knob(c *ui.Context, text string, value *float64, o KnobOptions) KnobResult {
k := pixel.K(c)
lo, hi := bounds(o.Min, o.Max)
r := rail{lo: lo, hi: hi, step: o.Step}
shell, base, st := pixel.Shell(c, func() *ui.Element {
return ui.Box(c).Focusable().Role(ui.RoleSlider).Label(text).Description(fmtValue(*value)).Disabled(o.Disabled)
})
old, set := *value, r.snap(value)
// raw is the unrounded value a drag carries.
raw := ui.Local(base, knobKey{}, func() float64 { return *value })
if !base.IsDisabled() {
if _, dy, held := base.Dragged(); held {
*raw = max(lo, min(hi, *raw-float64(dy/k.Dot())*(hi-lo)/64))
set(*raw)
} else {
*raw = *value
}
r.keys(base, value, set)
}
shell.Size(k.D(24), k.D(24)).Draw(func(p *ui.Painter, rc ui.Rect) {
g := k.Canvas(p, rc)
g.Disc(12, 12, 11, pixel.Edge)
g.Disc(12, 12, 10, pixel.Shade)
g.Disc(11, 11, 9, pixel.Face)
a := (-135 + 18*math.Round((*value-lo)/(hi-lo)*15)) * math.Pi / 180
x, y := 12+int(math.Round(7*math.Sin(a))), 12-int(math.Round(7*math.Cos(a)))
g.Line(12, 12, x, y, pixel.Accent)
g.Dot(x, y, pixel.Ink)
g.Finish(st)
})
return report(shell, *value != old, "knob", text, *value)
}