From 03e45194cf16a5bf5ef3831aefc4a400189658d1 Mon Sep 17 00:00:00 2001 From: wan-may Date: Sun, 28 Apr 2024 22:21:32 -0300 Subject: [PATCH] add some saving, some buttons --- ai.lua | 5 +- bmp.lua | 129 ++++++++++++++++++++++++++++++++++++++--- button.lua | 22 +++++++ cities.lua | 17 +++--- conf.lua | 2 +- icons/city.bmp | Bin 0 -> 3126 bytes icons/cursorerase.png | Bin 0 -> 3126 bytes icons/cursorselect.png | Bin 0 -> 3126 bytes icons/undo.bmp | Bin 0 -> 3126 bytes icons/up.bmp | Bin 0 -> 3126 bytes lines.lua | 15 +++-- main.lua | 93 +++++++++++++++++------------ mainmenu.lua | 0 map.lua | 23 +++----- modal.lua | 51 ++++++++++++++++ savemodal.lua | 52 +++++++++++++++++ territory.lua | 3 +- textinput.lua | 64 ++++++++++++++++++++ travelNodes.lua | 6 +- 19 files changed, 396 insertions(+), 86 deletions(-) create mode 100644 icons/city.bmp create mode 100644 icons/cursorerase.png create mode 100644 icons/cursorselect.png create mode 100644 icons/undo.bmp create mode 100644 icons/up.bmp create mode 100644 mainmenu.lua create mode 100644 modal.lua create mode 100644 savemodal.lua create mode 100644 textinput.lua diff --git a/ai.lua b/ai.lua index b440121..fec5ba0 100644 --- a/ai.lua +++ b/ai.lua @@ -20,6 +20,7 @@ end function t.load( filename ) local img, imgd = bmp.load( filename ) local nodes = { + filename = filename, visible = true, all = {}, att = {}, @@ -77,8 +78,8 @@ function t.draw( nodes ) lg.points( nodes.ptsDef ) end -function t.save( nodes, filename ) - +function t.save( nodes ) + return bmp.savePoints( nodes.all, "512rgb24" ) end return t \ No newline at end of file diff --git a/bmp.lua b/bmp.lua index 81afbf4..64d35b0 100644 --- a/bmp.lua +++ b/bmp.lua @@ -7,6 +7,41 @@ local lfs = love.filesystem local ffi = require 'ffi' local bit = require 'bit' +local function getHeader( filename ) + local offset = love.data.unpack( "&U66$mds#aviIm8(cW0#__TM&7|Fy}v%3iMh znZ8oVy5{9l&LgtK5|M|sFCRRael?&;j}%^tG{-bn@Uamld{hL0_eVU!dm>19E+Pbv zMkL^TL>kUSbi&PuAxr?d|FA&DkM6*><~gRa1-2d2$TVA&*I{Ut#zT4i%8sud1otsh vrx=2#3h@eTXQm!m`iz~?m=TPT*HjU_kymO2Z;2`gzPTCw^37+_X5xKLv9ToL literal 0 HcmV?d00001 diff --git a/icons/cursorerase.png b/icons/cursorerase.png new file mode 100644 index 0000000000000000000000000000000000000000..7790fd1a32eeef6916b5d5aab1b441c96a3dc530 GIT binary patch literal 3126 zcmeH_I}U*E&{0FI8Hzyr8@|Kn(532FKhLJmwZJK8NS*BsaQiyi-`{ zMJM`MVR?Pz4DEL4LL|KV6bSECB5_8PNO1mr*%EI)CIMBtDxthqRrSy5sf3l5jmHvb z$tKntB~ZTW2`UmHY$2&4+f?Kerx%=29cegYwtoyWS(quwXfXj*Fw)SXQkqpcMKh$3 Q=={QR+~pha4g75bH=PsRMF0Q* literal 0 HcmV?d00001 diff --git a/icons/cursorselect.png b/icons/cursorselect.png new file mode 100644 index 0000000000000000000000000000000000000000..3ce5ee17ecccde867860f41bde1ef849a1493a39 GIT binary patch literal 3126 zcmZ?rHRE9b12Z700mKSW%*Y@CWB~zG9>Rf;|3|^l2?1J1*{J#W)52)@jD`>X5Wp`> zN&124#ZN+P$FBw`i`9_?b-^_fREgDmVgw;36QhzmHKXR^54X|q84Vx&A%I_&*69bo K?W3|}ga7~mDd{-? literal 0 HcmV?d00001 diff --git a/icons/undo.bmp b/icons/undo.bmp new file mode 100644 index 0000000000000000000000000000000000000000..88bcfd0f13473ec1cea8e18a83c49971c2dae045 GIT binary patch literal 3126 zcmeH_K@Nj33=p>o7f|eINGT z=l3$)OxZKfOHsb>^Eq!|x`7}46&mYw&&9i_tZZA6rDgrLEya~(x_-|Jye?Bqn0}$L zs{4~k&{#CnT?Da+U1A9#Rt3{70^=ggRK+40n_Dc(pktJFz>3gO)DE^XGIYLzi)*{Z zku0qX=dv>6Au}NMu^=Kt2gU@=OekD*M$(U#xM-1y7@1C}-9}ngvYia1${v8`~P!VF&883RJdgn0tuujk}Ml{`OeSjcV>J)xbOWw zrWX*-uG_=D>o}ckfd3ls-=`k=Vx?m_VmXlR+>f5*f_mrh=yhR2UDEaZT@24dhJV2l za8Qd+5h5fGXB+c|SvBk)mYih3hEVIa>;YpcOYYB-u))l#;~e7o?r8$IF=#pinL7r9 yJvo+f(|n5Lv?e~4#^z>PCb^`DbBJb&v1ZdzM{1?TQEideEs7yU8}}-fxNQR+dB8>h literal 0 HcmV?d00001 diff --git a/lines.lua b/lines.lua index 0cd8bd7..2b67877 100644 --- a/lines.lua +++ b/lines.lua @@ -8,15 +8,15 @@ local polymt = { __index = polygon } function polygon:formatDisplayInfo() return ([[ - x: %f - y: %f - X: %f - Y: %f - N: %d]]):format( self.x, self.y, self.X, self.Y, #self ) + x: %f + y: %f + X: %f + Y: %f + N: %d]]):format( self.x, self.y, self.X, self.Y, #self ) end function t.load( filename ) - local polys = { visible = true } + local polys = { visible = true, filename = filename } local poly = {} local n = 1 local k = 1 @@ -72,8 +72,7 @@ function t.selectNearest( lines, wx, wy ) return nearest end -function t.save( lines, filename ) - print( "=== SAVING LINES ===" ) +function t.save( lines ) local str = {} for i, poly in ipairs( lines ) do str[i] = table.concat( poly, " " ) diff --git a/main.lua b/main.lua index 1db3b3b..a51c04e 100644 --- a/main.lua +++ b/main.lua @@ -1,10 +1,10 @@ local love = assert( love, "This tool requires LOVE: love2d.org" ) --assert( require('mobdebug') ).start() --remote debugger local map = require 'map' +local savemodal = require 'savemodal' local button = require 'button' local SAVEDIRECTORY = "out/" local Camera = require 'camera' -local wasKeyPressed function love.load() @@ -13,7 +13,7 @@ function love.load() assert( lfs.createDirectory( SAVEDIRECTORY.."data/earth" )) assert( lfs.createDirectory( SAVEDIRECTORY.."data/graphics" )) - + love.keyboard.setKeyRepeat( true ) love.graphics.setNewFont( 14 )--, "mono" ) end @@ -43,7 +43,7 @@ end function love.draw() if not map.loaded then 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 love.graphics.push( "all" ) @@ -59,9 +59,10 @@ function love.draw() love.graphics.rectangle( "fill", 0, 0, 250, love.graphics.getHeight() ) love.graphics.setColor( 1, 1, 1, 1 ) love.graphics.print(([[ - SCREEN%8d%8d - WORLD %8.2f%8.2f - BITMAP%8.2f%8.2f]]):format(x, y, wx, wy, bx, by), 0, 0) + 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) if map.selected then love.graphics.print( map.selected:formatDisplayInfo(), 0, 80 ) end if map.selectionLocked then end @@ -75,13 +76,16 @@ function love.resize(w, h) end 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 function love.mousepressed( x, y, mouseButton, istouch, presses ) local wx, wy = Camera.GetWorldCoordinate( x, y ) - 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 + + 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 ) @@ -97,15 +101,13 @@ function love.mousemoved( x, y, dx, dy, istouch ) end function love.keypressed(key, code, isRepeat) - wasKeyPressed = true - if code == "down" then return button.selectNext() end - if code == "up" then return button.selectPrev() end + 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 if code == "return" then return button.selected:callback() end - if key == "l" then - return map.save() - end if key == "c" then map.selectionLocked = not( map.selectionLocked ) end @@ -115,8 +117,6 @@ end do - button.new{ name = "UNDO", y = 250, callback = map.undo } - local function toolCallback( self ) local f = (map.layers[self.layer])[self.name] if f then return f(self) end @@ -136,23 +136,8 @@ do v.visible = false end - 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 = { { name = "AF", layer = "africa" }, { name = "EU", layer = "europe" }, @@ -185,13 +170,17 @@ do button.icon = false end end + self.icon = soloIcon + for k, layer in pairs( map.layers ) do print( "invisible layer, map:", k, layer) layer.visible = false end map.layers[ self.layer ].visible = true + end + local backButton local function editLayer( self ) map.editLayer = map.layers[ self.layer ] for k, button in pairs( layerButtons ) do button.visible = false end @@ -212,32 +201,60 @@ do local y = 250 + local soloButtons = {} + local showButtons = {} + local editButtons = {} for i = 1, #layers do - - layerButtons[ 3 * i - 2 ] = button.new( copy( i, { + editButtons[i] = button.new( copy( i, { x = 8, y = y + (button.h + 4) * i, 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, y = y + (button.h + 4) * i, w = 24, name = "V", callback = toggleVisibleLayer, 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, y = y + (button.h + 4) * i, w = 24, name = "S", callback = soloVisibleLayer, - icon = soloIcon + icon = soloIcon, + group = "solo", })) + 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 diff --git a/mainmenu.lua b/mainmenu.lua new file mode 100644 index 0000000..e69de29 diff --git a/map.lua b/map.lua index d024fb3..c2801a7 100644 --- a/map.lua +++ b/map.lua @@ -177,27 +177,18 @@ function map.draw() 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 ) 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 function map.save() - write( map.path.."/data/earth/cities.dat", map.cities:save()) - write( map.path.."/data/earth/coastlines.dat", map.coastlines:save()) - write( map.path.."/data/earth/coastlines-low.dat", map.coastlinesLow: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() + for k, layer in pairs( layers ) do + print( "SAVING:", k, tostring( layer.filename ) ) + write( map.path..tostring( layer.filename ), assert( layer:save() ) ) end end diff --git a/modal.lua b/modal.lua new file mode 100644 index 0000000..552a8c9 --- /dev/null +++ b/modal.lua @@ -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 \ No newline at end of file diff --git a/savemodal.lua b/savemodal.lua new file mode 100644 index 0000000..5f54ffc --- /dev/null +++ b/savemodal.lua @@ -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 ) \ No newline at end of file diff --git a/territory.lua b/territory.lua index 8ef3382..4209102 100644 --- a/territory.lua +++ b/territory.lua @@ -18,6 +18,7 @@ function t.load( filename, name ) local territory = { visible = true, name = name, + filename = filename, colour = colours[name], border = {}, img = img, @@ -145,7 +146,7 @@ function t.computeBorder( territory, threshold, key ) end function t.save( territory ) - + return bmp.save( territory.imgd, "512rgb24" ) end return t \ No newline at end of file diff --git a/textinput.lua b/textinput.lua new file mode 100644 index 0000000..e206d56 --- /dev/null +++ b/textinput.lua @@ -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 \ No newline at end of file diff --git a/travelNodes.lua b/travelNodes.lua index bdb807f..b554d3c 100644 --- a/travelNodes.lua +++ b/travelNodes.lua @@ -54,7 +54,7 @@ function t.load( filename, sailable ) isSailable = sailable 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 for x = 0, 799 do for y = 0, 399 do @@ -106,8 +106,8 @@ function t.drawConnections( nodes ) end -function t.save( nodes, filename ) - +function t.save( nodes ) + return bmp.savePoints( nodes.nodes, "800r4") end return t \ No newline at end of file