Files
8bitMyGoUI/tabletop/deck.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

62 lines
1.6 KiB
Go

package tabletop
import (
"fmt"
"log/slog"
"strconv"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type DeckOptions struct {
Disabled bool
}
type DeckResult struct {
Element ui.Element
// Drawn is a card taken this frame.
Drawn bool
}
// Deck is a face-down pile, counted; a click draws.
func Deck(c *ui.Context, text string, count int, o DeckOptions) DeckResult {
k := pixel.K(c)
// The badge holds three digits.
if count < 0 || count > 999 {
panic(fmt.Sprintf("tabletop: Deck of %d", count))
}
left := fmt.Sprintf(k.Msg("cards_left"), count)
shell, base, st := pixel.Shell(c, func() ui.Element {
return ui.Box(c).Focusable().Role(ui.RoleButton).Label(text).Description(left).Disabled(o.Disabled || count == 0)
})
res := DeckResult{Element: shell, Drawn: !base.IsDisabled() && base.Clicked()}
if res.Drawn {
slog.Debug("8bit: deck drew", "label", text, "left", count-1)
}
// A deeper pile shows more edges, six at most.
layers := min(count, 6)
shell.Size(k.D(cardW+7), k.D(cardH+7)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
if count == 0 {
g.Checker(0, 6, cardW, 1, pixel.Edge)
g.Checker(0, cardH+5, cardW, 1, pixel.Edge)
g.Checker(0, 6, 1, cardH, pixel.Edge)
g.Checker(cardW-1, 6, 1, cardH, pixel.Edge)
}
for i := range layers {
off := layers - 1 - i
drawCard(g, off, 6-off, Card{Rank: 1}, true)
}
if count > 0 {
n := strconv.Itoa(count)
w := pixel.MicroWidth(n) + 4
g.Rect(cardW+6-w, cardH, w, 7, pixel.Accent)
g.Micro(cardW+8-w, cardH+1, n, pixel.AccentInk, 1)
}
g.Finish(st)
})
return res
}