Skip to content

Commit 3cfa65b

Browse files
committed
Add client Cursor functions
This is a first draft for functions and the schema.
1 parent c98cea3 commit 3cfa65b

27 files changed

+364
-0
lines changed

.vscode/extensions.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"redhat.vscode-yaml"
4+
]
5+
}

.vscode/settings.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"yaml.schemas": {
3+
"schemas/function.yaml": "/functions/**/*"
4+
}
5+
}

articles/.gitkeep

Whitespace-only changes.

assets/setCursorAlpha.jpg

9.96 KB
Loading

elements/.gitkeep

Whitespace-only changes.

events/.gitkeep

Whitespace-only changes.

functions/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Simple command to test the getCursorAlpha function
2+
addCommandHandler( "cursorAlpha",
3+
function ()
4+
if ( isCursorShowing ( ) ) then
5+
outputChatBox( "The cursor alpha: "..getCursorAlpha( ) )
6+
else
7+
outputChatBox( "The cursor is not showing!" )
8+
end
9+
end
10+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function cursorInfo()
2+
if isCursorShowing() then -- if the cursor is showing
3+
local screenx, screeny, worldx, worldy, worldz = getCursorPosition()
4+
5+
outputChatBox( string.format( "Cursor screen position (relative): X=%.4f Y=%.4f", screenx, screeny ) ) -- make the accuracy of floats 4 decimals
6+
outputChatBox( string.format( "Cursor world position: X=%.4f Y=%.4f Z=%.4f", worldx, worldy, worldz ) ) -- make the accuracy of floats 4 decimals accurate
7+
else
8+
outputChatBox( "Your cursor is not showing." )
9+
end
10+
end
11+
addCommandHandler( "cursorpos", cursorInfo )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
addEventHandler( "onClientRender", root,
2+
function()
3+
if isCursorShowing() then
4+
local screenx, screeny, worldx, worldy, worldz = getCursorPosition()
5+
local px, py, pz = getCameraMatrix()
6+
local hit, x, y, z, elementHit = processLineOfSight ( px, py, pz, worldx, worldy, worldz )
7+
8+
if hit then
9+
dxDrawText( "Cursor at " .. x .. " " .. y .. " " .. z, 200, 200 )
10+
if elementHit then
11+
dxDrawText( "Hit element " .. getElementType(elementHit), 200, 220 )
12+
end
13+
end
14+
end
15+
end
16+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function toggleCursor(playerElement)
2+
if playerElement and isElement(playerElement) then -- Check whether the given element exists
3+
local cursorState = isCursorShowing(playerElement) -- Retrieve the state of the player's cursor
4+
local cursorStateOpposite = not cursorState -- The logical opposite of the cursor state
5+
6+
showCursor(playerElement, cursorStateOpposite) -- Setting the new cursor state
7+
end
8+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function outputCursor(playerElement)
2+
if playerElement and isElement(playerElement) then -- Check whether the given element exists
3+
local cursorState = isCursorShowing(playerElement) -- Retrieve the state of the player's cursor
4+
local cursorStateText = cursorState and "visible" or "hidden" -- Calculate the context from the boolean variable.
5+
6+
outputChatBox("Your cursor is " .. cursorStateText .. ".", playerElement) -- Output the text in the chatbox according to the cursor state.
7+
end
8+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function toggleCursor()
2+
local cursorState = isCursorShowing() -- Retrieve the state of the player's cursor
3+
local cursorStateOpposite = not cursorState -- The logical opposite of the cursor state
4+
5+
showCursor(cursorStateOpposite) -- Setting the new cursor state
6+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function toggleCursor()
2+
showCursor(not isCursorShowing())
3+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function toggleCursor()
2+
local cursorState = isCursorShowing() -- Retrieve the state of the player's cursor
3+
local cursorStateOpposite = not cursorState -- The logical opposite of the cursor state
4+
5+
showCursor(cursorStateOpposite) -- Setting the new cursor state
6+
end
7+
8+
bindKey("m", "down", toggleCursor) -- Assigning our toggleCursor function to the 'm' key press.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bindKey("m", "down",
2+
function()
3+
showCursor(not isCursorShowing())
4+
end
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Simple command to test the setCursorAlpha function
2+
addCommandHandler( "cursorAlpha",
3+
function ()
4+
-- Show the cursor if it is not showing or hide the cursor if it is
5+
showCursor( not isCursorShowing ( ) )
6+
-- Set the alpha to 100
7+
setCursorAlpha(100)
8+
end
9+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function centerCursorFunction()
2+
local showing = isCursorShowing ()
3+
if showing then -- if the cursor is showing
4+
local screenX, screenY = guiGetScreenSize () --get the screen size in pixels
5+
setCursorPosition (screenX/2, screenY/2) --set the cursor position to the center of the screen
6+
else
7+
outputChatBox( "Your cursor is not showing." )
8+
end
9+
end
10+
addCommandHandler( "cursorpos", centerCursorFunction )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local thePlayer = getPlayerFromName ( "Dave" ) -- get the player named Dave
2+
if thePlayer then -- if we got him
3+
showCursor ( thePlayer, true ) -- make his cursor show
4+
if isCursorShowing ( thePlayer ) then -- did it show?
5+
outputChatBox ( "Cursor is now showing for Dave." ) -- print a message to the chat box
6+
end
7+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
showCursor ( true ) -- Shows cursor
2+
showCursor ( false ) -- Doesnt Show Cursor

functions/Cursor/getCursorAlpha.yaml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
client:
2+
name: 'getCursorAlpha'
3+
pair: 'setCursorAlpha'
4+
description: |
5+
This function is used to get the client's cursor alpha (transparency).
6+
returns:
7+
description: |
8+
Returns a int between 0 and 255, where 255 is fully opaque and 0 is fully transparent.
9+
values:
10+
- type: 'int'
11+
name: 'alpha'
12+
version:
13+
added: '1.3.2'
14+
examples:
15+
- path: 'examples/getCursorAlpha.lua'
16+
see_also:
17+
- 'category:GUI functions'
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
client:
2+
name: 'getCursorPosition'
3+
pair: 'setCursorPosition'
4+
description: |
5+
This function gets the current position of the mouse cursor.
6+
Note that for performance reasons, the world position returned is always 300 units away.
7+
If you want the exact world point (similar to [[onClientClick]]), use [[processLineOfSight]] between the camera position and the worldX/Y/Z result of this function.
8+
(See example below)
9+
returns:
10+
description: |
11+
Returns 5 values: *cursorX*, *cursorY*, *worldX*, *worldY*, *worldZ*.
12+
The first two values are the 2D **relative** screen coordinates of the cursor.
13+
The 3 values that follow are the 3D world map coordinates that the cursor points at.
14+
If the cursor isn't showing, returns *false* as the first value.
15+
values:
16+
- type: 'float'
17+
name: 'cursorX'
18+
- type: 'float'
19+
name: 'cursorY'
20+
- type: 'float'
21+
name: 'worldX'
22+
- type: 'float'
23+
name: 'worldY'
24+
- type: 'float'
25+
name: 'worldZ'
26+
issues:
27+
- id: 1166
28+
description: 'getCursorPosition() returns false even when cursor is showing due to opened console'
29+
examples:
30+
- path: 'examples/getCursorPosition-1.lua'
31+
description: |
32+
This example prints your cursors current world coordinates and relative screen coordinates to chatbox after typing cursorpos.
33+
- path: 'examples/getCursorPosition-2.lua'
34+
description: |
35+
This (untested) example uses processLineOfSight to calculate the exact world location: Warning, using the script down there will cause high CPU usage.

functions/Cursor/isCursorShowing.yaml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
shared: &shared
2+
name: 'isCursorShowing'
3+
description: |
4+
This function determines the state of a player's cursor.
5+
notes:
6+
- This function only handles the cursor state set by the [[showCursor]] function, ignoring it if the console, chatbox, or menu is open.
7+
- If you use this function on the server-side, keep in mind that it only detects the [[showCursor]] function executed on the server-side and does not detect the function executed on the client-side.
8+
returns:
9+
description: |
10+
Returns *true* if the player's cursor is visible, and *false* if it is not.
11+
values:
12+
- type: 'bool'
13+
name: 'result'
14+
15+
server:
16+
<<: *shared
17+
parameters:
18+
- name: 'playerElement'
19+
type: 'player'
20+
description: 'The [[player]] from whom we want to retrieve the cursor state.'
21+
examples:
22+
- path: 'examples/isCursorShowing-1.lua'
23+
description: |
24+
This example creates a function to set the state of the player's cursor using the [[showCursor]] function.
25+
- path: 'examples/isCursorShowing-2.lua'
26+
description: |
27+
This example creates a function that gets the state of the player's cursor and outputs it to the chatbox using the [[outputChatBox]] function.
28+
29+
client:
30+
<<: *shared
31+
examples:
32+
- path: 'examples/isCursorShowing-3.lua'
33+
description: |
34+
This example creates a function to set the state of the player's cursor using the [[showCursor]] function.
35+
- path: 'examples/isCursorShowing-4.lua'
36+
description: |
37+
If you are already advanced in scripting, using this code is recommended, as it is much more compact:
38+
append: true
39+
- path: 'examples/isCursorShowing-5.lua'
40+
description: |
41+
This example creates a function that allows the player to change the state of the cursor using the [[showCursor]] and [[bindKey]] functions.
42+
- path: 'examples/isCursorShowing-6.lua'
43+
description: |
44+
If you are already advanced in scripting, using this code is recommended, as it is much more compact:
45+
append: true

functions/Cursor/setCursorAlpha.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
client:
2+
name: 'setCursorAlpha'
3+
pair: 'getCursorAlpha'
4+
description: |
5+
This function is used to change alpha (transparency) from the client's cursor.
6+
preview:
7+
- path: '/assets/setCursorAlpha.jpg'
8+
description: 'Example for setCursorAlpha'
9+
parameters:
10+
- name: 'alpha'
11+
type: 'int'
12+
description: 'The alpha value to set. Value can be 0-255, where 255 is fully opaque and 0 is fully transparent.'
13+
returns:
14+
description: |
15+
Returns *true* if the new alpha value was set, or *false* otherwise.
16+
values:
17+
- type: 'bool'
18+
name: 'result'
19+
version:
20+
added: '1.3.2'
21+
examples:
22+
- path: 'examples/setCursorAlpha.lua'
23+
see_also:
24+
- 'category:GUI functions'
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
client:
2+
name: 'setCursorPosition'
3+
pair: 'getCursorPosition'
4+
description: 'This function sets the current position of the mouse cursor.'
5+
parameters:
6+
- name: 'cursorX'
7+
type: 'int'
8+
description: 'Position over the X axis'
9+
- name: 'cursorY'
10+
type: 'int'
11+
description: 'Position over the Y axis'
12+
returns:
13+
description: 'Returns *true* if the position has been successfully set, *false* otherwise.'
14+
values:
15+
- type: 'bool'
16+
name: 'result'
17+
examples:
18+
- path: 'examples/setCursorPosition.lua'
19+
description: |
20+
This example sets your cursor position to the center of your screen after using the command *cursorpos*.
21+
see_also:
22+
- 'tag:Client input functions'

functions/Cursor/showCursor.yaml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
shared: &shared
2+
name: 'showCursor'
3+
description: |
4+
This function is used to show or hide a [[player]]'s cursor.
5+
notes:
6+
- Regardless of the cursor state you set using this function, the cursor will always be visible while the menu, the chatbox input line or the console are active, or if another resource has called this function.
7+
- Be aware of that if showCursor enbaled by a resource you can't disabled it from a different ressource showCursor(false) will not works, in order to make it works, disable it from the original resource that enabled it or use export.
8+
parameters:
9+
- name: 'thePlayer'
10+
type: 'player'
11+
description: 'The [[player]] you want to show or hide the cursor of.'
12+
- name: 'show'
13+
type: 'bool'
14+
description: 'A boolean value determining whether to show (*true*) or hide (*false*) the cursor.'
15+
- name: 'toggleControls'
16+
type: 'bool'
17+
description: |
18+
A boolean value determining whether to disable controls whilst the cursor is showing. *true* implies controls are disabled, *false* implies controls remain enabled.
19+
returns:
20+
description: |
21+
Returns *true* if the player's cursor was shown or hidden successfully, *false* otherwise.
22+
values:
23+
- type: 'bool'
24+
name: 'result'
25+
26+
server:
27+
<<: *shared
28+
examples:
29+
- path: 'examples/showCursor-1.lua'
30+
description: |
31+
This example shows the cursor for a player named "Dave", then outputs a message if it was shown successfully.
32+
33+
client:
34+
<<: *shared
35+
ignore_parameters:
36+
- 'thePlayer'
37+
examples:
38+
- path: 'examples/showCursor-2.lua'
39+
description: |
40+
This example shows the cursor all the time

schemas/function.yaml

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
$schema: https://json-schema.org/draft/2020-12/schema
2+
$id: /schemas/function
3+
title: Function schema
4+
type: object
5+
6+
$defs:
7+
common_properties:
8+
type: object
9+
required:
10+
- name
11+
properties:
12+
name:
13+
type: string
14+
description: Name of the function.
15+
pair:
16+
type: string
17+
description: Associates this function with another getter or setter function.
18+
description:
19+
type: string
20+
description: Describes the functionality provided by the function.
21+
version:
22+
description: Version information when the function got added/deprecated/removed.
23+
$ref: '#/$defs/version'
24+
examples:
25+
$ref: '#/$defs/examples'
26+
27+
version:
28+
type: object
29+
properties:
30+
added:
31+
type: string
32+
description: Version when this item was added to MTA.
33+
removed:
34+
type: string
35+
description: Version when this item was removed from MTA.
36+
deprecated:
37+
type: string
38+
description: Version when this item was deprecated in MTA.
39+
40+
examples:
41+
description: A list of source code examples demonstrating the function.
42+
type: array
43+
items:
44+
type: object
45+
required:
46+
- path
47+
properties:
48+
path:
49+
type: string
50+
description: A relative or repository-absolute path to an example source file.
51+
description:
52+
type: string
53+
description: Description for this source code example.
54+
append:
55+
type: boolean
56+
default: false
57+
description: If set to true, this example will be appended to the previous example.
58+
59+
properties:
60+
shared:
61+
description: A shared specification of a function.
62+
$ref: '#/$defs/common_properties'
63+
server:
64+
description: A server-side specification of a function.
65+
$ref: '#/$defs/common_properties'
66+
client:
67+
description: A client-side specification of a function.
68+
$ref: '#/$defs/common_properties'

0 commit comments

Comments
 (0)