Difference between revisions of "Global Command: getCursorPos"
From The Official Visionaire Studio: Adventure Game Engine Wiki
(9 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | {| class=" | + | Get the current position of the mouse cursor. |
− | + | ||
+ | Note that the function returns the cursor position in relation to the screen instead of in relation to the scene. This difference matters if your scene is larger than your screen resolution and thus scrollable. Have a look at example 2 on how to deal with this. | ||
+ | |||
+ | {| class="ts" | ||
|- | |- | ||
− | | | + | | style="width:15%" | Related functions |
+ | | [[Global Command: setCursorPos|setCursorPos]] | ||
|} | |} | ||
− | + | == Syntax == | |
+ | <syntaxhighlight lang="lua"> | ||
+ | getCursorPos() | ||
+ | </syntaxhighlight> | ||
− | + | == Parameters == | |
− | + | ||
− | + | none | |
− | + | ||
+ | |||
+ | == Return values == | ||
+ | A Visionaire "t_point" field holding the current mouse cursor position is returned. That's an associative array containing the elements '''x''' and '''y'''. | ||
+ | |||
+ | {| class="ts" | ||
+ | |- | ||
+ | ! style="width:15%" colspan="2" | Type/Structure | ||
+ | ! Description | ||
+ | |- | ||
+ | | rowspan="2" | table | ||
+ | | x (int) | ||
+ | | Horizontal position (X coordinate) of current mouse cursor position | ||
+ | |- | ||
+ | | y (int) | ||
+ | | Vertical position (Y coordinate) of current mouse cursor position | ||
+ | |} | ||
+ | |||
+ | == Examples == | ||
− | Example: | + | '''Example 1:''' Check if the current cursor position matches a defined position. |
− | <syntaxhighlight> | + | <syntaxhighlight lang ="lua"> |
− | -- | + | -- Store the current position into a variable |
local curPos = getCursorPos() | local curPos = getCursorPos() | ||
− | -- | + | -- Check if the stored cursor position equals another {x,y} value |
if curPos.x == 200 and curPos.y == 400 then | if curPos.x == 200 and curPos.y == 400 then | ||
− | + | return true | |
else | else | ||
− | + | return false | |
end | end | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | '''Example 2:''' Function to get the cursor position in relation to the scene by taking the current scroll position into account. | ||
+ | <syntaxhighlight lang ="lua"> | ||
+ | function getAbsoluteCursorPos() | ||
+ | -- Store the current position into a variable | ||
+ | local curPos = getCursorPos() | ||
− | + | -- Empty table to store the absolute position | |
+ | local curAbsPos = {} | ||
− | + | -- Add the current scroll position to the cursor position | |
+ | curAbsPos.x = game.ScrollPosition.x + curPos.x | ||
+ | curAbsPos.y = game.ScrollPosition.y + curPos.y | ||
− | + | return curAbsPos | |
− | + | end | |
− | + | </syntaxhighlight> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{{toc}} | {{toc}} |
Latest revision as of 16:10, 2 May 2023
Get the current position of the mouse cursor.
Note that the function returns the cursor position in relation to the screen instead of in relation to the scene. This difference matters if your scene is larger than your screen resolution and thus scrollable. Have a look at example 2 on how to deal with this.
Related functions | setCursorPos |
Syntax
getCursorPos()
Parameters
none
Return values
A Visionaire "t_point" field holding the current mouse cursor position is returned. That's an associative array containing the elements x and y.
Type/Structure | Description | |
---|---|---|
table | x (int) | Horizontal position (X coordinate) of current mouse cursor position |
y (int) | Vertical position (Y coordinate) of current mouse cursor position |
Examples
Example 1: Check if the current cursor position matches a defined position.
-- Store the current position into a variable
local curPos = getCursorPos()
-- Check if the stored cursor position equals another {x,y} value
if curPos.x == 200 and curPos.y == 400 then
return true
else
return false
end
Example 2: Function to get the cursor position in relation to the scene by taking the current scroll position into account.
function getAbsoluteCursorPos()
-- Store the current position into a variable
local curPos = getCursorPos()
-- Empty table to store the absolute position
local curAbsPos = {}
-- Add the current scroll position to the cursor position
curAbsPos.x = game.ScrollPosition.x + curPos.x
curAbsPos.y = game.ScrollPosition.y + curPos.y
return curAbsPos
end