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.
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package account
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
|
)
|
|
|
|
type OptionCyclerOptions struct {
|
|
// Width is the value's room; zero fits the longest.
|
|
Width int
|
|
Disabled bool
|
|
}
|
|
|
|
type OptionCyclerResult struct {
|
|
Element *ui.Element
|
|
Changed bool
|
|
}
|
|
|
|
// OptionCycler steps ◀ VALUE ▶ through options, wrapping around.
|
|
func OptionCycler(c *ui.Context, text string, options []string, selected *int, o OptionCyclerOptions) OptionCyclerResult {
|
|
k := pixel.K(c)
|
|
choice("OptionCycler", selected, len(options))
|
|
longest := 0
|
|
for _, s := range options {
|
|
longest = max(longest, pixel.Measure(s))
|
|
}
|
|
w := pixel.Or("OptionCycler Width", o.Width, longest)
|
|
shell, base, st := pixel.Shell(c, func() *ui.Element {
|
|
return ui.Box(c).Focusable().Role(ui.RoleStepper).Label(text).Disabled(o.Disabled)
|
|
})
|
|
n, old := len(options), *selected
|
|
if !base.IsDisabled() {
|
|
step := 0
|
|
if pixel.Shortcut(base, 0, ui.KeyLeft) {
|
|
step--
|
|
}
|
|
// Enter and Space step on; taken, clicks come from pointers.
|
|
right, enter, space := pixel.Shortcut(base, 0, ui.KeyRight), pixel.Shortcut(base, 0, ui.KeyEnter), pixel.Shortcut(base, 0, ui.KeySpace)
|
|
if right || enter || space {
|
|
step++
|
|
}
|
|
// A click on the left half steps back, else on.
|
|
if base.Clicked() {
|
|
if x, _, _ := base.PointerPosition(); x < shell.Bounds().W/2 {
|
|
step--
|
|
} else {
|
|
step++
|
|
}
|
|
}
|
|
*selected = ((*selected+step)%n + n) % n
|
|
}
|
|
base.Description(options[*selected])
|
|
value := options[*selected]
|
|
total := w + 32
|
|
shell.Size(k.D(total), k.D(pixel.LineHeight+4)).Draw(func(p *ui.Painter, r ui.Rect) {
|
|
g := k.Canvas(p, r)
|
|
g.Text(2, 2, "◀", pixel.Accent, 1)
|
|
g.Text(16+(w-pixel.Measure(value))/2, 2, value, pixel.Ink, 1)
|
|
g.Text(total-2-pixel.Measure("▶"), 2, "▶", pixel.Accent, 1)
|
|
g.Finish(st)
|
|
})
|
|
res := OptionCyclerResult{Element: shell, Changed: *selected != old}
|
|
if res.Changed {
|
|
slog.Debug("8bit: option cycler", "label", text, "selected", *selected)
|
|
}
|
|
return res
|
|
}
|