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

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
}