Files
8bitMyGoUI/input/checkbox.go
T
ZacharyZhang-NY c8d058799d Add project components 421-435; Checkbox turns only when on
Fifteen project components: QuestBoard, TaskItem, TaskList, Milestones,
Burndown, Pomodoro, Priority, AssigneePicker, LabelPicker, TimeTracker,
Roadmap, SprintPanel, IssueCard, Checklist and WorkloadBar.

QuestBoard is one base that draws TODO, DOING and DONE; arrows move,
Alt with an arrow carries a card, and a drag carries it live. Priority
draws 12x12 bars, and Critical is on fire. Pomodoro banks time on PAUSE
and the tomato sleeps through the rest. Extreme inputs fail fast or
saturate instead of wrapping.

MyGo's CheckboxBase turns its value before Disabled is set, so
input.Checkbox now hands it a copy and keeps only turns of an enabled
base. The home scene test checks the sky row, not one pixel that
falling snow can cover.
2026-10-12 10:25:07 -04:00

75 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package input
import (
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
var (
check = pixel.MustSprite(`
.....#
....##
#..##.
####..
.##...
`)
dash = pixel.MustSprite(`
####
`)
)
type CheckboxOptions struct {
Disabled bool
}
type CheckboxResult struct {
Element *ui.Element
Changed bool
}
// Checkbox is a labeled 8×8 box with a hand-placed check.
func Checkbox(c *ui.Context, text string, checked *bool, o CheckboxOptions) CheckboxResult {
k := pixel.K(c)
// The base turns before it learns it is off.
on := *checked
shell, base, st := pixel.Shell(c, func() *ui.Element { return ui.CheckboxBase(c, &on).Label(text).Disabled(o.Disabled) })
changed := on != *checked && !base.IsDisabled()
if changed {
*checked = on
}
mark := check
if !*checked {
mark = nil
}
shell.Size(k.D(12+pixel.Measure(text)), k.D(pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
box(k.Canvas(p, r), text, mark, st)
})
res := CheckboxResult{Element: shell, Changed: changed}
if res.Changed {
slog.Debug("8bit: checkbox", "label", text, "checked", *checked)
}
return res
}
// box draws the 8×8 box, its mark and the label.
func box(g pixel.Canvas, text string, mark *pixel.Sprite, st pixel.State) {
y := (pixel.LineHeight - 8) / 2
fill := pixel.Well
if st.Hover {
fill = pixel.Lit
}
g.Frame(0, y, 8, 8, pixel.Sunken, fill)
if mark != nil {
g.Sprite((8-mark.W)/2, y+(8-mark.H)/2, mark, 0, pixel.Accent, 1)
}
ink := pixel.Ink
if st.Disabled {
ink = pixel.Faint
}
g.Text(12, 0, text, ink, 1)
g.Finish(st)
}