Merge template-live-editor-mobile

This commit is contained in:
Kartik K. Agaram 2024-01-15 02:45:55 -08:00
commit a6f69931f3
20 changed files with 67 additions and 72 deletions

View File

@ -7,7 +7,7 @@ on.initialize = function()
Font = love.graphics.newFont(Font_height)
love.graphics.setFont(Font)
Line_height = math.floor(Font_height*1.3)
Line_number_padding = Line_number_width*App.width('m')
Line_number_padding = Line_number_width*Font:getWidth('m')
Menu_left, Menu_top, Safe_width, Safe_height = love.window.getSafeArea()
Menu_height = 5 + Line_height + 5
Menu_bottom = Menu_top + Menu_height

View File

@ -9,13 +9,13 @@ draw_menu = function()
App.color(White)
love.graphics.print(Current_pane_index, x+5, Menu_top+5+2)
App.color(Normal_color)
x = x+5 + App.width(tostring(Current_pane_index)) + 10
x = x+5 + Font:getWidth(tostring(Current_pane_index)) + 10
x = run_button(x, y)
x = stop_button(x, y)
-- assume screen will always be wide enough to print this far without overflow
if not Overflow_button then
local w = App.width('code')+10
local w2 = App.width('>>')+10
local w = Font:getWidth('code')+10
local w2 = Font:getWidth('>>')+10
if x+w+10+w2+10 < r then
App.color{r=0.5, g=0.5, b=0.5}
love.graphics.print('code', x, y)
@ -33,8 +33,8 @@ draw_menu = function()
x, y = paste_button(x, y, r)
x, y = clear_pane_button(x, y, r)
if not Overflow_button then
local w = App.width('screen')+10
local w2 = App.width('>>')+10
local w = Font:getWidth('screen')+10
local w2 = Font:getWidth('>>')+10
if x+w+10+w2+10 < r then
App.color{r=0.5, g=0.5, b=0.5}
love.graphics.print('screen', x, y)
@ -53,4 +53,4 @@ draw_menu = function()
if Show_menu == 'settings' then
draw_settings_menu()
end
end
end

View File

@ -1,7 +1,7 @@
on.resize = function()
_, _, Safe_width, Safe_height = love.window.getSafeArea()
for _,pane in ipairs(Panes) do
pane.editor_state.right = math.min(100+30*App.width('m'), Safe_width*2/3)
pane.editor_state.right = math.min(100+30*pane.font:getWidth('m'), Safe_width*2/3)
if pane.editor_state.right < pane.editor_state.left then
pane.editor_state.right = pane.editor_state.left+1
end
@ -10,4 +10,4 @@ on.resize = function()
Text.redraw_all(pane.editor_state)
update_output_editor(pane)
end
end
end

View File

@ -3,20 +3,20 @@ update_font_settings = function(font_height)
Line_height = math.floor(Font_height*1.3)
Font = love.graphics.newFont(Font_height)
love.graphics.setFont(Font)
Line_number_padding = Line_number_width*App.width('m')
Line_number_padding = Line_number_width*Font:getWidth('m')
Menu_height = 5 + Line_height + 5
Menu_bottom = Menu_top + Menu_height
for _,pane in ipairs(Panes) do
pane.editor_state.top = Menu_bottom + 20
pane.editor_state.left = Menu_left + 50 + Line_number_padding
pane.editor_state.right = math.min(100+30*App.width('m'), Safe_width*2/3)
pane.editor_state.right = math.min(100+30*Font:getWidth('m'), Safe_width*2/3)
if pane.editor_state.right < pane.editor_state.left then
pane.editor_state.right = pane.editor_state.left+1
end
pane.editor_state.width = pane.editor_state.right - pane.editor_state.left
edit.update_font_settings(pane.editor_state, Font_height)
edit.update_font_settings(pane.editor_state, Font_height, Font)
Text.redraw_all(pane.editor_state)
edit.update_font_settings(pane.output_editor_state, Font_height)
edit.update_font_settings(pane.output_editor_state, Font_height, Font)
update_output_editor(pane)
end
end
end

View File

@ -26,6 +26,6 @@ run_button = function(x, y)
love.graphics.pop()
love.graphics.setCanvas()
end)
local w = App.width('run')+10
local w = Font:getWidth('run')+10
return x+w+10, y
end
end

View File

@ -3,8 +3,8 @@ code_editor_state = function()
Menu_bottom + 20, -- top
Safe_height/2-Line_height, -- bottom
Menu_left + 50 + Line_number_padding, -- left
math.min(100+30*App.width('m'), Safe_width*2/3), -- right
math.min(100+30*Font:getWidth('m'), Safe_width*2/3), -- right
Font, Font_height, Line_height)
Text.redraw_all(result)
return result
end
end

View File

@ -8,7 +8,7 @@ draw_settings_menu = function()
-- font size slider
love.graphics.print('font size', x+10,y+10)
y = y+10+Line_height
local sx = x+App.width('bg')+50 -- align all sliders
local sx = x+Font:getWidth('bg')+50 -- align all sliders
slider(Global_state, 'font', {
fg=Normal_color,
-- x limits
@ -60,4 +60,4 @@ draw_settings_menu = function()
})
y = y+15
end
end
end

View File

@ -7,6 +7,6 @@ stop_button = function(x, y)
Show_menu = nil
clear_handlers()
end)
local w = App.width('stop')+10
local w = Font:getWidth('stop')+10
return x+w+10, y
end
end

View File

@ -2,7 +2,7 @@ maybe_draw_overflow_button = function(x, y, w, r, final_button)
if Overflow_button then return x, y end
local after = x+w
if not final_button then
local w2 = App.width('>>')+10
local w2 = Font:getWidth('>>')+10
-- only draw a button if there's going to be at least space for the overflow button afterwards
after = after+10+w2
end

View File

@ -1,7 +1,7 @@
-- draw a button in the menu if possible
-- if not, stash it behind an overflow ('>>') button
overflowable_button = function(name, x, y, r, onpress1, final_button)
local w = App.width(name)+10
local w = Font:getWidth(name)+10
local x2, y2 = maybe_draw_overflow_button(x, y, w, r, final_button)
if Overflow_button then
-- overflow

View File

@ -34,7 +34,7 @@ draw_file_dialog = function()
local x, y = Menu_left+10, Menu_bottom+10
for _,filename in ipairs(Directory_contents) do
if filename:find(File_dialog_input_text) then
local w = App.width(filename) + 10
local w = Font:getWidth(filename) + 10
if x ~= Menu_left+10 and x+w > Safe_width-Menu_left-10 then
x = Menu_left+10
y = y+Line_height+10
@ -49,4 +49,4 @@ draw_file_dialog = function()
x = x+w+10
end
end
end
end

View File

@ -1,5 +1,5 @@
styled_button = function(name, x, y, onpress1)
local w = App.width(name)+10
local w = Font:getWidth(name)+10
button(Global_state, name, {x=x, y=y, w=w, h=Line_height, bg={r=0.6, g=0.8, b=0.6},
icon = function(p)
if Active_button and Active_button.name == name and Active_button.expire > Current_time then
@ -18,4 +18,4 @@ styled_button = function(name, x, y, onpress1)
}
end,
})
end
end

View File

@ -2,6 +2,6 @@ draw_file_dialog_input = function()
-- draw a suffix to ensure the cursor is visible
App.color(Normal_color)
love.graphics.print(File_dialog_input_draw_suffix, Menu_left+15, Menu_top+10)
local w = App.width(File_dialog_input_draw_suffix)
local w = Font:getWidth(File_dialog_input_draw_suffix)
draw_cursor(Menu_left+15+w, Menu_top+10, Current_pane.editor_state.line_height)
end
end

View File

@ -2,10 +2,10 @@ refresh_file_dialog_input_start = function()
local len = utf8.len(File_dialog_input_text)
for start=0,len do
local s = File_dialog_input_text:sub(start)
if App.width(s) < File_dialog_input_right_margin-15 then
if Font:getWidth(s) < File_dialog_input_right_margin-15 then
File_dialog_input_draw_suffix = s
return
end
end
assert(false, "refresh_file_dialog_input_start isn't working right")
end
end

View File

@ -1,6 +1,6 @@
right_justified_button = function(name, x, y, callback)
local w = App.width(name)
local w = Font:getWidth(name)
local x = x-5-w-5
styled_button(name, x, y, callback)
return x-10
end
end

View File

@ -408,9 +408,9 @@ end
function edit.key_release(State, key, scancode)
end
function edit.update_font_settings(State, font_height)
function edit.update_font_settings(State, font_height, font)
State.font_height = font_height
State.font = love.graphics.newFont(State.font_height)
State.font = font or love.graphics.newFont(State.font_height)
State.line_height = math.floor(font_height*1.3)
end

View File

@ -292,8 +292,3 @@ for handler_name in pairs(love.handlers) do
end
end
end
function warning_message()
assert(type(Current_app) == 'table')
return Current_app.message
end

View File

@ -13,7 +13,7 @@ function Text.draw_search_bar(State)
love.graphics.rectangle('line', 20, y-6, App.screen.width-40, h+2, 2,2)
App.color(Foreground_color)
App.screen.print(State.search_term, 25,y-5)
Text.draw_cursor(State, 25+App.width(State.search_term),y-5, Cursor_color)
Text.draw_cursor(State, 25+State.font:getWidth(State.search_term),y-5, Cursor_color)
end
function Text.search_next(State)

View File

@ -57,11 +57,11 @@ function Text.draw_highlight(State, line, x,y, pos, lo,hi)
lo_px = 0
else
local before = line.data:sub(pos_offset, lo_offset-1)
lo_px = App.width(before)
lo_px = State.font:getWidth(before)
end
local s = line.data:sub(lo_offset, hi_offset-1)
App.color(Highlight_color)
love.graphics.rectangle('fill', x+lo_px,y, App.width(s),State.line_height)
love.graphics.rectangle('fill', x+lo_px,y, State.font:getWidth(s),State.line_height)
App.color(Foreground_color)
return lo_px
end

View File

@ -18,7 +18,7 @@ function Text.draw(State, line_index, y, startpos, fg, hide_cursor, show_line_nu
Text.populate_screen_line_starting_pos(State, line_index)
if show_line_numbers then
App.color(Line_number_color)
love.graphics.print(line_index, State.left-Line_number_width*App.width('m')+10,y)
love.graphics.print(line_index, State.left-Line_number_width*State.font:getWidth('m')+10,y)
end
if fg == nil then
initialize_color()
@ -54,18 +54,18 @@ function Text.draw(State, line_index, y, startpos, fg, hide_cursor, show_line_nu
else
if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
if hide_cursor then
Text.pretend_draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
Text.pretend_draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y)
else
Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y, Cursor_color)
Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y, Cursor_color)
end
elseif pos + frag_len == State.cursor1.pos then
-- Show cursor at end of line.
-- This place also catches end of wrapping screen lines. That doesn't seem worth distinguishing.
-- It seems useful to see a cursor whether your eye is on the left or right margin.
if hide_cursor then
Text.pretend_draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
Text.pretend_draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y)
else
Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y, Cursor_color)
Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y, Cursor_color)
end
end
end
@ -79,7 +79,7 @@ function Text.draw(State, line_index, y, startpos, fg, hide_cursor, show_line_nu
select_color(frag)
end
App.screen.print(frag, x,y)
x = x+App.width(frag)
x = x+State.font:getWidth(frag)
end
y = y + State.line_height
if y + State.line_height > State.bottom then
@ -127,14 +127,14 @@ function Text.populate_screen_line_starting_pos(State, line_index)
local pos = 1
-- try to wrap at word boundaries
for frag in line.data:gmatch('%S*%s*') do
local frag_width = App.width(frag)
local frag_width = State.font:getWidth(frag)
--? print('-- frag:', frag, pos, x, frag_width, State.width)
while x + frag_width > State.width do
--? print('frag:', frag, pos, x, frag_width, State.width)
if x < 0.8 * State.width then
-- long word; chop it at some letter
-- We're not going to reimplement TeX here.
local bpos = Text.nearest_pos_less_than(frag, State.width - x)
local bpos = Text.nearest_pos_less_than(State.font, frag, State.width - x)
if x == 0 and bpos == 0 then
assert(false, ("Infinite loop while line-wrapping. Editor is %dpx wide; window is %dpx wide"):format(State.width, App.screen.width))
end
@ -144,7 +144,7 @@ function Text.populate_screen_line_starting_pos(State, line_index)
--? if bpos > 0 then
--? print('after chop:', frag)
--? end
frag_width = App.width(frag)
frag_width = State.font:getWidth(frag)
end
--? print('screen line:', pos)
table.insert(line_cache.screen_line_starting_pos, pos)
@ -438,7 +438,7 @@ function Text.up(State)
screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, screen_line_starting_pos)
local s = string.sub(State.lines[State.cursor1.line].data, screen_line_starting_byte_offset)
State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
end
else
-- move up one screen line in current line
@ -446,7 +446,7 @@ function Text.up(State)
local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
--? print('cursor pos is now '..tostring(State.cursor1.pos))
end
if Text.lt1(State.cursor1, State.screen_top1) then
@ -467,7 +467,7 @@ function Text.down(State)
if State.cursor1.line < #State.lines then
local new_cursor_line = State.cursor1.line+1
State.cursor1.line = new_cursor_line
State.cursor1.pos = Text.nearest_cursor_pos(State.lines[State.cursor1.line].data, State.cursor_x, State.left)
State.cursor1.pos = Text.nearest_cursor_pos(State.font, State.lines[State.cursor1.line].data, State.cursor_x, State.left)
--? print(State.cursor1.pos)
end
if State.cursor1.line > State.screen_bottom1.line then
@ -486,7 +486,7 @@ function Text.down(State)
--? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
--? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
if scroll_down then
--? print('scroll up preserving cursor')
@ -700,8 +700,8 @@ function Text.to_pos_on_line(State, line_index, mx, my)
return line_cache.screen_line_starting_pos[screen_line_index+1]-1
end
local s = string.sub(line.data, screen_line_starting_byte_offset)
--? print('return', mx, Text.nearest_cursor_pos(s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1)
return screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1
--? print('return', mx, Text.nearest_cursor_pos(State.font, s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, mx, State.left) - 1)
return screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, mx, State.left) - 1
end
y = nexty
end
@ -721,7 +721,7 @@ function Text.screen_line_width(State, line_index, i)
else
screen_line = string.sub(line.data, start_pos)
end
return App.width(screen_line)
return State.font:getWidth(screen_line)
end
function Text.screen_line_index(screen_line_starting_pos, pos)
@ -735,12 +735,12 @@ end
-- convert x pixel coordinate to pos
-- oblivious to wrapping
-- result: 1 to len+1
function Text.nearest_cursor_pos(line, x, left)
function Text.nearest_cursor_pos(font, line, x, left)
if x < left then
return 1
end
local len = utf8.len(line)
local max_x = left+Text.x(line, len+1)
local max_x = left+Text.x(font, line, len+1)
if x > max_x then
return len+1
end
@ -752,8 +752,8 @@ function Text.nearest_cursor_pos(line, x, left)
return leftpos
end
local curr = math.floor((leftpos+rightpos)/2)
local currxmin = left+Text.x(line, curr)
local currxmax = left+Text.x(line, curr+1)
local currxmin = left+Text.x(font, line, curr)
local currxmax = left+Text.x(font, line, curr+1)
--? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
if currxmin <= x and x < currxmax then
if x-currxmin < currxmax-x then
@ -777,18 +777,18 @@ end
-- return the nearest index of line (in utf8 code points) which lies entirely
-- within x pixels of the left margin
-- result: 0 to len+1
function Text.nearest_pos_less_than(line, x)
function Text.nearest_pos_less_than(font, line, x)
--? print('', '-- nearest_pos_less_than', line, x)
local len = utf8.len(line)
local max_x = Text.x_after(line, len)
local max_x = Text.x_after(font, line, len)
if x > max_x then
return len+1
end
local left, right = 0, len+1
while true do
local curr = math.floor((left+right)/2)
local currxmin = Text.x_after(line, curr+1)
local currxmax = Text.x_after(line, curr+2)
local currxmin = Text.x_after(font, line, curr+1)
local currxmax = Text.x_after(font, line, curr+2)
--? print('', x, left, right, curr, currxmin, currxmax)
if currxmin <= x and x < currxmax then
return curr
@ -805,18 +805,18 @@ function Text.nearest_pos_less_than(line, x)
assert(false, 'failed to map x pixel to pos')
end
function Text.x_after(s, pos)
function Text.x_after(font, s, pos)
local len = utf8.len(s)
local offset = Text.offset(s, math.min(pos+1, len+1))
local s_before = s:sub(1, offset-1)
--? print('^'..s_before..'$')
return App.width(s_before)
return font:getWidth(s_before)
end
function Text.x(s, pos)
function Text.x(font, s, pos)
local offset = Text.offset(s, pos)
local s_before = s:sub(1, offset-1)
return App.width(s_before)
return font:getWidth(s_before)
end
function Text.to2(State, loc1)