dcearth/main.lua

121 lines
3.5 KiB
Lua

local love = assert( love, "This tool requires LOVE: love2d.org" )
--assert( require('mobdebug') ).start() --remote debugger
local map = require 'map'
local button = require 'button'
require 'mainmenu'
local Camera = require 'camera'
function love.load()
love.filesystem.setIdentity( "dcearth", false )
love.keyboard.setKeyRepeat( true )
love.graphics.setNewFont( 14 )
end
function love.directorydropped( path )
if map.path then
assert( love.filesystem.unmount( map.path ) )
map.loaded = false
end
love.filesystem.mount( path, "" )
return map.load( path )
end
function love.update( dt )
local tx, ty = 0, 0
local moveCamera = false
if love.keyboard.isScancodeDown( "w" ) then moveCamera = true; ty = ty + 30 * dt end
if love.keyboard.isScancodeDown( "a" ) then moveCamera = true; tx = tx - 30 * dt end
if love.keyboard.isScancodeDown( "s" ) then moveCamera = true; ty = ty - 30 * dt end
if love.keyboard.isScancodeDown( "d" ) then moveCamera = true; tx = tx + 30 * dt end
if love.keyboard.isScancodeDown( "q" ) then Camera.Zoom( dt * 400 ) end
if love.keyboard.isScancodeDown( "e" ) then Camera.Zoom( -dt* 400 ) end
if moveCamera then Camera.Translate( tx, ty ) end
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, 400, "center")
end
love.graphics.push( "all" )
map.draw()
love.graphics.pop()
--Status bar.
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, 0.9 )
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]]):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
love.graphics.rectangle( "line", 0, 0 , 250, 218 )
love.graphics.rectangle( "line", 0, 218, 250, love.graphics.getHeight() )
love.graphics.setColor( 1, 1, 1, 0.6 )
button:draw()
end
function love.resize(w, h)
Camera.Resize( w, h )
end
function love.wheelmoved(x, y)
Camera.Zoom( (y > 0) and 3 or -3 )
end
function love.mousepressed( x, y, mouseButton, istouch, presses )
local wx, wy = Camera.GetWorldCoordinate( x, y )
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
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 ) )
end
end
function love.keypressed(key, code, isRepeat)
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 == "c" then
map.selectionLocked = not( map.selectionLocked )
end
end
do
end