Files
8bitMyGoUI/game/credits.go
T
ZacharyZhang-NYandClaude Opus 5.5 03e1f91706 Upgrade MyGo to v0.5.5
Elements are now pass-scoped value handles: tests that read bounds
after a frame bind a ui.Handle. pixel tells frames apart by the build
pass's root, since the Tester clock no longer moves on its own.

Fix the regressions the upgrade brought:
- Clipboard reads in input handlers go through c.Services().
- MyGo notes an overlay's opener after building it, so AutoFocus
  inside lost it; pixel.AutoFocus focuses through a Handle, and
  archtest bans the raw call. Dialogs hand the focus back again.
- Elements around a press stay hovered: Tooltip tells a press-given
  focus by focus arriving under the pointer.
- The web host moves the Tester clock with real time, at most 500 ms
  a call, and gives components the real clock.

The gallery's source index also accepts CRLF checkouts.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:31:08 -04:00

79 lines
1.9 KiB
Go

package game
import (
"strings"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
// Credit is a heading and the names under it.
type Credit struct {
Heading string
Names []string
}
type CreditsOptions struct {
// Width and Height are dots; zeros mean 160 by 96.
Width, Height int
// Cue rolls them again each time it changes.
Cue int
}
type CreditsResult struct {
Element ui.Element
// Done is true in the frame the roll ends.
Done bool
}
type rolledKey struct{}
// Credits roll up until the last name rests mid-window.
func Credits(c *ui.Context, text string, credits []Credit, o CreditsOptions) CreditsResult {
k := pixel.K(c)
w, h := pixel.Or("Credits Width", o.Width, 160), pixel.Or("Credits Height", o.Height, 96)
type line struct {
s string
slot pixel.Slot
}
var lines []line
var all []string
for i, cr := range credits {
if i > 0 {
lines = append(lines, line{})
}
lines = append(lines, line{cr.Heading, pixel.Warn})
all = append(all, cr.Heading+":")
for _, n := range cr.Names {
lines, all = append(lines, line{n, pixel.Ink}), append(all, n)
}
}
full := len(lines) * pixel.LineHeight
e := ui.Box(c).Role(ui.RoleGroup).Label(text).Description(strings.Join(all, " "))
// A dot a frame, until the last line centers.
end, roll := h/2+full-pixel.LineHeight/2, start(k, e, o.Cue)
top, step := 0, end
if !k.Reduce() {
step = k.Since(c, roll.at, 15, end)
top = h - step
} else {
h = full
}
// Done comes once a roll.
done := ui.Local(e, rolledKey{}, func() int { return -1 })
res := CreditsResult{Element: e, Done: step == end && *done != roll.run}
if res.Done {
*done = roll.run
}
e.Size(k.D(w), k.D(h)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Clip(0, 0, w, h, func() {
for i, l := range lines {
g.Text((w-pixel.Measure(l.s))/2, top+i*pixel.LineHeight, l.s, l.slot, 1)
}
})
})
return res
}