dcearth/ui/menu/mainmenu.lua

177 lines
5.5 KiB
Lua
Raw Normal View History

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 map = require 'map.map'
2024-07-20 03:09:53 +00:00
local t = {}
local loadImg = love.graphics.newImage
local layers = {
{ name = "AF", layer = "africa" , menu = require 'ui.menu.territory' },
{ name = "EU", layer = "europe" , menu = require 'ui.menu.territory' },
{ name = "NA", layer = "northamerica" , menu = require 'ui.menu.territory' },
{ name = "SA", layer = "southamerica" , menu = require 'ui.menu.territory' },
{ name = "AS", layer = "southasia" , menu = require 'ui.menu.territory' },
{ name = "RU", layer = "russia" , menu = require 'ui.menu.territory' },
{ name = "PATH", layer = "travelnodes" , menu = require 'ui.menu.travelnodes', icon = loadImg( "icons/layer-travelnodes.png" )},
{ name = "AI", layer = "ainodes" , menu = require 'ui.menu.ainodes', icon = loadImg( "icons/layer-ainodes.png" )},
{ name = "CITY", layer = "cities" , menu = require 'ui.menu.cities', icon = loadImg( "icons/layer-cities.png" )},
{ name = "COAST", layer = "coastlines" , menu = require 'ui.menu.lines', icon = loadImg( "icons/layer-coastlines.png" )},
{ name = "LOW", layer = "coastlinesLow", menu = require 'ui.menu.lines', icon = loadImg( "icons/layer-coastlines-low.png" )},
{ name = "INT", layer = "international", menu = require 'ui.menu.lines', icon = loadImg( "icons/layer-international.png" )},
{ name = "SAIL", layer = "sailable" , menu = require 'ui.menu.territory', icon = loadImg( "icons/layer-sailable.png" )},
}
button.new{
name = "LOAD", x = 250, y = 0,
2024-07-20 03:09:53 +00:00
group = t,
callback = loadmodal.start,
icon = love.graphics.newImage( "icons/load.png" )}
button.new{
name = "SAVE", x = 250, y = 28,
2024-07-20 03:09:53 +00:00
group = t,
callback = savemodal.start,
icon = love.graphics.newImage( "icons/save.png" )}
button.new{
name = "UNDO", x = 250, y = 2 * 28,
2024-07-20 03:09:53 +00:00
group = t,
callback = map.undo,
icon = love.graphics.newImage( "icons/undo.bmp" ) }
2024-07-20 03:09:53 +00:00
local editButtons = {}
local layerButtons = {}
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 activeLayerButton
local function back( self )
2024-07-20 03:09:53 +00:00
activeLayerButton.lit = false
activeLayerButton = nil
2024-07-20 03:09:53 +00:00
map.setEditLayer()
button.displayGroup( t, false, true )
for i, b in ipairs( editButtons ) do
b.visible = true
end
for i, b in ipairs( showButtons ) do
b.visible = true
end
self.visible = false
return updateVisibilityIcons()
end
local backButton = button.new{
name = "UP",
visible = false,
y = 5 * 28,
x = 250,
2024-07-20 03:09:53 +00:00
group = false,
icon = love.graphics.newImage( "icons/up.bmp" ),
callback = back,
}
local function editLayer( self )
2024-07-20 03:09:53 +00:00
self.lit = true
map.setEditLayer( self.layer )
activeLayerButton = self
2024-07-20 03:09:53 +00:00
if self.menu then
button.displayGroup( self.menu )
end
backButton.visible = true
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 x = 250
for i = 1, #layers do
editButtons[i] = button.new( copy( i, {
y = 3 * 28,
x = x + (button.h + 4) * ( i - 1 ),
w = 24,
callback = editLayer,
2024-07-20 03:09:53 +00:00
group = editButtons,
tooltip = "edit "..layers[i].layer
}))
layerButtons[ 2 * i - 2 ] = editButtons[i]
showButtons[i] = button.new( copy( i, {
y = 4 * 28,
x = x + (button.h + 4) * ( i - 1 ),
w = 24,
name = "",
callback = toggleVisibleLayer,
icon = visibilityIcon,
2024-07-20 03:09:53 +00:00
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 )
local x, y = love.mouse.getPosition()
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() )
love.graphics.setColor( 1, 1, 1, 1 )
love.graphics.print(([[
SCREEN %-12d %-12d
WORLD %-12.2f%-12.2f
BITMAP %-12d %-12d
%s
%s]]):format(
x, y,
wx, wy,
bx, by,
button.selected and button.selected.tooltip or "",
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
2024-07-20 03:09:53 +00:00
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 )
2024-07-20 03:09:53 +00:00
love.graphics.rectangle( "line", 250, 0, button.w, 200 )
love.graphics.setColor( 1, 1, 1, 0.8 )
button:draw()
love.graphics.setColor( 1, 0, 0, 0.4 )
2024-07-20 03:09:53 +00:00
end
do --button visibility
button.displayGroup( t, false, true )
button.displayGroup( editButtons, false, false )
button.displayGroup( layerButtons, false, false )
button.displayGroup( showButtons, false, false )
end
return t