sokoban.love/main.lua

182 lines
5.1 KiB
Lua
Raw Normal View History

utf8 = require 'utf8'
json = require 'json'
require 'app'
require 'test'
2022-11-27 22:06:11 +00:00
require 'live'
require 'keychord'
require 'button'
-- delegate most business logic to a layer that can be reused by other projects
require 'edit'
Editor_state = {}
2022-06-07 21:35:56 +01:00
-- called both in tests and real run
function App.initialize_globals()
-- tests currently mostly clear their own state
-- a few text objects we can avoid recomputing unless the font changes
Text_cache = {}
2022-07-01 06:46:45 +01:00
-- blinking cursor
Cursor_time = 0
2022-07-01 06:46:45 +01:00
-- 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
2022-07-01 06:46:45 +01:00
end
2022-05-02 05:55:57 +01:00
-- called only for real run
function App.initialize(arg)
love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
love.keyboard.setKeyRepeat(true)
2022-06-07 21:24:43 +01:00
2022-11-27 22:06:11 +00:00
Editor_state = nil -- not used outside editor tests
love.graphics.setBackgroundColor(1,1,1)
2022-11-27 22:06:11 +00:00
live.initialize(arg)
2023-01-02 01:50:41 +00:00
load_settings()
-- TODO: app-specific stuff goes here
love.window.setTitle('TODO')
if on.initialize then on.initialize(arg) end
if rawget(_G, 'jit') then
jit.off()
jit.flush()
end
end
2023-01-02 01:52:06 +00:00
function love.quit()
if on.quit then on.quit() end
love.filesystem.write('config', json.encode(settings()))
end
function settings()
local x, y, displayindex = App.screen.position()
return {
x=x, y=y, displayindex=displayindex,
width=App.screen.width, height=App.screen.height,
2023-01-02 02:22:52 +00:00
app = on.save_settings and on.save_settings(),
}
2023-01-02 01:52:06 +00:00
end
2023-01-02 01:50:41 +00:00
function load_settings()
2023-01-02 01:51:35 +00:00
if love.filesystem.getInfo('config') then
local settings = json.decode(love.filesystem.read('config'))
local width, height, flags = love.window.getMode()
flags.resizable = true
flags.minwidth = math.min(width, 200)
flags.minheight = math.min(height, 200)
App.screen.flags = flags
App.screen.width = settings.width
App.screen.height = settings.height
love.window.setMode(App.screen.width, App.screen.height, App.screen.flags)
love.window.setPosition(settings.x, settings.y, settings.displayindex)
2023-01-02 01:51:35 +00:00
else
local font_height = 20
love.graphics.setFont(love.graphics.newFont(font_height))
-- maximize window
love.window.setMode(0, 0) -- maximize
App.screen.width, App.screen.height, App.screen.flags = love.window.getMode()
-- shrink height slightly to account for window decoration
App.screen.height = App.screen.height-100
App.screen.width = 40*App.width(App.newText(love.graphics.getFont(), 'm'))
App.screen.flags.resizable = true
App.screen.flags.minwidth = math.min(App.screen.width, 200)
2023-01-02 02:10:43 +00:00
App.screen.flags.minheight = math.min(App.screen.height, 200)
2023-01-02 01:51:35 +00:00
love.window.setMode(App.screen.width, App.screen.height, App.screen.flags)
end
if on.load_settings then on.load_settings(settings.app) end
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
2022-11-04 04:46:20 +00:00
Last_resize_time = Current_time
2022-11-27 22:06:11 +00:00
if on.resize then on.resize(w,h) end
end
function App.filedropped(file)
2022-12-24 04:30:35 +00:00
if on.file_drop then on.file_drop() end
end
function App.draw()
2022-11-27 22:06:11 +00:00
if on.draw then on.draw() end
2022-05-02 05:55:57 +01:00
end
function App.update(dt)
Current_time = Current_time + dt
-- some hysteresis while resizing
if Current_time < Last_resize_time + 0.1 then
2022-07-25 23:23:01 +01:00
return
end
Cursor_time = Cursor_time + dt
live.update(dt)
2022-11-27 22:06:11 +00:00
if on.update then on.update(dt) end
end
function App.mousepressed(x,y, mouse_button)
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
2022-12-24 04:30:35 +00:00
if on.mouse_press then on.mouse_press(x,y, mouse_button) end
end
function App.mousereleased(x,y, mouse_button)
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
2022-12-24 04:30:35 +00:00
if on.mouse_release then on.mouse_release(x,y, mouse_button) end
end
function App.focus(in_focus)
if in_focus then
2022-11-04 04:46:20 +00:00
Last_focus_time = Current_time
end
if in_focus then
love.graphics.setBackgroundColor(1,1,1)
else
love.graphics.setBackgroundColor(0.8,0.8,0.8)
end
2022-11-27 22:06:11 +00:00
if on.focus then on.focus(in_focus) end
end
2022-11-27 22:06:11 +00:00
-- App.keypressed is defined in keychord.lua
function App.keychord_press(chord, key)
-- 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
2022-12-24 04:17:16 +00:00
if on.keychord_press then on.keychord_press(chord, key) end
end
function App.textinput(t)
-- 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
2022-12-24 04:17:16 +00:00
if on.text_input then on.text_input(t) end
end
function App.keyreleased(key, scancode)
-- 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
2022-12-24 04:17:16 +00:00
if on.key_release then on.key_release(key, scancode, isrepeat) end
end
-- use this sparingly
function to_text(s)
if Text_cache[s] == nil then
Text_cache[s] = App.newText(love.graphics.getFont(), s)
end
return Text_cache[s]
end