Skip to content

Commit c8ebf10

Browse files
move_cursor: refactor code (#310)
* Refactor move_cursor * Update [editor]/move_cursor/move_cursor.lua Co-authored-by: Breno Danyel <[email protected]> * Update [editor]/move_cursor/move_cursor.lua Co-authored-by: Breno Danyel <[email protected]> Co-authored-by: Breno Danyel <[email protected]>
1 parent 8af4f94 commit c8ebf10

File tree

1 file changed

+82
-106
lines changed

1 file changed

+82
-106
lines changed

[editor]/move_cursor/move_cursor.lua

Lines changed: 82 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ local ignoreElementWalls = true -- 'false' not supported yet
1414

1515
local isEnabled = false
1616

17-
local root = getRootElement()
18-
1917
local camX, camY, camZ
2018

2119
local selectedElement
@@ -36,27 +34,12 @@ local hasRotation = {
3634
ped = true,
3735
}
3836

39-
-- PRIVATE
4037
local onClientMouseMove
4138

42-
local mta_getElementRotation = getElementRotation
43-
local function getElementRotation(element)
44-
local elementType = getElementType(element)
45-
if elementType == "player" or elementType == "ped" then
46-
return 0,0,getPedRotation(element)
47-
elseif elementType == "object" then
48-
return mta_getElementRotation(element)
49-
elseif elementType == "vehicle" then
50-
return mta_getElementRotation(element)
51-
end
52-
end
53-
5439
local function getCoordsWithBoundingBox(origX, origY, origZ)
5540
if (not collisionless) then
5641
local newX, newY, newZ = origX, origY, origZ
57-
if (not ignoreElementWalls) then
58-
-- hard stuff
59-
else
42+
if ignoreElementWalls then
6043
local surfaceFound, surfaceX, surfaceY, surfaceZ, element = processLineOfSight(origX, origY, origZ + SURFACE_ERROR_CORRECTION_OFFSET, origX, origY, origZ + minZ, true, true, true, true, true, true, false, true, selectedElement)
6144
if (surfaceFound) then
6245
newZ = surfaceZ + centerToBaseDistance
@@ -68,87 +51,88 @@ local function getCoordsWithBoundingBox(origX, origY, origZ)
6851
end
6952
end
7053

71-
local function processCursorMove (absoluteX,absoluteY)
72-
if not absoluteX then
73-
local relX,relY = getCursorPosition()
74-
absoluteX,absoluteY = relX*g_screenX,relY*g_screenY
54+
local function processCursorMove(absoluteX, absoluteY)
55+
if not absoluteX or not absoluteY then
56+
local relX, relY = getCursorPosition()
57+
absoluteX, absoluteY = relX*g_screenX, relY*g_screenY
7558
end
7659
-- process line, checking for water and surfaces
7760
local worldX, worldY, worldZ = getWorldFromScreenPosition(absoluteX, absoluteY, MAX_DISTANCE )
7861
-- make sure there is a camera position
79-
if (not camX) then return end
80-
local surfaceFound, surfaceX, surfaceY, surfaceZ, element = processLineOfSight(camX, camY, camZ, worldX, worldY, worldZ, true, true, true, true, true, true, false, true, selectedElement)
81-
local waterFound, waterX, waterY, waterZ = testLineAgainstWater(camX, camY, camZ, worldX, worldY, worldZ)
82-
83-
-- check if surfaces are not too far
84-
local surfaceDistance
85-
local waterDistance
86-
if (surfaceFound) then
87-
surfaceDistance = math.sqrt((surfaceX - camX)^2 + (surfaceY - camY)^2 + (surfaceZ - camZ)^2)
88-
if (surfaceDistance > maxMoveDistance) then
89-
surfaceFound = false
90-
end
91-
end
92-
if (waterFound) then
93-
waterDistance = math.sqrt((waterX - camX)^2 + (waterY - camY)^2 + (waterZ - camZ)^2)
94-
if (waterDistance > maxMoveDistance) then
95-
waterFound = false
96-
end
97-
end
62+
if camX and camY then
63+
local surfaceFound, surfaceX, surfaceY, surfaceZ, element = processLineOfSight(camX, camY, camZ, worldX, worldY, worldZ, true, true, true, true, true, true, false, true, selectedElement)
64+
local waterFound, waterX, waterY, waterZ = testLineAgainstWater(camX, camY, camZ, worldX, worldY, worldZ)
9865

99-
-- raise height if pickup or a marker
100-
if (getElementType(selectedElement) == "pickup") or (getElementType(selectedElement) == "marker") then
66+
-- check if surfaces are not too far
67+
local surfaceDistance
68+
local waterDistance
10169
if (surfaceFound) then
102-
surfaceZ = surfaceZ + 1
70+
surfaceDistance = math.sqrt((surfaceX - camX)^2 + (surfaceY - camY)^2 + (surfaceZ - camZ)^2)
71+
if (surfaceDistance > maxMoveDistance) then
72+
surfaceFound = false
73+
end
10374
end
10475
if (waterFound) then
105-
waterZ = waterZ + 1
76+
waterDistance = math.sqrt((waterX - camX)^2 + (waterY - camY)^2 + (waterZ - camZ)^2)
77+
if (waterDistance > maxMoveDistance) then
78+
waterFound = false
79+
end
10680
end
107-
end
10881

109-
-- check if water or surface was found
110-
if (surfaceFound and waterFound) then
111-
-- if both found, compare distances
112-
if (waterDistance >= surfaceDistance) then
82+
-- raise height if pickup or a marker
83+
if (getElementType(selectedElement) == "pickup") or (getElementType(selectedElement) == "marker") then
84+
if (surfaceFound) then
85+
surfaceZ = surfaceZ + 1
86+
end
87+
if (waterFound) then
88+
waterZ = waterZ + 1
89+
end
90+
end
91+
92+
-- check if water or surface was found
93+
if (surfaceFound and waterFound) then
94+
-- if both found, compare distances
95+
if (waterDistance >= surfaceDistance) then
96+
if (not collisionless) then
97+
centerToBaseDistance = exports.edf:edfGetElementDistanceToBase(selectedElement)
98+
local finalX, finalY, finalZ = getCoordsWithBoundingBox(surfaceX, surfaceY, surfaceZ)
99+
setElementPosition(selectedElement, finalX, finalY, finalZ)
100+
else
101+
setElementPosition(selectedElement, surfaceX, surfaceY, surfaceZ)
102+
end
103+
else
104+
if (not collisionless) then
105+
centerToBaseDistance = exports.edf:edfGetElementDistanceToBase(selectedElement)
106+
local finalX, finalY, finalZ = getCoordsWithBoundingBox(waterX, waterY, waterZ)
107+
setElementPosition(selectedElement, finalX, finalY, finalZ)
108+
else
109+
setElementPosition(selectedElement, waterX, waterY, waterZ)
110+
end
111+
end
112+
elseif (surfaceFound) then
113113
if (not collisionless) then
114114
centerToBaseDistance = exports.edf:edfGetElementDistanceToBase(selectedElement)
115115
local finalX, finalY, finalZ = getCoordsWithBoundingBox(surfaceX, surfaceY, surfaceZ)
116116
setElementPosition(selectedElement, finalX, finalY, finalZ)
117117
else
118118
setElementPosition(selectedElement, surfaceX, surfaceY, surfaceZ)
119119
end
120-
else
120+
elseif (waterFound) then
121121
if (not collisionless) then
122122
centerToBaseDistance = exports.edf:edfGetElementDistanceToBase(selectedElement)
123123
local finalX, finalY, finalZ = getCoordsWithBoundingBox(waterX, waterY, waterZ)
124124
setElementPosition(selectedElement, finalX, finalY, finalZ)
125125
else
126126
setElementPosition(selectedElement, waterX, waterY, waterZ)
127127
end
128+
else -- in air
129+
local tempDistance = math.sqrt((worldX - camX)^2 + (worldY - camY)^2 + (worldZ - camZ)^2)
130+
local distanceRatio = maxMoveDistance / tempDistance
131+
local x = camX + (worldX - camX) * distanceRatio
132+
local y = camY + (worldY - camY) * distanceRatio
133+
local z = camZ + (worldZ - camZ) * distanceRatio
134+
setElementPosition(selectedElement, x, y, z)
128135
end
129-
elseif (surfaceFound) then
130-
if (not collisionless) then
131-
centerToBaseDistance = exports.edf:edfGetElementDistanceToBase(selectedElement)
132-
local finalX, finalY, finalZ = getCoordsWithBoundingBox(surfaceX, surfaceY, surfaceZ)
133-
setElementPosition(selectedElement, finalX, finalY, finalZ)
134-
else
135-
setElementPosition(selectedElement, surfaceX, surfaceY, surfaceZ)
136-
end
137-
elseif (waterFound) then
138-
if (not collisionless) then
139-
centerToBaseDistance = exports.edf:edfGetElementDistanceToBase(selectedElement)
140-
local finalX, finalY, finalZ = getCoordsWithBoundingBox(waterX, waterY, waterZ)
141-
setElementPosition(selectedElement, finalX, finalY, finalZ)
142-
else
143-
setElementPosition(selectedElement, waterX, waterY, waterZ)
144-
end
145-
else -- in air
146-
local tempDistance = math.sqrt((worldX - camX)^2 + (worldY - camY)^2 + (worldZ - camZ)^2)
147-
local distanceRatio = maxMoveDistance / tempDistance
148-
local x = camX + (worldX - camX) * distanceRatio
149-
local y = camY + (worldY - camY) * distanceRatio
150-
local z = camZ + (worldZ - camZ) * distanceRatio
151-
setElementPosition(selectedElement, x, y, z)
152136
end
153137
end
154138

@@ -163,17 +147,16 @@ local function zoomWithMouseWheel(key, keyState)
163147
speed = zoomSpeed.medium
164148
end
165149

166-
if getCommandState"zoom_in" then
150+
if getCommandState("zoom_in") then
167151
maxMoveDistance = math.max(maxMoveDistance - speed, MIN_DISTANCE)
168-
processCursorMove ()
152+
processCursorMove()
169153
else --if key == "zoom_out"
170154
maxMoveDistance = math.min(maxMoveDistance + speed, MAX_DISTANCE)
171-
processCursorMove ()
155+
processCursorMove()
172156
end
173157
end
174158
end
175159

176-
177160
local function onClientCursorMove_cursor(_, _, absoluteX, absoluteY )
178161
if (selectedElement) then
179162
if ignoreFirst then
@@ -184,7 +167,6 @@ local function onClientCursorMove_cursor(_, _, absoluteX, absoluteY )
184167
end
185168
end
186169

187-
188170
local function rotateWithMouseWheel(key, keyState)
189171
if (not rotationless) and getCommandState("mod_rotate") then
190172
rotX, rotY, rotZ = getElementRotation(selectedElement)
@@ -292,10 +274,10 @@ function detachElement()
292274

293275
-- sync position/rotation
294276
local tempPosX, tempPosY, tempPosZ = getElementPosition(selectedElement)
295-
triggerServerEvent("syncProperty", getLocalPlayer(), "position", {tempPosX, tempPosY, tempPosZ}, exports.edf:edfGetAncestor(selectedElement))
277+
triggerServerEvent("syncProperty", localPlayer, "position", {tempPosX, tempPosY, tempPosZ}, exports.edf:edfGetAncestor(selectedElement))
296278
if hasRotation[getElementType(selectedElement)] then
297279
rotX, rotY, rotZ = getElementRotation(selectedElement)
298-
triggerServerEvent("syncProperty", getLocalPlayer(), "rotation", {rotX, rotY, rotZ}, exports.edf:edfGetAncestor(selectedElement))
280+
triggerServerEvent("syncProperty", localPlayer, "rotation", {rotX, rotY, rotZ}, exports.edf:edfGetAncestor(selectedElement))
299281
end
300282
selectedElement = nil
301283

@@ -330,11 +312,7 @@ function setZoomSpeeds(slow, medium, fast)
330312
end
331313

332314
function getAttachedElement()
333-
if (selectedElement) then
334-
return selectedElement
335-
else
336-
return false
337-
end
315+
return selectedElement
338316
end
339317

340318
function getMaxMoveDistance()
@@ -350,29 +328,27 @@ function getZoomSpeeds()
350328
end
351329

352330
function disable()
353-
if (not isEnabled) then
354-
return false
331+
if isEnabled then
332+
removeEventHandler("onClientCursorMove", root, onClientCursorMove_cursor)
333+
unbindControl("quick_rotate_increase", "down", rotateWithMouseWheel)
334+
unbindControl("quick_rotate_decrease", "down", rotateWithMouseWheel)
335+
unbindControl("zoom_in", "down", zoomWithMouseWheel)
336+
unbindControl("zoom_out", "down", zoomWithMouseWheel)
337+
isEnabled = false
355338
end
356-
-- remove events, unbind keys
357-
removeEventHandler("onClientCursorMove", root, onClientCursorMove_cursor)
358-
unbindControl("quick_rotate_increase", "down", rotateWithMouseWheel)
359-
unbindControl("quick_rotate_decrease", "down", rotateWithMouseWheel)
360-
unbindControl("zoom_in", "down", zoomWithMouseWheel)
361-
unbindControl("zoom_out", "down", zoomWithMouseWheel)
362-
isEnabled = false
339+
return true
363340
end
364341

365342
function enable()
366-
if isEnabled then
367-
return false
343+
if not isEnabled then
344+
ignoreFirst = true
345+
addEventHandler("onClientCursorMove", root, onClientCursorMove_cursor)
346+
bindControl("quick_rotate_increase", "down", rotateWithMouseWheel)
347+
bindControl("quick_rotate_decrease", "down", rotateWithMouseWheel)
348+
bindControl("zoom_in", "down", zoomWithMouseWheel)
349+
bindControl("zoom_out", "down", zoomWithMouseWheel)
350+
setTimer(processCursorMove, 50, 1) --Lazy but we have to wait for MTA to switch modes
351+
isEnabled = true
368352
end
369-
-- add events, bind keys
370-
ignoreFirst = true
371-
addEventHandler("onClientCursorMove", root, onClientCursorMove_cursor)
372-
bindControl("quick_rotate_increase", "down", rotateWithMouseWheel) --rotate left
373-
bindControl("quick_rotate_decrease", "down", rotateWithMouseWheel) --rotate right
374-
bindControl("zoom_in", "down", zoomWithMouseWheel) --zoom in
375-
bindControl("zoom_out", "down", zoomWithMouseWheel) --zoom out
376-
setTimer ( processCursorMove, 50, 1 ) --Lazy but we have to wait for MTA to switch modes
377-
isEnabled = true
353+
return true
378354
end

0 commit comments

Comments
 (0)