294 lines
9.3 KiB
Lua
294 lines
9.3 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)
|
|
Menu_bar_height = 5 + Line_height + 5
|
|
|
|
-- state machine:
|
|
-- states:
|
|
-- - editing: if Export_filename_value == nil
|
|
-- - export: if Export_filename_value and not Export_filename_doublecheck
|
|
-- - doublecheck: if Export_filename_doublecheck
|
|
-- transitions:
|
|
-- editing | press 'export' button -> export
|
|
-- export | hit return ->
|
|
-- | Export_filename_value doesn't exist -> editing [after writing]
|
|
-- | Export_filename_value exists -> doublecheck
|
|
-- doublecheck | hit return -> editing [after writing]
|
|
-- doublecheck | hit some other key -> editing [cancel]
|
|
Export_filename_value = nil
|
|
Export_filename_text = nil
|
|
Export_filename_doublecheck = false
|
|
|
|
Current_flash = nil
|
|
Current_flash_time = nil
|
|
|
|
-- a few text objects we can avoid recomputing unless the font changes
|
|
Text_cache = {}
|
|
|
|
-- 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 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 run.load_settings()
|
|
Font_height = Settings.font_height
|
|
Line_height = math.floor(Font_height*1.3)
|
|
love.graphics.setFont(love.graphics.newFont(Font_height))
|
|
Editor_state = edit.initialize_state(Menu_bar_height + Margin_top, Margin_left, App.screen.width-Margin_right, 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)
|
|
Menu_bar_height = 5 + Line_height + 5
|
|
love.graphics.setFont(love.graphics.newFont(Font_height))
|
|
local em = App.newText(love.graphics.getFont(), 'm')
|
|
Editor_state = edit.initialize_state(Menu_bar_height + Margin_top, Margin_left, App.screen.width-Margin_right)
|
|
Editor_state.font_height = Font_height
|
|
Editor_state.line_height = Line_height
|
|
Editor_state.em = em
|
|
Settings = run.settings()
|
|
end
|
|
|
|
function run.draw()
|
|
Editor_state.button_handlers = {}
|
|
if App.run_tests == nil then
|
|
if not Export_filename_value then
|
|
run.draw_buttons()
|
|
elseif Export_filename_doublecheck then
|
|
love.graphics.setColor(0,1,0)
|
|
love.graphics.print(Export_filename_value..' already exists. Overwrite? Hit enter/return to confirm, any other key to cancel', 15, 5)
|
|
end
|
|
end
|
|
edit.draw(Editor_state)
|
|
if Export_filename_value and not Export_filename_doublecheck then
|
|
run.draw_export_filename_bar()
|
|
end
|
|
end
|
|
|
|
function run.draw_buttons()
|
|
local button_text = 'clear'
|
|
local width = to_text(button_text):getWidth()
|
|
local x=5
|
|
button(Editor_state, button_text, {x=x, y=5, w=width+10,h=Line_height, color={1,0.7,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)
|
|
love.graphics.setColor(0,0,0)
|
|
love.graphics.print(button_text, x+5, y)
|
|
end,
|
|
onpress1 = function()
|
|
Editor_state.lines = {{mode='text', data=''}}
|
|
Text.redraw_all(Editor_state)
|
|
Editor_state.screen_top1 = {line=1, pos=1}
|
|
Editor_state.cursor1 = {line=1, pos=1}
|
|
end,
|
|
})
|
|
x = x+width+10 + 10
|
|
button_text = 'export'
|
|
width = to_text(button_text):getWidth()
|
|
button(Editor_state, button_text, {x=x, y=5, w=width+10,h=Line_height, color={1,0.7,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)
|
|
love.graphics.setColor(0,0,0)
|
|
love.graphics.print(button_text, x+5, y)
|
|
end,
|
|
onpress1 = function()
|
|
Export_filename_value = ''
|
|
Export_filename_text = nil
|
|
end,
|
|
})
|
|
x = x+width+10 + 10
|
|
if Current_flash then
|
|
App.color{r=0,g=0.4,b=0}
|
|
love.graphics.print(Current_flash, x, 5)
|
|
end
|
|
end
|
|
|
|
function run.draw_export_filename_bar()
|
|
local h = Line_height+2
|
|
local y = 10
|
|
love.graphics.setColor(0.9,0.9,0.9)
|
|
love.graphics.rectangle('fill', 0, y-10, App.screen.width-1, h+8)
|
|
love.graphics.setColor(0.6,0.6,0.6)
|
|
love.graphics.line(0, y-10, App.screen.width-1, y-10)
|
|
App.color(Text_color)
|
|
local prompt = to_text('open file: ')
|
|
love.graphics.draw(prompt, 15, y-5)
|
|
love.graphics.setColor(1,1,1)
|
|
local text_start_x = App.width(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)
|
|
if Export_filename_text == nil then
|
|
Export_filename_text = App.newText(love.graphics.getFont(), Export_filename_value)
|
|
end
|
|
App.screen.draw(Export_filename_text, text_start_x,y-5)
|
|
Text.draw_cursor(Editor_state, text_start_x+App.width(Export_filename_text),y-5)
|
|
end
|
|
|
|
function run.update(dt)
|
|
Cursor_time = Cursor_time + dt
|
|
edit.update(Editor_state, dt)
|
|
if Current_flash then
|
|
if App.getTime() - Current_flash_time > 3 then
|
|
Current_flash = nil
|
|
Current_flash_time = nil
|
|
end
|
|
end
|
|
end
|
|
|
|
function run.quit()
|
|
edit.quit(Editor_state)
|
|
end
|
|
|
|
function run.settings()
|
|
if Settings == nil then
|
|
Settings = {}
|
|
end
|
|
if Current_app == 'run' then
|
|
Settings.x, Settings.y, Settings.displayindex = App.screen.position()
|
|
end
|
|
local filename = Editor_state.filename
|
|
if is_relative_path(filename) then
|
|
filename = love.filesystem.getWorkingDirectory()..'/'..filename -- '/' should work even on Windows
|
|
end
|
|
return {
|
|
font_height=Editor_state.font_height,
|
|
filename=filename,
|
|
screen_top=Editor_state.screen_top1, cursor=Editor_state.cursor1
|
|
}
|
|
end
|
|
|
|
function run.mouse_press(x,y, mouse_button)
|
|
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
|
|
if Export_filename_value then return end
|
|
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 Export_filename_value then return end
|
|
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 Export_filename_doublecheck then
|
|
-- keychord_press did the work; we'll just clean up
|
|
Export_filename_value = nil
|
|
Export_filename_text = nil
|
|
Export_filename_doublecheck = false
|
|
elseif Export_filename_value then
|
|
Export_filename_value = Export_filename_value..t
|
|
Export_filename_text = nil
|
|
return
|
|
else
|
|
edit.text_input(Editor_state, t)
|
|
end
|
|
end
|
|
|
|
function run.keychord_press(chord, key)
|
|
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
|
|
if Export_filename_doublecheck then
|
|
if chord == 'return' then
|
|
local outfilename = love.filesystem.getSourceBaseDirectory()..'/'..Export_filename_value..'.html'
|
|
export(outfilename)
|
|
Current_flash = 'exported'
|
|
Current_flash_time = App.getTime()
|
|
end
|
|
elseif Export_filename_value then
|
|
if chord == 'escape' then
|
|
Export_filename_value = nil
|
|
Export_filename_text = nil
|
|
elseif chord == 'return' then
|
|
local outfilename = love.filesystem.getSourceBaseDirectory()..'/'..Export_filename_value..'.html'
|
|
if file_exists(outfilename) then
|
|
Export_filename_doublecheck = true
|
|
else
|
|
export(outfilename)
|
|
Export_filename_value = nil
|
|
Export_filename_text = nil
|
|
Current_flash = 'exported'
|
|
Current_flash_time = App.getTime()
|
|
end
|
|
elseif chord == 'backspace' then
|
|
local len = utf8.len(Export_filename_value)
|
|
local byte_offset = Text.offset(Export_filename_value, len)
|
|
Export_filename_value = string.sub(Export_filename_value, 1, byte_offset-1)
|
|
Export_filename_text = nil
|
|
end
|
|
else
|
|
edit.keychord_press(Editor_state, chord, key)
|
|
end
|
|
end
|
|
|
|
function run.key_release(key, scancode)
|
|
Cursor_time = 0 -- ensure cursor is visible immediately after it moves
|
|
edit.key_release(Editor_state, key, scancode)
|
|
end
|
|
|
|
-- use this sparingly
|
|
function to_text(s)
|
|
if Text_cache[s] == nil then
|
|
Text_cache[s] = App.newText(love.graphics.getFont(), s)
|
|
end
|
|
return Text_cache[s]
|
|
end
|