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)
move_candidate_to_front(s)
local candidate = guess_source(s..'.lua')
source.switch_to_file(candidate)
source.switch_to_file(s..'.lua')
love.window.setTitle('lines.love - source - '..Editor_state.filename)
reset_file_navigator()
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
return
end
local r = round(geom.dist(center.x, center.y, mx, my))
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
local center = drawing.points[shape.center]
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
local drawing = State.lines.current_drawing
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')
local pmx, pmy = App.mouse_x(), App.mouse_y()
local mx = Drawing.coord(pmx-State.left, State.width)

View File

@ -233,11 +233,13 @@ end
function edit.mouse_wheel_move(State, dx,dy)
if dy > 0 then
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
Text.up(State)
end
elseif dy < 0 then
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
Text.down(State)
end

View File

@ -35,7 +35,6 @@ function log_browser.parse(State)
if rest then
line.data = rest
end
line.filename = guess_source(line.filename)
line.line_number = tonumber(line.line_number)
if line.data:sub(1,1) == '{' then
local data = json.decode(line.data)
@ -75,15 +74,6 @@ function table.shallowcopy(x)
return {unpack(x)}
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)
assert(#State.lines == #State.line_cache)
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
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
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
-- divider
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,
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),
width = right-left,
@ -115,7 +115,7 @@ function edit.check_locs(State)
or not edit.cursor_on_text(State)
or not Text.le1(State.screen_top1, State.cursor1) then
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
@ -131,16 +131,20 @@ function edit.cursor_on_text(State)
and State.lines[State.cursor1.line].mode == 'text'
end
function edit.put_cursor_on_first_text_line(State)
for i,line in ipairs(State.lines) do
if line.mode == 'text' then
State.cursor1 = {line=i, pos=1}
break
end
function edit.put_cursor_on_next_text_line(State)
while true do
if State.cursor1.line >= #State.lines then
break
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
function edit.draw(State, hide_cursor)
function edit.draw(State, hide_cursor, show_line_numbers)
State.button_handlers = {}
App.color(Text_color)
if #State.lines ~= #State.line_cache then
@ -169,7 +173,11 @@ function edit.draw(State, hide_cursor)
end
if line.data == '' then
-- 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,
onpress1 = function()
Drawing.before = snapshot(State, line_index-1, line_index)
@ -183,7 +191,7 @@ function edit.draw(State, hide_cursor)
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)
elseif line.mode == 'drawing' then
y = y+Drawing_padding_top
@ -329,11 +337,13 @@ end
function edit.mouse_wheel_move(State, dx,dy)
if dy > 0 then
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
Text.up(State)
end
elseif dy < 0 then
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
Text.down(State)
end

View File

@ -3,7 +3,7 @@ Text = {}
-- 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
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_cache = State.line_cache[line_index]
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
Text.populate_screen_line_starting_pos(State, line_index)
Text.populate_link_offsets(State, line_index)
App.color(Line_number_color)
love.graphics.print(line_index, State.left-Line_number_width*App.width('m')+10,y)
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)
end
initialize_color()
assert(#line_cache.screen_line_starting_pos >= 1)
for i=1,#line_cache.screen_line_starting_pos do