Merge template-live-editor

This commit is contained in:
Kartik K. Agaram 2023-12-29 15:39:36 -08:00
commit 6806a328ff
5 changed files with 12 additions and 8 deletions

View File

@ -2,7 +2,7 @@ new_word = function()
Cursor_word = edit.initialize_state(
50 + (#Words+1)*Line_height*2, -- top
20 + 50, App.screen.width - 50,
Font_height, Line_height)
love.graphics.getFont(), Font_height, Line_height)
Text.redraw_all(Cursor_word)
Cursor_word.contents = ''
table.insert(Words, Cursor_word)

View File

@ -2,7 +2,7 @@ learn_initialize = function()
Workbook = edit.initialize_state(
50, -- top
20 + 50, App.screen.width-50,
Font_height, Line_height)
love.graphics.getFont(), Font_height, Line_height)
Text.redraw_all(Workbook)
Answer_idx = love.math.random(#Words)
if Answer_idx then

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

@ -107,6 +107,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.