diff --git a/button.lua b/button.lua index adee847..603ff2d 100644 --- a/button.lua +++ b/button.lua @@ -3,7 +3,7 @@ local lg = love.graphics local t = { name = "", tooltip = "button", - icon = lg.newImage( "icons/eye.bmp" ), + icon = false, x = 8, y = 250, w = 176, @@ -19,12 +19,35 @@ function t.contains( button, x, y ) and y < button.y + button.h and y > button.y end +local function debugLoop() + local i = 0 + local j = 0 + local a = t + repeat + i = i + 1 + --print( "BUTTON", i, tostring( a ) ) + a = a.next + until a == t or i > 100 + a = t + repeat + j = j + 1 + --print( "BUTTON", i, tostring( a ) ) + a = a.prev + until a == t or j > 100 + print( i, j, "BUTTONS" ) +end + +local k = 1 function t.new( b ) b = setmetatable( b or {}, t ) b.next = t t.prev.next = b b.prev = t.prev t.prev = b + --nonsense + k = k + 1 + print( "ADD BUTTON", k, tostring( b ) ) + debugLoop() return b end @@ -38,12 +61,14 @@ function t.draw( b ) lg.printf( b.name, b.x, b.y + 0.5 * ( b.h- lg.getFont():getHeight() ), - b.w - 5, - "right" ) - if b.icon then lg.draw( b.icon, - b.x, b.y + 0.5 * (b.h - b.icon:getHeight()), + b.w, + "center" ) + if b.icon then + local h = b.icon:getHeight() + lg.draw( b.icon, + b.x, b.y, 0, - 0.5, 0.5 ) + b.h / h ) end if t.selected == b then lg.rectangle( "fill", b.x, b.y, b.w, b.h, 6 ) @@ -57,11 +82,11 @@ function t.select( b ) end function t.selectNext() - repeat t.selected = t.selected.next until t.selected.visible + repeat t.selected = t.selected.next until (t.selected == t) or t.selected.visible end function t.selectPrev() - repeat t.selected = t.selected.prev until t.selected.visible + repeat t.selected = t.selected.prev until (t.selected == t) or t.selected.visible end function t.selectIn( x, y ) @@ -70,13 +95,15 @@ function t.selectIn( x, y ) end function t.selectNextInGroup() - local group = t.selected and t.selected.group + --make sure our group is visible, otherwise the loop doesn't end + local group = t.selected and t.selected.visible 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 + --make sure our group is visible, otherwise the loop doesn't end + local group = t.selected and t.selected.visible and t.selected.group if not group then return t.selectPrev() end repeat t.selectPrev() until group == t.selected.group end diff --git a/camera.lua b/camera.lua index 4f10c8b..3c5c8b4 100644 --- a/camera.lua +++ b/camera.lua @@ -3,7 +3,7 @@ local tfTerritory = love.math.newTransform() local tfNodes = love.math.newTransform() local lg = assert( love.graphics ) local Camera = { - x = 0, y = 0, + x = -90, y = 45, w = 360, h = 200, zoom = 1, tf = tf, tfTerritory = tfTerritory, tfNodes = tfNodes } diff --git a/icons/save.png b/icons/save.png new file mode 100644 index 0000000..6ab57de Binary files /dev/null and b/icons/save.png differ diff --git a/icons/x.png b/icons/x.png new file mode 100644 index 0000000..bedd29c Binary files /dev/null and b/icons/x.png differ diff --git a/main.lua b/main.lua index a51c04e..6a58c70 100644 --- a/main.lua +++ b/main.lua @@ -1,20 +1,14 @@ 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/" +require 'mainmenu' local Camera = require 'camera' function love.load() - - local lfs = assert( love.filesystem ) - lfs.setIdentity( "dcearth", false ) - assert( lfs.createDirectory( SAVEDIRECTORY.."data/earth" )) - assert( lfs.createDirectory( SAVEDIRECTORY.."data/graphics" )) - + love.filesystem.setIdentity( "dcearth", false ) love.keyboard.setKeyRepeat( true ) - love.graphics.setNewFont( 14 )--, "mono" ) + love.graphics.setNewFont( 14 ) end function love.directorydropped( path ) @@ -61,7 +55,7 @@ function love.draw() love.graphics.print(([[ SCREEN %-12d %-12d WORLD %-12.2f%-12.2f - BITMAP %-12.2f%-12.2f + BITMAP %-12d %-12d %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 @@ -84,6 +78,7 @@ function love.mousepressed( x, y, mouseButton, istouch, presses ) 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) ) + button.callback( button.selected ) return button.selected:callback() end end @@ -117,144 +112,5 @@ 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 - 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 - 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 - local soloButtons = {} - local showButtons = {} - local editButtons = {} - for i = 1, #layers do - editButtons[i] = button.new( copy( i, { - x = 8, - y = y + (button.h + 4) * i, - w = 112, - callback = editLayer, - group = "edit", - })) - layerButtons[ 3 * i - 2 ] = editButtons[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] - - soloButtons[i] = button.new( copy( i, { - x = 160, - y = y + (button.h + 4) * i, - w = 24, - name = "S", - callback = soloVisibleLayer, - 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 index e69de29..2c62e6e 100644 --- a/mainmenu.lua +++ b/mainmenu.lua @@ -0,0 +1,143 @@ + +local love = assert( love ) +local button = require 'button' +local savemodal = require 'savemodal' +local map = require 'map' + +button.new{ name = "SAVE", y = 222, callback = savemodal.start, icon = love.graphics.newImage( "icons/save.png" )} +button.new{ name = "UNDO", y = 250, callback = map.undo, icon = love.graphics.newImage( "icons/undo.bmp" ) } + +local tools +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 + +backButton = button.new{ + name = "UP", + visible = false, + y = 250 + button.h + 4, + icon = love.graphics.newImage( "icons/up.bmp" ), + callback = back, +} + +local function toolCallback( self ) + local f = (map.layers[self.layer])[self.name] + if f then return f(self) end +end + +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 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 = "INT", layer = "international"}, + { name = "SAIL", layer = "sailable" }, +} + +local showButtons = {} +local visibilityIcon = love.graphics.newImage( "icons/eye.bmp" ) +local function updateVisibilityIcons() + for i = 1, #showButtons do + showButtons[i].icon = map.layers[ showButtons[i].layer ].visible and visibilityIcon + end +end + +local function toggleVisibleLayer( self ) + if not (self and self.layer) then return end + local ml = map.layers[ self.layer ] + ml.visible = not( ml.visible ) + return updateVisibilityIcons() +end + +local soloIcon = false--love.graphics.newImage( "icons/eye.bmp" ) +local function soloVisibleLayer( self ) + for k, layer in pairs( map.layers ) do + print( "invisible layer, map:", k, layer) + layer.visible = false + end + map.layers[ self.layer ].visible = true + return updateVisibilityIcons() +end + +local backButton +local function editLayer( self ) + map.editLayer = map.layers[ self.layer ] + map.editLayer.visible = true + 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 + print( "EDITING LAYER", self.layer ) + return updateVisibilityIcons() +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 +local soloButtons = {} +local editButtons = {} +for i = 1, #layers do + editButtons[i] = button.new( copy( i, { + x = 8, + y = y + (button.h + 4) * i, + w = 112, + callback = editLayer, + group = "edit", + })) + layerButtons[ 3 * i - 2 ] = editButtons[i] + + showButtons[i] = button.new( copy( i, { + x = 128, + y = y + (button.h + 4) * i, + w = 24, + name = "", + callback = toggleVisibleLayer, + icon = visibilityIcon, + group = "show", + })) + layerButtons[ 3 * i - 1 ] = showButtons[i] + + soloButtons[i] = button.new( copy( i, { + x = 160, + y = y + (button.h + 4) * i, + w = 24, + name = "S", + callback = soloVisibleLayer, + icon = soloIcon, + group = "solo", + })) + layerButtons[ 3 * i ] = soloButtons[i] +end \ No newline at end of file diff --git a/modal.lua b/modal.lua index 552a8c9..4fabd62 100644 --- a/modal.lua +++ b/modal.lua @@ -34,6 +34,7 @@ function t.stop( self ) --restore menus local b = button + button.selected = button repeat b = b.next b.visible = t[i][b] diff --git a/savemodal.lua b/savemodal.lua index 5f54ffc..af01e9b 100644 --- a/savemodal.lua +++ b/savemodal.lua @@ -5,24 +5,30 @@ local map = require( "map" ) local t = {} local saveLocation = map.path +local floppy = love.graphics.newImage( "icons/save.png" ) +floppy:setFilter( "nearest", "nearest" ) local saveButton = button.new{ group = "saveModal", name = "save", callback = function() map.save(); return t:stop() end, visible = false, - x = 20, - y = 20, + icon = floppy, + x = love.graphics.getWidth() / 2 - 200, + y = love.graphics.getHeight() / 2 - 150, w = 400, h = 100, } +local xIcon = love.graphics.newImage( "icons/x.png" ) +xIcon:setFilter( "nearest", "nearest" ) local cancelButton = button.new{ group = "saveModal", name = "cancel", visible = false, + icon = xIcon, callback = function() return t:stop() end, - x = 20, - y = 150, + x = love.graphics.getWidth() / 2 - 200, + y = love.graphics.getHeight() / 2, w = 400, h = 100 } @@ -36,7 +42,7 @@ end function t.draw() love.graphics.clear( 0,0,0,1 ) - love.graphics.setColor( 1, 1, 1, 0.5 ) + love.graphics.setColor( 1, 0, 0, 0.4 ) button:draw() end