2022-07-12 05:11:48 +00:00
|
|
|
utf8 = require 'utf8'
|
2022-05-23 01:12:23 +00:00
|
|
|
|
|
|
|
require 'app'
|
2022-05-23 01:27:48 +00:00
|
|
|
require 'test'
|
2022-07-12 20:40:53 +00:00
|
|
|
|
2022-05-02 15:20:30 +00:00
|
|
|
require 'keychord'
|
2022-07-12 20:40:53 +00:00
|
|
|
require 'button'
|
2022-05-02 15:20:30 +00:00
|
|
|
|
2022-06-18 05:24:46 +00:00
|
|
|
require 'main_tests'
|
|
|
|
|
2022-07-12 05:14:59 +00:00
|
|
|
-- delegate most business logic to a layer that can be reused by other projects
|
|
|
|
require 'edit'
|
2022-07-12 20:40:53 +00:00
|
|
|
Editor_state = {}
|
2022-07-12 05:14:59 +00:00
|
|
|
|
2022-07-12 05:03:41 +00:00
|
|
|
-- called both in tests and real run
|
2022-05-25 19:59:12 +00:00
|
|
|
function App.initialize_globals()
|
2022-07-12 05:14:59 +00:00
|
|
|
return edit.initialize_globals()
|
|
|
|
end
|
2022-05-25 19:59:12 +00:00
|
|
|
|
2022-07-12 05:03:41 +00:00
|
|
|
-- called only for real run
|
2022-05-25 19:59:12 +00:00
|
|
|
function App.initialize(arg)
|
|
|
|
love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
|
|
|
|
love.keyboard.setKeyRepeat(true)
|
|
|
|
|
2022-07-01 04:59:15 +00:00
|
|
|
love.graphics.setBackgroundColor(1,1,1)
|
|
|
|
|
2022-07-01 05:46:45 +00:00
|
|
|
if love.filesystem.getInfo('config') then
|
|
|
|
load_settings()
|
|
|
|
else
|
|
|
|
load_defaults()
|
|
|
|
end
|
|
|
|
|
2022-05-16 22:08:49 +00:00
|
|
|
if #arg > 0 then
|
2022-05-18 02:33:35 +00:00
|
|
|
Filename = arg[1]
|
2022-07-01 05:46:45 +00:00
|
|
|
Lines = load_from_disk(Filename)
|
|
|
|
Screen_top1 = {line=1, pos=1}
|
|
|
|
Cursor1 = {line=1, pos=1}
|
|
|
|
for i,line in ipairs(Lines) do
|
|
|
|
if line.mode == 'text' then
|
|
|
|
Cursor1.line = i
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Lines = load_from_disk(Filename)
|
2022-07-04 04:03:52 +00:00
|
|
|
if Cursor1.line > #Lines or Lines[Cursor1.line].mode ~= 'text' then
|
2022-07-01 05:46:45 +00:00
|
|
|
for i,line in ipairs(Lines) do
|
|
|
|
if line.mode == 'text' then
|
|
|
|
Cursor1.line = i
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2022-05-18 02:15:12 +00:00
|
|
|
end
|
|
|
|
end
|
2022-06-07 20:59:26 +00:00
|
|
|
love.window.setTitle('lines.love - '..Filename)
|
2022-06-07 20:35:56 +00:00
|
|
|
|
|
|
|
if #arg > 1 then
|
|
|
|
print('ignoring commandline args after '..arg[1])
|
|
|
|
end
|
2022-06-09 15:15:32 +00:00
|
|
|
|
|
|
|
if rawget(_G, 'jit') then
|
|
|
|
jit.off()
|
|
|
|
jit.flush()
|
|
|
|
end
|
2022-07-01 05:46:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function load_settings()
|
2022-07-01 05:58:33 +00:00
|
|
|
-- maximize window to determine maximum allowable dimensions
|
|
|
|
love.window.setMode(0, 0) -- maximize
|
|
|
|
App.screen.width, App.screen.height, App.screen.flags = love.window.getMode()
|
|
|
|
--
|
2022-07-01 05:46:45 +00:00
|
|
|
local settings = json.decode(love.filesystem.read('config'))
|
|
|
|
love.window.setPosition(settings.x, settings.y, settings.displayindex)
|
2022-07-01 05:58:33 +00:00
|
|
|
App.screen.width, App.screen.height, App.screen.flags = love.window.getMode()
|
|
|
|
App.screen.flags.resizable = true
|
|
|
|
App.screen.flags.minwidth = math.min(App.screen.width, 200)
|
|
|
|
App.screen.flags.minheight = math.min(App.screen.width, 200)
|
2022-07-01 05:46:45 +00:00
|
|
|
App.screen.width, App.screen.height = settings.width, settings.height
|
2022-07-01 05:58:33 +00:00
|
|
|
love.window.setMode(App.screen.width, App.screen.height, App.screen.flags)
|
2022-07-01 05:46:45 +00:00
|
|
|
Filename = settings.filename
|
|
|
|
initialize_font_settings(settings.font_height)
|
|
|
|
Screen_top1 = settings.screen_top
|
|
|
|
Cursor1 = settings.cursor
|
|
|
|
end
|
|
|
|
|
|
|
|
function load_defaults()
|
|
|
|
initialize_font_settings(20)
|
|
|
|
initialize_window_geometry()
|
|
|
|
end
|
2022-05-02 04:55:57 +00:00
|
|
|
|
2022-07-01 05:02:31 +00:00
|
|
|
function initialize_window_geometry()
|
|
|
|
-- maximize window
|
|
|
|
love.window.setMode(0, 0) -- maximize
|
|
|
|
App.screen.width, App.screen.height, App.screen.flags = love.window.getMode()
|
|
|
|
-- shrink slightly to account for window decoration
|
2022-07-01 05:04:27 +00:00
|
|
|
App.screen.width = 40*App.width(Em)
|
2022-07-01 05:02:31 +00:00
|
|
|
App.screen.height = App.screen.height-100
|
2022-06-07 20:24:43 +00:00
|
|
|
App.screen.flags.resizable = true
|
|
|
|
App.screen.flags.minwidth = math.min(App.screen.width, 200)
|
|
|
|
App.screen.flags.minheight = math.min(App.screen.width, 200)
|
2022-07-01 05:58:33 +00:00
|
|
|
love.window.setMode(App.screen.width, App.screen.height, App.screen.flags)
|
2022-06-07 20:24:43 +00:00
|
|
|
end
|
|
|
|
|
2022-06-18 05:24:46 +00:00
|
|
|
function App.resize(w, h)
|
2022-06-07 20:19:17 +00:00
|
|
|
--? print(("Window resized to width: %d and height: %d."):format(w, h))
|
|
|
|
App.screen.width, App.screen.height = w, h
|
2022-06-08 04:51:48 +00:00
|
|
|
Text.redraw_all()
|
2022-07-01 17:30:52 +00:00
|
|
|
Selection1 = {} -- no support for shift drag while we're resizing
|
2022-07-08 22:07:55 +00:00
|
|
|
Text.tweak_screen_top_and_cursor(Margin_left, App.screen.height-Margin_right)
|
2022-06-17 22:42:53 +00:00
|
|
|
Last_resize_time = App.getTime()
|
2022-06-07 20:19:17 +00:00
|
|
|
end
|
|
|
|
|
2022-06-05 03:06:37 +00:00
|
|
|
function initialize_font_settings(font_height)
|
|
|
|
Font_height = font_height
|
|
|
|
love.graphics.setFont(love.graphics.newFont(Font_height))
|
|
|
|
Line_height = math.floor(font_height*1.3)
|
|
|
|
|
|
|
|
Em = App.newText(love.graphics.getFont(), 'm')
|
|
|
|
end
|
|
|
|
|
2022-05-23 01:27:48 +00:00
|
|
|
function App.filedropped(file)
|
2022-06-30 00:58:58 +00:00
|
|
|
-- first make sure to save edits on any existing file
|
|
|
|
if Next_save then
|
|
|
|
save_to_disk(Lines, Filename)
|
|
|
|
end
|
|
|
|
-- clear the slate for the new file
|
2022-06-02 22:45:25 +00:00
|
|
|
App.initialize_globals() -- in particular, forget all undo history
|
2022-05-18 02:33:35 +00:00
|
|
|
Filename = file:getFilename()
|
2022-05-16 22:26:22 +00:00
|
|
|
file:open('r')
|
2022-05-18 02:27:54 +00:00
|
|
|
Lines = load_from_file(file)
|
2022-05-17 01:44:17 +00:00
|
|
|
file:close()
|
2022-05-18 02:27:54 +00:00
|
|
|
for i,line in ipairs(Lines) do
|
2022-05-18 02:15:12 +00:00
|
|
|
if line.mode == 'text' then
|
2022-05-21 23:24:07 +00:00
|
|
|
Cursor1.line = i
|
2022-05-19 01:18:34 +00:00
|
|
|
break
|
2022-05-18 02:15:12 +00:00
|
|
|
end
|
|
|
|
end
|
2022-05-18 02:33:35 +00:00
|
|
|
love.window.setTitle('Text with Lines - '..Filename)
|
2022-05-16 22:26:22 +00:00
|
|
|
end
|
|
|
|
|
2022-05-23 01:27:48 +00:00
|
|
|
function App.draw()
|
2022-07-12 20:40:53 +00:00
|
|
|
Button_handlers = {}
|
2022-07-12 05:14:59 +00:00
|
|
|
edit.draw()
|
2022-05-02 04:55:57 +00:00
|
|
|
end
|
|
|
|
|
2022-05-23 01:27:48 +00:00
|
|
|
function App.update(dt)
|
2022-07-12 05:14:59 +00:00
|
|
|
edit.update(dt)
|
2022-05-11 04:17:50 +00:00
|
|
|
end
|
|
|
|
|
2022-06-18 23:25:20 +00:00
|
|
|
function love.quit()
|
2022-07-12 05:14:59 +00:00
|
|
|
edit.quit()
|
2022-07-01 05:15:07 +00:00
|
|
|
-- save some important settings
|
|
|
|
local x,y,displayindex = love.window.getPosition()
|
2022-07-01 20:55:43 +00:00
|
|
|
local filename = Filename
|
|
|
|
if filename:sub(1,1) ~= '/' then
|
|
|
|
filename = love.filesystem.getWorkingDirectory()..'/'..filename -- '/' should work even on Windows
|
|
|
|
end
|
2022-07-01 05:15:07 +00:00
|
|
|
local settings = {
|
|
|
|
x=x, y=y, displayindex=displayindex,
|
|
|
|
width=App.screen.width, height=App.screen.height,
|
2022-07-01 19:26:58 +00:00
|
|
|
font_height=Font_height,
|
2022-07-01 20:55:43 +00:00
|
|
|
filename=filename,
|
2022-07-01 19:26:58 +00:00
|
|
|
screen_top=Screen_top1, cursor=Cursor1}
|
2022-07-01 05:15:07 +00:00
|
|
|
love.filesystem.write('config', json.encode(settings))
|
2022-06-18 23:25:20 +00:00
|
|
|
end
|
|
|
|
|
2022-05-23 01:27:48 +00:00
|
|
|
function App.mousepressed(x,y, mouse_button)
|
2022-07-12 05:14:59 +00:00
|
|
|
return edit.mouse_pressed(x,y, mouse_button)
|
2022-05-12 04:57:36 +00:00
|
|
|
end
|
|
|
|
|
2022-07-12 05:01:30 +00:00
|
|
|
function App.mousereleased(x,y, mouse_button)
|
2022-07-12 05:14:59 +00:00
|
|
|
return edit.mouse_released(x,y, mouse_button)
|
2022-05-14 20:08:33 +00:00
|
|
|
end
|
|
|
|
|
2022-05-23 01:27:48 +00:00
|
|
|
function App.textinput(t)
|
2022-07-12 05:14:59 +00:00
|
|
|
return edit.textinput(t)
|
2022-05-21 21:03:06 +00:00
|
|
|
end
|
|
|
|
|
2022-06-27 00:07:27 +00:00
|
|
|
function App.keychord_pressed(chord, key)
|
2022-07-12 05:14:59 +00:00
|
|
|
return edit.keychord_pressed(chord, key)
|
2022-05-12 05:29:21 +00:00
|
|
|
end
|
|
|
|
|
2022-05-23 01:27:48 +00:00
|
|
|
function App.keyreleased(key, scancode)
|
2022-07-12 05:14:59 +00:00
|
|
|
return edit.key_released(key, scancode)
|
2022-05-02 15:20:30 +00:00
|
|
|
end
|