Input
in package
implements
WindowEventHandlerInterface
This class is responsible for handling window events, and includes a bunch of helpers and utility methods to easly handle user input.
Table of Contents
Interfaces
Constants
- EVENT_CHAR = 'input.char'
- EVENT_CHAR_MOD = 'input.char_mods'
- EVENT_CURSOR = 'input.cursor'
- EVENT_CURSOR_ENTER = 'input.cursor_enter'
- EVENT_DROP = 'input.drop'
- EVENT_KEY = 'input.key'
- The event names the input class will dispatch on
- EVENT_MOUSE_BUTTON = 'input.mouse_button'
- EVENT_MOUSE_CLICK = 'input.mouse_click'
- EVENT_SCROLL = 'input.scroll'
- PRESS = GLFW_PRESS
- We warp the GLFW state constants to make the syntax a bit more eye pleasing.
- RELEASE = GLFW_RELEASE
- REPEAT = GLFW_REPEAT
Properties
- $currentInputContext : string|null
- Current input context is just represented by a string.
- $dispatcher : DispatcherInterface
- $glfwWindowHandle : GLFWwindow
- GLFW window instance.
- $keysDidPress : array<int, bool>
- An array of all keys that have been pressed since the last poll
- $keysDidPressFrame : array<int, bool>
- Same as "keysDidPress" but for per frame state instead of per poll state
- $keysDidRelease : array<int, bool>
- An array of all keys that have been released since the last poll
- $keysDidReleaseFrame : array<int, bool>
- Same as "keysDidRelease" but for per frame state instead of per poll state
- $lastCursorPosition : Vec2
- The last polled cursor position
- $lastLeftMouseDownPosition : Vec2
- Last left mouse button pressed down cursor position
- $lastLeftMouseReleasePosition : Vec2
- Last left mouse button released cursor position
- $mouseButtonsDidPress : array<int, bool>
- An array of all mouse buttons that have been pressed since the last poll
- $mouseButtonsDidPressFrame : array<int, bool>
- Same as "mouseButtonsDidPress" but for per frame state instead of per poll state
- $mouseButtonsDidRelease : array<int, bool>
- An array of all mouse buttons that have been released since the last poll
- $mouseButtonsDidReleaseFrame : array<int, bool>
- Same as "mouseButtonsDidRelease" but for per frame state instead of per poll state
- $mouseClickMaxDistanceFromStart : float
- The maximum distance the cursor can move between a mouse down and mouse up event to still trigger a mouse click event.
Methods
- __construct() : void
- Constructor for the Input class.
- claimContext() : void
- Claim the input context
- endFrame() : void
- Invoke this method at the end of a frame to let the input manager apply state changes to the per frame state.
- getCurrentContext() : string|null
- Get the current input context
- getCursorMode() : CursorMode
- Get the current cursor mode
- getCursorPosition() : Vec2
- Get the current cursor position
- getKeyPresses() : array<string|int, int>
- Returns an array of all currently pressed keys
- getKeyPressesThisFrame() : array<string|int, int>
- Returns an array of all keys pressed this frame
- getKeyState() : int
- Get the state for a given key
- getLastCursorPosition() : Vec2
- Get the last recieved cursor position This represents the last position the cursor was at before the current position, !Note: This will be overwritten after the `input.cursor` events are dispatched.
- getLastLeftMouseDownPosition() : Vec2
- Returns the last mouse down position
- getLastLeftMouseReleasePosition() : Vec2
- Returns the last mouse release position
- getMouseButtonState() : int
- Get the state for a given mouse button
- getNormalizedCursorPosition() : Vec2
- Returns the normalized cursor position
- handleWindowChar() : void
- Window char event callback This method is invoked when a character is inputted (e.g. when typing).
- handleWindowCharMods() : void
- Window char mods event callback This method is invoked when a character is inputted (e.g. when typing).
- handleWindowCursorEnter() : void
- Window cursor enter event callback This method is invoked when the cursor enters or leaves the client area of the window.
- handleWindowCursorPos() : void
- Window cursor position event callback This method is invoked when the cursor is moved.
- handleWindowDidPollEvents() : void
- Callback after new evenets are polled This method is invoked after new events are polled think of it as a post-update method.
- handleWindowDrop() : void
- Window drop event callback This method is invoked when one or more dragged files are dropped on the window.
- handleWindowKey() : void
- Window key event callback This method is invoked when a key is pressed, repeated or released.
- handleWindowMouseButton() : void
- Window mouse button event callback This method is invoked when a mouse button is pressed or released.
- handleWindowScroll() : void
- Window scroll event callback This method is invoked when a scrolling device is used, such as a mouse wheel or scrolling area of a touchpad.
- handleWindowWillPollEvents() : void
- Callback before new evenets are polled This method is invoked before new events are polled think of it as a pre-update method.
- hasKeyBeenPressed() : bool
- Returns boolean if the given key was pressed since the last poll
- hasKeyBeenPressedThisFrame() : bool
- Returns boolean if the given key was pressed since the last frame
- hasKeyBeenReleased() : bool
- Returns boolean if the given key was released since the last poll
- hasKeyBeenReleasedThisFrame() : bool
- Returns boolean if the given key was released since the last frame
- hasMouseButtonBeenPressed() : bool
- Returns boolean if the given mouse button was pressed since the last poll
- hasMouseButtonBeenPressedThisFrame() : bool
- Returns boolean if the given mouse button was pressed since the last frame
- hasMouseButtonBeenReleased() : bool
- Returns boolean if the given mouse button was released since the last poll
- hasMouseButtonBeenReleasedThisFrame() : bool
- Returns boolean if the given mouse button was released since the last frame
- isClaimedContext() : bool
- Returns true if the given input context is currently claimed
- isContextUnclaimed() : bool
- Is the input context currently claimed?
- isKeyPressed() : bool
- Returns boolean if the given key is pressed
- isKeyReleased() : bool
- Returns boolean if the given key is released
- isKeyRepeated() : bool
- Returns boolean if the given key is repeated
- isMouseButtonPressed() : bool
- Returns boolean if the given mouse button is pressed
- isMouseButtonReleased() : bool
- Returns boolean if the given mouse button is released
- releaseContext() : void
- Release the input context
- setCursorMode() : void
- Set the cursor mode
- setCursorPosition() : void
- Set the cursor position
Constants
EVENT_CHAR
public
mixed
EVENT_CHAR
= 'input.char'
EVENT_CHAR_MOD
public
mixed
EVENT_CHAR_MOD
= 'input.char_mods'
EVENT_CURSOR
public
mixed
EVENT_CURSOR
= 'input.cursor'
EVENT_CURSOR_ENTER
public
mixed
EVENT_CURSOR_ENTER
= 'input.cursor_enter'
EVENT_DROP
public
mixed
EVENT_DROP
= 'input.drop'
EVENT_KEY
The event names the input class will dispatch on
public
mixed
EVENT_KEY
= 'input.key'
EVENT_MOUSE_BUTTON
public
mixed
EVENT_MOUSE_BUTTON
= 'input.mouse_button'
EVENT_MOUSE_CLICK
public
mixed
EVENT_MOUSE_CLICK
= 'input.mouse_click'
EVENT_SCROLL
public
mixed
EVENT_SCROLL
= 'input.scroll'
PRESS
We warp the GLFW state constants to make the syntax a bit more eye pleasing.
public
mixed
PRESS
= GLFW_PRESS
RELEASE
public
mixed
RELEASE
= GLFW_RELEASE
REPEAT
public
mixed
REPEAT
= GLFW_REPEAT
Properties
$currentInputContext
Current input context is just represented by a string.
private
string|null
$currentInputContext
= null
The input context can be used as mutex like flag so that different systems can use the input class without interfering with each other.
$dispatcher
private
DispatcherInterface
$dispatcher
$glfwWindowHandle
GLFW window instance.
private
GLFWwindow
$glfwWindowHandle
required to fetch key states. We copy a reference to the raw window
so we don't always have to use getGLFWHandle everytime we want to check
a windows key state.
$keysDidPress
An array of all keys that have been pressed since the last poll
private
array<int, bool>
$keysDidPress
= []
$keysDidPressFrame
Same as "keysDidPress" but for per frame state instead of per poll state
private
array<int, bool>
$keysDidPressFrame
= []
$keysDidRelease
An array of all keys that have been released since the last poll
private
array<int, bool>
$keysDidRelease
= []
$keysDidReleaseFrame
Same as "keysDidRelease" but for per frame state instead of per poll state
private
array<int, bool>
$keysDidReleaseFrame
= []
$lastCursorPosition
The last polled cursor position
private
Vec2
$lastCursorPosition
$lastLeftMouseDownPosition
Last left mouse button pressed down cursor position
private
Vec2
$lastLeftMouseDownPosition
This is needed to calculate the delta needed to cancel click events
$lastLeftMouseReleasePosition
Last left mouse button released cursor position
private
Vec2
$lastLeftMouseReleasePosition
This is needed to calculate the delta needed to cancel click events
$mouseButtonsDidPress
An array of all mouse buttons that have been pressed since the last poll
private
array<int, bool>
$mouseButtonsDidPress
= []
$mouseButtonsDidPressFrame
Same as "mouseButtonsDidPress" but for per frame state instead of per poll state
private
array<int, bool>
$mouseButtonsDidPressFrame
= []
$mouseButtonsDidRelease
An array of all mouse buttons that have been released since the last poll
private
array<int, bool>
$mouseButtonsDidRelease
= []
$mouseButtonsDidReleaseFrame
Same as "mouseButtonsDidRelease" but for per frame state instead of per poll state
private
array<int, bool>
$mouseButtonsDidReleaseFrame
= []
$mouseClickMaxDistanceFromStart
The maximum distance the cursor can move between a mouse down and mouse up event to still trigger a mouse click event.
private
float
$mouseClickMaxDistanceFromStart
= 10.0
Methods
__construct()
Constructor for the Input class.
public
__construct(Window $window, DispatcherInterface $dispatcher) : void
Parameters
- $window : Window
-
The window instance to handle input for.
- $dispatcher : DispatcherInterface
-
The dispatcher instance events will be dispatched to.
claimContext()
Claim the input context
public
claimContext(string $context) : void
Parameters
- $context : string
-
The context string to claim
endFrame()
Invoke this method at the end of a frame to let the input manager apply state changes to the per frame state.
public
endFrame() : void
getCurrentContext()
Get the current input context
public
getCurrentContext() : string|null
Return values
string|null —The current input context or null if no context is claimed
getCursorMode()
Get the current cursor mode
public
getCursorMode() : CursorMode
Return values
CursorMode —The current cursor mode
getCursorPosition()
Get the current cursor position
public
getCursorPosition() : Vec2
Return values
Vec2 —The current cursor position
getKeyPresses()
Returns an array of all currently pressed keys
public
getKeyPresses() : array<string|int, int>
This method returns the key codes of all keys that were pressed since the last poll event.
Example:
$pressedKeys = $input->getKeyPresses();
foreach ($pressedKeys as $key) {
echo "Key $key was pressed\n";
}
Return values
array<string|int, int> —Array of key codes that were pressed since last poll
getKeyPressesThisFrame()
Returns an array of all keys pressed this frame
public
getKeyPressesThisFrame() : array<string|int, int>
This method returns the key codes of all keys that were pressed since the last frame.
Example:
$pressedKeys = $input->getKeyPressesThisFrame();
foreach ($pressedKeys as $key) {
echo "Key $key was pressed this frame\n";
}
Return values
array<string|int, int> —Array of key codes that were pressed this frame
getKeyState()
Get the state for a given key
public
getKeyState(int $key) : int
Can return one of the following values:
Input::PRESSInput::RELEASEInput::REPEAT
Parameters
- $key : int
-
The key to get the state for
Return values
int —The state of the key
getLastCursorPosition()
Get the last recieved cursor position This represents the last position the cursor was at before the current position, !Note: This will be overwritten after the `input.cursor` events are dispatched.
public
getLastCursorPosition() : Vec2
Return values
Vec2getLastLeftMouseDownPosition()
Returns the last mouse down position
public
getLastLeftMouseDownPosition() : Vec2
Return values
Vec2getLastLeftMouseReleasePosition()
Returns the last mouse release position
public
getLastLeftMouseReleasePosition() : Vec2
Return values
Vec2getMouseButtonState()
Get the state for a given mouse button
public
getMouseButtonState(int $button) : int
Can return one of the following values:
Input::PRESSInput::RELEASE
Parameters
- $button : int
-
The mouse button to get the state for
Return values
int —The state of the mouse button
getNormalizedCursorPosition()
Returns the normalized cursor position
public
getNormalizedCursorPosition() : Vec2
This is the cursor position normalized to the range of -1.0 to 1.0
Return values
Vec2 —The normalized cursor position
handleWindowChar()
Window char event callback This method is invoked when a character is inputted (e.g. when typing).
public
handleWindowChar(Window $window, int $char) : void
Parameters
- $window : Window
-
The window that received the event
- $char : int
-
The Unicode code point of the character
handleWindowCharMods()
Window char mods event callback This method is invoked when a character is inputted (e.g. when typing).
public
handleWindowCharMods(Window $window, int $char, int $mods) : void
Parameters
- $window : Window
-
The window that received the event
- $char : int
-
The Unicode code point of the character
- $mods : int
-
Bit field describing which modifier keys were held down
handleWindowCursorEnter()
Window cursor enter event callback This method is invoked when the cursor enters or leaves the client area of the window.
public
handleWindowCursorEnter(Window $window, int $entered) : void
Parameters
- $window : Window
-
The window that received the event
- $entered : int
-
True if the cursor entered the window's client area, or false if it left it
handleWindowCursorPos()
Window cursor position event callback This method is invoked when the cursor is moved.
public
handleWindowCursorPos(Window $window, float $xpos, float $ypos) : void
Parameters
- $window : Window
-
The window that received the event
- $xpos : float
-
The new x-coordinate, in screen coordinates, of the cursor
- $ypos : float
-
The new y-coordinate, in screen coordinates, of the cursor
handleWindowDidPollEvents()
Callback after new evenets are polled This method is invoked after new events are polled think of it as a post-update method.
public
handleWindowDidPollEvents(Window $window) : void
Parameters
- $window : Window
-
The window that received the event
handleWindowDrop()
Window drop event callback This method is invoked when one or more dragged files are dropped on the window.
public
handleWindowDrop(Window $window, array<string|int, string> $paths) : void
Parameters
- $window : Window
-
The window that received the event
- $paths : array<string|int, string>
-
The UTF-8 encoded file and/or directory path names
handleWindowKey()
Window key event callback This method is invoked when a key is pressed, repeated or released.
public
handleWindowKey(Window $window, int $key, int $scancode, int $action, int $mods) : void
Parameters
- $window : Window
-
The window that received the event
- $key : int
-
The key that was pressed, repeated or released
- $scancode : int
-
The system-specific scancode of the key
- $action : int
-
The key action. One of: GLFW_PRESS, GLFW_RELEASE or GLFW_REPEAT
- $mods : int
-
Bit field describing which modifier keys were held down
handleWindowMouseButton()
Window mouse button event callback This method is invoked when a mouse button is pressed or released.
public
handleWindowMouseButton(Window $window, int $button, int $action, int $mods) : void
Parameters
- $window : Window
-
The window that received the event
- $button : int
-
The mouse button that was pressed or released
- $action : int
-
The mouse button action. One of: GLFW_PRESS or GLFW_RELEASE
- $mods : int
-
Bit field describing which modifier keys were held down
handleWindowScroll()
Window scroll event callback This method is invoked when a scrolling device is used, such as a mouse wheel or scrolling area of a touchpad.
public
handleWindowScroll(Window $window, float $xoffset, float $yoffset) : void
Parameters
- $window : Window
-
The window that received the event
- $xoffset : float
-
The scroll offset along the x-axis
- $yoffset : float
-
The scroll offset along the y-axis
handleWindowWillPollEvents()
Callback before new evenets are polled This method is invoked before new events are polled think of it as a pre-update method.
public
handleWindowWillPollEvents(Window $window) : void
Parameters
- $window : Window
-
The window that received the event
hasKeyBeenPressed()
Returns boolean if the given key was pressed since the last poll
public
hasKeyBeenPressed(int $key) : bool
Example:
$input->hasKeyBeenPressed(Key::SPACE);
Parameters
- $key : int
-
The key to check
Return values
bool —True if the key was pressed, false otherwise
hasKeyBeenPressedThisFrame()
Returns boolean if the given key was pressed since the last frame
public
hasKeyBeenPressedThisFrame(int $key) : bool
Example:
$input->hasKeyBeenPressedThisFrame(Key::SPACE);
Parameters
- $key : int
-
The key to check
Return values
bool —True if the key was pressed, false otherwise
hasKeyBeenReleased()
Returns boolean if the given key was released since the last poll
public
hasKeyBeenReleased(int $key) : bool
Example:
$input->hasKeyBeenReleased(Key::SPACE);
Parameters
- $key : int
-
The key to check
Return values
bool —True if the key was released, false otherwise
hasKeyBeenReleasedThisFrame()
Returns boolean if the given key was released since the last frame
public
hasKeyBeenReleasedThisFrame(int $key) : bool
Example:
$input->hasKeyBeenReleasedThisFrame(Key::SPACE);
Parameters
- $key : int
-
The key to check
Return values
bool —True if the key was released, false otherwise
hasMouseButtonBeenPressed()
Returns boolean if the given mouse button was pressed since the last poll
public
hasMouseButtonBeenPressed(int $button) : bool
Example:
$input->hasMouseButtonBeenPressed(MouseButton::LEFT);
Parameters
- $button : int
-
The mouse button to check
Return values
bool —True if the mouse button was pressed, false otherwise
hasMouseButtonBeenPressedThisFrame()
Returns boolean if the given mouse button was pressed since the last frame
public
hasMouseButtonBeenPressedThisFrame(int $button) : bool
Example:
$input->hasMouseButtonBeenPressedThisFrame(MouseButton::LEFT);
Parameters
- $button : int
-
The mouse button to check
Return values
bool —True if the mouse button was pressed, false otherwise
hasMouseButtonBeenReleased()
Returns boolean if the given mouse button was released since the last poll
public
hasMouseButtonBeenReleased(int $button) : bool
Example:
$input->hasMouseButtonBeenReleased(MouseButton::LEFT);
Parameters
- $button : int
-
The mouse button to check
Return values
bool —True if the mouse button was released, false otherwise
hasMouseButtonBeenReleasedThisFrame()
Returns boolean if the given mouse button was released since the last frame
public
hasMouseButtonBeenReleasedThisFrame(int $button) : bool
Example:
$input->hasMouseButtonBeenReleasedThisFrame(MouseButton::LEFT);
Parameters
- $button : int
-
The mouse button to check
Return values
bool —True if the mouse button was released, false otherwise
isClaimedContext()
Returns true if the given input context is currently claimed
public
isClaimedContext(string $context) : bool
Parameters
- $context : string
-
The context string to check
Return values
bool —True if the given input context is currently claimed
isContextUnclaimed()
Is the input context currently claimed?
public
isContextUnclaimed() : bool
Return values
boolisKeyPressed()
Returns boolean if the given key is pressed
public
isKeyPressed(int $key) : bool
Example:
$input->isKeyPressed(Key::SPACE);
Parameters
- $key : int
-
The key to check
Return values
bool —True if the key is pressed, false otherwise
isKeyReleased()
Returns boolean if the given key is released
public
isKeyReleased(int $key) : bool
Example:
$input->isKeyReleased(Key::SPACE);
Parameters
- $key : int
-
The key to check
Return values
bool —True if the key is released, false otherwise
isKeyRepeated()
Returns boolean if the given key is repeated
public
isKeyRepeated(int $key) : bool
Example:
$input->isKeyRepeated(Key::SPACE);
Parameters
- $key : int
-
The key to check
Return values
bool —True if the key is repeated, false otherwise
isMouseButtonPressed()
Returns boolean if the given mouse button is pressed
public
isMouseButtonPressed(int $button) : bool
Example:
$input->isMouseButtonPressed(MouseButton::LEFT);
Parameters
- $button : int
-
The mouse button to check
Return values
bool —True if the mouse button is pressed, false otherwise
isMouseButtonReleased()
Returns boolean if the given mouse button is released
public
isMouseButtonReleased(int $button) : bool
Example:
$input->isMouseButtonReleased(MouseButton::LEFT);
Parameters
- $button : int
-
The mouse button to check
Return values
bool —True if the mouse button is released, false otherwise
releaseContext()
Release the input context
public
releaseContext(string $context) : void
Parameters
- $context : string
-
The context string to release
setCursorMode()
Set the cursor mode
public
setCursorMode(CursorMode $mode) : void
Available modes:
CursorMode::NORMALCursorMode::HIDDENCursorMode::DISABLED
Parameters
- $mode : CursorMode
-
The mode to set the cursor to
setCursorPosition()
Set the cursor position
public
setCursorPosition(Vec2 $position) : void
Parameters
- $position : Vec2
-
The position to set the cursor to