Files
8bitMyGoUI/tabletop/deck.go
T
ZacharyZhang-NY 81003b21ee Add tabletop components 486-500 and ten game icons
Fifteen pieces of a games night: Dice, DiceTray, PlayingCard, CardHand,
Deck, Chessboard, GoBoard, MahjongTile, Domino, Roulette, CoinFlip,
BingoCard, TicTacToe, ScoreTrack and Meeple. Component number 500 is
a meeple, which seems right.

Dice tumble, coins turn and wheels spin on a cue, a frame at a time,
and land at once under Reduce Motion. Hands lift the card you point
at and play the one drawn on top; pieces and stones pick the lighter
of Lit and Ink for White, so they read in every palette; the computer
plays a fair O; FREE is a star in every language.

New 12x12 icons: Spade, Club, Diamond, Meeple, King, Queen, Rook,
Bishop, Knight and Pawn.
2026-10-12 14:03:58 -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
}