From 037bd6b21ea7ebac0b926cc44e4c4458b99ab441 Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Mon, 15 Jan 2024 14:57:51 +0100 Subject: [PATCH] updated guid reference handler --- src/core/GUIDReferenceApi.ttslua | 12 ++++++++ src/core/GUIDReferenceHandler.ttslua | 38 ++++++++++++++++++++++++ src/core/Global.ttslua | 43 +++++++++++++++++++++------- src/playermat/PlaymatApi.ttslua | 20 +++++++++++++ 4 files changed, 102 insertions(+), 11 deletions(-) diff --git a/src/core/GUIDReferenceApi.ttslua b/src/core/GUIDReferenceApi.ttslua index 3728eb09..63db9288 100644 --- a/src/core/GUIDReferenceApi.ttslua +++ b/src/core/GUIDReferenceApi.ttslua @@ -24,5 +24,17 @@ do return getGuidHandler().call("getObjectsByOwner", owner) end + -- sends new information to the reference handler to edit the main index + ---@param owner String Parent of the object + ---@param type String Type of the object + ---@param guid String GUID of the object + GUIDReferenceApi.editIndex = function(owner, type, guid) + return getGuidHandler().call("editIndex", { + owner = owner, + type = type, + guid = guid + }) + end + return GUIDReferenceApi end diff --git a/src/core/GUIDReferenceHandler.ttslua b/src/core/GUIDReferenceHandler.ttslua index 5fbd6547..6f1f2ea2 100644 --- a/src/core/GUIDReferenceHandler.ttslua +++ b/src/core/GUIDReferenceHandler.ttslua @@ -90,12 +90,43 @@ local GuidReferences = { } } +local editsToIndex = { + White = {}, + Orange = {}, + Green = {}, + Red = {}, + Mythos = {} +} + +-- save function to keep edits to the index +function onSave() return JSON.encode({ editsToIndex = editsToIndex }) end + +-- load function to restore edits to the index +function onLoad(savedData) + if savedData and savedData ~= "" then + local loadedData = JSON.decode(savedData) + editsToIndex = loadedData.editsToIndex + updateMainIndex() + end +end + +-- merges the main index and the edits +function updateMainIndex() + for owner, subTable in pairs(editsToIndex) do + for type, guid in pairs(subTable) do + GuidReferences[owner][type] = guid + end + end +end + +-- returns an object reference for the requested owner and type function getObjectByOwnerAndType(params) local owner = params.owner or "Mythos" local type = params.type return getObjectFromGUID(GuidReferences[owner][type]) end +-- returns a list of objects for the requested type function getObjectsByType(type) local objList = {} for owner, objects in pairs(GuidReferences) do @@ -107,6 +138,7 @@ function getObjectsByType(type) return objList end +-- returns a list of objects for the requested owner function getObjectsByOwner(owner) local objList = {} for type, guid in pairs(GuidReferences[owner]) do @@ -117,3 +149,9 @@ function getObjectsByOwner(owner) end return objList end + +-- makes an edit to the main index +function editIndex(params) + editsToIndex[params.owner][params.type] = params.guid + updateMainIndex() +end diff --git a/src/core/Global.ttslua b/src/core/Global.ttslua index d785ce0c..14987b91 100644 --- a/src/core/Global.ttslua +++ b/src/core/Global.ttslua @@ -1225,7 +1225,8 @@ function playermatRemovalSelected(player, selectedIndex, id) if mat then -- confirmation dialog about deletion - player.showConfirmDialog("Do you really want to remove " .. matColor .. "'s playermat? This can't be reversed.", function() removePlayermat(matColor) end) + player.pingTable(mat.getPosition()) + player.showConfirmDialog("Do you really want to remove " .. matColor .. "'s playermat and related objects? This can't be reversed.", function() removePlayermat(matColor) end) else -- info dialog that it is already deleted player.showInfoDialog(matColor .. "'s playermat has already been removed.") @@ -1235,7 +1236,19 @@ end -- removes a playermat and all related objects from play ---@param matColor String Color of the playermat to remove function removePlayermat(matColor) + local matObjects = guidReferenceApi.getObjectsByOwner(matColor) + if not matObjects.Playermat then return end + -- remove action tokens + local actionTokens = playmatApi.searchAroundPlaymat(matColor, "isActionToken") + for _, obj in ipairs(actionTokens) do + obj.destruct() + end + + -- remove mat owned objects + for _, obj in pairs(matObjects) do + obj.destruct() + end end -- sets the option panel to the correct state (corresponding to 'optionPanel') @@ -1312,18 +1325,22 @@ function applyOptionPanelChange(id, state) -- option: Show hand helper for each player elseif id == "showHandHelper" then - for i, color in ipairs(MAT_COLORS) do - local pos = playmatApi.transformLocalPosition({0.05, 0, -1.182}, color) - local rot = playmatApi.returnRotation(color) - optionPanel[id][i] = spawnOrRemoveHelper(state, "Hand Helper", pos, rot) + local helperName = "Hand Helper" + local spawnData = playmatApi.getHelperSpawnData("All", helperName) + local i = 0 + for color, data in pairs(spawnData) do + i = i + 1 + optionPanel[id][i] = spawnOrRemoveHelper(state, helperName, data.position, data.rotation, color) end -- option: Show search assistant for each player elseif id == "showSearchAssistant" then - for i, color in ipairs(MAT_COLORS) do - local pos = playmatApi.transformLocalPosition({-0.3, 0, -1.182}, color) - local rot = playmatApi.returnRotation(color) - optionPanel[id][i] = spawnOrRemoveHelper(state, "Search Assistant", pos, rot) + local helperName = "Search Assistant" + local spawnData = playmatApi.getHelperSpawnData("All", helperName) + local i = 0 + for color, data in pairs(spawnData) do + i = i + 1 + optionPanel[id][i] = spawnOrRemoveHelper(state, helperName, data.position, data.rotation, color) end -- option: Show attachment helper @@ -1345,13 +1362,17 @@ end ---@param name String Name of the helper object ---@param position Vector Position of the object (where it will spawn) ---@param rotation Vector Rotation of the object for spawning (default: {0, 270, 0}) +---@param owner String Owner of the object (defaults to "Mythos") ---@return. GUID of the spawnedObj (or nil if object was removed) -function spawnOrRemoveHelper(state, name, position, rotation) +function spawnOrRemoveHelper(state, name, position, rotation, owner) if (type(state) == "table" and #state == 0) then return removeHelperObject(name) elseif state then Player.getPlayers()[1].pingTable(position) - return spawnHelperObject(name, position, rotation).getGUID() + local spawnedGUID = spawnHelperObject(name, position, rotation).getGUID() + local cleanName = name:gsub("%s+", "") + guidReferenceApi.editIndex(owner or "Mythos", cleanName, spawnedGUID) + return spawnedGUID else return removeHelperObject(name) end diff --git a/src/playermat/PlaymatApi.ttslua b/src/playermat/PlaymatApi.ttslua index 1e067bc7..ed4a5366 100644 --- a/src/playermat/PlaymatApi.ttslua +++ b/src/playermat/PlaymatApi.ttslua @@ -96,6 +96,26 @@ do end end + -- Returns a table with spawn data (position and rotation) for a helper object + ---@param matColor String Color of the playmat - White, Orange, Green, Red or All + ---@param helperName String Name of the helper object + PlaymatApi.getHelperSpawnData = function(matColor, helperName) + local resultTable = {} + local localPositionTable = { + ["Hand Helper"] = {0.05, 0, -1.182}, + ["Search Assistant"] = {-0.3, 0, -1.182} + } + + for color, mat in pairs(getMatForColor(matColor)) do + resultTable[color] = { + position = mat.positionToWorld(localPositionTable[helperName]), + rotation = mat.getRotation() + } + end + return resultTable + end + + -- Triggers the Upkeep for the requested playmat ---@param matColor String Color of the playmat - White, Orange, Green, Red or All ---@param playerColor String Color of the calling player (for messages)