NSGo Game

a11y

game/a11y provides accessibility building blocks built on NSGo's public API: live dialogue and directional SFX captions, color-blind-safe palettes with CVD simulation and WCAG contrast helpers, persisted assist options, and screen-reader-ready native menus.

import "github.com/nsgo-dev/game/packages/game/a11y"

Persisted assist options (Assist)

NewAssist(cx, key) stores accessibility preferences in core.Storage as reactive values so changes persist across launches and update NSGo views automatically:

assist := a11y.NewAssist(cx, "a11y")

// In game.Loop.Update, scale simulation dt by GameSpeed (0.25..1.0):
loop := &game.Loop{
	Update: func(dt time.Duration) {
		sim.Step(assist.ScaleDT(dt))
	},
}

Available persisted options on *Assist (and snapshot struct AssistOptions via Snapshot() / Apply(opts) / Reset()):

Option Range / Default Notes
GameSpeed / ScaleDT 0.25–1.0 (1.0) Scales gameplay dt without changing display refresh rate.
ReduceMotion bool Defaults to the OS reduce-motion preference when unset.
HighContrast bool Defaults to the OS high-contrast preference; switches assist.Palette() to VisionHighContrast.
ColorVision ColorVision Selects VisionNormal, VisionProtanopia, VisionDeuteranopia, VisionTritanopia, or VisionHighContrast.
TextScale 1.0–2.0 (1.0) Multiplier read by CaptionsView and AccessibleMenu.
Invincibility, AutoAim, HoldToToggle bool Gameplay assist flags for your combat and input layers.
CaptionsEnabled, SFXCaptionsEnabled bool Toggle dialogue and sound-effect caption overlays.

Live captions (CaptionTrack & CaptionsView)

Queue timed dialogue, directional SFX, music cues, and important alerts on a CaptionTrack, step it on game.Loop, and render it as a native NSGo overlay with CaptionsView:

captions := a11y.NewCaptionTrack(cx)

// When an explosion plays to the player's right (direction in [-1, 1]):
if assist.SFXCaptionsEnabled() {
	captions.ShowSFXAt("Explosion", 0.8, 2500*time.Millisecond)
}
captions.ShowDialogue("Guide", "Watch the bridge ahead.", 3*time.Second)

// In game.Loop.Update:
captions.Update(dt)

// In your root Stack view:
a11y.CaptionsView(cx, captions, a11y.CaptionStyle{Assist: assist})

Caption.Format() formats speakers ("Guide: ..."), SFX brackets ("[Explosion →]"), and music cues ("♪ ..."). Dismiss entries early with Dismiss(id) or Clear().

Color-blind palettes & CVD simulation

PaletteFor(mode) returns an Okabe-Ito / Wong-grounded semantic Palette (Player, Ally, Enemy, Hazard, Pickup, Neutral, Background, Foreground, Highlight, Accent). Use Simulate, ContrastRatio, and EnsureContrast in tests or runtime UI theming:

pal := assist.Palette()
simEnemy := a11y.Simulate(pal.Enemy, a11y.VisionDeuteranopia)
ratio := a11y.ContrastRatio(pal.Foreground, pal.Background)
readable := a11y.EnsureContrast(accentColor, pal.Background, 4.5)
_, _, _ = simEnemy, ratio, readable

Screen-reader menus (AccessibleMenu)

Render pause, settings, and assist menus with native ARIA/accessibility roles (RoleMenu, RoleMenuItem, RoleMenuItemCheckbox), full keyboard navigation (Up/Down/Home/End/Return/Space/Escape), and cx.Announce screen-reader feedback:

a11y.AccessibleMenu(cx, a11y.MenuProps{
	Label:   "Assist Options",
	Assist:  assist,
	OnClose: closeMenu,
	Items: []a11y.MenuItem{
		{
			ID:       "reduce-motion",
			Label:    "Reduce Motion",
			Checked:  assist.ReduceMotion,
			OnSelect: func() { _ = assist.SetReduceMotion(!assist.ReduceMotion()) },
		},
		{
			ID:       "game-speed",
			Label:    "Game Speed",
			Value:    func() string { return fmt.Sprintf("%d%%", int(assist.GameSpeed()*100)) },
			OnSelect: cycleGameSpeed,
		},
	},
})
Edit this page