SCED/src/util/ConnectionDrawingTool.ttslua

86 lines
2.3 KiB
Plaintext
Raw Normal View History

2023-02-02 11:23:51 -05:00
local lines = {}
-- save "lines" to be able to remove them after loading
2023-02-02 11:23:51 -05:00
function onSave()
return JSON.encode(lines)
end
function onLoad(savedData)
lines = JSON.decode(savedData) or {}
end
-- create timer when numpad 0 is pressed
function onScriptingButtonDown(index, player_color)
2023-02-02 11:23:51 -05:00
if index ~= 10 then return end
TimerID = Wait.time(function() draw_from(Player[player_color]) end, 1)
end
-- called for long press of numpad 0, draws lines from hovered object to selected objects
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
if item.getGUID() ~= source.getGUID() then
if item.getGUID() > source.getGUID() then
draw_with_pair(item, source)
else
draw_with_pair(source, item)
end
end
2023-02-02 11:23:51 -05:00
end
2023-02-02 11:23:51 -05:00
process_lines()
end
-- general drawing of all lines between selected objects
function onScriptingButtonUp(index, player_color)
2023-02-02 11:23:51 -05:00
if index ~= 10 then return end
-- returns true only if there is a timer to cancel. If this is false then we've waited longer than a second.
if not Wait.stop(TimerID) then return end
2023-02-02 11:23:51 -05:00
local items = Player[player_color].getSelectedObjects()
if #items < 2 then
broadcastToColor("You must have at least two items selected (currently: " .. #items .. ").", player_color, "Red")
return
end
2023-02-02 11:23:51 -05:00
table.sort(items, function(a, b) return a.getGUID() > b.getGUID() end)
for f = 1, #items - 1 do
for s = f + 1, #items do
draw_with_pair(items[f], items[s])
end
2023-02-02 11:23:51 -05:00
end
2023-02-02 11:23:51 -05:00
process_lines()
end
-- adds two objects to table of vector lines
function draw_with_pair(first, second)
2023-02-02 11:23:51 -05:00
local guid_first = first.getGUID()
local guid_second = second.getGUID()
2023-02-02 11:23:51 -05:00
if Global.getVectorLines() == nil then lines = {} end
if not lines[guid_first] then lines[guid_first] = {} end
2023-02-02 11:23:51 -05:00
if lines[guid_first][guid_second] then
lines[guid_first][guid_second] = nil
else
lines[guid_first][guid_second] = { points = { first.getPosition(), second.getPosition() }, color = "White" }
end
end
-- updates the global vector lines based on "lines"
function process_lines()
2023-02-02 11:23:51 -05:00
local drawing = {}
2023-02-02 11:23:51 -05:00
for _, first in pairs(lines) do
for _, data in pairs(first) do
table.insert(drawing, data)
end
2023-02-02 11:23:51 -05:00
end
2023-02-02 11:23:51 -05:00
Global.setVectorLines(drawing)
end