Merge lines.love

This commit is contained in:
Kartik K. Agaram 2023-09-15 23:56:49 -07:00
commit 09c76c82c2
7 changed files with 40 additions and 29 deletions

View File

@ -129,8 +129,8 @@ end
function navigate_to_file(s) function navigate_to_file(s)
move_candidate_to_front(s) move_candidate_to_front(s)
local candidate = guess_source(s..'.lua') source.switch_to_file(s..'.lua')
source.switch_to_file(candidate) love.window.setTitle('lines.love - source - '..Editor_state.filename)
reset_file_navigator() reset_file_navigator()
end end

View File

@ -191,8 +191,9 @@ function Drawing.draw_pending_shape(drawing, top, left,right)
if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h then if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h then
return return
end end
local r = round(geom.dist(center.x, center.y, mx, my))
local cx,cy = px(center.x), py(center.y) local cx,cy = px(center.x), py(center.y)
love.graphics.circle('line', cx,cy, geom.dist(cx,cy, App.mouse_x(),App.mouse_y())) love.graphics.circle('line', cx,cy, Drawing.pixels(r, width))
elseif shape.mode == 'arc' then elseif shape.mode == 'arc' then
local center = drawing.points[shape.center] local center = drawing.points[shape.center]
if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h then if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h then
@ -248,6 +249,12 @@ function Drawing.update(State)
if State.lines.current_drawing == nil then return end if State.lines.current_drawing == nil then return end
local drawing = State.lines.current_drawing local drawing = State.lines.current_drawing
local line_cache = State.line_cache[State.lines.current_drawing_index] local line_cache = State.line_cache[State.lines.current_drawing_index]
if line_cache.starty == nil then
-- some event cleared starty just this frame
-- draw in this frame will soon set starty
-- just skip this frame
return
end
assert(drawing.mode == 'drawing') assert(drawing.mode == 'drawing')
local pmx, pmy = App.mouse_x(), App.mouse_y() local pmx, pmy = App.mouse_x(), App.mouse_y()
local mx = Drawing.coord(pmx-State.left, State.width) local mx = Drawing.coord(pmx-State.left, State.width)

View File

@ -233,11 +233,13 @@ end
function edit.mouse_wheel_move(State, dx,dy) function edit.mouse_wheel_move(State, dx,dy)
if dy > 0 then if dy > 0 then
State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos} State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
edit.put_cursor_on_next_text_line(State)
for i=1,math.floor(dy) do for i=1,math.floor(dy) do
Text.up(State) Text.up(State)
end end
elseif dy < 0 then elseif dy < 0 then
State.cursor1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos} State.cursor1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
edit.put_cursor_on_next_text_line(State)
for i=1,math.floor(-dy) do for i=1,math.floor(-dy) do
Text.down(State) Text.down(State)
end end

View File

@ -35,7 +35,6 @@ function log_browser.parse(State)
if rest then if rest then
line.data = rest line.data = rest
end end
line.filename = guess_source(line.filename)
line.line_number = tonumber(line.line_number) line.line_number = tonumber(line.line_number)
if line.data:sub(1,1) == '{' then if line.data:sub(1,1) == '{' then
local data = json.decode(line.data) local data = json.decode(line.data)
@ -75,15 +74,6 @@ function table.shallowcopy(x)
return {unpack(x)} return {unpack(x)}
end end
function guess_source(filename)
local possible_source = filename:gsub('%.lua$', '%.splua')
if file_exists(possible_source) then
return possible_source
else
return filename
end
end
function log_browser.draw(State, hide_cursor) function log_browser.draw(State, hide_cursor)
assert(#State.lines == #State.line_cache) assert(#State.lines == #State.line_cache)
local mouse_line_index = log_browser.line_index(State, App.mouse_x(), App.mouse_y()) local mouse_line_index = log_browser.line_index(State, App.mouse_x(), App.mouse_y())

View File

@ -74,7 +74,7 @@ function source.initialize()
-- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708 -- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708
love.window.setTitle('text.love - source') love.window.setTitle('text.love - source - '..Editor_state.filename)
@ -237,7 +237,7 @@ function source.switch_to_file(filename)
end end
function source.draw() function source.draw()
edit.draw(Editor_state, --[[hide cursor?]] Show_file_navigator) edit.draw(Editor_state, --[[hide cursor?]] Show_file_navigator, --[[show line numbers]] true)
if Show_log_browser_side then if Show_log_browser_side then
-- divider -- divider
App.color(Divider_color) App.color(Divider_color)

View File

@ -89,7 +89,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
line_height = line_height, line_height = line_height,
top = top, top = top,
left = math.floor(left), left = math.floor(left), -- left margin for text; line numbers go to the left of this
right = math.floor(right), right = math.floor(right),
width = right-left, width = right-left,
@ -115,7 +115,7 @@ function edit.check_locs(State)
or not edit.cursor_on_text(State) or not edit.cursor_on_text(State)
or not Text.le1(State.screen_top1, State.cursor1) then or not Text.le1(State.screen_top1, State.cursor1) then
State.screen_top1 = {line=1, pos=1} State.screen_top1 = {line=1, pos=1}
edit.put_cursor_on_first_text_line(State) edit.put_cursor_on_next_text_line(State)
end end
end end
@ -131,16 +131,20 @@ function edit.cursor_on_text(State)
and State.lines[State.cursor1.line].mode == 'text' and State.lines[State.cursor1.line].mode == 'text'
end end
function edit.put_cursor_on_first_text_line(State) function edit.put_cursor_on_next_text_line(State)
for i,line in ipairs(State.lines) do while true do
if line.mode == 'text' then if State.cursor1.line >= #State.lines then
State.cursor1 = {line=i, pos=1} break
break end
end if State.lines[State.cursor1.line].mode == 'text' then
break
end
State.cursor1.line = State.cursor1.line+1
State.cursor1.pos = 1
end end
end end
function edit.draw(State, hide_cursor) function edit.draw(State, hide_cursor, show_line_numbers)
State.button_handlers = {} State.button_handlers = {}
App.color(Text_color) App.color(Text_color)
if #State.lines ~= #State.line_cache then if #State.lines ~= #State.line_cache then
@ -169,7 +173,11 @@ function edit.draw(State, hide_cursor)
end end
if line.data == '' then if line.data == '' then
-- button to insert new drawing -- button to insert new drawing
button(State, 'draw', {x=4, y=y+4, w=12,h=12, color={1,1,0}, local buttonx = State.left-Margin_left+4
if show_line_numbers then
buttonx = 4 -- HACK: position draw buttons at a fixed x on screen
end
button(State, 'draw', {x=buttonx, y=y+4, w=12,h=12, color={1,1,0},
icon = icon.insert_drawing, icon = icon.insert_drawing,
onpress1 = function() onpress1 = function()
Drawing.before = snapshot(State, line_index-1, line_index) Drawing.before = snapshot(State, line_index-1, line_index)
@ -183,7 +191,7 @@ function edit.draw(State, hide_cursor)
end, end,
}) })
end end
y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos, hide_cursor) y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos, hide_cursor, show_line_numbers)
--? print('=> y', y) --? print('=> y', y)
elseif line.mode == 'drawing' then elseif line.mode == 'drawing' then
y = y+Drawing_padding_top y = y+Drawing_padding_top
@ -329,11 +337,13 @@ end
function edit.mouse_wheel_move(State, dx,dy) function edit.mouse_wheel_move(State, dx,dy)
if dy > 0 then if dy > 0 then
State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos} State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos}
edit.put_cursor_on_next_text_line(State)
for i=1,math.floor(dy) do for i=1,math.floor(dy) do
Text.up(State) Text.up(State)
end end
elseif dy < 0 then elseif dy < 0 then
State.cursor1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos} State.cursor1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos}
edit.put_cursor_on_next_text_line(State)
for i=1,math.floor(-dy) do for i=1,math.floor(-dy) do
Text.down(State) Text.down(State)
end end

View File

@ -3,7 +3,7 @@ Text = {}
-- draw a line starting from startpos to screen at y between State.left and State.right -- draw a line starting from startpos to screen at y between State.left and State.right
-- return y for the next line, and position of start of final screen line drawn -- return y for the next line, and position of start of final screen line drawn
function Text.draw(State, line_index, y, startpos, hide_cursor) function Text.draw(State, line_index, y, startpos, hide_cursor, show_line_numbers)
local line = State.lines[line_index] local line = State.lines[line_index]
local line_cache = State.line_cache[line_index] local line_cache = State.line_cache[line_index]
line_cache.starty = y line_cache.starty = y
@ -12,8 +12,10 @@ function Text.draw(State, line_index, y, startpos, hide_cursor)
local final_screen_line_starting_pos = startpos -- track value to return local final_screen_line_starting_pos = startpos -- track value to return
Text.populate_screen_line_starting_pos(State, line_index) Text.populate_screen_line_starting_pos(State, line_index)
Text.populate_link_offsets(State, line_index) Text.populate_link_offsets(State, line_index)
App.color(Line_number_color) if show_line_numbers then
love.graphics.print(line_index, State.left-Line_number_width*App.width('m')+10,y) App.color(Line_number_color)
love.graphics.print(line_index, State.left-Line_number_width*App.width('m')+10,y)
end
initialize_color() initialize_color()
assert(#line_cache.screen_line_starting_pos >= 1) assert(#line_cache.screen_line_starting_pos >= 1)
for i=1,#line_cache.screen_line_starting_pos do for i=1,#line_cache.screen_line_starting_pos do