fix menu bug, integer bitmap coordinates

This commit is contained in:
wan-may 2024-04-28 23:44:18 -03:00
parent 03e45194cf
commit e39e772589
8 changed files with 198 additions and 165 deletions

View File

@ -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

View File

@ -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 }

BIN
icons/save.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
icons/x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

154
main.lua
View File

@ -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

View File

@ -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

View File

@ -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]

View File

@ -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