updated code

This commit is contained in:
Chr1Z93 2024-10-24 16:17:59 +02:00
parent 40a3bba7ce
commit 2c4428daa3
2 changed files with 44 additions and 104 deletions

View File

@ -85,7 +85,7 @@
}, },
"tooltip": "None", "tooltip": "None",
"value": [ "value": [
"Arkham Horror LCG SCE 4.0.0 - ??/??/2024" "Arkham Horror LCG SCE 4.0.0 - 11/01/2024"
] ]
}, },
{ {

View File

@ -1,14 +1,5 @@
---@diagnostic disable ---@diagnostic disable
function onload(saved_data) function onLoad(savedData)
sheetLocked = self.script_state.sheetLocked or false
local inverseScale = { x = math.floor(100 / self.getScale().x) / 100, y = math.floor(100 / self.getScale().z) / 100 }
scale = self.script_state.scale or inverseScale
flip = self.script_state.flip or "False"
fields = self.script_state.fields or {}
checks = self.script_state.checks or {}
decals = self.script_state.decals or {}
height = self.script_state.height or 0.5
locks = self.script_state.locks or { fields = false, checks = false, decals = false }
lookupInputIndexToInfo = {} lookupInputIndexToInfo = {}
lookupButtonIndexToInfo = {} lookupButtonIndexToInfo = {}
lookupFieldIndices = {} lookupFieldIndices = {}
@ -18,18 +9,21 @@ function onload(saved_data)
lastFieldLockedMessage = 0 lastFieldLockedMessage = 0
buttonIndex = 0 buttonIndex = 0
inputIndex = 0 inputIndex = 0
if saved_data ~= "" then
local loadedData = JSON.decode(saved_data) -- load saved data if possible
local loadedData = JSON.decode(savedData) or {}
sheetLocked = loadedData.sheetLocked or false sheetLocked = loadedData.sheetLocked or false
flip = loadedData.flip or "False" flip = loadedData.flip or "False"
height = loadedData.height or 0.5 height = loadedData.height or 0.5
fields = loadedData.fields or {} fields = loadedData.fields or {}
checks = loadedData.checks or {} checks = loadedData.checks or {}
decals = loadedData.decals or {} decals = loadedData.decals or {}
scale = loadedData.scale or inverseScale wasCommitted = loadedData.wasCommitted or false
local selfScale = self.getScale()
scale = loadedData.scale or { x = math.floor(100 / selfScale.x) / 100, y = math.floor(100 / selfScale.z) / 100 }
locks = loadedData.locks or { fields = false, checks = false, decals = false } locks = loadedData.locks or { fields = false, checks = false, decals = false }
end
if (not getCommited()) then if not wasCommitted then
self.addContextMenuItem("Edit Layout", showEditPanel) self.addContextMenuItem("Edit Layout", showEditPanel)
self.addContextMenuItem("Commit Layout", showCommitPanel) self.addContextMenuItem("Commit Layout", showCommitPanel)
nudgeDistance = self.script_state.nudgeDistance or 0.1 nudgeDistance = self.script_state.nudgeDistance or 0.1
@ -80,14 +74,10 @@ end
function toggleAllLocks(ply, pos, obj) function toggleAllLocks(ply, pos, obj)
if (locks.fields and locks.checks and locks.decals) then if (locks.fields and locks.checks and locks.decals) then
locks.fields = false locks.fields, locks.checks, locks.decals = false, false, false
locks.checks = false
locks.decals = false
broadcastToColor("Unlocked everything", ply) broadcastToColor("Unlocked everything", ply)
else else
locks.fields = true locks.fields, locks.checks, locks.decals = true, true, true
locks.checks = true
locks.decals = true
broadcastToColor("Locked everything", ply) broadcastToColor("Locked everything", ply)
end end
updateSave() updateSave()
@ -96,46 +86,31 @@ function toggleAllLocks(ply, pos, obj)
end end
function toggleLockFields(ply, pos, obj) function toggleLockFields(ply, pos, obj)
if (locks.fields) then locks.fields = not locks.fields
locks.fields = false broadcastToColor(locks.fields and "Locked texts" or "Unlocked texts", ply)
broadcastToColor("Unlocked texts", ply)
else
locks.fields = true
broadcastToColor("Locked texts", ply)
end
updateSave() updateSave()
makeContextMenuItems() makeContextMenuItems()
refresh() refresh()
end end
function toggleLockChecks(ply, pos, obj) function toggleLockChecks(ply, pos, obj)
if (locks.checks) then locks.checks = not locks.checks
locks.checks = false broadcastToColor(locks.checks and "Locked checkboxes" or "Unlocked checkboxes", ply)
broadcastToColor("Unlocked checkboxes", ply)
else
locks.checks = true
broadcastToColor("Locked checkboxes", ply)
end
updateSave() updateSave()
makeContextMenuItems() makeContextMenuItems()
refresh() refresh()
end end
function toggleLockDecals(ply, pos, obj) function toggleLockDecals(ply, pos, obj)
if (locks.decals) then locks.decals = not locks.decals
locks.decals = false broadcastToColor(locks.decals and "Locked images" or "Unlocked images", ply)
broadcastToColor("Unlocked images", ply)
else
locks.decals = true
broadcastToColor("Locked images", ply)
end
updateSave() updateSave()
makeContextMenuItems() makeContextMenuItems()
refresh() refresh()
end end
function updateSave() function updateSave()
local data_to_save = { self.script_state = JSON.encode({
scale = scale, scale = scale,
height = height, height = height,
fields = fields, fields = fields,
@ -143,15 +118,10 @@ function updateSave()
decals = decals, decals = decals,
flip = flip, flip = flip,
sheetLocked = sheetLocked, sheetLocked = sheetLocked,
locks = locks locks = locks,
} nudgeDistance = not wasCommitted and nudgeDistance or nil,
if (not getCommited()) then wasCommitted = wasCommitted
data_to_save.nudgeDistance = nudgeDistance })
else
data_to_save.nudgeDistance = nil
end
saved_data = JSON.encode(data_to_save)
self.script_state = saved_data
end end
function null() end function null() end
@ -179,7 +149,7 @@ function createAll()
end end
function createAllCoroutine() function createAllCoroutine()
if (not getCommited()) then if not wasCommitted then
UI.setAttribute(getPanelId("Loading"), "active", "True") UI.setAttribute(getPanelId("Loading"), "active", "True")
end end
coroutine.yield(0) coroutine.yield(0)
@ -231,8 +201,7 @@ function createAllCoroutine()
fieldScale.x = -fieldScale.x fieldScale.x = -fieldScale.x
end end
local vsum = 0 local vsum = 0
local fontSize = field.font local fontSize = math.min(field.size.y - 24, field.font)
fontSize = math.min(field.size.y - 24, fontSize)
local color = getFieldTextColor(fieldID) local color = getFieldTextColor(fieldID)
for x = 1, field.array.x do for x = 1, field.array.x do
vsum = 0 vsum = 0
@ -344,10 +313,7 @@ function createAllCoroutine()
createDecals() createDecals()
for decalID, decal in pairs(decals) do for decalID, decal in pairs(decals) do
lookupDecalIndices[decalID] = { lookupDecalIndices[decalID] = { inputs = {}, selectionButtons = {} }
inputs = {},
selectionButtons = {}
}
local pos = getDecalPosition(decalID) local pos = getDecalPosition(decalID)
if (decal.locked ~= "True" and not sheetLocked and not locks.decals and not editingSheet) then if (decal.locked ~= "True" and not sheetLocked and not locks.decals and not editingSheet) then
local func = 'MarumEditableSetURL_' .. decalID local func = 'MarumEditableSetURL_' .. decalID
@ -378,10 +344,7 @@ function createAllCoroutine()
end end
for checkID, check in pairs(checks) do for checkID, check in pairs(checks) do
lookupCheckIndices[checkID] = { lookupCheckIndices[checkID] = { buttons = {}, selectionButtons = {} }
buttons = {},
selectionButtons = {}
}
local rotationZ = 0 local rotationZ = 0
local checkScale = { x = scale.x * check.size.x, y = 1, z = scale.y * check.size.y } local checkScale = { x = scale.x * check.size.x, y = 1, z = scale.y * check.size.y }
local upright = self.getTransformUp().y > 0 local upright = self.getTransformUp().y > 0
@ -438,7 +401,7 @@ function createAllCoroutine()
end end
end end
end end
if (not getCommited() and editingSheet) then if not wasCommitted and editingSheet then
createSelectionButtons() createSelectionButtons()
end end
creating = false creating = false
@ -485,12 +448,7 @@ function revertFieldSum(args)
local ply = args[2] local ply = args[2]
local selected = args[4] local selected = args[4]
if (not selected) then if (not selected) then
Wait.frames( Wait.frames(function() recalculateVSums(fieldID) end, 1)
function()
recalculateVSums(fieldID)
end,
1
)
else else
if (lastFieldLockedMessage ~= fieldID) then if (lastFieldLockedMessage ~= fieldID) then
broadcastToColor("This text is reserved for the total sum", ply, { r = 1, g = 1, b = 0 }) broadcastToColor("This text is reserved for the total sum", ply, { r = 1, g = 1, b = 0 })
@ -549,8 +507,7 @@ function getCheckPosition(checkID, x, y)
return { return {
x = (check.pos.x + (x - 1) * check.distance.x) * mul * scale.x, x = (check.pos.x + (x - 1) * check.distance.x) * mul * scale.x,
y = height + 0.002, y = height + 0.002,
z = (check.pos.y + (y - 1) * check.distance.y) * z = (check.pos.y + (y - 1) * check.distance.y) * mul * scale.y
mul * scale.y
} }
end end
@ -831,8 +788,7 @@ function updateFieldNameContentAndTooltip(fieldID)
self.editInput({ self.editInput({
index = v.index - 1, index = v.index - 1,
value = field.value[v.arrayID], value = field.value[v.arrayID],
tooltip = getFieldTooltip(fieldID, tooltip = getFieldTooltip(fieldID, v.arrayID)
v.arrayID)
}) })
end end
local name = "T" .. fieldID local name = "T" .. fieldID
@ -842,16 +798,8 @@ function updateFieldNameContentAndTooltip(fieldID)
end end
end end
function getCommited()
return true
end
--$ --$
function getCommited()
return false
end
function get_line_count(str) function get_line_count(str)
local lines = 1 local lines = 1
for i = 1, #str do for i = 1, #str do
@ -1419,8 +1367,7 @@ function refreshEditPanel()
UI.setAttribute(attrId("PreviousPage"), "interactable", tostring(page > 1)) UI.setAttribute(attrId("PreviousPage"), "interactable", tostring(page > 1))
UI.setAttribute(attrId("FirstPage"), "interactable", tostring(page > 1)) UI.setAttribute(attrId("FirstPage"), "interactable", tostring(page > 1))
if (subpages > 1) then if (subpages > 1) then
UI.setAttribute(attrId("Pages"), "text", page .. " (" .. selectedArrayId .. "/" .. subpages .. UI.setAttribute(attrId("Pages"), "text", page .. " (" .. selectedArrayId .. "/" .. subpages .. ") / " .. pageCount)
") / " .. pageCount)
else else
UI.setAttribute(attrId("Pages"), "text", page .. " / " .. pageCount) UI.setAttribute(attrId("Pages"), "text", page .. " / " .. pageCount)
end end
@ -1435,6 +1382,8 @@ function commit(ply)
if (Global.getVar("MarumEditableSheetGUID", self.guid)) then if (Global.getVar("MarumEditableSheetGUID", self.guid)) then
Global.setVar("MarumEditableSheetGUID", nil) Global.setVar("MarumEditableSheetGUID", nil)
end end
wasCommitted = true
updateSave()
self.script_code = codeSplit[1] self.script_code = codeSplit[1]
self.reload() self.reload()
end end
@ -1515,9 +1464,8 @@ function showCommitPanel(ply, value, id)
getObjectFromGUID(previousGUID).call("closePanel") getObjectFromGUID(previousGUID).call("closePanel")
end end
end end
Player[ply].showConfirmDialog( Player[ply].showConfirmDialog("Do you want to lock the current layout and remove editing related code? " ..
"Do you want to lock the current layout and remove editing related code? You will still be able to change the contents of unlocked fields.", "You will still be able to change the contents of unlocked fields.", commit)
commit)
end end
function removeMESPanel() function removeMESPanel()
@ -3501,13 +3449,5 @@ function waitForUiLoaded(callback)
callback() callback()
return nil return nil
end end
return Wait.condition(callback, function() return UI.loading == false end)
return Wait.condition(
function()
callback()
end,
function()
return UI.loading == false
end
)
end end