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

44 lines
1.3 KiB
Go

package game
import (
"fmt"
"math"
"strconv"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type HeartsOptions struct {
// PerRow wraps the hearts; zero means 10.
PerRow int
}
// Hearts shows hp of full hearts, in quarters.
func Hearts(c *ui.Context, text string, hp, full float64, o HeartsOptions) ui.Element {
k := pixel.K(c)
if full < 1 || full != math.Trunc(full) || !(hp >= 0 && hp <= full) || hp*4 != math.Trunc(hp*4) {
panic(fmt.Sprintf("game: Hearts %v of %v; hearts are whole, health in quarters", hp, full))
}
per := pixel.Or("Hearts PerRow", o.PerRow, 10)
n := int(full)
cols, rows := min(n, per), (n+per-1)/per
shown := strconv.FormatFloat(hp, 'f', -1, 64) + "/" + strconv.Itoa(n)
return ui.Box(c).Role(ui.RoleMeter).Label(text).Description(shown).Size(k.D(cols*11+1), k.D(rows*10+2)).Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
for i := range n {
x, y := i%per*11, i/per*10
// Quarters fill from the bottom left, clockwise.
q := min(4, max(0, int(hp*4)-4*i))
for j, part := range [][2]int{{0, 6}, {0, 0}, {6, 0}, {6, 6}} {
if j < q {
g.Clip(x+part[0], y+part[1], 6, 6, func() { g.Sprite(x, y, icon.Heart, 0, pixel.Bad, 1) })
}
}
g.Sprite(x, y, icon.HeartEmpty, 0, pixel.Edge, 1)
}
})
}