indiesoftby/defold-pointer-lock/pointer_lock
Mouse lock state and a virtual in-game cursor. Add pointer_lock/pointer_lock.script
to the bootstrap collection: it locks the cursor on a click and unlocks it on
key_esc, and keeps locked current. The cursor fields are plain module fields;
assigning one goes through a cast, since a namespace import is read-only:
(pointer as { cursor_visible: boolean }).cursor_visible = true.
- GitHub: indiesoftby/defold-pointer-lock — pinned to
2.1.0- Pick a release from indiesoftby/defold-pointer-lock releases and add its Source code (zip) URL (or a packaged
.zipasset, if the library ships one) togame.projectunder[project]dependencies, then Fetch Libraries in the Defold editor. - Run
bunx @defold-typescript/cli resolveto materialize its types. - Import it under a namespace alias of your choice:
import * as pointer_lock from "pointer_lock.pointer"
- Pick a release from indiesoftby/defold-pointer-lock releases and add its Source code (zip) URL (or a packaged
Functions
transform_input<A extends { x?: number; y?: number; }>(action_id: Hash | undefined, action: A): LuaMultiReturn<[ Hash | undefined, A & { x: number; y: number; } ]>
Converts on_input arguments into the virtual in-game cursor system: sets
action.x and action.y to the cursor position, on the same table it returns, and
returns undefined as the action id unless the pointer is locked and the cursor is
visible. Call it first in on_input:
[action_id, action] = pointer.transform_input(action_id, action).
Parameters
action_id:Hash | undefinedaction:A
Returns
LuaMultiReturn<[ Hash | undefined, A & { x: number; y: number; } ]>
update_cursor_position(action: { dx: number; dy: number; })
Moves the virtual cursor by the action's dx and dy, clamped to the display
size. Pass it a pointer-movement action, which carries both.
Parameters
action:{ dx: number; dy: number; }dx:numberdy:number
update_lock_state()
Copies the window's mouse lock state into locked.
Variables
DISPLAY_WIDTH: number
The display.width project setting, read once when the module loads.
DISPLAY_HEIGHT: number
The display.height project setting, read once when the module loads.
locked: boolean
Whether the pointer is locked. Read-only for a project: pointer_lock.script sets
it, and update_lock_state copies the window's mouse lock into it. Starts false.
cursor_x: number
The X position of the virtual in-game cursor, between 0 and DISPLAY_WIDTH.
Starts at the middle of the display; update_cursor_position moves it.
cursor_y: number
The Y position of the virtual in-game cursor, between 0 and DISPLAY_HEIGHT.
Starts at the middle of the display; update_cursor_position moves it.
cursor_visible: boolean
Whether the virtual in-game cursor is shown. Starts false. transform_input
passes the action id through only while the pointer is locked and this is true.
cursor_render_order: number
The GUI render order of the virtual cursor, 15 being the top. Starts at 15.