From 6a522f8f20e2341cc03c94c665006a75e823f093 Mon Sep 17 00:00:00 2001 From: SpeedyFolf Date: Fri, 13 Nov 2026 23:07:09 -0600 Subject: [PATCH] Preserve marker targets when moving markers Store marker target coordinates in EDF data to avoid rotation round-trip precision loss, preserve default directions when targets are cleared, and sync targets when detaching markers. --- [editor]/edf/edf.lua | 2 +- [editor]/edf/sharedutil.lua | 57 ++++++++++++++++++------ [editor]/move_cursor/move_cursor.lua | 4 +- [editor]/move_freecam/move_freecam.lua | 7 ++- [editor]/move_keyboard/move_keyboard.lua | 4 +- 5 files changed, 53 insertions(+), 21 deletions(-) diff --git a/[editor]/edf/edf.lua b/[editor]/edf/edf.lua index cb905d962..c41b8b293 100644 --- a/[editor]/edf/edf.lua +++ b/[editor]/edf/edf.lua @@ -600,7 +600,7 @@ function edfCreateElement(elementType, creatorClient, fromResource, parametersTa local childData = {} for property, propertyData in pairs(edf[fromResource]["elements"][elementType].data) do --try to get the given value in target datatype - if convert[propertyData.datatype] then + if parametersTable[property] ~= nil and convert[propertyData.datatype] then parametersTable[property] = convert[propertyData.datatype](parametersTable[property]) end --store the value, or its default diff --git a/[editor]/edf/sharedutil.lua b/[editor]/edf/sharedutil.lua index 8bcc0be62..6241db155 100644 --- a/[editor]/edf/sharedutil.lua +++ b/[editor]/edf/sharedutil.lua @@ -6,27 +6,55 @@ function isRotatableMarker(element) return false end +local function parseMarkerTarget(target) + if type(target) == "string" then + target = split(target, 44) + end + if type(target) ~= "table" then return false end + local x, y, z = tonumber(target[1]), tonumber(target[2]), tonumber(target[3]) + if not x or not y or not z then return false end + return x, y, z +end + +local function getMarkerTargetPosition(element) + -- Prefer EDF's saved coordinates to avoid the precision loss from round-tripping through rotation. + local target = getElementData(element, "target") + if target == "" then return false end + if target ~= nil and target ~= false then + return parseMarkerTarget(target) + end + return getMarkerTarget(element) +end + -- Keep targets on unsupported marker types so switching back restores the direction. function edfSetMarkerTarget(element, target) if not isRotatableMarker(element) then return true end if not target or target == "" then - return setMarkerTarget(element) - end - if type(target) == "string" then - target = split(target, 44) + -- MTA's Lua setter requires a target, so emulate each marker type's no-target default. + local markerType = getMarkerType(element) + local success + if markerType == "checkpoint" then + success = setMarkerIcon(element, "none") + else + local x, y, z = getElementPosition(element) + success = setMarkerTarget(element, x + 1, y, z) + end + if not success then return false end + return setElementData(element, "target", "", not isClient()) end - if type(target) ~= "table" then return false end - local x, y, z = tonumber(target[1]), tonumber(target[2]), tonumber(target[3]) - if not x or not y or not z then return false end - return setMarkerTarget(element, x, y, z) + local x, y, z = parseMarkerTarget(target) + if not x then return false end + if not setMarkerTarget(element, x, y, z) then return false end + local storedTarget = type(target) == "string" and target or string.format("%.9g,%.9g,%.9g", x, y, z) + return setElementData(element, "target", storedTarget, not isClient()) end -- Marker rotations use ZYX Euler angles, like the editor's rotation controls. function edfGetMarkerRotation(element) local x, y, z = getElementPosition(element) - local tx, ty, tz = getMarkerTarget(element) + local tx, ty, tz = getMarkerTargetPosition(element) -- MTA rings without a target face along the positive X axis. if not tx then return 0, 0, -90 end local dx, dy, dz = tx - x, ty - y, tz - z @@ -41,7 +69,7 @@ end function edfSetMarkerRotation(element, rx, ry, rz) local x, y, z = getElementPosition(element) - local tx, ty, tz = getMarkerTarget(element) + local tx, ty, tz = getMarkerTargetPosition(element) if not tx and rx == 0 and ry == 0 and rz == -90 then return true end local distance = tx and math.sqrt((tx - x)^2 + (ty - y)^2 + (tz - z)^2) or 1 if distance < 0.001 then distance = 1 end @@ -52,12 +80,13 @@ function edfSetMarkerRotation(element, rx, ry, rz) y + distance * (sx * sy * sz + cx * cz), z + distance * sx * cy) end +-- Marker targets are absolute world coordinates; moving a marker must not translate them. function edfMoveMarker(element, x, y, z) - local px, py, pz = getElementPosition(element) - local tx, ty, tz = getMarkerTarget(element) + local keepRingDefaultDirection = getMarkerType(element) == "ring" and getElementData(element, "target") == "" if not setElementPosition(element, x, y, z) then return false end - if tx then - return storeMarkerTarget(element, tx + x - px, ty + y - py, tz + z - pz) + -- A cleared ring uses a synthetic +X target because MTA has no clear-target API. + if keepRingDefaultDirection then + return setMarkerTarget(element, x + 1, y, z) end return true end diff --git a/[editor]/move_cursor/move_cursor.lua b/[editor]/move_cursor/move_cursor.lua index 41becb5c4..eec94c175 100644 --- a/[editor]/move_cursor/move_cursor.lua +++ b/[editor]/move_cursor/move_cursor.lua @@ -280,7 +280,9 @@ function detachElement() local tempPosX, tempPosY, tempPosZ = getElementPosition(selectedElement) triggerServerEvent("syncProperty", localPlayer, "position", {tempPosX, tempPosY, tempPosZ}, exports.edf:edfGetAncestor(selectedElement)) - if hasRotation[getElementType(selectedElement)] or exports.edf:isRotatableMarker(selectedElement) then + if exports.edf:isRotatableMarker(selectedElement) then + triggerServerEvent("syncProperty", localPlayer, "target", getElementData(selectedElement, "target") or "", exports.edf:edfGetAncestor(selectedElement)) + elseif hasRotation[getElementType(selectedElement)] then rotX, rotY, rotZ = getElementRotation(selectedElement, "ZYX") triggerServerEvent("syncProperty", localPlayer, "rotation", {rotX, rotY, rotZ}, exports.edf:edfGetAncestor(selectedElement)) end diff --git a/[editor]/move_freecam/move_freecam.lua b/[editor]/move_freecam/move_freecam.lua index a90598844..1048b78d1 100644 --- a/[editor]/move_freecam/move_freecam.lua +++ b/[editor]/move_freecam/move_freecam.lua @@ -250,10 +250,9 @@ function detachElement() local tempPosX, tempPosY, tempPosZ = getElementPosition(selectedElement) triggerServerEvent("syncProperty", localPlayer, "position", {tempPosX, tempPosY, tempPosZ}, exports.edf:edfGetAncestor(selectedElement)) - if hasRotation[getElementType(selectedElement)] or exports.edf:isRotatableMarker(selectedElement) then - if getElementType(selectedElement) == "marker" then - rotX, rotY, rotZ = getElementRotation(selectedElement) - end + if exports.edf:isRotatableMarker(selectedElement) then + triggerServerEvent("syncProperty", localPlayer, "target", getElementData(selectedElement, "target") or "", exports.edf:edfGetAncestor(selectedElement)) + elseif hasRotation[getElementType(selectedElement)] then triggerServerEvent("syncProperty", localPlayer, "rotation", {rotX, rotY, rotZ}, exports.edf:edfGetAncestor(selectedElement)) end if (getElementType(selectedElement) == "object") then diff --git a/[editor]/move_keyboard/move_keyboard.lua b/[editor]/move_keyboard/move_keyboard.lua index 3c4e97d89..496d809e6 100644 --- a/[editor]/move_keyboard/move_keyboard.lua +++ b/[editor]/move_keyboard/move_keyboard.lua @@ -495,7 +495,9 @@ function detachElement() -- sync position/rotation posX, posY, posZ = getElementPosition(selectedElement) triggerServerEvent("syncProperty", localPlayer, "position", {posX, posY, posZ}, exports.edf:edfGetAncestor(selectedElement)) - if hasRotation[getElementType(selectedElement)] or exports.edf:isRotatableMarker(selectedElement) then + if exports.edf:isRotatableMarker(selectedElement) then + triggerServerEvent("syncProperty", localPlayer, "target", getElementData(selectedElement, "target") or "", exports.edf:edfGetAncestor(selectedElement)) + elseif hasRotation[getElementType(selectedElement)] then rotX, rotY, rotZ = getElementRotation(selectedElement, "ZYX") triggerServerEvent("syncProperty", localPlayer, "rotation", {rotX, rotY, rotZ}, exports.edf:edfGetAncestor(selectedElement)) end