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>
79 lines
2.2 KiB
Go
79 lines
2.2 KiB
Go
package game
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
|
)
|
|
|
|
// Enemy is a target; at zero HP, a shadow.
|
|
type Enemy struct {
|
|
Name string
|
|
Sprite *pixel.Sprite
|
|
HP, MaxHP int
|
|
}
|
|
|
|
type TargetSelectorOptions struct {
|
|
Disabled bool
|
|
}
|
|
|
|
type TargetSelectorResult struct {
|
|
Element ui.Element
|
|
Changed bool
|
|
// Chosen is the target confirmed this frame, or -1.
|
|
Chosen int
|
|
}
|
|
|
|
// foeW and foeH are a target's cell: cursor, sprite, name.
|
|
const foeW, foeH = 40, 12 + 24 + 4 + pixel.LineHeight
|
|
|
|
// TargetSelector rows enemies under a bobbing ▼; arrows choose.
|
|
func TargetSelector(c *ui.Context, text string, enemies []Enemy, selected *int, o TargetSelectorOptions) TargetSelectorResult {
|
|
k := pixel.K(c)
|
|
if len(enemies) == 0 || *selected < 0 || *selected >= len(enemies) {
|
|
panic(fmt.Sprintf("game: TargetSelector target %d of %d", *selected, len(enemies)))
|
|
}
|
|
for _, en := range enemies {
|
|
if en.Sprite == nil {
|
|
panic(fmt.Sprintf("game: enemy %q has no sprite", en.Name))
|
|
}
|
|
amount("enemy "+en.Name+" HP", en.HP, en.MaxHP)
|
|
}
|
|
old := *selected
|
|
shell, base, st, chosen := pixel.Pad(c, text, len(enemies), len(enemies), foeW, foeH, selected, o.Disabled)
|
|
res := TargetSelectorResult{Element: shell, Changed: *selected != old, Chosen: chosen}
|
|
en := enemies[*selected]
|
|
base.Description(en.Name + " " + ratio(en.HP, en.MaxHP))
|
|
if res.Changed || res.Chosen >= 0 {
|
|
slog.Debug("8bit: target", "label", text, "enemy", en.Name, "chosen", res.Chosen)
|
|
}
|
|
sel, shown := *selected, append([]Enemy(nil), enemies...)
|
|
shell.Draw(func(p *ui.Painter, r ui.Rect) {
|
|
g := k.Canvas(p, r)
|
|
for i, en := range shown {
|
|
x := i * foeW
|
|
sx := x + (foeW-2*en.Sprite.W)/2
|
|
if en.HP == 0 {
|
|
g.Silhouette(sx, 12, en.Sprite, 0, pixel.Shade, 2)
|
|
} else {
|
|
g.Sprite(sx, 12, en.Sprite, 0, pixel.Ink, 2)
|
|
}
|
|
ink := pixel.Ink
|
|
if en.HP == 0 {
|
|
ink = pixel.Faint
|
|
}
|
|
name := pixel.Wrap(en.Name, foeW-2, 1)[0]
|
|
g.Text(x+(foeW-pixel.Measure(name))/2, 12+24+4, name, ink, 1)
|
|
// The cursor bobs a dot; reduced, it holds.
|
|
if i == sel {
|
|
g.Text(x+(foeW-pixel.Measure("▼"))/2, g.Step(4)%2-4, "▼", pixel.Accent, 1)
|
|
}
|
|
}
|
|
g.Finish(st)
|
|
})
|
|
return res
|
|
}
|