load in-memory data for a thread

Nothing displayed yet. I need to convert threads to Surface nodes.
This commit is contained in:
Kartik K. Agaram 2023-06-18 20:10:20 -07:00
parent 28a937a2e7
commit fd5ca6f3f8
18 changed files with 150 additions and 37 deletions

View File

@ -1,18 +1,8 @@
on.mouse_press = function(x,y, mouse_button)
if Cursor_node then
Cursor_node.show_cursor = nil
Cursor_node = nil
end
if mouse_press_consumed_by_any_button_handler(HUD, x,y, mouse_button) then
if mouse_press_consumed_by_any_button_handler(Global_state, x,y, mouse_button) then
return
end
local node = on_text(x,y)
if node then
-- position cursor in node
Cursor_node = node
edit.mouse_press(node.editor, x,y, mouse_button)
return
if Global_state.root then
mouse_press_on_surface(x,y, mouse_button)
end
-- pan surface
Pan = {x=Viewport.x+x/Viewport.zoom,y=Viewport.y+y/Viewport.zoom}
end

View File

@ -1,4 +1,11 @@
on.initialize = function()
Files = love.filesystem.getDirectoryItems('data')
for i=#Files,1,-1 do
if (not Files[i]:match('%.md$')) or Files[i]:match('%-%d+.md$') then
table.remove(Files, i)
end
end
table.sort(Files)
new_definition()
A()
end

View File

@ -14,8 +14,6 @@ on.keychord_press = function(chord, key)
-- reset zoom
Viewport.zoom = 1.0
B()
elseif chord == 'C-q' then
live.send_to_app('QUIT')
elseif Cursor_node then
local old_top = {line=Cursor_node.editor.screen_top1.line, pos=Cursor_node.editor.screen_top1.pos}
edit.keychord_press(Cursor_node.editor, chord, key)

View File

@ -1,25 +1,11 @@
on.draw = function()
for _,obj in ipairs(Surface) do
love.graphics.setColor(obj.r or 0, obj.g or 0, obj.b or 0)
if obj.type == 'rectangle' then
love.graphics.rectangle(obj.drawmode or 'fill', vx(obj.x),vy(obj.y), scale(obj.w),scale(obj.h), scale(obj.rx or 5), scale(obj.ry or 5))
elseif obj.type == 'line' then
love.graphics.line(unpack(obj.zdata))
elseif obj.type == 'circle' then
love.graphics.circle(obj.drawmode or 'fill', vx(obj.x), vy(obj.y), scale(obj.radius))
elseif obj.type == 'arc' then
love.graphics.arc(obj.drawmode or 'line', obj.arctype or 'open', vx(obj.x), vy(obj.y), scale(obj.radius), obj.angle1, obj.angle2, obj.segments)
elseif obj.type == 'ellipse' then
love.graphics.ellipse(obj.drawmode or 'fill', vx(obj.x), vy(obj.y), scale(obj.radiusx), scale(obj.radiusy))
elseif obj.type == 'bezier' then
love.graphics.line(unpack(obj.zdata))
elseif obj.type == 'text' then
if obj.w == nil then
love.graphics.draw(obj.text, vx(obj.x), vy(obj.y))
else
edit.draw(obj.editor, obj.fg, not obj.show_cursor)
end
end
Global_state.button_handlers = {}
local font = love.graphics.getFont()
font:setLineHeight(1.3)
if Global_state.root == nil then
-- TODO: use surface for file picker as well
draw_file_picker()
else
draw_surface()
end
draw_menu_bar()
end

View File

@ -6,6 +6,7 @@ draw_menu_bar = function()
love.graphics.rectangle('line', 0,0, App.screen.width, Menu_bar_height)
App.color(Menu_command_color)
Menu_cursor = 5
add_hotkey_to_menu('ctrl+o: switch file')
add_hotkey_to_menu('ctrl+f: find')
add_hotkey_to_menu('ctrl+left ctrl+right: prev/next word')
add_hotkey_to_menu('ctrl+z ctrl+y: undo/redo')

1
0111-Files Normal file
View File

@ -0,0 +1 @@
Files = nil -- array of filenames

3
0112-Global_state Normal file
View File

@ -0,0 +1,3 @@
Global_state = {}
-- button_handlers: for the file picker
-- root: when reading a single thread

25
0113-draw_surface Normal file
View File

@ -0,0 +1,25 @@
draw_surface = function()
for _,obj in ipairs(Surface) do
love.graphics.setColor(obj.r or 0, obj.g or 0, obj.b or 0)
if obj.type == 'rectangle' then
love.graphics.rectangle(obj.drawmode or 'fill', vx(obj.x),vy(obj.y), scale(obj.w),scale(obj.h), scale(obj.rx or 5), scale(obj.ry or 5))
elseif obj.type == 'line' then
love.graphics.line(unpack(obj.zdata))
elseif obj.type == 'circle' then
love.graphics.circle(obj.drawmode or 'fill', vx(obj.x), vy(obj.y), scale(obj.radius))
elseif obj.type == 'arc' then
love.graphics.arc(obj.drawmode or 'line', obj.arctype or 'open', vx(obj.x), vy(obj.y), scale(obj.radius), obj.angle1, obj.angle2, obj.segments)
elseif obj.type == 'ellipse' then
love.graphics.ellipse(obj.drawmode or 'fill', vx(obj.x), vy(obj.y), scale(obj.radiusx), scale(obj.radiusy))
elseif obj.type == 'bezier' then
love.graphics.line(unpack(obj.zdata))
elseif obj.type == 'text' then
if obj.w == nil then
love.graphics.draw(obj.text, vx(obj.x), vy(obj.y))
else
edit.draw(obj.editor, obj.fg, not obj.show_cursor)
end
end
end
draw_menu_bar()
end

25
0114-draw_file_picker Normal file
View File

@ -0,0 +1,25 @@
draw_file_picker = function()
local font = love.graphics.getFont()
local y, x = Margin_top, Margin_left
for _,f in ipairs(Files) do
local w = font:getWidth(f)
if x + w > App.screen.width then
y = y + font:getHeight()*font:getLineHeight() + 10
x = Margin_left
end
button(Global_state, f, {
x=x-5, y=y-2, w=w+10, h=font:getHeight()*font:getLineHeight()+4,
color={0.7,0.7,1.0},
icon=function(p)
App.color{r=0.4,g=0.4,b=0.7}
love.graphics.rectangle('line', p.x,p.y, p.w,p.h, 5,5)
App.color{r=0,g=0,b=0}
love.graphics.print(f, x,y)
end,
onpress1 = function()
open_thread(f)
end,
})
x = x + App.width(f) + 15
end
end

11
0115-update_font_size Normal file
View File

@ -0,0 +1,11 @@
update_font_size = function(n)
Font_height = n
love.graphics.setFont(love.graphics.newFont(Font_height))
local font = love.graphics.getFont()
font:setLineHeight(1.3)
Line_height = math.floor(Font_height*1.3)
Menu_bar_height = 5 + Line_height + 5
if Global_state.root then
edit.update_font_settings(Global_state, n)
end
end

View File

@ -0,0 +1,18 @@
mouse_press_on_surface = function(x,y, mouse_button)
if Cursor_node then
Cursor_node.show_cursor = nil
Cursor_node = nil
end
if mouse_press_consumed_by_any_button_handler(HUD, x,y, mouse_button) then
return
end
local node = on_text(x,y)
if node then
-- position cursor in node
Cursor_node = node
edit.mouse_press(node.editor, x,y, mouse_button)
return
end
-- pan surface
Pan = {x=Viewport.x+x/Viewport.zoom,y=Viewport.y+y/Viewport.zoom}
end

9
0117-open_thread Normal file
View File

@ -0,0 +1,9 @@
open_thread = function(filename)
local font = love.graphics.getFont()
local em_width = font:getWidth('m')
Global_state.root = load_subtree(filename, Margin_left, em_width)
Global_state.root.top = Menu_bar_height + Margin_top
-- HERE: Cursor_node needs to be set at the Surface level of abstraction
--Cursor_node = Global_state.root
love.window.setTitle('pothi.love - '..Global_state.root.id)
end

11
0118-load_subtree Normal file
View File

@ -0,0 +1,11 @@
load_subtree = function(filename, left, em_width)
-- load a file and recursively all replies to it
-- print('load_subtree', filename)
local state = initialize_item(0, left, left+40*em_width, filename)
local replies = load_metadata(filename).replies
for i,reply_id in ipairs(replies) do
local reply = load_subtree(reply_id, left+2*em_width, em_width)
table.insert(state.replies, reply)
end
return state
end

8
0119-load_metadata Normal file
View File

@ -0,0 +1,8 @@
load_metadata = function(filename)
local mfile = metadata_file(filename)
local mpath = save_dir_path(mfile)
if not love.filesystem.getInfo(mpath) then
return {replies={}}
end
return json.decode(love.filesystem.read(mpath))
end

11
0120-initialize_item Normal file
View File

@ -0,0 +1,11 @@
initialize_item = function(top, left, right, filename)
local font = love.graphics.getFont()
local result = edit.initialize_state(top, left, right, font:getHeight(), font:getHeight()*font:getLineHeight())
result.id = filename
result.filename = full_path(filename)
result.editable = false
result.replies = {}
load_from_disk(result)
Text.redraw_all(result)
return result
end

3
0121-full_path Normal file
View File

@ -0,0 +1,3 @@
full_path = function(id)
return love.filesystem.getSaveDirectory()..'/data/'..id
end

3
0122-metadata_file Normal file
View File

@ -0,0 +1,3 @@
metadata_file = function(filename)
return filename:gsub('%.md$', '')..'.json'
end

3
0123-save_dir_path Normal file
View File

@ -0,0 +1,3 @@
save_dir_path = function(id)
return 'data/'..id
end