sokoban.love/main.lua

267 lines
8.2 KiB
Lua

utf8 = require 'utf8'
json = require 'json'
require 'app'
require 'test'
require 'keychord'
require 'button'
require 'main_tests'
-- delegate most business logic to a layer that can be reused by other projects
require 'edit'
Editor_state = {}
function App.version_check()
Mode = 'version_check'
Supported_versions = {'11.4', '11.3', '11.2', '11.1', '11.0'} -- keep these sorted in descending order
local major, minor = love.getVersion()
Version = major..'.'..minor
if array.find(Supported_versions, Version) then
Mode = 'run'
end
end
-- called both in tests and real run
function App.initialize_globals()
-- tests currently mostly clear their own state
-- blinking cursor
Cursor_time = 0
-- for hysteresis in a few places
Current_time = 0
Last_focus_time = 0 -- https://love2d.org/forums/viewtopic.php?p=249700
Last_resize_time = 0
end
-- called only for real run
function App.initialize(arg)
love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
love.keyboard.setKeyRepeat(true)
love.graphics.setBackgroundColor(1,1,1)
if love.filesystem.getInfo('config') then
load_settings()
else
initialize_default_settings()
end
if #arg > 0 then
Editor_state.filename = arg[1]
load_from_disk(Editor_state)
Text.redraw_all(Editor_state)
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.cursor1 = {line=1, pos=1}
else
load_from_disk(Editor_state)
Text.redraw_all(Editor_state)
end
edit.check_locs(Editor_state)
-- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708
love.window.setTitle('text.love - '..Editor_state.filename)
if #arg > 1 then
print('ignoring commandline args after '..arg[1])
end
if rawget(_G, 'jit') then
jit.off()
jit.flush()
end
end
function print_and_log(s)
print(s)
log(3, s)
end
function load_settings()
local settings = json.decode(love.filesystem.read('config'))
love.graphics.setFont(love.graphics.newFont(settings.font_height))
-- set up desired window dimensions and make window resizable
_, _, App.screen.flags = App.screen.size()
App.screen.flags.resizable = true
App.screen.width, App.screen.height = settings.width, settings.height
App.screen.resize(App.screen.width, App.screen.height, App.screen.flags)
set_window_position_from_settings(settings)
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, settings.font_height, math.floor(settings.font_height*1.3))
Editor_state.filename = settings.filename
Editor_state.screen_top1 = settings.screen_top
Editor_state.cursor1 = settings.cursor
end
function set_window_position_from_settings(settings)
local os = love.system.getOS()
if os == 'Linux' then
-- love.window.setPosition doesn't quite seem to do what is asked of it on Linux.
App.screen.move(settings.x, settings.y-37, settings.displayindex)
else
App.screen.move(settings.x, settings.y, settings.displayindex)
end
end
function initialize_default_settings()
local font_height = 20
love.graphics.setFont(love.graphics.newFont(font_height))
initialize_window_geometry()
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right)
Editor_state.font_height = font_height
Editor_state.line_height = math.floor(font_height*1.3)
end
function initialize_window_geometry()
-- Initialize window width/height and make window resizable.
--
-- I get tempted to have opinions about window dimensions here, but they're
-- non-portable:
-- - maximizing doesn't work on mobile and messes things up
-- - maximizing keeps the title bar on screen in Linux, but off screen on
-- Windows. And there's no way to get the height of the title bar.
-- It seems more robust to just follow LÖVE's default window size until
-- someone overrides it.
App.screen.width, App.screen.height, App.screen.flags = App.screen.size()
App.screen.flags.resizable = true
App.screen.resize(App.screen.width, App.screen.height, App.screen.flags)
end
function App.resize(w, h)
--? print(("Window resized to width: %d and height: %d."):format(w, h))
App.screen.width, App.screen.height = w, h
Text.redraw_all(Editor_state)
Editor_state.selection1 = {} -- no support for shift drag while we're resizing
Editor_state.right = App.screen.width-Margin_right
Editor_state.width = Editor_state.right-Editor_state.left
Text.tweak_screen_top_and_cursor(Editor_state, Editor_state.left, Editor_state.right)
Last_resize_time = Current_time
end
function App.filedropped(file)
-- first make sure to save edits on any existing file
if Editor_state.next_save then
save_to_disk(Editor_state)
end
-- clear the slate for the new file
App.initialize_globals()
Editor_state.filename = file:getFilename()
file:open('r')
Editor_state.lines = load_from_file(file)
file:close()
Text.redraw_all(Editor_state)
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.cursor1 = {line=1, pos=1}
-- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708
love.window.setTitle('text.love - '..Editor_state.filename)
end
function App.draw()
if Mode == 'version_check' then
love.graphics.setColor(1,1,0)
love.graphics.rectangle('fill', 30,30, 400,400)
love.graphics.setColor(0,0,0)
love.graphics.printf(("This app doesn't support version %s; please use version %s. Press any key to try it with this version anyway."):format(Version, Supported_versions[1]), 40,40, 400)
return
end
edit.draw(Editor_state)
end
function App.update(dt)
if Mode == 'version_check' then return end
Current_time = Current_time + dt
-- some hysteresis while resizing
if Current_time < Last_resize_time + 0.1 then
return
end
Cursor_time = Cursor_time + dt
edit.update(Editor_state, dt)
end
function love.quit()
if Mode == 'version_check' then return end
edit.quit(Editor_state)
-- save some important settings
local x,y,displayindex = App.screen.position()
local filename = Editor_state.filename
if is_relative_path(filename) then
filename = love.filesystem.getWorkingDirectory()..'/'..filename -- '/' should work even on Windows
end
local settings = {
x=x, y=y, displayindex=displayindex,
width=App.screen.width, height=App.screen.height,
font_height=Editor_state.font_height,
filename=filename,
screen_top=Editor_state.screen_top1, cursor=Editor_state.cursor1}
love.filesystem.write('config', json.encode(settings))
end
function App.mousepressed(x,y, mouse_button)
if Mode == 'version_check' then return end
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
return edit.mouse_press(Editor_state, x,y, mouse_button)
end
function App.mousereleased(x,y, mouse_button)
if Mode == 'version_check' then return end
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
return edit.mouse_release(Editor_state, x,y, mouse_button)
end
function App.wheelmoved(dx,dy)
if Mode == 'version_check' then return end
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
return edit.mouse_wheel_move(Editor_state, dx,dy)
end
function App.focus(in_focus)
if Mode == 'version_check' then return end
if in_focus then
Last_focus_time = Current_time
end
end
function App.keychord_press(chord, key)
if Mode == 'version_check' then return end
-- ignore events for some time after window in focus (mostly alt-tab)
if Current_time < Last_focus_time + 0.01 then
return
end
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
return edit.keychord_press(Editor_state, chord, key)
end
function App.textinput(t)
if Mode == 'version_check' then return end
-- ignore events for some time after window in focus (mostly alt-tab)
if Current_time < Last_focus_time + 0.01 then
return
end
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
return edit.text_input(Editor_state, t)
end
function App.keyreleased(key, scancode)
if Mode == 'version_check' then
Mode = 'run'
return
end
-- ignore events for some time after window in focus (mostly alt-tab)
if Current_time < Last_focus_time + 0.01 then
return
end
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
return edit.key_release(Editor_state, key, scancode)
end