Documentation
¶
Overview ¶
Package gamepad reads game controllers: their buttons, triggers and sticks in the standard layout of an Xbox controller, which the W3C's Gamepad API and SDL's game controller database name, whatever pad it is:
for _, pad := range gamepad.Pads() {
s := pad.State()
x, y := s.LeftStick()
if s.Pressed(gamepad.South) {
jump()
}
}
A game reads the pads' states once a frame, or once a step of its world, and keeps the last to tell a button just pressed from one held (State.JustPressed).
The pads come from GameController.framework on macOS (Xbox, PlayStation, Switch Pro and MFi controllers), XInput on Windows (Xbox controllers and those that act as one, as Steam Input makes of others), evdev on Linux (/dev/input, readable by the user at the seat), and the Gamepad API in browsers. On macOS, input reaches the app in front only, as games expect. Every function and method is safe to call from any goroutine.
Index ¶
- Constants
- type Axis
- type Button
- type Pad
- type State
- func (s State) Axis(a Axis) float32
- func (s State) JustPressed(last State, b Button) bool
- func (s State) JustReleased(last State, b Button) bool
- func (s State) LeftStick() (x, y float32)
- func (s State) Pressed(b Button) bool
- func (s State) RightStick() (x, y float32)
- func (s State) Value(b Button) float32
Constants ¶
const Deadzone = 0.15
Deadzone is how far from the middle a stick must move to count, as a fraction of its reach: sticks rest a little off their middle.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Axis ¶
type Axis uint8
Axis is an axis of the sticks, from -1 to 1: right and down are positive, as on the screen.
type Button ¶
type Button uint8
Button is a button of the standard layout, named by place: South is A on an Xbox controller, Cross on a PlayStation one, B on a Nintendo one.
const ( South Button = iota East West North LeftShoulder RightShoulder LeftTrigger // analog: Value is from 0 to 1 RightTrigger Back // View, Select, Share, Minus Start LeftStickButton RightStickButton Up Down Left Right Guide // the Xbox, PlayStation or Home button, where the system leaves it ButtonCount )
The buttons, in the order of the W3C's standard gamepad.
type Pad ¶
type Pad struct {
// contains filtered or unexported fields
}
A Pad is a game controller, from when it connects until it disconnects; connected again, it is a Pad anew.
func Pads ¶
func Pads() []*Pad
Pads returns the pads connected, in the order they connected. It looks for pads connected since at most once a second.
type State ¶
type State struct {
// Buttons are from 0 to 1: 0 or 1 but for the triggers.
Buttons [ButtonCount]float32
// Axes are from -1 to 1, as the pad reports them, without a dead
// zone.
Axes [AxisCount]float32
}
State is what a pad's controls read at one time.
func (State) JustPressed ¶
JustPressed reports whether b is pressed in s and was not in last, the state read before.
func (State) JustReleased ¶
JustReleased reports whether b was pressed in last and is not in s.
func (State) LeftStick ¶
LeftStick returns where the left stick is, each coordinate from -1 to 1, right and down positive, with a radial dead zone: 0 near the middle, then from 0 at Deadzone to 1 at the rim, the same in every direction.
func (State) RightStick ¶
RightStick returns where the right stick is, as LeftStick.