Files
8bitMyGoUI/game/levelupscreen.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

84 lines
2.7 KiB
Go

package game
import (
"fmt"
"log/slog"
"strconv"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/text"
)
// Attribute is a stat raised at a level up.
type Attribute struct {
Name string
Base int
// Added is the points put in it this time.
Added int
}
type LevelUpScreenOptions struct {
Disabled bool
}
type LevelUpScreenResult struct {
Element ui.Element
Changed bool
Done bool
}
// back is ◀, the triangle icon turned around.
var back = icon.TriangleRight.Mirror()
// LevelUpScreen spends points on attributes with ◀ ▶, then DONE.
func LevelUpScreen(c *ui.Context, title string, attrs []Attribute, points *int, o LevelUpScreenOptions) LevelUpScreenResult {
k := pixel.K(c)
if *points < 0 {
panic(fmt.Sprintf("game: LevelUpScreen has %d points", *points))
}
name := 0
for _, a := range attrs {
if a.Base < 0 || a.Added < 0 {
panic(fmt.Sprintf("game: attribute %q is %d%+d", a.Name, a.Base, a.Added))
}
name = max(name, pixel.Measure(a.Name))
}
col := ui.Column(c).Gap(k.D(4)).Role(ui.RoleGroup).Label(title)
res := LevelUpScreenResult{Element: col}
col.Children(func() {
ui.Row(c).Gap(k.D(10)).AlignItems(ui.End).Children(func() {
text.ShadowText(c, k.Msg("level_up"), text.ShadowTextOptions{Scale: 2, Slot: pixel.Warn})
pixel.Text(c, fmt.Sprintf(k.Msg("points"), *points), pixel.TextOptions{Slot: pixel.Accent})
})
for i := range attrs {
a := &attrs[i]
ui.Row(c).Gap(k.D(4)).AlignItems(ui.Center).Children(func() {
ui.Box(c).Width(k.D(name + 6)).Children(func() { pixel.Text(c, a.Name, pixel.TextOptions{}) })
if input.IconButton(c, back, a.Name+" "+k.Msg("down"), input.IconButtonOptions{Size: input.Small, Disabled: o.Disabled || a.Added == 0}).Clicked {
a.Added, *points, res.Changed = a.Added-1, *points+1, true
}
ui.Box(c).Width(k.D(40)).Children(func() {
ui.Row(c).Children(func() {
pixel.Text(c, strconv.Itoa(a.Base+a.Added), pixel.TextOptions{})
if a.Added > 0 {
pixel.Text(c, "+"+strconv.Itoa(a.Added), pixel.TextOptions{Slot: pixel.Good})
}
})
})
if input.IconButton(c, icon.TriangleRight, a.Name+" "+k.Msg("up"), input.IconButtonOptions{Size: input.Small, Disabled: o.Disabled || *points == 0}).Clicked {
a.Added, *points, res.Changed = a.Added+1, *points-1, true
}
})
}
res.Done = input.Button(c, k.Msg("done"), input.ButtonOptions{Variant: input.Primary, Disabled: o.Disabled}).Clicked
})
if res.Changed {
slog.Debug("8bit: level up", "label", title, "points", *points)
}
return res
}