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.
72 lines
2.3 KiB
Go
72 lines
2.3 KiB
Go
package input
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
|
)
|
|
|
|
type RangeSliderOptions struct {
|
|
Min, Max float64
|
|
Width int
|
|
Disabled bool
|
|
}
|
|
|
|
type RangeSliderResult = SliderResult
|
|
|
|
// RangeSlider sets *low and *high with two uncrossing knobs.
|
|
func RangeSlider(c *ui.Context, text string, low, high *float64, o RangeSliderOptions) RangeSliderResult {
|
|
k := pixel.K(c)
|
|
lo, hi := bounds(o.Min, o.Max)
|
|
w := pixel.Or("RangeSlider Width", o.Width, 96)
|
|
was := [2]float64{*low, *high}
|
|
e := ui.Box(c).Size(k.D(w), k.D(12)).Role(ui.RoleGroup).Label(text)
|
|
e.Draw(func(p *ui.Painter, r ui.Rect) {
|
|
g := k.Canvas(p, r)
|
|
a, b := knobAt(*low, lo, hi, g.W-6), knobAt(*high, lo, hi, g.W-6)
|
|
g.Frame(0, 4, g.W, 4, pixel.Sunken, pixel.Well)
|
|
g.Rect(a+3, 5, b-a, 2, pixel.Accent)
|
|
}).Children(func() {
|
|
knob(c, k, e, low, rail{lo: lo, hi: *high, label: text + " low", disabled: o.Disabled}, lo, hi, w)
|
|
knob(c, k, e, high, rail{lo: *low, hi: hi, label: text + " high", disabled: o.Disabled}, lo, hi, w)
|
|
})
|
|
if *low > *high {
|
|
panic(fmt.Sprintf("input: RangeSlider low %v above high %v", *low, *high))
|
|
}
|
|
changed := was != [2]float64{*low, *high}
|
|
if changed {
|
|
slog.Debug("8bit: range slider", "label", text, "low", *low, "high", *high)
|
|
}
|
|
return SliderResult{Element: e, Changed: changed}
|
|
}
|
|
|
|
// knob is one draggable end, fenced by r.lo and r.hi.
|
|
func knob(c *ui.Context, k pixel.Kit, track *ui.Element, v *float64, r rail, lo, hi float64, w int) {
|
|
shell, base, st := pixel.Shell(c, func() *ui.Element {
|
|
return ui.Box(c).Focusable().Role(ui.RoleSlider).Label(r.label).Description(fmtValue(*v)).Disabled(r.disabled)
|
|
})
|
|
set := func(x float64) { *v = max(r.lo, min(r.hi, x)) }
|
|
if !base.IsDisabled() {
|
|
if _, _, held := base.Dragged(); held {
|
|
px, _, _ := base.PointerPosition()
|
|
t := track.Bounds()
|
|
set(lo + float64((base.Bounds().X+px-t.X)/max(1, t.W))*(hi-lo))
|
|
}
|
|
step := (hi - lo) / 20
|
|
if pixel.Shortcut(base, 0, ui.KeyLeft) || pixel.Shortcut(base, 0, ui.KeyDown) {
|
|
set(*v - step)
|
|
}
|
|
if pixel.Shortcut(base, 0, ui.KeyRight) || pixel.Shortcut(base, 0, ui.KeyUp) {
|
|
set(*v + step)
|
|
}
|
|
}
|
|
shell.Absolute().Left(k.D(knobAt(*v, lo, hi, w-6))).Top(k.D(1)).Size(k.D(6), k.D(10)).Draw(func(p *ui.Painter, r ui.Rect) {
|
|
g := k.Canvas(p, r)
|
|
g.Frame(0, 0, 6, 10, pixel.Raised, pixel.Face)
|
|
g.Finish(st)
|
|
})
|
|
}
|