250 lines
7.2 KiB
Lua
250 lines
7.2 KiB
Lua
utf8 = require 'utf8'
|
|
|
|
json = require('json')
|
|
require('array')
|
|
|
|
require('keychord')
|
|
require('button')
|
|
|
|
require('app')
|
|
|
|
require('icons')
|
|
require('drawing')
|
|
require('geom')
|
|
require('help')
|
|
|
|
require('file')
|
|
require('edit')
|
|
require('text')
|
|
require('move')
|
|
require('search')
|
|
require('select')
|
|
require('undo')
|
|
|
|
function love.load(arg)
|
|
check_love_version()
|
|
|
|
if love.filesystem.getInfo('config') then
|
|
Settings = json.decode(love.filesystem.read('config'))
|
|
end
|
|
|
|
love.keyboard.setKeyRepeat(true)
|
|
|
|
love.graphics.setBackgroundColor(1,1,1)
|
|
|
|
---- initialize globals
|
|
Editor = {}
|
|
|
|
-- 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
|
|
|
|
-- blinking cursor
|
|
Cursor_time = 0
|
|
|
|
---- load editor
|
|
if Settings then
|
|
load_settings()
|
|
else
|
|
initialize_default_settings()
|
|
end
|
|
|
|
if #arg > 0 and Editor.filename ~= absolutize(arg[1]) then
|
|
Editor.filename = arg[1]
|
|
edit.scroll_to_top(Editor) -- clear out stale locations for the old filename
|
|
end
|
|
if #arg > 1 then
|
|
print('ignoring commandline args after '..arg[1])
|
|
end
|
|
|
|
love.window.setTitle('lines.love - '..Editor.filename)
|
|
|
|
-- load the file
|
|
load_from_disk(Editor)
|
|
if not edit.valid_loc(Editor, Editor.screen_top) or
|
|
not edit.valid_loc(Editor, Editor.cursor) then
|
|
edit.scroll_to_top(Editor)
|
|
end
|
|
|
|
if rawget(_G, 'jit') then
|
|
jit.off()
|
|
jit.flush()
|
|
end
|
|
end
|
|
|
|
function check_love_version()
|
|
local supported_versions = {'11.5', '11.4', '12.0'} -- put the recommended version first
|
|
|
|
local major_version, minor_version = love.getVersion()
|
|
local version = major_version..'.'..minor_version
|
|
|
|
if array.find(supported_versions, version) == nil then
|
|
error(
|
|
("This app hasn't been tested with LÖVE version %s. Please add %s to `supported_versions` in main.lua to try it out, and switch to version %s if you run into issues."):format(version, version, supported_versions[1]))
|
|
end
|
|
end
|
|
|
|
function load_settings()
|
|
local font = love.graphics.newFont(Settings.font_height)
|
|
-- set up desired window dimensions and make window resizable
|
|
_, _, App.screen.flags = love.window.getMode()
|
|
App.screen.flags.resizable = true
|
|
App.screen.width, App.screen.height = Settings.width, Settings.height
|
|
love.window.setMode(App.screen.width, App.screen.height, App.screen.flags)
|
|
set_window_position_from_settings(Settings)
|
|
Editor = edit.new(Margin_top, Margin_left, App.screen.width-Margin_right, App.screen.height-Margin_top, font, Settings.font_height)
|
|
Editor.filename = Settings.filename
|
|
Editor.screen_top = Settings.screen_top
|
|
Editor.cursor = 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.
|
|
love.window.setPosition(settings.x, settings.y-37, settings.displayindex)
|
|
else
|
|
love.window.setPosition(settings.x, settings.y, settings.displayindex)
|
|
end
|
|
end
|
|
|
|
function initialize_default_settings()
|
|
local font_height = 20
|
|
local font = love.graphics.newFont(font_height)
|
|
initialize_window_geometry()
|
|
Editor = edit.new(Margin_top, Margin_left, App.screen.width-Margin_right, App.screen.height-Margin_top, font, font_height)
|
|
Settings = settings()
|
|
end
|
|
|
|
function settings()
|
|
if Settings == nil then Settings = {} end
|
|
Settings.x, Settings.y, Settings.displayindex = love.window.getPosition()
|
|
return {
|
|
x=Settings.x, y=Settings.y, displayindex=Settings.displayindex,
|
|
width=App.screen.width, height=App.screen.height,
|
|
font_height=Editor.font_height,
|
|
filename=absolutize(Editor.filename),
|
|
screen_top=Editor.screen_top, cursor=Editor.cursor
|
|
}
|
|
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 = love.window.getMode()
|
|
App.screen.flags.resizable = true
|
|
love.window.setMode(App.screen.width, App.screen.height, App.screen.flags)
|
|
end
|
|
|
|
function love.resize(w,h)
|
|
--? print(("Window resized to width: %d and height: %d."):format(w, h))
|
|
App.screen.width, App.screen.height = w, h
|
|
Editor.right = App.screen.width-Margin_right
|
|
Editor.width = Editor.right-Editor.left
|
|
Editor.bottom = App.screen.height-Margin_top
|
|
Last_resize_time = Current_time
|
|
end
|
|
|
|
function love.filedropped(file)
|
|
-- first make sure to save edits on any existing file
|
|
if Editor.next_save then
|
|
save_to_disk(Editor)
|
|
end
|
|
-- clear the slate for the new file
|
|
Editor.filename = file:getFilename()
|
|
file:open('r')
|
|
Editor.lines = load_from_file(file)
|
|
file:close()
|
|
edit.scroll_to_top(Editor)
|
|
love.window.setTitle('lines.love - '..Editor.filename)
|
|
end
|
|
|
|
function love.focus(in_focus)
|
|
if in_focus then
|
|
Last_focus_time = Current_time
|
|
end
|
|
end
|
|
|
|
function love.draw()
|
|
edit.draw(Editor)
|
|
end
|
|
|
|
function love.update(dt)
|
|
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, dt)
|
|
end
|
|
|
|
function love.keychord_pressed(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
|
|
return edit.keychord_press(Editor, chord, key)
|
|
end
|
|
|
|
function love.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
|
|
return edit.text_input(Editor, t)
|
|
end
|
|
|
|
function love.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
|
|
return edit.key_release(Editor, key, scancode)
|
|
end
|
|
|
|
function love.mousepressed(x,y, mouse_button)
|
|
--? print('mouse press', x,y)
|
|
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
|
|
love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
|
|
return edit.mouse_press(Editor, x,y, mouse_button)
|
|
end
|
|
|
|
function love.mousereleased(x,y, mouse_button)
|
|
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
|
|
return edit.mouse_release(Editor, x,y, mouse_button)
|
|
end
|
|
|
|
function love.mousemoved(x,y, dx,dy, is_touch)
|
|
end
|
|
|
|
function love.wheelmoved(dx,dy)
|
|
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
|
|
return edit.mouse_wheel_move(Editor, dx,dy)
|
|
end
|
|
|
|
function love.mousefocus(in_focus)
|
|
end
|
|
|
|
function love.quit()
|
|
if Disable_all_quit_handlers then return end
|
|
Settings = settings()
|
|
love.filesystem.write('config', json.encode(Settings))
|
|
edit.quit(Editor)
|
|
end
|