Files
8bitMyGoUI/media/tuner.go
T
ZacharyZhang-NY 0e21870b64 Add media components 346-365; input.SlideBase, guarded Pad
An audio player with transport, scrubber and volume; an equalizer and
a visualizer; a playlist; a cassette, cartridge and record that turn
while they play and hold where they stop; piano keys, a step sequencer
and a tracker you can type into; a waveform, a 16:9 video frame, an
image compare, a gallery, a filmstrip, a record button and an FM tuner.

input exports SlideBase, the undrawn rail its sliders share; the
gallery treats exported Bases as building blocks, not components.
pixel's micro font gains '#' for notes like C#4. pixel.Pad and the
slide rail read input only while their base is enabled, so Inert or a
fresh Disabled stops a drag or a key already sent. AGENTS records why
a drawn container keeps the DISABLED description: MyGo paints a
disabled subtree at half opacity.
2026-10-12 06:17:12 -04:00

63 lines
1.7 KiB
Go

package media
import (
"fmt"
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type TunerOptions struct {
Disabled bool
}
type TunerResult struct {
Element *ui.Element
Changed bool
}
// Tuner sets *mhz on the FM dial by tenths.
func Tuner(c *ui.Context, text string, mhz *float64, o TunerOptions) TunerResult {
k := pixel.K(c)
const lo, hi = 87.5, 108.0
if !(*mhz >= lo && *mhz <= hi) {
panic(fmt.Sprintf("media: Tuner %v MHz outside FM", *mhz))
}
var res TunerResult
// The glass holds the scale; the scale is the slider.
res.Element = ui.Box(c).Size(k.D(176), k.D(32)).PaddingX(k.D(8)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Frame(0, 0, 176, 32, pixel.Sunken, pixel.Well)
g.Dither(2, 2, 172, 28, pixel.Well, pixel.Warn, 3)
g.HLine(8, 24, 162, pixel.Ink)
g.Micro(150, 26, "MHZ", pixel.Faint, 1)
}).Children(func() {
shell, st, changed := input.SlideBase(c, text, mhz, input.RailOptions{Min: lo, Max: hi, Step: 0.1, Disabled: o.Disabled})
res.Changed = changed
at := *mhz
shell.Size(k.D(160), k.D(32)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
// The dot under the pointer is the frequency it sets.
x := func(f float64) int { return int((f - lo) / (hi - lo) * 160) }
for f := 88; f <= 108; f++ {
h := 3
if f%4 == 0 {
h = 6
s := fmt.Sprint(f)
g.Micro(x(float64(f))-pixel.MicroWidth(s)/2, 4, s, pixel.Ink, 1)
}
g.VLine(x(float64(f)), 24-h, h, pixel.Ink)
}
g.Rect(x(at), 3, 2, 26, pixel.Bad)
g.Finish(st)
})
})
if res.Changed {
slog.Debug("8bit: tuner", "label", text, "mhz", *mhz)
}
return res
}