# `tecs.controller` ## Overview ::: api-overview **functions** - [`install`](#tecs.controller.install) - [`newManager`](#tecs.controller.newManager) - [`of`](#tecs.controller.of) **types** - [`Bindings`](#tecs.controller.Bindings) - [`Controller`](#tecs.controller.Controller) - [`ControlManager`](#tecs.controller.ControlManager) - [`GamepadConfig`](#tecs.controller.GamepadConfig) ::: ::: api-group ## Functions ::: api-item tecs.controller.install ### `install` _function_ ```nupp function(exclusive world: ecs.World): ControlManager ``` Installs a logical controller manager in a world that already owns input. #### Arguments `exclusive world` - [`ecs.World`](../../../modules/tecs/ecs/index.html#tecs.ecs.World) : the world whose input and gamepad polling the manager shares. #### Returns [`ControlManager`](../../../modules/tecs/controller/index.html#tecs.controller.ControlManager) : the new manager, also available through `of`. #### Raises | Type | Condition | | --- | --- | | `string` | when input is not installed or a controller manager already exists. | ::: ::: api-item tecs.controller.newManager ### `newManager` _function_ ```nupp function(state: input.Input): ControlManager ``` Creates a standalone manager over an existing input state. #### Arguments `state` - [`input.Input`](../../../modules/tecs/input/index.html#tecs.input.Input) : the input state supplying physical controls and capture layers. #### Returns [`ControlManager`](../../../modules/tecs/controller/index.html#tecs.controller.ControlManager) : a new manager; callers refresh its device assignments themselves. ::: ::: api-item tecs.controller.of ### `of` _function_ ```nupp function(borrows world: ecs.World): ControlManager? ``` Returns a world's installed logical controller manager. #### Arguments `borrows world` - [`ecs.World`](../../../modules/tecs/ecs/index.html#tecs.ecs.World) : the world to inspect. #### Returns [`ControlManager`](../../../modules/tecs/controller/index.html#tecs.controller.ControlManager)`?` : the installed manager, or nil when none is installed. ::: ::: ::: api-group ## Types ::: api-item tecs.controller.Bindings ### `Bindings` _type_ ```nupp type Bindings = { controls: {[string]: {string}}, pairs: {[string]: {string}}? } ``` Defines actions and optional four-direction movement pairs. #### Members ::: api-overview **fields** - [`controls`](#tecs.controller.Bindings.controls) - [`pairs`](#tecs.controller.Bindings.pairs) ::: ::: api-item tecs.controller.Bindings.controls #### `controls` _field_ ```nupp controls: {[string]: {string}} ``` Caller-writable. Maps action names to ordered binding lists. The controller copies the lists; the first nonzero binding supplies a value. ::: ::: api-item tecs.controller.Bindings.pairs #### `pairs` _field_ ```nupp pairs: {[string]: {string}}? ``` Caller-writable. Maps pair names to exactly four existing actions in left, right, up, down order. Nil defines no pairs. ::: ::: ::: api-item tecs.controller.Controller ### `Controller` _record_ ```nupp record Controller ``` Represents one player's logical input over a manager's shared input state. #### Members ::: api-overview **instance methods** - [`getPair`](#tecs.controller.Controller.getPair) - [`getPairNormalized`](#tecs.controller.Controller.getPairNormalized) - [`getRaw`](#tecs.controller.Controller.getRaw) - [`isDown`](#tecs.controller.Controller.isDown) - [`isPressed`](#tecs.controller.Controller.isPressed) - [`isReleased`](#tecs.controller.Controller.isReleased) - [`notifyWithRumble`](#tecs.controller.Controller.notifyWithRumble) - [`rebind`](#tecs.controller.Controller.rebind) - [`resetGamepad`](#tecs.controller.Controller.resetGamepad) - [`setGamepad`](#tecs.controller.Controller.setGamepad) **fields** - [`auto`](#tecs.controller.Controller.auto) - [`bindings`](#tecs.controller.Controller.bindings) - [`deadzone`](#tecs.controller.Controller.deadzone) - [`gamepad`](#tecs.controller.Controller.gamepad) - [`onGamepadChanged`](#tecs.controller.Controller.onGamepadChanged) ::: ::: api-item tecs.controller.Controller.bindings #### `bindings` _field_ `bindings:` [`Bindings`](../../../modules/tecs/controller/index.html#tecs.controller.Bindings) Read-only. Reports a copy of the current mapping. Change mappings with `rebind`; callers must not edit the returned tables. ::: ::: api-item tecs.controller.Controller.gamepad #### `gamepad` _field_ `gamepad:` [`tecs.input.Gamepad`](../../../modules/tecs/input/index.html#tecs.input.Gamepad)`?` Read-only. Reports the assigned gamepad, or nil when none is assigned. A disconnected object stays harmless until the manager refreshes it. ::: ::: api-item tecs.controller.Controller.deadzone #### `deadzone` _field_ ```nupp deadzone: number ``` Read-only. Reports the axis magnitude threshold configured at creation or through `resetGamepad`. ::: ::: api-item tecs.controller.Controller.auto #### `auto` _field_ ```nupp auto: boolean ``` Read-only. Reports whether the manager automatically assigns a gamepad. ::: ::: api-item tecs.controller.Controller.onGamepadChanged #### `onGamepadChanged` _field_ ```nupp onGamepadChanged: (function(player: Controller, current: tecs.input.Gamepad?, previous: tecs.input.Gamepad?): nil)? ``` Caller-writable. Receives this controller, the new gamepad and the old gamepad after assignment changes. Nil disables notification. ::: ::: api-item tecs.controller.Controller.isPressed #### `isPressed` _method_ ```nupp function(borrows self: Controller, action: string): boolean ``` Reports a press from any bound digital input in the active edge tier. ##### Arguments `borrows self` - [`Controller`](../../../modules/tecs/controller/index.html#tecs.controller.Controller) : the player whose input to read. `action` - `string` : the logical action name; an unknown name reads false. ##### Returns `boolean` : whether a bound key, mouse button or gamepad button was pressed. ##### Raises | Type | Condition | | --- | --- | | `string` | when evaluation reaches a hat binding that Input cannot read. | ::: ::: api-item tecs.controller.Controller.isDown #### `isDown` _method_ ```nupp function(borrows self: Controller, action: string): boolean ``` Reports whether an action has a nonzero value. ##### Arguments `borrows self` - [`Controller`](../../../modules/tecs/controller/index.html#tecs.controller.Controller) : the player whose input to read. `action` - `string` : the logical action name; an unknown name reads false. ##### Returns `boolean` : whether any binding supplies a nonzero value. ##### Raises | Type | Condition | | --- | --- | | `string` | when evaluation reaches a hat binding that Input cannot read. | ::: ::: api-item tecs.controller.Controller.isReleased #### `isReleased` _method_ ```nupp function(borrows self: Controller, action: string): boolean ``` Reports a release from any bound digital input in the active edge tier. ##### Arguments `borrows self` - [`Controller`](../../../modules/tecs/controller/index.html#tecs.controller.Controller) : the player whose input to read. `action` - `string` : the logical action name; an unknown name reads false. ##### Returns `boolean` : whether a bound key, mouse button or gamepad button was released. ##### Raises | Type | Condition | | --- | --- | | `string` | when evaluation reaches a hat binding that Input cannot read. | ::: ::: api-item tecs.controller.Controller.getRaw #### `getRaw` _method_ ```nupp function(borrows self: Controller, action: string): number ``` Returns the first nonzero binding value in declaration order. axes, or the signed value for raw axes, after deadzone filtering. ##### Arguments `borrows self` - [`Controller`](../../../modules/tecs/controller/index.html#tecs.controller.Controller) : the player whose input to read. `action` - `string` : the logical action name; an unknown name reads zero. ##### Returns `number` : zero or one for digital bindings, zero to one for directional ##### Raises | Type | Condition | | --- | --- | | `string` | when evaluation reaches a hat binding that Input cannot read. | ::: ::: api-item tecs.controller.Controller.getPair #### `getPair` _method_ ```nupp function(borrows self: Controller, name: string): (number, number) ``` Returns right minus left and down minus up for a movement pair. ##### Arguments `borrows self` - [`Controller`](../../../modules/tecs/controller/index.html#tecs.controller.Controller) : the player whose input to read. `name` - `string` : the configured pair name; an unknown name reads zeroes. ##### Returns `number` : the horizontal value clamped to minus one through one. `number` : the vertical value clamped to minus one through one. ##### Raises | Type | Condition | | --- | --- | | `string` | when a pair's evaluation reaches a hat binding that Input cannot read. | ::: ::: api-item tecs.controller.Controller.getPairNormalized #### `getPairNormalized` _method_ ```nupp function(borrows self: Controller, name: string): (number, number) ``` Limits a movement pair to unit length while preserving smaller inputs. ##### Arguments `borrows self` - [`Controller`](../../../modules/tecs/controller/index.html#tecs.controller.Controller) : the player whose input to read. `name` - `string` : the configured pair name; an unknown name reads zeroes. ##### Returns `number` : the horizontal value with diagonal speed limited. `number` : the vertical value with partial analog magnitude preserved. ##### Raises | Type | Condition | | --- | --- | | `string` | when a pair's evaluation reaches a hat binding that Input cannot read. | ::: ::: api-item tecs.controller.Controller.rebind #### `rebind` _method_ ```nupp function(exclusive self: Controller, bindings: Bindings) ``` Validates, compiles and copies a replacement mapping atomically. ##### Arguments `exclusive self` - [`Controller`](../../../modules/tecs/controller/index.html#tecs.controller.Controller) : the player whose mapping to replace. `bindings` - [`Bindings`](../../../modules/tecs/controller/index.html#tecs.controller.Bindings) : the complete replacement actions and pairs. ##### Raises | Type | Condition | | --- | --- | | `string` | when a name, binding source, device control or pair is invalid. | ::: ::: api-item tecs.controller.Controller.setGamepad #### `setGamepad` _method_ ```nupp function(self: Controller, gamepad: tecs.input.Gamepad?) ``` Assigns a gamepad manually and disables automatic assignment. to leave this player with keyboard and mouse controls only. ##### Arguments `self` - [`Controller`](../../../modules/tecs/controller/index.html#tecs.controller.Controller) : the player whose gamepad to change. `gamepad` - [`tecs.input.Gamepad`](../../../modules/tecs/input/index.html#tecs.input.Gamepad)`?` : a connected device owned by the manager's input, or nil ##### Raises | Type | Condition | | --- | --- | | `string` | when the gamepad is disconnected or belongs to another input. | ::: ::: api-item tecs.controller.Controller.resetGamepad #### `resetGamepad` _method_ ```nupp function(self: Controller, config: GamepadConfig?) ``` Resets assignment and optionally enables automatic assignment. is disconnected or belongs to another input. ##### Arguments `self` - [`Controller`](../../../modules/tecs/controller/index.html#tecs.controller.Controller) : the player whose assignment to reset. `config` - [`GamepadConfig`](../../../modules/tecs/controller/index.html#tecs.controller.GamepadConfig)`?` : the new assignment policy, or nil to disable gamepad input. ##### Raises | Type | Condition | | --- | --- | | `string` | when the deadzone is outside zero to one or the gamepad | ::: ::: api-item tecs.controller.Controller.notifyWithRumble #### `notifyWithRumble` _method_ ```nupp function( borrows self: Controller, strength: number?, seconds: number? ): boolean ``` Plays a brief identification rumble on this player's gamepad. ##### Arguments `borrows self` - [`Controller`](../../../modules/tecs/controller/index.html#tecs.controller.Controller) : the player whose gamepad to identify. `strength` - `number?` : the motor strength from zero to one, defaulting to 0.5. `seconds` - `number?` : the duration in seconds, defaulting to 0.2; zero stops it. ##### Returns `boolean` : whether the gamepad accepted the effect. ##### Raises | Type | Condition | | --- | --- | | `string` | when strength or duration is non-finite or outside its range. | ::: ::: ::: api-item tecs.controller.ControlManager ### `ControlManager` _record_ ```nupp record ControlManager ``` Owns the logical controllers that share one input state. #### Members ::: api-overview **instance methods** - [`addController`](#tecs.controller.ControlManager.addController) - [`get`](#tecs.controller.ControlManager.get) - [`refreshDevices`](#tecs.controller.ControlManager.refreshDevices) - [`removeController`](#tecs.controller.ControlManager.removeController) **fields** - [`controllers`](#tecs.controller.ControlManager.controllers) ::: ::: api-item tecs.controller.ControlManager.controllers #### `controllers` _field_ `controllers:` `{`[`Controller`](../../../modules/tecs/controller/index.html#tecs.controller.Controller)`}` Read-only. Reports the manager's controller list in creation order. Callers must use `addController` and `removeController` to change it. ::: ::: api-item tecs.controller.ControlManager.addController #### `addController` _method_ ```nupp function( self: ControlManager, bindings: Bindings, config: GamepadConfig? ): Controller ``` Creates and registers one player's copied mapping. an axis deadzone of 0.5. ##### Arguments `self` - [`ControlManager`](../../../modules/tecs/controller/index.html#tecs.controller.ControlManager) : the manager that owns the player. `bindings` - [`Bindings`](../../../modules/tecs/controller/index.html#tecs.controller.Bindings) : the logical actions and optional movement pairs. `config` - [`GamepadConfig`](../../../modules/tecs/controller/index.html#tecs.controller.GamepadConfig)`?` : the gamepad policy, defaulting to manual assignment and ##### Returns [`Controller`](../../../modules/tecs/controller/index.html#tecs.controller.Controller) : the newly registered controller. ##### Raises | Type | Condition | | --- | --- | | `string` | when the mapping, deadzone or selected gamepad is invalid. | ::: ::: api-item tecs.controller.ControlManager.removeController #### `removeController` _method_ ```nupp function(self: ControlManager, player: Controller) ``` Removes a player, clears its gamepad and disables automatic assignment. ##### Arguments `self` - [`ControlManager`](../../../modules/tecs/controller/index.html#tecs.controller.ControlManager) : the manager to update. `player` - [`Controller`](../../../modules/tecs/controller/index.html#tecs.controller.Controller) : the controller to remove; an absent controller is ignored. ::: ::: api-item tecs.controller.ControlManager.get #### `get` _method_ ```nupp function(borrows self: ControlManager, index: integer): Controller? ``` Returns a player by its current position in the controller list. ##### Arguments `borrows self` - [`ControlManager`](../../../modules/tecs/controller/index.html#tecs.controller.ControlManager) : the manager to read. `index` - `integer` : the one-based player index. ##### Returns [`Controller`](../../../modules/tecs/controller/index.html#tecs.controller.Controller)`?` : the controller, or nil when no player occupies that index. ::: ::: api-item tecs.controller.ControlManager.refreshDevices #### `refreshDevices` _method_ ```nupp function(self: ControlManager) ``` Clears disconnected assignments and fills automatic players in order. input polls gamepads in `Ingress`; standalone managers call this directly. ##### Arguments `self` - [`ControlManager`](../../../modules/tecs/controller/index.html#tecs.controller.ControlManager) : the manager to refresh. Installed managers refresh after ::: ::: ::: api-item tecs.controller.GamepadConfig ### `GamepadConfig` _type_ ```nupp type GamepadConfig = { gamepad: tecs.input.Gamepad?, auto: boolean?, deadzone: number? } ``` Configures one player's gamepad assignment and axis filtering. #### Members ::: api-overview **fields** - [`auto`](#tecs.controller.GamepadConfig.auto) - [`deadzone`](#tecs.controller.GamepadConfig.deadzone) - [`gamepad`](#tecs.controller.GamepadConfig.gamepad) ::: ::: api-item tecs.controller.GamepadConfig.gamepad #### `gamepad` _field_ `gamepad:` [`tecs.input.Gamepad`](../../../modules/tecs/input/index.html#tecs.input.Gamepad)`?` Caller-writable. Selects a connected gamepad from the manager's [`tecs.input.Input`](../../../modules/tecs/input/index.html#tecs.input.Input). Nil leaves assignment to `auto`. ::: ::: api-item tecs.controller.GamepadConfig.auto #### `auto` _field_ ```nupp auto: boolean? ``` Caller-writable. Enables exclusive automatic assignment of an available gamepad. Nil defaults to false; an explicit gamepad wins. ::: ::: api-item tecs.controller.GamepadConfig.deadzone #### `deadzone` _field_ ```nupp deadzone: number? ``` Caller-writable. Sets the minimum axis magnitude from zero to one. Nil defaults to 0.5 when creating a controller and preserves the current threshold when resetting its assignment. ::: ::: :::