2023-04-28 17:35:52 +00:00
|
|
|
--Load and save plaintext line mesh format for DEFCON coastlines etc.
|
|
|
|
local t = {}
|
|
|
|
local lfs = love.filesystem
|
|
|
|
local lg = love.graphics
|
|
|
|
|
2024-04-28 01:38:23 +00:00
|
|
|
local polygon = { x = 180, X = -180, y = 100, Y = -100 } --default empty bounding box
|
|
|
|
local polymt = { __index = polygon }
|
|
|
|
|
|
|
|
function polygon:formatDisplayInfo()
|
|
|
|
return ([[
|
2024-04-29 01:21:32 +00:00
|
|
|
x: %f
|
|
|
|
y: %f
|
|
|
|
X: %f
|
|
|
|
Y: %f
|
|
|
|
N: %d]]):format( self.x, self.y, self.X, self.Y, #self )
|
2024-04-28 01:38:23 +00:00
|
|
|
end
|
|
|
|
|
2023-04-28 17:35:52 +00:00
|
|
|
function t.load( filename )
|
2024-04-29 01:21:32 +00:00
|
|
|
local polys = { visible = true, filename = filename }
|
2023-04-28 17:35:52 +00:00
|
|
|
local poly = {}
|
2024-04-26 16:42:52 +00:00
|
|
|
local n = 1
|
2023-04-28 17:35:52 +00:00
|
|
|
local k = 1
|
|
|
|
for line in assert( lfs.lines( filename ) ) do
|
2024-04-21 16:07:56 +00:00
|
|
|
if line:find "b" then
|
2023-04-28 17:35:52 +00:00
|
|
|
k = 1
|
2024-04-21 16:07:56 +00:00
|
|
|
if #poly > 2 then n = n + 1 end
|
2024-04-28 01:38:23 +00:00
|
|
|
poly = setmetatable({}, polymt) --axis-aligned bounding box
|
2023-04-28 17:35:52 +00:00
|
|
|
polys[n] = poly
|
|
|
|
else
|
|
|
|
local _, _, x, y = line:find( "(%g+)%s+(%g+)" )
|
2024-04-21 16:07:56 +00:00
|
|
|
x, y = tonumber( x ), tonumber( y )
|
|
|
|
if x and y then
|
|
|
|
poly[k], poly[ k + 1 ] = x, y
|
|
|
|
k = k + 2
|
2024-04-28 01:38:23 +00:00
|
|
|
if x < poly.x then poly.x = x end
|
|
|
|
if x > poly.X then poly.X = x end
|
|
|
|
if y < poly.y then poly.y = y end
|
|
|
|
if y > poly.Y then poly.Y = y end
|
2024-04-21 16:07:56 +00:00
|
|
|
else
|
|
|
|
print( "LINES: malformed line:", filename, line )
|
|
|
|
end
|
2023-04-28 17:35:52 +00:00
|
|
|
end
|
|
|
|
end
|
2024-04-28 01:38:23 +00:00
|
|
|
|
2024-04-21 16:07:56 +00:00
|
|
|
if not polys[n] or (#(polys[n]) < 3) then
|
|
|
|
polys[n] = nil
|
|
|
|
n = n - 1
|
|
|
|
end
|
2023-04-28 17:35:52 +00:00
|
|
|
|
|
|
|
print( "LOADED", filename, n )
|
|
|
|
return setmetatable( polys, {__index = t } )
|
|
|
|
end
|
|
|
|
|
2024-04-28 01:38:23 +00:00
|
|
|
function t.selectNearest( lines, wx, wy )
|
|
|
|
local d = math.huge
|
|
|
|
local nearest
|
|
|
|
for i, poly in ipairs( lines ) do
|
|
|
|
if poly.x - 5 < wx and
|
|
|
|
poly.X + 5 > wx and
|
|
|
|
poly.y - 5 < wy and
|
|
|
|
poly.Y + 5 > wy then
|
|
|
|
for k = 1, #poly, 2 do
|
|
|
|
local x, y = poly[k], poly[k + 1]
|
|
|
|
local r = ( x - wx ) * ( x - wx ) + ( y - wy ) * ( y - wy )
|
|
|
|
if r < d then
|
|
|
|
d = r
|
|
|
|
nearest = poly
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nearest
|
|
|
|
end
|
2023-04-28 17:35:52 +00:00
|
|
|
|
2024-04-29 01:21:32 +00:00
|
|
|
function t.save( lines )
|
2024-04-28 01:38:23 +00:00
|
|
|
local str = {}
|
|
|
|
for i, poly in ipairs( lines ) do
|
|
|
|
str[i] = table.concat( poly, " " )
|
|
|
|
end
|
|
|
|
str = table.concat( str, "\nb\n" ):gsub("(%S+) (%S+) ", "%1 %2\n")
|
|
|
|
return str
|
2023-07-28 05:42:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function t.newPolygon( x, y )
|
2024-04-28 01:38:23 +00:00
|
|
|
|
2023-07-28 05:42:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function t.addToCurrentPolygon( x, y )
|
2024-04-28 01:38:23 +00:00
|
|
|
|
2023-07-28 05:42:16 +00:00
|
|
|
end
|
|
|
|
|
2023-07-28 23:23:08 +00:00
|
|
|
function t.deletePolygon( poly )
|
2024-04-28 01:38:23 +00:00
|
|
|
|
2023-07-28 23:23:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function t.drawPolygonEditHandles( poly )
|
2024-04-28 01:38:23 +00:00
|
|
|
|
2023-04-28 17:35:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function t.draw( lines )
|
2023-07-28 05:42:16 +00:00
|
|
|
if not lines.visible then return end
|
2023-04-28 17:35:52 +00:00
|
|
|
for _, poly in ipairs( lines ) do
|
|
|
|
lg.line( poly )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return t
|