etch.love/run.lua

220 lines
6.5 KiB
Lua

run = {}
Editor_state = {}
-- called both in tests and real run
function run.initialize_globals()
-- tests currently mostly clear their own state
Font_height = 20
Line_height = math.floor(Font_height*1.3)
File_select_bar_value = nil
-- blinking cursor
Cursor_time = 0
end
-- called only for real run
function run.initialize(arg)
log_new('run')
-- maximizing on iOS breaks text rendering: https://github.com/deltadaedalus/vudu/issues/7
-- no point second-guessing window dimensions on mobile
App.screen.width, App.screen.height, App.screen.flags = App.screen.size()
if Settings then
run.load_settings()
else
run.initialize_default_settings()
end
if #arg > 0 and Editor_state.filename ~= absolutize(arg[1]) 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}
else
load_from_disk(Editor_state)
Text.redraw_all(Editor_state)
end
edit.check_locs(Editor_state)
-- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708
love.window.setTitle('lines.love - '..Editor_state.filename)
if #arg > 1 then
print('ignoring commandline args after '..arg[1])
end
if rawget(_G, 'jit') then
jit.off()
jit.flush()
end
end
function print_and_log(s)
print(s)
log(3, s)
end
function run.load_settings()
Font_height = Settings.font_height
Line_height = math.floor(Font_height*1.3)
local font = love.graphics.newFont(Font_height)
Editor_state = edit.initialize_state(Margin_top, 45 + Margin_left, App.screen.width-Margin_right, font, Font_height, Line_height)
Editor_state.filename = Settings.filename
Editor_state.screen_top1 = Settings.screen_top
Editor_state.cursor1 = Settings.cursor
end
function run.initialize_default_settings()
Font_height = 20
Line_height = math.floor(Font_height*1.3)
local font = love.graphics.newFont(Font_height)
Editor_state = edit.initialize_state(Margin_top, 45 + Margin_left, App.screen.width-Margin_right, font, Font_height, Line_height)
Settings = run.settings()
end
function run.draw()
Editor_state.button_handlers = {}
if App.run_tests == nil then
run.draw_file_select_button()
end
edit.draw(Editor_state)
if File_select_bar_value then
run.draw_file_select_bar()
end
end
function run.draw_file_select_button()
button(Editor_state, 'open', {x=Menu_left, y=Menu_top, w=40,h=40, bg={r=1,g=0.7,b=1},
icon = function(button_params)
local x,y = button_params.x, button_params.y
local w,h = button_params.w, button_params.h
love.graphics.setColor(0.4,0.4,0.7)
love.graphics.rectangle('line', x,y, w,h, 5,5)
end,
onpress1 = function()
File_select_bar_value = ''
end,
})
end
function run.draw_file_select_bar()
local h = Line_height+2
local y = Menu_top+10
love.graphics.setColor(0.9,0.9,0.9)
love.graphics.rectangle('fill', Menu_left, y-10, App.screen.width-1, h+8)
love.graphics.setColor(0.6,0.6,0.6)
love.graphics.line(Menu_left, y-10, App.screen.width-1, y-10)
App.color(Text_color)
local prompt = 'open file: '
love.graphics.print(prompt, 15, y-5)
love.graphics.setColor(1,1,1)
local text_start_x = Menu_left + Editor_state.font:getWidth(prompt)+20
love.graphics.rectangle('fill', text_start_x-5, y-6, App.screen.width-text_start_x-20, h+2, 2,2)
love.graphics.setColor(0.6,0.6,0.6)
love.graphics.rectangle('line', text_start_x-5, y-6, App.screen.width-text_start_x-20, h+2, 2,2)
App.color(Text_color)
App.screen.print(File_select_bar_value, text_start_x,y-5)
Text.draw_cursor(Editor_state, text_start_x+Editor_state.font:getWidth(File_select_bar_value),y-5)
end
function run.update(dt)
Cursor_time = Cursor_time + dt
edit.update(Editor_state, dt)
end
function run.quit()
edit.quit(Editor_state)
end
function run.settings()
if Settings == nil then Settings = {} end
Settings.x, Settings.y, Settings.displayindex = App.screen.position()
return {
font_height=Editor_state.font_height,
filename=absolutize(Editor_state.filename),
screen_top=Editor_state.screen_top1, cursor=Editor_state.cursor1
}
end
function absolutize(path)
if is_relative_path(path) then
return App.current_dir..path
end
return path
end
function run.mouse_press(x,y, mouse_button)
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
if File_select_bar_value then return end
return edit.mouse_press(Editor_state, x,y, mouse_button)
end
function run.mouse_release(x,y, mouse_button)
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
if File_select_bar_value then return end
return edit.mouse_release(Editor_state, x,y, mouse_button)
end
function run.text_input(t)
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
if File_select_bar_value then
File_select_bar_value = File_select_bar_value..t
return
end
return edit.text_input(Editor_state, t)
end
function run.keychord_press(chord, key)
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
if File_select_bar_value then
if chord == 'escape' then
File_select_bar_value = nil
elseif chord == 'return' then
run.switch_to_file(File_select_bar_value)
File_select_bar_value = nil
elseif chord == 'backspace' then
local len = utf8.len(File_select_bar_value)
local byte_offset = Text.offset(File_select_bar_value, len)
File_select_bar_value = string.sub(File_select_bar_value, 1, byte_offset-1)
end
return
elseif chord == 'C-w' then
File_select_bar_value = ''
end
return edit.keychord_press(Editor_state, chord, key)
end
function run.switch_to_file(filename)
-- first make sure to save edits on any existing file
if Editor_state.next_save then
save_to_disk(Editor_state)
end
-- handle people hitting enter without a filename
if filename == '' then
return
end
-- clear the slate for the new file
Editor_state.filename = love.filesystem.getSourceBaseDirectory()..'/'..filename
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}
love.window.setTitle('lines.love - '..Editor_state.filename)
end
function run.key_release(key, scancode)
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
return edit.key_release(Editor_state, key, scancode)
end
function width(s)
return love.graphics.getFont():getWidth(s)
end