2022-09-03 21:54:38 +01:00
|
|
|
utf8 = require 'utf8'
|
2022-10-30 00:43:13 +01:00
|
|
|
json = require 'json'
|
2022-05-02 16:20:30 +01:00
|
|
|
|
2022-10-30 00:43:13 +01:00
|
|
|
require 'app'
|
|
|
|
require 'test'
|
2022-07-12 21:45:38 +01:00
|
|
|
|
2022-10-30 00:43:13 +01:00
|
|
|
require 'keychord'
|
|
|
|
require 'button'
|
2022-08-24 21:27:04 +01:00
|
|
|
|
2022-10-30 00:43:13 +01:00
|
|
|
require 'main_tests'
|
2022-07-01 05:59:15 +01:00
|
|
|
|
2022-10-30 00:43:13 +01:00
|
|
|
-- 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
|
|
|
|
2022-10-30 00:43:13 +01:00
|
|
|
-- called both in tests and real run
|
|
|
|
function App.initialize_globals()
|
|
|
|
-- tests currently mostly clear their own state
|
2022-06-09 16:15:32 +01:00
|
|
|
|
2022-10-30 00:43:13 +01:00
|
|
|
-- a few text objects we can avoid recomputing unless the font changes
|
|
|
|
Text_cache = {}
|
2022-07-01 06:46:45 +01:00
|
|
|
|
2022-10-30 00:43:13 +01:00
|
|
|
-- blinking cursor
|
|
|
|
Cursor_time = 0
|
2022-07-01 06:46:45 +01:00
|
|
|
|
2022-09-03 21:54:38 +01:00
|
|
|
-- for hysteresis in a few places
|
2022-11-04 04:02:31 +00:00
|
|
|
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
|
|
|
|
2022-10-30 00:43:13 +01:00
|
|
|
-- called only for real run
|
2022-09-03 21:54:38 +01:00
|
|
|
function App.initialize(arg)
|
2022-10-30 00:43:13 +01:00
|
|
|
love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
|
|
|
|
love.keyboard.setKeyRepeat(true)
|
2022-06-07 21:24:43 +01:00
|
|
|
|
2022-10-30 00:43:13 +01:00
|
|
|
love.graphics.setBackgroundColor(1,1,1)
|
|
|
|
|
|
|
|
if love.filesystem.getInfo('config') then
|
|
|
|
load_settings()
|
2022-09-03 21:54:38 +01:00
|
|
|
else
|
2022-10-30 00:43:13 +01:00
|
|
|
initialize_default_settings()
|
2022-09-03 21:54:38 +01:00
|
|
|
end
|
2022-06-07 21:19:17 +01:00
|
|
|
|
2022-10-30 00:43:13 +01:00
|
|
|
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}
|
2022-09-03 21:54:38 +01:00
|
|
|
else
|
2022-10-30 00:43:13 +01:00
|
|
|
load_from_disk(Editor_state)
|
|
|
|
Text.redraw_all(Editor_state)
|
2022-09-03 21:54:38 +01:00
|
|
|
end
|
2022-10-30 00:43:13 +01:00
|
|
|
love.window.setTitle('text.love - '..Editor_state.filename)
|
2022-09-03 21:54:38 +01:00
|
|
|
|
2022-10-30 00:43:13 +01:00
|
|
|
if #arg > 1 then
|
|
|
|
print('ignoring commandline args after '..arg[1])
|
2022-09-03 21:54:38 +01:00
|
|
|
end
|
2022-10-30 00:43:13 +01:00
|
|
|
|
|
|
|
if rawget(_G, 'jit') then
|
|
|
|
jit.off()
|
|
|
|
jit.flush()
|
2022-09-03 21:54:38 +01:00
|
|
|
end
|
2022-05-16 23:26:22 +01:00
|
|
|
end
|
|
|
|
|
2022-10-30 00:43:13 +01:00
|
|
|
function load_settings()
|
|
|
|
local settings = json.decode(love.filesystem.read('config'))
|
|
|
|
love.graphics.setFont(love.graphics.newFont(settings.font_height))
|
2023-01-01 17:38:14 +00:00
|
|
|
-- determine default dimensions and flags
|
2022-10-30 00:43:13 +01:00
|
|
|
App.screen.width, App.screen.height, App.screen.flags = love.window.getMode()
|
|
|
|
-- set up desired window dimensions
|
|
|
|
App.screen.flags.resizable = true
|
|
|
|
App.screen.flags.minwidth = math.min(App.screen.width, 200)
|
2023-01-02 02:08:43 +00:00
|
|
|
App.screen.flags.minheight = math.min(App.screen.height, 200)
|
2022-10-30 00:43:13 +01:00
|
|
|
App.screen.width, App.screen.height = settings.width, settings.height
|
|
|
|
love.window.setMode(App.screen.width, App.screen.height, App.screen.flags)
|
2022-11-06 16:53:46 +00:00
|
|
|
love.window.setPosition(settings.x, settings.y, settings.displayindex)
|
2022-10-30 00:43:13 +01:00
|
|
|
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 initialize_default_settings()
|
|
|
|
local font_height = 20
|
|
|
|
love.graphics.setFont(love.graphics.newFont(font_height))
|
|
|
|
local em = App.newText(love.graphics.getFont(), 'm')
|
|
|
|
initialize_window_geometry(App.width(em))
|
|
|
|
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)
|
|
|
|
Editor_state.em = em
|
|
|
|
end
|
|
|
|
|
|
|
|
function initialize_window_geometry(em_width)
|
|
|
|
-- 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*em_width
|
|
|
|
App.screen.flags.resizable = true
|
|
|
|
App.screen.flags.minwidth = math.min(App.screen.width, 200)
|
2023-01-02 02:08:43 +00:00
|
|
|
App.screen.flags.minheight = math.min(App.screen.height, 200)
|
2022-10-30 00:43:13 +01:00
|
|
|
love.window.setMode(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)
|
2022-11-04 04:46:20 +00:00
|
|
|
Last_resize_time = Current_time
|
2022-10-30 00:43:13 +01:00
|
|
|
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)
|
|
|
|
love.window.setTitle('text.love - '..Editor_state.filename)
|
|
|
|
end
|
|
|
|
|
2022-05-23 02:27:48 +01:00
|
|
|
function App.draw()
|
2022-10-30 00:43:13 +01:00
|
|
|
edit.draw(Editor_state)
|
2022-05-02 05:55:57 +01:00
|
|
|
end
|
|
|
|
|
2022-05-23 02:27:48 +01:00
|
|
|
function App.update(dt)
|
2022-11-04 04:02:31 +00:00
|
|
|
Current_time = Current_time + dt
|
2022-07-12 21:45:38 +01:00
|
|
|
-- some hysteresis while resizing
|
2022-11-04 04:02:31 +00:00
|
|
|
if Current_time < Last_resize_time + 0.1 then
|
2022-07-25 23:23:01 +01:00
|
|
|
return
|
2022-07-12 21:45:38 +01:00
|
|
|
end
|
2022-10-30 00:43:13 +01:00
|
|
|
Cursor_time = Cursor_time + dt
|
|
|
|
edit.update(Editor_state, dt)
|
|
|
|
end
|
|
|
|
|
|
|
|
function love.quit()
|
|
|
|
edit.quit(Editor_state)
|
|
|
|
-- save some important settings
|
|
|
|
local x,y,displayindex = love.window.getPosition()
|
|
|
|
local filename = Editor_state.filename
|
2022-11-06 16:53:46 +00:00
|
|
|
if is_relative_path(filename) then
|
2022-10-30 00:43:13 +01:00
|
|
|
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)
|
|
|
|
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
|
|
|
|
return edit.mouse_pressed(Editor_state, x,y, mouse_button)
|
|
|
|
end
|
|
|
|
|
|
|
|
function App.mousereleased(x,y, mouse_button)
|
|
|
|
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
|
|
|
|
return edit.mouse_released(Editor_state, x,y, mouse_button)
|
|
|
|
end
|
|
|
|
|
|
|
|
function App.focus(in_focus)
|
|
|
|
if in_focus then
|
2022-11-04 04:46:20 +00:00
|
|
|
Last_focus_time = Current_time
|
2022-09-03 21:54:38 +01:00
|
|
|
end
|
2022-05-14 21:08:33 +01:00
|
|
|
end
|
|
|
|
|
2022-12-24 01:16:19 +00:00
|
|
|
function App.keychord_press(chord, key)
|
2022-09-03 21:54:38 +01:00
|
|
|
-- ignore events for some time after window in focus (mostly alt-tab)
|
2022-11-04 04:02:31 +00:00
|
|
|
if Current_time < Last_focus_time + 0.01 then
|
2022-09-03 21:54:38 +01:00
|
|
|
return
|
|
|
|
end
|
2022-10-30 00:43:13 +01:00
|
|
|
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
|
2022-12-24 04:11:51 +00:00
|
|
|
return edit.keychord_press(Editor_state, chord, key)
|
2022-07-25 23:33:06 +01:00
|
|
|
end
|
|
|
|
|
2022-05-23 02:27:48 +01:00
|
|
|
function App.textinput(t)
|
2022-09-03 21:54:38 +01:00
|
|
|
-- ignore events for some time after window in focus (mostly alt-tab)
|
2022-11-04 04:02:31 +00:00
|
|
|
if Current_time < Last_focus_time + 0.01 then
|
2022-07-25 23:33:06 +01:00
|
|
|
return
|
|
|
|
end
|
2022-10-30 00:43:13 +01:00
|
|
|
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
|
2022-12-24 04:11:51 +00:00
|
|
|
return edit.text_input(Editor_state, t)
|
2022-05-21 22:03:06 +01:00
|
|
|
end
|
|
|
|
|
2022-10-30 00:43:13 +01:00
|
|
|
function App.keyreleased(key, scancode)
|
2022-09-03 21:54:38 +01:00
|
|
|
-- ignore events for some time after window in focus (mostly alt-tab)
|
2022-11-04 04:02:31 +00:00
|
|
|
if Current_time < Last_focus_time + 0.01 then
|
2022-07-25 23:33:06 +01:00
|
|
|
return
|
|
|
|
end
|
2022-10-30 00:43:13 +01:00
|
|
|
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
|
2022-12-24 04:11:51 +00:00
|
|
|
return edit.key_release(Editor_state, key, scancode)
|
2022-05-02 16:20:30 +01:00
|
|
|
end
|
2022-08-24 21:27:04 +01:00
|
|
|
|
2022-10-30 00:43:13 +01:00
|
|
|
-- use this sparingly
|
|
|
|
function to_text(s)
|
|
|
|
if Text_cache[s] == nil then
|
|
|
|
Text_cache[s] = App.newText(love.graphics.getFont(), s)
|
2022-08-24 21:27:04 +01:00
|
|
|
end
|
2022-10-30 00:43:13 +01:00
|
|
|
return Text_cache[s]
|
2022-08-24 21:27:04 +01:00
|
|
|
end
|