Files
8bitMyGoUI/overlay/dialoguebox.go
T
ZacharyZhang-NY b4b4d67b92 Audit keyboard, focus, veils, motion and clocks across all 500
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.
2026-10-12 16:03:04 -04:00

132 lines
3.5 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 overlay
import (
"fmt"
"log/slog"
"strings"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/layout"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
var more = pixel.MustSprite(`
#####
.###.
..#..
`)
type DialogueBoxOptions struct {
// Name labels the speaker's tab.
Name string
// Portrait draws the speaker; nil shows none.
Portrait *pixel.Sprite
// CPS is the typing speed; zero means 30.
CPS int
Disabled bool
}
type DialogueBoxResult struct {
Element *ui.Element
// Changed turns a page; Done passes the last one.
Changed, Done bool
}
type dialogueKey struct{}
// typing is the page, when it began, and if out.
type typing struct {
page int
since time.Time
out bool
}
// DialogueBox types pages; a press shows all, then turns.
func DialogueBox(c *ui.Context, pages []string, page *int, o DialogueBoxOptions) DialogueBoxResult {
k := pixel.K(c)
if *page < 0 || *page >= len(pages) {
panic(fmt.Sprintf("overlay: DialogueBox page %d of %d", *page, len(pages)))
}
cps := pixel.Or("DialogueBox CPS", o.CPS, 30)
if cps > 1000 {
panic(fmt.Sprintf("overlay: DialogueBox CPS %d over 1000", cps))
}
// S5 steps at 4–15 fps; fast typing shows more.
fps := max(4, min(cps, 15))
off := o.Disabled || pixel.Current(c).Inert
box := ui.Box(c).Role(ui.RoleText).Label(strings.TrimSpace(o.Name + " " + pages[*page])).FocusRing(false)
if !off {
box.Focusable()
}
pixel.Park(c, box, off)
st := ui.Local(box, dialogueKey{}, func() typing { return typing{page: -1} })
if st.page != *page {
*st = typing{page: *page, since: k.Now()}
}
res := DialogueBoxResult{Element: box}
// A press shows the whole page, then turns it.
if !off && box.Clicked() {
switch {
case !st.out:
st.out = true
case *page < len(pages)-1:
*page, res.Changed = *page+1, true
*st = typing{page: *page, since: k.Now()}
default:
res.Done = true
}
}
focus := box.FocusVisible()
box.DrawOver(func(p *ui.Painter, r ui.Rect) {
if focus {
g := k.Canvas(p, r)
g.FocusMark(0, 0, g.W, g.H)
}
}).Children(func() {
layout.Window(c, layout.WindowOptions{Title: o.Name}, func() {
ui.Row(c).Gap(k.D(8)).Children(func() {
if sp := o.Portrait; sp != nil {
ui.Box(c).Size(k.D(sp.W*2+6), k.D(sp.H*2+6)).Shrink(0).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Frame(0, 0, g.W, g.H, pixel.Sunken, pixel.Well)
g.Sprite(3, 3, sp, 0, pixel.Ink, 2)
})
}
words := ui.Box(c).Grow(1)
w, check := pixel.WrapWidth(c, words)
lines := pixel.Wrap(pages[*page], max(0, w-8), 0)
total := 0
for _, l := range lines {
total += len([]rune(l))
}
shown := total
if !st.out && !k.Reduce() {
shown = min(total, k.Since(c, st.since, fps, (total*fps+cps-1)/cps)*cps/fps)
}
st.out = shown == total
words.Height(k.D(max(2, len(lines)) * pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
check(r)
g := k.Canvas(p, r)
left, end := shown, 0
for i, l := range lines {
lr := []rune(l)
n := min(len(lr), max(0, left))
g.Text(0, i*pixel.LineHeight, string(lr[:n]), pixel.Ink, 1)
left -= len(lr)
end = pixel.Measure(string(lr[:n]))
}
if shown == total && g.Step(4)%4 < 2 {
g.Sprite(end+3, len(lines)*pixel.LineHeight-6, more, 0, pixel.Accent, 1)
}
})
})
})
})
if res.Changed || res.Done {
slog.Debug("8bit: dialogue", "name", o.Name, "page", *page, "done", res.Done)
}
return res
}