lines.love/main.lua

186 lines
5.0 KiB
Lua
Raw Normal View History

utf8 = require 'utf8'
2022-05-23 01:12:23 +00:00
require 'app'
require 'test'
2022-07-12 20:40:53 +00:00
require 'keychord'
2022-07-12 20:40:53 +00:00
require 'button'
2022-06-18 05:24:46 +00:00
require 'main_tests'
-- 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:03:41 +00:00
-- called both in tests and real run
function App.initialize_globals()
return edit.initialize_globals()
end
2022-07-12 05:03:41 +00:00
-- called only for real run
function App.initialize(arg)
love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
love.keyboard.setKeyRepeat(true)
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
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)
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
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
if rawget(_G, 'jit') then
jit.off()
jit.flush()
end
2022-07-01 05:46:45 +00:00
end
function load_settings()
-- 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)
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
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
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
App.screen.width = 40*App.width(Em)
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)
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)
--? print(("Window resized to width: %d and height: %d."):format(w, h))
App.screen.width, App.screen.height = w, h
Text.redraw_all()
Selection1 = {} -- no support for shift drag while we're resizing
Text.tweak_screen_top_and_cursor(Margin_left, App.screen.height-Margin_right)
Last_resize_time = App.getTime()
end
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
function App.filedropped(file)
-- 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
App.initialize_globals() -- in particular, forget all undo history
2022-05-18 02:33:35 +00:00
Filename = file:getFilename()
file:open('r')
Lines = load_from_file(file)
2022-05-17 01:44:17 +00:00
file:close()
for i,line in ipairs(Lines) do
if line.mode == 'text' then
Cursor1.line = i
break
end
end
2022-05-18 02:33:35 +00:00
love.window.setTitle('Text with Lines - '..Filename)
end
function App.draw()
2022-07-12 20:40:53 +00:00
Button_handlers = {}
edit.draw()
2022-05-02 04:55:57 +00:00
end
function App.update(dt)
edit.update(dt)
end
function love.quit()
edit.quit()
-- save some important settings
local x,y,displayindex = love.window.getPosition()
local filename = Filename
if filename:sub(1,1) ~= '/' then
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,
2022-07-01 19:26:58 +00:00
font_height=Font_height,
filename=filename,
2022-07-01 19:26:58 +00:00
screen_top=Screen_top1, cursor=Cursor1}
love.filesystem.write('config', json.encode(settings))
end
function App.mousepressed(x,y, mouse_button)
return edit.mouse_pressed(x,y, mouse_button)
end
2022-07-12 05:01:30 +00:00
function App.mousereleased(x,y, mouse_button)
return edit.mouse_released(x,y, mouse_button)
end
function App.textinput(t)
return edit.textinput(t)
end
function App.keychord_pressed(chord, key)
return edit.keychord_pressed(chord, key)
2022-05-12 05:29:21 +00:00
end
function App.keyreleased(key, scancode)
return edit.key_released(key, scancode)
end