Files
8bitMyGoUI/feedback/errorstate.go
T
ZacharyZhang-NY 1f3d38110e One component per file; GAME OVER offers CONTINUE
PRD G1 asks for one component per file. Eight files held two or
three; their eleven extra components move to files of their own,
helpers stay where both sides use them, and an archtest now fails
any file that holds two.

PRD 4.4 and the request say an error always offers CONTINUE.
ErrorState's button read RETRY; it now reads CONTINUE, under the
CONTINUE? countdown, on the crash screen and the lost page alike.
2026-10-12 16:50:17 -04:00

50 lines
1.6 KiB
Go

package feedback
import (
"log/slog"
"strconv"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/display"
"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"
)
type ErrorStateOptions struct {
Disabled bool
}
type ErrorStateResult struct {
Element *ui.Element
Continue bool
}
type startKey struct{}
// ErrorState is GAME OVER, a CONTINUE? countdown, and CONTINUE.
func ErrorState(c *ui.Context, message string, o ErrorStateOptions) ErrorStateResult {
k := pixel.K(c)
col := ui.Column(c).Gap(k.D(8)).Padding(k.D(16)).AlignItems(ui.Center).Role(ui.RoleGroup).Label(k.Msg("game_over"))
start := ui.Local(col, startKey{}, func() time.Time { return k.Now() })
left := max(0, 9-max(0, int(k.Now().Sub(*start)/time.Second)))
if left > 0 {
k.Tick(c, 10)
}
res := ErrorStateResult{Element: col}
col.Children(func() {
text.ShadowText(c, k.Msg("game_over"), text.ShadowTextOptions{Slot: pixel.Bad, Scale: 3})
pixel.Text(c, message, pixel.TextOptions{Slot: pixel.Faint, Wrap: true, MaxLines: 6}).Width(k.D(min(pixel.Measure(message), 280)))
display.Icon(c, icon.Skull, display.IconOptions{Scale: 3})
text.ShadowText(c, k.Msg("continue")+" "+strconv.Itoa(left), text.ShadowTextOptions{Slot: pixel.Accent, Scale: 2})
res.Continue = input.Button(c, k.Msg("continue_game"), input.ButtonOptions{Variant: input.Primary, Disabled: o.Disabled}).Clicked
})
if res.Continue {
slog.Debug("8bit: continue", "left", left)
}
return res
}