code cleanup

This commit is contained in:
Chr1Z93 2023-04-12 11:24:59 +02:00
parent 41f17d9a67
commit 69120fffec
2 changed files with 176 additions and 221 deletions

View File

@ -1,162 +1,174 @@
--Data tables used in button creation
ref_modifyPitchButtons = {
{ offset = -0.37, func = function() click_modify(-1, 0) end },
{ offset = -1.11, func = function() click_modify(-5, 0) end },
{ offset = 0.37, func = function() click_modify(1, 0) end },
{ offset = 1.11, func = function() click_modify(5, 0) end },
}
ref_modifyDistanceButtons = {
{ offset = -0.37, func = function() click_modify(-1, 1) end },
{ offset = -1.11, func = function() click_modify(-5, 1) end },
{ offset = 0.37, func = function() click_modify(1, 1) end },
{ offset = 1.11, func = function() click_modify(5, 1) end },
}
--On-demand save function, remembers pitch and distance values --On-demand save function, remembers pitch and distance values
function updateSave() function updateSave()
saved_data = JSON.encode({pitch=pitch, distance=distance}) self.script_state = JSON.encode({ pitch = pitch, distance = distance })
self.script_state = saved_data
end end
--Startup, loading memory --Startup, loading memory
function onload(saved_data) function onload(saved_data)
--Loads the tracking for if the game has started yet --Loads the tracking for if the game has started yet
if saved_data ~= "" then if saved_data ~= "" then
local loaded_data = JSON.decode(saved_data) local loaded_data = JSON.decode(saved_data)
pitch = loaded_data.pitch pitch = loaded_data.pitch
distance = loaded_data.distance distance = loaded_data.distance
else else
pitch = 45 pitch = 45
distance = 30 distance = 30
end end
createInputs() createInputs()
createButtons() createButtons()
end end
--Activated by finishing writing in the input box, updates save info --Activated by finishing writing in the input box, updates save info
function input_entered(inputString, stillEditing , typeIndex) function input_entered(inputString, stillEditing, typeIndex)
if stillEditing == false then if stillEditing == false then
--Check to avoid empty input strings --Check to avoid empty input strings
if tonumber(inputString) == nil then inputString = 0 end if tonumber(inputString) == nil then inputString = 0 end
--Update save data --Update save data
if typeIndex==0 then if typeIndex == 0 then
pitch = inputString pitch = inputString
else else
distance = inputString distance = inputString
end
updateSave()
end end
updateSave()
end
end end
--Activated by button, the -5 -1 +1 +5 buttons --Activated by button, the -5 -1 +1 +5 buttons
function click_modify(amount, typeIndex) function click_modify(amount, typeIndex)
if typeIndex==0 then if typeIndex == 0 then
pitch = pitch + amount pitch = pitch + amount
self.editInput({index=typeIndex, value=pitch}) self.editInput({ index = typeIndex, value = pitch })
else else
distance = distance + amount distance = distance + amount
self.editInput({index=typeIndex, value=distance}) self.editInput({ index = typeIndex, value = distance })
end end
updateSave() updateSave()
end end
--Activated by button, uses the data to move the camera --Activated by button, uses the data to move the camera
function click_setCamera(_, color) function click_setCamera(_, color)
--Check if there is another object to use instead of self --Check if there is another object to use instead of self
local targetObj = self local targetObj = self
local nameGUID = string.sub(self.getName(), 1, 6) local nameGUID = string.sub(self.getName(), 1, 6)
if getObjectFromGUID(nameGUID) ~= nil then if getObjectFromGUID(nameGUID) ~= nil then
targetObj = getObjectFromGUID(nameGUID) targetObj = getObjectFromGUID(nameGUID)
end end
--Check if there is an offset to use instead of 180 --Check if there is an offset to use instead of 180
local offsetY = 180 local offsetY = 180
local offsetString = string.sub(self.getName(), 7) local offsetString = string.sub(self.getName(), 7)
if tonumber(string.match(offsetString, "%d+")) ~= nil then if tonumber(string.match(offsetString, "%d+")) ~= nil then
offsetY = tonumber(string.match(offsetString, "%d+")) offsetY = tonumber(string.match(offsetString, "%d+"))
end end
--Move camera into position around object --Move camera into position around object
local pos = targetObj.getPosition() local pos = targetObj.getPosition()
local rot = targetObj.getRotation() local rot = targetObj.getRotation()
rot.y = rot.y + offsetY rot.y = rot.y + offsetY
Player[color].lookAt({position=pos, pitch=pitch, yaw=rot.y, distance=distance}) Player[color].lookAt({ position = pos, pitch = pitch, yaw = rot.y, distance = distance })
local objectList = getObjects() --Send values to main tile
local AHLCGNavTile = nil for _, v in ipairs(getObjects()) do
if v.getName() == "jaqenZann's Navigation Overlay Tile" then
for i,v in ipairs(objectList) do v.call('updateEditCamera', { { pos.x, pos.y, pos.z }, tonumber(pitch), rot.y, tonumber(distance) })
if v.getName() == "jaqenZann's Navigation Overlay Tile" then break
AHLCGNavTile = v
break
end
end
-- local AHLCGNavTile = getObjectFromGUID("0ffbc5")
if AHLCGNavTile then
AHLCGNavTile.call('updateEditCamera', { { pos.x, pos.y, pos.z }, tonumber(pitch), rot.y, tonumber(distance) })
end end
end
end end
--Button/Input creation --Button/Input creation
--Text boxes for number input --Text boxes for number input
function createInputs() function createInputs()
local funcName = "inputFuncNamePitch" local funcName = "inputFuncNamePitch"
local func = function(_,_,x,z) input_entered(x,z,0) end local func = function(_, _, x, z) input_entered(x, z, 0) end
self.setVar(funcName, func) self.setVar(funcName, func)
self.createInput({ self.createInput({
input_function=funcName, function_owner=self, label="input", input_function = funcName,
alignment=2, position={-3.4,0.35,-0.21}, rotation={0,0,0}, height=420, width=1400, function_owner = self,
font_size=400, color={57/255,46/255,40/255}, label = "input",
font_color={1,1,1}, value=pitch, alignment = 2,
validation=3 -- int (1 = None, 2 = Integer, 3 = Float, 4 = Alphanumeric, 5 = Username, 6 = Name), position = { -3.4, 0.35, -0.21 },
}) rotation = { 0, 0, 0 },
local funcName = "inputFuncNameDistance" height = 420,
local func = function(_,_,x,z) input_entered(x,z,1) end width = 1400,
self.setVar(funcName, func) font_size = 400,
self.createInput({ color = { 57 / 255, 46 / 255, 40 / 255 },
input_function=funcName, function_owner=self, label="input", font_color = { 1, 1, 1 },
alignment=4, position={3.4,0.35,-0.21}, rotation={0,0,0}, height=420, width=1400, value = pitch,
font_size=400, color={57/255,46/255,40/255}, validation = 3
font_color={1,1,1}, value=distance, })
validation=3 -- int (1 = None, 2 = Integer, 3 = Float, 4 = Alphanumeric, 5 = Username, 6 = Name), local funcName = "inputFuncNameDistance"
}) local func = function(_, _, x, z) input_entered(x, z, 1) end
self.setVar(funcName, func)
self.createInput({
input_function = funcName,
function_owner = self,
label = "input",
alignment = 4,
position = { 3.4, 0.35, -0.21 },
rotation = { 0, 0, 0 },
height = 420,
width = 1400,
font_size = 400,
color = { 57 / 255, 46 / 255, 40 / 255 },
font_color = { 1, 1, 1 },
value = distance,
validation = 3
})
end end
--Center button and -5 - +5 buttons --Center button and -5 - +5 buttons
function createButtons() function createButtons()
self.createButton({
click_function = "click_setCamera",
function_owner = self,
position = { 0, 0.4, 0 },
height = 900,
width = 900,
color = { 1, 1, 1, 0 },
tooltip = "Set camera to this angle"
})
for i, ref in ipairs(ref_modifyPitchButtons) do
local funcName = "pitchModifyFunction_" .. i
self.setVar(funcName, ref.func)
local pos = { -3.4 + ref.offset, 0.3, 0.6 }
self.createButton({ self.createButton({
click_function="click_setCamera", function_owner=self, click_function = funcName,
position={0,0.4,0}, height=900, width=900, color={1,1,1,0}, function_owner = self,
tooltip="Set camera to this angle" position = pos,
height = 240,
width = 320,
color = { 1, 1, 1, 0 }
}) })
end
for i, ref in ipairs(ref_modifyPitchButtons) do for i, ref in ipairs(ref_modifyDistanceButtons) do
local funcName = "pitchModifyFunction_"..i local funcName = "distanceModifyFunction_" .. i
self.setVar(funcName, ref.func) self.setVar(funcName, ref.func)
local pos = {-3.4+ref.offset,0.3,0.6} local pos = { 3.4 + ref.offset, 0.3, 0.6 }
self.createButton({ self.createButton({
click_function=funcName, function_owner=self, click_function = funcName,
position=pos, height=240, width=320, color={1,1,1,0} function_owner = self,
}) position = pos,
end height = 240,
width = 320,
for i, ref in ipairs(ref_modifyDistanceButtons) do color = { 1, 1, 1, 0 }
local funcName = "distanceModifyFunction_"..i })
self.setVar(funcName, ref.func) end
local pos = {3.4+ref.offset,0.3,0.6}
self.createButton({
click_function=funcName, function_owner=self,
position=pos, height=240, width=320, color={1,1,1,0}
})
end
end end
--Data tables used in button creation
ref_modifyPitchButtons = {
{offset=-0.37, func=function() click_modify(-1, 0) end},
{offset=-1.11, func=function() click_modify(-5, 0) end},
{offset=0.37, func=function() click_modify(1, 0) end},
{offset=1.11, func=function() click_modify(5, 0) end},
}
ref_modifyDistanceButtons = {
{offset=-0.37, func=function() click_modify(-1, 1) end},
{offset=-1.11, func=function() click_modify(-5, 1) end},
{offset=0.37, func=function() click_modify(1, 1) end},
{offset=1.11, func=function() click_modify(5, 1) end},
}

View File

@ -197,7 +197,6 @@ function onLoad(saved_data)
editing = false editing = false
claiming = false claiming = false
selectedEditButton = -1 selectedEditButton = -1
editPos = { 0, 0, 0 } editPos = { 0, 0, 0 }
editPitch = 0 editPitch = 0
editYaw = 0 editYaw = 0
@ -205,12 +204,11 @@ function onLoad(saved_data)
if saved_data ~= "" then if saved_data ~= "" then
local loaded_data = JSON.decode(saved_data) local loaded_data = JSON.decode(saved_data)
cameraParams = loaded_data.cameras cameraParams = loaded_data.cameras
fullVisibility = loaded_data.fullVis fullVisibility = loaded_data.fullVis
playVisibility = loaded_data.playVis playVisibility = loaded_data.playVis
local allclaims = loaded_data.claims
local allclaims = loaded_data.claims
for i = 1, 4 do for i = 1, 4 do
playermatData[i].claims = allclaims[i] playermatData[i].claims = allclaims[i]
end end
@ -285,7 +283,7 @@ function displayPlayArea(object, color)
colors = { getIndexForPlayerColor(color) } colors = { getIndexForPlayerColor(color) }
end end
for i, v in ipairs(colors) do for _, v in ipairs(colors) do
if v > 0 then if v > 0 then
fullVisibility[v] = false fullVisibility[v] = false
playVisibility[v] = true playVisibility[v] = true
@ -334,7 +332,7 @@ function closeOverlay(object, color)
colors = { getIndexForPlayerColor(color) } colors = { getIndexForPlayerColor(color) }
end end
for iv, v in ipairs(colors) do for _, v in ipairs(colors) do
if v > 0 then if v > 0 then
fullVisibility[v] = false fullVisibility[v] = false
playVisibility[v] = false playVisibility[v] = false
@ -356,7 +354,7 @@ function resizeOverlay(object, color)
colors = { getIndexForPlayerColor(color) } colors = { getIndexForPlayerColor(color) }
end end
for iv, v in ipairs(colors) do for _, v in ipairs(colors) do
if v > 0 then if v > 0 then
local full = fullVisibility[v] local full = fullVisibility[v]
fullVisibility[v] = not full fullVisibility[v] = not full
@ -429,7 +427,7 @@ function resetOverlay()
[[" allowDragging="true" returnToOriginalPositionWhenReleased="false" rectAlignment="LowerRight" offsetXY="-40 0"> [[" allowDragging="true" returnToOriginalPositionWhenReleased="false" rectAlignment="LowerRight" offsetXY="-40 0">
<image id="backgroundImage" image="OverlayLarge" />]] <image id="backgroundImage" image="OverlayLarge" />]]
for i, d in ipairs(data) do for _, d in ipairs(data) do
local buttonID = tonumber(d.id) local buttonID = tonumber(d.id)
if editing and buttonID < 19 then if editing and buttonID < 19 then
@ -474,7 +472,7 @@ function resetOverlay()
[[" allowDragging="true" returnToOriginalPositionWhenReleased="false" rectAlignment="LowerRight" offsetXY="-40 0"> [[" allowDragging="true" returnToOriginalPositionWhenReleased="false" rectAlignment="LowerRight" offsetXY="-40 0">
<image id="backgroundImage" image="OverlaySmall" />]] <image id="backgroundImage" image="OverlaySmall" />]]
for i, d in ipairs(data) do for _, d in ipairs(data) do
local buttonID = tonumber(d.id) local buttonID = tonumber(d.id)
if editing and buttonID < 19 then if editing and buttonID < 19 then
@ -514,8 +512,8 @@ function resetOverlay()
local largeOverlay = nil local largeOverlay = nil
local smallOverlay = nil local smallOverlay = nil
for i, v in pairs(existingAssets) do for _, v in pairs(existingAssets) do
for ii, vv in pairs(v) do for _, vv in pairs(v) do
if vv == 'OverlayLarge' then if vv == 'OverlayLarge' then
largeOverlay = v largeOverlay = v
end end
@ -548,12 +546,11 @@ end
function buttonClicked(player, _, idValue) function buttonClicked(player, _, idValue)
local buttonID = tonumber(idValue) local buttonID = tonumber(idValue)
if buttonID >= 19 then if buttonID == 19 then
if buttonID == 19 then resizeOverlay(nil, player.color)
resizeOverlay(nil, player.color) return
elseif buttonID == 20 then elseif buttonID == 20 then
closeOverlay(nil, player.color) closeOverlay(nil, player.color)
end
return return
end end
@ -637,7 +634,7 @@ function loadCamera(player, _, idValue)
local minZ = 100 local minZ = 100
local maxZ = -100 local maxZ = -100
for i, v in pairs(mapObjects) do for _, v in pairs(mapObjects) do
local obj = v.hit_object local obj = v.hit_object
if obj.type == 'Card' or obj.type == 'Infinite' then if obj.type == 'Card' or obj.type == 'Infinite' then
@ -647,32 +644,19 @@ function loadCamera(player, _, idValue)
local z1 = bounds['center'][3] - bounds['size'][3] / 2 local z1 = bounds['center'][3] - bounds['size'][3] / 2
local z2 = bounds['center'][3] + bounds['size'][3] / 2 local z2 = bounds['center'][3] + bounds['size'][3] / 2
if x1 < minX then minX = math.min(x1, minX)
minX = x1 maxX = math.max(x2, maxX)
end minZ = math.min(z1, minZ)
if x2 > maxX then maxZ = math.max(z2, maxZ)
maxX = x2
end
if z1 < minZ then
minZ = z1
end
if z2 > maxZ then
maxZ = z2
end
end end
end end
if minX < 100 then if minX < 100 then
local dx = maxX - minX local dx = maxX - minX
local dz = (maxZ - minZ) / (1.6) -- screen ratio * 1.2 (for my macbook pro, no idea how to generalize this) local dz = (maxZ - minZ) / (1.6) -- screen ratio * 1.2 (for my macbook pro, no idea how to generalize this)
local centerX = (minX + maxX) / local centerX = (minX + maxX) / 2 -- offset is to move it a bit up, so the cards don't block anything
2 -- - dx*0.12 -- offset is to move it a bit up, so the cards don't block anything
local centerZ = (minZ + maxZ) / 2 local centerZ = (minZ + maxZ) / 2
local scale = math.max(dx, dz)
local scale = dx
if dz > dx then
scale = dz
end
-- regression line from the following data points, seems linear -- regression line from the following data points, seems linear
-- rows 1 scale 4.5 d 12 -- rows 1 scale 4.5 d 12
@ -682,13 +666,8 @@ function loadCamera(player, _, idValue)
-- rows 5 scale 23.25 d 28 -- rows 5 scale 23.25 d 28
-- rows 6 scale 30.8 d 34 -- rows 6 scale 30.8 d 34
-- local d = 0.8685 * scale + 7.4505
-- modified by testing -- modified by testing
-- local d = 0.8685 * scale + 5
-- local d = 1.04 * scale + 5
local d = 0.96 * scale + 5 local d = 0.96 * scale + 5
player.lookAt({ position = { centerX, 0, centerZ }, pitch = 74, yaw = 90, distance = d }) player.lookAt({ position = { centerX, 0, centerZ }, pitch = 74, yaw = 90, distance = d })
else else
player.lookAt({ position = { -30.667, 0, 0 }, pitch = 74, yaw = 90, distance = 32 }) player.lookAt({ position = { -30.667, 0, 0 }, pitch = 74, yaw = 90, distance = 32 })
@ -721,75 +700,42 @@ function loadCamera(player, _, idValue)
for i, v in pairs(matObjects) do for i, v in pairs(matObjects) do
local obj = v.hit_object local obj = v.hit_object
if obj.type == 'Card' or obj.type == 'Infinite' then if obj.type == 'Card' or obj.type == 'Infinite' then
local bounds = obj.getBounds() local bounds = obj.getBounds()
local x1 = bounds['center'][1] - bounds['size'][1] / 2 local x1 = bounds['center'][1] - bounds['size'][1] / 2
local x2 = bounds['center'][1] + bounds['size'][1] / 2 local x2 = bounds['center'][1] + bounds['size'][1] / 2
local z1 = bounds['center'][3] - bounds['size'][3] / 2 local z1 = bounds['center'][3] - bounds['size'][3] / 2
local z2 = bounds['center'][3] + bounds['size'][3] / 2 local z2 = bounds['center'][3] + bounds['size'][3] / 2
if x1 < minX then minX = math.min(x1, minX)
minX = x1 maxX = math.max(x2, maxX)
end minZ = math.min(z1, minZ)
if x2 > maxX then maxZ = math.max(z2, maxZ)
maxX = x2
end
if z1 < minZ then
minZ = z1
end
if z2 > maxZ then
maxZ = z2
end
end end
end end
local dx local dx, dz, centerX, centerZ, yaw
local dz
local centerX
local centerZ
local scale
local yaw
local d
-- White/Orange -- White/Orange
if index > 3 and index < 6 then if index > 3 and index < 6 then
dx = maxX - minX dx = maxX - minX
dz = (maxZ - minZ) / 1.6 -- screen ratio * 1.2 (for my macbook pro, no idea how to generalize this) dz = (maxZ - minZ) / 1.6 -- screen ratio * 1.2 (for my macbook pro, no idea how to generalize this)
-- offset is to move it a bit up, so the cards don't block anything
centerX = (minX + maxX) / 2 - dx * playermatData[newMatIndex].xOffset
-- offset is to move it right a bit, so the toolbar doesn't block anything
centerZ = (minZ + maxZ) / 2 + dz * playermatData[newMatIndex].zOffset
yaw = 90 yaw = 90
scale = dx -- offset is to move it a bit up and right, so the cards/toolbar don't block anything
if dz > dx then centerX = (minX + maxX) / 2 - dx * playermatData[newMatIndex].xOffset
scale = dz centerZ = (minZ + maxZ) / 2 + dz * playermatData[newMatIndex].zOffset
end -- Green/Red
else
d = 0.64 * scale + 7 dx = (maxX - minX) / 1.6
else -- Green/Red
dx = (maxX - minX) / 1.6 -- screen ratio * 1.2 (for my macbook pro, no idea how to generalize this)
dz = maxZ - minZ dz = maxZ - minZ
yaw = playermatData[newMatIndex].orientation.y + 180
centerX = (minX + maxX) / 2 + dx * playermatData[newMatIndex].zOffset centerX = (minX + maxX) / 2 + dx * playermatData[newMatIndex].zOffset
centerZ = (minZ + maxZ) / 2 - dz * playermatData[newMatIndex].xOffset centerZ = (minZ + maxZ) / 2 - dz * playermatData[newMatIndex].xOffset
yaw = playermatData[newMatIndex].orientation.y + 180
scale = dz
if dx > dz then
scale = dx
end
d = 0.64 * scale + 7
end end
-- 15.46 -> 17.081 local scale = math.max(dx, dz)
-- 18.88 -> 19.33 local d = 0.64 * scale + 7
-- 24.34 -> 22.6
-- need to wait if the player color changed -- need to wait if the player color changed
Wait.frames(function() Wait.frames(function()
@ -882,13 +828,10 @@ function getPlayerColorForIndex(index)
end end
local guid = playermatData[index]['guid'] local guid = playermatData[index]['guid']
if guid ~= nil then if guid ~= nil then
local mat = getObjectFromGUID(guid) local mat = getObjectFromGUID(guid)
return mat.getVar("playerColor") return mat.getVar("playerColor")
end end
return nil
end end
function getIndexForPlayerColor(color) function getIndexForPlayerColor(color)