local json = require 'json' require('test') OS = love.system.getOS() -- some global names accessible to Carousel require 'app' local I = {} -- for file-local helpers, so I can refer to them before defining them edit = require 'edit' -- available to app local keychord = require 'keychord' live = require 'live' -- available to app Current_time = 0 Error_message = '' Error_count = 0 local I = {} function love.load(arg, unfiltered_arg) Arg, Unfiltered_arg = arg, unfiltered_arg -- TODO: LÖVE version check should probably be outside edit if not edit.is_this_love_version_supported() then unsupported_version_screen_until_keypress() end run_tests() love.keyboard.setKeyRepeat(true) love.graphics.setBackgroundColor(1,1,1) love.graphics.setFont(love.graphics.newFont(20)) OS = love.system.getOS() live.load() edit.load() -- remember settings across restarts local settings = I.load_settings() --? print(settings.x, settings.y) local screen_width, screen_height = I.adjust_window(settings) if on.initialize then on.initialize(arg, unfiltered_arg) end end function unsupported_version_screen_until_keypress() local 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 continue."):format(edit.love_version(), edit.preferred_love_version()) print(error_message) while true do love.graphics.origin() love.graphics.clear(0,0,1) love.graphics.setColor(1,1,1) love.graphics.printf(error_message, 40,40, 400) love.graphics.present() if love.event then love.event.pump() for name, a,b,c,d,e,f in love.event.poll() do if name == 'quit' then if not love.quit or not love.quit() then os.exit(1) end elseif name == 'keypressed' then return end end end love.timer.sleep(0.01) end end function I.adjust_window(settings) -- adjust dimensions and make resizable local screen_width, screen_height, flags = love.window.getMode() if settings then screen_width, screen_height = settings.width, settings.height end flags.resizable = true local has_text_input = love.keyboard.hasTextInput() I.set_window_mode(screen_width, screen_height, flags) if OS == 'iOS' then -- iOS recreates the window on setMode -- readd some properties of the window -- https://github.com/love2d/love/issues/1959 love.keyboard.setTextInput(has_text_input) love.keyboard.setKeyRepeat(true) end -- adjust position if settings then if OS == 'Linux' then -- HACK: love.window.setPosition doesn't quite seem to do what is asked of it on Linux. -- This line is currently tuned just for Kartik's computer. love.window.setPosition(settings.x-1, settings.y-35, settings.displayindex) else love.window.setPosition(settings.x, settings.y, settings.displayindex) end end return screen_width, screen_height end function I.set_window_mode(w, h, flags) -- Hack. On Linux with LÖVE 12, I've observed love.window.setMode go awry. -- Guidance from LÖVE folks (https://github.com/love2d/love/issues/2214) -- is that everything should be working with density-independent units -- most of the time, but on some platforms love.window.setMode doesn't do -- so. In that case, make sure to pass in values in raw pixel units to it. local w1 = love.window.getMode() local w2 = love.graphics.getDimensions() if w1 ~= w2 then w, h = love.window.toPixels(w, h) end love.window.setMode(w, h, flags) end function love.resize(w,h) if on.resize then on.resize(w,h) end end function love.draw() if on.draw then on.draw() end end function love.mousepressed(x,y, mouse_button, is_touch, presses) if on.mouse_press then on.mouse_press(x,y, mouse_button, is_touch, presses) end end function love.mousemoved(x,y, dx,dy, is_touch) if on.mouse_move then on.mouse_move(x,y, dx,dy, is_touch) end end function love.mousereleased(x,y, mouse_button, is_touch, presses) if on.mouse_release then on.mouse_release(x,y, mouse_button, is_touch, presses) end end function love.mousemoved(x,y, dx,dy, is_touch) if on.mouse_move then on.mouse_move(x,y, dx,dy, is_touch) end end function love.touchpressed(id, x,y, dx,dy, pressure) if Mode == 'error' then return end if on.touch_press then on.touch_press(id, x,y, dx,dy, pressure) end end function love.touchreleased(id, x,y, dx,dy, pressure) if Mode == 'error' then return end if on.touch_release then on.touch_release(id, x,y, dx,dy, pressure) end end function love.touchmoved(id, x,y, dx,dy, pressure) if Mode == 'error' then return end if on.touch_move then on.touch_move(id, x,y, dx,dy, pressure) end end function love.wheelmoved(dx,dy) if on.mouse_wheel_move then on.mouse_wheel_move(dx,dy) end end function love.keypressed(key, scancode, is_repeat) local chord = keychord.collect_keypress(key) if chord == nil then return end if OS == 'iOS' then -- magic. iOS is prone to losing textinput events. -- https://github.com/love2d/love/issues/1959 love.keyboard.setTextInput(true) love.keyboard.setKeyRepeat(true) end if on.keychord_press then on.keychord_press(chord, key, scancode, is_repeat) end end function love.textinput(t) if on.text_input then on.text_input(t) end end function love.keyreleased(key, scancode) if on.key_release then on.key_release(key, scancode) end end function love.update(dt) Current_time = Current_time + dt live.update(dt) edit.update_all(dt) if on.update then on.update(dt) end end function love.quit() if on.quit then on.quit() end save_settings() end function restart() if on.quit then on.quit() end save_settings() I.load_settings() if on.initialize then on.initialize(Arg, Unfiltered_arg) end end function love.mousefocus(in_focus) if on.mouse_focus then on.mouse_focus(in_focus) end end function love.focus(in_focus) 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 I.load_settings() if not love.filesystem.getInfo('config') then return nil end local contents = love.filesystem.read('config') if #contents == 0 then return nil end local settings = json.decode(contents) if on.load_settings then on.load_settings(settings.app) end return settings end function save_settings() -- available to app local settings = {} if on.save_settings then settings.app = on.save_settings() end settings.x, settings.y, settings.displayindex = love.window.getPosition() settings.width, settings.height = love.graphics.getDimensions() love.filesystem.write('config', json.encode(settings)) end