Files
8bitMyGoUI/agent/costcoins.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

76 lines
1.9 KiB
Go

package agent
import (
"fmt"
"strconv"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type CostCoinsOptions struct {
// Symbol leads the amount; zero means "$".
Symbol string
}
// CostCoins stacks cost as gold dollars, silver dimes, bronze cents.
func CostCoins(c *ui.Context, text string, cents int, o CostCoinsOptions) ui.Element {
k := pixel.K(c)
if cents < 0 {
panic(fmt.Sprintf("agent: CostCoins of %d cents", cents))
}
sym := o.Symbol
if sym == "" {
sym = "$"
}
amount := fmt.Sprintf("%s%d.%02d", sym, cents/100, cents%100)
counts := [3]int{cents / 100, cents / 10 % 10, cents % 10}
var cols [3]int
stacks := 8
for i, n := range counts {
cols[i] = max(14, pixel.MicroWidth(strconv.Itoa(n))+2)
stacks += cols[i]
}
w := max(stacks, pixel.Measure(amount))
e := ui.Box(c).Size(k.D(w), k.D(66)).Role(ui.RoleImage).Label(text).Description(amount)
// A change drops each stack's top coin in.
drop := []int{-6, -3, -1, 0}[k.Shot(c, e, cents, 12, 3)]
return e.Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
x := (w - stacks) / 2
for i, n := range counts {
cx := x + (cols[i]-12)/2
g.HLine(cx, 42, 12, pixel.Shade)
top := min(n, 10)
for j := range top {
y := 38 - 3*j
if j == top-1 {
y += drop
}
coin(g, cx, y, i)
}
s := strconv.Itoa(n)
g.Micro(x+(cols[i]-pixel.MicroWidth(s))/2, 45, s, pixel.Ink, 1)
x += cols[i] + 4
}
g.Text((w-pixel.Measure(amount))/2, 50, amount, pixel.Ink, 1)
})
}
// coin draws one edge-on: 0 gold, 1 silver, 2 bronze.
func coin(g pixel.Canvas, x, y, metal int) {
fill := pixel.Warn
if metal == 1 {
fill = pixel.Face
}
g.HLine(x+1, y, 10, pixel.Edge)
g.Rect(x, y+1, 12, 2, pixel.Edge)
g.Rect(x+1, y+1, 10, 2, fill)
if metal == 2 {
g.Checker(x+1, y+1, 10, 2, pixel.Bad)
}
g.Dot(x+2, y+1, pixel.Lit)
g.HLine(x+1, y+3, 10, pixel.Edge)
}