Files
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

65 lines
1.7 KiB
Go

package tabletop
import (
"math"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type CoinFlipOptions struct {
// Cue tosses it: a new cue flips, then it lands.
Cue int
}
// The coin's width and lift, a toss's frame by frame.
var (
turns = [...]int{24, 16, 6, 2, 6, 16, 24, 16, 6, 2, 6, 16}
toss = [...]int{0, 6, 11, 14, 16, 16, 14, 11, 7, 4, 2, 0}
)
const coinUp = 18
// CoinFlip tosses a coin on a new Cue.
func CoinFlip(c *ui.Context, text string, heads bool, o CoinFlipOptions) ui.Element {
k := pixel.K(c)
word := k.Msg("tails")
if heads {
word = k.Msg("heads")
}
e := ui.Column(c).Gap(k.D(2)).AlignItems(ui.Center).AlignSelf(ui.Start).Role(ui.RoleGroup).Label(text).Description(word)
// A toss turns twelve frames; reduced, it lands at once.
f := max(0, k.Shot(c, e, o.Cue, 15, len(turns)))
return e.Children(func() {
ui.Box(c).Size(k.D(26), k.D(26+coinUp)).Role(ui.RoleNone).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
w, y, up := 24, coinUp, heads
if f < len(turns) {
// Each half turn shows the other face.
w, y, up = turns[f], coinUp-toss[f], heads == (f/3%2 == 0)
}
for dy := -12; dy <= 12; dy++ {
half := int(math.Round(float64(w) / 2 * math.Sqrt(1-float64(dy*dy)/144)))
g.HLine(13-half, y+12+dy, 2*half, pixel.Edge)
if half > 1 {
g.HLine(14-half, y+12+dy, 2*half-2, pixel.Warn)
}
}
if w == 24 {
face := icon.Star
if up {
face = icon.Crown
}
g.Sprite(7, y+6, face, 0, pixel.Ink, 1)
}
})
if f >= len(turns) {
pixel.Text(c, word, pixel.TextOptions{})
} else {
ui.Box(c).Height(k.D(pixel.LineHeight))
}
})
}