bug fix: was unable to select polygons with the mouse if their vertices coincided with other polygons' vertices

This commit is contained in:
wan-may 2024-07-18 23:36:47 -03:00
parent 97f21e192f
commit 6483f42c80
13 changed files with 36 additions and 24 deletions

View File

@ -2,7 +2,7 @@ local love = assert( love, "This tool requires LOVE: love2d.org" )
--assert( require('mobdebug') ).start() --remote debugger --assert( require('mobdebug') ).start() --remote debugger
local map = require 'map.map' local map = require 'map.map'
local button = require 'ui.button' local button = require 'ui.button'
local mainmenu = require 'ui.mainmenu' local mainmenu = require 'ui.menu.mainmenu'
local Camera = require 'ui.camera' local Camera = require 'ui.camera'
function love.load() function love.load()

View File

@ -12,7 +12,7 @@ function polygon:formatDisplayInfo()
y: %f y: %f
X: %f X: %f
Y: %f Y: %f
N: %d]]):format( self.x, self.y, self.X, self.Y, #self ) Length: %d]]):format( self.x, self.y, self.X, self.Y, #self / 4 )
end end
function polygon:drawDirection() function polygon:drawDirection()
@ -73,8 +73,9 @@ function t.selectNearest( lines, wx, wy )
poly.X + 5 > wx and poly.X + 5 > wx and
poly.y - 5 < wy and poly.y - 5 < wy and
poly.Y + 5 > wy then poly.Y + 5 > wy then
for k = 1, #poly, 2 do for k = 1, #poly - 3, 4 do
local x, y = poly[k], poly[k + 1] --find the midpoint of each line segment
local x, y = 0.5 * (poly[k] + poly[k + 2]), 0.5 * (poly[k + 1] + poly[k + 3])
local r = ( x - wx ) * ( x - wx ) + ( y - wy ) * ( y - wy ) local r = ( x - wx ) * ( x - wx ) + ( y - wy ) * ( y - wy )
if r < d then if r < d then
d = r d = r

View File

@ -1 +1,5 @@
Map editor for DEFCON, the strategy game, written in LOVE2D, the engine. Map editor for DEFCON, the strategy game, written in LOVE2D, the engine.
Currently does not do anything besides read the files and write them back out.
Need to make a more structured UI, rewriting menu.lua and button.lua for this purpose.

0
ui/menu.lua Normal file
View File

0
ui/menu/ainodes.lua Normal file
View File

1
ui/menu/cities.lua Normal file
View File

@ -0,0 +1 @@
local textinput = require 'ui.textinput'

0
ui/menu/lines.lua Normal file
View File

View File

@ -6,6 +6,23 @@ local loadmodal = require 'ui.loadmodal'
local Camera = require 'ui.camera' local Camera = require 'ui.camera'
local map = require 'map.map' local map = require 'map.map'
local loadImg = love.graphics.newImage
local layers = {
{ name = "AF", layer = "africa" , menu = require 'ui.menu.territory' },
{ name = "EU", layer = "europe" , menu = require 'ui.menu.territory' },
{ name = "NA", layer = "northamerica" , menu = require 'ui.menu.territory' },
{ name = "SA", layer = "southamerica" , menu = require 'ui.menu.territory' },
{ name = "AS", layer = "southasia" , menu = require 'ui.menu.territory' },
{ name = "RU", layer = "russia" , menu = require 'ui.menu.territory' },
{ name = "PATH", layer = "travelnodes" , menu = require 'ui.menu.travelnodes', icon = loadImg( "icons/layer-travelnodes.png" )},
{ name = "AI", layer = "ainodes" , menu = require 'ui.menu.ainodes', icon = loadImg( "icons/layer-ainodes.png" )},
{ name = "CITY", layer = "cities" , menu = require 'ui.menu.cities', icon = loadImg( "icons/layer-cities.png" )},
{ name = "COAST", layer = "coastlines" , menu = require 'ui.menu.lines', icon = loadImg( "icons/layer-coastlines.png" )},
{ name = "LOW", layer = "coastlinesLow", menu = require 'ui.menu.lines', icon = loadImg( "icons/layer-coastlines-low.png" )},
{ name = "INT", layer = "international", menu = require 'ui.menu.lines', icon = loadImg( "icons/layer-international.png" )},
{ name = "SAIL", layer = "sailable" , menu = require 'ui.menu.territory', icon = loadImg( "icons/layer-sailable.png" )},
}
button.new{ button.new{
name = "LOAD", x = 250, y = 0, name = "LOAD", x = 250, y = 0,
w = 28 * 13, w = 28 * 13,
@ -22,23 +39,6 @@ button.new{
callback = map.undo, callback = map.undo,
icon = love.graphics.newImage( "icons/undo.bmp" ) } icon = love.graphics.newImage( "icons/undo.bmp" ) }
local layers = {
{ name = "AF", layer = "africa" },
{ name = "EU", layer = "europe" },
{ name = "NA", layer = "northamerica" },
{ name = "SA", layer = "southamerica" },
{ name = "AS", layer = "southasia" },
{ name = "RU", layer = "russia" },
{ name = "PATH", layer = "travelnodes" , icon = love.graphics.newImage( "icons/layer-travelnodes.png" )},
{ name = "AI", layer = "ainodes" , icon = love.graphics.newImage( "icons/layer-ainodes.png" )},
{ name = "CITY", layer = "cities" , icon = love.graphics.newImage( "icons/layer-cities.png" )},
{ name = "COAST", layer = "coastlines" , icon = love.graphics.newImage( "icons/layer-coastlines.png" )},
{ name = "LOW", layer = "coastlinesLow", icon = love.graphics.newImage( "icons/layer-coastlines-low.png" )},
{ name = "INT", layer = "international", icon = love.graphics.newImage( "icons/layer-international.png" )},
{ name = "SAIL", layer = "sailable" , icon = love.graphics.newImage( "icons/layer-sailable.png" )},
}
local showButtons = {} local showButtons = {}
local visibilityIcon = love.graphics.newImage( "icons/eye.bmp" ) local visibilityIcon = love.graphics.newImage( "icons/eye.bmp" )
local function updateVisibilityIcons() local function updateVisibilityIcons()
@ -92,6 +92,7 @@ local x = 250
local soloButtons = {} local soloButtons = {}
local editButtons = {} local editButtons = {}
for i = 1, #layers do for i = 1, #layers do
editButtons[i] = button.new( copy( i, { editButtons[i] = button.new( copy( i, {
y = 3 * 28, y = 3 * 28,
x = x + (button.h + 4) * ( i - 1 ), x = x + (button.h + 4) * ( i - 1 ),

0
ui/menu/territory.lua Normal file
View File

0
ui/menu/travelnodes.lua Normal file
View File

View File

@ -5,7 +5,11 @@ t.__index = t
local i = 0 local i = 0
function t.start( self ) function t.start( self )
love.graphics.setScissor( 0, 0, love.graphics.getDimensions() ) love.graphics.setScissor(
self.x or 0,
self.y or 0,
self.w or love.graphics.getWidth(),
self.h or love.graphics.getDimensions())
i = i + 1 i = i + 1
t[i] = t[i] or {} t[i] = t[i] or {}

0
ui/territory.lua Normal file
View File

View File

@ -47,6 +47,7 @@ function t.keypressed(key, code, isRepeat)
end end
if key == "backspace" then if key == "backspace" then
local text = t.currentModal.currentField
-- get the byte offset to the last UTF-8 character in the string. -- get the byte offset to the last UTF-8 character in the string.
local byteoffset = utf8.offset(text, -1) local byteoffset = utf8.offset(text, -1)
@ -56,7 +57,7 @@ function t.keypressed(key, code, isRepeat)
text = string.sub(text, 1, byteoffset - 1) text = string.sub(text, 1, byteoffset - 1)
end end
end end
if key == "escape" then if code == "escape" then
return t:stop() return t:stop()
end end
end end