From db5ce8b4f789c9c7cfc8a633fb81378ff8907fdc Mon Sep 17 00:00:00 2001 From: wan-may Date: Sat, 20 Jul 2024 14:43:09 -0300 Subject: [PATCH] sketch out more menus, implement editing and deleting cities, quantize 8-bit sailables back to 4 --- icons/check.png | Bin 3126 -> 3126 bytes icons/city-delete.png | Bin 0 -> 391 bytes icons/city-move.png | Bin 0 -> 3126 bytes icons/city-new.png | Bin 0 -> 357 bytes icons/node-attack.png | Bin 0 -> 3126 bytes icons/node-place.png | Bin 0 -> 3126 bytes main.lua | 16 +++--- map/ai.lua | 12 +++++ map/bmp.lua | 77 ++++++++++++++++++---------- map/cities.lua | 58 ++++++++++++++------- map/map.lua | 14 ++++-- ui/button.lua | 3 +- ui/loadmodal.lua | 4 +- ui/menu/ainodes.lua | 45 +++++++++++++++++ ui/menu/cities.lua | 109 +++++++++++++++++++++++++++++++--------- ui/menu/lines.lua | 3 ++ ui/menu/mainmenu.lua | 37 ++++++++------ ui/menu/territory.lua | 3 ++ ui/menu/travelnodes.lua | 3 ++ ui/modal.lua | 9 +++- 20 files changed, 293 insertions(+), 100 deletions(-) create mode 100644 icons/city-delete.png create mode 100644 icons/city-move.png create mode 100644 icons/city-new.png create mode 100644 icons/node-attack.png create mode 100644 icons/node-place.png diff --git a/icons/check.png b/icons/check.png index eabbeadd10fc880281b1cfe7faf5a05045be729a..30dad7e030420712965a85fd629ea86b28777a70 100644 GIT binary patch literal 3126 zcmeH_%?*Gs2!-|T03JO$fdhDV|8exi8bT4NYu7ugq|<#o53e=Wd8>V$ z_i4CPRcrpJHXj)?^5rt+5v>2k$w8l^(3e6B`F+`zhjiE#onjz3)h(gmELy0IfL!iYFg{C!z z5>aS|K#L)&nfYl!lvI6;>1s;*b3=DinK$vl=HlH+5gN&z(V@QPi(dmJF%?3Qqx9|Qxm*d9LI%&&B z$6LP>eMaUX0xG`kv1eQ%ku1#VQQ#S}raVWJ= z4irl$4~vZ#N;uu?m8aRD_$pttNV{`oL#rrfyxn9E)?)(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jLNxRrbNs2IP4sP zfWu!7acjZ4t;u6f0KFB%5t2Z$AUPKB%JnTj@4bG#r1~;Lw&aJHE&G2cR9riA`T~L= z&?Ry%R4CZJO0CY)C$J2%8b%lmy_Gp!6^;K)qD6#ioT-K$7?i^Ek_t$*-l#n(4jgz0 z!Vw6J_{lR@MN*SnpeBkGqt~QeIfAG2!nY=zJLvPf0ZIBC;PQpi^oz=Sj~?IdD{JiM z*Y?xudS%A0yO#=#Y?YI(!f8st(*`9vjIAO$N*7=6iatnPA9ZZU?xeVLY`$ikUFKGq zfyC(HDV#Uo5VIw(Okh25^kRHse0@8w{y7gKsO}e3J`;)R>M8W+ zyh=dVAq;5pYMiA890;Y-FV*`MRenW{LspIQ^=O#}@Tx#bT_Ak<$&irg|9CY-Aiua| zZrsLIse8T==Uy_(Wy%U8=T?WbszYq)D}{%F+8(jCwK^d~a)miiD90FTz)+D)4FL_X z{8sRXee9Q~tcrF1df+J4jWV>z>Nwie(C8OlTCgE%m$p%qbIzWfGEBYnEdM;i zSYBwXzfsjDG_;Bz^a$E)^wv?ixl?{9Jv||_`nm|EDFCekXKHb-5fd7`Qnlyod3nf- zQ$s?gyz$}u55sn`b)Z1$etpiK$ZeE;XP9w`zp=7VInAE5_wdl;QhPUTbb?jP{4O_t z*WLD_4hN^9n{9f`);y5sNz56nkzyU7sR+6nvheYYfdb)3LZa%OX%I>xs#cl$GKsno@6>(Ip}Dfdpy_gE&>6%rRt6 z+NT{U#m}x+_-I-JE>Wx^^5tXVN+45%3f+XZ#-lQLQfY1M>OiU54fl?GiEnqRMXIa$ EKMi6p2mk;8 literal 0 HcmV?d00001 diff --git a/icons/city-new.png b/icons/city-new.png new file mode 100644 index 0000000000000000000000000000000000000000..8dafd621db042ae8b0a4db88fda0313161cd1fa4 GIT binary patch literal 357 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz#^NA%Cx&(BWL^R}Ea{HEjtmSN z`?>!lvI6;>1s;*b3=DinK$vl=HlH+5@Rz5HV@QPi(dh^I4lD3DC;tC`{&tM%R5s2d zZDsE_$sVX%B75o8Z||SV>purCyH-_qIYMZ|CsEGG1-hZCDvc|ACN5x2Q(eQNI)OJ` zCM+9{2Xj6Piv@)+z8Ecsa$v9UD4swZ&}`Hncw;| zf4YIz{5{K^(`H_0d1qJI{Z*F9f4cMwTb3}lJxo>W-9Kz;`C4k8_^q(w(5ZXx(qFK? zUvl<6%Yj&-xOLhLW!^583mI;x8lLj}s~@pbJ?-ORPKj44M!$oY8m69D=g-}cqoM50 z*$`=Atr^AOqMBnlfyE(u*%anThKQ9>abBA8w~v-~4)z4*}Q$iB}k9&#( literal 0 HcmV?d00001 diff --git a/icons/node-attack.png b/icons/node-attack.png new file mode 100644 index 0000000000000000000000000000000000000000..eb42d6f4668781d93e7814af76938560c527b591 GIT binary patch literal 3126 zcmeH|F%Ezr5CpOJ0hX4&zz5j-|L5Qdgb?5(uovtga+n_qn>zP8kpBd+LkZ zWF{>=kBf5j%odH94t^L5YD)jWhxr%sA=JcjP&^~HMfv9C{FB@eh)Sms0#TV3k^shn z$_#1-Wj_OWeo(Q~)Jg=RqHeT{2t>tBQ!6WAwN$sXiWRe3w$jvkVPRf;|3?giKu5yMMT*E4aTXk07{FSQ(?{V-1!;E(icV4A)Bl literal 0 HcmV?d00001 diff --git a/main.lua b/main.lua index 0451efc..dbf65ef 100644 --- a/main.lua +++ b/main.lua @@ -64,12 +64,14 @@ end function love.mousemoved( x, y, dx, dy, istouch ) 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 ) ) + if y < mainmenu.menuHeight then + button.selectIn( x, y ) + --mouse on map + else + if map.selectionLocked then return end + if map.editLayer and map.editLayer.selectNearest then + map.selected = map.editLayer:selectNearest( Camera.GetWorldCoordinate( x, y ) ) + end end end @@ -87,5 +89,5 @@ function love.keypressed(key, code, isRepeat) end function love.textinput() - + end diff --git a/map/ai.lua b/map/ai.lua index 43bb5d2..ecedeb8 100644 --- a/map/ai.lua +++ b/map/ai.lua @@ -17,6 +17,14 @@ function aiNode:formatDisplayInfo() ]]):format( self.idx, self.x, self.y, tostring(self.attacking) ) end +function aiNode:add() + +end + +function aiNode:moveTo( x, y ) + +end + function t.load( filename ) local img, imgd = bmp.load( filename ) local nodes = { @@ -82,4 +90,8 @@ function t.save( nodes ) return bmp.ai( nodes.all ) end +function t.newNode( isAttacking ) + +end + return t \ No newline at end of file diff --git a/map/bmp.lua b/map/bmp.lua index 103c827..0db1ba7 100644 --- a/map/bmp.lua +++ b/map/bmp.lua @@ -244,40 +244,63 @@ function formats.sailable:test() print "sailable OK" end -function formats.sailable:encode( data ) - local w, h = self.w, self.h - local bytes = { self.header:sub( 1, -2 ) } - local i = 2 - --y coordinates are written top to bottom - for y = h - 1, 0, -1 do - for x = 0, w - 1 do - bytes[i] = assert( self.palette[ math.floor( data:getPixel(x, y) * 255 )] ) - i = i + 1 - end +do --sailable + + local reversePalette = {} + for eight, four in pairs( formats.sailable.palette ) do + reversePalette[ four ] = eight + end + + --take the red channel in float [0, 1] format + --expand it to a byte, then quantize it to 4 bits + --according to the sailable palette + local function sailableQuantize( r ) + if r == 0 then return 0 end + if r >= 1 then return 15 end + r = math.floor( r * 255 ) + + for four = 1, #reversePalette do + if reversePalette[ four ] > r then return four end + end + + error( "Could not quantize sailable.bmp!" ) end - --fold into 4-bit pixel array - local nybbles = { bytes[1] } - for j = 2, #bytes / 2 do - local a, b = bytes[ 2 * j ], bytes[ 2 * j + 1 ] - nybbles[j] = string.char( 16 * a + b ) + function formats.sailable:encode( data ) + local w, h = self.w, self.h + local bytes = { self.header:sub( 1, -2 ) } + local i = 2 + + --y coordinates are written top to bottom + for y = h - 1, 0, -1 do + for x = 0, w - 1 do + bytes[i] = sailableQuantize( data:getPixel(x, y) ) + i = i + 1 + end + end + + --fold into 4-bit pixel array + local nybbles = { bytes[1] } + for j = 2, #bytes / 2 do + local a, b = bytes[ 2 * j ], bytes[ 2 * j + 1 ] + nybbles[j] = string.char( 16 * a + b ) + end + + return table.concat( nybbles ) end - return table.concat( nybbles ) + function formats.africa:test() + print "testing africa" + local filename = "data/earth/africa.bmp" + local img, imgd = test.load( filename ) + local encoded = self:encode( imgd ) + love.filesystem.write( "africa_out.bmp", encoded ) + test.compareData( love.filesystem.read( filename ), encoded ) + print "africa OK" + end end -function formats.africa:test() - print "testing africa" - local filename = "data/earth/africa.bmp" - local img, imgd = test.load( filename ) - local encoded = self:encode( imgd ) - love.filesystem.write( "africa_out.bmp", encoded ) - test.compareData( love.filesystem.read( filename ), encoded ) - print "africa OK" -end - - function formats.africa:encode( data ) local w, h = self.w, self.h local bytes = { self.header:sub( 1, -3 ) } diff --git a/map/cities.lua b/map/cities.lua index e8d2eba..fbfda65 100644 --- a/map/cities.lua +++ b/map/cities.lua @@ -13,6 +13,7 @@ local caps = {} t.selected = nil t.selectionLocked = false +local invisible = 10000 --sentinel value outside the draw rectangle function t.lockSelection() t.selectionLocked = true @@ -54,14 +55,21 @@ function city:formatDisplayInfo() end function city:delete() - + print( "deleting city:", self.name ) + self.deleted = true + if self.capital then + caps[ self.caps ] = invisible + caps[ self.caps + 1] = invisible + end + points[ self.points ] = invisible + points[ self.points + 1] = invisible end function city:add() local n = #cities + 1 cities[ n ] = self self.n = n - + local idxPoints = #points + 1 self.points = idxPoints points[ idxPoints ], points[ idxPoints + 1 ] = self.x, self.y @@ -81,22 +89,32 @@ function city:moveTo(x, y) end function city:toggleCapital() - self.capital = not( self.capital ) + if self.capital then + self.capital = false + caps[ self.caps ] = invisible + caps[ self.caps + 1 ] = invisible + else + self.capital = true + local idx = #caps + 1 + caps[ idx ] = self.x + caps[ idx + 1 ] = self.y + self.caps = idx + end end function t.newCity( tbl ) return setmetatable({ - name = "", - country = "", - x = 0, - y = 0, - pop = 0, - capital = false, + name = "", + country = "", + x = 0, + y = 0, + pop = 0, + capital = false, }, citymt ) end function t.load( filename ) - + print( "=== LOADING CITIES. ===" ) cities = { visible = true, active = false, filename = filename } @@ -112,11 +130,11 @@ function t.load( filename ) if capital then --check against empty or malformed line x, y, pop, capital = tonumber( x ), tonumber( y ), tonumber( pop ), ( tonumber( capital ) > 0) local city = setmetatable({ - name = line:sub( 1, 39 ):gsub("%s+$",""), - country = line:sub( 42, 82 ):gsub("%s+$",""), - x = x, y = y, pop = pop, capital = capital, - n = n, points = idxPts, caps = capital and idxCaps - }, citymt ) + name = line:sub( 1, 39 ):gsub("%s+$",""), + country = line:sub( 42, 82 ):gsub("%s+$",""), + x = x, y = y, pop = pop, capital = capital, + n = n, points = idxPts, caps = capital and idxCaps + }, citymt ) cities[n] = city n = n + 1 @@ -142,9 +160,13 @@ end function t.save( cities ) local str = {} - for n, city in ipairs( cities ) do - str[n] = ("%-41s%-41s%-14f%-14f%-19d %d"):format( - city.name, city.country, city.x, city.y, city.pop, city.capital and 1 or 0 ) + local i = 1 + for _, city in ipairs( cities ) do + if not city.deleted then + str[i] = ("%-41s%-41s%-14f%-14f%-19d %d"):format( + city.name, city.country, city.x, city.y, city.pop, city.capital and 1 or 0 ) + i = i + 1 + end end return assert(table.concat( str, "\n" )) end diff --git a/map/map.lua b/map/map.lua index 4dd42ca..81c8d6c 100644 --- a/map/map.lua +++ b/map/map.lua @@ -191,18 +191,24 @@ local function write( filename, string ) end function map.save() - --should be cross platform-ish + --should be cross platform-ish. + --race condition, unfortunately. + --maybe we should do this on part on load, then keep a lockfile open in each of these folders for _, folder in ipairs{ "/data/", "/data/earth/", "/data/graphics/" } do - --getInfo checks if a directory exists assert( mkdir.exists( map.path ), map.path ) local path = map.path..folder if not mkdir.exists( path ) then mkdir.mkdir( path ) end end - --OK back to normal + local files = {} + --Write everything to strings first, in case there are errors we don't want to half-write the map for k, layer in pairs( layers ) do - write( map.path..tostring( layer.filename ), assert( layer:save() ) ) + files[ map.path..tostring( layer.filename ) ] = assert( layer:save() ) end + for filename, str in pairs( files ) do + write( filename, str ) + end + end function map.hover(x, y) diff --git a/ui/button.lua b/ui/button.lua index 5c2385c..b3f3273 100644 --- a/ui/button.lua +++ b/ui/button.lua @@ -11,6 +11,7 @@ local t = { h = 24, group = false, visible = false, + align = "center", callback = function( self ) return print( "clicked button: ", self.name, self.x, self.y, self.w, self.h, self.visible ) end } t.selected, t.next, t.prev = t, t, t @@ -45,7 +46,7 @@ function t.draw( b ) b.x + (b.icon and b.h or 0), b.y + 0.5 * ( b.h - lg.getFont():getHeight() ), b.w - (b.icon and b.h or 0), - "center" ) + b.align ) if b.icon then local h = b.icon:getHeight() lg.draw( b.icon, diff --git a/ui/loadmodal.lua b/ui/loadmodal.lua index 9f95f7f..be75b8b 100644 --- a/ui/loadmodal.lua +++ b/ui/loadmodal.lua @@ -40,8 +40,8 @@ function t.start() modal.start( t ) loadLocation = loadLocation or map.path button.selected = loadButton - loadButton.name = "save to "..loadLocation - button.displayGroup( t, true ) + loadButton.name = "load from "..loadLocation + button.displayGroup( t, false, true ) end function t.draw() diff --git a/ui/menu/ainodes.lua b/ui/menu/ainodes.lua index e69de29..b3ac15b 100644 --- a/ui/menu/ainodes.lua +++ b/ui/menu/ainodes.lua @@ -0,0 +1,45 @@ +local t = {} +local lg = assert( love ).graphics +local modal = require 'ui.modal' +local button = require 'ui.button' +local camera = require 'ui.camera' +local map = require 'map.map' + +local node +local moveModal = modal.new{} +local selectModal = modal.new{} + +button.new{ name = "ATTACK NODE", + group = t, + icon = lg.newImage("icons/node-attack.png"), + x = 615, + y = 0, + callback = function() + node = map.ainodes.newNode( true ) + return moveModal:start() + end +} + +button.new{ name = "PLACEMENT NODE", + group = t, + icon = lg.newImage("icons/node-place.png"), + x = 615, + y = 1 * (4 + button.h), + callback = function() + node = map.ainodes.newNode( true ) + return moveModal:start() + end +} + +button.new{ name = "MOVE NODE", + group = t, + y = 2 * (4 + button.h), + x = 615, +} + +button.new{ name = "DELETE NODE", + group = t, + y = 3 * (4 + button.h), + x = 615, +} +return t \ No newline at end of file diff --git a/ui/menu/cities.lua b/ui/menu/cities.lua index 9e1c02e..d8023cb 100644 --- a/ui/menu/cities.lua +++ b/ui/menu/cities.lua @@ -22,27 +22,29 @@ end --and clicking escape will clear the previously selected mode local selectModal = modal.new{} local textModal = modal.new{} -local numberModal = modal.new{ cursor = 1 } +local numberModal = modal.new{} local editModal = modal.new{ mousepressed = button.mousepressed, keypressed = keypressed, } local moveModal = modal.new{ mousepressed = button.mousepressed, keypressed = keypressed, mousemoved = button.selectIn } -local deleteModal = modal.new{ mousepressed = button.mousepressed, keypressed = keypressed } +local deleteModal = modal.new{ keypressed = keypressed } local currentMode button.new{ name = "NEW CITY", group = t, - icon = lg.newImage("icons/layer-cities.png"), + icon = lg.newImage("icons/city-new.png"), x = 615, y = 0, callback = function() city = map.cities.newCity() + city:add() return editModal:start() end } moveModal.button = button.new{ name = "MOVE CITY", group = t, + icon = lg.newImage("icons/city-move.png"), x = 615, - y = 28, + y = button.h + 4, callback = function( self ) self.lit = true selectModal.mode = moveModal @@ -53,7 +55,7 @@ moveModal.button = button.new{ name = "MOVE CITY", editModal.button = button.new{ name = "EDIT CITY", group = t, x = 615, - y = 28 * 2, + y = (button.h + 4) * 2, callback = function( self ) self.lit = true selectModal.mode = editModal @@ -63,48 +65,57 @@ editModal.button = button.new{ name = "EDIT CITY", deleteModal.button = button.new{ name = "DELETE CITY", group = t, + icon = lg.newImage("icons/city-delete.png"), x = 615, - y = 28 * 3, - icon = lg.newImage("icons/x.png"), + y = (button.h + 4) * 3, callback = function( self ) self.lit = true - selectModal.mode = deleteModal - return selectModal:start() + return deleteModal:start() end, } --editButtons local function editText( self ) print( "editing: ", self.field, city.name ) + self.lit = true return textModal:start( self.field ) end local function editNumber( self ) - local field = self.field + print( "editing: ", self.field, city.name ) + self.lit = true + return numberModal:start( self.field ) end local editButtons = { save = button.new{ - icon = lg.newImage( "icons/save.png" ), - callback = function() print( "stop editing city" ) return editModal:stop() end,}, + icon = lg.newImage( "icons/check.png" ), + callback = function() return editModal:stop() end,}, name = button.new{ callback = editText }, country = button.new{ callback = editText }, x = button.new{ callback = editNumber }, y = button.new{ callback = editNumber }, - capital = button.new{ callback = function() if city then return city:toggleCapital() end end }, + capital = button.new{ callback = function( self ) + if city then + self.name = tostring( not( city.capital ) ) + return city:toggleCapital() + end + end }, } do local i = 0 - for key, b in pairs( editButtons ) do + for _, key in ipairs{ "save", "name", "country", "x", "y", "capital" } do + local b = assert( editButtons[ key ] ) + b.align = "right" b.field = key - b.name = key + b.name = tostring( key ) --bools must be cast to string before getting passed to printf b.group = editModal - b.x = 0 - b.y = 28 * i + b.x = lg.getWidth() / 2 - button.w / 2 + b.y = (button.h + 4) * i i = i + 1 end end @@ -113,17 +124,34 @@ function editModal:start() modal.start( self ) button.displayGroup( self, false, true ) for k, b in pairs( editButtons ) do - b.name = city[k] or b.name + b.name = tostring( city[k] or b.name ) end + if city.capital == false then editButtons.capital.name = "false" end +end + +function editModal:stop() + return modal.stop( self ) end function editModal.draw() + lg.setColor( 1, 1, 1, 0.5 ) return button:draw() end +function editModal.resize( w, h ) + local i = 0 + local high = h / 6 + for key, b in pairs( editButtons ) do + b.x = w / 2 - button.w / 2 + b.y = ( high + 4 ) * i + b.h = high + i = i + 1 + end +end + function moveModal.update( dt ) local x, y = love.mouse.getPosition() - if y > 200 and love.mouse.isDown( 1 ) then + if y > t.menuHeight and love.mouse.isDown( 1 ) then local wx, wy = camera.GetWorldCoordinate( x, y ) city:moveTo( wx, wy ) end @@ -134,7 +162,7 @@ function moveModal.mousemoved( x, y, dx, dy, istouch ) end function moveModal.mousepressed( x, y, mouseButton, istouch, presses ) - if y < 200 then + if y < t.menuHeight then moveModal:stop() return button.mousepressed( x, y, mouseButton, istouch, presses ) end @@ -143,19 +171,48 @@ function moveModal.mousepressed( x, y, mouseButton, istouch, presses ) end end +function deleteModal.mousepressed( x, y, mouseButton, istouch, presses ) + if map.selected then + map.selected:delete() + end + if y < t.menuHeight then + deleteModal.button.lit = false + deleteModal:stop() + return button.mousepressed( x, y, mouseButton, istouch, presses ) + end +end + + function numberModal:start( field ) self.field = field return modal.start( self ) end function numberModal.keypressed( key, code, isrepeat ) - if code == 'backspace' then end - if code == 'escape' then return numberModal:stop() end + if code == "backspace" then + local text = tostring( city[numberModal.field] ) + -- get the byte offset to the last UTF-8 character in the string. + local byteoffset = utf8.offset(text, -1) + print( "textmodal: backspace", byteoffset ) + + 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). + local newstr = text:sub( 1, byteoffset - 1) + if newstr == "" then newstr = 0 end + city[numberModal.field] = tonumber( newstr ) or city[numberModal.field] + editButtons[numberModal.field].name = city[numberModal.field] + end + end + if code == "escape" or code == "return" then + return numberModal:stop() + end end function numberModal.textinput( char ) local str = tostring( city[ numberModal.field ] ) local plus = str..char + print( "text input: ", char ) if tonumber( plus ) then city[ numberModal.field ] = plus editButtons[ numberModal.field ].name = plus @@ -196,7 +253,7 @@ function textModal.keypressed( key, code, isRepeat ) editButtons[textModal.field].name = city[textModal.field] end end - if code == "escape" then + if code == "escape" or code == "return" then return textModal:stop() end end @@ -215,7 +272,7 @@ function selectModal:stop() end function selectModal.mousepressed( x, y, mouseButton, istouch, presses ) - if y < 200 then + if y < t.menuHeight then selectModal:stop() return button.mousepressed( x, y, mouseButton, istouch, presses ) end @@ -226,6 +283,8 @@ function selectModal.mousepressed( x, y, mouseButton, istouch, presses ) end end - +function t.setMenuHeight( h ) + t.menuHeight = h +end return t \ No newline at end of file diff --git a/ui/menu/lines.lua b/ui/menu/lines.lua index e69de29..b8617ae 100644 --- a/ui/menu/lines.lua +++ b/ui/menu/lines.lua @@ -0,0 +1,3 @@ +local t = {} + +return t \ No newline at end of file diff --git a/ui/menu/mainmenu.lua b/ui/menu/mainmenu.lua index 7f8aeec..34db08e 100644 --- a/ui/menu/mainmenu.lua +++ b/ui/menu/mainmenu.lua @@ -1,11 +1,10 @@ local love = assert( love ) local button = require 'ui.button' -local savemodal = require 'ui.savemodal' -local loadmodal = require 'ui.loadmodal' -local Camera = require 'ui.camera' +local modal = require 'ui.modal' +local camera = require 'ui.camera' local map = require 'map.map' -local t = {} +local t = { menuHeight = 200 } local loadImg = love.graphics.newImage local layers = { @@ -27,12 +26,12 @@ local layers = { button.new{ name = "LOAD", x = 250, y = 0, group = t, - callback = loadmodal.start, + callback = require( 'ui.loadmodal' ).start, icon = love.graphics.newImage( "icons/load.png" )} button.new{ name = "SAVE", x = 250, y = 28, group = t, - callback = savemodal.start, + callback = require( 'ui.savemodal' ).start, icon = love.graphics.newImage( "icons/save.png" )} button.new{ name = "UNDO", x = 250, y = 2 * 28, @@ -61,9 +60,13 @@ end local activeLayerButton local function back( self ) - activeLayerButton.lit = false + print( "back button clicked" ) + if activeLayerButton then + activeLayerButton.lit = false + end activeLayerButton = nil map.setEditLayer() + modal.exitAll() button.displayGroup( t, false, true ) for i, b in ipairs( editButtons ) do b.visible = true @@ -86,6 +89,7 @@ local backButton = button.new{ } local function editLayer( self ) + back( backButton ) self.lit = true map.setEditLayer( self.layer ) activeLayerButton = self @@ -106,6 +110,8 @@ end local x = 250 for i = 1, #layers do + layers[i].menu.menuHeight = t.menuHeight + editButtons[i] = button.new( copy( i, { y = 3 * 28, x = x + (button.h + 4) * ( i - 1 ), @@ -126,17 +132,20 @@ for i = 1, #layers do group = showButtons, tooltip = "show "..layers[i].layer })) + layerButtons[ 2 * i - 1 ] = showButtons[i] + + end function t.draw() --Status bar. - love.graphics.setScissor( 0, 0, 250, 200 ) + love.graphics.setScissor( 0, 0, 250, t.menuHeight ) local x, y = love.mouse.getPosition() - local wx, wy = Camera.GetWorldCoordinate( x, y ) - local bx, by = Camera.GetBitmapCoordinate( x, y ) + local wx, wy = camera.GetWorldCoordinate( x, y ) + local bx, by = camera.GetBitmapCoordinate( x, y ) local h = love.graphics.getHeight() - 60 love.graphics.setColor( 0, 0, 0, 1 ) love.graphics.rectangle( "fill", 0, 0, 250, love.graphics.getHeight() ) @@ -156,10 +165,10 @@ function t.draw() if map.selected then love.graphics.print( map.selected:formatDisplayInfo(), 0, 80 ) end if map.selectionLocked then end - love.graphics.setScissor( 250, 0, love.graphics.getWidth() - 250, 200 ) - love.graphics.rectangle( "line", 0, 0 , 250, 200 ) - love.graphics.rectangle( "line", 250, 0, love.graphics.getWidth() - 250, 200 ) - love.graphics.rectangle( "line", 250, 0, button.w, 200 ) + love.graphics.setScissor( 250, 0, love.graphics.getWidth() - 250, t.menuHeight) + love.graphics.rectangle( "line", 0, 0 , 250, t.menuHeight ) + love.graphics.rectangle( "line", 250, 0, love.graphics.getWidth() - 250, t.menuHeight ) + love.graphics.rectangle( "line", 250, 0, button.w, t.menuHeight ) love.graphics.setColor( 1, 1, 1, 0.8 ) diff --git a/ui/menu/territory.lua b/ui/menu/territory.lua index e69de29..b8617ae 100644 --- a/ui/menu/territory.lua +++ b/ui/menu/territory.lua @@ -0,0 +1,3 @@ +local t = {} + +return t \ No newline at end of file diff --git a/ui/menu/travelnodes.lua b/ui/menu/travelnodes.lua index e69de29..b8617ae 100644 --- a/ui/menu/travelnodes.lua +++ b/ui/menu/travelnodes.lua @@ -0,0 +1,3 @@ +local t = {} + +return t \ No newline at end of file diff --git a/ui/modal.lua b/ui/modal.lua index 57c7219..18ea5b9 100644 --- a/ui/modal.lua +++ b/ui/modal.lua @@ -34,7 +34,6 @@ function t.start( self ) end function t.stop( self ) - print( "stopping modal:", i ) --restore callbacks for name in pairs( self ) do if love[name] then @@ -56,7 +55,13 @@ function t.stop( self ) i = i - 1 t.previous = t[i - 1] - love.graphics.setScissor(0, 0, love.graphics.getDimensions()) + love.graphics.setScissor(0, 0, love.graphics.getDimensions()) +end + +function t.exitAll() + if i < 1 then return end + i = 1 + return t.stop( love ) end function t.new( modal )