Files
8bitMyGoUI/input/switch.go
T
ZacharyZhang-NY 03680980da Add data components 151-175; clear MyGo row highlights
Lists, tables, trees, grids, inventories and records in data/.
Outline and GridView build on ui.List: ui.Outline strokes its arrows
and ui.GridView spaces and tints items. The pixel theme keeps MyGo's
row highlights clear so scrolled rows stay on whole dots; off lists
take a veil against the wheel. Adds Canvas.Silhouette, SwitchOptions
Label and exports Rarity.Slot.
2026-10-11 22:29:07 -04:00

52 lines
1.3 KiB
Go

package input
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type SwitchOptions struct {
Disabled bool
// Label names it for assistive technology; zero uses text.
Label string
}
type SwitchResult struct {
Element *ui.Element
Changed bool
}
// Switch is a lever on an OFF/ON plate.
func Switch(c *ui.Context, text string, on *bool, o SwitchOptions) SwitchResult {
k := pixel.K(c)
shell, base, st := pixel.Shell(c, func() *ui.Element { return ui.SwitchBase(c, on).Label(label(o.Label, text)).Disabled(o.Disabled) })
shell.Size(k.D(32+pixel.Measure(text)), k.D(pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Frame(0, 1, 27, 14, pixel.Raised, pixel.Shade)
g.Micro(3, 2, "OFF", pixel.Faint, 1)
g.Micro(17, 2, "ON", pixel.Faint, 1)
x, lx, lit := 6, 3, "OFF"
if *on {
x, lx, lit = 19, 17, "ON"
}
g.Micro(lx, 2, lit, pixel.Ink, 1)
g.Rect(x, 8, 3, 5, pixel.Edge)
g.Rect(x-1, 8, 5, 3, pixel.Edge)
g.Rect(x, 9, 3, 1, pixel.Lit)
ink := pixel.Ink
if st.Disabled {
ink = pixel.Faint
}
g.Text(32, 0, text, ink, 1)
g.Finish(st)
})
res := SwitchResult{Element: shell, Changed: base.Changed()}
if res.Changed {
slog.Debug("8bit: switch", "label", text, "on", *on)
}
return res
}