switch representation to simpler table of tables

This commit is contained in:
Kartik K. Agaram 2023-10-16 19:41:14 -07:00
parent 60cfabe9c3
commit d50960e61f
7 changed files with 37 additions and 18 deletions

View File

@ -1,3 +1,6 @@
on.draw = function()
draw_table({{'X', 2}, {0, 'X'}, rows={'Ind', 'NZ'}})
draw_table({
Ind={Ind='X', NZ=2},
NZ={Ind=0, NZ='X'}
})
end

View File

@ -1,16 +1,17 @@
-- render a square table (array of arrays) nicely
-- t contains a list of row/col names in key 'rows'
-- render a square table (table of tables) nicely
draw_table = function(t)
love.graphics.setLineWidth(3)
local f, c, py = 36, 50, 5 -- font size, cell side, padding
local namepx = 5
love.graphics.setFont(love.graphics.newFont(f))
local rside = max_row_width(t) + namepx*2 -- width of name column
local hrow = total_row_width(t) + namepx*2*#t
local nt = table.size(t)
local hrow = total_row_width(t) + namepx*2*nt
local width = rside + hrow
local height = (#t+1)*c
local height = (nt+1)*c
local top, left = 30,30
local cx = column_xs(t, left+rside, namepx) -- right margin of each column
local rows = ordered_keys(t)
local cx = column_xs(rows, left+rside, namepx) -- right margin of each column
-- border
love.graphics.rectangle('line', left,top, width,height, 5,5)
do
@ -23,20 +24,21 @@ draw_table = function(t)
end
end
-- rows
for i=1,#t do
for i=1,#rows do
love.graphics.line(left, top+i*c, left+width, top+i*c)
i = i+1
end
-- print out row headings
for i,name in ipairs(t.rows) do
for i,name in ipairs(rows) do
love.graphics.print(name, left+namepx, top+i*c+py)
end
-- print out column headings
local l = left+rside
for i=1,#t do
for i=1,#rows do
local r = cx[i]
local w = r-l
local px = (w - App.width(t.rows[i])) / 2 -- x padding
love.graphics.print(t.rows[i], l+px, top+py)
local px = (w - App.width(rows[i])) / 2 -- x padding
love.graphics.print(rows[i], l+px, top+py)
l = r
end
-- print out cells

View File

@ -1,8 +1,8 @@
max_row_width = function(t)
local max = 0
for _, name in ipairs(t.rows) do
for name in pairs(t) do
local curr = App.width(name)
if max < curr then max = curr end
end
return max
end
end

View File

@ -1,7 +1,7 @@
total_row_width = function(t)
local result = 0
for _, name in ipairs(t.rows) do
for name in pairs(t) do
result = result + App.width(name)
end
return result
end
end

View File

@ -1,8 +1,8 @@
-- right margin of each column in 't', depending on the widths of names in 't.rows'
column_xs = function(t, init, px)
-- right margin of each name in 't', depending on the widths of names in 't.rows'
column_xs = function(rows, init, px)
local result = {}
local x = init
for _, name in ipairs(t.rows) do
for _,name in ipairs(rows) do
x = x + App.width(name) + px*2
table.insert(result, x)
end

7
0008-table.size Normal file
View File

@ -0,0 +1,7 @@
table.size = function(t)
local result = 0
for _ in pairs(t) do
result = result+1
end
return result
end

7
0009-ordered_keys Normal file
View File

@ -0,0 +1,7 @@
ordered_keys = function(t)
local result = {}
for k in pairs(t) do
table.insert(result, k)
end
return result
end