dcearth/main.lua

261 lines
7.5 KiB
Lua
Raw Normal View History

2023-04-28 17:35:52 +00:00
local love = assert( love, "This tool requires LOVE: love2d.org" )
--assert( require('mobdebug') ).start() --remote debugger
2023-04-28 17:35:52 +00:00
local map = require 'map'
2024-04-29 01:21:32 +00:00
local savemodal = require 'savemodal'
2024-04-21 14:10:53 +00:00
local button = require 'button'
2023-04-28 17:35:52 +00:00
local SAVEDIRECTORY = "out/"
local Camera = require 'camera'
2023-04-28 17:35:52 +00:00
function love.load()
2023-07-28 23:23:08 +00:00
2023-04-28 17:35:52 +00:00
local lfs = assert( love.filesystem )
lfs.setIdentity( "dcearth", false )
assert( lfs.createDirectory( SAVEDIRECTORY.."data/earth" ))
assert( lfs.createDirectory( SAVEDIRECTORY.."data/graphics" ))
2023-07-28 23:23:08 +00:00
2024-04-29 01:21:32 +00:00
love.keyboard.setKeyRepeat( true )
2024-04-28 01:38:23 +00:00
love.graphics.setNewFont( 14 )--, "mono" )
2023-04-28 17:35:52 +00:00
end
function love.directorydropped( path )
2024-04-28 01:38:23 +00:00
if map.path then
assert( love.filesystem.unmount( map.path ) )
map.loaded = false
end
love.filesystem.mount( path, "" )
return map.load( path )
end
2023-04-28 17:35:52 +00:00
function love.update( dt )
local tx, ty = 0, 0
local moveCamera = false
if love.keyboard.isScancodeDown( "w" ) then moveCamera = true; ty = ty + 30 * dt end
if love.keyboard.isScancodeDown( "a" ) then moveCamera = true; tx = tx - 30 * dt end
if love.keyboard.isScancodeDown( "s" ) then moveCamera = true; ty = ty - 30 * dt end
if love.keyboard.isScancodeDown( "d" ) then moveCamera = true; tx = tx + 30 * dt end
if love.keyboard.isScancodeDown( "q" ) then Camera.Zoom( dt ) end
if love.keyboard.isScancodeDown( "e" ) then Camera.Zoom( -dt ) end
if moveCamera then Camera.Translate( tx, ty ) end
2023-07-28 23:23:08 +00:00
2023-04-28 17:35:52 +00:00
end
function love.draw()
if not map.loaded then
2024-04-28 01:38:23 +00:00
local w, h = love.graphics.getDimensions()
2024-04-29 01:21:32 +00:00
return love.graphics.printf( "Drag and drop folder to begin.", w / 2 - 200, h / 2 - 128, 400, "center")
end
love.graphics.push( "all" )
map.draw()
love.graphics.pop()
2023-07-28 23:23:08 +00:00
--Status bar.
local x, y = love.mouse.getPosition()
local wx, wy = Camera.GetWorldCoordinate( x, y )
2023-07-28 23:23:08 +00:00
local bx, by = Camera.GetBitmapCoordinate( x, y )
2024-04-28 01:38:23 +00:00
local h = love.graphics.getHeight() - 60
love.graphics.setColor( 0.1, 0.1, 0.5, 0.8 )
love.graphics.rectangle( "fill", 0, 0, 250, love.graphics.getHeight() )
love.graphics.setColor( 1, 1, 1, 1 )
2024-04-28 01:38:23 +00:00
love.graphics.print(([[
2024-04-29 01:21:32 +00:00
SCREEN %-12d %-12d
WORLD %-12.2f%-12.2f
BITMAP %-12.2f%-12.2f
%s]]):format(x, y, wx, wy, bx, by, map.editLayer and map.editLayer.filename or ""), 0, 0)
2024-04-28 01:38:23 +00:00
if map.selected then love.graphics.print( map.selected:formatDisplayInfo(), 0, 80 ) end
if map.selectionLocked then end
love.graphics.setColor( 1, 1, 1, 0.8 )
button:draw()
end
function love.resize(w, h)
Camera.Resize( w, h )
end
function love.wheelmoved(x, y)
2024-04-29 01:21:32 +00:00
Camera.Zoom( (y > 0) and 0.1 or -0.1 )
end
2024-04-28 01:38:23 +00:00
function love.mousepressed( x, y, mouseButton, istouch, presses )
local wx, wy = Camera.GetWorldCoordinate( x, y )
2024-04-29 01:21:32 +00:00
if button.selected and button.selected:contains( x, y ) then
print( ("MOUSE\tx %f\ty %f\twx %f\twy %f"):format(x, y, wx, wy) )
return button.selected:callback()
end
end
function love.mousemoved( x, y, dx, dy, istouch )
2024-04-28 01:38:23 +00:00
if not map.loaded then return end
--mouse over menu
button.selectIn( x, y )
--mouse on map
if map.selectionLocked then return end
if map.editLayer and map.editLayer.selectNearest then
map.selected = map.editLayer:selectNearest( Camera.GetWorldCoordinate( x, y ) )
end
2023-04-28 17:35:52 +00:00
end
2024-04-28 01:38:23 +00:00
function love.keypressed(key, code, isRepeat)
2024-04-29 01:21:32 +00:00
if code == "left" then return button.selectPrev() end
if code == "right" then return button.selectNext() end
if code == "down" then return button.selectNextInGroup() end
if code == "up" then return button.selectPrevInGroup() end
2024-04-28 01:38:23 +00:00
if code == "return" then return button.selected:callback() end
if key == "c" then
map.selectionLocked = not( map.selectionLocked )
end
end
do
local function toolCallback( self )
local f = (map.layers[self.layer])[self.name]
if f then return f(self) end
end
local tools = {
button.new{ name = "SELECT"},
button.new{ name = "ERASE",},
button.new{ name = "MOVE", },
button.new{ name = "ADD", },
button.new{ name = "EDIT", },
button.new{ name = "DRAW", },
}
for i, v in ipairs( tools ) do
v.callback = toolCallback
v.y = 250 + (v.h + 4) * ( i + 1 )
v.visible = false
end
local layerButtons = {}
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" },
{ name = "AI", layer = "ainodes" },
{ name = "CITY", layer = "cities" },
{ name = "COAST", layer = "coastlines" },
{ name = "LOW", layer = "coastlinesLow"},
{ name = "SAIL", layer = "sailable" },
}
local visibilityIcon = love.graphics.newImage( "icons/eye.bmp" )
local function toggleVisibleLayer( self )
if not (self and self.layer) then return end
local ml = map.layers[ self.layer ]
ml.visible = not( ml.visible )
self.icon = ml.visible and visibilityIcon
end
local soloIcon = love.graphics.newImage( "icons/eye.bmp" )
local function soloVisibleLayer( self )
--hide icons for disabled invisible layers
print( "===SOLO LAYER===", self.layer )
for i, button in ipairs( layerButtons ) do
if button.layer ~= self.layer then
button.icon = false
end
end
2024-04-29 01:21:32 +00:00
self.icon = soloIcon
2024-04-28 01:38:23 +00:00
for k, layer in pairs( map.layers ) do
print( "invisible layer, map:", k, layer)
layer.visible = false
end
map.layers[ self.layer ].visible = true
2024-04-29 01:21:32 +00:00
2024-04-28 01:38:23 +00:00
end
2024-04-29 01:21:32 +00:00
local backButton
2024-04-28 01:38:23 +00:00
local function editLayer( self )
map.editLayer = map.layers[ self.layer ]
for k, button in pairs( layerButtons ) do button.visible = false end
for k, button in pairs( tools ) do
button.visible = true
button.layer = self.layer
end
backButton.visible = true
return soloVisibleLayer( self )
end
local function copy( i, target )
for k, v in pairs( layers[i] ) do
target[k] = target[k] or v
end
return target
end
local y = 250
2024-04-29 01:21:32 +00:00
local soloButtons = {}
local showButtons = {}
local editButtons = {}
2024-04-28 01:38:23 +00:00
for i = 1, #layers do
2024-04-29 01:21:32 +00:00
editButtons[i] = button.new( copy( i, {
2024-04-28 01:38:23 +00:00
x = 8,
y = y + (button.h + 4) * i,
w = 112,
2024-04-29 01:21:32 +00:00
callback = editLayer,
group = "edit",
2024-04-28 01:38:23 +00:00
}))
2024-04-29 01:21:32 +00:00
layerButtons[ 3 * i - 2 ] = editButtons[i]
2024-04-28 01:38:23 +00:00
2024-04-29 01:21:32 +00:00
showButtons[i] = button.new( copy( i, {
2024-04-28 01:38:23 +00:00
x = 128,
y = y + (button.h + 4) * i,
w = 24,
name = "V",
callback = toggleVisibleLayer,
icon = visibilityIcon,
2024-04-29 01:21:32 +00:00
group = "show",
2024-04-28 01:38:23 +00:00
}))
2024-04-29 01:21:32 +00:00
layerButtons[ 3 * i - 1 ] = showButtons[i]
2024-04-28 01:38:23 +00:00
2024-04-29 01:21:32 +00:00
soloButtons[i] = button.new( copy( i, {
2024-04-28 01:38:23 +00:00
x = 160,
y = y + (button.h + 4) * i,
w = 24,
name = "S",
callback = soloVisibleLayer,
2024-04-29 01:21:32 +00:00
icon = soloIcon,
group = "solo",
2024-04-28 01:38:23 +00:00
}))
2024-04-29 01:21:32 +00:00
layerButtons[ 3 * i ] = soloButtons[i]
2024-04-28 01:38:23 +00:00
end
2024-04-29 01:21:32 +00:00
local function back( self )
for k, button in pairs( tools ) do button.visible = false end
for k, button in pairs( layerButtons ) do button.visible = true end
self.visible = false
map.editLayer = false
end
backButton = button.new{
name = "UP",
visible = false,
y = 250 + button.h + 4,
icon = love.graphics.newImage( "icons/up.bmp" ),
callback = back,
}
button.new{ name = "UNDO", y = 250, callback = map.undo, icon = love.graphics.newImage( "icons/undo.bmp" ) }
button.new{ name = "SAVE", y = 222, callback = savemodal.start, icon = false }
2023-04-28 17:35:52 +00:00
end
2024-04-28 01:38:23 +00:00