Add navigation components 131-150; pixel Pad, Finish, Park, Since

This commit is contained in:
ZacharyZhang-NY committed 2026-10-11 21:27:09 -04:00
1 parent bd3877e1b1
commit f587d25a5a
63 files changed
+2776 -87

No files matched your search

+12
View File
@@ -55,6 +55,18 @@ func (k Kit) Tick(c *ui.Context, fps int) int {
return n
}
// Since counts steps from t at fps, up to done.
func (k Kit) Since(c *ui.Context, t time.Time, fps, done int) int {
if t.IsZero() {
return done
}
n := int(k.now.Sub(t) * time.Duration(fps) / time.Second)
if n < done {
k.Tick(c, fps)
}
return min(n, done)
}
// frameAt is the frame at t and the next wait.
func frameAt(t time.Time, fps int) (int, time.Duration) {
checkFPS(fps)
+27 -13
View File
@@ -97,25 +97,29 @@ func Shell(c *ui.Context, base func() *ui.Element) (shell, b *ui.Element, st Sta
b.Disabled(true)
}
off := own || inert
// MyGo keeps focus and keys on a newly disabled element.
parked := ui.Local(b, parkKey{}, func() bool { return false })
switch {
case off && b.Focused():
c.Root().Focus()
*parked = true
case !off && *parked:
// Focus comes back unless something else took it.
if root := c.Root(); root.Focused() || !root.FocusWithin() {
b.Focus()
}
*parked = false
}
Park(c, b, off)
st = State{Hover: b.Hovered(), Press: b.Pressed() && !off, Focus: b.FocusVisible(), Disabled: own}
return shell, b, st
}
type parkKey struct{}
// Park moves focus off e while off, back once on.
func Park(c *ui.Context, e *ui.Element, off bool) {
parked := ui.Local(e, parkKey{}, func() bool { return false })
switch {
case off && e.Focused():
c.Root().Focus()
*parked = true
case !off && *parked:
// Focus comes back unless something else took it.
if root := c.Root(); root.Focused() || !root.FocusWithin() {
e.Focus()
}
*parked = false
}
}
// Hand is the pointing cursor of focus and menus.
var Hand = MustSprite(`
.eee......
@@ -126,6 +130,16 @@ var Hand = MustSprite(`
.eeee.....
`)
// Finish dithers out a disabled control and marks its focus.
func (g Canvas) Finish(st State) {
if st.Disabled {
g.Checker(0, 0, g.W, g.H, Face)
}
if st.Focus {
g.FocusMark(0, 0, g.W, g.H)
}
}
// FocusMark rings a box with dots and points a hand.
func (g Canvas) FocusMark(x, y, w, h int) {
rx, ry, rw, rh := x-2, y-2, w+4, h+4
+2
View File
@@ -15,6 +15,7 @@ var messages = map[Locale]map[string]string{
"hold": "HOLD TO CONFIRM", "del": "DEL", "end": "END", "lives": "+30 LIVES!",
"retry": "RETRY", "stage_clear": "STAGE CLEAR!", "level_up": "LEVEL UP!", "achievement": "ACHIEVEMENT UNLOCKED",
"combo": "COMBO!", "saving": "SAVING", "saved": "SAVED", "go": "GO!", "wait": "PLEASE WAIT",
"prev": "PREVIOUS", "next": "NEXT", "back": "BACK", "forward": "FORWARD", "history": "HISTORY",
},
Zh: {
"ok": "确定", "cancel": "取消", "yes": "是", "no": "否", "close": "关闭",
@@ -28,6 +29,7 @@ var messages = map[Locale]map[string]string{
"hold": "长按确认", "del": "删", "end": "完", "lives": "命数 +30!",
"retry": "重试", "stage_clear": "过关!", "level_up": "升级!", "achievement": "成就解锁",
"combo": "连击!", "saving": "存档中", "saved": "已存档", "go": "开始!", "wait": "请稍候",
"prev": "上一页", "next": "下一页", "back": "后退", "forward": "前进", "history": "历史",
},
}
+43
View File
@@ -0,0 +1,43 @@
package pixel
import "github.com/egoist/mygo/ui"
// Pad is a focusable grid; it returns the picked cell.
func Pad(c *ui.Context, text string, cols, n, cw, ch int, cur *int, disabled bool) (shell, base *ui.Element, st State, picked int) {
k := K(c)
shell, base, st = Shell(c, func() *ui.Element {
return ui.Box(c).Focusable().Role(ui.RoleGroup).Label(text).Disabled(disabled)
})
shell.Size(k.D(cols*cw), k.D((n+cols-1)/cols*ch))
for _, m := range []struct {
key ui.Key
to func() int
}{
{ui.KeyLeft, func() int { return *cur - 1 }}, {ui.KeyRight, func() int { return *cur + 1 }},
{ui.KeyUp, func() int { return *cur - cols }}, {ui.KeyDown, func() int { return *cur + cols }},
{ui.KeyHome, func() int { return 0 }}, {ui.KeyEnd, func() int { return n - 1 }},
} {
if to := m.to(); base.Shortcut(0, m.key) && to >= 0 && to < n {
*cur = to
}
}
picked = -1
enter, space := base.Shortcut(0, ui.KeyEnter), base.Shortcut(0, ui.KeySpace)
if enter || space {
picked = *cur
}
// Enter and Space are taken, so clicks come from pointers.
if base.Clicked() {
x, y, _ := base.PointerPosition()
if col, i := int(x/k.D(cw)), int(y/k.D(ch))*cols+int(x/k.D(cw)); x >= 0 && y >= 0 && col < cols && i < n {
*cur, picked = i, i
}
}
return shell, base, st, picked
}
// Cursor lights a cell with an Accent ring.
func (g Canvas) Cursor(x, y, w, h int) {
g.Rect(x, y, w, h, Accent)
g.Rect(x+1, y+1, w-2, h-2, Lit)
}