utf8 = require 'utf8'
json = require 'json'
OS = love.system.getOS()
require 'app'
require 'test'
require 'live'
require 'keychord'
require 'button'
require 'wav'
require 'edit'
Editor_state = {}
function App.initialize_globals()
Supported_versions = {'11.5', '11.4', '11.3', '11.2'} if Mode == nil then Mode = 'run'
end
Error_count = 0
Line_number_width = 3
Cursor_time = 0
Current_time = 0
Last_focus_time = 0 Last_resize_time = 0
end
function App.initialize(arg)
love.keyboard.setKeyRepeat(true)
Editor_state = nil
love.graphics.setBackgroundColor(1,1,1)
if love.filesystem.getInfo('config') and #love.filesystem.read('config') > 0 then
load_settings()
else
initialize_default_settings()
end
if on.initialize then on.initialize(arg) end
if rawget(_G, 'jit') then
jit.off()
jit.flush()
end
check_love_version()
end
function check_love_version()
if array.find(Supported_versions, Version) == nil then
Mode = 'error'
if Error_message == nil then Error_message = '' end
Error_message = ("This app hasn't been tested with LÖVE version %s; please use version %s if you run into errors. Press a key to try recovering.\n\n%s"):format(Version, Supported_versions[1], Error_message)
print(Error_message)
end
end
function print_and_log(s)
print(s)
log(3, s)
end
function love.quit()
if on.quit then on.quit() end
love.filesystem.write('config', json.encode(settings()))
end
function restart()
if on.quit then on.quit() end
love.filesystem.write('config', json.encode(settings()))
load_settings()
if on.initialize then on.initialize() end
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,
app = on.save_settings and on.save_settings(),
}
end
function load_settings()
local settings = json.decode(love.filesystem.read('config'))
_, _, 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)
if on.load_settings then on.load_settings(settings.app) end
end
function set_window_position_from_settings(settings)
if OS == 'Linux' then
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()
end
function initialize_window_geometry()
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)
App.screen.width = love.window.fromPixels(App.screen.width)
App.screen.height = love.window.fromPixels(App.screen.height)
end
function App.resize(w, h)
App.screen.width, App.screen.height = w, h
Last_resize_time = Current_time
if on.resize then on.resize(w,h) end
end
function App.draw()
if Mode == 'error' then
love.graphics.setColor(0,0,1)
love.graphics.rectangle('fill', 0,0, App.screen.width, App.screen.height)
love.graphics.setColor(1,1,1)
love.graphics.printf(Error_message, 40,40, 600)
return
end
if on.draw then on.draw() end
end
function App.update(dt)
Current_time = Current_time + dt
if Current_time < Last_resize_time + 0.1 then
return
end
Cursor_time = Cursor_time + dt
live.update(dt)
if Mode == 'run' then
if on.update then on.update(dt) end
end
end
function App.mousepressed(x,y, mouse_button)
if Mode == 'error' then return end
Cursor_time = 0 if on.mouse_press then on.mouse_press(x,y, mouse_button) end
end
function App.mousereleased(x,y, mouse_button)
if Mode == 'error' then return end
Cursor_time = 0 if on.mouse_release then on.mouse_release(x,y, mouse_button) end
end
function App.mousemoved(x,y, dx,dy, istouch)
if on.mouse_move then on.mouse_move(x,y, dx,dy, istouch) end
end
function App.wheelmoved(dx,dy)
if Mode == 'error' then return end
Cursor_time = 0 if on.mouse_wheel_move then on.mouse_wheel_move(dx,dy) end
end
function App.mousefocus(in_focus)
if Mode == 'error' then return end
Cursor_time = 0 if on.mouse_focus then on.mouse_focus(in_focus) end
end
function App.touchpressed(id, x,y, dx,dy, pressure)
if Mode == 'error' then return end
Cursor_time = 0 if on.touch_press then on.touch_press(id, x,y, dx,dy, pressure) end
end
function App.touchreleased(id, x,y, dx,dy, pressure)
if Mode == 'error' then return end
Cursor_time = 0 if on.touch_release then on.touch_release(id, x,y, dx,dy, pressure) end
end
function App.touchmoved(id, x,y, dx,dy, pressure)
if on.touch_move then on.touch_move(id, x,y, dx,dy, pressure) end
end
function App.focus(in_focus)
if Mode == 'error' then return end
if in_focus then
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
if on.focus then on.focus(in_focus) end
end
function App.keychord_press(chord, key)
if Mode == 'error' then
if chord == 'C-c' then
love.system.setClipboardText(Error_message)
end
return
end
if OS == 'iOS' then
love.keyboard.setTextInput(true) love.keyboard.setKeyRepeat(true)
end
if Current_time < Last_focus_time + 0.01 then
return
end
Cursor_time = 0 if on.keychord_press then on.keychord_press(chord, key) end
end
function App.textinput(t)
if Mode == 'error' then return end
if Current_time < Last_focus_time + 0.01 then
return
end
Cursor_time = 0 if on.text_input then on.text_input(t) end
end
function App.keyreleased(key, scancode)
if Mode == 'error' then
if Redo_initialization then
Redo_initialization = nil
love.run() else
Mode = 'run'
end
return
end
if Current_time < Last_focus_time + 0.01 then
return
end
Cursor_time = 0 if on.key_release then on.key_release(key, scancode) end
end
for handler_name in pairs(love.handlers) do
if App[handler_name] == nil then
App[handler_name] = function(...)
if on[handler_name] then on[handler_name](...) end
end
end
end