Merge template-live-editor

This commit is contained in:
Kartik K. Agaram 2023-12-29 15:28:00 -08:00
commit 181bc5fb5d
5 changed files with 13 additions and 9 deletions

View File

@ -1,6 +1,6 @@
create_editor = function(col, row)
local left, top, panel_width, panel_height = panel_bounds(col, row)
local result = edit.initialize_state(top+5, left+5, left+panel_width-5, Font_size, math.floor(Font_size*1.3))
local result = edit.initialize_state(top+5, left+5, left+panel_width-5, love.graphics.getFont(), Font_size, math.floor(Font_size*1.3))
Text.redraw_all(result)
return result
end

View File

@ -1,6 +1,6 @@
create_editor_panel = function(col, row, title)
local left, top, panel_width, panel_height = panel_bounds(col, row)
local editor = edit.initialize_state(top+5+100, left+Margin+5, left+panel_width-5, Font_size, math.floor(Font_size*1.3))
local editor = edit.initialize_state(top+5+100, left+Margin+5, left+panel_width-5, love.graphics.getFont(), Font_size, math.floor(Font_size*1.3))
Text.redraw_all(editor)
return {
left=left, top=top,
@ -8,4 +8,4 @@ create_editor_panel = function(col, row, title)
editor=editor,
title_text=love.graphics.newText(Title_font, title),
}
end
end

View File

@ -15,7 +15,7 @@ require 'text'
edit = {}
-- run in both tests and a real run
function edit.initialize_state(top, left, right, font_height, line_height) -- currently always draws to bottom of screen
function edit.initialize_state(top, left, right, font, font_height, line_height) -- currently always draws to bottom of screen
local result = {
lines = {{data=''}}, -- array of strings
@ -52,6 +52,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
cursor_x = 0,
cursor_y = 0,
font = font,
font_height = font_height,
line_height = line_height,
@ -72,7 +73,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
search_backup = nil, -- stuff to restore when cancelling search
}
return result
end -- App.initialize_state
end -- edit.initialize_state
function edit.check_locs(State)
-- if State is inconsistent (i.e. file changed by some other program),
@ -104,6 +105,7 @@ end
-- return y drawn until
function edit.draw(State, fg, hide_cursor)
love.graphics.setFont(State.font)
assert(#State.lines == #State.line_cache, ('line_cache is out of date; %d elements when it should be %d'):format(#State.line_cache, #State.lines))
assert(Text.le1(State.screen_top1, State.cursor1), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos))
State.cursor_x = nil
@ -388,7 +390,7 @@ end
function edit.update_font_settings(State, font_height)
State.font_height = font_height
love.graphics.setFont(love.graphics.newFont(State.font_height))
State.font = love.graphics.newFont(State.font_height)
State.line_height = math.floor(font_height*1.3)
end
@ -405,7 +407,8 @@ function edit.initialize_test_state()
15, -- top margin
Test_margin_left,
App.screen.width - Test_margin_right,
14, -- font height assuming default LÖVE font
love.graphics.getFont(),
14,
15) -- line height
end

View File

@ -104,6 +104,7 @@ end
function load_settings()
local settings = json.decode(love.filesystem.read('config'))
-- set up desired window dimensions and make window resizable
_, _, App.screen.flags = App.screen.size()
App.screen.flags.resizable = true
App.screen.width, App.screen.height = settings.width, settings.height

View File

@ -208,8 +208,8 @@ There's much more I could include here; check out [the LÖVE manual](https://lov
The text-editor widget includes extremely thorough automated tests to give you
early warning if you break something.
* `state = edit.initialize_state(top, left, right, font_height, line_height)` --
returns an object that can be used to render an interactive editor widget
* `state = edit.initialize_state(top, left, right, font, font_height, line_height)`
-- returns an object that can be used to render an interactive editor widget
for text starting at `y=top` on the app window, between `x=left` and
`x=right`. Wraps long lines at word boundaries where possible, or in the
middle of words (no hyphenation yet) when it must.