Documentation
¶
Overview ¶
Package input provides backend-independent keyboard, mouse, and gamepad types. Snapshot records physical input, and ActionMap resolves it into game actions. A runner samples device state into a Snapshot for systems to read during a game phase; the package defines input state and bindings, not device polling or event delivery.
Index ¶
- Constants
- func NewFrameSystem(am *ActionMap) core.SystemFunc
- func NewPreferences(bindings Bindings) (*settings.Group[Preferences], error)
- func NewTickSystem(am *ActionMap) core.SystemFunc
- type Action
- type ActionMap
- func (am *ActionMap) Axis(action Action) float64
- func (am *ActionMap) Bindings() Bindings
- func (am *ActionMap) Duration(action Action) float64
- func (am *ActionMap) FlushEdges()
- func (am *ActionMap) Held(action Action) bool
- func (am *ActionMap) JustPressed(action Action) bool
- func (am *ActionMap) JustReleased(action Action) bool
- func (am *ActionMap) Preferences() Preferences
- func (am *ActionMap) Pressed(action Action) bool
- func (am *ActionMap) Released(action Action) bool
- func (am *ActionMap) SetBindings(bindings Bindings) error
- func (am *ActionMap) SetPreferences(p Preferences) error
- func (am *ActionMap) Tick()
- func (am *ActionMap) Update(s *Snapshot, dt float64)
- type Actions
- type Bindings
- type ChordInput
- type Edges
- type Input
- type Key
- type KeyInput
- type KeyPairInput
- type MouseButton
- type MouseButtonInput
- type MouseSnapshot
- type PadAxis
- type PadAxisInput
- type PadButton
- type PadButtonInput
- type PadSnapshot
- type Preferences
- type Snapshot
- func (s *Snapshot) Cursor() geom.Vector2
- func (s *Snapshot) KeyHeld(k Key) bool
- func (s *Snapshot) KeyPressed(k Key) bool
- func (s *Snapshot) KeyReleased(k Key) bool
- func (s *Snapshot) MouseHeld(b MouseButton) bool
- func (s *Snapshot) MousePressed(b MouseButton) bool
- func (s *Snapshot) MouseReleased(b MouseButton) bool
- func (s *Snapshot) PadAxis(padIndex int, axis PadAxis) float64
- func (s *Snapshot) PadConnected(padIndex int) bool
- func (s *Snapshot) PadHeld(padIndex int, button PadButton) bool
- func (s *Snapshot) PadPressed(padIndex int, button PadButton) bool
- func (s *Snapshot) PadReleased(padIndex int, button PadButton) bool
- func (s *Snapshot) Wheel() geom.Vector2
- type State
Constants ¶
const DefaultDeadzone = 0.15
DefaultDeadzone is the default gamepad axis deadzone used by New. Axis values with magnitude below it are treated as zero.
const FrameSystemName = "engine.input-update"
FrameSystemName is the name castrum.New registers the pre-frame input system under: the system that resolves one display frame of input into the frame view by calling ActionMap.Update.
const MaxPads = 4
MaxPads is the number of gamepad slots in a Snapshot.
const TickSystemName = "engine.input-tick"
TickSystemName is the name castrum.New registers the pre-fixed input system under: the system that delivers the press and release edges accumulated since the previous tick by calling ActionMap.Tick.
Variables ¶
This section is empty.
Functions ¶
func NewFrameSystem ¶ added in v0.4.0
func NewFrameSystem(am *ActionMap) core.SystemFunc
NewFrameSystem returns the pre-frame updater that resolves one frame of input into the frame view: it reads the snapshot resource and calls ActionMap.Update. castrum.New registers it first in the pre-frame schedule so user systems in that phase see fresh actions.
It returns an error if the snapshot resource is not registered.
func NewPreferences ¶ added in v0.4.0
func NewPreferences(bindings Bindings) (*settings.Group[Preferences], error)
NewPreferences returns a group containing a validated copy of bindings and DefaultDeadzone. It returns an error for nil or invalid bindings.
func NewTickSystem ¶ added in v0.4.0
func NewTickSystem(am *ActionMap) core.SystemFunc
NewTickSystem returns the pre-fixed updater that delivers the press and release edges accumulated since the previous tick by calling ActionMap.Tick. castrum.New registers it first in the pre-fixed schedule so fixed systems read latched edges.
Types ¶
type ActionMap ¶
type ActionMap struct {
// contains filtered or unexported fields
}
ActionMap resolves Bindings against physical input snapshots and exposes per-action state.
A nil ActionMap returns zero from queries; mutating methods require a non-nil map.
func New ¶
New creates an ActionMap with a copy of bindings. A deadzone of zero selects DefaultDeadzone. It returns an error if bindings is nil or deadzone is not finite or outside [0, 1], or a binding is invalid. Each input must select exactly one binding type; chords require at least two members and cannot contain other chords.
func (*ActionMap) Axis ¶
Axis reports the action's latest frame-sampled axis value. Fixed catch-up ticks share that value; Tick does not accumulate, consume, or clear it.
func (*ActionMap) FlushEdges ¶ added in v0.3.0
func (am *ActionMap) FlushEdges()
FlushEdges clears pending press and release edges without touching Held or durations. Called on scene transitions so a press that triggered a switch cannot fire in the next scene.
func (*ActionMap) JustPressed ¶
JustPressed reports whether the action was pressed in the current frame.
func (*ActionMap) JustReleased ¶
JustReleased reports whether the action was released in the current frame.
func (*ActionMap) Preferences ¶ added in v0.4.0
func (am *ActionMap) Preferences() Preferences
Preferences returns an owned snapshot with the effective map-wide deadzone.
func (*ActionMap) Pressed ¶
Pressed reports whether a press was latched for the current tick. It reports true for every read in the tick that consumed the press. Frame-rate code wants JustPressed instead.
func (*ActionMap) Released ¶
Released reports whether a release was latched for the current tick. It reports true for every read in the tick that consumed the release. Frame-rate code wants JustReleased instead.
func (*ActionMap) SetBindings ¶
SetBindings replaces the bindings with a copy of the argument. The new bindings take effect at the next Update; chords start inactive and may produce a new press. Removing a held action releases it on the next Update. Pending fixed-tick edges are preserved, including that release. Invalid bindings leave the map unchanged.
func (*ActionMap) SetPreferences ¶ added in v0.4.0
func (am *ActionMap) SetPreferences(p Preferences) error
SetPreferences validates and applies bindings and deadzone together for the next Update. Invalid values leave the map unchanged. Changed bindings restart chords as in ActionMap.SetBindings; unchanged bindings preserve chord state. Pending fixed-tick edges are preserved.
func (*ActionMap) Tick ¶
func (am *ActionMap) Tick()
Tick delivers the press and release events accumulated since the last tick. Call it once per fixed tick, before fixed systems read Pressed or Released.
type Actions ¶ added in v0.4.0
type Actions interface {
JustPressed(Action) bool
JustReleased(Action) bool
Held(Action) bool
Pressed(Action) bool
Released(Action) bool
Duration(Action) float64
Axis(Action) float64
Bindings() Bindings
Preferences() Preferences
SetBindings(Bindings) error
SetPreferences(Preferences) error
}
Actions defines the interface for querying input states, bindings, and preferences.
type Bindings ¶
Bindings maps each Action to the physical inputs that activate it. Multiple inputs can be bound to one action.
Bindings{
"jump": {KeyInput{Key: KeySpace}.Binding(), PadButtonInput{Button: PadSouth}.Binding()},
"move_x": {KeyPairInput{Negative: KeyA, Positive: KeyD}.Binding(), PadAxisInput{Axis: PadLeftStickX}.Binding()},
"p2_jump": {PadButtonInput{Pad: 2, Button: PadSouth}.Binding()},
}
type ChordInput ¶ added in v0.2.0
type ChordInput struct {
// Inputs lists the physical bindings that must all be active.
Inputs []Input `json:"inputs"`
}
ChordInput binds a digital action to two or more inputs held together. It presses when the last member becomes active and releases when any member becomes inactive. Axis members are active outside their deadzone; the chord itself contributes no axis value.
Members with Pad: 0 must be satisfied by the same connected gamepad. Explicit pad addresses remain independent. Nested chords are not supported.
func (ChordInput) Binding ¶ added in v0.4.0
func (value ChordInput) Binding() Input
Binding returns the chord as an Input, retaining its member slice.
type Input ¶
type Input struct {
// Key selects a keyboard binding.
Key *KeyInput `json:"key,omitempty"`
// MouseButton selects a mouse button binding.
MouseButton *MouseButtonInput `json:"mouse_button,omitempty"`
// PadButton selects a gamepad button binding.
PadButton *PadButtonInput `json:"pad_button,omitempty"`
// PadAxis selects a gamepad axis binding.
PadAxis *PadAxisInput `json:"pad_axis,omitempty"`
// KeyPair selects a keyboard axis binding.
KeyPair *KeyPairInput `json:"key_pair,omitempty"`
// Chord selects a combination of physical bindings.
Chord *ChordInput `json:"chord,omitempty"`
}
Input describes one JSON-serializable physical binding.
Exactly one field must be non-nil. The binding types' Binding methods and Chord construct inputs; New and ActionMap.SetBindings validate and copy them.
type Key ¶
type Key int
Key identifies a keyboard key using the package's backend-independent key set.
const ( // KeyNone is the zero value of [Key] and represents no key at all. KeyNone Key = iota KeyA KeyB KeyC KeyD KeyE KeyF KeyG KeyH KeyI KeyJ KeyK KeyL KeyM KeyN KeyO KeyP KeyQ KeyR KeyS KeyT KeyU KeyV KeyW KeyX KeyY KeyZ Key0 Key1 Key2 Key3 Key4 Key5 Key6 Key7 Key8 Key9 KeyBacktick KeyMinus KeyEqual KeyBracketLeft KeyBracketRight KeyBackslash KeySemicolon KeyApostrophe KeyComma KeyPeriod KeySlash // KeyISOBackslash identifies the ISO layout's additional backslash key. KeyISOBackslash KeySpace KeyEscape KeyTab KeyCapsLock KeyEnter KeyBackspace KeyShiftLeft KeyShiftRight KeyControlLeft KeyControlRight KeyAltLeft KeyAltRight KeyF1 KeyF2 KeyF3 KeyF4 KeyF5 KeyF6 KeyF7 KeyF8 KeyF9 KeyF10 KeyF11 KeyF12 KeyInsert KeyDelete KeyHome KeyEnd KeyPageUp KeyPageDown KeyPrintScreen KeyScrollLock KeyPause KeyArrowUp KeyArrowDown KeyArrowLeft KeyArrowRight KeyNumpad0 KeyNumpad1 KeyNumpad2 KeyNumpad3 KeyNumpad4 KeyNumpad5 KeyNumpad6 KeyNumpad7 KeyNumpad8 KeyNumpad9 KeyNumpadDecimal KeyNumpadEnter KeyNumpadAdd KeyNumpadSubtract KeyNumpadMultiply KeyNumpadDivide KeyNumLock // NumKeys is the number of key values, including [KeyNone]. NumKeys )
type KeyInput ¶
type KeyInput struct {
// Key is the trigger: Pressed, Held, and Released follow this key
// while every modifier is held. A Key of KeyNone never fires.
Key Key `json:"key"`
// Modifiers lists keys that must be held for Key to count.
// Empty slots (KeyNone) are ignored, so the zero value binds the
// plain key. A modifier dropped mid-hold stops the action without
// a release edge. A modifier equal to Key suppresses the release
// edge: the key is no longer held on its own release frame.
Modifiers [3]Key `json:"modifiers,omitzero"`
}
KeyInput binds an action to a keyboard key, optionally requiring modifier keys to be held. Use KeyInput.Binding to construct an Input.
type KeyPairInput ¶
type KeyPairInput struct {
// Negative is the key for the negative axis direction.
Negative Key `json:"negative"`
// Positive is the key for the positive axis direction.
Positive Key `json:"positive"`
}
KeyPairInput binds the negative and positive directions of an axis to keyboard keys.
func (KeyPairInput) Binding ¶ added in v0.4.0
func (value KeyPairInput) Binding() Input
Binding returns the keyboard axis binding as an Input.
type MouseButton ¶
type MouseButton int
MouseButton identifies a standard mouse button.
const ( MouseButtonLeft MouseButton = iota MouseButtonRight MouseButtonMiddle // NumMouseButtons is the number of mouse buttons. NumMouseButtons )
func (MouseButton) String ¶
func (b MouseButton) String() string
String returns the button's name, or a formatted value if b is invalid.
type MouseButtonInput ¶
type MouseButtonInput struct {
// Button is the mouse button that triggers the action.
Button MouseButton `json:"button"`
}
MouseButtonInput binds an action to a mouse button.
func (MouseButtonInput) Binding ¶ added in v0.4.0
func (value MouseButtonInput) Binding() Input
Binding returns the mouse button binding as an Input.
type MouseSnapshot ¶
type MouseSnapshot struct {
// Cursor is the cursor position in logical render coordinates.
Cursor geom.Vector2
// Buttons stores the state of each [MouseButton].
Buttons [NumMouseButtons]State
// Wheel is the signed scroll offset reported for this runner update.
// Values may be fractional; they are not a held state or a velocity.
Wheel geom.Vector2
}
MouseSnapshot is the mouse state in one Snapshot.
type PadAxis ¶
type PadAxis int
PadAxis identifies a normalized gamepad axis. Stick values span -1 to 1, with positive Y downward; trigger values span 0 to 1.
type PadAxisInput ¶
type PadAxisInput struct {
// Pad is the player number: 0 - the zero value - means any
// connected pad, 1 the first pad, 2 the second.
Pad int `json:"pad,omitempty"`
// Axis is the gamepad axis that triggers the action.
Axis PadAxis `json:"axis"`
// Direction gates which deflection drives the binding: 0 -
// the zero value - either direction, -1 only the negative half
// (screen-up for a Y axis), +1 only the positive half.
Direction int `json:"direction,omitempty"`
// Deadzone overrides the action map's deadzone. Zero inherits it.
// Values must be finite and in [0, 1]; active values are not rescaled.
Deadzone float64 `json:"deadzone,omitempty"`
}
PadAxisInput binds an action to a gamepad axis.
func (PadAxisInput) Binding ¶ added in v0.4.0
func (value PadAxisInput) Binding() Input
Binding returns the gamepad axis binding as an Input.
type PadButton ¶
type PadButton int
PadButton identifies a gamepad button by its position in the standard layout, independent of vendor-specific glyphs. Triggers are represented by PadAxis, not PadButton.
const ( // PadSouth identifies the button at the south position in a standard // gamepad layout. PadSouth PadButton = iota // PadEast identifies the button at the east position in a standard // gamepad layout. PadEast // PadWest identifies the button at the west position in a standard // gamepad layout. PadWest // PadNorth identifies the button at the north position in a standard // gamepad layout. PadNorth PadLeftShoulder PadRightShoulder PadLeftStick PadRightStick PadBack PadStart PadGuide PadDPadUp PadDPadDown PadDPadLeft PadDPadRight // NumPadButtons is the number of gamepad buttons. NumPadButtons )
type PadButtonInput ¶
type PadButtonInput struct {
// Pad is the player number: 0 - the zero value - means any
// connected pad, 1 the first pad, 2 the second.
Pad int `json:"pad,omitempty"`
// Button is the gamepad button that triggers the action.
Button PadButton `json:"button"`
}
PadButtonInput binds an action to a gamepad button.
func (PadButtonInput) Binding ¶ added in v0.4.0
func (value PadButtonInput) Binding() Input
Binding returns the gamepad button binding as an Input.
type PadSnapshot ¶
type PadSnapshot struct {
// Connected reports whether a gamepad occupies this slot.
Connected bool
// Buttons stores the state of each [PadButton].
Buttons [NumPadButtons]State
// Axes stores the value of each [PadAxis].
Axes [NumPadAxes]float64
}
PadSnapshot is the state of one gamepad slot in a Snapshot.
type Preferences ¶ added in v0.4.0
type Preferences struct {
// Bindings maps actions to physical inputs.
Bindings Bindings `json:"bindings"`
// Deadzone is the map-wide gamepad axis threshold. Zero selects
// [DefaultDeadzone]; nonzero binding deadzones override it.
Deadzone float64 `json:"deadzone"`
}
Preferences holds JSON-serializable desired action bindings and deadzone.
func (Preferences) Clone ¶ added in v0.4.0
func (p Preferences) Clone() Preferences
Clone returns a copy of the preferences, including binding payloads and chord members.
func (Preferences) Validate ¶ added in v0.4.0
func (p Preferences) Validate() error
Validate reports an error if the bindings are nil or invalid, or the deadzone is not finite and in [0, 1].
type Snapshot ¶
type Snapshot struct {
// Keys holds the state of all keyboard keys in this snapshot.
Keys [NumKeys]State
// Mouse holds the state of the mouse in this snapshot.
Mouse MouseSnapshot
// Pads holds the state of all gamepads in this snapshot.
Pads [MaxPads]PadSnapshot
}
Snapshot is the state of all input devices for one frame. The runner populates it; game systems should treat it as read-only.
Its read methods are nil-safe and return the zero value for invalid keys, buttons, axes, or pad indexes.
func (*Snapshot) KeyPressed ¶
KeyPressed reports whether k was pressed in this snapshot.
func (*Snapshot) KeyReleased ¶
KeyReleased reports whether k was released in this snapshot.
func (*Snapshot) MouseHeld ¶
func (s *Snapshot) MouseHeld(b MouseButton) bool
MouseHeld reports whether b is held in this snapshot.
func (*Snapshot) MousePressed ¶
func (s *Snapshot) MousePressed(b MouseButton) bool
MousePressed reports whether b was pressed in this snapshot.
func (*Snapshot) MouseReleased ¶
func (s *Snapshot) MouseReleased(b MouseButton) bool
MouseReleased reports whether b was released in this snapshot.
func (*Snapshot) PadConnected ¶
PadConnected reports whether a gamepad occupies padIndex in this snapshot.
func (*Snapshot) PadPressed ¶
PadPressed reports whether button was pressed on padIndex in this snapshot.
func (*Snapshot) PadReleased ¶
PadReleased reports whether button was released on padIndex in this snapshot.
type State ¶
type State struct {
// Pressed is true on the frame the input becomes active.
Pressed bool
// Held is true while the input is active.
Held bool
// Released is true on the frame the input becomes inactive.
Released bool
}
State records whether a physical input changed or remained active during a frame.