2020-12-06 09:42:32 -05:00
|
|
|
function updateSave()
|
2022-10-19 19:07:47 -04:00
|
|
|
local data_to_save = { ["ml"] = memoryList }
|
2020-12-06 09:42:32 -05:00
|
|
|
saved_data = JSON.encode(data_to_save)
|
|
|
|
self.script_state = saved_data
|
|
|
|
end
|
|
|
|
|
|
|
|
function onload(saved_data)
|
|
|
|
if saved_data ~= "" then
|
|
|
|
local loaded_data = JSON.decode(saved_data)
|
|
|
|
--Set up information off of loaded_data
|
|
|
|
memoryList = loaded_data.ml
|
|
|
|
else
|
|
|
|
--Set up information for if there is no saved saved data
|
|
|
|
memoryList = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
if next(memoryList) == nil then
|
|
|
|
createSetupButton()
|
|
|
|
else
|
|
|
|
createMemoryActionButtons()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--Beginning Setup
|
|
|
|
--Make setup button
|
|
|
|
function createSetupButton()
|
|
|
|
self.createButton({
|
2022-10-19 19:07:47 -04:00
|
|
|
label = "Setup",
|
|
|
|
click_function = "buttonClick_setup",
|
|
|
|
function_owner = self,
|
|
|
|
position = { 0, 5, -2 },
|
|
|
|
rotation = { 0, 0, 0 },
|
|
|
|
height = 250,
|
|
|
|
width = 600,
|
|
|
|
font_size = 150,
|
|
|
|
color = { 0, 0, 0 },
|
|
|
|
font_color = { 1, 1, 1 }
|
2020-12-06 09:42:32 -05:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
--Triggered by setup button,
|
|
|
|
function buttonClick_setup()
|
|
|
|
memoryListBackup = duplicateTable(memoryList)
|
|
|
|
memoryList = {}
|
|
|
|
self.clearButtons()
|
|
|
|
createButtonsOnAllObjects()
|
|
|
|
createSetupActionButtons()
|
|
|
|
end
|
|
|
|
|
|
|
|
--Creates selection buttons on objects
|
|
|
|
function createButtonsOnAllObjects()
|
|
|
|
local howManyButtons = 0
|
|
|
|
for _, obj in ipairs(getAllObjects()) do
|
|
|
|
if obj ~= self then
|
|
|
|
local dummyIndex = howManyButtons
|
|
|
|
--On a normal bag, the button positions aren't the same size as the bag.
|
2022-10-19 19:07:47 -04:00
|
|
|
globalScaleFactor = 1.25 * 1 / self.getScale().x
|
2020-12-06 09:42:32 -05:00
|
|
|
--Super sweet math to set button positions
|
|
|
|
local selfPos = self.getPosition()
|
|
|
|
local objPos = obj.getPosition()
|
|
|
|
local deltaPos = findOffsetDistance(selfPos, objPos, obj)
|
|
|
|
local objPos = rotateLocalCoordinates(deltaPos, self)
|
|
|
|
objPos.x = -objPos.x * globalScaleFactor
|
|
|
|
objPos.y = objPos.y * globalScaleFactor
|
|
|
|
objPos.z = objPos.z * 4
|
|
|
|
--Offset rotation of bag
|
|
|
|
local rot = self.getRotation()
|
|
|
|
rot.y = -rot.y + 180
|
|
|
|
--Create function
|
|
|
|
local funcName = "selectButton_" .. howManyButtons
|
|
|
|
local func = function() buttonClick_selection(dummyIndex, obj) end
|
|
|
|
self.setVar(funcName, func)
|
|
|
|
self.createButton({
|
2022-10-19 19:07:47 -04:00
|
|
|
click_function = funcName, function_owner = self,
|
|
|
|
position = objPos, rotation = rot, height = 1000, width = 1000,
|
|
|
|
color = { 0.75, 0.25, 0.25, 0.6 },
|
2020-12-06 09:42:32 -05:00
|
|
|
})
|
|
|
|
howManyButtons = howManyButtons + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--Creates submit and cancel buttons
|
|
|
|
function createSetupActionButtons()
|
|
|
|
self.createButton({
|
2022-10-19 19:07:47 -04:00
|
|
|
label = "Cancel", click_function = "buttonClick_cancel", function_owner = self,
|
|
|
|
position = { 1.5, 5, 2 }, rotation = { 0, 0, 0 }, height = 350, width = 1100,
|
|
|
|
font_size = 250, color = { 0, 0, 0 }, font_color = { 1, 1, 1 }
|
2020-12-06 09:42:32 -05:00
|
|
|
})
|
|
|
|
self.createButton({
|
2022-10-19 19:07:47 -04:00
|
|
|
label = "Submit", click_function = "buttonClick_submit", function_owner = self,
|
|
|
|
position = { -1.2, 5, 2 }, rotation = { 0, 0, 0 }, height = 350, width = 1100,
|
|
|
|
font_size = 250, color = { 0, 0, 0 }, font_color = { 1, 1, 1 }
|
2020-12-06 09:42:32 -05:00
|
|
|
})
|
|
|
|
self.createButton({
|
2022-10-19 19:07:47 -04:00
|
|
|
label = "Reset", click_function = "buttonClick_reset", function_owner = self,
|
|
|
|
position = { -3.5, 5, 2 }, rotation = { 0, 0, 0 }, height = 350, width = 800,
|
|
|
|
font_size = 250, color = { 0, 0, 0 }, font_color = { 1, 1, 1 }
|
2020-12-06 09:42:32 -05:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
--During Setup
|
|
|
|
--Checks or unchecks buttons
|
|
|
|
function buttonClick_selection(index, obj)
|
2022-10-19 19:07:47 -04:00
|
|
|
local color = { 0, 1, 0, 0.6 }
|
2020-12-06 09:42:32 -05:00
|
|
|
if memoryList[obj.getGUID()] == nil then
|
2022-10-19 19:07:47 -04:00
|
|
|
self.editButton({ index = index, color = color })
|
2020-12-06 09:42:32 -05:00
|
|
|
--Adding pos/rot to memory table
|
|
|
|
local pos, rot = obj.getPosition(), obj.getRotation()
|
|
|
|
--I need to add it like this or it won't save due to indexing issue
|
|
|
|
memoryList[obj.getGUID()] = {
|
2022-10-19 19:07:47 -04:00
|
|
|
pos = { x = round(pos.x, 4), y = round(pos.y, 4), z = round(pos.z, 4) },
|
|
|
|
rot = { x = round(rot.x, 4), y = round(rot.y, 4), z = round(rot.z, 4) },
|
|
|
|
lock = obj.getLock()
|
2020-12-06 09:42:32 -05:00
|
|
|
}
|
2022-10-19 19:07:47 -04:00
|
|
|
obj.highlightOn({ 0, 1, 0 })
|
2020-12-06 09:42:32 -05:00
|
|
|
else
|
2022-10-19 19:07:47 -04:00
|
|
|
color = { 0.75, 0.25, 0.25, 0.6 }
|
|
|
|
self.editButton({ index = index, color = color })
|
2020-12-06 09:42:32 -05:00
|
|
|
memoryList[obj.getGUID()] = nil
|
|
|
|
obj.highlightOff()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--Cancels selection process
|
|
|
|
function buttonClick_cancel()
|
|
|
|
memoryList = memoryListBackup
|
|
|
|
self.clearButtons()
|
|
|
|
if next(memoryList) == nil then
|
|
|
|
createSetupButton()
|
|
|
|
else
|
|
|
|
createMemoryActionButtons()
|
|
|
|
end
|
|
|
|
removeAllHighlights()
|
2022-10-19 19:07:47 -04:00
|
|
|
broadcastToAll("Selection Canceled", { 1, 1, 1 })
|
2020-12-06 09:42:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
--Saves selections
|
|
|
|
function buttonClick_submit()
|
|
|
|
if next(memoryList) == nil then
|
2022-10-19 19:07:47 -04:00
|
|
|
broadcastToAll("You cannot submit without any selections.", { 0.75, 0.25, 0.25 })
|
2020-12-06 09:42:32 -05:00
|
|
|
else
|
|
|
|
self.clearButtons()
|
|
|
|
createMemoryActionButtons()
|
|
|
|
local count = 0
|
|
|
|
for guid in pairs(memoryList) do
|
|
|
|
count = count + 1
|
|
|
|
local obj = getObjectFromGUID(guid)
|
|
|
|
if obj ~= nil then obj.highlightOff() end
|
|
|
|
end
|
2022-10-19 19:07:47 -04:00
|
|
|
broadcastToAll(count .. " Objects Saved", { 1, 1, 1 })
|
2020-12-06 09:42:32 -05:00
|
|
|
updateSave()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--Resets bag to starting status
|
|
|
|
function buttonClick_reset()
|
|
|
|
memoryList = {}
|
|
|
|
self.clearButtons()
|
|
|
|
createSetupButton()
|
|
|
|
removeAllHighlights()
|
2022-10-19 19:07:47 -04:00
|
|
|
broadcastToAll("Tool Reset", { 1, 1, 1 })
|
2020-12-06 09:42:32 -05:00
|
|
|
updateSave()
|
|
|
|
end
|
|
|
|
|
|
|
|
--After Setup
|
|
|
|
--Creates recall and place buttons
|
|
|
|
function createMemoryActionButtons()
|
|
|
|
self.createButton({
|
2022-10-19 19:07:47 -04:00
|
|
|
label = "Clicker", click_function = "buttonClick_place", function_owner = self,
|
|
|
|
position = { 4.2, 1, 0 }, rotation = { 0, 0, 0 }, height = 500, width = 1100,
|
|
|
|
font_size = 350, color = { 0, 0, 0 }, font_color = { 1, 1, 1 }
|
2020-12-06 09:42:32 -05:00
|
|
|
})
|
|
|
|
self.createButton({
|
2022-10-19 19:07:47 -04:00
|
|
|
label = "Counter", click_function = "buttonClick_recall", function_owner = self,
|
|
|
|
position = { -4.2, 1, -0.1 }, rotation = { 0, 0, 0 }, height = 500, width = 1300,
|
|
|
|
font_size = 350, color = { 0, 0, 0 }, font_color = { 1, 1, 1 }
|
2020-12-06 09:42:32 -05:00
|
|
|
})
|
2021-11-16 23:41:43 -05:00
|
|
|
self.createButton({
|
2022-10-19 19:07:47 -04:00
|
|
|
label = "Add Draw 1 Buttons", click_function = "addDraw1Buttons", function_owner = self,
|
|
|
|
position = { 0, 1, -2.5 }, rotation = { 0, 0, 0 }, height = 500, width = 2600,
|
|
|
|
font_size = 250, color = { 0, 0, 0 }, font_color = { 1, 1, 1 }
|
2021-11-16 23:41:43 -05:00
|
|
|
})
|
2022-10-19 19:07:47 -04:00
|
|
|
--[[
|
|
|
|
self.createButton({
|
|
|
|
label="Setup", click_function="buttonClick_setup", function_owner=self,
|
|
|
|
position={-6,1,0}, rotation={0,90,0}, height=500, width=1200,
|
|
|
|
font_size=350, color={0,0,0}, font_color={1,1,1}
|
|
|
|
})
|
|
|
|
--]]
|
2020-12-06 09:42:32 -05:00
|
|
|
end
|
|
|
|
|
2021-11-16 23:41:43 -05:00
|
|
|
function addDraw1Buttons()
|
|
|
|
if ADD_BUTTONS_DISABLED then return end
|
|
|
|
|
|
|
|
local mats = { "8b081b", "bd0ff4", "383d8b", "0840d5" }
|
2022-10-19 19:07:47 -04:00
|
|
|
for i, guid in ipairs(mats) do
|
2021-11-16 23:41:43 -05:00
|
|
|
local mat = getObjectFromGUID(guid)
|
|
|
|
mat.createButton({
|
2022-10-19 19:07:47 -04:00
|
|
|
label = "Draw 1",
|
|
|
|
click_function = "doDrawOne",
|
|
|
|
function_owner = mat,
|
|
|
|
position = { 1.84, 0.1, -0.36 },
|
|
|
|
scale = { 0.12, 0.12, 0.12 },
|
|
|
|
width = 800,
|
|
|
|
height = 280,
|
|
|
|
font_size = 180
|
2021-11-16 23:41:43 -05:00
|
|
|
})
|
|
|
|
end
|
|
|
|
ADD_BUTTONS_DISABLED = true
|
|
|
|
end
|
|
|
|
|
2020-12-06 09:42:32 -05:00
|
|
|
--Sends objects from bag/table to their saved position/rotation
|
|
|
|
function buttonClick_place()
|
|
|
|
local bagObjList = self.getObjects()
|
|
|
|
for guid, entry in pairs(memoryList) do
|
|
|
|
local obj = getObjectFromGUID(guid)
|
|
|
|
--If obj is out on the table, move it to the saved pos/rot
|
|
|
|
if obj ~= nil then
|
|
|
|
obj.setPositionSmooth(entry.pos)
|
|
|
|
obj.setRotationSmooth(entry.rot)
|
|
|
|
obj.setLock(entry.lock)
|
|
|
|
else
|
|
|
|
--If obj is inside of the bag
|
|
|
|
for _, bagObj in ipairs(bagObjList) do
|
|
|
|
if bagObj.guid == guid then
|
|
|
|
local item = self.takeObject({
|
2022-10-19 19:07:47 -04:00
|
|
|
guid = guid, position = entry.pos, rotation = entry.rot,
|
2020-12-06 09:42:32 -05:00
|
|
|
})
|
|
|
|
item.setLock(entry.lock)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-10-19 19:07:47 -04:00
|
|
|
broadcastToAll("Objects Placed", { 1, 1, 1 })
|
2020-12-06 09:42:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
--Recalls objects to bag from table
|
|
|
|
function buttonClick_recall()
|
|
|
|
for guid, entry in pairs(memoryList) do
|
|
|
|
local obj = getObjectFromGUID(guid)
|
|
|
|
if obj ~= nil then self.putObject(obj) end
|
|
|
|
end
|
2022-10-19 19:07:47 -04:00
|
|
|
broadcastToAll("Objects Recalled", { 1, 1, 1 })
|
2020-12-06 09:42:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
--Utility functions
|
|
|
|
--Find delta (difference) between 2 x/y/z coordinates
|
|
|
|
function findOffsetDistance(p1, p2, obj)
|
|
|
|
local deltaPos = {}
|
|
|
|
local bounds = obj.getBounds()
|
2022-10-19 19:07:47 -04:00
|
|
|
deltaPos.x = (p2.x - p1.x)
|
|
|
|
deltaPos.y = (p2.y - p1.y) + (bounds.size.y - bounds.offset.y)
|
|
|
|
deltaPos.z = (p2.z - p1.z)
|
2020-12-06 09:42:32 -05:00
|
|
|
return deltaPos
|
|
|
|
end
|
|
|
|
|
|
|
|
--Used to rotate a set of coordinates by an angle
|
|
|
|
function rotateLocalCoordinates(desiredPos, obj)
|
2022-10-19 19:07:47 -04:00
|
|
|
local objPos, objRot = obj.getPosition(), obj.getRotation()
|
2020-12-06 09:42:32 -05:00
|
|
|
local angle = math.rad(objRot.y)
|
2022-10-19 19:07:47 -04:00
|
|
|
local x = desiredPos.x * math.cos(angle) - desiredPos.z * math.sin(angle)
|
|
|
|
local z = desiredPos.x * math.sin(angle) + desiredPos.z * math.cos(angle)
|
|
|
|
return { x = x, y = desiredPos.y, z = z }
|
2020-12-06 09:42:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
--Coroutine delay, in seconds
|
|
|
|
function wait(time)
|
|
|
|
local start = os.time()
|
|
|
|
repeat coroutine.yield(0) until os.time() > start + time
|
|
|
|
end
|
|
|
|
|
|
|
|
--Duplicates a table (needed to prevent it making reference to the same objects)
|
|
|
|
function duplicateTable(oldTable)
|
|
|
|
local newTable = {}
|
|
|
|
for k, v in pairs(oldTable) do
|
|
|
|
newTable[k] = v
|
|
|
|
end
|
|
|
|
return newTable
|
|
|
|
end
|
|
|
|
|
|
|
|
--Moves scripted highlight from all objects
|
|
|
|
function removeAllHighlights()
|
|
|
|
for _, obj in ipairs(getAllObjects()) do
|
|
|
|
obj.highlightOff()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--Round number (num) to the Nth decimal (dec)
|
|
|
|
function round(num, dec)
|
2022-10-19 19:07:47 -04:00
|
|
|
local mult = 10 ^ (dec or 0)
|
|
|
|
return math.floor(num * mult + 0.5) / mult
|
2020-09-29 00:27:29 -04:00
|
|
|
end
|