SCED/src/util/ConnectionDrawingTool.ttslua

99 lines
2.4 KiB
Plaintext
Raw Normal View History

2024-05-21 05:23:11 -04:00
local connections = {}
2023-02-02 11:23:51 -05:00
function onSave()
2024-05-21 05:23:11 -04:00
return JSON.encode({ connections = connections })
2023-02-02 11:23:51 -05:00
end
function onLoad(savedData)
2024-03-25 12:36:03 -04:00
if savedData and savedData ~= "" then
2024-05-21 05:23:11 -04:00
local loadedData = JSON.decode(savedData) or {}
connections = loadedData.connections
processLines()
2024-03-25 12:36:03 -04:00
end
2024-05-21 05:23:11 -04:00
addHotkey("Drawing Tool: Reset", function() connections = {} processLines() end)
addHotkey("Drawing Tool: Redraw", processLines)
end
function onScriptingButtonDown(index, player_color)
2023-02-02 11:23:51 -05:00
if index ~= 10 then return end
2024-05-21 05:23:11 -04:00
Timer.create {
identifier = player_color .. "_draw_from",
function_name = "draw_from",
parameters = { Player[player_color] },
delay = 1
}
end
function draw_from(player)
2023-02-02 11:23:51 -05:00
local source = player.getHoverObject()
if not source then return end
for _, item in ipairs(player.getSelectedObjects()) do
2024-05-21 05:23:11 -04:00
if item ~= source then
2023-02-02 11:23:51 -05:00
if item.getGUID() > source.getGUID() then
2024-05-21 05:23:11 -04:00
addPair(item, source)
2023-02-02 11:23:51 -05:00
else
2024-05-21 05:23:11 -04:00
addPair(source, item)
2023-02-02 11:23:51 -05:00
end
end
2023-02-02 11:23:51 -05:00
end
2024-05-21 05:23:11 -04:00
processLines()
end
function onScriptingButtonUp(index, player_color)
2023-02-02 11:23:51 -05:00
if index ~= 10 then return end
2024-05-21 05:23:11 -04:00
2023-02-02 11:23:51 -05:00
-- returns true only if there is a timer to cancel. If this is false then we've waited longer than a second.
2024-05-21 05:23:11 -04:00
if not Timer.destroy(player_color .. "_draw_from") then return end
2023-02-02 11:23:51 -05:00
local items = Player[player_color].getSelectedObjects()
2024-05-21 05:23:11 -04:00
if #items < 2 then return end
2023-02-02 11:23:51 -05:00
table.sort(items, function(a, b) return a.getGUID() > b.getGUID() end)
2024-05-21 05:23:11 -04:00
for i = 1, #items do
local first = items[i]
for j = i, #items do
local second = items[j]
addPair(first, second)
end
2023-02-02 11:23:51 -05:00
end
2024-05-21 05:23:11 -04:00
processLines()
end
2024-05-21 05:23:11 -04:00
function addPair(first, second)
local first_guid = first.getGUID()
local second_guid = second.getGUID()
2024-05-21 05:23:11 -04:00
if not connections[first_guid] then connections[first_guid] = {} end
connections[first_guid][second_guid] = not connections[first_guid][second_guid]
end
2024-05-21 05:23:11 -04:00
function processLines()
local lines = {}
for source_guid, target_guids in pairs(connections) do
local source = getObjectFromGUID(source_guid)
2024-05-21 05:23:11 -04:00
for target_guid, exists in pairs(target_guids) do
if exists then
local target = getObjectFromGUID(target_guid)
if source and target then
table.insert(lines, {
points = { source.getPosition(), target.getPosition() },
color = Color.White
})
end
end
end
2023-02-02 11:23:51 -05:00
end
2024-05-21 05:23:11 -04:00
Global.setVectorLines(lines)
end