Documentation
¶
Overview ¶
Package gamepad reads the buttons and axes of connected game controllers in the W3C Standard Gamepad layout on every target that supports controllers (macOS, iOS, Windows, Linux, Android and the web; see core.Env.SupportsGamepads).
Pads is a tracked reactive read that rerenders scopes reading it when a controller connects or disconnects without rerendering on every button press or stick movement. State returns one controller's buttons and axes as of the current display frame, intended for core.Scope.RequestAnimationFrame loops.
Index ¶
- func CaptureInput(cx *core.Scope) (release func())
- func EnableFocusNavigation(cx *core.Scope, opts ...FocusNavOption) (stop func())
- func Rumble(cx *core.Scope, id int, lowFreq, highFreq float32, duration time.Duration)
- func StopRumble(cx *core.Scope, id int)
- type Axis
- type Button
- type Family
- type FocusNavOption
- type Pad
- type RumbleEvent
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CaptureInput ¶
CaptureInput claims raw gamepad input for the lifetime of cx (or until the returned release function is called). While any scope holds CaptureInput, EnableFocusNavigation pauses completely so active gameplay views receive raw controller input through Pads and State without moving native focus. Multiple mounted scopes may hold captures concurrently; focus navigation resumes once every capture is released.
func EnableFocusNavigation ¶
func EnableFocusNavigation(cx *core.Scope, opts ...FocusNavOption) (stop func())
EnableFocusNavigation turns on explicit gamepad navigation through native focus stops in cx's tree. An app opts into it explicitly; nothing enables it implicitly by device.
While enabled, at least one gamepad is connected, and no scope holds CaptureInput, EnableFocusNavigation polls connected pads on RequestAnimationFrame:
- D-pad directions and the left analog stick (past the deadzone, default 0.5, with initial/repeat delay when held) move focus across the same enabled native focus stops Tab reaches.
- ButtonSouth (A / Cross) activates the currently focused native view.
- ButtonEast (B / Circle) triggers Back (overlays, modals, Scope.OnBackPress, navigation stacks).
When no pads are connected or while any mounted scope holds CaptureInput, the animation frame loop stops so no idle frames run. Navigation stops automatically when cx is disposed or when the returned stop function is called.
func Rumble ¶
Rumble drives controller id's low-frequency (left/heavy) and high-frequency (right/light) rumble motors for duration, replacing any running rumble on that controller. Intensities are clamped to [0, 1] (NaN becomes 0).
A controller that is disconnected or has CanRumble == false does nothing. Zero intensities or a non-positive duration stops any running rumble. Rumble stops automatically after duration, when the app enters the background or loses focus (cx.Env().Lifecycle() != core.LifecycleActive), when cx is disposed, or when the controller disconnects.
func StopRumble ¶
StopRumble stops any running rumble on controller id. A controller that is disconnected or has CanRumble == false does nothing.
Types ¶
type Axis ¶
type Axis uint8
Axis is an analog stick axis index in the W3C Standard Gamepad layout (0..3). Negative values point left or up; positive values point right or down, in [-1, 1].
type Button ¶
type Button uint8
Button is a button index in the W3C Standard Gamepad layout (0..16).
const ( ButtonSouth Button = 0 // Bottom face button (Xbox A, PlayStation Cross, Nintendo B) ButtonA = ButtonSouth ButtonEast Button = 1 // Right face button (Xbox B, PlayStation Circle, Nintendo A) ButtonB = ButtonEast ButtonWest Button = 2 // Left face button (Xbox X, PlayStation Square, Nintendo Y) ButtonX = ButtonWest ButtonNorth Button = 3 // Top face button (Xbox Y, PlayStation Triangle, Nintendo X) ButtonY = ButtonNorth ButtonLB Button = 4 // Left shoulder / bumper (L1) ButtonRB Button = 5 // Right shoulder / bumper (R1) ButtonLT Button = 6 // Left trigger (L2), analog in [0, 1] ButtonRT Button = 7 // Right trigger (R2), analog in [0, 1] ButtonBack Button = 8 // Left center button (Back, Select, Share, View) ButtonSelect = ButtonBack ButtonStart Button = 9 // Right center button (Start, Options, Menu) ButtonLeftStick Button = 10 // Left stick click (L3) ButtonRightStick Button = 11 // Right stick click (R3) ButtonDPadUp Button = 12 ButtonDPadDown Button = 13 ButtonDPadLeft Button = 14 ButtonDPadRight Button = 15 ButtonHome Button = 16 // Center logo / guide / Home button ButtonCount = 17 )
W3C Standard Gamepad button indices.
type Family ¶
type Family uint8
Family identifies a controller's physical button-label family so games can show matching on-screen prompts (Xbox A/B/X/Y, PlayStation Cross/Circle/Square/Triangle, Nintendo B/A/Y/X, or a generic fallback).
const ( FamilyGeneric Family = Family(driver.GamepadFamilyGeneric) FamilyXbox Family = Family(driver.GamepadFamilyXbox) FamilyPlayStation Family = Family(driver.GamepadFamilyPlayStation) FamilyNintendo Family = Family(driver.GamepadFamilyNintendo) )
func DetectFamily ¶
DetectFamily classifies a controller from its USB vendor/product IDs and reported name or product category string. First-party USB vendor IDs take precedence, followed by controller name patterns, known third-party vendor IDs, and finally FamilyGeneric.
type FocusNavOption ¶
type FocusNavOption func(*focusNavConfig)
FocusNavOption configures gamepad focus navigation (EnableFocusNavigation).
func WithDeadzone ¶
func WithDeadzone(threshold float32) FocusNavOption
WithDeadzone sets the left-stick axis threshold in (0, 1] beyond which stick deflection moves focus. The default is 0.5.
func WithRepeatDelay ¶
func WithRepeatDelay(initial, interval time.Duration) FocusNavOption
WithRepeatDelay sets the initial delay before a held D-pad or left-stick direction begins repeating and the interval between subsequent repeats. The defaults are 400ms and 120ms.
func WithWrap ¶
func WithWrap(wrap bool) FocusNavOption
WithWrap controls whether directional moves wrap in Tab order when no focus stop lies further in that direction. The default is false.
type Pad ¶
type Pad struct {
ID int
Name string
VendorID uint16
ProductID uint16
Family Family
CanRumble bool
Buttons [ButtonCount]float32
Axes [AxisCount]float32
}
Pad is a connected game controller's state in the W3C Standard Gamepad layout.
func Pads ¶
Pads returns the currently connected game controllers, sorted by ID. It is a tracked reactive read: scopes reading it rerender when controllers connect or disconnect, but not when buttons or axes change. When cx's core.Access refuses core.AccessGamepad, Pads returns nil.
func State ¶
State returns controller id's buttons and axes as of the current display frame, or (Pad{}, false) if no controller with that ID is connected or cx's core.Access refuses core.AccessGamepad. During a core.Scope.RequestAnimationFrame pass all calls observe the same frame snapshot; outside a display frame State polls the current values.