add some saving, some buttons

This commit is contained in:
wan-may 2024-04-28 22:21:32 -03:00
parent 8b766723f1
commit 03e45194cf
19 changed files with 396 additions and 86 deletions

5
ai.lua
View File

@ -20,6 +20,7 @@ end
function t.load( filename ) function t.load( filename )
local img, imgd = bmp.load( filename ) local img, imgd = bmp.load( filename )
local nodes = { local nodes = {
filename = filename,
visible = true, visible = true,
all = {}, all = {},
att = {}, att = {},
@ -77,8 +78,8 @@ function t.draw( nodes )
lg.points( nodes.ptsDef ) lg.points( nodes.ptsDef )
end end
function t.save( nodes, filename ) function t.save( nodes )
return bmp.savePoints( nodes.all, "512rgb24" )
end end
return t return t

121
bmp.lua
View File

@ -7,6 +7,41 @@ local lfs = love.filesystem
local ffi = require 'ffi' local ffi = require 'ffi'
local bit = require 'bit' local bit = require 'bit'
local function getHeader( filename )
local offset = love.data.unpack( "<I4", assert(love.filesystem.read( "data", filename, 14 )), 11 )
local header, size = assert( love.filesystem.read( filename, offset ) )
print( "BMP HEADER", filename, size, "\n", header )
return header
end
local formats = {
["512rgb24"] = {
header = getHeader( "data/earth/africa.bmp" ),
encode = function( r ) return math.floor( r * 255 ) end,
bytesPerPixel = 3,
w = 512,
h = 285,
},
["512r4"] = {
header = getHeader( "data/earth/sailable.bmp" ),
encode = function( r ) return math.floor( r * 255 / 16 ) end,
bytesPerPixel = 0.5,
w = 512,
h = 285,
},
["800r4"] = {
header = getHeader( "data/earth/travel_nodes.bmp" ),
encode = function( r ) return math.floor( r * 255 / 16 ) end,
bytesPerPixel = 0.5,
w = 512,
h = 285,
}
}
function t.header( format )
return formats[format] and formats[format].header
end
function t.load( filename ) function t.load( filename )
local imgd = love.image.newImageData( filename ) local imgd = love.image.newImageData( filename )
print( "LOADING BITMAP: ", filename, imgd:getSize(), imgd:getFormat(), imgd:getDimensions() ) print( "LOADING BITMAP: ", filename, imgd:getSize(), imgd:getFormat(), imgd:getDimensions() )
@ -15,15 +50,93 @@ function t.load( filename )
return img, imgd return img, imgd
end end
function t.save( data, filename, format ) --maps an array of 1-byte pixel values (numbers 0 to 15 inclusive)
--to an array of half-byte pixel values (which can be concatenated into a string)
--BUT don't touch the first element (which is assumed to be a header or something)
local function foldByteArray( bytes )
local nybbles = { bytes[1] }
for j = 2, #bytes / 2 do
local a, b = bytes[ 2 * j - 2 ], bytes[ 2 * j - 1 ]
nybbles[j] = string.char( 16 * a + b )
end
return nybbles
end
local function packTwentyFour( bytes )
local twentyFour = { bytes[1] }
for j = 2, #bytes do
bytes[j] = string.char( bytes[j], bytes[j], bytes[j] )
end
end
function t.save( data, format )
local w, h = data:getDimensions() local w, h = data:getDimensions()
local str = "" format = assert( formats[format] )
local bytes = { format.header }
format.byte = 0
local i = 2
for x = 0, w - 1 do for x = 0, w - 1 do
for y = 0, h - 1 do for y = 0, h - 1 do
bytes[i] = format.encode( data:getPixel(x, y) )
i = i + 1
end end
end end
return str if format.bytesPerPixel < 1 then bytes = foldByteArray( bytes )
else packTwentyFour( bytes ) end
return table.concat( bytes )
end end
--takes an array of world-space points in [-180, 180] x [-100, 100]
--e.g { { x = 0.5, y = 0.5 }, { x = 0.4, y = 0.6 }, }, etc.
--and a function which maps points to colours
function t.savePoints( points, format )
format = assert( formats[format] )
--set up bitmap as an array of pixels
local w, h = format.w, format.h
local size = 2 + format.w * format.h
local bitmap = { format.header }
for j = 2, size do bitmap[j] = 0 end
--this is black-and-white only. easy case
if format.bytesPerPixel < 1 then
for i, point in ipairs( points ) do
local wx, wy = point.x, point.y
--get bitmap coordinates
local x, y = math.floor(format.w * (wx + 180) / 360), math.floor(format.h * (1.0 - (wy + 100) / 200))
--get index into byte array
local idx = 2 + x * format.h + y
bitmap[idx] = 0x0f --0b00001111
end
--now map pixels to 4-bit strings, slap on the header, and pass it back up
return table.concat( foldByteArray( bitmap ) )
end
--cf. ai.lua: these points can be red or green
for i, point in ipairs( points ) do
local wx, wy = point.x, point.y
--get bitmap coordinates
local x, y = math.floor(format.w * (wx + 180) / 360), math.floor(format.h * (1.0 - (wy + 100) / 200))
--get index into byte array
local idx = 2 + x * format.h + y
--in-editor we could have two points in the same place, possibly of the same or different types
--in case we miss something upstream, don't corrupt these duplicate points,
--just saturate the attack/defense value
if bitmap[idx] == 0 then bitmap[idx] = { attack = point.attacking, place = not(point.attacking)}
else
bitmap[idx].attack = bitmap[idx].attack or point.attacking
bitmap[idx].place = bitmap[idx].place or not(point.attacking)
end
end
--now map pixels to 3-byte strings
for j = 2, size do
if bitmap[j] == 0
then bitmap[j] = "\0\0\0"
else bitmap[j] = string.char( bitmap[j].attack and 0xff or 0, bitmap[j].place and 0xff or 0, 0 )
end
end
return table.concat( bitmap )
end
return t return t

View File

@ -8,6 +8,7 @@ local t = {
y = 250, y = 250,
w = 176, w = 176,
h = 24, h = 24,
group = false,
visible = true, visible = true,
callback = function( self ) return print( "clicked button: ", self.name, self.x, self.y, self.w, self.h, self.visible ) end callback = function( self ) return print( "clicked button: ", self.name, self.x, self.y, self.w, self.h, self.visible ) end
} }
@ -68,6 +69,27 @@ function t.selectIn( x, y )
repeat t.selected = t.selected.next until (t.selected == t) or (t.selected.visible and t.selected:contains( x, y )) repeat t.selected = t.selected.next until (t.selected == t) or (t.selected.visible and t.selected:contains( x, y ))
end end
function t.selectNextInGroup()
local group = t.selected and t.selected.group
if not group then return t.selectNext() end
repeat t.selectNext() until group == t.selected.group
end
function t.selectPrevInGroup()
local group = t.selected and t.selected.group
if not group then return t.selectPrev() end
repeat t.selectPrev() until group == t.selected.group
end
function t.displayGroup( group, show )
local b = t
repeat
b = b.next
b.visible = ( b.group == group )
until b == t
t.visible = true
end
function t.deselect( b ) function t.deselect( b )
t.selected = t t.selected = t
end end

View File

@ -57,7 +57,7 @@ function t.load( filename )
print( "=== LOADING CITIES. ===" ) print( "=== LOADING CITIES. ===" )
cities = { visible = true, active = false } cities = { visible = true, active = false, filename = filename }
local n = 1 local n = 1
local idxPts = 1 local idxPts = 1
local idxCaps = 1 local idxCaps = 1
@ -97,8 +97,7 @@ function t.load( filename )
return cities return cities
end end
function t.save( cities, filename ) function t.save( cities )
print( "=== SAVING CITIES ===" )
local str = {} local str = {}
for n, city in ipairs( cities ) do for n, city in ipairs( cities ) do
str[n] = ("%-41s%-41s%-14f%-14f%-19d %d"):format( str[n] = ("%-41s%-41s%-14f%-14f%-19d %d"):format(

View File

@ -13,7 +13,7 @@ function love.conf(t)
t.window.title = "dcEarth" -- The window title (string) t.window.title = "dcEarth" -- The window title (string)
t.window.icon = "icons/favicon.png" -- Filepath to an image to use as the window's icon (string) t.window.icon = "icons/favicon.png" -- Filepath to an image to use as the window's icon (string)
t.window.width = 800 -- The window width (number) t.window.width = 800 -- The window width (number)
t.window.height = 600 -- The window height (number) t.window.height = 640 -- The window height (number)
t.window.borderless = false -- Remove all border visuals from the window (boolean) t.window.borderless = false -- Remove all border visuals from the window (boolean)
t.window.resizable = true -- Let the window be user-resizable (boolean) t.window.resizable = true -- Let the window be user-resizable (boolean)
t.window.minwidth = 512 -- Minimum window width if the window is resizable (number) t.window.minwidth = 512 -- Minimum window width if the window is resizable (number)

BIN
icons/city.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
icons/cursorerase.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
icons/cursorselect.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
icons/undo.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
icons/up.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@ -16,7 +16,7 @@ function polygon:formatDisplayInfo()
end end
function t.load( filename ) function t.load( filename )
local polys = { visible = true } local polys = { visible = true, filename = filename }
local poly = {} local poly = {}
local n = 1 local n = 1
local k = 1 local k = 1
@ -72,8 +72,7 @@ function t.selectNearest( lines, wx, wy )
return nearest return nearest
end end
function t.save( lines, filename ) function t.save( lines )
print( "=== SAVING LINES ===" )
local str = {} local str = {}
for i, poly in ipairs( lines ) do for i, poly in ipairs( lines ) do
str[i] = table.concat( poly, " " ) str[i] = table.concat( poly, " " )

View File

@ -1,10 +1,10 @@
local love = assert( love, "This tool requires LOVE: love2d.org" ) 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' local map = require 'map'
local savemodal = require 'savemodal'
local button = require 'button' local button = require 'button'
local SAVEDIRECTORY = "out/" local SAVEDIRECTORY = "out/"
local Camera = require 'camera' local Camera = require 'camera'
local wasKeyPressed
function love.load() function love.load()
@ -13,7 +13,7 @@ function love.load()
assert( lfs.createDirectory( SAVEDIRECTORY.."data/earth" )) assert( lfs.createDirectory( SAVEDIRECTORY.."data/earth" ))
assert( lfs.createDirectory( SAVEDIRECTORY.."data/graphics" )) assert( lfs.createDirectory( SAVEDIRECTORY.."data/graphics" ))
love.keyboard.setKeyRepeat( true )
love.graphics.setNewFont( 14 )--, "mono" ) love.graphics.setNewFont( 14 )--, "mono" )
end end
@ -43,7 +43,7 @@ end
function love.draw() function love.draw()
if not map.loaded then if not map.loaded then
local w, h = love.graphics.getDimensions() local w, h = love.graphics.getDimensions()
return love.graphics.printf( "Drag and drop folder to begin.", w / 2 - 200, h / 2 - 128, 200, "center") return love.graphics.printf( "Drag and drop folder to begin.", w / 2 - 200, h / 2 - 128, 400, "center")
end end
love.graphics.push( "all" ) love.graphics.push( "all" )
@ -59,9 +59,10 @@ function love.draw()
love.graphics.rectangle( "fill", 0, 0, 250, love.graphics.getHeight() ) love.graphics.rectangle( "fill", 0, 0, 250, love.graphics.getHeight() )
love.graphics.setColor( 1, 1, 1, 1 ) love.graphics.setColor( 1, 1, 1, 1 )
love.graphics.print(([[ love.graphics.print(([[
SCREEN%8d%8d SCREEN %-12d %-12d
WORLD %8.2f%8.2f WORLD %-12.2f%-12.2f
BITMAP%8.2f%8.2f]]):format(x, y, wx, wy, bx, by), 0, 0) BITMAP %-12.2f%-12.2f
%s]]):format(x, y, wx, wy, bx, by, map.editLayer and map.editLayer.filename or ""), 0, 0)
if map.selected then love.graphics.print( map.selected:formatDisplayInfo(), 0, 80 ) end if map.selected then love.graphics.print( map.selected:formatDisplayInfo(), 0, 80 ) end
if map.selectionLocked then end if map.selectionLocked then end
@ -75,13 +76,16 @@ function love.resize(w, h)
end end
function love.wheelmoved(x, y) function love.wheelmoved(x, y)
Camera.Zoom( (y > 0) and 0.5 or -0.5 ) Camera.Zoom( (y > 0) and 0.1 or -0.1 )
end end
function love.mousepressed( x, y, mouseButton, istouch, presses ) function love.mousepressed( x, y, mouseButton, istouch, presses )
local wx, wy = Camera.GetWorldCoordinate( x, y ) local wx, wy = Camera.GetWorldCoordinate( x, y )
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) ) print( ("MOUSE\tx %f\ty %f\twx %f\twy %f"):format(x, y, wx, wy) )
if button.selected and button.selected:contains( x, y ) then button.selected:callback() end return button.selected:callback()
end
end end
function love.mousemoved( x, y, dx, dy, istouch ) function love.mousemoved( x, y, dx, dy, istouch )
@ -97,15 +101,13 @@ function love.mousemoved( x, y, dx, dy, istouch )
end end
function love.keypressed(key, code, isRepeat) function love.keypressed(key, code, isRepeat)
wasKeyPressed = true
if code == "down" then return button.selectNext() end if code == "left" then return button.selectPrev() end
if code == "up" 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
if code == "return" then return button.selected:callback() end if code == "return" then return button.selected:callback() end
if key == "l" then
return map.save()
end
if key == "c" then if key == "c" then
map.selectionLocked = not( map.selectionLocked ) map.selectionLocked = not( map.selectionLocked )
end end
@ -115,8 +117,6 @@ end
do do
button.new{ name = "UNDO", y = 250, callback = map.undo }
local function toolCallback( self ) local function toolCallback( self )
local f = (map.layers[self.layer])[self.name] local f = (map.layers[self.layer])[self.name]
if f then return f(self) end if f then return f(self) end
@ -136,23 +136,8 @@ do
v.visible = false v.visible = false
end end
local layerButtons = {} local layerButtons = {}
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
local backButton = button.new{
name = "BACK",
visible = false,
y = 250 + button.h + 4,
callback = back,
}
local layers = { local layers = {
{ name = "AF", layer = "africa" }, { name = "AF", layer = "africa" },
{ name = "EU", layer = "europe" }, { name = "EU", layer = "europe" },
@ -185,13 +170,17 @@ do
button.icon = false button.icon = false
end end
end end
self.icon = soloIcon
for k, layer in pairs( map.layers ) do for k, layer in pairs( map.layers ) do
print( "invisible layer, map:", k, layer) print( "invisible layer, map:", k, layer)
layer.visible = false layer.visible = false
end end
map.layers[ self.layer ].visible = true map.layers[ self.layer ].visible = true
end end
local backButton
local function editLayer( self ) local function editLayer( self )
map.editLayer = map.layers[ self.layer ] map.editLayer = map.layers[ self.layer ]
for k, button in pairs( layerButtons ) do button.visible = false end for k, button in pairs( layerButtons ) do button.visible = false end
@ -212,32 +201,60 @@ do
local y = 250 local y = 250
local soloButtons = {}
local showButtons = {}
local editButtons = {}
for i = 1, #layers do for i = 1, #layers do
editButtons[i] = button.new( copy( i, {
layerButtons[ 3 * i - 2 ] = button.new( copy( i, {
x = 8, x = 8,
y = y + (button.h + 4) * i, y = y + (button.h + 4) * i,
w = 112, w = 112,
callback = editLayer callback = editLayer,
group = "edit",
})) }))
layerButtons[ 3 * i - 2 ] = editButtons[i]
layerButtons[ 3 * i - 1 ] = button.new( copy( i, { showButtons[i] = button.new( copy( i, {
x = 128, x = 128,
y = y + (button.h + 4) * i, y = y + (button.h + 4) * i,
w = 24, w = 24,
name = "V", name = "V",
callback = toggleVisibleLayer, callback = toggleVisibleLayer,
icon = visibilityIcon, icon = visibilityIcon,
group = "show",
})) }))
layerButtons[ 3 * i - 1 ] = showButtons[i]
layerButtons[ 3 * i ] = button.new( copy( i, { soloButtons[i] = button.new( copy( i, {
x = 160, x = 160,
y = y + (button.h + 4) * i, y = y + (button.h + 4) * i,
w = 24, w = 24,
name = "S", name = "S",
callback = soloVisibleLayer, callback = soloVisibleLayer,
icon = soloIcon icon = soloIcon,
group = "solo",
})) }))
end layerButtons[ 3 * i ] = soloButtons[i]
end
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 }
end end

0
mainmenu.lua Normal file
View File

23
map.lua
View File

@ -177,27 +177,18 @@ function map.draw()
end end
--[[local function write( filename, string )
os.rename( filename, filename..".bak" ) --just in case :^)
local file = assert( io.open( filename, "w+" ) )
assert( file:write( string ) )
file:close()
end]]
local function write( filename, string ) local function write( filename, string )
print( "Pretending to write", string:len(), "bytes to", filename ) print( "Pretending to write", string:len(), "bytes to", filename )
--[[ os.rename( filename, filename..".bak" ) --just in case :^)
local file = assert( io.open( filename, "w+" ) )
assert( file:write( string ) )
file:close()]]
end end
function map.save() function map.save()
write( map.path.."/data/earth/cities.dat", map.cities:save()) for k, layer in pairs( layers ) do
write( map.path.."/data/earth/coastlines.dat", map.coastlines:save()) print( "SAVING:", k, tostring( layer.filename ) )
write( map.path.."/data/earth/coastlines-low.dat", map.coastlinesLow:save()) write( map.path..tostring( layer.filename ), assert( layer:save() ) )
write( map.path.."/data/earth/international.dat", map.international:save())
map.sailable:save()
map.travelnodes:save()
map.ainodes:save()
for k, v in pairs(map.territory) do
map.territory[k]:save()
end end
end end

51
modal.lua Normal file
View File

@ -0,0 +1,51 @@
local love = assert( love )
local button = require( "button" )
local t = {}
t.__index = t
local i = 0
function t.start( self )
i = i + 1
t[i] = t[i] or {}
--store callbacks
for name in pairs( self ) do
if love[name] then
t[i][name] = love[name]
love[name] = self[name]
end
end
--store menus
local b = button.next
repeat
t[i][b] = b.visible
b = b.next
until b == button
end
function t.stop( self )
--restore callbacks
for name in pairs( self ) do
if love[name] then
love[name] = t[i][name]
end
end
--restore menus
local b = button
repeat
b = b.next
b.visible = t[i][b]
until b == button
t[i] = nil
i = i - 1
end
function t.new( modal )
return setmetatable( modal or {}, t )
end
return t

52
savemodal.lua Normal file
View File

@ -0,0 +1,52 @@
local love = assert( love )
local modal = require( "modal" )
local button = require( "button" )
local map = require( "map" )
local t = {}
local saveLocation = map.path
local saveButton = button.new{
group = "saveModal",
name = "save",
callback = function() map.save(); return t:stop() end,
visible = false,
x = 20,
y = 20,
w = 400,
h = 100,
}
local cancelButton = button.new{
group = "saveModal",
name = "cancel",
visible = false,
callback = function() return t:stop() end,
x = 20,
y = 150,
w = 400,
h = 100
}
function t.start()
modal.start( t )
button.selected = saveButton
saveButton.name = "save to "..map.path
button.displayGroup( "saveModal", true )
end
function t.draw()
love.graphics.clear( 0,0,0,1 )
love.graphics.setColor( 1, 1, 1, 0.5 )
button:draw()
end
function t.directorydropped( path )
saveLocation = path
map.path = path
saveButton.name = "save to "..map.path
return love.filesystem.mount( path, "" )
end
return modal.new( t )

View File

@ -18,6 +18,7 @@ function t.load( filename, name )
local territory = { local territory = {
visible = true, visible = true,
name = name, name = name,
filename = filename,
colour = colours[name], colour = colours[name],
border = {}, border = {},
img = img, img = img,
@ -145,7 +146,7 @@ function t.computeBorder( territory, threshold, key )
end end
function t.save( territory ) function t.save( territory )
return bmp.save( territory.imgd, "512rgb24" )
end end
return t return t

64
textinput.lua Normal file
View File

@ -0,0 +1,64 @@
local love = assert( love )
local utf8 = require("utf8")
local modal = require( "modal" )
local t = modal.new{ }
function t.setCurrentModal( fields )
t.currentModal = assert( fields )
t.currentIdx = 1
return t:start()
end
function t.draw()
if not t.currentModal then return end
love.graphics.setColor( 0, 0, 0, 0.3 )
love.graphics.rectangle("fill", 0, 0, love.graphics.getDimensions())
-- other fields
for i, field in ipairs( t.currentModal ) do
love.graphics.setColor( 1, 1, 1, 0.2 )
local mode = (i == t.currentIdx) and "fill" or "line"
love.graphics.rectangle( mode, 200, i * 28, 200, 14, 4 )
love.graphics.setColor( 1, 1, 1, 1 )
love.graphics.print( field.name, 8, i * 28 )
love.graphics.print( field.value, 216, i * 28 )
end
end
function t.textinput( char )
if t.currentModal then
local field = t.currentModal.currentFieldIdx
t.currentModal.currentField.value = t.currentModal.currentField.value .. char
end
end
function t.keypressed(key, code, isRepeat)
if code == "down" then
t.currentIdx = t.currentIdx + 1
if t.currentIdx > #(t.currentModal) then
t.currentIdx = 1
end
elseif code == "up" then
t.currentIdx = t.currentIdx - 1
if t.currentIdx < 1 then
t.currentIdx = #(t.currentModal)
end
end
if key == "backspace" then
-- get the byte offset to the last UTF-8 character in the string.
local byteoffset = utf8.offset(text, -1)
if byteoffset then
-- remove the last UTF-8 character.
-- string.sub operates on bytes rather than UTF-8 characters, so we couldn't do string.sub(text, 1, -2).
text = string.sub(text, 1, byteoffset - 1)
end
end
if key == "escape" then
return t:stop()
end
end
return t

View File

@ -54,7 +54,7 @@ function t.load( filename, sailable )
isSailable = sailable isSailable = sailable
local img, imgd = bmp.load( filename ) local img, imgd = bmp.load( filename )
local nodes = { visible = true, nodes = {}, points = {}, connections = {}, img = img } local nodes = { filename = filename, visible = true, nodes = {}, points = {}, connections = {}, img = img }
local n = 1 local n = 1
for x = 0, 799 do for x = 0, 799 do
for y = 0, 399 do for y = 0, 399 do
@ -106,8 +106,8 @@ function t.drawConnections( nodes )
end end
function t.save( nodes, filename ) function t.save( nodes )
return bmp.savePoints( nodes.nodes, "800r4")
end end
return t return t