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

72 lines
2.3 KiB
Go

package account
import (
"fmt"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/display"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/layout"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/text"
)
// Stat is a number out of its most, as HP.
type Stat struct {
Name string
Value, Max int
}
// Profile is a player as their card shows them.
type Profile struct {
Name, Title string
Level int
Stats []Stat
}
type ProfileCardOptions struct {
// Width is in dots; zero means 180.
Width int
}
// ProfileCard shows a player: face, name, level, title and stats.
func ProfileCard(c *ui.Context, p Profile, o ProfileCardOptions) ui.Element {
k := pixel.K(c)
if p.Level < 1 {
panic(fmt.Sprintf("account: ProfileCard level %d", p.Level))
}
w := pixel.Or("ProfileCard Width", o.Width, 180)
// Columns fit the widest name and value, so bars align.
nw, vw := 0, 0
for _, s := range p.Stats {
if s.Max < 1 || s.Value < 0 || s.Value > s.Max {
panic(fmt.Sprintf("account: ProfileCard stat %+v", s))
}
nw, vw = max(nw, pixel.MicroWidth(s.Name)), max(vw, pixel.MicroWidth(fmt.Sprintf("%d/%d", s.Value, s.Max)))
}
bx, bw := nw+4, w-12-nw-vw-8
return layout.Panel(c, layout.PanelOptions{Style: pixel.Raised, Padding: 6}, func() {
ui.Row(c).Gap(k.D(6)).AlignItems(ui.Center).Children(func() {
display.Avatar(c, p.Name, display.AvatarOptions{Scale: 2})
ui.Column(c).Gap(k.D(2)).Children(func() {
text.ShadowText(c, p.Name, text.ShadowTextOptions{})
caption(c, k, fmt.Sprintf("LV %d", p.Level), pixel.Accent)
pixel.Text(c, p.Title, pixel.TextOptions{Slot: pixel.Faint})
})
})
for _, s := range p.Stats {
v := fmt.Sprintf("%d/%d", s.Value, s.Max)
ui.Box(c).Size(k.D(w-12), k.D(9)).Role(ui.RoleMeter).Label(s.Name).Description(v).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
g.Micro(0, 2, s.Name, pixel.Faint, 1)
g.Frame(bx, 1, bw, 7, pixel.Sunken, pixel.Well)
// Floats, so huge stats cannot overflow.
if n := int(float64(bw-4) * float64(s.Value) / float64(s.Max)); n > 0 {
g.Rect(bx+2, 3, n, 3, pixel.Good)
}
g.Micro(g.W-pixel.MicroWidth(v), 2, v, pixel.Ink, 1)
})
}
}).Width(k.D(w)).Gap(k.D(4)).Role(ui.RoleGroup).Label(p.Name)
}