InputState
namespace Griff.Runtime;
public sealed class InputState : IHostInput, IInputThe input every host feeds (IHostInput) and every game reads (IInput): call SetKey/SetMouseButton as events arrive and EndFrame after each game tick. The game calls BeginFrame at the start of each.
Properties
Section titled “Properties”Actions
Section titled “Actions”public IReadOnlyCollection<string> Actions { get; }The names of every action, built in, bound or rebound.
AnyPressed
Section titled “AnyPressed”public InputBinding? AnyPressed { get; }Cursor
Section titled “Cursor”public MouseCursor Cursor { get; set; }GamepadCount
Section titled “GamepadCount”public int GamepadCount { get; }How many controllers are plugged in.
Horizontal
Section titled “Horizontal”public float Horizontal { get; }IsGamepadConnected
Section titled “IsGamepadConnected”public bool IsGamepadConnected { get; set; }True while any controller is plugged in. Setting it says whether the first is (a platform with one controller); SetGamepadConnected says so of each.
IsMouseInView
Section titled “IsMouseInView”public bool IsMouseInView { get; set; }Set by the platform as the mouse enters and leaves the view. True by default, for tests and headless runs.
IsMouseLocked
Section titled “IsMouseLocked”public bool IsMouseLocked { get; set; }Set by the platform while it holds the mouse for the game (see WantsMouseLock).
LeftStick
Section titled “LeftStick”public Vector2 LeftStick { get; }The left stick of any controller (the first held off its middle), each axis -1..1. Up is positive.
LeftTrigger
Section titled “LeftTrigger”public float LeftTrigger { get; }How far any controller’s left trigger is pulled (the furthest), 0..1.
MouseDelta
Section titled “MouseDelta”public Vector2 MouseDelta { get; }MouseOnScreen
Section titled “MouseOnScreen”public Vector2? MouseOnScreen { get; set; }MouseWorld
Section titled “MouseWorld”public Vector2 MouseWorld { get; set; }public Vector2 Move { get; }RightStick
Section titled “RightStick”public Vector2 RightStick { get; }The right stick of any controller (the first held off its middle), each axis -1..1. Up is positive.
RightTrigger
Section titled “RightTrigger”public float RightTrigger { get; }How far any controller’s right trigger is pulled (the furthest), 0..1.
RumbleStrength
Section titled “RumbleStrength”public float RumbleStrength { get; }How hard the controller should be shaking right now, 0..1. The platform reads it each frame.
Scroll
Section titled “Scroll”public float Scroll { get; }Touches
Section titled “Touches”public IReadOnlyList<TouchPoint> Touches { get; }TypedText
Section titled “TypedText”public string TypedText { get; }UsingGamepad
Section titled “UsingGamepad”public bool UsingGamepad { get; }Vertical
Section titled “Vertical”public float Vertical { get; }WantsMouseLock
Section titled “WantsMouseLock”public bool WantsMouseLock { get; set; }Methods
Section titled “Methods”AddMouseDelta
Section titled “AddMouseDelta”public void AddMouseDelta(Vector2 pixels)Adds mouse movement, in pixels (right and up are positive), for MouseDelta.
AddScroll
Section titled “AddScroll”public void AddScroll(float notches)Adds mouse wheel movement, in notches (positive is scrolling up).
AddText
Section titled “AddText”public void AddText(string text)Adds typed characters. Control characters are ignored; Backspace goes through TypeBackspace.
AimFrom
Section titled “AimFrom”public Vector2 AimFrom(Vector2 origin)AimFrom
Section titled “AimFrom”public Vector2 AimFrom(Vector3 origin)BeginFrame
Section titled “BeginFrame”public void BeginFrame(float realSeconds)Starts a game frame realSeconds after the last one: dates this frame’s presses (which arrived since the last frame) for buffered WasPressed, and counts down any rumble.
public void Bind(string action, params InputBinding[] bindings)BindingsOf
Section titled “BindingsOf”public IReadOnlyList<InputBinding> BindingsOf(string action)ConnectedGamepad
Section titled “ConnectedGamepad”public int ConnectedGamepad(int nth)The platform’s number for the nth controller plugged in (from 0: the first plugged in is 0, whatever slot it’s in), or -1 if there aren’t that many.
EndFrame
Section titled “EndFrame”public void EndFrame()HoldGamepadButton
Section titled “HoldGamepadButton”public void HoldGamepadButton(int index, GamepadButton button, bool down)Holds (or lets go of) button on controller index as if pressed on it, over what the controller itself says: for tests, agents and demos playing as a second player’s controller. A controller with a button held this way counts as plugged in from then on.
IsDown
Section titled “IsDown”public bool IsDown(GamepadButton button)IsDown
Section titled “IsDown”public bool IsDown(string action)IsDown
Section titled “IsDown”public bool IsDown(Key key)IsDown
Section titled “IsDown”public bool IsDown(MouseButton button)IsDown
Section titled “IsDown”public bool IsDown(int index, GamepadButton button)True while button is held on controller index.
IsGamepadConnectedAt
Section titled “IsGamepadConnectedAt”public bool IsGamepadConnectedAt(int index)True while controller index is plugged in (or has had a button held by HoldGamepadButton).
LeftStickOf
Section titled “LeftStickOf”public Vector2 LeftStickOf(int index)Controller index’s left stick, with the dead zone applied.
LeftTriggerOf
Section titled “LeftTriggerOf”public float LeftTriggerOf(int index)How far controller index’s left trigger is pulled, 0..1.
MouseMoved
Section titled “MouseMoved”public void MouseMoved()Called by the platform when the mouse moves over the game: the player has switched to the mouse.
PlaceTouches
Section titled “PlaceTouches”public void PlaceTouches(Func<Vector2, Vector2> toWorld)Puts the fingers given on the screen in the world, through toWorld (the view as it is now).
PressedOn
Section titled “PressedOn”public GamepadButton? PressedOn(int index)The first button pressed on controller index this frame, if any.
Rebind
Section titled “Rebind”public void Rebind(string action, params InputBinding[] bindings)ReleaseAll
Section titled “ReleaseAll”public void ReleaseAll()Releases everything, e.g. when the game window loses focus.
ResetBindings
Section titled “ResetBindings”public void ResetBindings(string? action = null)RightStickOf
Section titled “RightStickOf”public Vector2 RightStickOf(int index)Controller index’s right stick, with the dead zone applied.
RightTriggerOf
Section titled “RightTriggerOf”public float RightTriggerOf(int index)How far controller index’s right trigger is pulled, 0..1.
Rumble
Section titled “Rumble”public void Rumble(float strength = 0.5f, float seconds = 0.2f)Rumble
Section titled “Rumble”public void Rumble(int index, float strength, float seconds)Shakes controller index alone (a player’s own), as Rumble shakes every controller.
RumbleStrengthOf
Section titled “RumbleStrengthOf”public float RumbleStrengthOf(int index)How hard controller index should be shaking now, 0..1: its own rumble or every controller’s. The platform reads it each frame.
SetGamepad
Section titled “SetGamepad”public void SetGamepad(IEnumerable<GamepadButton> down, Vector2 leftStick)Sets the first controller’s state for this frame (called by the platform each tick).
SetGamepad
Section titled “SetGamepad”public void SetGamepad(IEnumerable<GamepadButton> down, Vector2 leftStick, Vector2 rightStick, float leftTrigger = 0f, float rightTrigger = 0f)Sets the first controller’s state for this frame (called by the platform each tick). Triggers are 0..1.
SetGamepad
Section titled “SetGamepad”public void SetGamepad(int index, IEnumerable<GamepadButton> down, Vector2 leftStick, Vector2 rightStick, float leftTrigger = 0f, float rightTrigger = 0f)Sets controller index’s state for this frame (0 to MaxGamepads - 1, the platform’s own numbering; called by it each tick). Triggers are 0..1. What the game reads by itself (IsDown, the sticks, the actions) is every controller at once; a player at this machine reads their own (see LocalPlayers).
SetGamepadConnected
Section titled “SetGamepadConnected”public void SetGamepadConnected(int index, bool connected)Says whether controller index is plugged in (called by the platform as controllers come and go). Unplugged, it stops counting as plugged in even if HoldGamepadButton pretended it was.
SetKey
Section titled “SetKey”public void SetKey(Key key, bool down)SetMouseButton
Section titled “SetMouseButton”public void SetMouseButton(MouseButton button, bool down)SetTouch
Section titled “SetTouch”public void SetTouch(int id, Vector2 at, bool down, bool onScreen = false)A finger went down, moved or was lifted. at is in world units, or with onScreen where it is on the view (from -1 to 1 across it, up positive), which PlaceTouches puts in the world.
StopRumble
Section titled “StopRumble”public void StopRumble()Stops any rumble straight away, e.g. when the game pauses or stops.
TypeBackspace
Section titled “TypeBackspace”public void TypeBackspace()Records a Backspace for TypeInto, including the repeats while it’s held.
TypeInto
Section titled “TypeInto”public string TypeInto(string text, int maxLength = 2147483647)UseControls
Section titled “UseControls”public void UseControls(IReadOnlyDictionary<string, IReadOnlyList<InputBinding>> controls)Uses the project’s controls (from the editor’s Controls window) as the game’s own: each action in controls gets those bindings, like Bind. Calling it again with a changed set (the controls edited while the game runs) puts back built-in actions it no longer changes and drops actions it no longer has, leaving anything scripts bound alone.
UseSaveData
Section titled “UseSaveData”public void UseSaveData(SaveData saveData)Keeps the player’s Rebind choices in saveData, replacing the current ones with those saved there. The game calls this whenever its save data is set.
WasPressed
Section titled “WasPressed”public bool WasPressed(GamepadButton button)WasPressed
Section titled “WasPressed”public bool WasPressed(string action)WasPressed
Section titled “WasPressed”public bool WasPressed(Key key)WasPressed
Section titled “WasPressed”public bool WasPressed(MouseButton button)WasPressed
Section titled “WasPressed”public bool WasPressed(string action, float buffer)WasPressed
Section titled “WasPressed”public bool WasPressed(int index, GamepadButton button)True on the frame button goes down on controller index.
WasReleased
Section titled “WasReleased”public bool WasReleased(GamepadButton button)WasReleased
Section titled “WasReleased”public bool WasReleased(string action)WasReleased
Section titled “WasReleased”public bool WasReleased(Key key)WasReleased
Section titled “WasReleased”public bool WasReleased(MouseButton button)WasReleased
Section titled “WasReleased”public bool WasReleased(int index, GamepadButton button)True on the frame button is let go on controller index.
Fields
Section titled “Fields”BindingsSaveKey
Section titled “BindingsSaveKey”public const string BindingsSaveKeyWhere the player’s Rebind choices are kept in SaveData.
DeadZone
Section titled “DeadZone”public const float DeadZoneStick movement smaller than this is ignored, so a resting stick doesn’t drift.
MaxGamepads
Section titled “MaxGamepads”public const int MaxGamepadsHow many controllers it keeps apart: up to four, as XInput and most browsers give.
TriggerDeadZone
Section titled “TriggerDeadZone”public const float TriggerDeadZoneA trigger pulled less than this reads as 0.