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>
80 lines
2.6 KiB
Go
80 lines
2.6 KiB
Go
package game
|
||
|
||
import (
|
||
"fmt"
|
||
"log/slog"
|
||
"strconv"
|
||
|
||
"github.com/egoist/mygo/ui"
|
||
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/data"
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
||
)
|
||
|
||
// Ware is an item for sale; Owned counts those held.
|
||
type Ware struct {
|
||
Item data.Item
|
||
Price, Owned int
|
||
}
|
||
|
||
type ShopOptions struct {
|
||
Disabled bool
|
||
}
|
||
|
||
type ShopResult struct {
|
||
Element ui.Element
|
||
// Bought and Sold are this frame's trades, or -1.
|
||
Bought, Sold int
|
||
}
|
||
|
||
// Shop buys at price, sells at half; dear wares fade.
|
||
func Shop(c *ui.Context, text string, wares []Ware, gold *int, o ShopOptions) ShopResult {
|
||
k := pixel.K(c)
|
||
if *gold < 0 {
|
||
panic(fmt.Sprintf("game: Shop gold %d", *gold))
|
||
}
|
||
name := 0
|
||
for _, w := range wares {
|
||
if w.Price < 0 || w.Owned < 0 || w.Item.Sprite == nil {
|
||
panic(fmt.Sprintf("game: ware %q costs %d, owned %d", w.Item.Name, w.Price, w.Owned))
|
||
}
|
||
name = max(name, pixel.Measure(w.Item.Name))
|
||
}
|
||
col := ui.Column(c).Gap(k.D(3)).Role(ui.RoleGroup).Label(text)
|
||
res := ShopResult{Element: col, Bought: -1, Sold: -1}
|
||
col.Children(func() {
|
||
CoinCounter(c, k.Msg("gold"), *gold, CoinCounterOptions{})
|
||
for i := range wares {
|
||
w := &wares[i]
|
||
dear := w.Price > *gold
|
||
ink := pixel.Ink
|
||
if dear {
|
||
ink = pixel.Faint
|
||
}
|
||
ui.Row(c).Gap(k.D(5)).AlignItems(ui.Center).Children(func() {
|
||
data.ItemSlot(c, w.Item, data.ItemSlotOptions{})
|
||
ui.Box(c).Width(k.D(name + 4)).Children(func() { pixel.Text(c, w.Item.Name, pixel.TextOptions{Slot: ink}) })
|
||
ui.Row(c).Width(k.D(40)).Gap(k.D(2)).AlignItems(ui.Center).Children(func() {
|
||
ui.Box(c).Size(k.D(12), k.D(12)).Draw(func(p *ui.Painter, r ui.Rect) {
|
||
k.Canvas(p, r).Sprite(0, 0, icon.Coin, 0, pixel.Ink, 1)
|
||
})
|
||
pixel.Text(c, strconv.Itoa(w.Price), pixel.TextOptions{Slot: ink})
|
||
})
|
||
ui.Box(c).Width(k.D(24)).Children(func() { pixel.Text(c, "×"+strconv.Itoa(w.Owned), pixel.TextOptions{Slot: pixel.Faint}) })
|
||
if input.Button(c, k.Msg("buy"), input.ButtonOptions{Size: input.Small, Label: k.Msg("buy") + " " + w.Item.Name, Disabled: o.Disabled || dear}).Clicked {
|
||
*gold, w.Owned, res.Bought = *gold-w.Price, w.Owned+1, i
|
||
}
|
||
if input.Button(c, k.Msg("sell"), input.ButtonOptions{Size: input.Small, Label: k.Msg("sell") + " " + w.Item.Name, Disabled: o.Disabled || w.Owned == 0}).Clicked {
|
||
*gold, w.Owned, res.Sold = *gold+w.Price/2, w.Owned-1, i
|
||
}
|
||
})
|
||
}
|
||
})
|
||
if res.Bought >= 0 || res.Sold >= 0 {
|
||
slog.Debug("8bit: shop", "label", text, "bought", res.Bought, "sold", res.Sold, "gold", *gold)
|
||
}
|
||
return res
|
||
}
|