Files
8bitMyGoUI/shop/shopitem.go
T
ZacharyZhang-NY 5605d785de Add shop components 436-450; Odometer exact at 15 fps
Fifteen shop components: PriceTag, Wallet, ShopItem, Cart, Receipt,
Checkout, CurrencyConvert, SaleBadge, Quantity, PricingTiers, Coupon,
DailyReward, BidTicker, TradeWindow and VendingMachine.

Receipt prints dot by dot with feed holes and a torn edge; Coupon cuts
real notches; SaleBadge is a sixteen-point starburst; VendingMachine
sells A1 to D4 by code and drops the snack into its tray. Sums are
checked against overflow, TradeWindow withdraws both confirms on any
change to an offer, and a disabled Checkout veils its inert content.

text.Odometer now counts digits as text, steps at 15 fps as S5 asks,
draws exact digits at rest and lands rolls that floats would stall.
2026-10-12 11:15:07 -04:00

70 lines
2.2 KiB
Go

package shop
import (
"fmt"
"log/slog"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/layout"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
// Ware is something for sale.
type Ware struct {
Name string
// Sprite is at most 16 dots square.
Sprite *pixel.Sprite
Price int
// Was is the price before a sale; zero means none.
Was int
Currency Currency
}
type ShopItemOptions struct {
// Short says the wallet cannot pay; BUY turns off.
Short bool
Disabled bool
}
type ShopItemResult struct {
Element *ui.Element
Bought bool
}
// ShopItem is a ware's sprite, name, price and BUY.
func ShopItem(c *ui.Context, w Ware, o ShopItemOptions) ShopItemResult {
k := pixel.K(c)
if w.Name == "" || w.Sprite == nil || w.Sprite.W > 16 || w.Sprite.H > 16 {
panic(fmt.Sprintf("shop: ShopItem %q needs a sprite of 16 dots at most", w.Name))
}
var res ShopItemResult
res.Element = layout.Panel(c, layout.PanelOptions{Style: pixel.Raised}, func() {
ui.Column(c).Width(k.D(96)).Gap(k.D(3)).AlignItems(ui.Center).Children(func() {
ui.Box(c).Size(k.D(32), k.D(32)).Draw(func(p *ui.Painter, r ui.Rect) {
k.Canvas(p, r).Sprite(16-w.Sprite.W, 16-w.Sprite.H, w.Sprite, 0, pixel.Ink, 2)
})
name := pixel.Wrap(w.Name, 92, 1)[0]
ui.Box(c).FillWidth().Height(k.D(pixel.LineHeight)).Role(ui.RoleText).Label(w.Name).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Text((g.W-pixel.Measure(name))/2, 0, name, pixel.Ink, 1)
})
PriceTag(c, w.Price, PriceTagOptions{Was: w.Was, Currency: w.Currency})
res.Bought = input.Button(c, k.Msg("buy"), input.ButtonOptions{Variant: input.Primary, Size: input.Small, Disabled: o.Disabled || o.Short, Label: k.Msg("buy") + " " + w.Name}).Clicked
// The line is kept, so shelves of cards stay even.
short := ""
if o.Short {
short = k.Msg("not_enough")
}
ui.Box(c).Size(k.D(pixel.Measure(short)), k.D(pixel.LineHeight)).Role(ui.RoleText).Label(short).Draw(func(p *ui.Painter, r ui.Rect) {
k.Canvas(p, r).Text(0, 0, short, pixel.Bad, 1)
})
})
})
if res.Bought {
slog.Debug("8bit: shop buy", "ware", w.Name)
}
return res
}