InputManager
Description:
InputManager
is a class for managing input contexts and actions. Input events can be listened for and handled by creating input contexts and actions, and then adding them to the input manager. Specific combinations of input contexts can be activated and deactivated by calling the pushContext
and popContext
methods. When an event is triggered, input events can be handled by registering global input event listeners.
Class Object: InputManager Class.
Usage:
local InputManager = require("InputManager")
local inputManager = InputManager.CreateManager({
{
name = "context1",
actions = {
{
name = "action1",
trigger = Trigger.KeyDown("W")
},
}
},
})
-- activate context1
inputManager:pushContext("context1")
-- add prefix "Input." to the listened action name
node:gslot("Input.action1", function()
print("action1 triggered")
end)
-- remove context1 from the context stack
inputManager:popContext()
inputManager:destroy()
getNode
Type: Function.
Description:
Gets the current input node. The input node is used to receive input events. It will be added to Director.entry
automatically.
Signature:
getNode: function(self: InputManager): Node.Type
Returns:
Return Type | Description |
---|---|
Node | The input node. |
pushContext
Type: Function.
Description:
Adds an input context to the context stack. Temporarily disables the previous context, then activates the actions in the new context.
Signature:
pushContext: function(self: InputManager, contextNames: string | {string}): boolean
Parameters:
Parameter | Type | Description |
---|---|---|
contextNames | string | {string} |
Returns:
Return Type | Description |
---|---|
boolean | Whether the context is successfully pushed. |
popContext
Type: Function.
Description:
Removes the current input context from the context stack. Activates the previous context.
Signature:
popContext: function(self: InputManager, count?: number): boolean
Parameters:
Parameter | Type | Description |
---|---|---|
count | number | [optional] The number of contexts to be popped. Default is 1. |
Returns:
Return Type | Description |
---|---|
boolean | Whether the context is successfully popped. |
emitKeyDown
Type: Function.
Description:
Emits a key down event for input simulation.
Signature:
emitKeyDown: function(self: InputManager, keyName: KeyName)
Parameters:
Parameter | Type | Description |
---|---|---|
keyName | KeyName | The name of the key. |
emitKeyUp
Type: Function.
Description:
Emits a key up event for input simulation.
Signature:
emitKeyUp: function(self: InputManager, keyName: KeyName)
Parameters:
Parameter | Type | Description |
---|---|---|
keyName | KeyName | The name of the key. |
emitButtonDown
Type: Function.
Description:
Emits a button down event for input simulation.
Signature:
emitButtonDown: function(self: InputManager, buttonName: ButtonName, controllerId?: number)
Parameters:
Parameter | Type | Description |
---|---|---|
buttonName | ButtonName | The name of the button. |
controllerId | number | [optional] The ID of the gamepad controller. Default is 0. |
emitButtonUp
Type: Function.
Description:
Emits a button up event for input simulation.
Signature:
emitButtonUp: function(self: InputManager, buttonName: ButtonName, controllerId?: number)
Parameters:
Parameter | Type | Description |
---|---|---|
buttonName | ButtonName | The name of the button. |
controllerId | number | [optional] The ID of the gamepad controller. Default is 0. |
emitAxis
Type: Function.
Description:
Emits an axis event for input simulation.
Signature:
emitAxis: function(self: InputManager, axisName: AxisName, value: number, controllerId?: number)
Parameters:
Parameter | Type | Description |
---|---|---|
axisName | AxisName | The name of the axis. |
value | number | The value of the axis, ranging from -1 to 1. |
controllerId | number | [optional] The ID of the gamepad controller. Default is 0. |
destroy
Type: Function.
Description:
Destroys the input manager and clears input event listeners.
Signature:
destroy: function(self: InputManager)