2023-02-02 11:23:51 -05:00
|
|
|
local lines = {}
|
2022-10-25 03:36:45 -04:00
|
|
|
|
|
|
|
-- 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 {}
|
2022-10-25 03:36:45 -04:00
|
|
|
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)
|
2022-10-25 03:36:45 -04:00
|
|
|
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
|
2022-10-25 03:36:45 -04:00
|
|
|
end
|
2023-02-02 11:23:51 -05:00
|
|
|
end
|
2022-10-25 03:36:45 -04:00
|
|
|
|
2023-02-02 11:23:51 -05:00
|
|
|
process_lines()
|
2022-10-25 03:36:45 -04:00
|
|
|
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
|
2022-10-25 03:36:45 -04:00
|
|
|
|
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
|
2022-10-25 03:36:45 -04:00
|
|
|
|
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])
|
2022-10-25 03:36:45 -04:00
|
|
|
end
|
2023-02-02 11:23:51 -05:00
|
|
|
end
|
2022-10-25 03:36:45 -04:00
|
|
|
|
2023-02-02 11:23:51 -05:00
|
|
|
process_lines()
|
2022-10-25 03:36:45 -04:00
|
|
|
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()
|
2022-10-25 03:36:45 -04:00
|
|
|
|
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
|
2022-10-25 03:36:45 -04:00
|
|
|
|
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
|
2022-10-25 03:36:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
-- updates the global vector lines based on "lines"
|
|
|
|
function process_lines()
|
2023-02-02 11:23:51 -05:00
|
|
|
local drawing = {}
|
2022-10-25 03:36:45 -04:00
|
|
|
|
2023-02-02 11:23:51 -05:00
|
|
|
for _, first in pairs(lines) do
|
|
|
|
for _, data in pairs(first) do
|
|
|
|
table.insert(drawing, data)
|
2022-10-25 03:36:45 -04:00
|
|
|
end
|
2023-02-02 11:23:51 -05:00
|
|
|
end
|
2022-10-25 03:36:45 -04:00
|
|
|
|
2023-02-02 11:23:51 -05:00
|
|
|
Global.setVectorLines(drawing)
|
2022-10-25 03:36:45 -04:00
|
|
|
end
|