Home Reference Source
import InputHandler from 'oxygen-core/components/InputHandler.js'
public class | source

InputHandler

Extends:

ComponentScript → InputHandler

Simple yet powerful input handler.

Example:

const component = new InputHandler();
component.deserialize({ requireGamepad: true });

Static Member Summary

Static Public Members
public static get
public static get

Static Method Summary

Static Public Methods
public static

Component factory.

Constructor Summary

Public Constructor
public

Constructor.

Member Summary

Public Members
public get
public set
public get
public set
public get
public set
public get
public set
public get

axes: *

public get
public set
public get
public set
public get
public get
public get
public
public get
public set
public get
public set
public get

Method Summary

Public Methods
public

acquireGamepad(trigger: number, timeout: number): Promise

Asynchronously acquire gamepad (specify trigger to press and waiting duration).

public

clear()

Clear all axes, triggers and configs.

public
public

getAcquiredGamepad(): Gamepad | null

Get acquired gamepad instance.

public

getAxis(id: string, treshold: number): number

Get value of given axis.

public

Get delta value of given axis.

public

getTrigger(id: string, treshold: number): number

Get value of given trigger.

public

Get delta value of given trigger.

public

isAxisHold(id: string, treshold: number): boolean

Tells if axis is currently hold.

public

isAxisPressed(id: string, treshold: number): boolean

Tells if axis is pressed in current frame.

public

isAxisReleased(id: string, treshold: number): boolean

Tells if axis is released in current frame.

public

isTriggerHold(id: string, treshold: number): boolean

Tells if trigger is currently hold.

public

Tells if trigger is pressed in current frame.

public

Tells if trigger is released in current frame.

public
public
public

onGamepadConnected(gamepad: *)

public

onGamepadDisconnected(gamepad: *)

public

onGamepadProcess(gamepad: *)

public

onKeyDown(code: *)

public

onKeyUp(code: *)

public

onMouseDown(unitVec: *, screenVec: *, button: *)

public

onMouseMove(unitVec: *, screenVec: *)

public

onMouseUp(unitVec: *, screenVec: *, button: *)

public

onUpdate(deltaTime: *)

public

Release acquired gamepad.

public

Manually acquire gamepad by it's index.

public

setAxis(id: string, value: number)

Set given axis value.

public

setTrigger(id: string, value: number)

Set given trigger value.

public

setup(config: *)

Setup axes and triggers.

Inherited Summary

From class Component
public static get
public static

Component factory.

public get
public

[name]: *

public

deserialize(json: *)

Deserialize JSON properties into this component.

public

Destructor (dispose internal resources and detach from entity).

public

onAction(name: string, args: *)

Called when action arrived.

public

onAlterActionArguments(name: string, args: *): array | undefined

Called when asked to alter arrived action parameters.

public

Called after attached to entity.

public

Called before detached from entity.

public

onPropertySerialize(name: string, value: *): *

Called when property is serialized.

public

onPropertySetup(name: string, value: *)

Called when given property is deserialized.

public

serialize(): *

Serialize component into JSON data.

From class Script
public static get
public static get

propsTypes: {"listenTo": *}

public static

factory(): *

public get
public set
public
public

onAction(name: *, args: ...*): *

public
public

onContactBegin(body: *, contact: *)

public

onContactEnd(body: *, contact: *)

public
public

onGamepadConnected(gamepad: *)

public

onGamepadDisconnected(gamepad: *)

public

onGamepadProcess(gamepad: *)

public

onKeyDown(code: *)

public

onKeyUp(code: *)

public

onMouseDown(unitVec: *, screenVec: *, button: *)

public

onMouseMove(unitVec: *, screenVec: *)

public

onMouseUp(unitVec: *, screenVec: *, button: *)

public

onPreview(gl: *, renderer: *, deltaTime: *)

public

onPropertySerialize(name: *, value: *): *

public

onPropertySetup(name: *, value: *)

public

onRender(gl: *, renderer: *, deltaTime: *, layer: *)

public

onRenderLayer(gl: *, renderer: *, deltaTime: *, layer: *)

public

onTouchDown(unitVec: *, screenVec: *, identifier: *)

public

onTouchMove(unitVec: *, screenVec: *, identifier: *)

public

onTouchUp(unitVec: *, screenVec: *, identifier: *)

public

onUpdate(deltaTime: *)

Static Public Members

public static get ControlDevice: * source

public static get propsTypes: * source

Override:

Script#propsTypes

Static Public Methods

public static factory(): InputHandler source

Component factory.

Override:

Script#factory

Return:

InputHandler

Component instance.

Public Constructors

public constructor() source

Constructor.

Override:

Script#constructor

Public Members

public get acceptFirstConnectedGamepad: boolean source

public set acceptFirstConnectedGamepad: boolean source

public get acceptGamepad: boolean source

public set acceptGamepad: boolean source

public get acceptKeyboard: boolean source

public set acceptKeyboard: boolean source

public get acceptMouse: boolean source

public set acceptMouse: boolean source

public get axes: * source

public get controlDeviceChangeTreshold: number source

public set controlDeviceChangeTreshold: number source

public get firstTriggersDelay: number source

public set firstTriggersDelay: number source

public get gamepadIndex: number source

public get isAcquiringGamepad: boolean source

public get lastControlDevice: string source

public listenTo: * source

Override:

Script#listenTo

public get repeatingTriggersDelay: number source

public set repeatingTriggersDelay: number source

public get requireGamepad: boolean source

public set requireGamepad: boolean source

public get triggers: * source

Public Methods

public acquireGamepad(trigger: number, timeout: number): Promise source

Asynchronously acquire gamepad (specify trigger to press and waiting duration).

Params:

NameTypeAttributeDescription
trigger number

Trigger index.

timeout number

Waiting duration in milliseconds.

Return:

Promise

Waiting promise.

Example:

component.acquireGamepad(0).then(() => System.events.trigger('player-is-ready'));

public clear() source

Clear all axes, triggers and configs.

Example:

component.clear();

public dispose() source

Destructor (dispose internal resources and detach from entity).

Override:

Script#dispose

public getAcquiredGamepad(): Gamepad | null source

Get acquired gamepad instance.

Return:

Gamepad | null

Acquired gamepad instance or null.

public getAxis(id: string, treshold: number): number source

Get value of given axis.

Params:

NameTypeAttributeDescription
id string

Axis id.

treshold number

Value treshold (if value is greater than treshold, return value, zero otherwise).

Return:

number

Axis value.

Example:

x += component.getAxis('pos-x');

public getAxisDelta(id: string): number source

Get delta value of given axis.

Params:

NameTypeAttributeDescription
id string

Axis id.

Return:

number

Axis value.

Example:

if (component.getAxisDelta('pos-y') < 0) { entity.performAction('jump'); }

public getTrigger(id: string, treshold: number): number source

Get value of given trigger.

Params:

NameTypeAttributeDescription
id string

Trigger id.

treshold number

Value treshold (if value is greater than treshold, return value, zero otherwise).

Return:

number

Trigger value.

Example:

speed += component.getTrigger('accel');

public getTriggerDelta(id: string): number source

Get delta value of given trigger.

Params:

NameTypeAttributeDescription
id string

Trigger id.

Return:

number

Trigger value.

Example:

if (component.getTriggerDelta('pull') > 0) { entity.performAction('pull-rope'); }

public isAxisHold(id: string, treshold: number): boolean source

Tells if axis is currently hold.

Params:

NameTypeAttributeDescription
id string

Axis id.

treshold number

Value treshold (if value is greater than treshold, return true, false otherwise).

Return:

boolean

Holding state.

Example:

if (component.isAxisHold('pos-x')) { entity.performAction('animate', 'walk'); }

public isAxisPressed(id: string, treshold: number): boolean source

Tells if axis is pressed in current frame.

Params:

NameTypeAttributeDescription
id string

Axis id.

treshold number

Value treshold (if value is greater than treshold and axis was not hold in previous frame, return true, false otherwise).

Return:

boolean

Pressing state.

Example:

if (component.isAxisPressed('bow')) { entity.performAction('aim'); }

public isAxisReleased(id: string, treshold: number): boolean source

Tells if axis is released in current frame.

Params:

NameTypeAttributeDescription
id string

Axis id.

treshold number

Value treshold (if value is smaller than treshold and axis was hold in previous frame, return true, false otherwise).

Return:

boolean

Releasing state.

Example:

if (component.isAxisReleased('bow')) { entity.performAction('fire'); }

public isTriggerHold(id: string, treshold: number): boolean source

Tells if trigger is currently hold.

Params:

NameTypeAttributeDescription
id string

Trigger id.

treshold number

Value treshold (if value is greater than treshold, return true, false otherwise).

Return:

boolean

Holding state.

Example:

if (component.isTriggerHold('swim')) { entity.performAction('swim'); }

public isTriggerPressed(id: string, treshold: number): boolean source

Tells if trigger is pressed in current frame.

Params:

NameTypeAttributeDescription
id string

Trigger id.

treshold number

Value treshold (if value is greater than treshold and trigger was not hold in previous frame, return true, false otherwise).

Return:

boolean

Pressing state.

Example:

if (component.isTriggerPressed('jump')) { entity.performAction('jump'); }

public isTriggerReleased(id: string, treshold: number): boolean source

Tells if trigger is released in current frame.

Params:

NameTypeAttributeDescription
id string

Trigger id.

treshold number

Value treshold (if value is smaller than treshold and trigger was hold in previous frame, return true, false otherwise).

Return:

boolean

Releasing state.

Example:

if (component.isTriggerReleased('fire')) { entity.performAction('fire'); }

public onAttach() source

Called after attached to entity.

Override:

Script#onAttach

public onDetach() source

Called before detached from entity.

Override:

Script#onDetach

public onGamepadConnected(gamepad: *) source

Override:

Script#onGamepadConnected

Params:

NameTypeAttributeDescription
gamepad *

public onGamepadDisconnected(gamepad: *) source

Override:

Script#onGamepadDisconnected

Params:

NameTypeAttributeDescription
gamepad *

public onGamepadProcess(gamepad: *) source

Override:

Script#onGamepadProcess

Params:

NameTypeAttributeDescription
gamepad *

public onKeyDown(code: *) source

Override:

Script#onKeyDown

Params:

NameTypeAttributeDescription
code *

public onKeyUp(code: *) source

Override:

Script#onKeyUp

Params:

NameTypeAttributeDescription
code *

public onMouseDown(unitVec: *, screenVec: *, button: *) source

Override:

Script#onMouseDown

Params:

NameTypeAttributeDescription
unitVec *
screenVec *
button *

public onMouseMove(unitVec: *, screenVec: *) source

Override:

Script#onMouseMove

Params:

NameTypeAttributeDescription
unitVec *
screenVec *

public onMouseUp(unitVec: *, screenVec: *, button: *) source

Override:

Script#onMouseUp

Params:

NameTypeAttributeDescription
unitVec *
screenVec *
button *

public onUpdate(deltaTime: *) source

Override:

Script#onUpdate

Params:

NameTypeAttributeDescription
deltaTime *

public releaseGamepad() source

Release acquired gamepad.

Example:

if (gameOver) { component.releaseGamepad(); }

public setAcquiredGamepad(index: number) source

Manually acquire gamepad by it's index.

Params:

NameTypeAttributeDescription
index number

Gamepad index.

Example:

component.setAcquiredGamepad(0);

public setAxis(id: string, value: number) source

Set given axis value.

Params:

NameTypeAttributeDescription
id string

Axis id.

value number

New axis value.

Example:

// reset axis to prevent further usage in this frame.
component.setAxis('pos-x', 0);

public setTrigger(id: string, value: number) source

Set given trigger value.

Params:

NameTypeAttributeDescription
id string

Trigger id.

value number

New trigger value.

Example:

// reset trigger to prevent further usage in this frame.
component.setTrigger('pos-x', 0);

public setup(config: *) source

Setup axes and triggers.

Params:

NameTypeAttributeDescription
config *

Configuration object.

Example:

component.setup({ axes: { 'pos-x': { mouse: 'x' } }, triggers: { action: { key: 32 } } });