Skip to main content

Mouse

Description:

  An interface for handling mouse inputs.

position

Type: Readonly Field.

Description:

  The position of the mouse in the visible window's logical coordinate system, with the origin at the top-left. For a 2D scene, map it to View.size, apply the inverse Camera2D transform, and then call node:convertToNodeSpace(). Camera zoom must be divided rather than multiplied. For 3D node transforms or custom projections, use node:onMouseMove() and read touch.location instead.

Signature:

const position: Vec2

Usage:

local node = Node()
local camera = Director.currentCamera as Camera2D.Type
local mouse = Mouse.position
local visualSize = App.visualSize
local viewSize = View.size
local viewPos = Vec2(
mouse.x * viewSize.width / visualSize.width - viewSize.width / 2,
viewSize.height / 2 - mouse.y * viewSize.height / visualSize.height
) / camera.zoom
local angle = math.rad(camera.rotation)
local worldPos = Vec2(
viewPos.x * math.cos(angle) - viewPos.y * math.sin(angle),
viewPos.x * math.sin(angle) + viewPos.y * math.cos(angle)
) + camera.position
local nodePos = node:convertToNodeSpace(worldPos)

delta

Type: Readonly Field.

Description:

  The accumulated mouse movement since the previous frame.

Signature:

const delta: Vec2

relativeMode

Type: Field.

Description:

  Whether relative mouse mode is enabled. Relative mode hides and captures the cursor.

Signature:

relativeMode: boolean

leftButtonPressed

Type: Readonly Field.

Description:

  Whether the left mouse button is being pressed down.

Signature:

const leftButtonPressed: boolean

rightButtonPressed

Type: Readonly Field.

Description:

  Whether the right mouse button is being pressed down.

Signature:

const rightButtonPressed: boolean

middleButtonPressed

Type: Readonly Field.

Description:

  Whether the middle mouse button is being pressed down.

Signature:

const middleButtonPressed: boolean

wheel

Type: Readonly Field.

Description:

  The mouse wheel value.

Signature:

const wheel: Vec2